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 | |
| 26 | #define _GNU_SOURCE |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 27 | #include <ctype.h> |
| 28 | #include <dirent.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <unistd.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 35 | |
| 36 | #include <sys/socket.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 39 | #include <netdb.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 40 | #include <netinet/tcp.h> |
| 41 | |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 42 | #include <openssl/bn.h> |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 43 | #include <openssl/crypto.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 44 | #include <openssl/ssl.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 45 | #include <openssl/x509.h> |
| 46 | #include <openssl/x509v3.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 47 | #include <openssl/err.h> |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 48 | #include <openssl/rand.h> |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 49 | #include <openssl/hmac.h> |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 50 | #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] | 51 | #include <openssl/ocsp.h> |
| 52 | #endif |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 53 | #ifndef OPENSSL_NO_DH |
| 54 | #include <openssl/dh.h> |
| 55 | #endif |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 56 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 57 | #include <openssl/engine.h> |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 58 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 59 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 60 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 61 | #include <openssl/async.h> |
| 62 | #endif |
| 63 | |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 64 | #ifndef OPENSSL_VERSION |
| 65 | #define OPENSSL_VERSION SSLEAY_VERSION |
| 66 | #define OpenSSL_version(x) SSLeay_version(x) |
| 67 | #define OpenSSL_version_num SSLeay |
| 68 | #endif |
| 69 | |
| 70 | #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER < 0x20700000L) |
| 71 | #define X509_getm_notBefore X509_get_notBefore |
| 72 | #define X509_getm_notAfter X509_get_notAfter |
| 73 | #endif |
| 74 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 75 | #include <import/lru.h> |
| 76 | #include <import/xxhash.h> |
| 77 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 78 | #include <common/buffer.h> |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 79 | #include <common/chunk.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 80 | #include <common/compat.h> |
| 81 | #include <common/config.h> |
| 82 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 83 | #include <common/errors.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 84 | #include <common/initcall.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 85 | #include <common/standard.h> |
| 86 | #include <common/ticks.h> |
| 87 | #include <common/time.h> |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 88 | #include <common/cfgparse.h> |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 89 | #include <common/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 90 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 91 | #include <ebsttree.h> |
| 92 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 93 | #include <types/applet.h> |
| 94 | #include <types/cli.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 95 | #include <types/global.h> |
| 96 | #include <types/ssl_sock.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 97 | #include <types/stats.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 98 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 99 | #include <proto/acl.h> |
| 100 | #include <proto/arg.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 101 | #include <proto/channel.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 102 | #include <proto/connection.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 103 | #include <proto/cli.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 104 | #include <proto/fd.h> |
| 105 | #include <proto/freq_ctr.h> |
| 106 | #include <proto/frontend.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 107 | #include <proto/http_rules.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 108 | #include <proto/listener.h> |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 109 | #include <proto/openssl-compat.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 110 | #include <proto/pattern.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 111 | #include <proto/proto_tcp.h> |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 112 | #include <proto/proto_http.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 113 | #include <proto/server.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 114 | #include <proto/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 115 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 116 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 117 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 118 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 119 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 120 | #include <proto/task.h> |
| 121 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 122 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 123 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 124 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 125 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 126 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 127 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 128 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 129 | |
| 130 | /* Verify errors macros */ |
| 131 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 132 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 133 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 134 | |
| 135 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 136 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 137 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 138 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 139 | /* Supported hash function for TLS tickets */ |
| 140 | #ifdef OPENSSL_NO_SHA256 |
| 141 | #define HASH_FUNCT EVP_sha1 |
| 142 | #else |
| 143 | #define HASH_FUNCT EVP_sha256 |
| 144 | #endif /* OPENSSL_NO_SHA256 */ |
| 145 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 146 | /* ssl_methods flags for ssl options */ |
| 147 | #define MC_SSL_O_ALL 0x0000 |
| 148 | #define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */ |
| 149 | #define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */ |
| 150 | #define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */ |
| 151 | #define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 152 | #define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 153 | |
| 154 | /* ssl_methods versions */ |
| 155 | enum { |
| 156 | CONF_TLSV_NONE = 0, |
| 157 | CONF_TLSV_MIN = 1, |
| 158 | CONF_SSLV3 = 1, |
| 159 | CONF_TLSV10 = 2, |
| 160 | CONF_TLSV11 = 3, |
| 161 | CONF_TLSV12 = 4, |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 162 | CONF_TLSV13 = 5, |
| 163 | CONF_TLSV_MAX = 5, |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 164 | }; |
| 165 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 166 | /* server and bind verify method, it uses a global value as default */ |
| 167 | enum { |
| 168 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 169 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 170 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 171 | SSL_SOCK_VERIFY_NONE = 3, |
| 172 | }; |
| 173 | |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 174 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 175 | int sslconns = 0; |
| 176 | int totalsslconns = 0; |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 177 | static struct xprt_ops ssl_sock; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 178 | int nb_engines = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 179 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 180 | static struct { |
| 181 | char *crt_base; /* base directory path for certificates */ |
| 182 | char *ca_base; /* base directory path for CAs and CRLs */ |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 183 | int async; /* whether we use ssl async mode */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 184 | |
| 185 | char *listen_default_ciphers; |
| 186 | char *connect_default_ciphers; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 187 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 188 | char *listen_default_ciphersuites; |
| 189 | char *connect_default_ciphersuites; |
| 190 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 191 | int listen_default_ssloptions; |
| 192 | int connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 193 | struct tls_version_filter listen_default_sslmethods; |
| 194 | struct tls_version_filter connect_default_sslmethods; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 195 | |
| 196 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 197 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 198 | unsigned int max_record; /* SSL max record size */ |
| 199 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 200 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 201 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 202 | } global_ssl = { |
| 203 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 204 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 205 | #endif |
| 206 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 207 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 208 | #endif |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 209 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 210 | #ifdef LISTEN_DEFAULT_CIPHERSUITES |
| 211 | .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES, |
| 212 | #endif |
| 213 | #ifdef CONNECT_DEFAULT_CIPHERSUITES |
| 214 | .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES, |
| 215 | #endif |
| 216 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 217 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 218 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 219 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 220 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 221 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 222 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 223 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 224 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 225 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 226 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 227 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 228 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 229 | #endif |
| 230 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 231 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 232 | .capture_cipherlist = 0, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 233 | }; |
| 234 | |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 235 | #if defined(USE_THREAD) && ((OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 236 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 237 | static HA_RWLOCK_T *ssl_rwlocks; |
| 238 | |
| 239 | |
| 240 | unsigned long ssl_id_function(void) |
| 241 | { |
| 242 | return (unsigned long)tid; |
| 243 | } |
| 244 | |
| 245 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 246 | { |
| 247 | if (mode & CRYPTO_LOCK) { |
| 248 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 249 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 250 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 251 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 252 | } |
| 253 | else { |
| 254 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 255 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 256 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 257 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | static int ssl_locking_init(void) |
| 262 | { |
| 263 | int i; |
| 264 | |
| 265 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 266 | if (!ssl_rwlocks) |
| 267 | return -1; |
| 268 | |
| 269 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 270 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 271 | |
| 272 | CRYPTO_set_id_callback(ssl_id_function); |
| 273 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 274 | |
| 275 | return 0; |
| 276 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 277 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 278 | #endif |
| 279 | |
| 280 | |
| 281 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 282 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 283 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 284 | unsigned long long int xxh64; |
| 285 | unsigned char ciphersuite_len; |
| 286 | char ciphersuite[0]; |
| 287 | }; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 288 | struct pool_head *pool_head_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 289 | static int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 290 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 291 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 292 | static int ssl_pkey_info_index = -1; |
| 293 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 294 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 295 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 296 | #endif |
| 297 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 298 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 299 | static unsigned int openssl_engines_initialized; |
| 300 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 301 | struct ssl_engine_list { |
| 302 | struct list list; |
| 303 | ENGINE *e; |
| 304 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 305 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 306 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 307 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 308 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 309 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 310 | static DH *local_dh_1024 = NULL; |
| 311 | static DH *local_dh_2048 = NULL; |
| 312 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 313 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 314 | #endif /* OPENSSL_NO_DH */ |
| 315 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 316 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 317 | /* X509V3 Extensions that will be added on generated certificates */ |
| 318 | #define X509V3_EXT_SIZE 5 |
| 319 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 320 | "basicConstraints", |
| 321 | "nsComment", |
| 322 | "subjectKeyIdentifier", |
| 323 | "authorityKeyIdentifier", |
| 324 | "keyUsage", |
| 325 | }; |
| 326 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 327 | "CA:FALSE", |
| 328 | "\"OpenSSL Generated Certificate\"", |
| 329 | "hash", |
| 330 | "keyid,issuer:always", |
| 331 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 332 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 333 | /* LRU cache to store generated certificate */ |
| 334 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 335 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 336 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 337 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 338 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 339 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 340 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 341 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 342 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 343 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 344 | /* The order here matters for picking a default context, |
| 345 | * keep the most common keytype at the bottom of the list |
| 346 | */ |
| 347 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 348 | "dsa", |
| 349 | "ecdsa", |
| 350 | "rsa" |
| 351 | }; |
| 352 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 353 | #else |
| 354 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 355 | #endif |
| 356 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 357 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 358 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 359 | |
| 360 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 361 | |
| 362 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 363 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 364 | |
| 365 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 366 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 367 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 368 | /* |
| 369 | * This function gives the detail of the SSL error. It is used only |
| 370 | * if the debug mode and the verbose mode are activated. It dump all |
| 371 | * the SSL error until the stack was empty. |
| 372 | */ |
| 373 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 374 | { |
| 375 | unsigned long ret; |
| 376 | |
| 377 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 378 | while(1) { |
| 379 | ret = ERR_get_error(); |
| 380 | if (ret == 0) |
| 381 | return; |
| 382 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 383 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 384 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 389 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 390 | /* |
| 391 | * struct alignment works here such that the key.key is the same as key_data |
| 392 | * Do not change the placement of key_data |
| 393 | */ |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 394 | struct certificate_ocsp { |
| 395 | struct ebmb_node key; |
| 396 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 397 | struct buffer response; |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 398 | long expire; |
| 399 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 400 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 401 | struct ocsp_cbk_arg { |
| 402 | int is_single; |
| 403 | int single_kt; |
| 404 | union { |
| 405 | struct certificate_ocsp *s_ocsp; |
| 406 | /* |
| 407 | * m_ocsp will have multiple entries dependent on key type |
| 408 | * Entry 0 - DSA |
| 409 | * Entry 1 - ECDSA |
| 410 | * Entry 2 - RSA |
| 411 | */ |
| 412 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 413 | }; |
| 414 | }; |
| 415 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 416 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 417 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 418 | { |
| 419 | int err_code = ERR_ABORT; |
| 420 | ENGINE *engine; |
| 421 | struct ssl_engine_list *el; |
| 422 | |
| 423 | /* grab the structural reference to the engine */ |
| 424 | engine = ENGINE_by_id(engine_id); |
| 425 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 426 | 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] | 427 | goto fail_get; |
| 428 | } |
| 429 | |
| 430 | if (!ENGINE_init(engine)) { |
| 431 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 432 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 433 | goto fail_init; |
| 434 | } |
| 435 | |
| 436 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 437 | 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] | 438 | goto fail_set_method; |
| 439 | } |
| 440 | |
| 441 | el = calloc(1, sizeof(*el)); |
| 442 | el->e = engine; |
| 443 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 444 | nb_engines++; |
| 445 | if (global_ssl.async) |
| 446 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 447 | return 0; |
| 448 | |
| 449 | fail_set_method: |
| 450 | /* release the functional reference from ENGINE_init() */ |
| 451 | ENGINE_finish(engine); |
| 452 | |
| 453 | fail_init: |
| 454 | /* release the structural reference from ENGINE_by_id() */ |
| 455 | ENGINE_free(engine); |
| 456 | |
| 457 | fail_get: |
| 458 | return err_code; |
| 459 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 460 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 461 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 462 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 463 | /* |
| 464 | * openssl async fd handler |
| 465 | */ |
| 466 | static void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 467 | { |
| 468 | struct connection *conn = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 469 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 470 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 471 | * to poll this fd until it is requested |
| 472 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 473 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 474 | fd_cant_recv(fd); |
| 475 | |
| 476 | /* crypto engine is available, let's notify the associated |
| 477 | * connection that it can pursue its processing. |
| 478 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 479 | __conn_sock_want_recv(conn); |
| 480 | __conn_sock_want_send(conn); |
| 481 | conn_update_sock_polling(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 484 | /* |
| 485 | * openssl async delayed SSL_free handler |
| 486 | */ |
| 487 | static void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 488 | { |
| 489 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 490 | OSSL_ASYNC_FD all_fd[32]; |
| 491 | size_t num_all_fds = 0; |
| 492 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 493 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 494 | /* We suppose that the async job for a same SSL * |
| 495 | * are serialized. So if we are awake it is |
| 496 | * because the running job has just finished |
| 497 | * and we can remove all async fds safely |
| 498 | */ |
| 499 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 500 | if (num_all_fds > 32) { |
| 501 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 506 | for (i=0 ; i < num_all_fds ; i++) |
| 507 | fd_remove(all_fd[i]); |
| 508 | |
| 509 | /* 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] | 510 | SSL_free(ssl); |
Emeric Brun | 7ad43e7 | 2018-10-10 14:51:02 +0200 | [diff] [blame] | 511 | HA_ATOMIC_SUB(&sslconns, 1); |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 512 | HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 513 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 514 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 515 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 516 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 517 | */ |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 518 | static void inline ssl_async_process_fds(struct connection *conn, SSL *ssl) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 519 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 520 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 521 | OSSL_ASYNC_FD del_fd[32]; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 522 | size_t num_add_fds = 0; |
| 523 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 524 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 525 | |
| 526 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 527 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 528 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 529 | 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] | 530 | return; |
| 531 | } |
| 532 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 533 | 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] | 534 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 535 | /* We remove unused fds from the fdtab */ |
| 536 | for (i=0 ; i < num_del_fds ; i++) |
| 537 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 538 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 539 | /* We add new fds to the fdtab */ |
| 540 | for (i=0 ; i < num_add_fds ; i++) { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 541 | fd_insert(add_fd[i], conn, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 544 | num_add_fds = 0; |
| 545 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 546 | if (num_add_fds > 32) { |
| 547 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 548 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 549 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 550 | |
| 551 | /* We activate the polling for all known async fds */ |
| 552 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 553 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 554 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 555 | /* To ensure that the fd cache won't be used |
| 556 | * We'll prefer to catch a real RD event |
| 557 | * because handling an EAGAIN on this fd will |
| 558 | * result in a context switch and also |
| 559 | * some engines uses a fd in blocking mode. |
| 560 | */ |
| 561 | fd_cant_recv(add_fd[i]); |
| 562 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 563 | |
| 564 | /* We must also prevent the conn_handler |
| 565 | * to be called until a read event was |
| 566 | * polled on an async fd |
| 567 | */ |
| 568 | __conn_sock_stop_both(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 569 | } |
| 570 | #endif |
| 571 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 572 | /* |
| 573 | * This function returns the number of seconds elapsed |
| 574 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 575 | * date presented un ASN1_GENERALIZEDTIME. |
| 576 | * |
| 577 | * In parsing error case, it returns -1. |
| 578 | */ |
| 579 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 580 | { |
| 581 | long epoch; |
| 582 | char *p, *end; |
| 583 | const unsigned short month_offset[12] = { |
| 584 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 585 | }; |
| 586 | int year, month; |
| 587 | |
| 588 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 589 | |
| 590 | p = (char *)d->data; |
| 591 | end = p + d->length; |
| 592 | |
| 593 | if (end - p < 4) return -1; |
| 594 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 595 | p += 4; |
| 596 | if (end - p < 2) return -1; |
| 597 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 598 | if (month < 1 || month > 12) return -1; |
| 599 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 600 | We consider leap years and the current month (<marsh or not) */ |
| 601 | epoch = ( ((year - 1970) * 365) |
| 602 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 603 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 604 | + month_offset[month-1] |
| 605 | ) * 24 * 60 * 60; |
| 606 | p += 2; |
| 607 | if (end - p < 2) return -1; |
| 608 | /* Add the number of seconds of completed days of current month */ |
| 609 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 610 | p += 2; |
| 611 | if (end - p < 2) return -1; |
| 612 | /* Add the completed hours of the current day */ |
| 613 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 614 | p += 2; |
| 615 | if (end - p < 2) return -1; |
| 616 | /* Add the completed minutes of the current hour */ |
| 617 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 618 | p += 2; |
| 619 | if (p == end) return -1; |
| 620 | /* Test if there is available seconds */ |
| 621 | if (p[0] < '0' || p[0] > '9') |
| 622 | goto nosec; |
| 623 | if (end - p < 2) return -1; |
| 624 | /* Add the seconds of the current minute */ |
| 625 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 626 | p += 2; |
| 627 | if (p == end) return -1; |
| 628 | /* Ignore seconds float part if present */ |
| 629 | if (p[0] == '.') { |
| 630 | do { |
| 631 | if (++p == end) return -1; |
| 632 | } while (p[0] >= '0' && p[0] <= '9'); |
| 633 | } |
| 634 | |
| 635 | nosec: |
| 636 | if (p[0] == 'Z') { |
| 637 | if (end - p != 1) return -1; |
| 638 | return epoch; |
| 639 | } |
| 640 | else if (p[0] == '+') { |
| 641 | if (end - p != 5) return -1; |
| 642 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 643 | 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] | 644 | } |
| 645 | else if (p[0] == '-') { |
| 646 | if (end - p != 5) return -1; |
| 647 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 648 | 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] | 649 | } |
| 650 | |
| 651 | return -1; |
| 652 | } |
| 653 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 654 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 655 | |
| 656 | /* This function starts to check if the OCSP response (in DER format) contained |
| 657 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 658 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 659 | * contained in the OCSP Response and exits on error if no match. |
| 660 | * If it's a valid OCSP Response: |
| 661 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 662 | * pointed by 'ocsp'. |
| 663 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 664 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 665 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 666 | * already present in the container, it will be overwritten. |
| 667 | * |
| 668 | * Note: OCSP response containing more than one OCSP Single response is not |
| 669 | * considered valid. |
| 670 | * |
| 671 | * Returns 0 on success, 1 in error case. |
| 672 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 673 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 674 | struct certificate_ocsp *ocsp, |
| 675 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 676 | { |
| 677 | OCSP_RESPONSE *resp; |
| 678 | OCSP_BASICRESP *bs = NULL; |
| 679 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 680 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 681 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 682 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 683 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 684 | int reason; |
| 685 | int ret = 1; |
| 686 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 687 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 688 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 689 | if (!resp) { |
| 690 | memprintf(err, "Unable to parse OCSP response"); |
| 691 | goto out; |
| 692 | } |
| 693 | |
| 694 | rc = OCSP_response_status(resp); |
| 695 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 696 | memprintf(err, "OCSP response status not successful"); |
| 697 | goto out; |
| 698 | } |
| 699 | |
| 700 | bs = OCSP_response_get1_basic(resp); |
| 701 | if (!bs) { |
| 702 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 703 | goto out; |
| 704 | } |
| 705 | |
| 706 | count_sr = OCSP_resp_count(bs); |
| 707 | if (count_sr > 1) { |
| 708 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 709 | goto out; |
| 710 | } |
| 711 | |
| 712 | sr = OCSP_resp_get0(bs, 0); |
| 713 | if (!sr) { |
| 714 | memprintf(err, "Failed to get OCSP single response"); |
| 715 | goto out; |
| 716 | } |
| 717 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 718 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 719 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 720 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 721 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 722 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 723 | goto out; |
| 724 | } |
| 725 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 726 | if (!nextupd) { |
| 727 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 728 | goto out; |
| 729 | } |
| 730 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 731 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 732 | if (!rc) { |
| 733 | memprintf(err, "OCSP single response: no longer valid."); |
| 734 | goto out; |
| 735 | } |
| 736 | |
| 737 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 738 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 739 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 740 | goto out; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | if (!ocsp) { |
| 745 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 746 | unsigned char *p; |
| 747 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 748 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 749 | if (!rc) { |
| 750 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 751 | goto out; |
| 752 | } |
| 753 | |
| 754 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 755 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 756 | goto out; |
| 757 | } |
| 758 | |
| 759 | p = key; |
| 760 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 761 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 762 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 763 | if (!ocsp) { |
| 764 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 765 | goto out; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /* According to comments on "chunk_dup", the |
| 770 | previous chunk buffer will be freed */ |
| 771 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 772 | memprintf(err, "OCSP response: Memory allocation error"); |
| 773 | goto out; |
| 774 | } |
| 775 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 776 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 777 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 778 | ret = 0; |
| 779 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 780 | ERR_clear_error(); |
| 781 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 782 | if (bs) |
| 783 | OCSP_BASICRESP_free(bs); |
| 784 | |
| 785 | if (resp) |
| 786 | OCSP_RESPONSE_free(resp); |
| 787 | |
| 788 | return ret; |
| 789 | } |
| 790 | /* |
| 791 | * External function use to update the OCSP response in the OCSP response's |
| 792 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 793 | * to update in DER format. |
| 794 | * |
| 795 | * Returns 0 on success, 1 in error case. |
| 796 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 797 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 798 | { |
| 799 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 800 | } |
| 801 | |
| 802 | /* |
| 803 | * This function load the OCSP Resonse in DER format contained in file at |
| 804 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 805 | * |
| 806 | * Returns 0 on success, 1 in error case. |
| 807 | */ |
| 808 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 809 | { |
| 810 | int fd = -1; |
| 811 | int r = 0; |
| 812 | int ret = 1; |
| 813 | |
| 814 | fd = open(ocsp_path, O_RDONLY); |
| 815 | if (fd == -1) { |
| 816 | memprintf(err, "Error opening OCSP response file"); |
| 817 | goto end; |
| 818 | } |
| 819 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 820 | trash.data = 0; |
| 821 | while (trash.data < trash.size) { |
| 822 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 823 | if (r < 0) { |
| 824 | if (errno == EINTR) |
| 825 | continue; |
| 826 | |
| 827 | memprintf(err, "Error reading OCSP response from file"); |
| 828 | goto end; |
| 829 | } |
| 830 | else if (r == 0) { |
| 831 | break; |
| 832 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 833 | trash.data += r; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | close(fd); |
| 837 | fd = -1; |
| 838 | |
| 839 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 840 | end: |
| 841 | if (fd != -1) |
| 842 | close(fd); |
| 843 | |
| 844 | return ret; |
| 845 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 846 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 847 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 848 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 849 | 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) |
| 850 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 851 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 852 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 853 | struct connection *conn; |
| 854 | int head; |
| 855 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 856 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 857 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 858 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 859 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 860 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 861 | |
| 862 | keys = ref->tlskeys; |
| 863 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 864 | |
| 865 | if (enc) { |
| 866 | memcpy(key_name, keys[head].name, 16); |
| 867 | |
| 868 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 869 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 870 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 871 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 872 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 873 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 874 | goto end; |
| 875 | |
| 876 | HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, HASH_FUNCT(), NULL); |
| 877 | ret = 1; |
| 878 | } |
| 879 | else if (ref->key_size_bits == 256 ) { |
| 880 | |
| 881 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 882 | goto end; |
| 883 | |
| 884 | HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, HASH_FUNCT(), NULL); |
| 885 | ret = 1; |
| 886 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 887 | } else { |
| 888 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 889 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 890 | goto found; |
| 891 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 892 | ret = 0; |
| 893 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 894 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 895 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 896 | if (ref->key_size_bits == 128) { |
| 897 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, HASH_FUNCT(), NULL); |
| 898 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 899 | goto end; |
| 900 | /* 2 for key renewal, 1 if current key is still valid */ |
| 901 | ret = i ? 2 : 1; |
| 902 | } |
| 903 | else if (ref->key_size_bits == 256) { |
| 904 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, HASH_FUNCT(), NULL); |
| 905 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 906 | goto end; |
| 907 | /* 2 for key renewal, 1 if current key is still valid */ |
| 908 | ret = i ? 2 : 1; |
| 909 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 910 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 911 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 912 | end: |
| 913 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 914 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 918 | { |
| 919 | struct tls_keys_ref *ref; |
| 920 | |
| 921 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 922 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 923 | return ref; |
| 924 | return NULL; |
| 925 | } |
| 926 | |
| 927 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 928 | { |
| 929 | struct tls_keys_ref *ref; |
| 930 | |
| 931 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 932 | if (ref->unique_id == unique_id) |
| 933 | return ref; |
| 934 | return NULL; |
| 935 | } |
| 936 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 937 | /* Update the key into ref: if keysize doesnt |
| 938 | * match existing ones, this function returns -1 |
| 939 | * else it returns 0 on success. |
| 940 | */ |
| 941 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 942 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 943 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 944 | if (ref->key_size_bits == 128) { |
| 945 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 946 | return -1; |
| 947 | } |
| 948 | else if (ref->key_size_bits == 256) { |
| 949 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 950 | return -1; |
| 951 | } |
| 952 | else |
| 953 | return -1; |
| 954 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 955 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 956 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 957 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 958 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 959 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 960 | |
| 961 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 962 | } |
| 963 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 964 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 965 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 966 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 967 | |
| 968 | if(!ref) { |
| 969 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 970 | return 1; |
| 971 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 972 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 973 | memprintf(err, "Invalid key size"); |
| 974 | return 1; |
| 975 | } |
| 976 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 977 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 978 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 979 | |
| 980 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 981 | * automatic ids. It's called just after the basic checks. It returns |
| 982 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 983 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 984 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 985 | { |
| 986 | int i = 0; |
| 987 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 988 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 989 | |
| 990 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 991 | if (ref->unique_id == -1) { |
| 992 | /* Look for the first free id. */ |
| 993 | while (1) { |
| 994 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 995 | if (ref2->unique_id == i) { |
| 996 | i++; |
| 997 | break; |
| 998 | } |
| 999 | } |
| 1000 | if (&ref2->list == &tlskeys_reference) |
| 1001 | break; |
| 1002 | } |
| 1003 | |
| 1004 | /* Uses the unique id and increment it for the next entry. */ |
| 1005 | ref->unique_id = i; |
| 1006 | i++; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /* This sort the reference list by id. */ |
| 1011 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1012 | LIST_DEL(&ref->list); |
| 1013 | list_for_each_entry(ref3, &tkr, list) { |
| 1014 | if (ref->unique_id < ref3->unique_id) { |
| 1015 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | if (&ref3->list == &tkr) |
| 1020 | LIST_ADDQ(&tkr, &ref->list); |
| 1021 | } |
| 1022 | |
| 1023 | /* swap root */ |
| 1024 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1025 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1026 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1027 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1028 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1029 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1030 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1031 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1032 | { |
| 1033 | switch (evp_keytype) { |
| 1034 | case EVP_PKEY_RSA: |
| 1035 | return 2; |
| 1036 | case EVP_PKEY_DSA: |
| 1037 | return 0; |
| 1038 | case EVP_PKEY_EC: |
| 1039 | return 1; |
| 1040 | } |
| 1041 | |
| 1042 | return -1; |
| 1043 | } |
| 1044 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1045 | /* |
| 1046 | * Callback used to set OCSP status extension content in server hello. |
| 1047 | */ |
| 1048 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1049 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1050 | struct certificate_ocsp *ocsp; |
| 1051 | struct ocsp_cbk_arg *ocsp_arg; |
| 1052 | char *ssl_buf; |
| 1053 | EVP_PKEY *ssl_pkey; |
| 1054 | int key_type; |
| 1055 | int index; |
| 1056 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1057 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1058 | |
| 1059 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1060 | if (!ssl_pkey) |
| 1061 | return SSL_TLSEXT_ERR_NOACK; |
| 1062 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1063 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1064 | |
| 1065 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1066 | ocsp = ocsp_arg->s_ocsp; |
| 1067 | else { |
| 1068 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1069 | * the certificate type |
| 1070 | */ |
| 1071 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1072 | |
| 1073 | if (index < 0) |
| 1074 | return SSL_TLSEXT_ERR_NOACK; |
| 1075 | |
| 1076 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1077 | |
| 1078 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1079 | |
| 1080 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1081 | !ocsp->response.area || |
| 1082 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1083 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1084 | return SSL_TLSEXT_ERR_NOACK; |
| 1085 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1086 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1087 | if (!ssl_buf) |
| 1088 | return SSL_TLSEXT_ERR_NOACK; |
| 1089 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1090 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1091 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1092 | |
| 1093 | return SSL_TLSEXT_ERR_OK; |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 1098 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 1099 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 1100 | * It should be present in the certificate's extra chain builded from file |
| 1101 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 1102 | * named 'cert_path' suffixed using '.issuer'. |
| 1103 | * |
| 1104 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 1105 | * response. If file is empty or content is not a valid OCSP response, |
| 1106 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 1107 | * is displayed). |
| 1108 | * |
| 1109 | * 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] | 1110 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1111 | */ |
| 1112 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 1113 | { |
| 1114 | |
| 1115 | BIO *in = NULL; |
| 1116 | X509 *x, *xi = NULL, *issuer = NULL; |
| 1117 | STACK_OF(X509) *chain = NULL; |
| 1118 | OCSP_CERTID *cid = NULL; |
| 1119 | SSL *ssl; |
| 1120 | char ocsp_path[MAXPATHLEN+1]; |
| 1121 | int i, ret = -1; |
| 1122 | struct stat st; |
| 1123 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1124 | char *warn = NULL; |
| 1125 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1126 | pem_password_cb *passwd_cb; |
| 1127 | void *passwd_cb_userdata; |
| 1128 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1129 | |
| 1130 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1131 | |
| 1132 | if (stat(ocsp_path, &st)) |
| 1133 | return 1; |
| 1134 | |
| 1135 | ssl = SSL_new(ctx); |
| 1136 | if (!ssl) |
| 1137 | goto out; |
| 1138 | |
| 1139 | x = SSL_get_certificate(ssl); |
| 1140 | if (!x) |
| 1141 | goto out; |
| 1142 | |
| 1143 | /* Try to lookup for issuer in certificate extra chain */ |
| 1144 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 1145 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 1146 | #else |
| 1147 | chain = ctx->extra_certs; |
| 1148 | #endif |
| 1149 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1150 | issuer = sk_X509_value(chain, i); |
| 1151 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 1152 | break; |
| 1153 | else |
| 1154 | issuer = NULL; |
| 1155 | } |
| 1156 | |
| 1157 | /* If not found try to load issuer from a suffixed file */ |
| 1158 | if (!issuer) { |
| 1159 | char issuer_path[MAXPATHLEN+1]; |
| 1160 | |
| 1161 | in = BIO_new(BIO_s_file()); |
| 1162 | if (!in) |
| 1163 | goto out; |
| 1164 | |
| 1165 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 1166 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 1167 | goto out; |
| 1168 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1169 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 1170 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 1171 | |
| 1172 | 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] | 1173 | if (!xi) |
| 1174 | goto out; |
| 1175 | |
| 1176 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 1177 | goto out; |
| 1178 | |
| 1179 | issuer = xi; |
| 1180 | } |
| 1181 | |
| 1182 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1183 | if (!cid) |
| 1184 | goto out; |
| 1185 | |
| 1186 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1187 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1188 | goto out; |
| 1189 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1190 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1191 | if (!ocsp) |
| 1192 | goto out; |
| 1193 | |
| 1194 | p = ocsp->key_data; |
| 1195 | i2d_OCSP_CERTID(cid, &p); |
| 1196 | |
| 1197 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1198 | if (iocsp == ocsp) |
| 1199 | ocsp = NULL; |
| 1200 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1201 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1202 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1203 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1204 | #endif |
| 1205 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1206 | |
| 1207 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1208 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1209 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1210 | |
| 1211 | cb_arg->is_single = 1; |
| 1212 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1213 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1214 | pkey = X509_get_pubkey(x); |
| 1215 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1216 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1217 | |
| 1218 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1219 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1220 | } else { |
| 1221 | /* |
| 1222 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1223 | * Update that cb_arg with the new cert's staple |
| 1224 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1225 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1226 | struct certificate_ocsp *tmp_ocsp; |
| 1227 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1228 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1229 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1230 | |
| 1231 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1232 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1233 | #else |
| 1234 | cb_arg = ctx->tlsext_status_arg; |
| 1235 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1236 | |
| 1237 | /* |
| 1238 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1239 | * the order of operations below matter, take care when changing it |
| 1240 | */ |
| 1241 | tmp_ocsp = cb_arg->s_ocsp; |
| 1242 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1243 | cb_arg->s_ocsp = NULL; |
| 1244 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1245 | cb_arg->is_single = 0; |
| 1246 | cb_arg->single_kt = 0; |
| 1247 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1248 | pkey = X509_get_pubkey(x); |
| 1249 | key_type = EVP_PKEY_base_id(pkey); |
| 1250 | EVP_PKEY_free(pkey); |
| 1251 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1252 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1253 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1254 | cb_arg->m_ocsp[index] = iocsp; |
| 1255 | |
| 1256 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1257 | |
| 1258 | ret = 0; |
| 1259 | |
| 1260 | warn = NULL; |
| 1261 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 1262 | 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] | 1263 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | out: |
| 1267 | if (ssl) |
| 1268 | SSL_free(ssl); |
| 1269 | |
| 1270 | if (in) |
| 1271 | BIO_free(in); |
| 1272 | |
| 1273 | if (xi) |
| 1274 | X509_free(xi); |
| 1275 | |
| 1276 | if (cid) |
| 1277 | OCSP_CERTID_free(cid); |
| 1278 | |
| 1279 | if (ocsp) |
| 1280 | free(ocsp); |
| 1281 | |
| 1282 | if (warn) |
| 1283 | free(warn); |
| 1284 | |
| 1285 | |
| 1286 | return ret; |
| 1287 | } |
| 1288 | |
| 1289 | #endif |
| 1290 | |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1291 | #ifdef OPENSSL_IS_BORINGSSL |
| 1292 | static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path) |
| 1293 | { |
| 1294 | char ocsp_path[MAXPATHLEN+1]; |
| 1295 | struct stat st; |
| 1296 | int fd = -1, r = 0; |
| 1297 | |
| 1298 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1299 | if (stat(ocsp_path, &st)) |
| 1300 | return 0; |
| 1301 | |
| 1302 | fd = open(ocsp_path, O_RDONLY); |
| 1303 | if (fd == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1304 | ha_warning("Error opening OCSP response file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1305 | return -1; |
| 1306 | } |
| 1307 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1308 | trash.data = 0; |
| 1309 | while (trash.data < trash.size) { |
| 1310 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1311 | if (r < 0) { |
| 1312 | if (errno == EINTR) |
| 1313 | continue; |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1314 | ha_warning("Error reading OCSP response from file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1315 | close(fd); |
| 1316 | return -1; |
| 1317 | } |
| 1318 | else if (r == 0) { |
| 1319 | break; |
| 1320 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1321 | trash.data += r; |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1322 | } |
| 1323 | close(fd); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1324 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *) trash.area, |
| 1325 | trash.data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1326 | } |
| 1327 | #endif |
| 1328 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 1329 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1330 | |
| 1331 | #define CT_EXTENSION_TYPE 18 |
| 1332 | |
| 1333 | static int sctl_ex_index = -1; |
| 1334 | |
| 1335 | /* |
| 1336 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1337 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1338 | * is performed. |
| 1339 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1340 | static int ssl_sock_parse_sctl(struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1341 | { |
| 1342 | int ret = 1; |
| 1343 | int len, pos, sct_len; |
| 1344 | unsigned char *data; |
| 1345 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1346 | if (sctl->data < 2) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1347 | goto out; |
| 1348 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1349 | data = (unsigned char *) sctl->area; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1350 | len = (data[0] << 8) | data[1]; |
| 1351 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1352 | if (len + 2 != sctl->data) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1353 | goto out; |
| 1354 | |
| 1355 | data = data + 2; |
| 1356 | pos = 0; |
| 1357 | while (pos < len) { |
| 1358 | if (len - pos < 2) |
| 1359 | goto out; |
| 1360 | |
| 1361 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1362 | if (pos + sct_len + 2 > len) |
| 1363 | goto out; |
| 1364 | |
| 1365 | pos += sct_len + 2; |
| 1366 | } |
| 1367 | |
| 1368 | ret = 0; |
| 1369 | |
| 1370 | out: |
| 1371 | return ret; |
| 1372 | } |
| 1373 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1374 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, |
| 1375 | struct buffer **sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1376 | { |
| 1377 | int fd = -1; |
| 1378 | int r = 0; |
| 1379 | int ret = 1; |
| 1380 | |
| 1381 | *sctl = NULL; |
| 1382 | |
| 1383 | fd = open(sctl_path, O_RDONLY); |
| 1384 | if (fd == -1) |
| 1385 | goto end; |
| 1386 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1387 | trash.data = 0; |
| 1388 | while (trash.data < trash.size) { |
| 1389 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1390 | if (r < 0) { |
| 1391 | if (errno == EINTR) |
| 1392 | continue; |
| 1393 | |
| 1394 | goto end; |
| 1395 | } |
| 1396 | else if (r == 0) { |
| 1397 | break; |
| 1398 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1399 | trash.data += r; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | ret = ssl_sock_parse_sctl(&trash); |
| 1403 | if (ret) |
| 1404 | goto end; |
| 1405 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1406 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1407 | if (!chunk_dup(*sctl, &trash)) { |
| 1408 | free(*sctl); |
| 1409 | *sctl = NULL; |
| 1410 | goto end; |
| 1411 | } |
| 1412 | |
| 1413 | end: |
| 1414 | if (fd != -1) |
| 1415 | close(fd); |
| 1416 | |
| 1417 | return ret; |
| 1418 | } |
| 1419 | |
| 1420 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1421 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1422 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1423 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1424 | *out = (unsigned char *) sctl->area; |
| 1425 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1426 | |
| 1427 | return 1; |
| 1428 | } |
| 1429 | |
| 1430 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1431 | { |
| 1432 | return 1; |
| 1433 | } |
| 1434 | |
| 1435 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 1436 | { |
| 1437 | char sctl_path[MAXPATHLEN+1]; |
| 1438 | int ret = -1; |
| 1439 | struct stat st; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1440 | struct buffer *sctl = NULL; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1441 | |
| 1442 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 1443 | |
| 1444 | if (stat(sctl_path, &st)) |
| 1445 | return 1; |
| 1446 | |
| 1447 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 1448 | goto out; |
| 1449 | |
| 1450 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 1451 | free(sctl); |
| 1452 | goto out; |
| 1453 | } |
| 1454 | |
| 1455 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1456 | |
| 1457 | ret = 0; |
| 1458 | |
| 1459 | out: |
| 1460 | return ret; |
| 1461 | } |
| 1462 | |
| 1463 | #endif |
| 1464 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1465 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1466 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1467 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1468 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1469 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1470 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1471 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1472 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1473 | * change this to #if and do not assign a default value to this macro! |
| 1474 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1475 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1476 | /* Disable renegotiation (CVE-2009-3555) */ |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 1477 | 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] | 1478 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1479 | conn->err_code = CO_ER_SSL_RENEG; |
| 1480 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1481 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1482 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1483 | |
| 1484 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 1485 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 1486 | /* Long certificate chains optimz |
| 1487 | If write and read bios are differents, we |
| 1488 | consider that the buffering was activated, |
| 1489 | so we rise the output buffer size from 4k |
| 1490 | to 16k */ |
| 1491 | write_bio = SSL_get_wbio(ssl); |
| 1492 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1493 | BIO_set_write_buffer_size(write_bio, 16384); |
| 1494 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 1495 | } |
| 1496 | } |
| 1497 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1498 | } |
| 1499 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1500 | /* Callback is called for each certificate of the chain during a verify |
| 1501 | ok is set to 1 if preverify detect no error on current certificate. |
| 1502 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1503 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1504 | { |
| 1505 | SSL *ssl; |
| 1506 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1507 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1508 | |
| 1509 | 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] | 1510 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1511 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1512 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1513 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1514 | if (ok) /* no errors */ |
| 1515 | return ok; |
| 1516 | |
| 1517 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1518 | err = X509_STORE_CTX_get_error(x_store); |
| 1519 | |
| 1520 | /* check if CA error needs to be ignored */ |
| 1521 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1522 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 1523 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1524 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1525 | } |
| 1526 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1527 | 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] | 1528 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1529 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1530 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1531 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1532 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1533 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1534 | return 0; |
| 1535 | } |
| 1536 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1537 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 1538 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1539 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1540 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1541 | 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] | 1542 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1543 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1544 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1545 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1546 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1547 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1548 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1549 | } |
| 1550 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1551 | static inline |
| 1552 | 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] | 1553 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1554 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1555 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1556 | unsigned char *msg; |
| 1557 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1558 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1559 | |
| 1560 | /* This function is called for "from client" and "to server" |
| 1561 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1562 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1563 | */ |
| 1564 | |
| 1565 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1566 | * otherwise it is set to 1. |
| 1567 | */ |
| 1568 | if (write_p != 0) |
| 1569 | return; |
| 1570 | |
| 1571 | /* content_type contains the type of message received or sent |
| 1572 | * according with the SSL/TLS protocol spec. This message is |
| 1573 | * encoded with one byte. The value 256 (two bytes) is used |
| 1574 | * for designing the SSL/TLS record layer. According with the |
| 1575 | * rfc6101, the expected message (other than 256) are: |
| 1576 | * - change_cipher_spec(20) |
| 1577 | * - alert(21) |
| 1578 | * - handshake(22) |
| 1579 | * - application_data(23) |
| 1580 | * - (255) |
| 1581 | * We are interessed by the handshake and specially the client |
| 1582 | * hello. |
| 1583 | */ |
| 1584 | if (content_type != 22) |
| 1585 | return; |
| 1586 | |
| 1587 | /* The message length is at least 4 bytes, containing the |
| 1588 | * message type and the message length. |
| 1589 | */ |
| 1590 | if (len < 4) |
| 1591 | return; |
| 1592 | |
| 1593 | /* First byte of the handshake message id the type of |
| 1594 | * message. The konwn types are: |
| 1595 | * - hello_request(0) |
| 1596 | * - client_hello(1) |
| 1597 | * - server_hello(2) |
| 1598 | * - certificate(11) |
| 1599 | * - server_key_exchange (12) |
| 1600 | * - certificate_request(13) |
| 1601 | * - server_hello_done(14) |
| 1602 | * We are interested by the client hello. |
| 1603 | */ |
| 1604 | msg = (unsigned char *)buf; |
| 1605 | if (msg[0] != 1) |
| 1606 | return; |
| 1607 | |
| 1608 | /* Next three bytes are the length of the message. The total length |
| 1609 | * must be this decoded length + 4. If the length given as argument |
| 1610 | * is not the same, we abort the protocol dissector. |
| 1611 | */ |
| 1612 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1613 | if (len < rec_len + 4) |
| 1614 | return; |
| 1615 | msg += 4; |
| 1616 | end = msg + rec_len; |
| 1617 | if (end < msg) |
| 1618 | return; |
| 1619 | |
| 1620 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1621 | * 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] | 1622 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1623 | */ |
| 1624 | msg += 1 + 1 + 4 + 28; |
| 1625 | if (msg > end) |
| 1626 | return; |
| 1627 | |
| 1628 | /* Next, is session id: |
| 1629 | * if present, we have to jump by length + 1 for the size information |
| 1630 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1631 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1632 | if (msg[0] > 0) |
| 1633 | msg += msg[0]; |
| 1634 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1635 | if (msg > end) |
| 1636 | return; |
| 1637 | |
| 1638 | /* Next two bytes are the ciphersuite length. */ |
| 1639 | if (msg + 2 > end) |
| 1640 | return; |
| 1641 | rec_len = (msg[0] << 8) + msg[1]; |
| 1642 | msg += 2; |
| 1643 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1644 | return; |
| 1645 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1646 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1647 | if (!capture) |
| 1648 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1649 | /* Compute the xxh64 of the ciphersuite. */ |
| 1650 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1651 | |
| 1652 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1653 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1654 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1655 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1656 | |
| 1657 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1658 | } |
| 1659 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1660 | /* Callback is called for ssl protocol analyse */ |
| 1661 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1662 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1663 | #ifdef TLS1_RT_HEARTBEAT |
| 1664 | /* test heartbeat received (write_p is set to 0 |
| 1665 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1666 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1667 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1668 | const unsigned char *p = buf; |
| 1669 | unsigned int payload; |
| 1670 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1671 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1672 | |
| 1673 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1674 | if (*p != TLS1_HB_REQUEST) |
| 1675 | return; |
| 1676 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1677 | 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] | 1678 | goto kill_it; |
| 1679 | |
| 1680 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1681 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1682 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1683 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1684 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1685 | * advertised payload is larger than the advertised packet |
| 1686 | * length, so we have garbage in the buffer between the |
| 1687 | * payload and the end of the buffer (p+len). We can't know |
| 1688 | * if the SSL stack is patched, and we don't know if we can |
| 1689 | * safely wipe out the area between p+3+len and payload. |
| 1690 | * So instead, we prevent the response from being sent by |
| 1691 | * setting the max_send_fragment to 0 and we report an SSL |
| 1692 | * error, which will kill this connection. It will be reported |
| 1693 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1694 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1695 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1696 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1697 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1698 | return; |
| 1699 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1700 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1701 | if (global_ssl.capture_cipherlist > 0) |
| 1702 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1703 | } |
| 1704 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1705 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1706 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1707 | const unsigned char *in, unsigned int inlen, |
| 1708 | void *arg) |
| 1709 | { |
| 1710 | struct server *srv = arg; |
| 1711 | |
| 1712 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1713 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1714 | return SSL_TLSEXT_ERR_OK; |
| 1715 | return SSL_TLSEXT_ERR_NOACK; |
| 1716 | } |
| 1717 | #endif |
| 1718 | |
| 1719 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1720 | /* This callback is used so that the server advertises the list of |
| 1721 | * negociable protocols for NPN. |
| 1722 | */ |
| 1723 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1724 | unsigned int *len, void *arg) |
| 1725 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1726 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1727 | |
| 1728 | *data = (const unsigned char *)conf->npn_str; |
| 1729 | *len = conf->npn_len; |
| 1730 | return SSL_TLSEXT_ERR_OK; |
| 1731 | } |
| 1732 | #endif |
| 1733 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1734 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1735 | /* This callback is used so that the server advertises the list of |
| 1736 | * negociable protocols for ALPN. |
| 1737 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1738 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1739 | unsigned char *outlen, |
| 1740 | const unsigned char *server, |
| 1741 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1742 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1743 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1744 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1745 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1746 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1747 | return SSL_TLSEXT_ERR_NOACK; |
| 1748 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1749 | return SSL_TLSEXT_ERR_OK; |
| 1750 | } |
| 1751 | #endif |
| 1752 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1753 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1754 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1755 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1756 | /* Create a X509 certificate with the specified servername and serial. This |
| 1757 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1758 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1759 | 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] | 1760 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1761 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1762 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1763 | SSL_CTX *ssl_ctx = NULL; |
| 1764 | X509 *newcrt = NULL; |
| 1765 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1766 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1767 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1768 | X509_NAME *name; |
| 1769 | const EVP_MD *digest; |
| 1770 | X509V3_CTX ctx; |
| 1771 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1772 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1773 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1774 | /* Get the private key of the default certificate and use it */ |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1775 | #if (OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined LIBRESSL_VERSION_NUMBER) |
| 1776 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1777 | #else |
| 1778 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1779 | if (tmp_ssl) |
| 1780 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1781 | #endif |
| 1782 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1783 | goto mkcert_error; |
| 1784 | |
| 1785 | /* Create the certificate */ |
| 1786 | if (!(newcrt = X509_new())) |
| 1787 | goto mkcert_error; |
| 1788 | |
| 1789 | /* Set version number for the certificate (X509v3) and the serial |
| 1790 | * number */ |
| 1791 | if (X509_set_version(newcrt, 2L) != 1) |
| 1792 | goto mkcert_error; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1793 | 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] | 1794 | |
| 1795 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 1796 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 1797 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1798 | goto mkcert_error; |
| 1799 | |
| 1800 | /* set public key in the certificate */ |
| 1801 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1802 | goto mkcert_error; |
| 1803 | |
| 1804 | /* Set issuer name from the CA */ |
| 1805 | if (!(name = X509_get_subject_name(cacert))) |
| 1806 | goto mkcert_error; |
| 1807 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1808 | goto mkcert_error; |
| 1809 | |
| 1810 | /* Set the subject name using the same, but the CN */ |
| 1811 | name = X509_NAME_dup(name); |
| 1812 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1813 | (const unsigned char *)servername, |
| 1814 | -1, -1, 0) != 1) { |
| 1815 | X509_NAME_free(name); |
| 1816 | goto mkcert_error; |
| 1817 | } |
| 1818 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1819 | X509_NAME_free(name); |
| 1820 | goto mkcert_error; |
| 1821 | } |
| 1822 | X509_NAME_free(name); |
| 1823 | |
| 1824 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1825 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1826 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1827 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1828 | X509_EXTENSION *ext; |
| 1829 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1830 | 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] | 1831 | goto mkcert_error; |
| 1832 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1833 | X509_EXTENSION_free(ext); |
| 1834 | goto mkcert_error; |
| 1835 | } |
| 1836 | X509_EXTENSION_free(ext); |
| 1837 | } |
| 1838 | |
| 1839 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1840 | |
| 1841 | key_type = EVP_PKEY_base_id(capkey); |
| 1842 | |
| 1843 | if (key_type == EVP_PKEY_DSA) |
| 1844 | digest = EVP_sha1(); |
| 1845 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1846 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1847 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1848 | digest = EVP_sha256(); |
| 1849 | else { |
Emmanuel Hocdet | 747ca61 | 2018-10-01 18:45:19 +0200 | [diff] [blame] | 1850 | #if (OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1851 | int nid; |
| 1852 | |
| 1853 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1854 | goto mkcert_error; |
| 1855 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1856 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1857 | #else |
| 1858 | goto mkcert_error; |
| 1859 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1860 | } |
| 1861 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1862 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1863 | goto mkcert_error; |
| 1864 | |
| 1865 | /* Create and set the new SSL_CTX */ |
| 1866 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1867 | goto mkcert_error; |
| 1868 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1869 | goto mkcert_error; |
| 1870 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1871 | goto mkcert_error; |
| 1872 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1873 | goto mkcert_error; |
| 1874 | |
| 1875 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1876 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1877 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1878 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1879 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1880 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1881 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1882 | 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] | 1883 | EC_KEY *ecc; |
| 1884 | int nid; |
| 1885 | |
| 1886 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1887 | goto end; |
| 1888 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1889 | goto end; |
| 1890 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1891 | EC_KEY_free(ecc); |
| 1892 | } |
| 1893 | #endif |
| 1894 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1895 | return ssl_ctx; |
| 1896 | |
| 1897 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1898 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1899 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1900 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1901 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1902 | return NULL; |
| 1903 | } |
| 1904 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1905 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1906 | 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] | 1907 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1908 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1909 | |
| 1910 | return ssl_sock_do_create_cert(servername, bind_conf, conn->xprt_ctx); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1911 | } |
| 1912 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1913 | /* 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] | 1914 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1915 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1916 | 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] | 1917 | { |
| 1918 | struct lru64 *lru = NULL; |
| 1919 | |
| 1920 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1921 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1922 | 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] | 1923 | if (lru && lru->domain) { |
| 1924 | if (ssl) |
| 1925 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1926 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1927 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1928 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1929 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1930 | } |
| 1931 | return NULL; |
| 1932 | } |
| 1933 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1934 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 1935 | * function is not thread-safe, it should only be used to check if a certificate |
| 1936 | * exists in the lru cache (with no warranty it will not be removed by another |
| 1937 | * thread). It is kept for backward compatibility. */ |
| 1938 | SSL_CTX * |
| 1939 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 1940 | { |
| 1941 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 1942 | } |
| 1943 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1944 | /* Set a certificate int the LRU cache used to store generated |
| 1945 | * certificate. Return 0 on success, otherwise -1 */ |
| 1946 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1947 | 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] | 1948 | { |
| 1949 | struct lru64 *lru = NULL; |
| 1950 | |
| 1951 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1952 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1953 | 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] | 1954 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1955 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1956 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1957 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1958 | if (lru->domain && lru->data) |
| 1959 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1960 | 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] | 1961 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1962 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1963 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1964 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1965 | } |
| 1966 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1967 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1968 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1969 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1970 | { |
| 1971 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 1972 | } |
| 1973 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1974 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 1975 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 1976 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1977 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1978 | 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] | 1979 | { |
| 1980 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1981 | SSL_CTX *ssl_ctx = NULL; |
| 1982 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1983 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1984 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1985 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1986 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1987 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1988 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1989 | if (lru && lru->domain) |
| 1990 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1991 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1992 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1993 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1994 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1995 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1996 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1997 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1998 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1999 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2000 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2001 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2002 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2003 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2004 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2005 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2006 | return 0; |
| 2007 | } |
| 2008 | static int |
| 2009 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2010 | { |
| 2011 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2012 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2013 | |
| 2014 | conn_get_to_addr(conn); |
| 2015 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
| 2016 | 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] | 2017 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2018 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2019 | } |
| 2020 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2021 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2022 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2023 | |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2024 | |
| 2025 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 2026 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 2027 | #endif |
| 2028 | |
| 2029 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 2030 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
| 2031 | #define SSL_renegotiate_pending(arg) 0 |
| 2032 | #endif |
| 2033 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 2034 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 2035 | #endif |
| 2036 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 2037 | #define SSL_OP_NO_TICKET 0 |
| 2038 | #endif |
| 2039 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 2040 | #define SSL_OP_NO_COMPRESSION 0 |
| 2041 | #endif |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2042 | #ifdef OPENSSL_NO_SSL3 /* SSLv3 support removed */ |
| 2043 | #undef SSL_OP_NO_SSLv3 |
| 2044 | #define SSL_OP_NO_SSLv3 0 |
| 2045 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2046 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 2047 | #define SSL_OP_NO_TLSv1_1 0 |
| 2048 | #endif |
| 2049 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 2050 | #define SSL_OP_NO_TLSv1_2 0 |
| 2051 | #endif |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2052 | #ifndef SSL_OP_NO_TLSv1_3 /* needs OpenSSL >= 1.1.1 */ |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2053 | #define SSL_OP_NO_TLSv1_3 0 |
| 2054 | #endif |
| 2055 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 2056 | #define SSL_OP_SINGLE_DH_USE 0 |
| 2057 | #endif |
| 2058 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 2059 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 2060 | #endif |
| 2061 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 2062 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 2063 | #endif |
| 2064 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 2065 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 2066 | #endif |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 2067 | #ifndef SSL_OP_PRIORITIZE_CHACHA /* needs OpenSSL >= 1.1.1 */ |
| 2068 | #define SSL_OP_PRIORITIZE_CHACHA 0 |
| 2069 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2070 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2071 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2072 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 2073 | |
| 2074 | 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] | 2075 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2076 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2077 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2078 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2079 | #endif |
| 2080 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2081 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2082 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2083 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2084 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2085 | 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] | 2086 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2087 | 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] | 2088 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2089 | #endif |
| 2090 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2091 | 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] | 2092 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2093 | 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] | 2094 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2095 | #endif |
| 2096 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2097 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2098 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2099 | /* Unusable in this context. */ |
| 2100 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2101 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2102 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2103 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2104 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2105 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2106 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 2107 | |
| 2108 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2109 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2110 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2111 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2112 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2113 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2114 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2115 | } |
| 2116 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2117 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2118 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2119 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2120 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2121 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2122 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2123 | } |
| 2124 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2125 | 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] | 2126 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2127 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2128 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2129 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2130 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2131 | } |
| 2132 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2133 | 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] | 2134 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2135 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2136 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2137 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2138 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2139 | } |
| 2140 | 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] | 2141 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2142 | 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] | 2143 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2144 | #endif |
| 2145 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2146 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2147 | #if SSL_OP_NO_TLSv1_3 |
| 2148 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2149 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2150 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2151 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2152 | #endif |
| 2153 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2154 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2155 | |
| 2156 | static struct { |
| 2157 | int option; |
| 2158 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2159 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 2160 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2161 | const char *name; |
| 2162 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2163 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2164 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2165 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2166 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2167 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2168 | {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] | 2169 | }; |
| 2170 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2171 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2172 | { |
| 2173 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2174 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2175 | SSL_set_SSL_CTX(ssl, ctx); |
| 2176 | } |
| 2177 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2178 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2179 | |
| 2180 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2181 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2182 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2183 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2184 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2185 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2186 | return SSL_TLSEXT_ERR_OK; |
| 2187 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2188 | } |
| 2189 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2190 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2191 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2192 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2193 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2194 | #else |
| 2195 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2196 | { |
| 2197 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2198 | struct connection *conn; |
| 2199 | struct bind_conf *s; |
| 2200 | const uint8_t *extension_data; |
| 2201 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2202 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2203 | |
| 2204 | char *wildp = NULL; |
| 2205 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2206 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2207 | 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] | 2208 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2209 | int i; |
| 2210 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2211 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2212 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2213 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2214 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2215 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2216 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2217 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2218 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2219 | #else |
| 2220 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2221 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2222 | /* |
| 2223 | * The server_name extension was given too much extensibility when it |
| 2224 | * was written, so parsing the normal case is a bit complex. |
| 2225 | */ |
| 2226 | size_t len; |
| 2227 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2228 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2229 | /* Extract the length of the supplied list of names. */ |
| 2230 | len = (*extension_data++) << 8; |
| 2231 | len |= *extension_data++; |
| 2232 | if (len + 2 != extension_len) |
| 2233 | goto abort; |
| 2234 | /* |
| 2235 | * The list in practice only has a single element, so we only consider |
| 2236 | * the first one. |
| 2237 | */ |
| 2238 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2239 | goto abort; |
| 2240 | extension_len = len - 1; |
| 2241 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2242 | if (extension_len <= 2) |
| 2243 | goto abort; |
| 2244 | len = (*extension_data++) << 8; |
| 2245 | len |= *extension_data++; |
| 2246 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2247 | || memchr(extension_data, 0, len) != NULL) |
| 2248 | goto abort; |
| 2249 | servername = extension_data; |
| 2250 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2251 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2252 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2253 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2254 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2255 | } |
| 2256 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2257 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2258 | if (!s->strict_sni) { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2259 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2260 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2261 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2262 | goto abort; |
| 2263 | } |
| 2264 | |
| 2265 | /* extract/check clientHello informations */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2266 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2267 | 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] | 2268 | #else |
| 2269 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2270 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2271 | uint8_t sign; |
| 2272 | size_t len; |
| 2273 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2274 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2275 | len = (*extension_data++) << 8; |
| 2276 | len |= *extension_data++; |
| 2277 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2278 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2279 | if (len % 2 != 0) |
| 2280 | goto abort; |
| 2281 | for (; len > 0; len -= 2) { |
| 2282 | extension_data++; /* hash */ |
| 2283 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2284 | switch (sign) { |
| 2285 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2286 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2287 | break; |
| 2288 | case TLSEXT_signature_ecdsa: |
| 2289 | has_ecdsa_sig = 1; |
| 2290 | break; |
| 2291 | default: |
| 2292 | continue; |
| 2293 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2294 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2295 | break; |
| 2296 | } |
| 2297 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2298 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2299 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2300 | } |
| 2301 | 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] | 2302 | const SSL_CIPHER *cipher; |
| 2303 | size_t len; |
| 2304 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2305 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2306 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2307 | len = ctx->cipher_suites_len; |
| 2308 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2309 | #else |
| 2310 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2311 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2312 | if (len % 2 != 0) |
| 2313 | goto abort; |
| 2314 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2315 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2316 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2317 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2318 | #else |
| 2319 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2320 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2321 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2322 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2323 | break; |
| 2324 | } |
| 2325 | } |
| 2326 | } |
| 2327 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2328 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2329 | trash.area[i] = tolower(servername[i]); |
| 2330 | if (!wildp && (trash.area[i] == '.')) |
| 2331 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2332 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2333 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2334 | |
| 2335 | /* lookup in full qualified names */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2336 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2337 | |
| 2338 | /* lookup a not neg filter */ |
| 2339 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2340 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2341 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2342 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2343 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2344 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2345 | break; |
| 2346 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2347 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2348 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2349 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2350 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2351 | if (!node_anonymous) |
| 2352 | node_anonymous = n; |
| 2353 | break; |
| 2354 | } |
| 2355 | } |
| 2356 | } |
| 2357 | if (wildp) { |
| 2358 | /* lookup in wildcards names */ |
| 2359 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2360 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2361 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2362 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2363 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2364 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2365 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2366 | break; |
| 2367 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2368 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2369 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2370 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2371 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2372 | if (!node_anonymous) |
| 2373 | node_anonymous = n; |
| 2374 | break; |
| 2375 | } |
| 2376 | } |
| 2377 | } |
| 2378 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2379 | /* select by key_signature priority order */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2380 | node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa |
| 2381 | : ((has_rsa_sig && node_rsa) ? node_rsa |
| 2382 | : (node_anonymous ? node_anonymous |
| 2383 | : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */ |
| 2384 | : node_rsa /* no rsa signature case (far far away) */ |
| 2385 | ))); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2386 | if (node) { |
| 2387 | /* switch ctx */ |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 2388 | 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] | 2389 | 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] | 2390 | if (conf) { |
| 2391 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2392 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2393 | if (conf->early_data) |
| 2394 | allow_early = 1; |
| 2395 | } |
| 2396 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2397 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2398 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2399 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2400 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2401 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2402 | } |
| 2403 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2404 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2405 | /* no certificate match, is the default_ctx */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2406 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2407 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2408 | allow_early: |
| 2409 | #ifdef OPENSSL_IS_BORINGSSL |
| 2410 | if (allow_early) |
| 2411 | SSL_set_early_data_enabled(ssl, 1); |
| 2412 | #else |
| 2413 | if (!allow_early) |
| 2414 | SSL_set_max_early_data(ssl, 0); |
| 2415 | #endif |
| 2416 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2417 | abort: |
| 2418 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2419 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2420 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2421 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2422 | #else |
| 2423 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2424 | return 0; |
| 2425 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2429 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2430 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2431 | * warning when no match is found, which implies the default (first) cert |
| 2432 | * will keep being used. |
| 2433 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2434 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2435 | { |
| 2436 | const char *servername; |
| 2437 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2438 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2439 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2440 | int i; |
| 2441 | (void)al; /* shut gcc stupid warning */ |
| 2442 | |
| 2443 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2444 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2445 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2446 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2447 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2448 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2449 | if (s->strict_sni) |
| 2450 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2451 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2452 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2453 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2454 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2455 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2456 | if (!servername[i]) |
| 2457 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2458 | trash.area[i] = tolower(servername[i]); |
| 2459 | if (!wildp && (trash.area[i] == '.')) |
| 2460 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2461 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2462 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2463 | |
| 2464 | /* lookup in full qualified names */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2465 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2466 | |
| 2467 | /* lookup a not neg filter */ |
| 2468 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2469 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2470 | node = n; |
| 2471 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2472 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2473 | } |
| 2474 | if (!node && wildp) { |
| 2475 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2476 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2477 | } |
| 2478 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2479 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2480 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2481 | /* switch ctx done in ssl_sock_generate_certificate */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2482 | return SSL_TLSEXT_ERR_OK; |
| 2483 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2484 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2485 | if (s->strict_sni) |
| 2486 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2487 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2488 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2492 | 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] | 2493 | return SSL_TLSEXT_ERR_OK; |
| 2494 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2495 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2496 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2497 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2498 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2499 | |
| 2500 | static DH * ssl_get_dh_1024(void) |
| 2501 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2502 | static unsigned char dh1024_p[]={ |
| 2503 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2504 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2505 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2506 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2507 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2508 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2509 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2510 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2511 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2512 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2513 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2514 | }; |
| 2515 | static unsigned char dh1024_g[]={ |
| 2516 | 0x02, |
| 2517 | }; |
| 2518 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2519 | BIGNUM *p; |
| 2520 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2521 | DH *dh = DH_new(); |
| 2522 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2523 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2524 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2525 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2526 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2527 | DH_free(dh); |
| 2528 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2529 | } else { |
| 2530 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2531 | } |
| 2532 | } |
| 2533 | return dh; |
| 2534 | } |
| 2535 | |
| 2536 | static DH *ssl_get_dh_2048(void) |
| 2537 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2538 | static unsigned char dh2048_p[]={ |
| 2539 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2540 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2541 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2542 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2543 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2544 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2545 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2546 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2547 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2548 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2549 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2550 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2551 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2552 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2553 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2554 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2555 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2556 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2557 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2558 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2559 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2560 | 0xB7,0x1F,0x77,0xF3, |
| 2561 | }; |
| 2562 | static unsigned char dh2048_g[]={ |
| 2563 | 0x02, |
| 2564 | }; |
| 2565 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2566 | BIGNUM *p; |
| 2567 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2568 | DH *dh = DH_new(); |
| 2569 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2570 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2571 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2572 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2573 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2574 | DH_free(dh); |
| 2575 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2576 | } else { |
| 2577 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2578 | } |
| 2579 | } |
| 2580 | return dh; |
| 2581 | } |
| 2582 | |
| 2583 | static DH *ssl_get_dh_4096(void) |
| 2584 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2585 | static unsigned char dh4096_p[]={ |
| 2586 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2587 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2588 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2589 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2590 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2591 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2592 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2593 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2594 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2595 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2596 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2597 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2598 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2599 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2600 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2601 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2602 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2603 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2604 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2605 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2606 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2607 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2608 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2609 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2610 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2611 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2612 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2613 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2614 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2615 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2616 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2617 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2618 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2619 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2620 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2621 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2622 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2623 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2624 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2625 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2626 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2627 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2628 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2629 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2630 | static unsigned char dh4096_g[]={ |
| 2631 | 0x02, |
| 2632 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2633 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2634 | BIGNUM *p; |
| 2635 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2636 | DH *dh = DH_new(); |
| 2637 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2638 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2639 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2640 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2641 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2642 | DH_free(dh); |
| 2643 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2644 | } else { |
| 2645 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2646 | } |
| 2647 | } |
| 2648 | return dh; |
| 2649 | } |
| 2650 | |
| 2651 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2652 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2653 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2654 | { |
| 2655 | DH *dh = NULL; |
| 2656 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2657 | int type; |
| 2658 | |
| 2659 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2660 | |
| 2661 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2662 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2663 | */ |
| 2664 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2665 | keylen = EVP_PKEY_bits(pkey); |
| 2666 | } |
| 2667 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2668 | if (keylen > global_ssl.default_dh_param) { |
| 2669 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2670 | } |
| 2671 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2672 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2673 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2674 | } |
| 2675 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2676 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2677 | } |
| 2678 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2679 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2680 | } |
| 2681 | |
| 2682 | return dh; |
| 2683 | } |
| 2684 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2685 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2686 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2687 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2688 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2689 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2690 | if (in == NULL) |
| 2691 | goto end; |
| 2692 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2693 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2694 | goto end; |
| 2695 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2696 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2697 | |
| 2698 | end: |
| 2699 | if (in) |
| 2700 | BIO_free(in); |
| 2701 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2702 | ERR_clear_error(); |
| 2703 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2704 | return dh; |
| 2705 | } |
| 2706 | |
| 2707 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2708 | { |
| 2709 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2710 | |
| 2711 | if (global_dh) { |
| 2712 | return 0; |
| 2713 | } |
| 2714 | |
| 2715 | return -1; |
| 2716 | } |
| 2717 | |
| 2718 | /* 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] | 2719 | if an error occurred, and 0 if parameter not found. */ |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2720 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 2721 | { |
| 2722 | int ret = -1; |
| 2723 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 2724 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2725 | if (dh) { |
| 2726 | ret = 1; |
| 2727 | SSL_CTX_set_tmp_dh(ctx, dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2728 | |
| 2729 | if (ssl_dh_ptr_index >= 0) { |
| 2730 | /* store a pointer to the DH params to avoid complaining about |
| 2731 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2732 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2733 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2734 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2735 | else if (global_dh) { |
| 2736 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 2737 | ret = 0; /* DH params not found */ |
| 2738 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2739 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2740 | /* Clear openssl global errors stack */ |
| 2741 | ERR_clear_error(); |
| 2742 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2743 | if (global_ssl.default_dh_param <= 1024) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2744 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 2745 | if (local_dh_1024 == NULL) |
| 2746 | local_dh_1024 = ssl_get_dh_1024(); |
| 2747 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2748 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2749 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2750 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2751 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2752 | } |
| 2753 | else { |
| 2754 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2755 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2756 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2757 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2758 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2759 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2760 | end: |
| 2761 | if (dh) |
| 2762 | DH_free(dh); |
| 2763 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2764 | return ret; |
| 2765 | } |
| 2766 | #endif |
| 2767 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2768 | 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] | 2769 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2770 | { |
| 2771 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2772 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2773 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2774 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2775 | if (*name == '!') { |
| 2776 | neg = 1; |
| 2777 | name++; |
| 2778 | } |
| 2779 | if (*name == '*') { |
| 2780 | wild = 1; |
| 2781 | name++; |
| 2782 | } |
| 2783 | /* !* filter is a nop */ |
| 2784 | if (neg && wild) |
| 2785 | return order; |
| 2786 | if (*name) { |
| 2787 | int j, len; |
| 2788 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2789 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2790 | trash.area[j] = tolower(name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2791 | if (j >= trash.size) |
| 2792 | return order; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2793 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2794 | |
| 2795 | /* Check for duplicates. */ |
| 2796 | if (wild) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2797 | node = ebst_lookup(&s->sni_w_ctx, trash.area); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2798 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2799 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2800 | for (; node; node = ebmb_next_dup(node)) { |
| 2801 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2802 | if (sc->ctx == ctx && sc->conf == conf && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2803 | return order; |
| 2804 | } |
| 2805 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2806 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2807 | if (!sc) |
| 2808 | return order; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2809 | memcpy(sc->name.key, trash.area, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2810 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2811 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2812 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2813 | sc->order = order++; |
| 2814 | sc->neg = neg; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 2815 | if (kinfo.sig != TLSEXT_signature_anonymous) |
| 2816 | SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2817 | if (wild) |
| 2818 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 2819 | else |
| 2820 | ebst_insert(&s->sni_ctx, &sc->name); |
| 2821 | } |
| 2822 | return order; |
| 2823 | } |
| 2824 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2825 | |
| 2826 | /* The following code is used for loading multiple crt files into |
| 2827 | * SSL_CTX's based on CN/SAN |
| 2828 | */ |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 2829 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(LIBRESSL_VERSION_NUMBER) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2830 | /* This is used to preload the certifcate, private key |
| 2831 | * and Cert Chain of a file passed in via the crt |
| 2832 | * argument |
| 2833 | * |
| 2834 | * This way, we do not have to read the file multiple times |
| 2835 | */ |
| 2836 | struct cert_key_and_chain { |
| 2837 | X509 *cert; |
| 2838 | EVP_PKEY *key; |
| 2839 | unsigned int num_chain_certs; |
| 2840 | /* This is an array of X509 pointers */ |
| 2841 | X509 **chain_certs; |
| 2842 | }; |
| 2843 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2844 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 2845 | |
| 2846 | struct key_combo_ctx { |
| 2847 | SSL_CTX *ctx; |
| 2848 | int order; |
| 2849 | }; |
| 2850 | |
| 2851 | /* Map used for processing multiple keypairs for a single purpose |
| 2852 | * |
| 2853 | * This maps CN/SNI name to certificate type |
| 2854 | */ |
| 2855 | struct sni_keytype { |
| 2856 | int keytypes; /* BITMASK for keytypes */ |
| 2857 | struct ebmb_node name; /* node holding the servername value */ |
| 2858 | }; |
| 2859 | |
| 2860 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2861 | /* Frees the contents of a cert_key_and_chain |
| 2862 | */ |
| 2863 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2864 | { |
| 2865 | int i; |
| 2866 | |
| 2867 | if (!ckch) |
| 2868 | return; |
| 2869 | |
| 2870 | /* Free the certificate and set pointer to NULL */ |
| 2871 | if (ckch->cert) |
| 2872 | X509_free(ckch->cert); |
| 2873 | ckch->cert = NULL; |
| 2874 | |
| 2875 | /* Free the key and set pointer to NULL */ |
| 2876 | if (ckch->key) |
| 2877 | EVP_PKEY_free(ckch->key); |
| 2878 | ckch->key = NULL; |
| 2879 | |
| 2880 | /* Free each certificate in the chain */ |
| 2881 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2882 | if (ckch->chain_certs[i]) |
| 2883 | X509_free(ckch->chain_certs[i]); |
| 2884 | } |
| 2885 | |
| 2886 | /* Free the chain obj itself and set to NULL */ |
| 2887 | if (ckch->num_chain_certs > 0) { |
| 2888 | free(ckch->chain_certs); |
| 2889 | ckch->num_chain_certs = 0; |
| 2890 | ckch->chain_certs = NULL; |
| 2891 | } |
| 2892 | |
| 2893 | } |
| 2894 | |
| 2895 | /* checks if a key and cert exists in the ckch |
| 2896 | */ |
| 2897 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 2898 | { |
| 2899 | return (ckch->cert != NULL && ckch->key != NULL); |
| 2900 | } |
| 2901 | |
| 2902 | |
| 2903 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 2904 | * This allows us to carry the contents of the file without having to |
| 2905 | * read the file multiple times. |
| 2906 | * |
| 2907 | * returns: |
| 2908 | * 0 on Success |
| 2909 | * 1 on SSL Failure |
| 2910 | * 2 on file not found |
| 2911 | */ |
| 2912 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 2913 | { |
| 2914 | |
| 2915 | BIO *in; |
| 2916 | X509 *ca = NULL; |
| 2917 | int ret = 1; |
| 2918 | |
| 2919 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2920 | |
| 2921 | in = BIO_new(BIO_s_file()); |
| 2922 | if (in == NULL) |
| 2923 | goto end; |
| 2924 | |
| 2925 | if (BIO_read_filename(in, path) <= 0) |
| 2926 | goto end; |
| 2927 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2928 | /* Read Private Key */ |
| 2929 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 2930 | if (ckch->key == NULL) { |
| 2931 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 2932 | err && *err ? *err : "", path); |
| 2933 | goto end; |
| 2934 | } |
| 2935 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2936 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 2937 | if (BIO_reset(in) == -1) { |
| 2938 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 2939 | err && *err ? *err : "", path); |
| 2940 | goto end; |
| 2941 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2942 | |
| 2943 | /* Read Certificate */ |
| 2944 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 2945 | if (ckch->cert == NULL) { |
| 2946 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 2947 | err && *err ? *err : "", path); |
| 2948 | goto end; |
| 2949 | } |
| 2950 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2951 | /* Read Certificate Chain */ |
| 2952 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 2953 | /* Grow the chain certs */ |
| 2954 | ckch->num_chain_certs++; |
| 2955 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 2956 | |
| 2957 | /* use - 1 here since we just incremented it above */ |
| 2958 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 2959 | } |
| 2960 | ret = ERR_get_error(); |
| 2961 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 2962 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 2963 | err && *err ? *err : "", path); |
| 2964 | ret = 1; |
| 2965 | goto end; |
| 2966 | } |
| 2967 | |
| 2968 | ret = 0; |
| 2969 | |
| 2970 | end: |
| 2971 | |
| 2972 | ERR_clear_error(); |
| 2973 | if (in) |
| 2974 | BIO_free(in); |
| 2975 | |
| 2976 | /* Something went wrong in one of the reads */ |
| 2977 | if (ret != 0) |
| 2978 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2979 | |
| 2980 | return ret; |
| 2981 | } |
| 2982 | |
| 2983 | /* Loads the info in ckch into ctx |
| 2984 | * Currently, this does not process any information about ocsp, dhparams or |
| 2985 | * sctl |
| 2986 | * Returns |
| 2987 | * 0 on success |
| 2988 | * 1 on failure |
| 2989 | */ |
| 2990 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2991 | { |
| 2992 | int i = 0; |
| 2993 | |
| 2994 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2995 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2996 | err && *err ? *err : "", path); |
| 2997 | return 1; |
| 2998 | } |
| 2999 | |
| 3000 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 3001 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 3002 | err && *err ? *err : "", path); |
| 3003 | return 1; |
| 3004 | } |
| 3005 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3006 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 3007 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 3008 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3009 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 3010 | err && *err ? *err : "", (i+1), path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3011 | return 1; |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3016 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3017 | err && *err ? *err : "", path); |
| 3018 | return 1; |
| 3019 | } |
| 3020 | |
| 3021 | return 0; |
| 3022 | } |
| 3023 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3024 | |
| 3025 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 3026 | { |
| 3027 | struct sni_keytype *s_kt = NULL; |
| 3028 | struct ebmb_node *node; |
| 3029 | int i; |
| 3030 | |
| 3031 | for (i = 0; i < trash.size; i++) { |
| 3032 | if (!str[i]) |
| 3033 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3034 | trash.area[i] = tolower(str[i]); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3035 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3036 | trash.area[i] = 0; |
| 3037 | node = ebst_lookup(sni_keytypes, trash.area); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3038 | if (!node) { |
| 3039 | /* CN not found in tree */ |
| 3040 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 3041 | /* Using memcpy here instead of strncpy. |
| 3042 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 3043 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 3044 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3045 | memcpy(s_kt->name.key, trash.area, i+1); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3046 | s_kt->keytypes = 0; |
| 3047 | ebst_insert(sni_keytypes, &s_kt->name); |
| 3048 | } else { |
| 3049 | /* CN found in tree */ |
| 3050 | s_kt = container_of(node, struct sni_keytype, name); |
| 3051 | } |
| 3052 | |
| 3053 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 3054 | s_kt->keytypes |= 1<<key_index; |
| 3055 | |
| 3056 | } |
| 3057 | |
| 3058 | |
| 3059 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 3060 | * If any are found, group these files into a set of SSL_CTX* |
| 3061 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 3062 | * |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3063 | * 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] | 3064 | * |
| 3065 | * Returns |
| 3066 | * 0 on success |
| 3067 | * 1 on failure |
| 3068 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3069 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3070 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3071 | { |
| 3072 | char fp[MAXPATHLEN+1] = {0}; |
| 3073 | int n = 0; |
| 3074 | int i = 0; |
| 3075 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 3076 | struct eb_root sni_keytypes_map = { {0} }; |
| 3077 | struct ebmb_node *node; |
| 3078 | struct ebmb_node *next; |
| 3079 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 3080 | * of keytypes |
| 3081 | */ |
| 3082 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 3083 | int rv = 0; |
| 3084 | X509_NAME *xname = NULL; |
| 3085 | char *str = NULL; |
| 3086 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3087 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 3088 | #endif |
| 3089 | |
| 3090 | /* Load all possible certs and keys */ |
| 3091 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3092 | struct stat buf; |
| 3093 | |
| 3094 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3095 | if (stat(fp, &buf) == 0) { |
| 3096 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
| 3097 | rv = 1; |
| 3098 | goto end; |
| 3099 | } |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | /* Process each ckch and update keytypes for each CN/SAN |
| 3104 | * for example, if CN/SAN www.a.com is associated with |
| 3105 | * certs with keytype 0 and 2, then at the end of the loop, |
| 3106 | * www.a.com will have: |
| 3107 | * keyindex = 0 | 1 | 4 = 5 |
| 3108 | */ |
| 3109 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3110 | |
| 3111 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 3112 | continue; |
| 3113 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3114 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 3115 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3116 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 3117 | } else { |
| 3118 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 3119 | * so the line that contains logic is marked via comments |
| 3120 | */ |
| 3121 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 3122 | i = -1; |
| 3123 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3124 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3125 | ASN1_STRING *value; |
| 3126 | value = X509_NAME_ENTRY_get_data(entry); |
| 3127 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3128 | /* Important line is here */ |
| 3129 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3130 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3131 | OPENSSL_free(str); |
| 3132 | str = NULL; |
| 3133 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3134 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3135 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3136 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3137 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3138 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 3139 | if (names) { |
| 3140 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3141 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3142 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3143 | if (name->type == GEN_DNS) { |
| 3144 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 3145 | /* Important line is here */ |
| 3146 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3147 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3148 | OPENSSL_free(str); |
| 3149 | str = NULL; |
| 3150 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3151 | } |
| 3152 | } |
| 3153 | } |
| 3154 | } |
| 3155 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 3156 | } |
| 3157 | |
| 3158 | /* If no files found, return error */ |
| 3159 | if (eb_is_empty(&sni_keytypes_map)) { |
| 3160 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3161 | err && *err ? *err : "", path); |
| 3162 | rv = 1; |
| 3163 | goto end; |
| 3164 | } |
| 3165 | |
| 3166 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 3167 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 3168 | * and add each CTX to the SNI tree |
| 3169 | * |
| 3170 | * Some math here: |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3171 | * There are 2^n - 1 possible combinations, each unique |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3172 | * combination is denoted by the key in the map. Each key |
| 3173 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 3174 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 3175 | * entry in the array to correspond to the unique combo (key) |
| 3176 | * associated with i. This unique key combo (i) will be associated |
| 3177 | * with combos[i-1] |
| 3178 | */ |
| 3179 | |
| 3180 | node = ebmb_first(&sni_keytypes_map); |
| 3181 | while (node) { |
| 3182 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3183 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3184 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3185 | |
| 3186 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 3187 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 3188 | cur_ctx = key_combos[i-1].ctx; |
| 3189 | |
| 3190 | if (cur_ctx == NULL) { |
| 3191 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3192 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3193 | if (cur_ctx == NULL) { |
| 3194 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3195 | err && *err ? *err : ""); |
| 3196 | rv = 1; |
| 3197 | goto end; |
| 3198 | } |
| 3199 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3200 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3201 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3202 | if (i & (1<<n)) { |
| 3203 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3204 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3205 | 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] | 3206 | SSL_CTX_free(cur_ctx); |
| 3207 | rv = 1; |
| 3208 | goto end; |
| 3209 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3210 | |
| 3211 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 3212 | /* Load OCSP Info into context */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3213 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3214 | if (err) |
| 3215 | 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] | 3216 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3217 | SSL_CTX_free(cur_ctx); |
| 3218 | rv = 1; |
| 3219 | goto end; |
| 3220 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3221 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3222 | ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3223 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | /* Load DH params into the ctx to support DHE keys */ |
| 3228 | #ifndef OPENSSL_NO_DH |
| 3229 | if (ssl_dh_ptr_index >= 0) |
| 3230 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 3231 | |
| 3232 | rv = ssl_sock_load_dh_params(cur_ctx, NULL); |
| 3233 | if (rv < 0) { |
| 3234 | if (err) |
| 3235 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3236 | *err ? *err : "", path); |
| 3237 | rv = 1; |
| 3238 | goto end; |
| 3239 | } |
| 3240 | #endif |
| 3241 | |
| 3242 | /* Update key_combos */ |
| 3243 | key_combos[i-1].ctx = cur_ctx; |
| 3244 | } |
| 3245 | |
| 3246 | /* Update SNI Tree */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3247 | 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] | 3248 | kinfo, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3249 | node = ebmb_next(node); |
| 3250 | } |
| 3251 | |
| 3252 | |
| 3253 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 3254 | if (!bind_conf->default_ctx) { |
| 3255 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 3256 | if (key_combos[i].ctx) { |
| 3257 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3258 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3259 | break; |
| 3260 | } |
| 3261 | } |
| 3262 | } |
| 3263 | |
| 3264 | end: |
| 3265 | |
| 3266 | if (names) |
| 3267 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 3268 | |
| 3269 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 3270 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 3271 | |
| 3272 | node = ebmb_first(&sni_keytypes_map); |
| 3273 | while (node) { |
| 3274 | next = ebmb_next(node); |
| 3275 | ebmb_delete(node); |
| 3276 | node = next; |
| 3277 | } |
| 3278 | |
| 3279 | return rv; |
| 3280 | } |
| 3281 | #else |
| 3282 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3283 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3284 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3285 | { |
| 3286 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3287 | err && *err ? *err : "", path, strerror(errno)); |
| 3288 | return 1; |
| 3289 | } |
| 3290 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3291 | #endif /* #if OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
| 3292 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3293 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 3294 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 3295 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3296 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, |
| 3297 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3298 | { |
| 3299 | BIO *in; |
| 3300 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3301 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3302 | int ret = -1; |
| 3303 | int order = 0; |
| 3304 | X509_NAME *xname; |
| 3305 | char *str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3306 | pem_password_cb *passwd_cb; |
| 3307 | void *passwd_cb_userdata; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3308 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3309 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3310 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3311 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3312 | STACK_OF(GENERAL_NAME) *names; |
| 3313 | #endif |
| 3314 | |
| 3315 | in = BIO_new(BIO_s_file()); |
| 3316 | if (in == NULL) |
| 3317 | goto end; |
| 3318 | |
| 3319 | if (BIO_read_filename(in, file) <= 0) |
| 3320 | goto end; |
| 3321 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3322 | |
| 3323 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 3324 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 3325 | |
| 3326 | 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] | 3327 | if (x == NULL) |
| 3328 | goto end; |
| 3329 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3330 | pkey = X509_get_pubkey(x); |
| 3331 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3332 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3333 | switch(EVP_PKEY_base_id(pkey)) { |
| 3334 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3335 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3336 | break; |
| 3337 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3338 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3339 | break; |
| 3340 | case EVP_PKEY_DSA: |
| 3341 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3342 | break; |
| 3343 | } |
| 3344 | EVP_PKEY_free(pkey); |
| 3345 | } |
| 3346 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3347 | if (fcount) { |
| 3348 | while (fcount--) |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3349 | 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] | 3350 | } |
| 3351 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3352 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3353 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 3354 | if (names) { |
| 3355 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3356 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3357 | if (name->type == GEN_DNS) { |
| 3358 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3359 | 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] | 3360 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3361 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3362 | } |
| 3363 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3364 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3365 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3366 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3367 | xname = X509_get_subject_name(x); |
| 3368 | i = -1; |
| 3369 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3370 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3371 | ASN1_STRING *value; |
| 3372 | |
| 3373 | value = X509_NAME_ENTRY_get_data(entry); |
| 3374 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3375 | 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] | 3376 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3377 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 3382 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 3383 | goto end; |
| 3384 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3385 | #ifdef SSL_CTX_clear_extra_chain_certs |
| 3386 | SSL_CTX_clear_extra_chain_certs(ctx); |
| 3387 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3388 | if (ctx->extra_certs != NULL) { |
| 3389 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 3390 | ctx->extra_certs = NULL; |
| 3391 | } |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3392 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3393 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3394 | 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] | 3395 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3396 | X509_free(ca); |
| 3397 | goto end; |
| 3398 | } |
| 3399 | } |
| 3400 | |
| 3401 | err = ERR_get_error(); |
| 3402 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3403 | /* we successfully reached the last cert in the file */ |
| 3404 | ret = 1; |
| 3405 | } |
| 3406 | ERR_clear_error(); |
| 3407 | |
| 3408 | end: |
| 3409 | if (x) |
| 3410 | X509_free(x); |
| 3411 | |
| 3412 | if (in) |
| 3413 | BIO_free(in); |
| 3414 | |
| 3415 | return ret; |
| 3416 | } |
| 3417 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3418 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3419 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3420 | { |
| 3421 | int ret; |
| 3422 | SSL_CTX *ctx; |
| 3423 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3424 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3425 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3426 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3427 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3428 | return 1; |
| 3429 | } |
| 3430 | |
| 3431 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3432 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 3433 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3434 | SSL_CTX_free(ctx); |
| 3435 | return 1; |
| 3436 | } |
| 3437 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3438 | 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] | 3439 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3440 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 3441 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3442 | if (ret < 0) /* serious error, must do that ourselves */ |
| 3443 | SSL_CTX_free(ctx); |
| 3444 | return 1; |
| 3445 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 3446 | |
| 3447 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3448 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3449 | err && *err ? *err : "", path); |
| 3450 | return 1; |
| 3451 | } |
| 3452 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3453 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3454 | * the tree, so it will be discovered and cleaned in time. |
| 3455 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3456 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3457 | /* store a NULL pointer to indicate we have not yet loaded |
| 3458 | a custom DH param file */ |
| 3459 | if (ssl_dh_ptr_index >= 0) { |
| 3460 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3461 | } |
| 3462 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3463 | ret = ssl_sock_load_dh_params(ctx, path); |
| 3464 | if (ret < 0) { |
| 3465 | if (err) |
| 3466 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3467 | *err ? *err : "", path); |
| 3468 | return 1; |
| 3469 | } |
| 3470 | #endif |
| 3471 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 3472 | #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] | 3473 | ret = ssl_sock_load_ocsp(ctx, path); |
| 3474 | if (ret < 0) { |
| 3475 | if (err) |
| 3476 | 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", |
| 3477 | *err ? *err : "", path); |
| 3478 | return 1; |
| 3479 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3480 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3481 | ssl_sock_set_ocsp_response_from_file(ctx, path); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3482 | #endif |
| 3483 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 3484 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 3485 | if (sctl_ex_index >= 0) { |
| 3486 | ret = ssl_sock_load_sctl(ctx, path); |
| 3487 | if (ret < 0) { |
| 3488 | if (err) |
| 3489 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 3490 | *err ? *err : "", path); |
| 3491 | return 1; |
| 3492 | } |
| 3493 | } |
| 3494 | #endif |
| 3495 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3496 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3497 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3498 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3499 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3500 | return 1; |
| 3501 | } |
| 3502 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3503 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3504 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3505 | bind_conf->default_ssl_conf = ssl_conf; |
| 3506 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3507 | |
| 3508 | return 0; |
| 3509 | } |
| 3510 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3511 | 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] | 3512 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3513 | struct dirent **de_list; |
| 3514 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3515 | DIR *dir; |
| 3516 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 3517 | char *end; |
| 3518 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3519 | int cfgerr = 0; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3520 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3521 | int is_bundle; |
| 3522 | int j; |
| 3523 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3524 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3525 | if (stat(path, &buf) == 0) { |
| 3526 | dir = opendir(path); |
| 3527 | if (!dir) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3528 | 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] | 3529 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3530 | /* strip trailing slashes, including first one */ |
| 3531 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 3532 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3533 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3534 | n = scandir(path, &de_list, 0, alphasort); |
| 3535 | if (n < 0) { |
| 3536 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 3537 | err && *err ? *err : "", path, strerror(errno)); |
| 3538 | cfgerr++; |
| 3539 | } |
| 3540 | else { |
| 3541 | for (i = 0; i < n; i++) { |
| 3542 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 3543 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3544 | end = strrchr(de->d_name, '.'); |
| 3545 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 3546 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3547 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3548 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 3549 | if (stat(fp, &buf) != 0) { |
| 3550 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3551 | err && *err ? *err : "", fp, strerror(errno)); |
| 3552 | cfgerr++; |
| 3553 | goto ignore_entry; |
| 3554 | } |
| 3555 | if (!S_ISREG(buf.st_mode)) |
| 3556 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3557 | |
| 3558 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3559 | is_bundle = 0; |
| 3560 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 3561 | |
| 3562 | if (end) { |
| 3563 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 3564 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 3565 | is_bundle = 1; |
| 3566 | break; |
| 3567 | } |
| 3568 | } |
| 3569 | |
| 3570 | if (is_bundle) { |
| 3571 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 3572 | int dp_len; |
| 3573 | |
| 3574 | dp_len = end - de->d_name; |
| 3575 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 3576 | |
| 3577 | /* increment i and free de until we get to a non-bundle cert |
| 3578 | * Note here that we look at de_list[i + 1] before freeing de |
| 3579 | * this is important since ignore_entry will free de |
| 3580 | */ |
| 3581 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 3582 | free(de); |
| 3583 | i++; |
| 3584 | de = de_list[i]; |
| 3585 | } |
| 3586 | |
| 3587 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
Emeric Brun | eb155b6 | 2018-08-16 15:11:12 +0200 | [diff] [blame] | 3588 | cfgerr += ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3589 | |
| 3590 | /* Successfully processed the bundle */ |
| 3591 | goto ignore_entry; |
| 3592 | } |
| 3593 | } |
| 3594 | |
| 3595 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3596 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3597 | ignore_entry: |
| 3598 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3599 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3600 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3601 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3602 | closedir(dir); |
| 3603 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3604 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3605 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3606 | cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3607 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3608 | return cfgerr; |
| 3609 | } |
| 3610 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3611 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3612 | * done once. Zero is returned if the operation fails. No error is returned |
| 3613 | * if the random is said as not implemented, because we expect that openssl |
| 3614 | * will use another method once needed. |
| 3615 | */ |
| 3616 | static int ssl_initialize_random() |
| 3617 | { |
| 3618 | unsigned char random; |
| 3619 | static int random_initialized = 0; |
| 3620 | |
| 3621 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3622 | random_initialized = 1; |
| 3623 | |
| 3624 | return random_initialized; |
| 3625 | } |
| 3626 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3627 | /* release ssl bind conf */ |
| 3628 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3629 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3630 | if (conf) { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 3631 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3632 | free(conf->npn_str); |
| 3633 | conf->npn_str = NULL; |
| 3634 | #endif |
| 3635 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 3636 | free(conf->alpn_str); |
| 3637 | conf->alpn_str = NULL; |
| 3638 | #endif |
| 3639 | free(conf->ca_file); |
| 3640 | conf->ca_file = NULL; |
| 3641 | free(conf->crl_file); |
| 3642 | conf->crl_file = NULL; |
| 3643 | free(conf->ciphers); |
| 3644 | conf->ciphers = NULL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 3645 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 3646 | free(conf->ciphersuites); |
| 3647 | conf->ciphersuites = NULL; |
| 3648 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3649 | free(conf->curves); |
| 3650 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3651 | free(conf->ecdhe); |
| 3652 | conf->ecdhe = NULL; |
| 3653 | } |
| 3654 | } |
| 3655 | |
| 3656 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 3657 | { |
| 3658 | char thisline[CRT_LINESIZE]; |
| 3659 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3660 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3661 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3662 | int linenum = 0; |
| 3663 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3664 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3665 | if ((f = fopen(file, "r")) == NULL) { |
| 3666 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3667 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3668 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3669 | |
| 3670 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3671 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3672 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3673 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3674 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3675 | char *crt_path; |
| 3676 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3677 | |
| 3678 | linenum++; |
| 3679 | end = line + strlen(line); |
| 3680 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 3681 | /* Check if we reached the limit and the last char is not \n. |
| 3682 | * Watch out for the last line without the terminating '\n'! |
| 3683 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3684 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 3685 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3686 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3687 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3688 | } |
| 3689 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3690 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3691 | newarg = 1; |
| 3692 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3693 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 3694 | /* end of string, end of loop */ |
| 3695 | *line = 0; |
| 3696 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3697 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3698 | newarg = 1; |
| 3699 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3700 | } else if (*line == '[') { |
| 3701 | if (ssl_b) { |
| 3702 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 3703 | cfgerr = 1; |
| 3704 | break; |
| 3705 | } |
| 3706 | if (!arg) { |
| 3707 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 3708 | cfgerr = 1; |
| 3709 | break; |
| 3710 | } |
| 3711 | ssl_b = arg; |
| 3712 | newarg = 1; |
| 3713 | *line = 0; |
| 3714 | } else if (*line == ']') { |
| 3715 | if (ssl_e) { |
| 3716 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3717 | cfgerr = 1; |
| 3718 | break; |
| 3719 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3720 | if (!ssl_b) { |
| 3721 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 3722 | cfgerr = 1; |
| 3723 | break; |
| 3724 | } |
| 3725 | ssl_e = arg; |
| 3726 | newarg = 1; |
| 3727 | *line = 0; |
| 3728 | } else if (newarg) { |
| 3729 | if (arg == MAX_CRT_ARGS) { |
| 3730 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 3731 | cfgerr = 1; |
| 3732 | break; |
| 3733 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3734 | newarg = 0; |
| 3735 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3736 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3737 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3738 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3739 | if (cfgerr) |
| 3740 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3741 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3742 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3743 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3744 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3745 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3746 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3747 | crt_path = args[0]; |
| 3748 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 3749 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 3750 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 3751 | crt_path, linenum, file); |
| 3752 | cfgerr = 1; |
| 3753 | break; |
| 3754 | } |
| 3755 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 3756 | crt_path = path; |
| 3757 | } |
| 3758 | |
| 3759 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 3760 | cur_arg = ssl_b ? ssl_b : 1; |
| 3761 | while (cur_arg < ssl_e) { |
| 3762 | newarg = 0; |
| 3763 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 3764 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 3765 | newarg = 1; |
| 3766 | cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
| 3767 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 3768 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 3769 | args[cur_arg], linenum, file); |
| 3770 | cfgerr = 1; |
| 3771 | } |
| 3772 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 3773 | break; |
| 3774 | } |
| 3775 | } |
| 3776 | if (!cfgerr && !newarg) { |
| 3777 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 3778 | args[cur_arg], linenum, file); |
| 3779 | cfgerr = 1; |
| 3780 | break; |
| 3781 | } |
| 3782 | } |
| 3783 | if (cfgerr) { |
| 3784 | ssl_sock_free_ssl_conf(ssl_conf); |
| 3785 | free(ssl_conf); |
| 3786 | ssl_conf = NULL; |
| 3787 | break; |
| 3788 | } |
| 3789 | |
| 3790 | if (stat(crt_path, &buf) == 0) { |
| 3791 | cfgerr = ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf, |
| 3792 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3793 | } else { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3794 | cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf, |
| 3795 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3796 | } |
| 3797 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3798 | if (cfgerr) { |
| 3799 | 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] | 3800 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3801 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3802 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3803 | fclose(f); |
| 3804 | return cfgerr; |
| 3805 | } |
| 3806 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3807 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3808 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3809 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3810 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3811 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3812 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3813 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3814 | SSL_OP_NO_SSLv2 | |
| 3815 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3816 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3817 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3818 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 3819 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3820 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3821 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3822 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3823 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3824 | SSL_MODE_RELEASE_BUFFERS | |
| 3825 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3826 | 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] | 3827 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3828 | int flags = MC_SSL_O_ALL; |
| 3829 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3830 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3831 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3832 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3833 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3834 | 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] | 3835 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3836 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3837 | 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] | 3838 | else |
| 3839 | flags = conf_ssl_methods->flags; |
| 3840 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3841 | min = conf_ssl_methods->min; |
| 3842 | max = conf_ssl_methods->max; |
| 3843 | /* start with TLSv10 to remove SSLv3 per default */ |
| 3844 | if (!min && (!max || max >= CONF_TLSV10)) |
| 3845 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3846 | /* 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] | 3847 | if (min) |
| 3848 | flags |= (methodVersions[min].flag - 1); |
| 3849 | if (max) |
| 3850 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3851 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3852 | min = max = CONF_TLSV_NONE; |
| 3853 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3854 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3855 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3856 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3857 | if (min) { |
| 3858 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3859 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 3860 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3861 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3862 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3863 | hole = 0; |
| 3864 | } |
| 3865 | max = i; |
| 3866 | } |
| 3867 | else { |
| 3868 | min = max = i; |
| 3869 | } |
| 3870 | } |
| 3871 | else { |
| 3872 | if (min) |
| 3873 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3874 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3875 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3876 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3877 | 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] | 3878 | cfgerr += 1; |
| 3879 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3880 | /* save real min/max in bind_conf */ |
| 3881 | conf_ssl_methods->min = min; |
| 3882 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3883 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 3884 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3885 | /* 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] | 3886 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3887 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3888 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3889 | else |
| 3890 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3891 | if (flags & methodVersions[i].flag) |
| 3892 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3893 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3894 | /* 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] | 3895 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3896 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3897 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3898 | |
| 3899 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3900 | options |= SSL_OP_NO_TICKET; |
| 3901 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3902 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 3903 | |
| 3904 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 3905 | options |= SSL_OP_NO_RENEGOTIATION; |
| 3906 | #endif |
| 3907 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3908 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3909 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 3910 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3911 | if (global_ssl.async) |
| 3912 | mode |= SSL_MODE_ASYNC; |
| 3913 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3914 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3915 | if (global_ssl.life_time) |
| 3916 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3917 | |
| 3918 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3919 | #ifdef OPENSSL_IS_BORINGSSL |
| 3920 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3921 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3922 | #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 3923 | if (bind_conf->ssl_conf.early_data) { |
| 3924 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
| 3925 | SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite); |
| 3926 | } |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3927 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 3928 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3929 | #else |
| 3930 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3931 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 3932 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3933 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3934 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3935 | } |
| 3936 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3937 | |
| 3938 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 3939 | { |
| 3940 | if (first == block) { |
| 3941 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3942 | if (first->len > 0) |
| 3943 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 3944 | } |
| 3945 | } |
| 3946 | |
| 3947 | /* return first block from sh_ssl_sess */ |
| 3948 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 3949 | { |
| 3950 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 3951 | |
| 3952 | } |
| 3953 | |
| 3954 | /* store a session into the cache |
| 3955 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 3956 | * data: asn1 encoded session |
| 3957 | * data_len: asn1 encoded session length |
| 3958 | * Returns 1 id session was stored (else 0) |
| 3959 | */ |
| 3960 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 3961 | { |
| 3962 | struct shared_block *first; |
| 3963 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 3964 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 3965 | 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] | 3966 | if (!first) { |
| 3967 | /* Could not retrieve enough free blocks to store that session */ |
| 3968 | return 0; |
| 3969 | } |
| 3970 | |
| 3971 | /* STORE the key in the first elem */ |
| 3972 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3973 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 3974 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3975 | |
| 3976 | /* it returns the already existing node |
| 3977 | or current node if none, never returns null */ |
| 3978 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 3979 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 3980 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 3981 | /* release the reserved row */ |
| 3982 | shctx_row_dec_hot(ssl_shctx, first); |
| 3983 | /* replace the previous session already in the tree */ |
| 3984 | sh_ssl_sess = oldsh_ssl_sess; |
| 3985 | /* ignore the previous session data, only use the header */ |
| 3986 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 3987 | shctx_row_inc_hot(ssl_shctx, first); |
| 3988 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3989 | } |
| 3990 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 3991 | 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] | 3992 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3993 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 3994 | } |
| 3995 | |
| 3996 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3997 | |
| 3998 | return 1; |
| 3999 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4000 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4001 | /* SSL callback used when a new session is created while connecting to a server */ |
| 4002 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 4003 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4004 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4005 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4006 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4007 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4008 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4009 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 4010 | int len; |
| 4011 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4012 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4013 | len = i2d_SSL_SESSION(sess, NULL); |
| 4014 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 4015 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 4016 | } else { |
| 4017 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 4018 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 4019 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 4020 | } |
| 4021 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 4022 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 4023 | &ptr); |
| 4024 | } |
| 4025 | } else { |
| 4026 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 4027 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 4028 | } |
| 4029 | |
| 4030 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4031 | } |
| 4032 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4033 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4034 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4035 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4036 | { |
| 4037 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 4038 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 4039 | unsigned char *p; |
| 4040 | int data_len; |
| 4041 | unsigned int sid_length, sid_ctx_length; |
| 4042 | const unsigned char *sid_data; |
| 4043 | const unsigned char *sid_ctx_data; |
| 4044 | |
| 4045 | /* Session id is already stored in to key and session id is known |
| 4046 | * so we dont store it to keep size. |
| 4047 | */ |
| 4048 | |
| 4049 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4050 | sid_ctx_data = SSL_SESSION_get0_id_context(sess, &sid_ctx_length); |
| 4051 | SSL_SESSION_set1_id(sess, sid_data, 0); |
| 4052 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, 0); |
| 4053 | |
| 4054 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 4055 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 4056 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 4057 | goto err; |
| 4058 | |
| 4059 | p = encsess; |
| 4060 | |
| 4061 | /* process ASN1 session encoding before the lock */ |
| 4062 | i2d_SSL_SESSION(sess, &p); |
| 4063 | |
| 4064 | memcpy(encid, sid_data, sid_length); |
| 4065 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 4066 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 4067 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4068 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4069 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4070 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4071 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4072 | err: |
| 4073 | /* reset original length values */ |
| 4074 | SSL_SESSION_set1_id(sess, sid_data, sid_length); |
| 4075 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, sid_ctx_length); |
| 4076 | |
| 4077 | return 0; /* do not increment session reference count */ |
| 4078 | } |
| 4079 | |
| 4080 | /* 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] | 4081 | 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] | 4082 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4083 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4084 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 4085 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4086 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4087 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4088 | |
| 4089 | global.shctx_lookups++; |
| 4090 | |
| 4091 | /* allow the session to be freed automatically by openssl */ |
| 4092 | *do_copy = 0; |
| 4093 | |
| 4094 | /* tree key is zeros padded sessionid */ |
| 4095 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4096 | memcpy(tmpkey, key, key_len); |
| 4097 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 4098 | key = tmpkey; |
| 4099 | } |
| 4100 | |
| 4101 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4102 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4103 | |
| 4104 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4105 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 4106 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4107 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4108 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4109 | global.shctx_misses++; |
| 4110 | return NULL; |
| 4111 | } |
| 4112 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4113 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 4114 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4115 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4116 | 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] | 4117 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4118 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4119 | |
| 4120 | /* decode ASN1 session */ |
| 4121 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4122 | 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] | 4123 | /* Reset session id and session id contenxt */ |
| 4124 | if (sess) { |
| 4125 | SSL_SESSION_set1_id(sess, key, key_len); |
| 4126 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4127 | } |
| 4128 | |
| 4129 | return sess; |
| 4130 | } |
| 4131 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4132 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4133 | /* 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] | 4134 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4135 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4136 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4137 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 4138 | unsigned int sid_length; |
| 4139 | const unsigned char *sid_data; |
| 4140 | (void)ctx; |
| 4141 | |
| 4142 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4143 | /* tree key is zeros padded sessionid */ |
| 4144 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4145 | memcpy(tmpkey, sid_data, sid_length); |
| 4146 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 4147 | sid_data = tmpkey; |
| 4148 | } |
| 4149 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4150 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4151 | |
| 4152 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4153 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 4154 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4155 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4156 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4157 | } |
| 4158 | |
| 4159 | /* unlock cache */ |
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 | } |
| 4162 | |
| 4163 | /* Set session cache mode to server and disable openssl internal cache. |
| 4164 | * Set shared cache callbacks on an ssl context. |
| 4165 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4166 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4167 | { |
| 4168 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4169 | |
| 4170 | if (!ssl_shctx) { |
| 4171 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4172 | return; |
| 4173 | } |
| 4174 | |
| 4175 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4176 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4177 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4178 | |
| 4179 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4180 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4181 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4182 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4183 | } |
| 4184 | |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4185 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
| 4186 | { |
| 4187 | struct proxy *curproxy = bind_conf->frontend; |
| 4188 | int cfgerr = 0; |
| 4189 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4190 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4191 | const char *conf_ciphers; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4192 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 4193 | const char *conf_ciphersuites; |
| 4194 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4195 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4196 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4197 | if (ssl_conf) { |
| 4198 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4199 | int i, min, max; |
| 4200 | int flags = MC_SSL_O_ALL; |
| 4201 | |
| 4202 | /* 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] | 4203 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4204 | 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] | 4205 | if (min) |
| 4206 | flags |= (methodVersions[min].flag - 1); |
| 4207 | if (max) |
| 4208 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4209 | min = max = CONF_TLSV_NONE; |
| 4210 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4211 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4212 | if (min) |
| 4213 | max = i; |
| 4214 | else |
| 4215 | min = max = i; |
| 4216 | } |
| 4217 | /* save real min/max */ |
| 4218 | conf_ssl_methods->min = min; |
| 4219 | conf_ssl_methods->max = max; |
| 4220 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4221 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4222 | 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] | 4223 | cfgerr += 1; |
| 4224 | } |
| 4225 | } |
| 4226 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4227 | 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] | 4228 | case SSL_SOCK_VERIFY_NONE: |
| 4229 | verify = SSL_VERIFY_NONE; |
| 4230 | break; |
| 4231 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4232 | verify = SSL_VERIFY_PEER; |
| 4233 | break; |
| 4234 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4235 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4236 | break; |
| 4237 | } |
| 4238 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4239 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4240 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 4241 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 4242 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4243 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4244 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4245 | ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
| 4246 | 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] | 4247 | cfgerr++; |
| 4248 | } |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 4249 | if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) { |
| 4250 | /* set CA names for client cert request, function returns void */ |
| 4251 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file)); |
| 4252 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4253 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4254 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4255 | ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4256 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4257 | cfgerr++; |
| 4258 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4259 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4260 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4261 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4262 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4263 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4264 | ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4265 | 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] | 4266 | cfgerr++; |
| 4267 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4268 | else { |
| 4269 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4270 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4271 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4272 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4273 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4274 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4275 | #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] | 4276 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4277 | 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] | 4278 | ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4279 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4280 | cfgerr++; |
| 4281 | } |
| 4282 | } |
| 4283 | #endif |
| 4284 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4285 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4286 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4287 | if (conf_ciphers && |
| 4288 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4289 | ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4290 | 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] | 4291 | cfgerr++; |
| 4292 | } |
| 4293 | |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4294 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 4295 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 4296 | if (conf_ciphersuites && |
| 4297 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
| 4298 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 4299 | curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4300 | cfgerr++; |
| 4301 | } |
| 4302 | #endif |
| 4303 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4304 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4305 | /* If tune.ssl.default-dh-param has not been set, |
| 4306 | neither has ssl-default-dh-file and no static DH |
| 4307 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4308 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4309 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4310 | (ssl_dh_ptr_index == -1 || |
| 4311 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4312 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 4313 | const SSL_CIPHER * cipher = NULL; |
| 4314 | char cipher_description[128]; |
| 4315 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 4316 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 4317 | which is not ephemeral DH. */ |
| 4318 | const char dhe_description[] = " Kx=DH "; |
| 4319 | const char dhe_export_description[] = " Kx=DH("; |
| 4320 | int idx = 0; |
| 4321 | int dhe_found = 0; |
| 4322 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4323 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4324 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4325 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4326 | if (ssl) { |
| 4327 | ciphers = SSL_get_ciphers(ssl); |
| 4328 | |
| 4329 | if (ciphers) { |
| 4330 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 4331 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 4332 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 4333 | if (strstr(cipher_description, dhe_description) != NULL || |
| 4334 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 4335 | dhe_found = 1; |
| 4336 | break; |
| 4337 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 4338 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4339 | } |
| 4340 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4341 | SSL_free(ssl); |
| 4342 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4343 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4344 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4345 | if (dhe_found) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4346 | 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] | 4347 | } |
| 4348 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4349 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4350 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4351 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4352 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4353 | if (local_dh_1024 == NULL) { |
| 4354 | local_dh_1024 = ssl_get_dh_1024(); |
| 4355 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4356 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4357 | if (local_dh_2048 == NULL) { |
| 4358 | local_dh_2048 = ssl_get_dh_2048(); |
| 4359 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4360 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4361 | if (local_dh_4096 == NULL) { |
| 4362 | local_dh_4096 = ssl_get_dh_4096(); |
| 4363 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4364 | } |
| 4365 | } |
| 4366 | } |
| 4367 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4368 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4369 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4370 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4371 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4372 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4373 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4374 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4375 | ssl_conf_cur = NULL; |
| 4376 | if (ssl_conf && ssl_conf->npn_str) |
| 4377 | ssl_conf_cur = ssl_conf; |
| 4378 | else if (bind_conf->ssl_conf.npn_str) |
| 4379 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4380 | if (ssl_conf_cur) |
| 4381 | 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] | 4382 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4383 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4384 | ssl_conf_cur = NULL; |
| 4385 | if (ssl_conf && ssl_conf->alpn_str) |
| 4386 | ssl_conf_cur = ssl_conf; |
| 4387 | else if (bind_conf->ssl_conf.alpn_str) |
| 4388 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4389 | if (ssl_conf_cur) |
| 4390 | 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] | 4391 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4392 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 4393 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4394 | if (conf_curves) { |
| 4395 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4396 | ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4397 | 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] | 4398 | cfgerr++; |
| 4399 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4400 | #if defined(SSL_CTX_set_ecdh_auto) |
| 4401 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
| 4402 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4403 | } |
| 4404 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4405 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4406 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4407 | int i; |
| 4408 | EC_KEY *ecdh; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4409 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4410 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4411 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4412 | NULL); |
| 4413 | |
| 4414 | if (ecdhe == NULL) { |
| 4415 | SSL_CTX_set_dh_auto(ctx, 1); |
| 4416 | return cfgerr; |
| 4417 | } |
| 4418 | #else |
| 4419 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4420 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4421 | ECDHE_DEFAULT_CURVE); |
| 4422 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4423 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4424 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4425 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4426 | ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4427 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4428 | cfgerr++; |
| 4429 | } |
| 4430 | else { |
| 4431 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4432 | EC_KEY_free(ecdh); |
| 4433 | } |
| 4434 | } |
| 4435 | #endif |
| 4436 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4437 | return cfgerr; |
| 4438 | } |
| 4439 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4440 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4441 | { |
| 4442 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4443 | size_t prefixlen, suffixlen; |
| 4444 | |
| 4445 | /* Trivial case */ |
| 4446 | if (strcmp(pattern, hostname) == 0) |
| 4447 | return 1; |
| 4448 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4449 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4450 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4451 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4452 | pattern_wildcard = NULL; |
| 4453 | pattern_left_label_end = pattern; |
| 4454 | while (*pattern_left_label_end != '.') { |
| 4455 | switch (*pattern_left_label_end) { |
| 4456 | case 0: |
| 4457 | /* End of label not found */ |
| 4458 | return 0; |
| 4459 | case '*': |
| 4460 | /* If there is more than one wildcards */ |
| 4461 | if (pattern_wildcard) |
| 4462 | return 0; |
| 4463 | pattern_wildcard = pattern_left_label_end; |
| 4464 | break; |
| 4465 | } |
| 4466 | pattern_left_label_end++; |
| 4467 | } |
| 4468 | |
| 4469 | /* If it's not trivial and there is no wildcard, it can't |
| 4470 | * match */ |
| 4471 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4472 | return 0; |
| 4473 | |
| 4474 | /* Make sure all labels match except the leftmost */ |
| 4475 | hostname_left_label_end = strchr(hostname, '.'); |
| 4476 | if (!hostname_left_label_end |
| 4477 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 4478 | return 0; |
| 4479 | |
| 4480 | /* Make sure the leftmost label of the hostname is long enough |
| 4481 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4482 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4483 | return 0; |
| 4484 | |
| 4485 | /* Finally compare the string on either side of the |
| 4486 | * wildcard */ |
| 4487 | prefixlen = pattern_wildcard - pattern; |
| 4488 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4489 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 4490 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4491 | return 0; |
| 4492 | |
| 4493 | return 1; |
| 4494 | } |
| 4495 | |
| 4496 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4497 | { |
| 4498 | SSL *ssl; |
| 4499 | struct connection *conn; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4500 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4501 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4502 | |
| 4503 | int depth; |
| 4504 | X509 *cert; |
| 4505 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4506 | int i; |
| 4507 | X509_NAME *cert_subject; |
| 4508 | char *str; |
| 4509 | |
| 4510 | if (ok == 0) |
| 4511 | return ok; |
| 4512 | |
| 4513 | 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] | 4514 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4515 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4516 | /* We're checking if the provided hostnames match the desired one. The |
| 4517 | * desired hostname comes from the SNI we presented if any, or if not |
| 4518 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4519 | * directive. If neither is set, we don't care about the name so the |
| 4520 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4521 | */ |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4522 | servername = SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4523 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4524 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4525 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4526 | if (!servername) |
| 4527 | return ok; |
| 4528 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4529 | |
| 4530 | /* We only need to verify the CN on the actual server cert, |
| 4531 | * not the indirect CAs */ |
| 4532 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4533 | if (depth != 0) |
| 4534 | return ok; |
| 4535 | |
| 4536 | /* At this point, the cert is *not* OK unless we can find a |
| 4537 | * hostname match */ |
| 4538 | ok = 0; |
| 4539 | |
| 4540 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4541 | /* It seems like this might happen if verify peer isn't set */ |
| 4542 | if (!cert) |
| 4543 | return ok; |
| 4544 | |
| 4545 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4546 | if (alt_names) { |
| 4547 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4548 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4549 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4550 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 4551 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4552 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4553 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4554 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4555 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4556 | OPENSSL_free(str); |
| 4557 | } |
| 4558 | } |
| 4559 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4560 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4561 | } |
| 4562 | |
| 4563 | cert_subject = X509_get_subject_name(cert); |
| 4564 | i = -1; |
| 4565 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4566 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4567 | ASN1_STRING *value; |
| 4568 | value = X509_NAME_ENTRY_get_data(entry); |
| 4569 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4570 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4571 | OPENSSL_free(str); |
| 4572 | } |
| 4573 | } |
| 4574 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4575 | /* report the mismatch and indicate if SNI was used or not */ |
| 4576 | if (!ok && !conn->err_code) |
| 4577 | 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] | 4578 | return ok; |
| 4579 | } |
| 4580 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4581 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4582 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4583 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4584 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4585 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4586 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4587 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4588 | SSL_OP_NO_SSLv2 | |
| 4589 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4590 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4591 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4592 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4593 | SSL_MODE_RELEASE_BUFFERS | |
| 4594 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4595 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4596 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4597 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4598 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4599 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4600 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4601 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4602 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4603 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4604 | cfgerr++; |
| 4605 | } |
| 4606 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4607 | /* Automatic memory computations need to know we use SSL there */ |
| 4608 | global.ssl_used_backend = 1; |
| 4609 | |
| 4610 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4611 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4612 | 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] | 4613 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 4614 | curproxy->id, srv->id, |
| 4615 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4616 | cfgerr++; |
| 4617 | return cfgerr; |
| 4618 | } |
| 4619 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4620 | if (srv->use_ssl) |
| 4621 | srv->xprt = &ssl_sock; |
| 4622 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 4623 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4624 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4625 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4626 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4627 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 4628 | proxy_type_str(curproxy), curproxy->id, |
| 4629 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4630 | cfgerr++; |
| 4631 | return cfgerr; |
| 4632 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4633 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4634 | 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] | 4635 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 4636 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4637 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4638 | else |
| 4639 | flags = conf_ssl_methods->flags; |
| 4640 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4641 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 4642 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4643 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4644 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4645 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4646 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4647 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4648 | min = max = CONF_TLSV_NONE; |
| 4649 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4650 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4651 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4652 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4653 | if (min) { |
| 4654 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4655 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 4656 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4657 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4658 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4659 | hole = 0; |
| 4660 | } |
| 4661 | max = i; |
| 4662 | } |
| 4663 | else { |
| 4664 | min = max = i; |
| 4665 | } |
| 4666 | } |
| 4667 | else { |
| 4668 | if (min) |
| 4669 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4670 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4671 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4672 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4673 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4674 | cfgerr += 1; |
| 4675 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4676 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 4677 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4678 | /* 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] | 4679 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4680 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4681 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4682 | else |
| 4683 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4684 | if (flags & methodVersions[i].flag) |
| 4685 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4686 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4687 | /* 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] | 4688 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4689 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4690 | #endif |
| 4691 | |
| 4692 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4693 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4694 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4695 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 4696 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4697 | if (global_ssl.async) |
| 4698 | mode |= SSL_MODE_ASYNC; |
| 4699 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4700 | SSL_CTX_set_mode(ctx, mode); |
| 4701 | srv->ssl_ctx.ctx = ctx; |
| 4702 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4703 | if (srv->ssl_ctx.client_crt) { |
| 4704 | 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] | 4705 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4706 | proxy_type_str(curproxy), curproxy->id, |
| 4707 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4708 | cfgerr++; |
| 4709 | } |
| 4710 | 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] | 4711 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4712 | proxy_type_str(curproxy), curproxy->id, |
| 4713 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4714 | cfgerr++; |
| 4715 | } |
| 4716 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4717 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4718 | proxy_type_str(curproxy), curproxy->id, |
| 4719 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4720 | cfgerr++; |
| 4721 | } |
| 4722 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4723 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4724 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4725 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4726 | switch (srv->ssl_ctx.verify) { |
| 4727 | case SSL_SOCK_VERIFY_NONE: |
| 4728 | verify = SSL_VERIFY_NONE; |
| 4729 | break; |
| 4730 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4731 | verify = SSL_VERIFY_PEER; |
| 4732 | break; |
| 4733 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4734 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4735 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4736 | (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] | 4737 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4738 | if (srv->ssl_ctx.ca_file) { |
| 4739 | /* load CAfile to verify */ |
| 4740 | 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] | 4741 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n", |
| 4742 | curproxy->id, srv->id, |
| 4743 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4744 | cfgerr++; |
| 4745 | } |
| 4746 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4747 | else { |
| 4748 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4749 | 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", |
| 4750 | curproxy->id, srv->id, |
| 4751 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4752 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4753 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 4754 | curproxy->id, srv->id, |
| 4755 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4756 | cfgerr++; |
| 4757 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4758 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4759 | if (srv->ssl_ctx.crl_file) { |
| 4760 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4761 | |
| 4762 | 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] | 4763 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 4764 | curproxy->id, srv->id, |
| 4765 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4766 | cfgerr++; |
| 4767 | } |
| 4768 | else { |
| 4769 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4770 | } |
| 4771 | } |
| 4772 | #endif |
| 4773 | } |
| 4774 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4775 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 4776 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 4777 | 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] | 4778 | if (srv->ssl_ctx.ciphers && |
| 4779 | !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] | 4780 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4781 | curproxy->id, srv->id, |
| 4782 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4783 | cfgerr++; |
| 4784 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4785 | |
| 4786 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 4787 | if (srv->ssl_ctx.ciphersuites && |
| 4788 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
| 4789 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 4790 | curproxy->id, srv->id, |
| 4791 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 4792 | cfgerr++; |
| 4793 | } |
| 4794 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4795 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 4796 | if (srv->ssl_ctx.npn_str) |
| 4797 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 4798 | #endif |
| 4799 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4800 | if (srv->ssl_ctx.alpn_str) |
| 4801 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 4802 | #endif |
| 4803 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4804 | |
| 4805 | return cfgerr; |
| 4806 | } |
| 4807 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4808 | /* 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] | 4809 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4810 | * encountered. |
| 4811 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4812 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4813 | { |
| 4814 | struct ebmb_node *node; |
| 4815 | struct sni_ctx *sni; |
| 4816 | int err = 0; |
| 4817 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4818 | /* Automatic memory computations need to know we use SSL there */ |
| 4819 | global.ssl_used_frontend = 1; |
| 4820 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4821 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4822 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4823 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4824 | err++; |
| 4825 | } |
| 4826 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4827 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4828 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4829 | /* It should not be necessary to call this function, but it's |
| 4830 | necessary first to check and move all initialisation related |
| 4831 | to initial_ctx in ssl_sock_initial_ctx. */ |
| 4832 | err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx); |
| 4833 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4834 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4835 | 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] | 4836 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4837 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4838 | while (node) { |
| 4839 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4840 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4841 | /* only initialize the CTX on its first occurrence and |
| 4842 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4843 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4844 | node = ebmb_next(node); |
| 4845 | } |
| 4846 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4847 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4848 | while (node) { |
| 4849 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4850 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4851 | /* only initialize the CTX on its first occurrence and |
| 4852 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4853 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4854 | node = ebmb_next(node); |
| 4855 | } |
| 4856 | return err; |
| 4857 | } |
| 4858 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4859 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4860 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4861 | * alerts are directly emitted since the rest of the stack does it below. |
| 4862 | */ |
| 4863 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4864 | { |
| 4865 | struct proxy *px = bind_conf->frontend; |
| 4866 | int alloc_ctx; |
| 4867 | int err; |
| 4868 | |
| 4869 | if (!bind_conf->is_ssl) { |
| 4870 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4871 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 4872 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4873 | } |
| 4874 | return 0; |
| 4875 | } |
| 4876 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4877 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4878 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 4879 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4880 | } |
| 4881 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4882 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 4883 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4884 | return -1; |
| 4885 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4886 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 4887 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4888 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 4889 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4890 | sizeof(*sh_ssl_sess_tree), |
| 4891 | ((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] | 4892 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4893 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 4894 | 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"); |
| 4895 | else |
| 4896 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 4897 | return -1; |
| 4898 | } |
| 4899 | /* free block callback */ |
| 4900 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 4901 | /* init the root tree within the extra space */ |
| 4902 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 4903 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4904 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4905 | err = 0; |
| 4906 | /* initialize all certificate contexts */ |
| 4907 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 4908 | |
| 4909 | /* initialize CA variables if the certificates generation is enabled */ |
| 4910 | err += ssl_sock_load_ca(bind_conf); |
| 4911 | |
| 4912 | return -err; |
| 4913 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4914 | |
| 4915 | /* release ssl context allocated for servers. */ |
| 4916 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 4917 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4918 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4919 | if (srv->ssl_ctx.alpn_str) |
| 4920 | free(srv->ssl_ctx.alpn_str); |
| 4921 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 4922 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4923 | if (srv->ssl_ctx.npn_str) |
| 4924 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 4925 | #endif |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4926 | if (srv->ssl_ctx.ctx) |
| 4927 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 4928 | } |
| 4929 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4930 | /* 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] | 4931 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 4932 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4933 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4934 | { |
| 4935 | struct ebmb_node *node, *back; |
| 4936 | struct sni_ctx *sni; |
| 4937 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4938 | node = ebmb_first(&bind_conf->sni_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); |
| 4941 | back = ebmb_next(node); |
| 4942 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4943 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4944 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4945 | ssl_sock_free_ssl_conf(sni->conf); |
| 4946 | free(sni->conf); |
| 4947 | sni->conf = NULL; |
| 4948 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4949 | free(sni); |
| 4950 | node = back; |
| 4951 | } |
| 4952 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4953 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4954 | while (node) { |
| 4955 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4956 | back = ebmb_next(node); |
| 4957 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4958 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4959 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4960 | ssl_sock_free_ssl_conf(sni->conf); |
| 4961 | free(sni->conf); |
| 4962 | sni->conf = NULL; |
| 4963 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4964 | free(sni); |
| 4965 | node = back; |
| 4966 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4967 | SSL_CTX_free(bind_conf->initial_ctx); |
| 4968 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4969 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4970 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4971 | } |
| 4972 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4973 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 4974 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 4975 | { |
| 4976 | ssl_sock_free_ca(bind_conf); |
| 4977 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4978 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4979 | free(bind_conf->ca_sign_file); |
| 4980 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 4981 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4982 | free(bind_conf->keys_ref->filename); |
| 4983 | free(bind_conf->keys_ref->tlskeys); |
| 4984 | LIST_DEL(&bind_conf->keys_ref->list); |
| 4985 | free(bind_conf->keys_ref); |
| 4986 | } |
| 4987 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4988 | bind_conf->ca_sign_pass = NULL; |
| 4989 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4990 | } |
| 4991 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4992 | /* Load CA cert file and private key used to generate certificates */ |
| 4993 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4994 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4995 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4996 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4997 | FILE *fp; |
| 4998 | X509 *cacert = NULL; |
| 4999 | EVP_PKEY *capkey = NULL; |
| 5000 | int err = 0; |
| 5001 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 5002 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5003 | return err; |
| 5004 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5005 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5006 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5007 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5008 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5009 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5010 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 5011 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5012 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5013 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5014 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 5015 | "no CA certificate File configured at [%s:%d].\n", |
| 5016 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5017 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5018 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5019 | |
| 5020 | /* read in the CA certificate */ |
| 5021 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5022 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 5023 | 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] | 5024 | goto load_error; |
| 5025 | } |
| 5026 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5027 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 5028 | 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] | 5029 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5030 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5031 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5032 | 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] | 5033 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 5034 | 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] | 5035 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5036 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5037 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5038 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5039 | bind_conf->ca_sign_cert = cacert; |
| 5040 | bind_conf->ca_sign_pkey = capkey; |
| 5041 | return err; |
| 5042 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5043 | read_error: |
| 5044 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5045 | if (capkey) EVP_PKEY_free(capkey); |
| 5046 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5047 | load_error: |
| 5048 | bind_conf->generate_certs = 0; |
| 5049 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5050 | return err; |
| 5051 | } |
| 5052 | |
| 5053 | /* Release CA cert and private key used to generate certificated */ |
| 5054 | void |
| 5055 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 5056 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5057 | if (bind_conf->ca_sign_pkey) |
| 5058 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 5059 | if (bind_conf->ca_sign_cert) |
| 5060 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 5061 | bind_conf->ca_sign_pkey = NULL; |
| 5062 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5063 | } |
| 5064 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5065 | /* |
| 5066 | * This function is called if SSL * context is not yet allocated. The function |
| 5067 | * is designed to be called before any other data-layer operation and sets the |
| 5068 | * handshake flag on the connection. It is safe to call it multiple times. |
| 5069 | * It returns 0 on success and -1 in error case. |
| 5070 | */ |
| 5071 | static int ssl_sock_init(struct connection *conn) |
| 5072 | { |
| 5073 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5074 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5075 | return 0; |
| 5076 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5077 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5078 | return 0; |
| 5079 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5080 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 5081 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5082 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5083 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5084 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5085 | /* If it is in client mode initiate SSL session |
| 5086 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5087 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5088 | int may_retry = 1; |
| 5089 | |
| 5090 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5091 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5092 | conn->xprt_ctx = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5093 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5094 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5095 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5096 | goto retry_connect; |
| 5097 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5098 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5099 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5100 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5101 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5102 | /* set fd on SSL session context */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5103 | if (!SSL_set_fd(conn->xprt_ctx, conn->handle.fd)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5104 | SSL_free(conn->xprt_ctx); |
| 5105 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5106 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5107 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5108 | goto retry_connect; |
| 5109 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5110 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5111 | return -1; |
| 5112 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5113 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5114 | /* set connection pointer */ |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 5115 | if (!SSL_set_ex_data(conn->xprt_ctx, ssl_app_data_index, conn)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5116 | SSL_free(conn->xprt_ctx); |
| 5117 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5118 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5119 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5120 | goto retry_connect; |
| 5121 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5122 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5123 | return -1; |
| 5124 | } |
| 5125 | |
| 5126 | SSL_set_connect_state(conn->xprt_ctx); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5127 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5128 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 5129 | SSL_SESSION *sess = d2i_SSL_SESSION(NULL, &ptr, __objt_server(conn->target)->ssl_ctx.reused_sess[tid].size); |
| 5130 | if (sess && !SSL_set_session(conn->xprt_ctx, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5131 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5132 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5133 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5134 | } else if (sess) { |
| 5135 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5136 | } |
| 5137 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5138 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5139 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5140 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5141 | |
Emeric Brun | 7ad43e7 | 2018-10-10 14:51:02 +0200 | [diff] [blame] | 5142 | HA_ATOMIC_ADD(&sslconns, 1); |
| 5143 | HA_ATOMIC_ADD(&totalsslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5144 | return 0; |
| 5145 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5146 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5147 | int may_retry = 1; |
| 5148 | |
| 5149 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5150 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5151 | conn->xprt_ctx = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5152 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5153 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5154 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5155 | goto retry_accept; |
| 5156 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5157 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5158 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5159 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5160 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5161 | /* set fd on SSL session context */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5162 | if (!SSL_set_fd(conn->xprt_ctx, conn->handle.fd)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5163 | SSL_free(conn->xprt_ctx); |
| 5164 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5165 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5166 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5167 | goto retry_accept; |
| 5168 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5169 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5170 | return -1; |
| 5171 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5172 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5173 | /* set connection pointer */ |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 5174 | if (!SSL_set_ex_data(conn->xprt_ctx, ssl_app_data_index, conn)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5175 | SSL_free(conn->xprt_ctx); |
| 5176 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5177 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5178 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5179 | goto retry_accept; |
| 5180 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5181 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5182 | return -1; |
| 5183 | } |
| 5184 | |
| 5185 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5186 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5187 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5188 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5189 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L || defined(OPENSSL_IS_BORINGSSL) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5190 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 5191 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5192 | |
Emeric Brun | 7ad43e7 | 2018-10-10 14:51:02 +0200 | [diff] [blame] | 5193 | HA_ATOMIC_ADD(&sslconns, 1); |
| 5194 | HA_ATOMIC_ADD(&totalsslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5195 | return 0; |
| 5196 | } |
| 5197 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5198 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5199 | return -1; |
| 5200 | } |
| 5201 | |
| 5202 | |
| 5203 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5204 | * updates the FD status if it wants some polling before being called again. |
| 5205 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5206 | * otherwise it returns non-zero and removes itself from the connection's |
| 5207 | * flags (the bit is provided in <flag> by the caller). |
| 5208 | */ |
| 5209 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 5210 | { |
| 5211 | int ret; |
| 5212 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5213 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5214 | return 0; |
| 5215 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5216 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5217 | goto out_error; |
| 5218 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5219 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L |
| 5220 | /* |
| 5221 | * Check if we have early data. If we do, we have to read them |
| 5222 | * before SSL_do_handshake() is called, And there's no way to |
| 5223 | * detect early data, except to try to read them |
| 5224 | */ |
| 5225 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5226 | size_t read_data; |
| 5227 | |
| 5228 | ret = SSL_read_early_data(conn->xprt_ctx, &conn->tmp_early_data, |
| 5229 | 1, &read_data); |
| 5230 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5231 | goto check_error; |
| 5232 | if (ret == SSL_READ_EARLY_DATA_SUCCESS) { |
| 5233 | conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN); |
| 5234 | return 1; |
| 5235 | } else |
| 5236 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5237 | } |
| 5238 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5239 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5240 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5241 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5242 | * the reneg handshake. |
| 5243 | * Here we use SSL_peek as a workaround for reneg. |
| 5244 | */ |
| 5245 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5246 | char c; |
| 5247 | |
| 5248 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 5249 | if (ret <= 0) { |
| 5250 | /* handshake may have not been completed, let's find why */ |
| 5251 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5252 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5253 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5254 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 5255 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5256 | __conn_sock_want_send(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5257 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5258 | return 0; |
| 5259 | } |
| 5260 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5261 | /* handshake may have been completed but we have |
| 5262 | * no more data to read. |
| 5263 | */ |
| 5264 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5265 | ret = 1; |
| 5266 | goto reneg_ok; |
| 5267 | } |
| 5268 | /* SSL handshake needs to read, L4 connection is ready */ |
| 5269 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 5270 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5271 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5272 | __conn_sock_want_recv(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5273 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5274 | return 0; |
| 5275 | } |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5276 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5277 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5278 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5279 | return 0; |
| 5280 | } |
| 5281 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5282 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5283 | /* if errno is null, then connection was successfully established */ |
| 5284 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5285 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5286 | if (!conn->err_code) { |
Emeric Brun | 77e8919 | 2018-08-16 11:36:40 +0200 | [diff] [blame] | 5287 | #ifdef OPENSSL_IS_BORINGSSL /* BoringSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5288 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5289 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5290 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 5291 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5292 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 5293 | empty_handshake = state == TLS_ST_BEFORE; |
| 5294 | #else |
| 5295 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
| 5296 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5297 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5298 | if (!errno) { |
| 5299 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5300 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5301 | else |
| 5302 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5303 | } |
| 5304 | else { |
| 5305 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5306 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5307 | else |
| 5308 | conn->err_code = CO_ER_SSL_ABORT; |
| 5309 | } |
| 5310 | } |
| 5311 | else { |
| 5312 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5313 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5314 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5315 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5316 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5317 | #endif |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5318 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5319 | goto out_error; |
| 5320 | } |
| 5321 | else { |
| 5322 | /* Fail on all other handshake errors */ |
| 5323 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5324 | * buffer, causing an RST to be emitted upon close() on |
| 5325 | * TCP sockets. We first try to drain possibly pending |
| 5326 | * data to avoid this as much as possible. |
| 5327 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5328 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5329 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5330 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 5331 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5332 | goto out_error; |
| 5333 | } |
| 5334 | } |
| 5335 | /* read some data: consider handshake completed */ |
| 5336 | goto reneg_ok; |
| 5337 | } |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5338 | ret = SSL_do_handshake(conn->xprt_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5339 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5340 | if (ret != 1) { |
| 5341 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5342 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5343 | |
| 5344 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5345 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 5346 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5347 | __conn_sock_want_send(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5348 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5349 | return 0; |
| 5350 | } |
| 5351 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5352 | /* SSL handshake needs to read, L4 connection is ready */ |
| 5353 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 5354 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5355 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5356 | __conn_sock_want_recv(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5357 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5358 | return 0; |
| 5359 | } |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5360 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5361 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5362 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5363 | return 0; |
| 5364 | } |
| 5365 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5366 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5367 | /* if errno is null, then connection was successfully established */ |
| 5368 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5369 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5370 | if (!conn->err_code) { |
Emeric Brun | 77e8919 | 2018-08-16 11:36:40 +0200 | [diff] [blame] | 5371 | #ifdef OPENSSL_IS_BORINGSSL /* BoringSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5372 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5373 | #else |
| 5374 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 5375 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5376 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 5377 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5378 | #else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5379 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5380 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5381 | if (empty_handshake) { |
| 5382 | if (!errno) { |
| 5383 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5384 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5385 | else |
| 5386 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5387 | } |
| 5388 | else { |
| 5389 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5390 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5391 | else |
| 5392 | conn->err_code = CO_ER_SSL_ABORT; |
| 5393 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5394 | } |
| 5395 | else { |
| 5396 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5397 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5398 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5399 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5400 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5401 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5402 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5403 | goto out_error; |
| 5404 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5405 | else { |
| 5406 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5407 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5408 | * buffer, causing an RST to be emitted upon close() on |
| 5409 | * TCP sockets. We first try to drain possibly pending |
| 5410 | * data to avoid this as much as possible. |
| 5411 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5412 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5413 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5414 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 5415 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5416 | goto out_error; |
| 5417 | } |
| 5418 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5419 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5420 | else { |
| 5421 | /* |
| 5422 | * If the server refused the early data, we have to send a |
| 5423 | * 425 to the client, as we no longer have the data to sent |
| 5424 | * them again. |
| 5425 | */ |
| 5426 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
| 5427 | if (SSL_get_early_data_status(conn->xprt_ctx) == SSL_EARLY_DATA_REJECTED) { |
| 5428 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5429 | goto out_error; |
| 5430 | } |
| 5431 | } |
| 5432 | } |
| 5433 | #endif |
| 5434 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5435 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5436 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5437 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5438 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5439 | /* ASYNC engine API doesn't support moving read/write |
| 5440 | * buffers. So we disable ASYNC mode right after |
| 5441 | * the handshake to avoid buffer oveflows. |
| 5442 | */ |
| 5443 | if (global_ssl.async) |
| 5444 | SSL_clear_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5445 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5446 | /* Handshake succeeded */ |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5447 | if (!SSL_session_reused(conn->xprt_ctx)) { |
| 5448 | if (objt_server(conn->target)) { |
| 5449 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5450 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5451 | 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] | 5452 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5453 | else { |
| 5454 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5455 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5456 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5457 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5458 | } |
| 5459 | |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5460 | #ifdef OPENSSL_IS_BORINGSSL |
| 5461 | if ((conn->flags & CO_FL_EARLY_SSL_HS) && !SSL_in_early_data(conn->xprt_ctx)) |
| 5462 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5463 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5464 | /* The connection is now established at both layers, it's time to leave */ |
| 5465 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5466 | return 1; |
| 5467 | |
| 5468 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5469 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5470 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5471 | ERR_clear_error(); |
| 5472 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5473 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5474 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5475 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5476 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5477 | } |
| 5478 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5479 | /* Fail on all other handshake errors */ |
| 5480 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5481 | if (!conn->err_code) |
| 5482 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5483 | return 0; |
| 5484 | } |
| 5485 | |
| 5486 | /* 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] | 5487 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5488 | * buffer wraps, in which case a second call may be performed. The connection's |
| 5489 | * flags are updated with whatever special event is detected (error, read0, |
| 5490 | * empty). The caller is responsible for taking care of those events and |
| 5491 | * avoiding the call if inappropriate. The function does not call the |
| 5492 | * connection's polling update function, so the caller is responsible for this. |
| 5493 | */ |
Willy Tarreau | 7f3225f | 2018-06-19 06:15:17 +0200 | [diff] [blame] | 5494 | static size_t ssl_sock_to_buf(struct connection *conn, struct buffer *buf, size_t count, int flags) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5495 | { |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 5496 | ssize_t ret; |
| 5497 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5498 | |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5499 | conn_refresh_polling_flags(conn); |
| 5500 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5501 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5502 | goto out_error; |
| 5503 | |
| 5504 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5505 | /* a handshake was requested */ |
| 5506 | return 0; |
| 5507 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5508 | /* read the largest possible block. For this, we perform only one call |
| 5509 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 5510 | * in which case we accept to do it once again. A new attempt is made on |
| 5511 | * EINTR too. |
| 5512 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 5513 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5514 | int need_out = 0; |
| 5515 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5516 | try = b_contig_space(buf); |
| 5517 | if (!try) |
| 5518 | break; |
| 5519 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5520 | if (try > count) |
| 5521 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5522 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5523 | if (((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_EARLY_SSL_HS) && |
| 5524 | conn->tmp_early_data != -1) { |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 5525 | *b_tail(buf) = conn->tmp_early_data; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5526 | done++; |
| 5527 | try--; |
| 5528 | count--; |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5529 | b_add(buf, 1); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5530 | conn->tmp_early_data = -1; |
| 5531 | continue; |
| 5532 | } |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5533 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5534 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5535 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5536 | size_t read_length; |
| 5537 | |
| 5538 | ret = SSL_read_early_data(conn->xprt_ctx, |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 5539 | b_tail(buf), try, &read_length); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5540 | if (ret == SSL_READ_EARLY_DATA_SUCCESS && |
| 5541 | read_length > 0) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5542 | conn->flags |= CO_FL_EARLY_DATA; |
| 5543 | if (ret == SSL_READ_EARLY_DATA_SUCCESS || |
| 5544 | ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5545 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5546 | /* |
| 5547 | * We're done reading the early data, |
| 5548 | * let's make the handshake |
| 5549 | */ |
| 5550 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5551 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5552 | need_out = 1; |
| 5553 | if (read_length == 0) |
| 5554 | break; |
| 5555 | } |
| 5556 | ret = read_length; |
| 5557 | } |
| 5558 | } else |
| 5559 | #endif |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 5560 | ret = SSL_read(conn->xprt_ctx, b_tail(buf), try); |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5561 | #ifdef OPENSSL_IS_BORINGSSL |
| 5562 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5563 | if (SSL_in_early_data(conn->xprt_ctx)) { |
| 5564 | if (ret > 0) |
| 5565 | conn->flags |= CO_FL_EARLY_DATA; |
| 5566 | } else { |
Emmanuel Hocdet | cebd796 | 2017-11-27 16:14:40 +0100 | [diff] [blame] | 5567 | conn->flags &= ~(CO_FL_EARLY_SSL_HS); |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5568 | } |
| 5569 | } |
| 5570 | #endif |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5571 | if (conn->flags & CO_FL_ERROR) { |
| 5572 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5573 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5574 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5575 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5576 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5577 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5578 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5579 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5580 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5581 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5582 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5583 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5584 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5585 | __conn_sock_want_send(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5586 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5587 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5588 | if (global_ssl.async) |
| 5589 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5590 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5591 | break; |
| 5592 | } |
| 5593 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5594 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5595 | /* handshake is running, and it may need to re-enable read */ |
| 5596 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5597 | __conn_sock_want_recv(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5598 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5599 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5600 | if (global_ssl.async) |
| 5601 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5602 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5603 | break; |
| 5604 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5605 | /* we need to poll for retry a read later */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5606 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5607 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5608 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 5609 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5610 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 5611 | * stack before shutting down the connection for |
| 5612 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5613 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 5614 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5615 | /* otherwise it's a real error */ |
| 5616 | goto out_error; |
| 5617 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5618 | if (need_out) |
| 5619 | break; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5620 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5621 | leave: |
| 5622 | conn_cond_update_sock_polling(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5623 | return done; |
| 5624 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5625 | clear_ssl_error: |
| 5626 | /* Clear openssl global errors stack */ |
| 5627 | ssl_sock_dump_errors(conn); |
| 5628 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5629 | read0: |
| 5630 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5631 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5632 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5633 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5634 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5635 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5636 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5637 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5638 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5639 | } |
| 5640 | |
| 5641 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5642 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 5643 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 5644 | * 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] | 5645 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5646 | * a second call may be performed. The connection's flags are updated with |
| 5647 | * whatever special event is detected (error, empty). The caller is responsible |
| 5648 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5649 | * 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] | 5650 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 5651 | * caller to take care of this. It's up to the caller to update the buffer's |
| 5652 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5653 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5654 | static size_t ssl_sock_from_buf(struct connection *conn, const struct buffer *buf, size_t count, int flags) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5655 | { |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5656 | ssize_t ret; |
| 5657 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5658 | |
| 5659 | done = 0; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5660 | conn_refresh_polling_flags(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5661 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5662 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5663 | goto out_error; |
| 5664 | |
| 5665 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5666 | /* a handshake was requested */ |
| 5667 | return 0; |
| 5668 | |
| 5669 | /* send the largest possible block. For this we perform only one call |
| 5670 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5671 | * in which case we accept to do it once again. |
| 5672 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5673 | while (count) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5674 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5675 | size_t written_data; |
| 5676 | #endif |
| 5677 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5678 | try = b_contig_data(buf, done); |
| 5679 | if (try > count) |
| 5680 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5681 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 5682 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5683 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5684 | global_ssl.max_record && try > global_ssl.max_record) { |
| 5685 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5686 | } |
| 5687 | else { |
| 5688 | /* we need to keep the information about the fact that |
| 5689 | * we're not limiting the upcoming send(), because if it |
| 5690 | * fails, we'll have to retry with at least as many data. |
| 5691 | */ |
| 5692 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 5693 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5694 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5695 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5696 | if (!SSL_is_init_finished(conn->xprt_ctx)) { |
| 5697 | unsigned int max_early; |
| 5698 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5699 | if (objt_listener(conn->target)) |
| 5700 | max_early = SSL_get_max_early_data(conn->xprt_ctx); |
| 5701 | else { |
| 5702 | if (SSL_get0_session(conn->xprt_ctx)) |
| 5703 | max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(conn->xprt_ctx)); |
| 5704 | else |
| 5705 | max_early = 0; |
| 5706 | } |
| 5707 | |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 5708 | if (try + conn->sent_early_data > max_early) { |
| 5709 | try -= (try + conn->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5710 | if (try <= 0) { |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 5711 | if (!(conn->flags & CO_FL_EARLY_SSL_HS)) |
| 5712 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5713 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5714 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5715 | } |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5716 | ret = SSL_write_early_data(conn->xprt_ctx, b_peek(buf, done), try, &written_data); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5717 | if (ret == 1) { |
| 5718 | ret = written_data; |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 5719 | conn->sent_early_data += ret; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5720 | if (objt_server(conn->target)) { |
| 5721 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5722 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA; |
| 5723 | } |
| 5724 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5725 | } |
| 5726 | |
| 5727 | } else |
| 5728 | #endif |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5729 | ret = SSL_write(conn->xprt_ctx, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5730 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5731 | if (conn->flags & CO_FL_ERROR) { |
| 5732 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5733 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5734 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5735 | if (ret > 0) { |
Olivier Houchard | f24502b | 2019-01-17 19:09:11 +0100 | [diff] [blame] | 5736 | /* A send succeeded, so we can consier ourself connected */ |
| 5737 | conn->flags |= CO_FL_CONNECTED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5738 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5739 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5740 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5741 | } |
| 5742 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5743 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5744 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5745 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5746 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5747 | /* handshake is running, and it may need to re-enable write */ |
| 5748 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5749 | __conn_sock_want_send(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5750 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5751 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5752 | if (global_ssl.async) |
| 5753 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5754 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5755 | break; |
| 5756 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5757 | /* we need to poll to retry a write later */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5758 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5759 | break; |
| 5760 | } |
| 5761 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5762 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5763 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5764 | __conn_sock_want_recv(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5765 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5766 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5767 | if (global_ssl.async) |
| 5768 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5769 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5770 | break; |
| 5771 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5772 | goto out_error; |
| 5773 | } |
| 5774 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5775 | leave: |
| 5776 | conn_cond_update_sock_polling(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5777 | return done; |
| 5778 | |
| 5779 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5780 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5781 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5782 | ERR_clear_error(); |
| 5783 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5784 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5785 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5786 | } |
| 5787 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5788 | static void ssl_sock_close(struct connection *conn) { |
| 5789 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5790 | if (conn->xprt_ctx) { |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5791 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5792 | if (global_ssl.async) { |
| 5793 | OSSL_ASYNC_FD all_fd[32], afd; |
| 5794 | size_t num_all_fds = 0; |
| 5795 | int i; |
| 5796 | |
| 5797 | SSL_get_all_async_fds(conn->xprt_ctx, NULL, &num_all_fds); |
| 5798 | if (num_all_fds > 32) { |
| 5799 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 5800 | return; |
| 5801 | } |
| 5802 | |
| 5803 | SSL_get_all_async_fds(conn->xprt_ctx, all_fd, &num_all_fds); |
| 5804 | |
| 5805 | /* If an async job is pending, we must try to |
| 5806 | to catch the end using polling before calling |
| 5807 | SSL_free */ |
| 5808 | if (num_all_fds && SSL_waiting_for_async(conn->xprt_ctx)) { |
| 5809 | for (i=0 ; i < num_all_fds ; i++) { |
| 5810 | /* switch on an handler designed to |
| 5811 | * handle the SSL_free |
| 5812 | */ |
| 5813 | afd = all_fd[i]; |
| 5814 | fdtab[afd].iocb = ssl_async_fd_free; |
| 5815 | fdtab[afd].owner = conn->xprt_ctx; |
| 5816 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 5817 | /* To ensure that the fd cache won't be used |
| 5818 | * and we'll catch a real RD event. |
| 5819 | */ |
| 5820 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5821 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5822 | conn->xprt_ctx = NULL; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5823 | HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5824 | return; |
| 5825 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5826 | /* Else we can remove the fds from the fdtab |
| 5827 | * and call SSL_free. |
| 5828 | * note: we do a fd_remove and not a delete |
| 5829 | * because the fd is owned by the engine. |
| 5830 | * the engine is responsible to close |
| 5831 | */ |
| 5832 | for (i=0 ; i < num_all_fds ; i++) |
| 5833 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5834 | } |
| 5835 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5836 | SSL_free(conn->xprt_ctx); |
| 5837 | conn->xprt_ctx = NULL; |
Emeric Brun | 7ad43e7 | 2018-10-10 14:51:02 +0200 | [diff] [blame] | 5838 | HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5839 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5840 | } |
| 5841 | |
| 5842 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 5843 | * any case, flags the connection as reusable if no handshake was in progress. |
| 5844 | */ |
| 5845 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 5846 | { |
| 5847 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5848 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5849 | if (!clean) |
| 5850 | /* don't sent notify on SSL_shutdown */ |
Willy Tarreau | e3cc3a3 | 2017-02-13 11:12:29 +0100 | [diff] [blame] | 5851 | SSL_set_quiet_shutdown(conn->xprt_ctx, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5852 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5853 | if (SSL_shutdown(conn->xprt_ctx) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5854 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5855 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5856 | ERR_clear_error(); |
| 5857 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5858 | } |
| 5859 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5860 | /* used for ppv2 pkey alog (can be used for logging) */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5861 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5862 | { |
| 5863 | struct pkey_info *pkinfo; |
| 5864 | int bits = 0; |
| 5865 | int sig = TLSEXT_signature_anonymous; |
| 5866 | int len = -1; |
| 5867 | |
| 5868 | if (!ssl_sock_is_ssl(conn)) |
| 5869 | return 0; |
| 5870 | |
Emmanuel Hocdet | 3448c49 | 2018-06-18 12:44:19 +0200 | [diff] [blame] | 5871 | pkinfo = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(conn->xprt_ctx), ssl_pkey_info_index); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5872 | if (pkinfo) { |
| 5873 | sig = pkinfo->sig; |
| 5874 | bits = pkinfo->bits; |
| 5875 | } else { |
| 5876 | /* multicert and generated cert have no pkey info */ |
| 5877 | X509 *crt; |
| 5878 | EVP_PKEY *pkey; |
| 5879 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5880 | if (!crt) |
| 5881 | return 0; |
| 5882 | pkey = X509_get_pubkey(crt); |
| 5883 | if (pkey) { |
| 5884 | bits = EVP_PKEY_bits(pkey); |
| 5885 | switch(EVP_PKEY_base_id(pkey)) { |
| 5886 | case EVP_PKEY_RSA: |
| 5887 | sig = TLSEXT_signature_rsa; |
| 5888 | break; |
| 5889 | case EVP_PKEY_EC: |
| 5890 | sig = TLSEXT_signature_ecdsa; |
| 5891 | break; |
| 5892 | case EVP_PKEY_DSA: |
| 5893 | sig = TLSEXT_signature_dsa; |
| 5894 | break; |
| 5895 | } |
| 5896 | EVP_PKEY_free(pkey); |
| 5897 | } |
| 5898 | } |
| 5899 | |
| 5900 | switch(sig) { |
| 5901 | case TLSEXT_signature_rsa: |
| 5902 | len = chunk_printf(out, "RSA%d", bits); |
| 5903 | break; |
| 5904 | case TLSEXT_signature_ecdsa: |
| 5905 | len = chunk_printf(out, "EC%d", bits); |
| 5906 | break; |
| 5907 | case TLSEXT_signature_dsa: |
| 5908 | len = chunk_printf(out, "DSA%d", bits); |
| 5909 | break; |
| 5910 | default: |
| 5911 | return 0; |
| 5912 | } |
| 5913 | if (len < 0) |
| 5914 | return 0; |
| 5915 | return 1; |
| 5916 | } |
| 5917 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 5918 | /* used for ppv2 cert signature (can be used for logging) */ |
| 5919 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 5920 | { |
| 5921 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 5922 | X509 *crt; |
| 5923 | |
| 5924 | if (!ssl_sock_is_ssl(conn)) |
| 5925 | return NULL; |
| 5926 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5927 | if (!crt) |
| 5928 | return NULL; |
| 5929 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 5930 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 5931 | } |
| 5932 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 5933 | /* used for ppv2 authority */ |
| 5934 | const char *ssl_sock_get_sni(struct connection *conn) |
| 5935 | { |
| 5936 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5937 | if (!ssl_sock_is_ssl(conn)) |
| 5938 | return NULL; |
| 5939 | return SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 5940 | #else |
| 5941 | return 0; |
| 5942 | #endif |
| 5943 | } |
| 5944 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5945 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5946 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 5947 | { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5948 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5949 | return NULL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5950 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5951 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 5952 | } |
| 5953 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5954 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5955 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 5956 | { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5957 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5958 | return NULL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5959 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5960 | return SSL_get_version(conn->xprt_ctx); |
| 5961 | } |
| 5962 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5963 | /* Extract a serial from a cert, and copy it to a chunk. |
| 5964 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 5965 | * -1 if output is not large enough. |
| 5966 | */ |
| 5967 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5968 | ssl_sock_get_serial(X509 *crt, struct buffer *out) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5969 | { |
| 5970 | ASN1_INTEGER *serial; |
| 5971 | |
| 5972 | serial = X509_get_serialNumber(crt); |
| 5973 | if (!serial) |
| 5974 | return 0; |
| 5975 | |
| 5976 | if (out->size < serial->length) |
| 5977 | return -1; |
| 5978 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5979 | memcpy(out->area, serial->data, serial->length); |
| 5980 | out->data = serial->length; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5981 | return 1; |
| 5982 | } |
| 5983 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5984 | /* Extract a cert to der, and copy it to a chunk. |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 5985 | * Returns 1 if the cert is found and copied, 0 on der conversion failure |
| 5986 | * and -1 if the output is not large enough. |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5987 | */ |
| 5988 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5989 | ssl_sock_crt2der(X509 *crt, struct buffer *out) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5990 | { |
| 5991 | int len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5992 | unsigned char *p = (unsigned char *) out->area;; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5993 | |
| 5994 | len =i2d_X509(crt, NULL); |
| 5995 | if (len <= 0) |
| 5996 | return 1; |
| 5997 | |
| 5998 | if (out->size < len) |
| 5999 | return -1; |
| 6000 | |
| 6001 | i2d_X509(crt,&p); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6002 | out->data = len; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6003 | return 1; |
| 6004 | } |
| 6005 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6006 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6007 | /* Copy Date in ASN1_UTCTIME format in struct buffer out. |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6008 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 6009 | * and -1 if output is not large enough. |
| 6010 | */ |
| 6011 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6012 | ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6013 | { |
| 6014 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 6015 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 6016 | |
| 6017 | if (gentm->length < 12) |
| 6018 | return 0; |
| 6019 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 6020 | return 0; |
| 6021 | if (out->size < gentm->length-2) |
| 6022 | return -1; |
| 6023 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6024 | memcpy(out->area, gentm->data+2, gentm->length-2); |
| 6025 | out->data = gentm->length-2; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6026 | return 1; |
| 6027 | } |
| 6028 | else if (tm->type == V_ASN1_UTCTIME) { |
| 6029 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 6030 | |
| 6031 | if (utctm->length < 10) |
| 6032 | return 0; |
| 6033 | if (utctm->data[0] >= 0x35) |
| 6034 | return 0; |
| 6035 | if (out->size < utctm->length) |
| 6036 | return -1; |
| 6037 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6038 | memcpy(out->area, utctm->data, utctm->length); |
| 6039 | out->data = utctm->length; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6040 | return 1; |
| 6041 | } |
| 6042 | |
| 6043 | return 0; |
| 6044 | } |
| 6045 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6046 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 6047 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 6048 | */ |
| 6049 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6050 | ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos, |
| 6051 | struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6052 | { |
| 6053 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6054 | ASN1_OBJECT *obj; |
| 6055 | ASN1_STRING *data; |
| 6056 | const unsigned char *data_ptr; |
| 6057 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6058 | int i, j, n; |
| 6059 | int cur = 0; |
| 6060 | const char *s; |
| 6061 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6062 | int name_count; |
| 6063 | |
| 6064 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6065 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6066 | out->data = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6067 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6068 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6069 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6070 | else |
| 6071 | j = i; |
| 6072 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6073 | ne = X509_NAME_get_entry(a, j); |
| 6074 | obj = X509_NAME_ENTRY_get_object(ne); |
| 6075 | data = X509_NAME_ENTRY_get_data(ne); |
| 6076 | data_ptr = ASN1_STRING_get0_data(data); |
| 6077 | data_len = ASN1_STRING_length(data); |
| 6078 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6079 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6080 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6081 | s = tmp; |
| 6082 | } |
| 6083 | |
| 6084 | if (chunk_strcasecmp(entry, s) != 0) |
| 6085 | continue; |
| 6086 | |
| 6087 | if (pos < 0) |
| 6088 | cur--; |
| 6089 | else |
| 6090 | cur++; |
| 6091 | |
| 6092 | if (cur != pos) |
| 6093 | continue; |
| 6094 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6095 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6096 | return -1; |
| 6097 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6098 | memcpy(out->area, data_ptr, data_len); |
| 6099 | out->data = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6100 | return 1; |
| 6101 | } |
| 6102 | |
| 6103 | return 0; |
| 6104 | |
| 6105 | } |
| 6106 | |
| 6107 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 6108 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 6109 | */ |
| 6110 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6111 | ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6112 | { |
| 6113 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6114 | ASN1_OBJECT *obj; |
| 6115 | ASN1_STRING *data; |
| 6116 | const unsigned char *data_ptr; |
| 6117 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6118 | int i, n, ln; |
| 6119 | int l = 0; |
| 6120 | const char *s; |
| 6121 | char *p; |
| 6122 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6123 | int name_count; |
| 6124 | |
| 6125 | |
| 6126 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6127 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6128 | out->data = 0; |
| 6129 | p = out->area; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6130 | for (i = 0; i < name_count; i++) { |
| 6131 | ne = X509_NAME_get_entry(a, i); |
| 6132 | obj = X509_NAME_ENTRY_get_object(ne); |
| 6133 | data = X509_NAME_ENTRY_get_data(ne); |
| 6134 | data_ptr = ASN1_STRING_get0_data(data); |
| 6135 | data_len = ASN1_STRING_length(data); |
| 6136 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6137 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6138 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6139 | s = tmp; |
| 6140 | } |
| 6141 | ln = strlen(s); |
| 6142 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6143 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6144 | if (l > out->size) |
| 6145 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6146 | out->data = l; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6147 | |
| 6148 | *(p++)='/'; |
| 6149 | memcpy(p, s, ln); |
| 6150 | p += ln; |
| 6151 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6152 | memcpy(p, data_ptr, data_len); |
| 6153 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6154 | } |
| 6155 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6156 | if (!out->data) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6157 | return 0; |
| 6158 | |
| 6159 | return 1; |
| 6160 | } |
| 6161 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6162 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 6163 | { |
| 6164 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 6165 | SSL_set_alpn_protos(conn->xprt_ctx, alpn, len); |
| 6166 | #endif |
| 6167 | } |
| 6168 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6169 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 6170 | * to disable SNI. |
| 6171 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6172 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 6173 | { |
| 6174 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6175 | char *prev_name; |
| 6176 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6177 | if (!ssl_sock_is_ssl(conn)) |
| 6178 | return; |
| 6179 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6180 | /* if the SNI changes, we must destroy the reusable context so that a |
| 6181 | * new connection will present a new SNI. As an optimization we could |
| 6182 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 6183 | * server. |
| 6184 | */ |
| 6185 | prev_name = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 6186 | if ((!prev_name && hostname) || |
| 6187 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
| 6188 | SSL_set_session(conn->xprt_ctx, NULL); |
| 6189 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6190 | SSL_set_tlsext_host_name(conn->xprt_ctx, hostname); |
| 6191 | #endif |
| 6192 | } |
| 6193 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6194 | /* Extract peer certificate's common name into the chunk dest |
| 6195 | * Returns |
| 6196 | * the len of the extracted common name |
| 6197 | * or 0 if no CN found in DN |
| 6198 | * or -1 on error case (i.e. no peer certificate) |
| 6199 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6200 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 6201 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6202 | { |
| 6203 | X509 *crt = NULL; |
| 6204 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6205 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6206 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6207 | .area = (char *)&find_cn, |
| 6208 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6209 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6210 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6211 | |
| 6212 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6213 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6214 | |
| 6215 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6216 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6217 | if (!crt) |
| 6218 | goto out; |
| 6219 | |
| 6220 | name = X509_get_subject_name(crt); |
| 6221 | if (!name) |
| 6222 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6223 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6224 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6225 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6226 | if (crt) |
| 6227 | X509_free(crt); |
| 6228 | |
| 6229 | return result; |
| 6230 | } |
| 6231 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6232 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6233 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6234 | { |
| 6235 | X509 *crt = NULL; |
| 6236 | |
| 6237 | if (!ssl_sock_is_ssl(conn)) |
| 6238 | return 0; |
| 6239 | |
| 6240 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6241 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6242 | if (!crt) |
| 6243 | return 0; |
| 6244 | |
| 6245 | X509_free(crt); |
| 6246 | return 1; |
| 6247 | } |
| 6248 | |
| 6249 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6250 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6251 | { |
| 6252 | if (!ssl_sock_is_ssl(conn)) |
| 6253 | return 0; |
| 6254 | |
| 6255 | return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
| 6256 | } |
| 6257 | |
| 6258 | /* returns result from SSL verify */ |
| 6259 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6260 | { |
| 6261 | if (!ssl_sock_is_ssl(conn)) |
| 6262 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 6263 | |
| 6264 | return (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
| 6265 | } |
| 6266 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6267 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6268 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6269 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6270 | * freed by the caller. NPN is also checked if available since older versions |
| 6271 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6272 | */ |
| 6273 | static int ssl_sock_get_alpn(const struct connection *conn, const char **str, int *len) |
| 6274 | { |
| 6275 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6276 | return 0; |
| 6277 | |
| 6278 | *str = NULL; |
| 6279 | |
| 6280 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 6281 | SSL_get0_alpn_selected(conn->xprt_ctx, (const unsigned char **)str, (unsigned *)len); |
| 6282 | if (*str) |
| 6283 | return 1; |
| 6284 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6285 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6286 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, (const unsigned char **)str, (unsigned *)len); |
| 6287 | if (*str) |
| 6288 | return 1; |
| 6289 | #endif |
| 6290 | return 0; |
| 6291 | } |
| 6292 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6293 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 6294 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6295 | static int |
| 6296 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6297 | { |
| 6298 | struct connection *conn; |
| 6299 | |
| 6300 | conn = objt_conn(smp->sess->origin); |
| 6301 | if (!conn || conn->xprt != &ssl_sock) |
| 6302 | return 0; |
| 6303 | |
| 6304 | smp->flags = 0; |
| 6305 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 6306 | smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) && |
| 6307 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6308 | |
| 6309 | return 1; |
| 6310 | } |
| 6311 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6312 | /* boolean, returns true if client cert was present */ |
| 6313 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6314 | 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] | 6315 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6316 | struct connection *conn; |
| 6317 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6318 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6319 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6320 | return 0; |
| 6321 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6322 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6323 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6324 | return 0; |
| 6325 | } |
| 6326 | |
| 6327 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6328 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6329 | smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6330 | |
| 6331 | return 1; |
| 6332 | } |
| 6333 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6334 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 6335 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6336 | * should be use. |
| 6337 | */ |
| 6338 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6339 | 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] | 6340 | { |
| 6341 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 6342 | X509 *crt = NULL; |
| 6343 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6344 | struct buffer *smp_trash; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6345 | struct connection *conn; |
| 6346 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6347 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6348 | if (!conn || conn->xprt != &ssl_sock) |
| 6349 | return 0; |
| 6350 | |
| 6351 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 6352 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6353 | return 0; |
| 6354 | } |
| 6355 | |
| 6356 | if (cert_peer) |
| 6357 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6358 | else |
| 6359 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 6360 | |
| 6361 | if (!crt) |
| 6362 | goto out; |
| 6363 | |
| 6364 | smp_trash = get_trash_chunk(); |
| 6365 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 6366 | goto out; |
| 6367 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6368 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6369 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6370 | ret = 1; |
| 6371 | out: |
| 6372 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6373 | if (cert_peer && crt) |
| 6374 | X509_free(crt); |
| 6375 | return ret; |
| 6376 | } |
| 6377 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6378 | /* binary, returns serial of certificate in a binary chunk. |
| 6379 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6380 | * should be use. |
| 6381 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6382 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6383 | 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] | 6384 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6385 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6386 | X509 *crt = NULL; |
| 6387 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6388 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6389 | struct connection *conn; |
| 6390 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6391 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6392 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6393 | return 0; |
| 6394 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6395 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6396 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6397 | return 0; |
| 6398 | } |
| 6399 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6400 | if (cert_peer) |
| 6401 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6402 | else |
| 6403 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 6404 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6405 | if (!crt) |
| 6406 | goto out; |
| 6407 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6408 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6409 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 6410 | goto out; |
| 6411 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6412 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6413 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6414 | ret = 1; |
| 6415 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6416 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6417 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6418 | X509_free(crt); |
| 6419 | return ret; |
| 6420 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6421 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6422 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 6423 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6424 | * should be use. |
| 6425 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6426 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6427 | 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] | 6428 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6429 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6430 | X509 *crt = NULL; |
| 6431 | const EVP_MD *digest; |
| 6432 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6433 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6434 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6435 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6436 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6437 | if (!conn || conn->xprt != &ssl_sock) |
| 6438 | return 0; |
| 6439 | |
| 6440 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6441 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6442 | return 0; |
| 6443 | } |
| 6444 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6445 | if (cert_peer) |
| 6446 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6447 | else |
| 6448 | crt = SSL_get_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6449 | if (!crt) |
| 6450 | goto out; |
| 6451 | |
| 6452 | smp_trash = get_trash_chunk(); |
| 6453 | digest = EVP_sha1(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6454 | X509_digest(crt, digest, (unsigned char *) smp_trash->area, |
| 6455 | (unsigned int *)&smp_trash->data); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6456 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6457 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6458 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6459 | ret = 1; |
| 6460 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6461 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6462 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6463 | X509_free(crt); |
| 6464 | return ret; |
| 6465 | } |
| 6466 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6467 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 6468 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6469 | * should be use. |
| 6470 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6471 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6472 | 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] | 6473 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6474 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6475 | X509 *crt = NULL; |
| 6476 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6477 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6478 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6479 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6480 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6481 | if (!conn || conn->xprt != &ssl_sock) |
| 6482 | return 0; |
| 6483 | |
| 6484 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6485 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6486 | return 0; |
| 6487 | } |
| 6488 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6489 | if (cert_peer) |
| 6490 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6491 | else |
| 6492 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6493 | if (!crt) |
| 6494 | goto out; |
| 6495 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6496 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6497 | if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6498 | goto out; |
| 6499 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6500 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6501 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6502 | ret = 1; |
| 6503 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6504 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6505 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6506 | X509_free(crt); |
| 6507 | return ret; |
| 6508 | } |
| 6509 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6510 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 6511 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6512 | * should be use. |
| 6513 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6514 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6515 | 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] | 6516 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6517 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6518 | X509 *crt = NULL; |
| 6519 | X509_NAME *name; |
| 6520 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6521 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6522 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6523 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6524 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6525 | if (!conn || conn->xprt != &ssl_sock) |
| 6526 | return 0; |
| 6527 | |
| 6528 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6529 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6530 | return 0; |
| 6531 | } |
| 6532 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6533 | if (cert_peer) |
| 6534 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6535 | else |
| 6536 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6537 | if (!crt) |
| 6538 | goto out; |
| 6539 | |
| 6540 | name = X509_get_issuer_name(crt); |
| 6541 | if (!name) |
| 6542 | goto out; |
| 6543 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6544 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6545 | if (args && args[0].type == ARGT_STR) { |
| 6546 | int pos = 1; |
| 6547 | |
| 6548 | if (args[1].type == ARGT_SINT) |
| 6549 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6550 | |
| 6551 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6552 | goto out; |
| 6553 | } |
| 6554 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6555 | goto out; |
| 6556 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6557 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6558 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6559 | ret = 1; |
| 6560 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6561 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6562 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6563 | X509_free(crt); |
| 6564 | return ret; |
| 6565 | } |
| 6566 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6567 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 6568 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6569 | * should be use. |
| 6570 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6571 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6572 | 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] | 6573 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6574 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6575 | X509 *crt = NULL; |
| 6576 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6577 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6578 | struct connection *conn; |
| 6579 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6580 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6581 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6582 | return 0; |
| 6583 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6584 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6585 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6586 | return 0; |
| 6587 | } |
| 6588 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6589 | if (cert_peer) |
| 6590 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6591 | else |
| 6592 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6593 | if (!crt) |
| 6594 | goto out; |
| 6595 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6596 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6597 | if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6598 | goto out; |
| 6599 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6600 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6601 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6602 | ret = 1; |
| 6603 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6604 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6605 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6606 | X509_free(crt); |
| 6607 | return ret; |
| 6608 | } |
| 6609 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6610 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 6611 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6612 | * should be use. |
| 6613 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6614 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6615 | 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] | 6616 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6617 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6618 | X509 *crt = NULL; |
| 6619 | X509_NAME *name; |
| 6620 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6621 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6622 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6623 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6624 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6625 | if (!conn || conn->xprt != &ssl_sock) |
| 6626 | return 0; |
| 6627 | |
| 6628 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6629 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6630 | return 0; |
| 6631 | } |
| 6632 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6633 | if (cert_peer) |
| 6634 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6635 | else |
| 6636 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6637 | if (!crt) |
| 6638 | goto out; |
| 6639 | |
| 6640 | name = X509_get_subject_name(crt); |
| 6641 | if (!name) |
| 6642 | goto out; |
| 6643 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6644 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6645 | if (args && args[0].type == ARGT_STR) { |
| 6646 | int pos = 1; |
| 6647 | |
| 6648 | if (args[1].type == ARGT_SINT) |
| 6649 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6650 | |
| 6651 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6652 | goto out; |
| 6653 | } |
| 6654 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6655 | goto out; |
| 6656 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6657 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6658 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6659 | ret = 1; |
| 6660 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6661 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6662 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6663 | X509_free(crt); |
| 6664 | return ret; |
| 6665 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6666 | |
| 6667 | /* integer, returns true if current session use a client certificate */ |
| 6668 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6669 | 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] | 6670 | { |
| 6671 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6672 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6673 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6674 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6675 | if (!conn || conn->xprt != &ssl_sock) |
| 6676 | return 0; |
| 6677 | |
| 6678 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6679 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6680 | return 0; |
| 6681 | } |
| 6682 | |
| 6683 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6684 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6685 | if (crt) { |
| 6686 | X509_free(crt); |
| 6687 | } |
| 6688 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6689 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6690 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6691 | return 1; |
| 6692 | } |
| 6693 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6694 | /* integer, returns the certificate version |
| 6695 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6696 | * should be use. |
| 6697 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6698 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6699 | 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] | 6700 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6701 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6702 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6703 | struct connection *conn; |
| 6704 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6705 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6706 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6707 | return 0; |
| 6708 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6709 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6710 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6711 | return 0; |
| 6712 | } |
| 6713 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6714 | if (cert_peer) |
| 6715 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6716 | else |
| 6717 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6718 | if (!crt) |
| 6719 | return 0; |
| 6720 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6721 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6722 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6723 | if (cert_peer) |
| 6724 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6725 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6726 | |
| 6727 | return 1; |
| 6728 | } |
| 6729 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6730 | /* string, returns the certificate's signature algorithm. |
| 6731 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6732 | * should be use. |
| 6733 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6734 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6735 | 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] | 6736 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6737 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6738 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6739 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6740 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6741 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6742 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6743 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6744 | if (!conn || conn->xprt != &ssl_sock) |
| 6745 | return 0; |
| 6746 | |
| 6747 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6748 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6749 | return 0; |
| 6750 | } |
| 6751 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6752 | if (cert_peer) |
| 6753 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6754 | else |
| 6755 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6756 | if (!crt) |
| 6757 | return 0; |
| 6758 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6759 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6760 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6761 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6762 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 6763 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6764 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6765 | if (cert_peer) |
| 6766 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6767 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 6768 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6769 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6770 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6771 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6772 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6773 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6774 | if (cert_peer) |
| 6775 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6776 | |
| 6777 | return 1; |
| 6778 | } |
| 6779 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6780 | /* string, returns the certificate's key algorithm. |
| 6781 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6782 | * should be use. |
| 6783 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6784 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6785 | 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] | 6786 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6787 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6788 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6789 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6790 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6791 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6792 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6793 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6794 | if (!conn || conn->xprt != &ssl_sock) |
| 6795 | return 0; |
| 6796 | |
| 6797 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6798 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6799 | return 0; |
| 6800 | } |
| 6801 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6802 | if (cert_peer) |
| 6803 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6804 | else |
| 6805 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6806 | if (!crt) |
| 6807 | return 0; |
| 6808 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6809 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 6810 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6811 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6812 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 6813 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6814 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6815 | if (cert_peer) |
| 6816 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6817 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 6818 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6819 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6820 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6821 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6822 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6823 | if (cert_peer) |
| 6824 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6825 | |
| 6826 | return 1; |
| 6827 | } |
| 6828 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6829 | /* boolean, returns true if front conn. transport layer is SSL. |
| 6830 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6831 | * char is 'b'. |
| 6832 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6833 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6834 | 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] | 6835 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6836 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6837 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6838 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6839 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6840 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6841 | return 1; |
| 6842 | } |
| 6843 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6844 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6845 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6846 | 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] | 6847 | { |
| 6848 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6849 | struct connection *conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6850 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6851 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6852 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6853 | conn->xprt_ctx && |
| 6854 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6855 | return 1; |
| 6856 | #else |
| 6857 | return 0; |
| 6858 | #endif |
| 6859 | } |
| 6860 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 6861 | /* boolean, returns true if client session has been resumed. |
| 6862 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6863 | * char is 'b'. |
| 6864 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6865 | static int |
| 6866 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6867 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 6868 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6869 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 6870 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6871 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6872 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6873 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6874 | conn->xprt_ctx && |
| 6875 | SSL_session_reused(conn->xprt_ctx); |
| 6876 | return 1; |
| 6877 | } |
| 6878 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6879 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 6880 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6881 | * char is 'b'. |
| 6882 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6883 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6884 | 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] | 6885 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6886 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6887 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6888 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6889 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6890 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6891 | return 0; |
| 6892 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6893 | smp->data.u.str.area = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
| 6894 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6895 | return 0; |
| 6896 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6897 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6898 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6899 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6900 | |
| 6901 | return 1; |
| 6902 | } |
| 6903 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6904 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 6905 | * is SSL. |
| 6906 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6907 | * char is 'b'. |
| 6908 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6909 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6910 | 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] | 6911 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6912 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6913 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6914 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6915 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6916 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6917 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6918 | return 0; |
| 6919 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 6920 | if (!SSL_get_cipher_bits(conn->xprt_ctx, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6921 | return 0; |
| 6922 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6923 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6924 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6925 | |
| 6926 | return 1; |
| 6927 | } |
| 6928 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6929 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 6930 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6931 | * char is 'b'. |
| 6932 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6933 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6934 | 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] | 6935 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6936 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6937 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6938 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6939 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6940 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6941 | return 0; |
| 6942 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6943 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
| 6944 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6945 | return 0; |
| 6946 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6947 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6948 | |
| 6949 | return 1; |
| 6950 | } |
| 6951 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6952 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6953 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6954 | 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] | 6955 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6956 | struct connection *conn; |
| 6957 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6958 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6959 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6960 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 6961 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 6962 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6963 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6964 | return 0; |
| 6965 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6966 | smp->data.u.str.area = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6967 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6968 | (const unsigned char **)&smp->data.u.str.area, |
| 6969 | (unsigned *)&smp->data.u.str.data); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6970 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6971 | if (!smp->data.u.str.area) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6972 | return 0; |
| 6973 | |
| 6974 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6975 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6976 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6977 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6978 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6979 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6980 | 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] | 6981 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6982 | struct connection *conn; |
| 6983 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6984 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6985 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6986 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 6987 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 6988 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 6989 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6990 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6991 | return 0; |
| 6992 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6993 | smp->data.u.str.area = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6994 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6995 | (const unsigned char **)&smp->data.u.str.area, |
| 6996 | (unsigned *)&smp->data.u.str.data); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6997 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6998 | if (!smp->data.u.str.area) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6999 | return 0; |
| 7000 | |
| 7001 | return 1; |
| 7002 | } |
| 7003 | #endif |
| 7004 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7005 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 7006 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7007 | * char is 'b'. |
| 7008 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7009 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7010 | 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] | 7011 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7012 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7013 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7014 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7015 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7016 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7017 | return 0; |
| 7018 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7019 | smp->data.u.str.area = (char *)SSL_get_version(conn->xprt_ctx); |
| 7020 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7021 | return 0; |
| 7022 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7023 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7024 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7025 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7026 | |
| 7027 | return 1; |
| 7028 | } |
| 7029 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7030 | /* 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] | 7031 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7032 | * char is 'b'. |
| 7033 | */ |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7034 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7035 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7036 | 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] | 7037 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7038 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7039 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 7040 | SSL_SESSION *ssl_sess; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7041 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7042 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7043 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7044 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7045 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7046 | return 0; |
| 7047 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7048 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 7049 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7050 | return 0; |
| 7051 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7052 | smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, |
| 7053 | (unsigned int *)&smp->data.u.str.data); |
| 7054 | if (!smp->data.u.str.area || !smp->data.u.str.data) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7055 | return 0; |
| 7056 | |
| 7057 | return 1; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7058 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7059 | #endif |
| 7060 | |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7061 | |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7062 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
| 7063 | static int |
| 7064 | smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7065 | { |
| 7066 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7067 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 7068 | SSL_SESSION *ssl_sess; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7069 | struct buffer *data; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7070 | |
| 7071 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7072 | return 0; |
| 7073 | |
| 7074 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 7075 | if (!ssl_sess) |
| 7076 | return 0; |
| 7077 | |
| 7078 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7079 | data->data = SSL_SESSION_get_master_key(ssl_sess, |
| 7080 | (unsigned char *) data->area, |
| 7081 | data->size); |
| 7082 | if (!data->data) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7083 | return 0; |
| 7084 | |
| 7085 | smp->flags = 0; |
| 7086 | smp->data.type = SMP_T_BIN; |
| 7087 | smp->data.u.str = *data; |
| 7088 | |
| 7089 | return 1; |
| 7090 | } |
| 7091 | #endif |
| 7092 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7093 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7094 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7095 | 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] | 7096 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7097 | struct connection *conn; |
| 7098 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7099 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7100 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7101 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7102 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7103 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7104 | return 0; |
| 7105 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7106 | smp->data.u.str.area = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 7107 | if (!smp->data.u.str.area) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 7108 | return 0; |
| 7109 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7110 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7111 | return 1; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7112 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7113 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7114 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7115 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7116 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7117 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7118 | struct connection *conn; |
| 7119 | struct ssl_capture *capture; |
| 7120 | |
| 7121 | conn = objt_conn(smp->sess->origin); |
| 7122 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7123 | return 0; |
| 7124 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7125 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7126 | if (!capture) |
| 7127 | return 0; |
| 7128 | |
| 7129 | smp->flags = SMP_F_CONST; |
| 7130 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7131 | smp->data.u.str.area = capture->ciphersuite; |
| 7132 | smp->data.u.str.data = capture->ciphersuite_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7133 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7134 | } |
| 7135 | |
| 7136 | static int |
| 7137 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7138 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7139 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7140 | |
| 7141 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 7142 | return 0; |
| 7143 | |
| 7144 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7145 | 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] | 7146 | smp->data.type = SMP_T_BIN; |
| 7147 | smp->data.u.str = *data; |
| 7148 | return 1; |
| 7149 | } |
| 7150 | |
| 7151 | static int |
| 7152 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7153 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7154 | struct connection *conn; |
| 7155 | struct ssl_capture *capture; |
| 7156 | |
| 7157 | conn = objt_conn(smp->sess->origin); |
| 7158 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7159 | return 0; |
| 7160 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7161 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7162 | if (!capture) |
| 7163 | return 0; |
| 7164 | |
| 7165 | smp->data.type = SMP_T_SINT; |
| 7166 | smp->data.u.sint = capture->xxh64; |
| 7167 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7168 | } |
| 7169 | |
| 7170 | static int |
| 7171 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7172 | { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7173 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7174 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7175 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7176 | |
| 7177 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 7178 | return 0; |
| 7179 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7180 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7181 | for (i = 0; i + 1 < smp->data.u.str.data; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7182 | const char *str; |
| 7183 | const SSL_CIPHER *cipher; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7184 | 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] | 7185 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 7186 | #if defined(OPENSSL_IS_BORINGSSL) |
| 7187 | cipher = SSL_get_cipher_by_value(id); |
| 7188 | #else |
Willy Tarreau | b729077 | 2018-10-15 11:01:59 +0200 | [diff] [blame] | 7189 | struct connection *conn = __objt_conn(smp->sess->origin); |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7190 | cipher = SSL_CIPHER_find(conn->xprt_ctx, bin); |
| 7191 | #endif |
| 7192 | str = SSL_CIPHER_get_name(cipher); |
| 7193 | if (!str || strcmp(str, "(NONE)") == 0) |
| 7194 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7195 | else |
| 7196 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 7197 | } |
| 7198 | smp->data.type = SMP_T_STR; |
| 7199 | smp->data.u.str = *data; |
| 7200 | return 1; |
| 7201 | #else |
| 7202 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 7203 | #endif |
| 7204 | } |
| 7205 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7206 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7207 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7208 | 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] | 7209 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7210 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7211 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7212 | int finished_len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7213 | struct buffer *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7214 | |
| 7215 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7216 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7217 | return 0; |
| 7218 | |
| 7219 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 7220 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7221 | return 0; |
| 7222 | } |
| 7223 | |
| 7224 | finished_trash = get_trash_chunk(); |
| 7225 | if (!SSL_session_reused(conn->xprt_ctx)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7226 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, |
| 7227 | finished_trash->area, |
| 7228 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7229 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7230 | finished_len = SSL_get_finished(conn->xprt_ctx, |
| 7231 | finished_trash->area, |
| 7232 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7233 | |
| 7234 | if (!finished_len) |
| 7235 | return 0; |
| 7236 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7237 | finished_trash->data = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7238 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7239 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7240 | |
| 7241 | return 1; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7242 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7243 | #endif |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7244 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7245 | /* 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] | 7246 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7247 | 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] | 7248 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7249 | struct connection *conn; |
| 7250 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7251 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7252 | if (!conn || conn->xprt != &ssl_sock) |
| 7253 | return 0; |
| 7254 | |
| 7255 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7256 | smp->flags = SMP_F_MAY_CHANGE; |
| 7257 | return 0; |
| 7258 | } |
| 7259 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7260 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7261 | smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7262 | smp->flags = 0; |
| 7263 | |
| 7264 | return 1; |
| 7265 | } |
| 7266 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7267 | /* 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] | 7268 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7269 | 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] | 7270 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7271 | struct connection *conn; |
| 7272 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7273 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7274 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7275 | return 0; |
| 7276 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7277 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7278 | smp->flags = SMP_F_MAY_CHANGE; |
| 7279 | return 0; |
| 7280 | } |
| 7281 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7282 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7283 | smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(conn->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7284 | smp->flags = 0; |
| 7285 | |
| 7286 | return 1; |
| 7287 | } |
| 7288 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7289 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7290 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7291 | 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] | 7292 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7293 | struct connection *conn; |
| 7294 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7295 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7296 | if (!conn || conn->xprt != &ssl_sock) |
| 7297 | return 0; |
| 7298 | |
| 7299 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7300 | smp->flags = SMP_F_MAY_CHANGE; |
| 7301 | return 0; |
| 7302 | } |
| 7303 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7304 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7305 | smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7306 | smp->flags = 0; |
| 7307 | |
| 7308 | return 1; |
| 7309 | } |
| 7310 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7311 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7312 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7313 | 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] | 7314 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7315 | struct connection *conn; |
| 7316 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7317 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7318 | if (!conn || conn->xprt != &ssl_sock) |
| 7319 | return 0; |
| 7320 | |
| 7321 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7322 | smp->flags = SMP_F_MAY_CHANGE; |
| 7323 | return 0; |
| 7324 | } |
| 7325 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7326 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7327 | return 0; |
| 7328 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7329 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7330 | smp->data.u.sint = (long long int)SSL_get_verify_result(conn->xprt_ctx); |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7331 | smp->flags = 0; |
| 7332 | |
| 7333 | return 1; |
| 7334 | } |
| 7335 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7336 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7337 | 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] | 7338 | { |
| 7339 | if (!*args[cur_arg + 1]) { |
| 7340 | if (err) |
| 7341 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7342 | return ERR_ALERT | ERR_FATAL; |
| 7343 | } |
| 7344 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7345 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7346 | 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] | 7347 | else |
| 7348 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7349 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7350 | return 0; |
| 7351 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7352 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7353 | { |
| 7354 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7355 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7356 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7357 | /* parse the "ca-sign-file" bind keyword */ |
| 7358 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7359 | { |
| 7360 | if (!*args[cur_arg + 1]) { |
| 7361 | if (err) |
| 7362 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7363 | return ERR_ALERT | ERR_FATAL; |
| 7364 | } |
| 7365 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7366 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7367 | 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] | 7368 | else |
| 7369 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 7370 | |
| 7371 | return 0; |
| 7372 | } |
| 7373 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7374 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7375 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7376 | { |
| 7377 | if (!*args[cur_arg + 1]) { |
| 7378 | if (err) |
| 7379 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 7380 | return ERR_ALERT | ERR_FATAL; |
| 7381 | } |
| 7382 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 7383 | return 0; |
| 7384 | } |
| 7385 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7386 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7387 | 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] | 7388 | { |
| 7389 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7390 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7391 | return ERR_ALERT | ERR_FATAL; |
| 7392 | } |
| 7393 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7394 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7395 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7396 | return 0; |
| 7397 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7398 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7399 | { |
| 7400 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 7401 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 7402 | |
| 7403 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 7404 | /* parse the "ciphersuites" bind keyword */ |
| 7405 | static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7406 | { |
| 7407 | if (!*args[cur_arg + 1]) { |
| 7408 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
| 7409 | return ERR_ALERT | ERR_FATAL; |
| 7410 | } |
| 7411 | |
| 7412 | free(conf->ciphersuites); |
| 7413 | conf->ciphersuites = strdup(args[cur_arg + 1]); |
| 7414 | return 0; |
| 7415 | } |
| 7416 | static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7417 | { |
| 7418 | return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err); |
| 7419 | } |
| 7420 | #endif |
| 7421 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7422 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7423 | 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] | 7424 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 7425 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 7426 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7427 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7428 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7429 | return ERR_ALERT | ERR_FATAL; |
| 7430 | } |
| 7431 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7432 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 7433 | 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] | 7434 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 7435 | return ERR_ALERT | ERR_FATAL; |
| 7436 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7437 | 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] | 7438 | if (ssl_sock_load_cert(path, conf, err) > 0) |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7439 | return ERR_ALERT | ERR_FATAL; |
| 7440 | |
| 7441 | return 0; |
| 7442 | } |
| 7443 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 7444 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7445 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7446 | |
| 7447 | return 0; |
| 7448 | } |
| 7449 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7450 | /* parse the "crt-list" bind keyword */ |
| 7451 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7452 | { |
| 7453 | if (!*args[cur_arg + 1]) { |
| 7454 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 7455 | return ERR_ALERT | ERR_FATAL; |
| 7456 | } |
| 7457 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7458 | 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] | 7459 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7460 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 7461 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7462 | |
| 7463 | return 0; |
| 7464 | } |
| 7465 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7466 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7467 | 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] | 7468 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7469 | #ifndef X509_V_FLAG_CRL_CHECK |
| 7470 | if (err) |
| 7471 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 7472 | return ERR_ALERT | ERR_FATAL; |
| 7473 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7474 | if (!*args[cur_arg + 1]) { |
| 7475 | if (err) |
| 7476 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 7477 | return ERR_ALERT | ERR_FATAL; |
| 7478 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7479 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7480 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7481 | 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] | 7482 | else |
| 7483 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7484 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7485 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7486 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7487 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7488 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7489 | { |
| 7490 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7491 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7492 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 7493 | /* parse the "curves" bind keyword keyword */ |
| 7494 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7495 | { |
| 7496 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 7497 | if (!*args[cur_arg + 1]) { |
| 7498 | if (err) |
| 7499 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 7500 | return ERR_ALERT | ERR_FATAL; |
| 7501 | } |
| 7502 | conf->curves = strdup(args[cur_arg + 1]); |
| 7503 | return 0; |
| 7504 | #else |
| 7505 | if (err) |
| 7506 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 7507 | return ERR_ALERT | ERR_FATAL; |
| 7508 | #endif |
| 7509 | } |
| 7510 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7511 | { |
| 7512 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 7513 | } |
| 7514 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7515 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7516 | 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] | 7517 | { |
| 7518 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 7519 | if (err) |
| 7520 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 7521 | return ERR_ALERT | ERR_FATAL; |
| 7522 | #elif defined(OPENSSL_NO_ECDH) |
| 7523 | if (err) |
| 7524 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 7525 | return ERR_ALERT | ERR_FATAL; |
| 7526 | #else |
| 7527 | if (!*args[cur_arg + 1]) { |
| 7528 | if (err) |
| 7529 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 7530 | return ERR_ALERT | ERR_FATAL; |
| 7531 | } |
| 7532 | |
| 7533 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7534 | |
| 7535 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7536 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7537 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7538 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7539 | { |
| 7540 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 7541 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7542 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7543 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7544 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7545 | { |
| 7546 | int code; |
| 7547 | char *p = args[cur_arg + 1]; |
| 7548 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 7549 | |
| 7550 | if (!*p) { |
| 7551 | if (err) |
| 7552 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 7553 | return ERR_ALERT | ERR_FATAL; |
| 7554 | } |
| 7555 | |
| 7556 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 7557 | ignerr = &conf->ca_ignerr; |
| 7558 | |
| 7559 | if (strcmp(p, "all") == 0) { |
| 7560 | *ignerr = ~0ULL; |
| 7561 | return 0; |
| 7562 | } |
| 7563 | |
| 7564 | while (p) { |
| 7565 | code = atoi(p); |
| 7566 | if ((code <= 0) || (code > 63)) { |
| 7567 | if (err) |
| 7568 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 7569 | args[cur_arg], code, args[cur_arg + 1]); |
| 7570 | return ERR_ALERT | ERR_FATAL; |
| 7571 | } |
| 7572 | *ignerr |= 1ULL << code; |
| 7573 | p = strchr(p, ','); |
| 7574 | if (p) |
| 7575 | p++; |
| 7576 | } |
| 7577 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7578 | return 0; |
| 7579 | } |
| 7580 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7581 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 7582 | 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] | 7583 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7584 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7585 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7586 | p = strchr(arg, '-'); |
| 7587 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7588 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7589 | p++; |
| 7590 | if (!strcmp(p, "sslv3")) |
| 7591 | v = CONF_SSLV3; |
| 7592 | else if (!strcmp(p, "tlsv10")) |
| 7593 | v = CONF_TLSV10; |
| 7594 | else if (!strcmp(p, "tlsv11")) |
| 7595 | v = CONF_TLSV11; |
| 7596 | else if (!strcmp(p, "tlsv12")) |
| 7597 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 7598 | else if (!strcmp(p, "tlsv13")) |
| 7599 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7600 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7601 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7602 | if (!strncmp(arg, "no-", 3)) |
| 7603 | methods->flags |= methodVersions[v].flag; |
| 7604 | else if (!strncmp(arg, "force-", 6)) |
| 7605 | methods->min = methods->max = v; |
| 7606 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7607 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7608 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7609 | fail: |
| 7610 | if (err) |
| 7611 | memprintf(err, "'%s' : option not implemented", arg); |
| 7612 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7613 | } |
| 7614 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7615 | 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] | 7616 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7617 | 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] | 7618 | } |
| 7619 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7620 | 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] | 7621 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7622 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 7623 | } |
| 7624 | |
| 7625 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 7626 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 7627 | { |
| 7628 | uint16_t i, v = 0; |
| 7629 | char *argv = args[cur_arg + 1]; |
| 7630 | if (!*argv) { |
| 7631 | if (err) |
| 7632 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
| 7633 | return ERR_ALERT | ERR_FATAL; |
| 7634 | } |
| 7635 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 7636 | if (!strcmp(argv, methodVersions[i].name)) |
| 7637 | v = i; |
| 7638 | if (!v) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7639 | if (err) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7640 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7641 | return ERR_ALERT | ERR_FATAL; |
| 7642 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7643 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 7644 | methods->min = v; |
| 7645 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 7646 | methods->max = v; |
| 7647 | else { |
| 7648 | if (err) |
| 7649 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
| 7650 | return ERR_ALERT | ERR_FATAL; |
| 7651 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7652 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7653 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7654 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 7655 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7656 | { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 7657 | #if (OPENSSL_VERSION_NUMBER < 0x10101000L) || !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7658 | 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] | 7659 | #endif |
| 7660 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 7661 | } |
| 7662 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7663 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7664 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7665 | 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] | 7666 | } |
| 7667 | |
| 7668 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7669 | { |
| 7670 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 7671 | } |
| 7672 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7673 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 7674 | 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] | 7675 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 7676 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7677 | return 0; |
| 7678 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7679 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7680 | /* parse the "allow-0rtt" bind keyword */ |
| 7681 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7682 | { |
| 7683 | conf->early_data = 1; |
| 7684 | return 0; |
| 7685 | } |
| 7686 | |
| 7687 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7688 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 7689 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7690 | return 0; |
| 7691 | } |
| 7692 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7693 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7694 | 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] | 7695 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7696 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7697 | char *p1, *p2; |
| 7698 | |
| 7699 | if (!*args[cur_arg + 1]) { |
| 7700 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 7701 | return ERR_ALERT | ERR_FATAL; |
| 7702 | } |
| 7703 | |
| 7704 | free(conf->npn_str); |
| 7705 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 7706 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 7707 | * so we reuse each comma to store the next <len> and need |
| 7708 | * one more for the end of the string. |
| 7709 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7710 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 7711 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7712 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 7713 | |
| 7714 | /* replace commas with the name length */ |
| 7715 | p1 = conf->npn_str; |
| 7716 | p2 = p1 + 1; |
| 7717 | while (1) { |
| 7718 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 7719 | if (!p2) |
| 7720 | p2 = p1 + 1 + strlen(p1 + 1); |
| 7721 | |
| 7722 | if (p2 - (p1 + 1) > 255) { |
| 7723 | *p2 = '\0'; |
| 7724 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 7725 | return ERR_ALERT | ERR_FATAL; |
| 7726 | } |
| 7727 | |
| 7728 | *p1 = p2 - (p1 + 1); |
| 7729 | p1 = p2; |
| 7730 | |
| 7731 | if (!*p2) |
| 7732 | break; |
| 7733 | |
| 7734 | *(p2++) = '\0'; |
| 7735 | } |
| 7736 | return 0; |
| 7737 | #else |
| 7738 | if (err) |
| 7739 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 7740 | return ERR_ALERT | ERR_FATAL; |
| 7741 | #endif |
| 7742 | } |
| 7743 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7744 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7745 | { |
| 7746 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 7747 | } |
| 7748 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7749 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7750 | 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] | 7751 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7752 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7753 | char *p1, *p2; |
| 7754 | |
| 7755 | if (!*args[cur_arg + 1]) { |
| 7756 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 7757 | return ERR_ALERT | ERR_FATAL; |
| 7758 | } |
| 7759 | |
| 7760 | free(conf->alpn_str); |
| 7761 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 7762 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 7763 | * so we reuse each comma to store the next <len> and need |
| 7764 | * one more for the end of the string. |
| 7765 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7766 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 7767 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7768 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 7769 | |
| 7770 | /* replace commas with the name length */ |
| 7771 | p1 = conf->alpn_str; |
| 7772 | p2 = p1 + 1; |
| 7773 | while (1) { |
| 7774 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 7775 | if (!p2) |
| 7776 | p2 = p1 + 1 + strlen(p1 + 1); |
| 7777 | |
| 7778 | if (p2 - (p1 + 1) > 255) { |
| 7779 | *p2 = '\0'; |
| 7780 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 7781 | return ERR_ALERT | ERR_FATAL; |
| 7782 | } |
| 7783 | |
| 7784 | *p1 = p2 - (p1 + 1); |
| 7785 | p1 = p2; |
| 7786 | |
| 7787 | if (!*p2) |
| 7788 | break; |
| 7789 | |
| 7790 | *(p2++) = '\0'; |
| 7791 | } |
| 7792 | return 0; |
| 7793 | #else |
| 7794 | if (err) |
| 7795 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 7796 | return ERR_ALERT | ERR_FATAL; |
| 7797 | #endif |
| 7798 | } |
| 7799 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7800 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7801 | { |
| 7802 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 7803 | } |
| 7804 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7805 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7806 | 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] | 7807 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 7808 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7809 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7810 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7811 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 7812 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 7813 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 7814 | if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites) |
| 7815 | conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 7816 | #endif |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 7817 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7818 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 7819 | if (!conf->ssl_conf.ssl_methods.min) |
| 7820 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 7821 | if (!conf->ssl_conf.ssl_methods.max) |
| 7822 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7823 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7824 | return 0; |
| 7825 | } |
| 7826 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 7827 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 7828 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7829 | { |
| 7830 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 7831 | return 0; |
| 7832 | } |
| 7833 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7834 | /* parse the "generate-certificates" bind keyword */ |
| 7835 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7836 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7837 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7838 | conf->generate_certs = 1; |
| 7839 | #else |
| 7840 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 7841 | err && *err ? *err : ""); |
| 7842 | #endif |
| 7843 | return 0; |
| 7844 | } |
| 7845 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 7846 | /* parse the "strict-sni" bind keyword */ |
| 7847 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7848 | { |
| 7849 | conf->strict_sni = 1; |
| 7850 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7851 | } |
| 7852 | |
| 7853 | /* parse the "tls-ticket-keys" bind keyword */ |
| 7854 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7855 | { |
| 7856 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7857 | FILE *f; |
| 7858 | int i = 0; |
| 7859 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7860 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7861 | |
| 7862 | if (!*args[cur_arg + 1]) { |
| 7863 | if (err) |
| 7864 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 7865 | return ERR_ALERT | ERR_FATAL; |
| 7866 | } |
| 7867 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7868 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 7869 | if (keys_ref) { |
| 7870 | keys_ref->refcount++; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7871 | conf->keys_ref = keys_ref; |
| 7872 | return 0; |
| 7873 | } |
| 7874 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 7875 | keys_ref = malloc(sizeof(*keys_ref)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 7876 | if (!keys_ref) { |
| 7877 | if (err) |
| 7878 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
| 7879 | return ERR_ALERT | ERR_FATAL; |
| 7880 | } |
| 7881 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 7882 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 7883 | if (!keys_ref->tlskeys) { |
| 7884 | free(keys_ref); |
| 7885 | if (err) |
| 7886 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
| 7887 | return ERR_ALERT | ERR_FATAL; |
| 7888 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7889 | |
| 7890 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 7891 | free(keys_ref->tlskeys); |
| 7892 | free(keys_ref); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7893 | if (err) |
| 7894 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 7895 | return ERR_ALERT | ERR_FATAL; |
| 7896 | } |
| 7897 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7898 | keys_ref->filename = strdup(args[cur_arg + 1]); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 7899 | if (!keys_ref->filename) { |
| 7900 | free(keys_ref->tlskeys); |
| 7901 | free(keys_ref); |
| 7902 | if (err) |
| 7903 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
| 7904 | return ERR_ALERT | ERR_FATAL; |
| 7905 | } |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7906 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 7907 | keys_ref->key_size_bits = 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7908 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 7909 | int len = strlen(thisline); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 7910 | int dec_size; |
| 7911 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7912 | /* Strip newline characters from the end */ |
| 7913 | if(thisline[len - 1] == '\n') |
| 7914 | thisline[--len] = 0; |
| 7915 | |
| 7916 | if(thisline[len - 1] == '\r') |
| 7917 | thisline[--len] = 0; |
| 7918 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 7919 | dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key)); |
| 7920 | if (dec_size < 0) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 7921 | free(keys_ref->filename); |
| 7922 | free(keys_ref->tlskeys); |
| 7923 | free(keys_ref); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7924 | if (err) |
| 7925 | 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] | 7926 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7927 | return ERR_ALERT | ERR_FATAL; |
| 7928 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 7929 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) { |
| 7930 | keys_ref->key_size_bits = 128; |
| 7931 | } |
| 7932 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) { |
| 7933 | keys_ref->key_size_bits = 256; |
| 7934 | } |
| 7935 | else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256))) |
| 7936 | || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128))) |
| 7937 | || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) { |
| 7938 | free(keys_ref->filename); |
| 7939 | free(keys_ref->tlskeys); |
| 7940 | free(keys_ref); |
| 7941 | if (err) |
| 7942 | memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1); |
| 7943 | fclose(f); |
| 7944 | return ERR_ALERT | ERR_FATAL; |
| 7945 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7946 | i++; |
| 7947 | } |
| 7948 | |
| 7949 | if (i < TLS_TICKETS_NO) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 7950 | free(keys_ref->filename); |
| 7951 | free(keys_ref->tlskeys); |
| 7952 | free(keys_ref); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7953 | if (err) |
| 7954 | 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] | 7955 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7956 | return ERR_ALERT | ERR_FATAL; |
| 7957 | } |
| 7958 | |
| 7959 | fclose(f); |
| 7960 | |
| 7961 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 7962 | i -= 2; |
| 7963 | 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] | 7964 | keys_ref->unique_id = -1; |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 7965 | keys_ref->refcount = 1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7966 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7967 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7968 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7969 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 7970 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7971 | return 0; |
| 7972 | #else |
| 7973 | if (err) |
| 7974 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 7975 | return ERR_ALERT | ERR_FATAL; |
| 7976 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 7977 | } |
| 7978 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7979 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7980 | 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] | 7981 | { |
| 7982 | if (!*args[cur_arg + 1]) { |
| 7983 | if (err) |
| 7984 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 7985 | return ERR_ALERT | ERR_FATAL; |
| 7986 | } |
| 7987 | |
| 7988 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7989 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7990 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7991 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7992 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7993 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7994 | else { |
| 7995 | if (err) |
| 7996 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 7997 | args[cur_arg], args[cur_arg + 1]); |
| 7998 | return ERR_ALERT | ERR_FATAL; |
| 7999 | } |
| 8000 | |
| 8001 | return 0; |
| 8002 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8003 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8004 | { |
| 8005 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 8006 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8007 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 8008 | /* parse the "no-ca-names" bind keyword */ |
| 8009 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8010 | { |
| 8011 | conf->no_ca_names = 1; |
| 8012 | return 0; |
| 8013 | } |
| 8014 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8015 | { |
| 8016 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 8017 | } |
| 8018 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8019 | /************** "server" keywords ****************/ |
| 8020 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8021 | /* parse the "npn" bind keyword */ |
| 8022 | static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8023 | { |
| 8024 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 8025 | char *p1, *p2; |
| 8026 | |
| 8027 | if (!*args[*cur_arg + 1]) { |
| 8028 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]); |
| 8029 | return ERR_ALERT | ERR_FATAL; |
| 8030 | } |
| 8031 | |
| 8032 | free(newsrv->ssl_ctx.npn_str); |
| 8033 | |
| 8034 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 8035 | * so we reuse each comma to store the next <len> and need |
| 8036 | * one more for the end of the string. |
| 8037 | */ |
| 8038 | newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1; |
| 8039 | newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1); |
| 8040 | memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1], |
| 8041 | newsrv->ssl_ctx.npn_len); |
| 8042 | |
| 8043 | /* replace commas with the name length */ |
| 8044 | p1 = newsrv->ssl_ctx.npn_str; |
| 8045 | p2 = p1 + 1; |
| 8046 | while (1) { |
| 8047 | p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str + |
| 8048 | newsrv->ssl_ctx.npn_len - (p1 + 1)); |
| 8049 | if (!p2) |
| 8050 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8051 | |
| 8052 | if (p2 - (p1 + 1) > 255) { |
| 8053 | *p2 = '\0'; |
| 8054 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 8055 | return ERR_ALERT | ERR_FATAL; |
| 8056 | } |
| 8057 | |
| 8058 | *p1 = p2 - (p1 + 1); |
| 8059 | p1 = p2; |
| 8060 | |
| 8061 | if (!*p2) |
| 8062 | break; |
| 8063 | |
| 8064 | *(p2++) = '\0'; |
| 8065 | } |
| 8066 | return 0; |
| 8067 | #else |
| 8068 | if (err) |
| 8069 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]); |
| 8070 | return ERR_ALERT | ERR_FATAL; |
| 8071 | #endif |
| 8072 | } |
| 8073 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8074 | /* parse the "alpn" or the "check-alpn" server keyword */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8075 | static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8076 | { |
| 8077 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 8078 | char *p1, *p2; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8079 | char **alpn_str; |
| 8080 | int *alpn_len; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8081 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8082 | if (*args[*cur_arg] == 'c') { |
| 8083 | alpn_str = &newsrv->check.alpn_str; |
| 8084 | alpn_len = &newsrv->check.alpn_len; |
| 8085 | } else { |
| 8086 | alpn_str = &newsrv->ssl_ctx.alpn_str; |
| 8087 | alpn_len = &newsrv->ssl_ctx.alpn_len; |
| 8088 | |
| 8089 | } |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8090 | if (!*args[*cur_arg + 1]) { |
| 8091 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]); |
| 8092 | return ERR_ALERT | ERR_FATAL; |
| 8093 | } |
| 8094 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8095 | free(*alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8096 | |
| 8097 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 8098 | * so we reuse each comma to store the next <len> and need |
| 8099 | * one more for the end of the string. |
| 8100 | */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8101 | *alpn_len = strlen(args[*cur_arg + 1]) + 1; |
| 8102 | *alpn_str = calloc(1, *alpn_len + 1); |
| 8103 | memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8104 | |
| 8105 | /* replace commas with the name length */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8106 | p1 = *alpn_str; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8107 | p2 = p1 + 1; |
| 8108 | while (1) { |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8109 | p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1)); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8110 | if (!p2) |
| 8111 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8112 | |
| 8113 | if (p2 - (p1 + 1) > 255) { |
| 8114 | *p2 = '\0'; |
| 8115 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 8116 | return ERR_ALERT | ERR_FATAL; |
| 8117 | } |
| 8118 | |
| 8119 | *p1 = p2 - (p1 + 1); |
| 8120 | p1 = p2; |
| 8121 | |
| 8122 | if (!*p2) |
| 8123 | break; |
| 8124 | |
| 8125 | *(p2++) = '\0'; |
| 8126 | } |
| 8127 | return 0; |
| 8128 | #else |
| 8129 | if (err) |
| 8130 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]); |
| 8131 | return ERR_ALERT | ERR_FATAL; |
| 8132 | #endif |
| 8133 | } |
| 8134 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8135 | /* parse the "ca-file" server keyword */ |
| 8136 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8137 | { |
| 8138 | if (!*args[*cur_arg + 1]) { |
| 8139 | if (err) |
| 8140 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 8141 | return ERR_ALERT | ERR_FATAL; |
| 8142 | } |
| 8143 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8144 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8145 | 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] | 8146 | else |
| 8147 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 8148 | |
| 8149 | return 0; |
| 8150 | } |
| 8151 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 8152 | /* parse the "check-sni" server keyword */ |
| 8153 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8154 | { |
| 8155 | if (!*args[*cur_arg + 1]) { |
| 8156 | if (err) |
| 8157 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
| 8158 | return ERR_ALERT | ERR_FATAL; |
| 8159 | } |
| 8160 | |
| 8161 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 8162 | if (!newsrv->check.sni) { |
| 8163 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 8164 | return ERR_ALERT | ERR_FATAL; |
| 8165 | } |
| 8166 | return 0; |
| 8167 | |
| 8168 | } |
| 8169 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8170 | /* parse the "check-ssl" server keyword */ |
| 8171 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8172 | { |
| 8173 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8174 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 8175 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8176 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 8177 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 8178 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 8179 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8180 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8181 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 8182 | if (!newsrv->ssl_ctx.methods.min) |
| 8183 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 8184 | if (!newsrv->ssl_ctx.methods.max) |
| 8185 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 8186 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8187 | return 0; |
| 8188 | } |
| 8189 | |
| 8190 | /* parse the "ciphers" server keyword */ |
| 8191 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8192 | { |
| 8193 | if (!*args[*cur_arg + 1]) { |
| 8194 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 8195 | return ERR_ALERT | ERR_FATAL; |
| 8196 | } |
| 8197 | |
| 8198 | free(newsrv->ssl_ctx.ciphers); |
| 8199 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 8200 | return 0; |
| 8201 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8202 | |
| 8203 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 8204 | /* parse the "ciphersuites" server keyword */ |
| 8205 | static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8206 | { |
| 8207 | if (!*args[*cur_arg + 1]) { |
| 8208 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 8209 | return ERR_ALERT | ERR_FATAL; |
| 8210 | } |
| 8211 | |
| 8212 | free(newsrv->ssl_ctx.ciphersuites); |
| 8213 | newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]); |
| 8214 | return 0; |
| 8215 | } |
| 8216 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8217 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8218 | /* parse the "crl-file" server keyword */ |
| 8219 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8220 | { |
| 8221 | #ifndef X509_V_FLAG_CRL_CHECK |
| 8222 | if (err) |
| 8223 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 8224 | return ERR_ALERT | ERR_FATAL; |
| 8225 | #else |
| 8226 | if (!*args[*cur_arg + 1]) { |
| 8227 | if (err) |
| 8228 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 8229 | return ERR_ALERT | ERR_FATAL; |
| 8230 | } |
| 8231 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8232 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8233 | 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] | 8234 | else |
| 8235 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 8236 | |
| 8237 | return 0; |
| 8238 | #endif |
| 8239 | } |
| 8240 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 8241 | /* parse the "crt" server keyword */ |
| 8242 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8243 | { |
| 8244 | if (!*args[*cur_arg + 1]) { |
| 8245 | if (err) |
| 8246 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 8247 | return ERR_ALERT | ERR_FATAL; |
| 8248 | } |
| 8249 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8250 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 8251 | 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] | 8252 | else |
| 8253 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 8254 | |
| 8255 | return 0; |
| 8256 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8257 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 8258 | /* parse the "no-check-ssl" server keyword */ |
| 8259 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8260 | { |
| 8261 | newsrv->check.use_ssl = 0; |
| 8262 | free(newsrv->ssl_ctx.ciphers); |
| 8263 | newsrv->ssl_ctx.ciphers = NULL; |
| 8264 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 8265 | return 0; |
| 8266 | } |
| 8267 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 8268 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 8269 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8270 | { |
| 8271 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 8272 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 8273 | return 0; |
| 8274 | } |
| 8275 | |
| 8276 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 8277 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8278 | { |
| 8279 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 8280 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 8281 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 8282 | return 0; |
| 8283 | } |
| 8284 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 8285 | /* parse the "no-ssl" server keyword */ |
| 8286 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8287 | { |
| 8288 | newsrv->use_ssl = 0; |
| 8289 | free(newsrv->ssl_ctx.ciphers); |
| 8290 | newsrv->ssl_ctx.ciphers = NULL; |
| 8291 | return 0; |
| 8292 | } |
| 8293 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 8294 | /* parse the "allow-0rtt" server keyword */ |
| 8295 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8296 | { |
| 8297 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 8298 | return 0; |
| 8299 | } |
| 8300 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 8301 | /* parse the "no-ssl-reuse" server keyword */ |
| 8302 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8303 | { |
| 8304 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 8305 | return 0; |
| 8306 | } |
| 8307 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 8308 | /* parse the "no-tls-tickets" server keyword */ |
| 8309 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8310 | { |
| 8311 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 8312 | return 0; |
| 8313 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 8314 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 8315 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8316 | { |
| 8317 | newsrv->pp_opts |= SRV_PP_V2; |
| 8318 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 8319 | return 0; |
| 8320 | } |
| 8321 | |
| 8322 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 8323 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8324 | { |
| 8325 | newsrv->pp_opts |= SRV_PP_V2; |
| 8326 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 8327 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 8328 | return 0; |
| 8329 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 8330 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8331 | /* parse the "sni" server keyword */ |
| 8332 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8333 | { |
| 8334 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 8335 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 8336 | return ERR_ALERT | ERR_FATAL; |
| 8337 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 8338 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8339 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 8340 | arg = args[*cur_arg + 1]; |
| 8341 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8342 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 8343 | return ERR_ALERT | ERR_FATAL; |
| 8344 | } |
| 8345 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 8346 | free(newsrv->sni_expr); |
| 8347 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8348 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8349 | return 0; |
| 8350 | #endif |
| 8351 | } |
| 8352 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8353 | /* parse the "ssl" server keyword */ |
| 8354 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8355 | { |
| 8356 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8357 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 8358 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8359 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 8360 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 8361 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 8362 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8363 | return 0; |
| 8364 | } |
| 8365 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 8366 | /* parse the "ssl-reuse" server keyword */ |
| 8367 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8368 | { |
| 8369 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 8370 | return 0; |
| 8371 | } |
| 8372 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 8373 | /* parse the "tls-tickets" server keyword */ |
| 8374 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8375 | { |
| 8376 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 8377 | return 0; |
| 8378 | } |
| 8379 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8380 | /* parse the "verify" server keyword */ |
| 8381 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8382 | { |
| 8383 | if (!*args[*cur_arg + 1]) { |
| 8384 | if (err) |
| 8385 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 8386 | return ERR_ALERT | ERR_FATAL; |
| 8387 | } |
| 8388 | |
| 8389 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8390 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8391 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8392 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8393 | else { |
| 8394 | if (err) |
| 8395 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 8396 | args[*cur_arg], args[*cur_arg + 1]); |
| 8397 | return ERR_ALERT | ERR_FATAL; |
| 8398 | } |
| 8399 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8400 | return 0; |
| 8401 | } |
| 8402 | |
| 8403 | /* parse the "verifyhost" server keyword */ |
| 8404 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8405 | { |
| 8406 | if (!*args[*cur_arg + 1]) { |
| 8407 | if (err) |
| 8408 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 8409 | return ERR_ALERT | ERR_FATAL; |
| 8410 | } |
| 8411 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 8412 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8413 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 8414 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8415 | return 0; |
| 8416 | } |
| 8417 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8418 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 8419 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 8420 | struct proxy *defpx, const char *file, int line, |
| 8421 | char **err) { |
| 8422 | int i = 1; |
| 8423 | |
| 8424 | if (*(args[i]) == 0) { |
| 8425 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8426 | return -1; |
| 8427 | } |
| 8428 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8429 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8430 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 8431 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 8432 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8433 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8434 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 8435 | i++; |
| 8436 | else { |
| 8437 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8438 | return -1; |
| 8439 | } |
| 8440 | } |
| 8441 | 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] | 8442 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8443 | return -1; |
| 8444 | } |
| 8445 | i++; |
| 8446 | } |
| 8447 | return 0; |
| 8448 | } |
| 8449 | |
| 8450 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 8451 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 8452 | struct proxy *defpx, const char *file, int line, |
| 8453 | char **err) { |
| 8454 | int i = 1; |
| 8455 | |
| 8456 | if (*(args[i]) == 0) { |
| 8457 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8458 | return -1; |
| 8459 | } |
| 8460 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8461 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8462 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8463 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8464 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 8465 | i++; |
| 8466 | else { |
| 8467 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8468 | return -1; |
| 8469 | } |
| 8470 | } |
| 8471 | 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] | 8472 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8473 | return -1; |
| 8474 | } |
| 8475 | i++; |
| 8476 | } |
| 8477 | return 0; |
| 8478 | } |
| 8479 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8480 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 8481 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8482 | */ |
| 8483 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 8484 | struct proxy *defpx, const char *file, int line, |
| 8485 | char **err) |
| 8486 | { |
| 8487 | char **target; |
| 8488 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8489 | 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] | 8490 | |
| 8491 | if (too_many_args(1, args, err, NULL)) |
| 8492 | return -1; |
| 8493 | |
| 8494 | if (*target) { |
| 8495 | memprintf(err, "'%s' already specified.", args[0]); |
| 8496 | return -1; |
| 8497 | } |
| 8498 | |
| 8499 | if (*(args[1]) == 0) { |
| 8500 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 8501 | return -1; |
| 8502 | } |
| 8503 | *target = strdup(args[1]); |
| 8504 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8505 | } |
| 8506 | |
| 8507 | /* parse the "ssl-mode-async" keyword in global section. |
| 8508 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8509 | */ |
| 8510 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 8511 | struct proxy *defpx, const char *file, int line, |
| 8512 | char **err) |
| 8513 | { |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 8514 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8515 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 8516 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8517 | return 0; |
| 8518 | #else |
| 8519 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 8520 | return -1; |
| 8521 | #endif |
| 8522 | } |
| 8523 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8524 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8525 | static int ssl_check_async_engine_count(void) { |
| 8526 | int err_code = 0; |
| 8527 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 8528 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8529 | 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] | 8530 | err_code = ERR_ABORT; |
| 8531 | } |
| 8532 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8533 | } |
| 8534 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8535 | /* parse the "ssl-engine" keyword in global section. |
| 8536 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8537 | */ |
| 8538 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 8539 | struct proxy *defpx, const char *file, int line, |
| 8540 | char **err) |
| 8541 | { |
| 8542 | char *algo; |
| 8543 | int ret = -1; |
| 8544 | |
| 8545 | if (*(args[1]) == 0) { |
| 8546 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 8547 | return ret; |
| 8548 | } |
| 8549 | |
| 8550 | if (*(args[2]) == 0) { |
| 8551 | /* if no list of algorithms is given, it defaults to ALL */ |
| 8552 | algo = strdup("ALL"); |
| 8553 | goto add_engine; |
| 8554 | } |
| 8555 | |
| 8556 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 8557 | if (strcmp(args[2], "algo") != 0) { |
| 8558 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 8559 | return ret; |
| 8560 | } |
| 8561 | |
| 8562 | if (*(args[3]) == 0) { |
| 8563 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 8564 | return ret; |
| 8565 | } |
| 8566 | algo = strdup(args[3]); |
| 8567 | |
| 8568 | add_engine: |
| 8569 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 8570 | openssl_engines_initialized++; |
| 8571 | ret = 0; |
| 8572 | } |
| 8573 | free(algo); |
| 8574 | return ret; |
| 8575 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8576 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8577 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8578 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 8579 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 8580 | */ |
| 8581 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 8582 | struct proxy *defpx, const char *file, int line, |
| 8583 | char **err) |
| 8584 | { |
| 8585 | char **target; |
| 8586 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8587 | 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] | 8588 | |
| 8589 | if (too_many_args(1, args, err, NULL)) |
| 8590 | return -1; |
| 8591 | |
| 8592 | if (*(args[1]) == 0) { |
| 8593 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 8594 | return -1; |
| 8595 | } |
| 8596 | |
| 8597 | free(*target); |
| 8598 | *target = strdup(args[1]); |
| 8599 | return 0; |
| 8600 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8601 | |
| 8602 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 8603 | /* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords |
| 8604 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 8605 | */ |
| 8606 | static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx, |
| 8607 | struct proxy *defpx, const char *file, int line, |
| 8608 | char **err) |
| 8609 | { |
| 8610 | char **target; |
| 8611 | |
| 8612 | target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites; |
| 8613 | |
| 8614 | if (too_many_args(1, args, err, NULL)) |
| 8615 | return -1; |
| 8616 | |
| 8617 | if (*(args[1]) == 0) { |
| 8618 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 8619 | return -1; |
| 8620 | } |
| 8621 | |
| 8622 | free(*target); |
| 8623 | *target = strdup(args[1]); |
| 8624 | return 0; |
| 8625 | } |
| 8626 | #endif |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8627 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8628 | /* parse various global tune.ssl settings consisting in positive integers. |
| 8629 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8630 | */ |
| 8631 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 8632 | struct proxy *defpx, const char *file, int line, |
| 8633 | char **err) |
| 8634 | { |
| 8635 | int *target; |
| 8636 | |
| 8637 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 8638 | target = &global.tune.sslcachesize; |
| 8639 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8640 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8641 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8642 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 8643 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 8644 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8645 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 8646 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8647 | else { |
| 8648 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 8649 | return -1; |
| 8650 | } |
| 8651 | |
| 8652 | if (too_many_args(1, args, err, NULL)) |
| 8653 | return -1; |
| 8654 | |
| 8655 | if (*(args[1]) == 0) { |
| 8656 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 8657 | return -1; |
| 8658 | } |
| 8659 | |
| 8660 | *target = atoi(args[1]); |
| 8661 | if (*target < 0) { |
| 8662 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 8663 | return -1; |
| 8664 | } |
| 8665 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8666 | } |
| 8667 | |
| 8668 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 8669 | struct proxy *defpx, const char *file, int line, |
| 8670 | char **err) |
| 8671 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8672 | int ret; |
| 8673 | |
| 8674 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 8675 | if (ret != 0) |
| 8676 | return ret; |
| 8677 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8678 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8679 | memprintf(err, "'%s' is already configured.", args[0]); |
| 8680 | return -1; |
| 8681 | } |
| 8682 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8683 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 8684 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8685 | memprintf(err, "Out of memory error."); |
| 8686 | return -1; |
| 8687 | } |
| 8688 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8689 | } |
| 8690 | |
| 8691 | /* parse "ssl.force-private-cache". |
| 8692 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8693 | */ |
| 8694 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 8695 | struct proxy *defpx, const char *file, int line, |
| 8696 | char **err) |
| 8697 | { |
| 8698 | if (too_many_args(0, args, err, NULL)) |
| 8699 | return -1; |
| 8700 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8701 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8702 | return 0; |
| 8703 | } |
| 8704 | |
| 8705 | /* parse "ssl.lifetime". |
| 8706 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8707 | */ |
| 8708 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 8709 | struct proxy *defpx, const char *file, int line, |
| 8710 | char **err) |
| 8711 | { |
| 8712 | const char *res; |
| 8713 | |
| 8714 | if (too_many_args(1, args, err, NULL)) |
| 8715 | return -1; |
| 8716 | |
| 8717 | if (*(args[1]) == 0) { |
| 8718 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 8719 | return -1; |
| 8720 | } |
| 8721 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8722 | 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] | 8723 | if (res) { |
| 8724 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 8725 | return -1; |
| 8726 | } |
| 8727 | return 0; |
| 8728 | } |
| 8729 | |
| 8730 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 8731 | /* parse "ssl-dh-param-file". |
| 8732 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8733 | */ |
| 8734 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 8735 | struct proxy *defpx, const char *file, int line, |
| 8736 | char **err) |
| 8737 | { |
| 8738 | if (too_many_args(1, args, err, NULL)) |
| 8739 | return -1; |
| 8740 | |
| 8741 | if (*(args[1]) == 0) { |
| 8742 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 8743 | return -1; |
| 8744 | } |
| 8745 | |
| 8746 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 8747 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 8748 | return -1; |
| 8749 | } |
| 8750 | return 0; |
| 8751 | } |
| 8752 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8753 | /* parse "ssl.default-dh-param". |
| 8754 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8755 | */ |
| 8756 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 8757 | struct proxy *defpx, const char *file, int line, |
| 8758 | char **err) |
| 8759 | { |
| 8760 | if (too_many_args(1, args, err, NULL)) |
| 8761 | return -1; |
| 8762 | |
| 8763 | if (*(args[1]) == 0) { |
| 8764 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 8765 | return -1; |
| 8766 | } |
| 8767 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8768 | global_ssl.default_dh_param = atoi(args[1]); |
| 8769 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8770 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 8771 | return -1; |
| 8772 | } |
| 8773 | return 0; |
| 8774 | } |
| 8775 | #endif |
| 8776 | |
| 8777 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8778 | /* This function is used with TLS ticket keys management. It permits to browse |
| 8779 | * each reference. The variable <getnext> must contain the current node, |
| 8780 | * <end> point to the root node. |
| 8781 | */ |
| 8782 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8783 | static inline |
| 8784 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 8785 | { |
| 8786 | struct tls_keys_ref *ref = getnext; |
| 8787 | |
| 8788 | while (1) { |
| 8789 | |
| 8790 | /* Get next list entry. */ |
| 8791 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 8792 | |
| 8793 | /* If the entry is the last of the list, return NULL. */ |
| 8794 | if (&ref->list == end) |
| 8795 | return NULL; |
| 8796 | |
| 8797 | return ref; |
| 8798 | } |
| 8799 | } |
| 8800 | |
| 8801 | static inline |
| 8802 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 8803 | { |
| 8804 | int id; |
| 8805 | char *error; |
| 8806 | |
| 8807 | /* If the reference starts by a '#', this is numeric id. */ |
| 8808 | if (reference[0] == '#') { |
| 8809 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 8810 | id = strtol(reference + 1, &error, 10); |
| 8811 | if (*error != '\0') |
| 8812 | return NULL; |
| 8813 | |
| 8814 | /* Perform the unique id lookup. */ |
| 8815 | return tlskeys_ref_lookupid(id); |
| 8816 | } |
| 8817 | |
| 8818 | /* Perform the string lookup. */ |
| 8819 | return tlskeys_ref_lookup(reference); |
| 8820 | } |
| 8821 | #endif |
| 8822 | |
| 8823 | |
| 8824 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8825 | |
| 8826 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 8827 | |
| 8828 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 8829 | return cli_io_handler_tlskeys_files(appctx); |
| 8830 | } |
| 8831 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8832 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 8833 | * (next index to be dumped), and cli.p0 (next key reference). |
| 8834 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8835 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 8836 | |
| 8837 | struct stream_interface *si = appctx->owner; |
| 8838 | |
| 8839 | switch (appctx->st2) { |
| 8840 | case STAT_ST_INIT: |
| 8841 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 8842 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8843 | * later and restart at the state "STAT_ST_INIT". |
| 8844 | */ |
| 8845 | chunk_reset(&trash); |
| 8846 | |
| 8847 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 8848 | chunk_appendf(&trash, "# id secret\n"); |
| 8849 | else |
| 8850 | chunk_appendf(&trash, "# id (file)\n"); |
| 8851 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8852 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8853 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8854 | return 0; |
| 8855 | } |
| 8856 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8857 | /* Now, we start the browsing of the references lists. |
| 8858 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 8859 | * available field of this pointer is <list>. It is used with the function |
| 8860 | * tlskeys_list_get_next() for retruning the first available entry |
| 8861 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8862 | if (appctx->ctx.cli.p0 == NULL) { |
| 8863 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 8864 | 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] | 8865 | } |
| 8866 | |
| 8867 | appctx->st2 = STAT_ST_LIST; |
| 8868 | /* fall through */ |
| 8869 | |
| 8870 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8871 | while (appctx->ctx.cli.p0) { |
| 8872 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8873 | |
| 8874 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8875 | 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] | 8876 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8877 | |
| 8878 | if (appctx->ctx.cli.i1 == 0) |
| 8879 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 8880 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8881 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8882 | int head; |
| 8883 | |
| 8884 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 8885 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8886 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8887 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8888 | |
| 8889 | chunk_reset(t2); |
| 8890 | /* 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] | 8891 | if (ref->key_size_bits == 128) { |
| 8892 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 8893 | sizeof(struct tls_sess_key_128), |
| 8894 | t2->area, t2->size); |
| 8895 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 8896 | t2->area); |
| 8897 | } |
| 8898 | else if (ref->key_size_bits == 256) { |
| 8899 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 8900 | sizeof(struct tls_sess_key_256), |
| 8901 | t2->area, t2->size); |
| 8902 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 8903 | t2->area); |
| 8904 | } |
| 8905 | else { |
| 8906 | /* This case should never happen */ |
| 8907 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 8908 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8909 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8910 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8911 | /* let's try again later from this stream. We add ourselves into |
| 8912 | * this stream's users so that it can remove us upon termination. |
| 8913 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8914 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8915 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8916 | return 0; |
| 8917 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8918 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8919 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8920 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8921 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8922 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8923 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8924 | /* let's try again later from this stream. We add ourselves into |
| 8925 | * this stream's users so that it can remove us upon termination. |
| 8926 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8927 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8928 | return 0; |
| 8929 | } |
| 8930 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8931 | 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] | 8932 | break; |
| 8933 | |
| 8934 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8935 | 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] | 8936 | } |
| 8937 | |
| 8938 | appctx->st2 = STAT_ST_FIN; |
| 8939 | /* fall through */ |
| 8940 | |
| 8941 | default: |
| 8942 | appctx->st2 = STAT_ST_FIN; |
| 8943 | return 1; |
| 8944 | } |
| 8945 | return 0; |
| 8946 | } |
| 8947 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8948 | /* 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] | 8949 | 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] | 8950 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8951 | /* no parameter, shows only file list */ |
| 8952 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8953 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8954 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 8955 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8956 | } |
| 8957 | |
| 8958 | if (args[2][0] == '*') { |
| 8959 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8960 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8961 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8962 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
| 8963 | if (!appctx->ctx.cli.p0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8964 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8965 | 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] | 8966 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8967 | return 1; |
| 8968 | } |
| 8969 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8970 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 8971 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8972 | } |
| 8973 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8974 | 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] | 8975 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8976 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 8977 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8978 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8979 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 8980 | if (!*args[3] || !*args[4]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8981 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8982 | 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] | 8983 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8984 | return 1; |
| 8985 | } |
| 8986 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8987 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 8988 | if (!ref) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8989 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8990 | 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] | 8991 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8992 | return 1; |
| 8993 | } |
| 8994 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 8995 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8996 | if (ret < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8997 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8998 | 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] | 8999 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9000 | return 1; |
| 9001 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9002 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9003 | trash.data = ret; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9004 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) { |
| 9005 | appctx->ctx.cli.severity = LOG_ERR; |
| 9006 | appctx->ctx.cli.msg = "'set ssl tls-key' received a key of wrong size.\n"; |
| 9007 | appctx->st0 = CLI_ST_PRINT; |
| 9008 | return 1; |
| 9009 | } |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9010 | appctx->ctx.cli.severity = LOG_INFO; |
Aurélien Nephtali | 6e8a41d | 2018-03-15 21:48:50 +0100 | [diff] [blame] | 9011 | appctx->ctx.cli.msg = "TLS ticket key updated!\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9012 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9013 | return 1; |
| 9014 | |
| 9015 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9016 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9017 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9018 | 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] | 9019 | { |
| 9020 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 9021 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9022 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 9023 | |
| 9024 | if (!payload) |
| 9025 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9026 | |
| 9027 | /* Expect one parameter: the new response in base64 encoding */ |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 9028 | if (!*payload) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9029 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9030 | 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] | 9031 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9032 | return 1; |
| 9033 | } |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 9034 | |
| 9035 | /* remove \r and \n from the payload */ |
| 9036 | for (i = 0, j = 0; payload[i]; i++) { |
| 9037 | if (payload[i] == '\r' || payload[i] == '\n') |
| 9038 | continue; |
| 9039 | payload[j++] = payload[i]; |
| 9040 | } |
| 9041 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9042 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9043 | ret = base64dec(payload, j, trash.area, trash.size); |
| 9044 | if (ret < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9045 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9046 | 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] | 9047 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9048 | return 1; |
| 9049 | } |
| 9050 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9051 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9052 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
| 9053 | if (err) { |
| 9054 | memprintf(&err, "%s.\n", err); |
| 9055 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9056 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9057 | } |
Aurélien Nephtali | 9a4da68 | 2018-04-16 19:02:42 +0200 | [diff] [blame] | 9058 | else { |
| 9059 | appctx->ctx.cli.severity = LOG_ERR; |
| 9060 | appctx->ctx.cli.msg = "Failed to update OCSP response.\n"; |
| 9061 | appctx->st0 = CLI_ST_PRINT; |
| 9062 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9063 | return 1; |
| 9064 | } |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9065 | appctx->ctx.cli.severity = LOG_INFO; |
Aurélien Nephtali | 6e8a41d | 2018-03-15 21:48:50 +0100 | [diff] [blame] | 9066 | appctx->ctx.cli.msg = "OCSP Response updated!\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9067 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9068 | return 1; |
| 9069 | #else |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9070 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9071 | 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] | 9072 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9073 | return 1; |
| 9074 | #endif |
| 9075 | |
| 9076 | } |
| 9077 | |
| 9078 | /* register cli keywords */ |
| 9079 | static struct cli_kw_list cli_kws = {{ },{ |
| 9080 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9081 | { { "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] | 9082 | { { "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] | 9083 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9084 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9085 | { { NULL }, NULL, NULL, NULL } |
| 9086 | }}; |
| 9087 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9088 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9089 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9090 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9091 | * Please take care of keeping this list alphabetically sorted. |
| 9092 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 9093 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9094 | { "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] | 9095 | { "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] | 9096 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Jérôme Magnin | e064a80 | 2018-12-03 22:21:04 +0100 | [diff] [blame] | 9097 | { "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] | 9098 | #endif |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9099 | { "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] | 9100 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 9101 | { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 9102 | #endif |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 9103 | { "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] | 9104 | { "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] | 9105 | { "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] | 9106 | { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 9107 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9108 | { "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] | 9109 | #endif |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 9110 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
| 9111 | { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 9112 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9113 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 9114 | { "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] | 9115 | { "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] | 9116 | { "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] | 9117 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9118 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9119 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9120 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9121 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9122 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9123 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9124 | { "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] | 9125 | { "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] | 9126 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 9127 | { "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] | 9128 | { "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] | 9129 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9130 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9131 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9132 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9133 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9134 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9135 | { "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] | 9136 | { "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] | 9137 | { "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] | 9138 | { "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] | 9139 | { "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] | 9140 | { "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] | 9141 | { "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] | 9142 | { "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] | 9143 | { "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] | 9144 | { "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] | 9145 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9146 | { "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] | 9147 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 9148 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9149 | { "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] | 9150 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9151 | { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 9152 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 9153 | { "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] | 9154 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9155 | { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 9156 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9157 | { "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] | 9158 | #endif |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 9159 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
| 9160 | { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9161 | #endif |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 9162 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9163 | { "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] | 9164 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9165 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9166 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9167 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9168 | { "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] | 9169 | { NULL, NULL, 0, 0, 0 }, |
| 9170 | }}; |
| 9171 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9172 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 9173 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9174 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9175 | * Please take care of keeping this list alphabetically sorted. |
| 9176 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 9177 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 9178 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 9179 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 9180 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9181 | }}; |
| 9182 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9183 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
| 9184 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9185 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9186 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 9187 | * all code contributors. |
| 9188 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 9189 | * the config parser can report an appropriate error when a known keyword was |
| 9190 | * not enabled. |
| 9191 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9192 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9193 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9194 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 9195 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 9196 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9197 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 9198 | { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 9199 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9200 | { "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] | 9201 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9202 | { "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] | 9203 | { "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] | 9204 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 9205 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 9206 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9207 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 9208 | { NULL, NULL, 0 }, |
| 9209 | }; |
| 9210 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9211 | /* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */ |
| 9212 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 9213 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9214 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9215 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 9216 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 9217 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 9218 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 9219 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 9220 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9221 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 9222 | { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 9223 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9224 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 9225 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 9226 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 9227 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 9228 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 9229 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 9230 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 9231 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 9232 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 9233 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 9234 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9235 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 9236 | { "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] | 9237 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 9238 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 9239 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 9240 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 9241 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9242 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 9243 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9244 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 9245 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9246 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 9247 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 9248 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 9249 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 9250 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9251 | { NULL, NULL, 0 }, |
| 9252 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9253 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9254 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 9255 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9256 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9257 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 9258 | * all code contributors. |
| 9259 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 9260 | * the config parser can report an appropriate error when a known keyword was |
| 9261 | * not enabled. |
| 9262 | */ |
| 9263 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 9264 | { "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] | 9265 | { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9266 | { "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] | 9267 | { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */ |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9268 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9269 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 9270 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9271 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 9272 | { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */ |
| 9273 | #endif |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9274 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 9275 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 9276 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 9277 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 9278 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 9279 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 9280 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 9281 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 9282 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 9283 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 9284 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 9285 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 9286 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 9287 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 9288 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 9289 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 9290 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 9291 | { "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] | 9292 | { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9293 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 9294 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 9295 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 9296 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 9297 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 9298 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 9299 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 9300 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 9301 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 9302 | { "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] | 9303 | { NULL, NULL, 0, 0 }, |
| 9304 | }}; |
| 9305 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9306 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
| 9307 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9308 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 9309 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 9310 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 9311 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9312 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 9313 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 9314 | #ifndef OPENSSL_NO_DH |
| 9315 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 9316 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9317 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9318 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9319 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9320 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9321 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 9322 | #ifndef OPENSSL_NO_DH |
| 9323 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 9324 | #endif |
| 9325 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 9326 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 9327 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 9328 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9329 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 9330 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 9331 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9332 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 9333 | { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites }, |
| 9334 | { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites }, |
| 9335 | #endif |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9336 | { 0, NULL, NULL }, |
| 9337 | }}; |
| 9338 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9339 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 9340 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 9341 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 9342 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9343 | .snd_buf = ssl_sock_from_buf, |
| 9344 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 9345 | .subscribe = conn_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 9346 | .unsubscribe = conn_unsubscribe, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9347 | .rcv_pipe = NULL, |
| 9348 | .snd_pipe = NULL, |
| 9349 | .shutr = NULL, |
| 9350 | .shutw = ssl_sock_shutw, |
| 9351 | .close = ssl_sock_close, |
| 9352 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 9353 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 9354 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 9355 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 9356 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 9357 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 9358 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9359 | }; |
| 9360 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9361 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 9362 | struct session *sess, struct stream *s, int flags) |
| 9363 | { |
| 9364 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 9365 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9366 | |
| 9367 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 9368 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9369 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 9370 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9371 | 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] | 9372 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9373 | s->req.flags |= CF_READ_NULL; |
| 9374 | return ACT_RET_YIELD; |
| 9375 | } |
| 9376 | } |
| 9377 | return (ACT_RET_CONT); |
| 9378 | } |
| 9379 | |
| 9380 | 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) |
| 9381 | { |
| 9382 | rule->action_ptr = ssl_action_wait_for_hs; |
| 9383 | |
| 9384 | return ACT_RET_PRS_OK; |
| 9385 | } |
| 9386 | |
| 9387 | static struct action_kw_list http_req_actions = {ILH, { |
| 9388 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 9389 | { /* END */ } |
| 9390 | }}; |
| 9391 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9392 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 9393 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 9394 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 9395 | |
| 9396 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 9397 | { |
| 9398 | if (ptr) { |
| 9399 | chunk_destroy(ptr); |
| 9400 | free(ptr); |
| 9401 | } |
| 9402 | } |
| 9403 | |
| 9404 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 9405 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 9406 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9407 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 9408 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 9409 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9410 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9411 | static void __ssl_sock_init(void) |
| 9412 | { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9413 | STACK_OF(SSL_COMP)* cm; |
| 9414 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9415 | if (global_ssl.listen_default_ciphers) |
| 9416 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 9417 | if (global_ssl.connect_default_ciphers) |
| 9418 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9419 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 9420 | if (global_ssl.listen_default_ciphersuites) |
| 9421 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 9422 | if (global_ssl.connect_default_ciphersuites) |
| 9423 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9424 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 9425 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 9426 | xprt_register(XPRT_SSL, &ssl_sock); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 9427 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9428 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 9429 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9430 | cm = SSL_COMP_get_compression_methods(); |
| 9431 | sk_SSL_COMP_zero(cm); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 9432 | #if defined(USE_THREAD) && ((OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9433 | ssl_locking_init(); |
| 9434 | #endif |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 9435 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 9436 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 9437 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 9438 | 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] | 9439 | 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] | 9440 | 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] | 9441 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9442 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9443 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9444 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 9445 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9446 | hap_register_post_check(tlskeys_finalize_config); |
| 9447 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9448 | |
| 9449 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 9450 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 9451 | |
| 9452 | #ifndef OPENSSL_NO_DH |
| 9453 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 9454 | hap_register_post_deinit(ssl_free_dh); |
| 9455 | #endif |
| 9456 | #ifndef OPENSSL_NO_ENGINE |
| 9457 | hap_register_post_deinit(ssl_free_engines); |
| 9458 | #endif |
| 9459 | /* Load SSL string for the verbose & debug mode. */ |
| 9460 | ERR_load_SSL_strings(); |
| 9461 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 9462 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9463 | /* Compute and register the version string */ |
| 9464 | static void ssl_register_build_options() |
| 9465 | { |
| 9466 | char *ptr = NULL; |
| 9467 | int i; |
| 9468 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9469 | memprintf(&ptr, "Built with OpenSSL version : " |
| 9470 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 9471 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9472 | #else /* OPENSSL_IS_BORINGSSL */ |
| 9473 | OPENSSL_VERSION_TEXT |
| 9474 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 9475 | OpenSSL_version(OPENSSL_VERSION), |
| 9476 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9477 | #endif |
| 9478 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
| 9479 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 9480 | "no (library version too old)" |
| 9481 | #elif defined(OPENSSL_NO_TLSEXT) |
| 9482 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 9483 | #else |
| 9484 | "yes" |
| 9485 | #endif |
| 9486 | "", ptr); |
| 9487 | |
| 9488 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 9489 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 9490 | "yes" |
| 9491 | #else |
| 9492 | #ifdef OPENSSL_NO_TLSEXT |
| 9493 | "no (because of OPENSSL_NO_TLSEXT)" |
| 9494 | #else |
| 9495 | "no (version might be too old, 0.9.8f min needed)" |
| 9496 | #endif |
| 9497 | #endif |
| 9498 | "", ptr); |
| 9499 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 9500 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 9501 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 9502 | if (methodVersions[i].option) |
| 9503 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 9504 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9505 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9506 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9507 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9508 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 9509 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9510 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9511 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9512 | void ssl_free_engines(void) { |
| 9513 | struct ssl_engine_list *wl, *wlb; |
| 9514 | /* free up engine list */ |
| 9515 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 9516 | ENGINE_finish(wl->e); |
| 9517 | ENGINE_free(wl->e); |
| 9518 | LIST_DEL(&wl->list); |
| 9519 | free(wl); |
| 9520 | } |
| 9521 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9522 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9523 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9524 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9525 | void ssl_free_dh(void) { |
| 9526 | if (local_dh_1024) { |
| 9527 | DH_free(local_dh_1024); |
| 9528 | local_dh_1024 = NULL; |
| 9529 | } |
| 9530 | if (local_dh_2048) { |
| 9531 | DH_free(local_dh_2048); |
| 9532 | local_dh_2048 = NULL; |
| 9533 | } |
| 9534 | if (local_dh_4096) { |
| 9535 | DH_free(local_dh_4096); |
| 9536 | local_dh_4096 = NULL; |
| 9537 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 9538 | if (global_dh) { |
| 9539 | DH_free(global_dh); |
| 9540 | global_dh = NULL; |
| 9541 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9542 | } |
| 9543 | #endif |
| 9544 | |
| 9545 | __attribute__((destructor)) |
| 9546 | static void __ssl_sock_deinit(void) |
| 9547 | { |
| 9548 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9549 | if (ssl_ctx_lru_tree) { |
| 9550 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 9551 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9552 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9553 | #endif |
| 9554 | |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 9555 | #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9556 | ERR_remove_state(0); |
| 9557 | ERR_free_strings(); |
| 9558 | |
| 9559 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 9560 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9561 | |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 9562 | #if ((OPENSSL_VERSION_NUMBER >= 0x00907000L) && (OPENSSL_VERSION_NUMBER < 0x10100000L)) || defined(LIBRESSL_VERSION_NUMBER) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9563 | CRYPTO_cleanup_all_ex_data(); |
| 9564 | #endif |
| 9565 | } |
| 9566 | |
| 9567 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9568 | /* |
| 9569 | * Local variables: |
| 9570 | * c-indent-level: 8 |
| 9571 | * c-basic-offset: 8 |
| 9572 | * End: |
| 9573 | */ |