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 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 42 | #include <openssl/crypto.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 43 | #include <openssl/ssl.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 44 | #include <openssl/x509.h> |
| 45 | #include <openssl/x509v3.h> |
| 46 | #include <openssl/x509.h> |
| 47 | #include <openssl/err.h> |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 48 | #include <openssl/rand.h> |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 49 | #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] | 50 | #include <openssl/ocsp.h> |
| 51 | #endif |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 52 | #ifndef OPENSSL_NO_DH |
| 53 | #include <openssl/dh.h> |
| 54 | #endif |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 55 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 56 | #include <openssl/engine.h> |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 57 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 58 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 59 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 60 | #include <openssl/async.h> |
| 61 | #endif |
| 62 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 63 | #include <import/lru.h> |
| 64 | #include <import/xxhash.h> |
| 65 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 66 | #include <common/buffer.h> |
| 67 | #include <common/compat.h> |
| 68 | #include <common/config.h> |
| 69 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 70 | #include <common/errors.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 71 | #include <common/standard.h> |
| 72 | #include <common/ticks.h> |
| 73 | #include <common/time.h> |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 74 | #include <common/cfgparse.h> |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 75 | #include <common/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 76 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 77 | #include <ebsttree.h> |
| 78 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 79 | #include <types/applet.h> |
| 80 | #include <types/cli.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 81 | #include <types/global.h> |
| 82 | #include <types/ssl_sock.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 83 | #include <types/stats.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 84 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 85 | #include <proto/acl.h> |
| 86 | #include <proto/arg.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 87 | #include <proto/channel.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 88 | #include <proto/connection.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 89 | #include <proto/cli.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 90 | #include <proto/fd.h> |
| 91 | #include <proto/freq_ctr.h> |
| 92 | #include <proto/frontend.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 93 | #include <proto/listener.h> |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 94 | #include <proto/openssl-compat.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 95 | #include <proto/pattern.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 96 | #include <proto/proto_tcp.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 97 | #include <proto/server.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 98 | #include <proto/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 99 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 100 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 101 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 102 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 103 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 104 | #include <proto/task.h> |
| 105 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 106 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 107 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 108 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 109 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 110 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 111 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 112 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 113 | |
| 114 | /* Verify errors macros */ |
| 115 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 116 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 117 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 118 | |
| 119 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 120 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 121 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 122 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 123 | /* Supported hash function for TLS tickets */ |
| 124 | #ifdef OPENSSL_NO_SHA256 |
| 125 | #define HASH_FUNCT EVP_sha1 |
| 126 | #else |
| 127 | #define HASH_FUNCT EVP_sha256 |
| 128 | #endif /* OPENSSL_NO_SHA256 */ |
| 129 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 130 | /* ssl_methods flags for ssl options */ |
| 131 | #define MC_SSL_O_ALL 0x0000 |
| 132 | #define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */ |
| 133 | #define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */ |
| 134 | #define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */ |
| 135 | #define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 136 | #define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 137 | |
| 138 | /* ssl_methods versions */ |
| 139 | enum { |
| 140 | CONF_TLSV_NONE = 0, |
| 141 | CONF_TLSV_MIN = 1, |
| 142 | CONF_SSLV3 = 1, |
| 143 | CONF_TLSV10 = 2, |
| 144 | CONF_TLSV11 = 3, |
| 145 | CONF_TLSV12 = 4, |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 146 | CONF_TLSV13 = 5, |
| 147 | CONF_TLSV_MAX = 5, |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 148 | }; |
| 149 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 150 | /* server and bind verify method, it uses a global value as default */ |
| 151 | enum { |
| 152 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 153 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 154 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 155 | SSL_SOCK_VERIFY_NONE = 3, |
| 156 | }; |
| 157 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 158 | int sslconns = 0; |
| 159 | int totalsslconns = 0; |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 160 | static struct xprt_ops ssl_sock; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 161 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 162 | static struct { |
| 163 | char *crt_base; /* base directory path for certificates */ |
| 164 | char *ca_base; /* base directory path for CAs and CRLs */ |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 165 | int async; /* whether we use ssl async mode */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 166 | |
| 167 | char *listen_default_ciphers; |
| 168 | char *connect_default_ciphers; |
| 169 | int listen_default_ssloptions; |
| 170 | int connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 171 | struct tls_version_filter listen_default_sslmethods; |
| 172 | struct tls_version_filter connect_default_sslmethods; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 173 | |
| 174 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 175 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 176 | unsigned int max_record; /* SSL max record size */ |
| 177 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 178 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 179 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 180 | } global_ssl = { |
| 181 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 182 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 183 | #endif |
| 184 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 185 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 186 | #endif |
| 187 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 188 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 189 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 190 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 191 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 192 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 193 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 194 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 195 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 196 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 197 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 198 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 199 | #endif |
| 200 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 201 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 202 | .capture_cipherlist = 0, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 203 | }; |
| 204 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 205 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 206 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 207 | unsigned long long int xxh64; |
| 208 | unsigned char ciphersuite_len; |
| 209 | char ciphersuite[0]; |
| 210 | }; |
| 211 | struct pool_head *pool2_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 212 | static int ssl_capture_ptr_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 213 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 214 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 215 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 216 | #endif |
| 217 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 218 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 219 | static unsigned int openssl_engines_initialized; |
| 220 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 221 | struct ssl_engine_list { |
| 222 | struct list list; |
| 223 | ENGINE *e; |
| 224 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 225 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 226 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 227 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 228 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 229 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 230 | static DH *local_dh_1024 = NULL; |
| 231 | static DH *local_dh_2048 = NULL; |
| 232 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 233 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 234 | #endif /* OPENSSL_NO_DH */ |
| 235 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 236 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 237 | /* X509V3 Extensions that will be added on generated certificates */ |
| 238 | #define X509V3_EXT_SIZE 5 |
| 239 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 240 | "basicConstraints", |
| 241 | "nsComment", |
| 242 | "subjectKeyIdentifier", |
| 243 | "authorityKeyIdentifier", |
| 244 | "keyUsage", |
| 245 | }; |
| 246 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 247 | "CA:FALSE", |
| 248 | "\"OpenSSL Generated Certificate\"", |
| 249 | "hash", |
| 250 | "keyid,issuer:always", |
| 251 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 252 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 253 | /* LRU cache to store generated certificate */ |
| 254 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 255 | static unsigned int ssl_ctx_lru_seed = 0; |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 256 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 257 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 258 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 259 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 260 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 261 | /* The order here matters for picking a default context, |
| 262 | * keep the most common keytype at the bottom of the list |
| 263 | */ |
| 264 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 265 | "dsa", |
| 266 | "ecdsa", |
| 267 | "rsa" |
| 268 | }; |
| 269 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 270 | #else |
| 271 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 272 | #endif |
| 273 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 274 | /* |
| 275 | * This function gives the detail of the SSL error. It is used only |
| 276 | * if the debug mode and the verbose mode are activated. It dump all |
| 277 | * the SSL error until the stack was empty. |
| 278 | */ |
| 279 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 280 | { |
| 281 | unsigned long ret; |
| 282 | |
| 283 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 284 | while(1) { |
| 285 | ret = ERR_get_error(); |
| 286 | if (ret == 0) |
| 287 | return; |
| 288 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
| 289 | (unsigned short)conn->t.sock.fd, ret, |
| 290 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 295 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 296 | /* |
| 297 | * struct alignment works here such that the key.key is the same as key_data |
| 298 | * Do not change the placement of key_data |
| 299 | */ |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 300 | struct certificate_ocsp { |
| 301 | struct ebmb_node key; |
| 302 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 303 | struct chunk response; |
| 304 | long expire; |
| 305 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 306 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 307 | struct ocsp_cbk_arg { |
| 308 | int is_single; |
| 309 | int single_kt; |
| 310 | union { |
| 311 | struct certificate_ocsp *s_ocsp; |
| 312 | /* |
| 313 | * m_ocsp will have multiple entries dependent on key type |
| 314 | * Entry 0 - DSA |
| 315 | * Entry 1 - ECDSA |
| 316 | * Entry 2 - RSA |
| 317 | */ |
| 318 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 319 | }; |
| 320 | }; |
| 321 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 322 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 323 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 324 | { |
| 325 | int err_code = ERR_ABORT; |
| 326 | ENGINE *engine; |
| 327 | struct ssl_engine_list *el; |
| 328 | |
| 329 | /* grab the structural reference to the engine */ |
| 330 | engine = ENGINE_by_id(engine_id); |
| 331 | if (engine == NULL) { |
| 332 | Alert("ssl-engine %s: failed to get structural reference\n", engine_id); |
| 333 | goto fail_get; |
| 334 | } |
| 335 | |
| 336 | if (!ENGINE_init(engine)) { |
| 337 | /* the engine couldn't initialise, release it */ |
| 338 | Alert("ssl-engine %s: failed to initialize\n", engine_id); |
| 339 | goto fail_init; |
| 340 | } |
| 341 | |
| 342 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
| 343 | Alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id); |
| 344 | goto fail_set_method; |
| 345 | } |
| 346 | |
| 347 | el = calloc(1, sizeof(*el)); |
| 348 | el->e = engine; |
| 349 | LIST_ADD(&openssl_engines, &el->list); |
| 350 | return 0; |
| 351 | |
| 352 | fail_set_method: |
| 353 | /* release the functional reference from ENGINE_init() */ |
| 354 | ENGINE_finish(engine); |
| 355 | |
| 356 | fail_init: |
| 357 | /* release the structural reference from ENGINE_by_id() */ |
| 358 | ENGINE_free(engine); |
| 359 | |
| 360 | fail_get: |
| 361 | return err_code; |
| 362 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 363 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 364 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 365 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 366 | /* |
| 367 | * openssl async fd handler |
| 368 | */ |
| 369 | static void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 370 | { |
| 371 | struct connection *conn = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 372 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 373 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 374 | * to poll this fd until it is requested |
| 375 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 376 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 377 | fd_cant_recv(fd); |
| 378 | |
| 379 | /* crypto engine is available, let's notify the associated |
| 380 | * connection that it can pursue its processing. |
| 381 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 382 | __conn_sock_want_recv(conn); |
| 383 | __conn_sock_want_send(conn); |
| 384 | conn_update_sock_polling(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 387 | /* |
| 388 | * openssl async delayed SSL_free handler |
| 389 | */ |
| 390 | static void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 391 | { |
| 392 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 393 | OSSL_ASYNC_FD all_fd[32]; |
| 394 | size_t num_all_fds = 0; |
| 395 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 396 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 397 | /* We suppose that the async job for a same SSL * |
| 398 | * are serialized. So if we are awake it is |
| 399 | * because the running job has just finished |
| 400 | * and we can remove all async fds safely |
| 401 | */ |
| 402 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 403 | if (num_all_fds > 32) { |
| 404 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 409 | for (i=0 ; i < num_all_fds ; i++) |
| 410 | fd_remove(all_fd[i]); |
| 411 | |
| 412 | /* 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] | 413 | SSL_free(ssl); |
| 414 | sslconns--; |
| 415 | jobs--; |
| 416 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 417 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 418 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 419 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 420 | */ |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 421 | static void inline ssl_async_process_fds(struct connection *conn, SSL *ssl) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 422 | { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 423 | OSSL_ASYNC_FD add_fd[32], afd; |
| 424 | OSSL_ASYNC_FD del_fd[32]; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 425 | size_t num_add_fds = 0; |
| 426 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 427 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 428 | |
| 429 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 430 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 431 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 432 | 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] | 433 | return; |
| 434 | } |
| 435 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 436 | 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] | 437 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 438 | /* We remove unused fds from the fdtab */ |
| 439 | for (i=0 ; i < num_del_fds ; i++) |
| 440 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 441 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 442 | /* We add new fds to the fdtab */ |
| 443 | for (i=0 ; i < num_add_fds ; i++) { |
| 444 | afd = add_fd[i]; |
| 445 | fdtab[afd].owner = conn; |
| 446 | fdtab[afd].iocb = ssl_async_fd_handler; |
| 447 | fd_insert(afd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 450 | num_add_fds = 0; |
| 451 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 452 | if (num_add_fds > 32) { |
| 453 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 454 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 455 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 456 | |
| 457 | /* We activate the polling for all known async fds */ |
| 458 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 459 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 460 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 461 | /* To ensure that the fd cache won't be used |
| 462 | * We'll prefer to catch a real RD event |
| 463 | * because handling an EAGAIN on this fd will |
| 464 | * result in a context switch and also |
| 465 | * some engines uses a fd in blocking mode. |
| 466 | */ |
| 467 | fd_cant_recv(add_fd[i]); |
| 468 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 469 | |
| 470 | /* We must also prevent the conn_handler |
| 471 | * to be called until a read event was |
| 472 | * polled on an async fd |
| 473 | */ |
| 474 | __conn_sock_stop_both(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 475 | } |
| 476 | #endif |
| 477 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 478 | /* |
| 479 | * This function returns the number of seconds elapsed |
| 480 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 481 | * date presented un ASN1_GENERALIZEDTIME. |
| 482 | * |
| 483 | * In parsing error case, it returns -1. |
| 484 | */ |
| 485 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 486 | { |
| 487 | long epoch; |
| 488 | char *p, *end; |
| 489 | const unsigned short month_offset[12] = { |
| 490 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 491 | }; |
| 492 | int year, month; |
| 493 | |
| 494 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 495 | |
| 496 | p = (char *)d->data; |
| 497 | end = p + d->length; |
| 498 | |
| 499 | if (end - p < 4) return -1; |
| 500 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 501 | p += 4; |
| 502 | if (end - p < 2) return -1; |
| 503 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 504 | if (month < 1 || month > 12) return -1; |
| 505 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 506 | We consider leap years and the current month (<marsh or not) */ |
| 507 | epoch = ( ((year - 1970) * 365) |
| 508 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 509 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 510 | + month_offset[month-1] |
| 511 | ) * 24 * 60 * 60; |
| 512 | p += 2; |
| 513 | if (end - p < 2) return -1; |
| 514 | /* Add the number of seconds of completed days of current month */ |
| 515 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 516 | p += 2; |
| 517 | if (end - p < 2) return -1; |
| 518 | /* Add the completed hours of the current day */ |
| 519 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 520 | p += 2; |
| 521 | if (end - p < 2) return -1; |
| 522 | /* Add the completed minutes of the current hour */ |
| 523 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 524 | p += 2; |
| 525 | if (p == end) return -1; |
| 526 | /* Test if there is available seconds */ |
| 527 | if (p[0] < '0' || p[0] > '9') |
| 528 | goto nosec; |
| 529 | if (end - p < 2) return -1; |
| 530 | /* Add the seconds of the current minute */ |
| 531 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 532 | p += 2; |
| 533 | if (p == end) return -1; |
| 534 | /* Ignore seconds float part if present */ |
| 535 | if (p[0] == '.') { |
| 536 | do { |
| 537 | if (++p == end) return -1; |
| 538 | } while (p[0] >= '0' && p[0] <= '9'); |
| 539 | } |
| 540 | |
| 541 | nosec: |
| 542 | if (p[0] == 'Z') { |
| 543 | if (end - p != 1) return -1; |
| 544 | return epoch; |
| 545 | } |
| 546 | else if (p[0] == '+') { |
| 547 | if (end - p != 5) return -1; |
| 548 | /* Apply timezone offset */ |
| 549 | return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 550 | } |
| 551 | else if (p[0] == '-') { |
| 552 | if (end - p != 5) return -1; |
| 553 | /* Apply timezone offset */ |
| 554 | return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 555 | } |
| 556 | |
| 557 | return -1; |
| 558 | } |
| 559 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 560 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 561 | |
| 562 | /* This function starts to check if the OCSP response (in DER format) contained |
| 563 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 564 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 565 | * contained in the OCSP Response and exits on error if no match. |
| 566 | * If it's a valid OCSP Response: |
| 567 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 568 | * pointed by 'ocsp'. |
| 569 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 570 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 571 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 572 | * already present in the container, it will be overwritten. |
| 573 | * |
| 574 | * Note: OCSP response containing more than one OCSP Single response is not |
| 575 | * considered valid. |
| 576 | * |
| 577 | * Returns 0 on success, 1 in error case. |
| 578 | */ |
| 579 | static int ssl_sock_load_ocsp_response(struct chunk *ocsp_response, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 580 | { |
| 581 | OCSP_RESPONSE *resp; |
| 582 | OCSP_BASICRESP *bs = NULL; |
| 583 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 584 | OCSP_CERTID *id; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 585 | unsigned char *p = (unsigned char *)ocsp_response->str; |
| 586 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 587 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 588 | int reason; |
| 589 | int ret = 1; |
| 590 | |
| 591 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, ocsp_response->len); |
| 592 | if (!resp) { |
| 593 | memprintf(err, "Unable to parse OCSP response"); |
| 594 | goto out; |
| 595 | } |
| 596 | |
| 597 | rc = OCSP_response_status(resp); |
| 598 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 599 | memprintf(err, "OCSP response status not successful"); |
| 600 | goto out; |
| 601 | } |
| 602 | |
| 603 | bs = OCSP_response_get1_basic(resp); |
| 604 | if (!bs) { |
| 605 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 606 | goto out; |
| 607 | } |
| 608 | |
| 609 | count_sr = OCSP_resp_count(bs); |
| 610 | if (count_sr > 1) { |
| 611 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 612 | goto out; |
| 613 | } |
| 614 | |
| 615 | sr = OCSP_resp_get0(bs, 0); |
| 616 | if (!sr) { |
| 617 | memprintf(err, "Failed to get OCSP single response"); |
| 618 | goto out; |
| 619 | } |
| 620 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 621 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 622 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 623 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
| 624 | if (rc != V_OCSP_CERTSTATUS_GOOD) { |
| 625 | memprintf(err, "OCSP single response: certificate status not good"); |
| 626 | goto out; |
| 627 | } |
| 628 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 629 | if (!nextupd) { |
| 630 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 631 | goto out; |
| 632 | } |
| 633 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 634 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 635 | if (!rc) { |
| 636 | memprintf(err, "OCSP single response: no longer valid."); |
| 637 | goto out; |
| 638 | } |
| 639 | |
| 640 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 641 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 642 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 643 | goto out; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | if (!ocsp) { |
| 648 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 649 | unsigned char *p; |
| 650 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 651 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 652 | if (!rc) { |
| 653 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 654 | goto out; |
| 655 | } |
| 656 | |
| 657 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 658 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 659 | goto out; |
| 660 | } |
| 661 | |
| 662 | p = key; |
| 663 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 664 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 665 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 666 | if (!ocsp) { |
| 667 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 668 | goto out; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /* According to comments on "chunk_dup", the |
| 673 | previous chunk buffer will be freed */ |
| 674 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 675 | memprintf(err, "OCSP response: Memory allocation error"); |
| 676 | goto out; |
| 677 | } |
| 678 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 679 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 680 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 681 | ret = 0; |
| 682 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 683 | ERR_clear_error(); |
| 684 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 685 | if (bs) |
| 686 | OCSP_BASICRESP_free(bs); |
| 687 | |
| 688 | if (resp) |
| 689 | OCSP_RESPONSE_free(resp); |
| 690 | |
| 691 | return ret; |
| 692 | } |
| 693 | /* |
| 694 | * External function use to update the OCSP response in the OCSP response's |
| 695 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 696 | * to update in DER format. |
| 697 | * |
| 698 | * Returns 0 on success, 1 in error case. |
| 699 | */ |
| 700 | int ssl_sock_update_ocsp_response(struct chunk *ocsp_response, char **err) |
| 701 | { |
| 702 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * This function load the OCSP Resonse in DER format contained in file at |
| 707 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 708 | * |
| 709 | * Returns 0 on success, 1 in error case. |
| 710 | */ |
| 711 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 712 | { |
| 713 | int fd = -1; |
| 714 | int r = 0; |
| 715 | int ret = 1; |
| 716 | |
| 717 | fd = open(ocsp_path, O_RDONLY); |
| 718 | if (fd == -1) { |
| 719 | memprintf(err, "Error opening OCSP response file"); |
| 720 | goto end; |
| 721 | } |
| 722 | |
| 723 | trash.len = 0; |
| 724 | while (trash.len < trash.size) { |
| 725 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 726 | if (r < 0) { |
| 727 | if (errno == EINTR) |
| 728 | continue; |
| 729 | |
| 730 | memprintf(err, "Error reading OCSP response from file"); |
| 731 | goto end; |
| 732 | } |
| 733 | else if (r == 0) { |
| 734 | break; |
| 735 | } |
| 736 | trash.len += r; |
| 737 | } |
| 738 | |
| 739 | close(fd); |
| 740 | fd = -1; |
| 741 | |
| 742 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 743 | end: |
| 744 | if (fd != -1) |
| 745 | close(fd); |
| 746 | |
| 747 | return ret; |
| 748 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 749 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 750 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 751 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 752 | 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) |
| 753 | { |
| 754 | struct tls_sess_key *keys; |
| 755 | struct connection *conn; |
| 756 | int head; |
| 757 | int i; |
| 758 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 759 | conn = SSL_get_app_data(s); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 760 | keys = objt_listener(conn->target)->bind_conf->keys_ref->tlskeys; |
| 761 | head = objt_listener(conn->target)->bind_conf->keys_ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 762 | |
| 763 | if (enc) { |
| 764 | memcpy(key_name, keys[head].name, 16); |
| 765 | |
| 766 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
| 767 | return -1; |
| 768 | |
| 769 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].aes_key, iv)) |
| 770 | return -1; |
| 771 | |
| 772 | HMAC_Init_ex(hctx, keys[head].hmac_key, 16, HASH_FUNCT(), NULL); |
| 773 | |
| 774 | return 1; |
| 775 | } else { |
| 776 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 777 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 778 | goto found; |
| 779 | } |
| 780 | return 0; |
| 781 | |
| 782 | found: |
| 783 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].hmac_key, 16, HASH_FUNCT(), NULL); |
| 784 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].aes_key, iv)) |
| 785 | return -1; |
| 786 | /* 2 for key renewal, 1 if current key is still valid */ |
| 787 | return i ? 2 : 1; |
| 788 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 792 | { |
| 793 | struct tls_keys_ref *ref; |
| 794 | |
| 795 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 796 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 797 | return ref; |
| 798 | return NULL; |
| 799 | } |
| 800 | |
| 801 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 802 | { |
| 803 | struct tls_keys_ref *ref; |
| 804 | |
| 805 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 806 | if (ref->unique_id == unique_id) |
| 807 | return ref; |
| 808 | return NULL; |
| 809 | } |
| 810 | |
| 811 | int ssl_sock_update_tlskey(char *filename, struct chunk *tlskey, char **err) { |
| 812 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 813 | |
| 814 | if(!ref) { |
| 815 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 816 | return 1; |
| 817 | } |
| 818 | |
Pradeep Jindal | cc79b00 | 2015-08-20 18:25:17 +0530 | [diff] [blame] | 819 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), tlskey->str, tlskey->len); |
| 820 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 821 | |
| 822 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 823 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 824 | |
| 825 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 826 | * automatic ids. It's called just after the basic checks. It returns |
| 827 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 828 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 829 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 830 | { |
| 831 | int i = 0; |
| 832 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 833 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 834 | |
| 835 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 836 | if (ref->unique_id == -1) { |
| 837 | /* Look for the first free id. */ |
| 838 | while (1) { |
| 839 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 840 | if (ref2->unique_id == i) { |
| 841 | i++; |
| 842 | break; |
| 843 | } |
| 844 | } |
| 845 | if (&ref2->list == &tlskeys_reference) |
| 846 | break; |
| 847 | } |
| 848 | |
| 849 | /* Uses the unique id and increment it for the next entry. */ |
| 850 | ref->unique_id = i; |
| 851 | i++; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | /* This sort the reference list by id. */ |
| 856 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 857 | LIST_DEL(&ref->list); |
| 858 | list_for_each_entry(ref3, &tkr, list) { |
| 859 | if (ref->unique_id < ref3->unique_id) { |
| 860 | LIST_ADDQ(&ref3->list, &ref->list); |
| 861 | break; |
| 862 | } |
| 863 | } |
| 864 | if (&ref3->list == &tkr) |
| 865 | LIST_ADDQ(&tkr, &ref->list); |
| 866 | } |
| 867 | |
| 868 | /* swap root */ |
| 869 | LIST_ADD(&tkr, &tlskeys_reference); |
| 870 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 871 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 872 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 873 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 874 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 875 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 876 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 877 | { |
| 878 | switch (evp_keytype) { |
| 879 | case EVP_PKEY_RSA: |
| 880 | return 2; |
| 881 | case EVP_PKEY_DSA: |
| 882 | return 0; |
| 883 | case EVP_PKEY_EC: |
| 884 | return 1; |
| 885 | } |
| 886 | |
| 887 | return -1; |
| 888 | } |
| 889 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 890 | /* |
| 891 | * Callback used to set OCSP status extension content in server hello. |
| 892 | */ |
| 893 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 894 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 895 | struct certificate_ocsp *ocsp; |
| 896 | struct ocsp_cbk_arg *ocsp_arg; |
| 897 | char *ssl_buf; |
| 898 | EVP_PKEY *ssl_pkey; |
| 899 | int key_type; |
| 900 | int index; |
| 901 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 902 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 903 | |
| 904 | ssl_pkey = SSL_get_privatekey(ssl); |
| 905 | if (!ssl_pkey) |
| 906 | return SSL_TLSEXT_ERR_NOACK; |
| 907 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 908 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 909 | |
| 910 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 911 | ocsp = ocsp_arg->s_ocsp; |
| 912 | else { |
| 913 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 914 | * the certificate type |
| 915 | */ |
| 916 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 917 | |
| 918 | if (index < 0) |
| 919 | return SSL_TLSEXT_ERR_NOACK; |
| 920 | |
| 921 | ocsp = ocsp_arg->m_ocsp[index]; |
| 922 | |
| 923 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 924 | |
| 925 | if (!ocsp || |
| 926 | !ocsp->response.str || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 927 | !ocsp->response.len || |
| 928 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 929 | return SSL_TLSEXT_ERR_NOACK; |
| 930 | |
| 931 | ssl_buf = OPENSSL_malloc(ocsp->response.len); |
| 932 | if (!ssl_buf) |
| 933 | return SSL_TLSEXT_ERR_NOACK; |
| 934 | |
| 935 | memcpy(ssl_buf, ocsp->response.str, ocsp->response.len); |
| 936 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.len); |
| 937 | |
| 938 | return SSL_TLSEXT_ERR_OK; |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 943 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 944 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 945 | * It should be present in the certificate's extra chain builded from file |
| 946 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 947 | * named 'cert_path' suffixed using '.issuer'. |
| 948 | * |
| 949 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 950 | * response. If file is empty or content is not a valid OCSP response, |
| 951 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 952 | * is displayed). |
| 953 | * |
| 954 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
| 955 | * succesfully enabled, or -1 in other error case. |
| 956 | */ |
| 957 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 958 | { |
| 959 | |
| 960 | BIO *in = NULL; |
| 961 | X509 *x, *xi = NULL, *issuer = NULL; |
| 962 | STACK_OF(X509) *chain = NULL; |
| 963 | OCSP_CERTID *cid = NULL; |
| 964 | SSL *ssl; |
| 965 | char ocsp_path[MAXPATHLEN+1]; |
| 966 | int i, ret = -1; |
| 967 | struct stat st; |
| 968 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 969 | char *warn = NULL; |
| 970 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 971 | pem_password_cb *passwd_cb; |
| 972 | void *passwd_cb_userdata; |
| 973 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 974 | |
| 975 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 976 | |
| 977 | if (stat(ocsp_path, &st)) |
| 978 | return 1; |
| 979 | |
| 980 | ssl = SSL_new(ctx); |
| 981 | if (!ssl) |
| 982 | goto out; |
| 983 | |
| 984 | x = SSL_get_certificate(ssl); |
| 985 | if (!x) |
| 986 | goto out; |
| 987 | |
| 988 | /* Try to lookup for issuer in certificate extra chain */ |
| 989 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 990 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 991 | #else |
| 992 | chain = ctx->extra_certs; |
| 993 | #endif |
| 994 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 995 | issuer = sk_X509_value(chain, i); |
| 996 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 997 | break; |
| 998 | else |
| 999 | issuer = NULL; |
| 1000 | } |
| 1001 | |
| 1002 | /* If not found try to load issuer from a suffixed file */ |
| 1003 | if (!issuer) { |
| 1004 | char issuer_path[MAXPATHLEN+1]; |
| 1005 | |
| 1006 | in = BIO_new(BIO_s_file()); |
| 1007 | if (!in) |
| 1008 | goto out; |
| 1009 | |
| 1010 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 1011 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 1012 | goto out; |
| 1013 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1014 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 1015 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 1016 | |
| 1017 | 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] | 1018 | if (!xi) |
| 1019 | goto out; |
| 1020 | |
| 1021 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 1022 | goto out; |
| 1023 | |
| 1024 | issuer = xi; |
| 1025 | } |
| 1026 | |
| 1027 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1028 | if (!cid) |
| 1029 | goto out; |
| 1030 | |
| 1031 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1032 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1033 | goto out; |
| 1034 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1035 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1036 | if (!ocsp) |
| 1037 | goto out; |
| 1038 | |
| 1039 | p = ocsp->key_data; |
| 1040 | i2d_OCSP_CERTID(cid, &p); |
| 1041 | |
| 1042 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1043 | if (iocsp == ocsp) |
| 1044 | ocsp = NULL; |
| 1045 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1046 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1047 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1048 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1049 | #endif |
| 1050 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1051 | |
| 1052 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1053 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1054 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1055 | |
| 1056 | cb_arg->is_single = 1; |
| 1057 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1058 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1059 | pkey = X509_get_pubkey(x); |
| 1060 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1061 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1062 | |
| 1063 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1064 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1065 | } else { |
| 1066 | /* |
| 1067 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1068 | * Update that cb_arg with the new cert's staple |
| 1069 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1070 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1071 | struct certificate_ocsp *tmp_ocsp; |
| 1072 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1073 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1074 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1075 | |
| 1076 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1077 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1078 | #else |
| 1079 | cb_arg = ctx->tlsext_status_arg; |
| 1080 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1081 | |
| 1082 | /* |
| 1083 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1084 | * the order of operations below matter, take care when changing it |
| 1085 | */ |
| 1086 | tmp_ocsp = cb_arg->s_ocsp; |
| 1087 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1088 | cb_arg->s_ocsp = NULL; |
| 1089 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1090 | cb_arg->is_single = 0; |
| 1091 | cb_arg->single_kt = 0; |
| 1092 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1093 | pkey = X509_get_pubkey(x); |
| 1094 | key_type = EVP_PKEY_base_id(pkey); |
| 1095 | EVP_PKEY_free(pkey); |
| 1096 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1097 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1098 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1099 | cb_arg->m_ocsp[index] = iocsp; |
| 1100 | |
| 1101 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1102 | |
| 1103 | ret = 0; |
| 1104 | |
| 1105 | warn = NULL; |
| 1106 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 1107 | memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure"); |
| 1108 | Warning("%s.\n", warn); |
| 1109 | } |
| 1110 | |
| 1111 | out: |
| 1112 | if (ssl) |
| 1113 | SSL_free(ssl); |
| 1114 | |
| 1115 | if (in) |
| 1116 | BIO_free(in); |
| 1117 | |
| 1118 | if (xi) |
| 1119 | X509_free(xi); |
| 1120 | |
| 1121 | if (cid) |
| 1122 | OCSP_CERTID_free(cid); |
| 1123 | |
| 1124 | if (ocsp) |
| 1125 | free(ocsp); |
| 1126 | |
| 1127 | if (warn) |
| 1128 | free(warn); |
| 1129 | |
| 1130 | |
| 1131 | return ret; |
| 1132 | } |
| 1133 | |
| 1134 | #endif |
| 1135 | |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1136 | #ifdef OPENSSL_IS_BORINGSSL |
| 1137 | static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path) |
| 1138 | { |
| 1139 | char ocsp_path[MAXPATHLEN+1]; |
| 1140 | struct stat st; |
| 1141 | int fd = -1, r = 0; |
| 1142 | |
| 1143 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1144 | if (stat(ocsp_path, &st)) |
| 1145 | return 0; |
| 1146 | |
| 1147 | fd = open(ocsp_path, O_RDONLY); |
| 1148 | if (fd == -1) { |
| 1149 | Warning("Error opening OCSP response file %s.\n", ocsp_path); |
| 1150 | return -1; |
| 1151 | } |
| 1152 | |
| 1153 | trash.len = 0; |
| 1154 | while (trash.len < trash.size) { |
| 1155 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 1156 | if (r < 0) { |
| 1157 | if (errno == EINTR) |
| 1158 | continue; |
| 1159 | Warning("Error reading OCSP response from file %s.\n", ocsp_path); |
| 1160 | close(fd); |
| 1161 | return -1; |
| 1162 | } |
| 1163 | else if (r == 0) { |
| 1164 | break; |
| 1165 | } |
| 1166 | trash.len += r; |
| 1167 | } |
| 1168 | close(fd); |
| 1169 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)trash.str, trash.len); |
| 1170 | } |
| 1171 | #endif |
| 1172 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 1173 | #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] | 1174 | |
| 1175 | #define CT_EXTENSION_TYPE 18 |
| 1176 | |
| 1177 | static int sctl_ex_index = -1; |
| 1178 | |
| 1179 | /* |
| 1180 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1181 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1182 | * is performed. |
| 1183 | */ |
| 1184 | static int ssl_sock_parse_sctl(struct chunk *sctl) |
| 1185 | { |
| 1186 | int ret = 1; |
| 1187 | int len, pos, sct_len; |
| 1188 | unsigned char *data; |
| 1189 | |
| 1190 | if (sctl->len < 2) |
| 1191 | goto out; |
| 1192 | |
| 1193 | data = (unsigned char *)sctl->str; |
| 1194 | len = (data[0] << 8) | data[1]; |
| 1195 | |
| 1196 | if (len + 2 != sctl->len) |
| 1197 | goto out; |
| 1198 | |
| 1199 | data = data + 2; |
| 1200 | pos = 0; |
| 1201 | while (pos < len) { |
| 1202 | if (len - pos < 2) |
| 1203 | goto out; |
| 1204 | |
| 1205 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1206 | if (pos + sct_len + 2 > len) |
| 1207 | goto out; |
| 1208 | |
| 1209 | pos += sct_len + 2; |
| 1210 | } |
| 1211 | |
| 1212 | ret = 0; |
| 1213 | |
| 1214 | out: |
| 1215 | return ret; |
| 1216 | } |
| 1217 | |
| 1218 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, struct chunk **sctl) |
| 1219 | { |
| 1220 | int fd = -1; |
| 1221 | int r = 0; |
| 1222 | int ret = 1; |
| 1223 | |
| 1224 | *sctl = NULL; |
| 1225 | |
| 1226 | fd = open(sctl_path, O_RDONLY); |
| 1227 | if (fd == -1) |
| 1228 | goto end; |
| 1229 | |
| 1230 | trash.len = 0; |
| 1231 | while (trash.len < trash.size) { |
| 1232 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 1233 | if (r < 0) { |
| 1234 | if (errno == EINTR) |
| 1235 | continue; |
| 1236 | |
| 1237 | goto end; |
| 1238 | } |
| 1239 | else if (r == 0) { |
| 1240 | break; |
| 1241 | } |
| 1242 | trash.len += r; |
| 1243 | } |
| 1244 | |
| 1245 | ret = ssl_sock_parse_sctl(&trash); |
| 1246 | if (ret) |
| 1247 | goto end; |
| 1248 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1249 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1250 | if (!chunk_dup(*sctl, &trash)) { |
| 1251 | free(*sctl); |
| 1252 | *sctl = NULL; |
| 1253 | goto end; |
| 1254 | } |
| 1255 | |
| 1256 | end: |
| 1257 | if (fd != -1) |
| 1258 | close(fd); |
| 1259 | |
| 1260 | return ret; |
| 1261 | } |
| 1262 | |
| 1263 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1264 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1265 | struct chunk *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1266 | |
| 1267 | *out = (unsigned char *)sctl->str; |
| 1268 | *outlen = sctl->len; |
| 1269 | |
| 1270 | return 1; |
| 1271 | } |
| 1272 | |
| 1273 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1274 | { |
| 1275 | return 1; |
| 1276 | } |
| 1277 | |
| 1278 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 1279 | { |
| 1280 | char sctl_path[MAXPATHLEN+1]; |
| 1281 | int ret = -1; |
| 1282 | struct stat st; |
| 1283 | struct chunk *sctl = NULL; |
| 1284 | |
| 1285 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 1286 | |
| 1287 | if (stat(sctl_path, &st)) |
| 1288 | return 1; |
| 1289 | |
| 1290 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 1291 | goto out; |
| 1292 | |
| 1293 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 1294 | free(sctl); |
| 1295 | goto out; |
| 1296 | } |
| 1297 | |
| 1298 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1299 | |
| 1300 | ret = 0; |
| 1301 | |
| 1302 | out: |
| 1303 | return ret; |
| 1304 | } |
| 1305 | |
| 1306 | #endif |
| 1307 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1308 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1309 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1310 | struct connection *conn = SSL_get_app_data(ssl); |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1311 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1312 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1313 | |
| 1314 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1315 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1316 | if (conn->flags & CO_FL_CONNECTED) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1317 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1318 | conn->err_code = CO_ER_SSL_RENEG; |
| 1319 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1320 | } |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1321 | |
| 1322 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 1323 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 1324 | /* Long certificate chains optimz |
| 1325 | If write and read bios are differents, we |
| 1326 | consider that the buffering was activated, |
| 1327 | so we rise the output buffer size from 4k |
| 1328 | to 16k */ |
| 1329 | write_bio = SSL_get_wbio(ssl); |
| 1330 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1331 | BIO_set_write_buffer_size(write_bio, 16384); |
| 1332 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 1333 | } |
| 1334 | } |
| 1335 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1336 | } |
| 1337 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1338 | /* Callback is called for each certificate of the chain during a verify |
| 1339 | ok is set to 1 if preverify detect no error on current certificate. |
| 1340 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1341 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1342 | { |
| 1343 | SSL *ssl; |
| 1344 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1345 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1346 | |
| 1347 | ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1348 | conn = SSL_get_app_data(ssl); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1349 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1350 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1351 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1352 | if (ok) /* no errors */ |
| 1353 | return ok; |
| 1354 | |
| 1355 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1356 | err = X509_STORE_CTX_get_error(x_store); |
| 1357 | |
| 1358 | /* check if CA error needs to be ignored */ |
| 1359 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1360 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 1361 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1362 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1363 | } |
| 1364 | |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1365 | 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] | 1366 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1367 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1368 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1369 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1370 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1371 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1372 | return 0; |
| 1373 | } |
| 1374 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1375 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 1376 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1377 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1378 | /* check if certificate error needs to be ignored */ |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1379 | 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] | 1380 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1381 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1382 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1383 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1384 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1385 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1386 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1389 | static inline |
| 1390 | 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] | 1391 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1392 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1393 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1394 | unsigned char *msg; |
| 1395 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1396 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1397 | |
| 1398 | /* This function is called for "from client" and "to server" |
| 1399 | * connections. The combination of write_p == 0 and content_type == 22 |
| 1400 | * is only avalaible during "from client" connection. |
| 1401 | */ |
| 1402 | |
| 1403 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1404 | * otherwise it is set to 1. |
| 1405 | */ |
| 1406 | if (write_p != 0) |
| 1407 | return; |
| 1408 | |
| 1409 | /* content_type contains the type of message received or sent |
| 1410 | * according with the SSL/TLS protocol spec. This message is |
| 1411 | * encoded with one byte. The value 256 (two bytes) is used |
| 1412 | * for designing the SSL/TLS record layer. According with the |
| 1413 | * rfc6101, the expected message (other than 256) are: |
| 1414 | * - change_cipher_spec(20) |
| 1415 | * - alert(21) |
| 1416 | * - handshake(22) |
| 1417 | * - application_data(23) |
| 1418 | * - (255) |
| 1419 | * We are interessed by the handshake and specially the client |
| 1420 | * hello. |
| 1421 | */ |
| 1422 | if (content_type != 22) |
| 1423 | return; |
| 1424 | |
| 1425 | /* The message length is at least 4 bytes, containing the |
| 1426 | * message type and the message length. |
| 1427 | */ |
| 1428 | if (len < 4) |
| 1429 | return; |
| 1430 | |
| 1431 | /* First byte of the handshake message id the type of |
| 1432 | * message. The konwn types are: |
| 1433 | * - hello_request(0) |
| 1434 | * - client_hello(1) |
| 1435 | * - server_hello(2) |
| 1436 | * - certificate(11) |
| 1437 | * - server_key_exchange (12) |
| 1438 | * - certificate_request(13) |
| 1439 | * - server_hello_done(14) |
| 1440 | * We are interested by the client hello. |
| 1441 | */ |
| 1442 | msg = (unsigned char *)buf; |
| 1443 | if (msg[0] != 1) |
| 1444 | return; |
| 1445 | |
| 1446 | /* Next three bytes are the length of the message. The total length |
| 1447 | * must be this decoded length + 4. If the length given as argument |
| 1448 | * is not the same, we abort the protocol dissector. |
| 1449 | */ |
| 1450 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1451 | if (len < rec_len + 4) |
| 1452 | return; |
| 1453 | msg += 4; |
| 1454 | end = msg + rec_len; |
| 1455 | if (end < msg) |
| 1456 | return; |
| 1457 | |
| 1458 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1459 | * for minor, the random, composed by 4 bytes for the unix time and |
| 1460 | * 28 bytes for unix payload, and them 1 byte for the session id. So |
| 1461 | * we jump 1 + 1 + 4 + 28 + 1 bytes. |
| 1462 | */ |
| 1463 | msg += 1 + 1 + 4 + 28 + 1; |
| 1464 | if (msg > end) |
| 1465 | return; |
| 1466 | |
| 1467 | /* Next two bytes are the ciphersuite length. */ |
| 1468 | if (msg + 2 > end) |
| 1469 | return; |
| 1470 | rec_len = (msg[0] << 8) + msg[1]; |
| 1471 | msg += 2; |
| 1472 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1473 | return; |
| 1474 | |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1475 | capture = pool_alloc_dirty(pool2_ssl_capture); |
| 1476 | if (!capture) |
| 1477 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1478 | /* Compute the xxh64 of the ciphersuite. */ |
| 1479 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1480 | |
| 1481 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1482 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1483 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1484 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1485 | |
| 1486 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1487 | } |
| 1488 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1489 | /* Callback is called for ssl protocol analyse */ |
| 1490 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1491 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1492 | #ifdef TLS1_RT_HEARTBEAT |
| 1493 | /* test heartbeat received (write_p is set to 0 |
| 1494 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1495 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1496 | struct connection *conn = SSL_get_app_data(ssl); |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1497 | const unsigned char *p = buf; |
| 1498 | unsigned int payload; |
| 1499 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1500 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1501 | |
| 1502 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1503 | if (*p != TLS1_HB_REQUEST) |
| 1504 | return; |
| 1505 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1506 | 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] | 1507 | goto kill_it; |
| 1508 | |
| 1509 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1510 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1511 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1512 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1513 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1514 | * advertised payload is larger than the advertised packet |
| 1515 | * length, so we have garbage in the buffer between the |
| 1516 | * payload and the end of the buffer (p+len). We can't know |
| 1517 | * if the SSL stack is patched, and we don't know if we can |
| 1518 | * safely wipe out the area between p+3+len and payload. |
| 1519 | * So instead, we prevent the response from being sent by |
| 1520 | * setting the max_send_fragment to 0 and we report an SSL |
| 1521 | * error, which will kill this connection. It will be reported |
| 1522 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1523 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1524 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1525 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1526 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1527 | return; |
| 1528 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1529 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1530 | if (global_ssl.capture_cipherlist > 0) |
| 1531 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1532 | } |
| 1533 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1534 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 1535 | /* This callback is used so that the server advertises the list of |
| 1536 | * negociable protocols for NPN. |
| 1537 | */ |
| 1538 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1539 | unsigned int *len, void *arg) |
| 1540 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1541 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1542 | |
| 1543 | *data = (const unsigned char *)conf->npn_str; |
| 1544 | *len = conf->npn_len; |
| 1545 | return SSL_TLSEXT_ERR_OK; |
| 1546 | } |
| 1547 | #endif |
| 1548 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1549 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1550 | /* This callback is used so that the server advertises the list of |
| 1551 | * negociable protocols for ALPN. |
| 1552 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1553 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1554 | unsigned char *outlen, |
| 1555 | const unsigned char *server, |
| 1556 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1557 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1558 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1559 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1560 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1561 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1562 | return SSL_TLSEXT_ERR_NOACK; |
| 1563 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1564 | return SSL_TLSEXT_ERR_OK; |
| 1565 | } |
| 1566 | #endif |
| 1567 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1568 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1569 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1570 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1571 | /* Create a X509 certificate with the specified servername and serial. This |
| 1572 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1573 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1574 | 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] | 1575 | { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1576 | static unsigned int serial = 0; |
| 1577 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1578 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1579 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1580 | SSL_CTX *ssl_ctx = NULL; |
| 1581 | X509 *newcrt = NULL; |
| 1582 | EVP_PKEY *pkey = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1583 | X509_NAME *name; |
| 1584 | const EVP_MD *digest; |
| 1585 | X509V3_CTX ctx; |
| 1586 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1587 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1588 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1589 | /* Get the private key of the defautl certificate and use it */ |
| 1590 | if (!(pkey = SSL_get_privatekey(ssl))) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1591 | goto mkcert_error; |
| 1592 | |
| 1593 | /* Create the certificate */ |
| 1594 | if (!(newcrt = X509_new())) |
| 1595 | goto mkcert_error; |
| 1596 | |
| 1597 | /* Set version number for the certificate (X509v3) and the serial |
| 1598 | * number */ |
| 1599 | if (X509_set_version(newcrt, 2L) != 1) |
| 1600 | goto mkcert_error; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1601 | if (!serial) |
| 1602 | serial = now_ms; |
| 1603 | ASN1_INTEGER_set(X509_get_serialNumber(newcrt), serial++); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1604 | |
| 1605 | /* Set duration for the certificate */ |
| 1606 | if (!X509_gmtime_adj(X509_get_notBefore(newcrt), (long)-60*60*24) || |
| 1607 | !X509_gmtime_adj(X509_get_notAfter(newcrt),(long)60*60*24*365)) |
| 1608 | goto mkcert_error; |
| 1609 | |
| 1610 | /* set public key in the certificate */ |
| 1611 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1612 | goto mkcert_error; |
| 1613 | |
| 1614 | /* Set issuer name from the CA */ |
| 1615 | if (!(name = X509_get_subject_name(cacert))) |
| 1616 | goto mkcert_error; |
| 1617 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1618 | goto mkcert_error; |
| 1619 | |
| 1620 | /* Set the subject name using the same, but the CN */ |
| 1621 | name = X509_NAME_dup(name); |
| 1622 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1623 | (const unsigned char *)servername, |
| 1624 | -1, -1, 0) != 1) { |
| 1625 | X509_NAME_free(name); |
| 1626 | goto mkcert_error; |
| 1627 | } |
| 1628 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1629 | X509_NAME_free(name); |
| 1630 | goto mkcert_error; |
| 1631 | } |
| 1632 | X509_NAME_free(name); |
| 1633 | |
| 1634 | /* Add x509v3 extensions as specified */ |
| 1635 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1636 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1637 | X509_EXTENSION *ext; |
| 1638 | |
| 1639 | if (!(ext = X509V3_EXT_conf(NULL, &ctx, x509v3_ext_names[i], x509v3_ext_values[i]))) |
| 1640 | goto mkcert_error; |
| 1641 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1642 | X509_EXTENSION_free(ext); |
| 1643 | goto mkcert_error; |
| 1644 | } |
| 1645 | X509_EXTENSION_free(ext); |
| 1646 | } |
| 1647 | |
| 1648 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1649 | |
| 1650 | key_type = EVP_PKEY_base_id(capkey); |
| 1651 | |
| 1652 | if (key_type == EVP_PKEY_DSA) |
| 1653 | digest = EVP_sha1(); |
| 1654 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1655 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1656 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1657 | digest = EVP_sha256(); |
| 1658 | else { |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1659 | #if (OPENSSL_VERSION_NUMBER >= 0x1000000fL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1660 | int nid; |
| 1661 | |
| 1662 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1663 | goto mkcert_error; |
| 1664 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1665 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1666 | #else |
| 1667 | goto mkcert_error; |
| 1668 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1669 | } |
| 1670 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1671 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1672 | goto mkcert_error; |
| 1673 | |
| 1674 | /* Create and set the new SSL_CTX */ |
| 1675 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1676 | goto mkcert_error; |
| 1677 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1678 | goto mkcert_error; |
| 1679 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1680 | goto mkcert_error; |
| 1681 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1682 | goto mkcert_error; |
| 1683 | |
| 1684 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1685 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1686 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1687 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1688 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1689 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1690 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1691 | 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] | 1692 | EC_KEY *ecc; |
| 1693 | int nid; |
| 1694 | |
| 1695 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1696 | goto end; |
| 1697 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1698 | goto end; |
| 1699 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1700 | EC_KEY_free(ecc); |
| 1701 | } |
| 1702 | #endif |
| 1703 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1704 | return ssl_ctx; |
| 1705 | |
| 1706 | mkcert_error: |
| 1707 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1708 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1709 | return NULL; |
| 1710 | } |
| 1711 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1712 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1713 | 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] | 1714 | { |
| 1715 | struct bind_conf *bind_conf = objt_listener(conn->target)->bind_conf; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1716 | |
| 1717 | return ssl_sock_do_create_cert(servername, bind_conf, conn->xprt_ctx); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1718 | } |
| 1719 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1720 | /* Do a lookup for a certificate in the LRU cache used to store generated |
| 1721 | * certificates. */ |
| 1722 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1723 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1724 | { |
| 1725 | struct lru64 *lru = NULL; |
| 1726 | |
| 1727 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1728 | lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1729 | if (lru && lru->domain) |
| 1730 | return (SSL_CTX *)lru->data; |
| 1731 | } |
| 1732 | return NULL; |
| 1733 | } |
| 1734 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1735 | /* Set a certificate int the LRU cache used to store generated |
| 1736 | * certificate. Return 0 on success, otherwise -1 */ |
| 1737 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1738 | 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] | 1739 | { |
| 1740 | struct lru64 *lru = NULL; |
| 1741 | |
| 1742 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1743 | lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1744 | if (!lru) |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1745 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1746 | if (lru->domain && lru->data) |
| 1747 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1748 | lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1749 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1750 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1751 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1752 | } |
| 1753 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1754 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1755 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1756 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1757 | { |
| 1758 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 1759 | } |
| 1760 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1761 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 1762 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 1763 | */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1764 | static SSL_CTX * |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1765 | 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] | 1766 | { |
| 1767 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1768 | SSL_CTX *ssl_ctx = NULL; |
| 1769 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1770 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1771 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1772 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1773 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1774 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1775 | if (lru && lru->domain) |
| 1776 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1777 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1778 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1779 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1780 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1781 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1782 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1783 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1784 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1785 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 1786 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 1787 | SSL_CTX_free(ssl_ctx); |
| 1788 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1789 | return ssl_ctx; |
| 1790 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1791 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1792 | |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1793 | |
| 1794 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 1795 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 1796 | #endif |
| 1797 | |
| 1798 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 1799 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
| 1800 | #define SSL_renegotiate_pending(arg) 0 |
| 1801 | #endif |
| 1802 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 1803 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1804 | #endif |
| 1805 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 1806 | #define SSL_OP_NO_TICKET 0 |
| 1807 | #endif |
| 1808 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 1809 | #define SSL_OP_NO_COMPRESSION 0 |
| 1810 | #endif |
| 1811 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 1812 | #define SSL_OP_NO_TLSv1_1 0 |
| 1813 | #endif |
| 1814 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 1815 | #define SSL_OP_NO_TLSv1_2 0 |
| 1816 | #endif |
| 1817 | #ifndef SSL_OP_NO_TLSv1_3 /* dev */ |
| 1818 | #define SSL_OP_NO_TLSv1_3 0 |
| 1819 | #endif |
| 1820 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 1821 | #define SSL_OP_SINGLE_DH_USE 0 |
| 1822 | #endif |
| 1823 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 1824 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1825 | #endif |
| 1826 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 1827 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 1828 | #endif |
| 1829 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 1830 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 1831 | #endif |
| 1832 | |
| 1833 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1834 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 1835 | |
| 1836 | 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] | 1837 | { |
Christopher Faulet | 1e59fcc | 2017-06-08 22:18:52 +0200 | [diff] [blame] | 1838 | #if SSL_OP_NO_SSLv3 && !defined(OPENSSL_NO_SSL3_METHOD) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1839 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1840 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 1841 | #endif |
| 1842 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1843 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 1844 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1845 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 1846 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1847 | 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] | 1848 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1849 | 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] | 1850 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 1851 | #endif |
| 1852 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1853 | 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] | 1854 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1855 | 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] | 1856 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 1857 | #endif |
| 1858 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1859 | /* TLS 1.2 is the last supported version in this context. */ |
| 1860 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 1861 | /* Unusable in this context. */ |
| 1862 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 1863 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 1864 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 1865 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 1866 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1867 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1868 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 1869 | |
| 1870 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 1871 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1872 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 1873 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1874 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 1875 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 1876 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 1877 | } |
| 1878 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 1879 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1880 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 1881 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1882 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 1883 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 1884 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 1885 | } |
| 1886 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 1887 | 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] | 1888 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 1889 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1890 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 1891 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 1892 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 1893 | } |
| 1894 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 1895 | 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] | 1896 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 1897 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1898 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 1899 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 1900 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 1901 | } |
| 1902 | 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] | 1903 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1904 | 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] | 1905 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1906 | #endif |
| 1907 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1908 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 1909 | #if SSL_OP_NO_TLSv1_3 |
| 1910 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 1911 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1912 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1913 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1914 | #endif |
| 1915 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 1916 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1917 | |
| 1918 | static struct { |
| 1919 | int option; |
| 1920 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1921 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 1922 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1923 | const char *name; |
| 1924 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1925 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 1926 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 1927 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 1928 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 1929 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 1930 | {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] | 1931 | }; |
| 1932 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 1933 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 1934 | { |
| 1935 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 1936 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 1937 | SSL_set_SSL_CTX(ssl, ctx); |
| 1938 | } |
| 1939 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1940 | #ifdef OPENSSL_IS_BORINGSSL |
| 1941 | |
| 1942 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 1943 | { |
| 1944 | (void)al; /* shut gcc stupid warning */ |
| 1945 | (void)priv; |
| 1946 | |
| 1947 | if (!SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name)) |
| 1948 | return SSL_TLSEXT_ERR_NOACK; |
| 1949 | return SSL_TLSEXT_ERR_OK; |
| 1950 | } |
| 1951 | |
| 1952 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 1953 | { |
| 1954 | struct connection *conn; |
| 1955 | struct bind_conf *s; |
| 1956 | const uint8_t *extension_data; |
| 1957 | size_t extension_len; |
| 1958 | CBS extension, cipher_suites, server_name_list, host_name, sig_algs; |
| 1959 | const SSL_CIPHER *cipher; |
| 1960 | uint16_t cipher_suite; |
| 1961 | uint8_t name_type, hash, sign; |
| 1962 | int has_rsa = 0, has_ecdsa = 0, has_ecdsa_sig = 0; |
| 1963 | |
| 1964 | char *wildp = NULL; |
| 1965 | const uint8_t *servername; |
| 1966 | struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL; |
| 1967 | int i; |
| 1968 | |
| 1969 | conn = SSL_get_app_data(ctx->ssl); |
| 1970 | s = objt_listener(conn->target)->bind_conf; |
| 1971 | |
| 1972 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 1973 | &extension_data, &extension_len)) { |
| 1974 | CBS_init(&extension, extension_data, extension_len); |
| 1975 | |
| 1976 | if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) |
| 1977 | || !CBS_get_u8(&server_name_list, &name_type) |
| 1978 | /* Although the server_name extension was intended to be extensible to |
| 1979 | * new name types and multiple names, OpenSSL 1.0.x had a bug which meant |
| 1980 | * different name types will cause an error. Further, RFC 4366 originally |
| 1981 | * defined syntax inextensibly. RFC 6066 corrected this mistake, but |
| 1982 | * adding new name types is no longer feasible. |
| 1983 | * |
| 1984 | * Act as if the extensibility does not exist to simplify parsing. */ |
| 1985 | || !CBS_get_u16_length_prefixed(&server_name_list, &host_name) |
| 1986 | || CBS_len(&server_name_list) != 0 |
| 1987 | || CBS_len(&extension) != 0 |
| 1988 | || name_type != TLSEXT_NAMETYPE_host_name |
| 1989 | || CBS_len(&host_name) == 0 |
| 1990 | || CBS_len(&host_name) > TLSEXT_MAXLEN_host_name |
| 1991 | || CBS_contains_zero_byte(&host_name)) { |
| 1992 | goto abort; |
| 1993 | } |
| 1994 | } else { |
| 1995 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 1996 | if (!s->strict_sni) { |
| 1997 | ssl_sock_switchctx_set(ctx->ssl, s->default_ctx); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1998 | return 1; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 1999 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2000 | goto abort; |
| 2001 | } |
| 2002 | |
| 2003 | /* extract/check clientHello informations */ |
| 2004 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2005 | CBS_init(&extension, extension_data, extension_len); |
| 2006 | |
| 2007 | if (!CBS_get_u16_length_prefixed(&extension, &sig_algs) |
| 2008 | || CBS_len(&sig_algs) == 0 |
| 2009 | || CBS_len(&extension) != 0) { |
| 2010 | goto abort; |
| 2011 | } |
| 2012 | if (CBS_len(&sig_algs) % 2 != 0) { |
| 2013 | goto abort; |
| 2014 | } |
| 2015 | while (CBS_len(&sig_algs) != 0) { |
| 2016 | if (!CBS_get_u8(&sig_algs, &hash) |
| 2017 | || !CBS_get_u8(&sig_algs, &sign)) { |
| 2018 | goto abort; |
| 2019 | } |
| 2020 | switch (sign) { |
| 2021 | case TLSEXT_signature_rsa: |
| 2022 | has_rsa = 1; |
| 2023 | break; |
| 2024 | case TLSEXT_signature_ecdsa: |
| 2025 | has_ecdsa_sig = 1; |
| 2026 | break; |
| 2027 | default: |
| 2028 | continue; |
| 2029 | } |
| 2030 | if (has_ecdsa_sig && has_rsa) |
| 2031 | break; |
| 2032 | } |
| 2033 | } else { |
| 2034 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLS 1.2) */ |
| 2035 | has_rsa = 1; |
| 2036 | } |
| 2037 | if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */ |
| 2038 | CBS_init(&cipher_suites, ctx->cipher_suites, ctx->cipher_suites_len); |
| 2039 | |
| 2040 | while (CBS_len(&cipher_suites) != 0) { |
| 2041 | if (!CBS_get_u16(&cipher_suites, &cipher_suite)) { |
| 2042 | goto abort; |
| 2043 | } |
| 2044 | cipher = SSL_get_cipher_by_value(cipher_suite); |
| 2045 | if (cipher && SSL_CIPHER_is_ECDSA(cipher)) { |
| 2046 | has_ecdsa = 1; |
| 2047 | break; |
| 2048 | } |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | servername = CBS_data(&host_name); |
| 2053 | for (i = 0; i < trash.size && i < CBS_len(&host_name); i++) { |
| 2054 | trash.str[i] = tolower(servername[i]); |
| 2055 | if (!wildp && (trash.str[i] == '.')) |
| 2056 | wildp = &trash.str[i]; |
| 2057 | } |
| 2058 | trash.str[i] = 0; |
| 2059 | |
| 2060 | /* lookup in full qualified names */ |
| 2061 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 2062 | |
| 2063 | /* lookup a not neg filter */ |
| 2064 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2065 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2066 | switch(container_of(n, struct sni_ctx, name)->key_sig) { |
| 2067 | case TLSEXT_signature_ecdsa: |
| 2068 | if (has_ecdsa) { |
| 2069 | node_ecdsa = n; |
| 2070 | goto find_one; |
| 2071 | } |
| 2072 | break; |
| 2073 | case TLSEXT_signature_rsa: |
| 2074 | if (has_rsa && !node_rsa) { |
| 2075 | node_rsa = n; |
| 2076 | if (!has_ecdsa) |
| 2077 | goto find_one; |
| 2078 | } |
| 2079 | break; |
| 2080 | default: /* TLSEXT_signature_anonymous */ |
| 2081 | if (!node_anonymous) |
| 2082 | node_anonymous = n; |
| 2083 | break; |
| 2084 | } |
| 2085 | } |
| 2086 | } |
| 2087 | if (wildp) { |
| 2088 | /* lookup in wildcards names */ |
| 2089 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2090 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2091 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2092 | switch(container_of(n, struct sni_ctx, name)->key_sig) { |
| 2093 | case TLSEXT_signature_ecdsa: |
| 2094 | if (has_ecdsa) { |
| 2095 | node_ecdsa = n; |
| 2096 | goto find_one; |
| 2097 | } |
| 2098 | break; |
| 2099 | case TLSEXT_signature_rsa: |
| 2100 | if (has_rsa && !node_rsa) { |
| 2101 | node_rsa = n; |
| 2102 | if (!has_ecdsa) |
| 2103 | goto find_one; |
| 2104 | } |
| 2105 | break; |
| 2106 | default: /* TLSEXT_signature_anonymous */ |
| 2107 | if (!node_anonymous) |
| 2108 | node_anonymous = n; |
| 2109 | break; |
| 2110 | } |
| 2111 | } |
| 2112 | } |
| 2113 | } |
| 2114 | find_one: |
| 2115 | /* select by key_signature priority order */ |
| 2116 | node = node_ecdsa ? node_ecdsa : (node_rsa ? node_rsa : node_anonymous); |
| 2117 | |
| 2118 | if (node) { |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 2119 | int min, max; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2120 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2121 | ssl_sock_switchctx_set(ctx->ssl, container_of(node, struct sni_ctx, name)->ctx); |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 2122 | min = container_of(node, struct sni_ctx, name)->conf->ssl_methods.min; |
| 2123 | if (min != s->ssl_methods.min) |
| 2124 | methodVersions[min].ssl_set_version(ctx->ssl, SET_MIN); |
| 2125 | max = container_of(node, struct sni_ctx, name)->conf->ssl_methods.max; |
| 2126 | if (max != s->ssl_methods.max) |
| 2127 | methodVersions[max].ssl_set_version(ctx->ssl, SET_MAX); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2128 | return 1; |
| 2129 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2130 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2131 | /* no certificate match, is the default_ctx */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2132 | ssl_sock_switchctx_set(ctx->ssl, s->default_ctx); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2133 | return 1; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2134 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2135 | abort: |
| 2136 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2137 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 2138 | return -1; |
| 2139 | } |
| 2140 | |
| 2141 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2142 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2143 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2144 | * warning when no match is found, which implies the default (first) cert |
| 2145 | * will keep being used. |
| 2146 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2147 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2148 | { |
| 2149 | const char *servername; |
| 2150 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2151 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2152 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2153 | int i; |
| 2154 | (void)al; /* shut gcc stupid warning */ |
| 2155 | |
| 2156 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2157 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2158 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 2159 | if (s->generate_certs) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2160 | struct connection *conn = SSL_get_app_data(ssl); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2161 | unsigned int key; |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 2162 | SSL_CTX *ctx; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2163 | |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 2164 | conn_get_to_addr(conn); |
| 2165 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2166 | key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to)); |
| 2167 | ctx = ssl_sock_get_generated_cert(key, s); |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 2168 | if (ctx) { |
| 2169 | /* switch ctx */ |
| 2170 | SSL_set_SSL_CTX(ssl, ctx); |
| 2171 | return SSL_TLSEXT_ERR_OK; |
| 2172 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2173 | } |
| 2174 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2175 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2176 | if (s->strict_sni) |
| 2177 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2178 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2179 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2180 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2181 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2182 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2183 | if (!servername[i]) |
| 2184 | break; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2185 | trash.str[i] = tolower(servername[i]); |
| 2186 | if (!wildp && (trash.str[i] == '.')) |
| 2187 | wildp = &trash.str[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2188 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2189 | trash.str[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2190 | |
| 2191 | /* lookup in full qualified names */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2192 | node = ebst_lookup(&s->sni_ctx, trash.str); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2193 | |
| 2194 | /* lookup a not neg filter */ |
| 2195 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2196 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2197 | node = n; |
| 2198 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2199 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2200 | } |
| 2201 | if (!node && wildp) { |
| 2202 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2203 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2204 | } |
| 2205 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2206 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2207 | SSL_CTX *ctx; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2208 | if (s->generate_certs && |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2209 | (ctx = ssl_sock_generate_certificate(servername, s, ssl))) { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2210 | /* switch ctx */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2211 | return SSL_TLSEXT_ERR_OK; |
| 2212 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2213 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2214 | if (s->strict_sni) |
| 2215 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2216 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2217 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2221 | 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] | 2222 | return SSL_TLSEXT_ERR_OK; |
| 2223 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2224 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2225 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2226 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2227 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2228 | |
| 2229 | static DH * ssl_get_dh_1024(void) |
| 2230 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2231 | static unsigned char dh1024_p[]={ |
| 2232 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2233 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2234 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2235 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2236 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2237 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2238 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2239 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2240 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2241 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2242 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2243 | }; |
| 2244 | static unsigned char dh1024_g[]={ |
| 2245 | 0x02, |
| 2246 | }; |
| 2247 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2248 | BIGNUM *p; |
| 2249 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2250 | DH *dh = DH_new(); |
| 2251 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2252 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2253 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2254 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2255 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2256 | DH_free(dh); |
| 2257 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2258 | } else { |
| 2259 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2260 | } |
| 2261 | } |
| 2262 | return dh; |
| 2263 | } |
| 2264 | |
| 2265 | static DH *ssl_get_dh_2048(void) |
| 2266 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2267 | static unsigned char dh2048_p[]={ |
| 2268 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2269 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2270 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2271 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2272 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2273 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2274 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2275 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2276 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2277 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2278 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2279 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2280 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2281 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2282 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2283 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2284 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2285 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2286 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2287 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2288 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2289 | 0xB7,0x1F,0x77,0xF3, |
| 2290 | }; |
| 2291 | static unsigned char dh2048_g[]={ |
| 2292 | 0x02, |
| 2293 | }; |
| 2294 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2295 | BIGNUM *p; |
| 2296 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2297 | DH *dh = DH_new(); |
| 2298 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2299 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2300 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2301 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2302 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2303 | DH_free(dh); |
| 2304 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2305 | } else { |
| 2306 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2307 | } |
| 2308 | } |
| 2309 | return dh; |
| 2310 | } |
| 2311 | |
| 2312 | static DH *ssl_get_dh_4096(void) |
| 2313 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2314 | static unsigned char dh4096_p[]={ |
| 2315 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2316 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2317 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2318 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2319 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2320 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2321 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2322 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2323 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2324 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2325 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2326 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2327 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2328 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2329 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2330 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2331 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2332 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2333 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2334 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2335 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2336 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2337 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2338 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2339 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2340 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2341 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2342 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2343 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2344 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2345 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2346 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2347 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2348 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2349 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2350 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2351 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2352 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2353 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2354 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2355 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2356 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2357 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2358 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2359 | static unsigned char dh4096_g[]={ |
| 2360 | 0x02, |
| 2361 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2362 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2363 | BIGNUM *p; |
| 2364 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2365 | DH *dh = DH_new(); |
| 2366 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2367 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2368 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2369 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2370 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2371 | DH_free(dh); |
| 2372 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2373 | } else { |
| 2374 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2375 | } |
| 2376 | } |
| 2377 | return dh; |
| 2378 | } |
| 2379 | |
| 2380 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2381 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2382 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2383 | { |
| 2384 | DH *dh = NULL; |
| 2385 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2386 | int type; |
| 2387 | |
| 2388 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2389 | |
| 2390 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2391 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2392 | */ |
| 2393 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2394 | keylen = EVP_PKEY_bits(pkey); |
| 2395 | } |
| 2396 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2397 | if (keylen > global_ssl.default_dh_param) { |
| 2398 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2399 | } |
| 2400 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2401 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2402 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2403 | } |
| 2404 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2405 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2406 | } |
| 2407 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2408 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2409 | } |
| 2410 | |
| 2411 | return dh; |
| 2412 | } |
| 2413 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2414 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2415 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2416 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2417 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2418 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2419 | if (in == NULL) |
| 2420 | goto end; |
| 2421 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2422 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2423 | goto end; |
| 2424 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2425 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2426 | |
| 2427 | end: |
| 2428 | if (in) |
| 2429 | BIO_free(in); |
| 2430 | |
| 2431 | return dh; |
| 2432 | } |
| 2433 | |
| 2434 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2435 | { |
| 2436 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2437 | |
| 2438 | if (global_dh) { |
| 2439 | return 0; |
| 2440 | } |
| 2441 | |
| 2442 | return -1; |
| 2443 | } |
| 2444 | |
| 2445 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 2446 | if an error occured, and 0 if parameter not found. */ |
| 2447 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 2448 | { |
| 2449 | int ret = -1; |
| 2450 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 2451 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2452 | if (dh) { |
| 2453 | ret = 1; |
| 2454 | SSL_CTX_set_tmp_dh(ctx, dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2455 | |
| 2456 | if (ssl_dh_ptr_index >= 0) { |
| 2457 | /* store a pointer to the DH params to avoid complaining about |
| 2458 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2459 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2460 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2461 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2462 | else if (global_dh) { |
| 2463 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 2464 | ret = 0; /* DH params not found */ |
| 2465 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2466 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2467 | /* Clear openssl global errors stack */ |
| 2468 | ERR_clear_error(); |
| 2469 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2470 | if (global_ssl.default_dh_param <= 1024) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2471 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 2472 | if (local_dh_1024 == NULL) |
| 2473 | local_dh_1024 = ssl_get_dh_1024(); |
| 2474 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2475 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2476 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2477 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2478 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2479 | } |
| 2480 | else { |
| 2481 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2482 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2483 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2484 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2485 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2486 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2487 | end: |
| 2488 | if (dh) |
| 2489 | DH_free(dh); |
| 2490 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2491 | return ret; |
| 2492 | } |
| 2493 | #endif |
| 2494 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2495 | static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, struct ssl_bind_conf *conf, |
| 2496 | uint8_t key_sig, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2497 | { |
| 2498 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2499 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2500 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2501 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2502 | if (*name == '!') { |
| 2503 | neg = 1; |
| 2504 | name++; |
| 2505 | } |
| 2506 | if (*name == '*') { |
| 2507 | wild = 1; |
| 2508 | name++; |
| 2509 | } |
| 2510 | /* !* filter is a nop */ |
| 2511 | if (neg && wild) |
| 2512 | return order; |
| 2513 | if (*name) { |
| 2514 | int j, len; |
| 2515 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2516 | for (j = 0; j < len && j < trash.size; j++) |
| 2517 | trash.str[j] = tolower(name[j]); |
| 2518 | if (j >= trash.size) |
| 2519 | return order; |
| 2520 | trash.str[j] = 0; |
| 2521 | |
| 2522 | /* Check for duplicates. */ |
| 2523 | if (wild) |
| 2524 | node = ebst_lookup(&s->sni_w_ctx, trash.str); |
| 2525 | else |
| 2526 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 2527 | for (; node; node = ebmb_next_dup(node)) { |
| 2528 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2529 | if (sc->ctx == ctx && sc->conf == conf && |
| 2530 | sc->key_sig == key_sig && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2531 | return order; |
| 2532 | } |
| 2533 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2534 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2535 | if (!sc) |
| 2536 | return order; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2537 | memcpy(sc->name.key, trash.str, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2538 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2539 | sc->conf = conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2540 | sc->key_sig = key_sig; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2541 | sc->order = order++; |
| 2542 | sc->neg = neg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2543 | if (wild) |
| 2544 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 2545 | else |
| 2546 | ebst_insert(&s->sni_ctx, &sc->name); |
| 2547 | } |
| 2548 | return order; |
| 2549 | } |
| 2550 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2551 | |
| 2552 | /* The following code is used for loading multiple crt files into |
| 2553 | * SSL_CTX's based on CN/SAN |
| 2554 | */ |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 2555 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(LIBRESSL_VERSION_NUMBER) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2556 | /* This is used to preload the certifcate, private key |
| 2557 | * and Cert Chain of a file passed in via the crt |
| 2558 | * argument |
| 2559 | * |
| 2560 | * This way, we do not have to read the file multiple times |
| 2561 | */ |
| 2562 | struct cert_key_and_chain { |
| 2563 | X509 *cert; |
| 2564 | EVP_PKEY *key; |
| 2565 | unsigned int num_chain_certs; |
| 2566 | /* This is an array of X509 pointers */ |
| 2567 | X509 **chain_certs; |
| 2568 | }; |
| 2569 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2570 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 2571 | |
| 2572 | struct key_combo_ctx { |
| 2573 | SSL_CTX *ctx; |
| 2574 | int order; |
| 2575 | }; |
| 2576 | |
| 2577 | /* Map used for processing multiple keypairs for a single purpose |
| 2578 | * |
| 2579 | * This maps CN/SNI name to certificate type |
| 2580 | */ |
| 2581 | struct sni_keytype { |
| 2582 | int keytypes; /* BITMASK for keytypes */ |
| 2583 | struct ebmb_node name; /* node holding the servername value */ |
| 2584 | }; |
| 2585 | |
| 2586 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2587 | /* Frees the contents of a cert_key_and_chain |
| 2588 | */ |
| 2589 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2590 | { |
| 2591 | int i; |
| 2592 | |
| 2593 | if (!ckch) |
| 2594 | return; |
| 2595 | |
| 2596 | /* Free the certificate and set pointer to NULL */ |
| 2597 | if (ckch->cert) |
| 2598 | X509_free(ckch->cert); |
| 2599 | ckch->cert = NULL; |
| 2600 | |
| 2601 | /* Free the key and set pointer to NULL */ |
| 2602 | if (ckch->key) |
| 2603 | EVP_PKEY_free(ckch->key); |
| 2604 | ckch->key = NULL; |
| 2605 | |
| 2606 | /* Free each certificate in the chain */ |
| 2607 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2608 | if (ckch->chain_certs[i]) |
| 2609 | X509_free(ckch->chain_certs[i]); |
| 2610 | } |
| 2611 | |
| 2612 | /* Free the chain obj itself and set to NULL */ |
| 2613 | if (ckch->num_chain_certs > 0) { |
| 2614 | free(ckch->chain_certs); |
| 2615 | ckch->num_chain_certs = 0; |
| 2616 | ckch->chain_certs = NULL; |
| 2617 | } |
| 2618 | |
| 2619 | } |
| 2620 | |
| 2621 | /* checks if a key and cert exists in the ckch |
| 2622 | */ |
| 2623 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 2624 | { |
| 2625 | return (ckch->cert != NULL && ckch->key != NULL); |
| 2626 | } |
| 2627 | |
| 2628 | |
| 2629 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 2630 | * This allows us to carry the contents of the file without having to |
| 2631 | * read the file multiple times. |
| 2632 | * |
| 2633 | * returns: |
| 2634 | * 0 on Success |
| 2635 | * 1 on SSL Failure |
| 2636 | * 2 on file not found |
| 2637 | */ |
| 2638 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 2639 | { |
| 2640 | |
| 2641 | BIO *in; |
| 2642 | X509 *ca = NULL; |
| 2643 | int ret = 1; |
| 2644 | |
| 2645 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2646 | |
| 2647 | in = BIO_new(BIO_s_file()); |
| 2648 | if (in == NULL) |
| 2649 | goto end; |
| 2650 | |
| 2651 | if (BIO_read_filename(in, path) <= 0) |
| 2652 | goto end; |
| 2653 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2654 | /* Read Private Key */ |
| 2655 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 2656 | if (ckch->key == NULL) { |
| 2657 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 2658 | err && *err ? *err : "", path); |
| 2659 | goto end; |
| 2660 | } |
| 2661 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2662 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 2663 | if (BIO_reset(in) == -1) { |
| 2664 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 2665 | err && *err ? *err : "", path); |
| 2666 | goto end; |
| 2667 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2668 | |
| 2669 | /* Read Certificate */ |
| 2670 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 2671 | if (ckch->cert == NULL) { |
| 2672 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 2673 | err && *err ? *err : "", path); |
| 2674 | goto end; |
| 2675 | } |
| 2676 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2677 | /* Read Certificate Chain */ |
| 2678 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 2679 | /* Grow the chain certs */ |
| 2680 | ckch->num_chain_certs++; |
| 2681 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 2682 | |
| 2683 | /* use - 1 here since we just incremented it above */ |
| 2684 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 2685 | } |
| 2686 | ret = ERR_get_error(); |
| 2687 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 2688 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 2689 | err && *err ? *err : "", path); |
| 2690 | ret = 1; |
| 2691 | goto end; |
| 2692 | } |
| 2693 | |
| 2694 | ret = 0; |
| 2695 | |
| 2696 | end: |
| 2697 | |
| 2698 | ERR_clear_error(); |
| 2699 | if (in) |
| 2700 | BIO_free(in); |
| 2701 | |
| 2702 | /* Something went wrong in one of the reads */ |
| 2703 | if (ret != 0) |
| 2704 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2705 | |
| 2706 | return ret; |
| 2707 | } |
| 2708 | |
| 2709 | /* Loads the info in ckch into ctx |
| 2710 | * Currently, this does not process any information about ocsp, dhparams or |
| 2711 | * sctl |
| 2712 | * Returns |
| 2713 | * 0 on success |
| 2714 | * 1 on failure |
| 2715 | */ |
| 2716 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2717 | { |
| 2718 | int i = 0; |
| 2719 | |
| 2720 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2721 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2722 | err && *err ? *err : "", path); |
| 2723 | return 1; |
| 2724 | } |
| 2725 | |
| 2726 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 2727 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 2728 | err && *err ? *err : "", path); |
| 2729 | return 1; |
| 2730 | } |
| 2731 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2732 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 2733 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2734 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2735 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 2736 | err && *err ? *err : "", (i+1), path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2737 | return 1; |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2742 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2743 | err && *err ? *err : "", path); |
| 2744 | return 1; |
| 2745 | } |
| 2746 | |
| 2747 | return 0; |
| 2748 | } |
| 2749 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2750 | |
| 2751 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 2752 | { |
| 2753 | struct sni_keytype *s_kt = NULL; |
| 2754 | struct ebmb_node *node; |
| 2755 | int i; |
| 2756 | |
| 2757 | for (i = 0; i < trash.size; i++) { |
| 2758 | if (!str[i]) |
| 2759 | break; |
| 2760 | trash.str[i] = tolower(str[i]); |
| 2761 | } |
| 2762 | trash.str[i] = 0; |
| 2763 | node = ebst_lookup(sni_keytypes, trash.str); |
| 2764 | if (!node) { |
| 2765 | /* CN not found in tree */ |
| 2766 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 2767 | /* Using memcpy here instead of strncpy. |
| 2768 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 2769 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 2770 | */ |
| 2771 | memcpy(s_kt->name.key, trash.str, i+1); |
| 2772 | s_kt->keytypes = 0; |
| 2773 | ebst_insert(sni_keytypes, &s_kt->name); |
| 2774 | } else { |
| 2775 | /* CN found in tree */ |
| 2776 | s_kt = container_of(node, struct sni_keytype, name); |
| 2777 | } |
| 2778 | |
| 2779 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 2780 | s_kt->keytypes |= 1<<key_index; |
| 2781 | |
| 2782 | } |
| 2783 | |
| 2784 | |
| 2785 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 2786 | * If any are found, group these files into a set of SSL_CTX* |
| 2787 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 2788 | * |
| 2789 | * This will allow the user to explictly group multiple cert/keys for a single purpose |
| 2790 | * |
| 2791 | * Returns |
| 2792 | * 0 on success |
| 2793 | * 1 on failure |
| 2794 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2795 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2796 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2797 | { |
| 2798 | char fp[MAXPATHLEN+1] = {0}; |
| 2799 | int n = 0; |
| 2800 | int i = 0; |
| 2801 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 2802 | struct eb_root sni_keytypes_map = { {0} }; |
| 2803 | struct ebmb_node *node; |
| 2804 | struct ebmb_node *next; |
| 2805 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 2806 | * of keytypes |
| 2807 | */ |
| 2808 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 2809 | int rv = 0; |
| 2810 | X509_NAME *xname = NULL; |
| 2811 | char *str = NULL; |
| 2812 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2813 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 2814 | #endif |
| 2815 | |
| 2816 | /* Load all possible certs and keys */ |
| 2817 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2818 | struct stat buf; |
| 2819 | |
| 2820 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2821 | if (stat(fp, &buf) == 0) { |
| 2822 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
| 2823 | rv = 1; |
| 2824 | goto end; |
| 2825 | } |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | /* Process each ckch and update keytypes for each CN/SAN |
| 2830 | * for example, if CN/SAN www.a.com is associated with |
| 2831 | * certs with keytype 0 and 2, then at the end of the loop, |
| 2832 | * www.a.com will have: |
| 2833 | * keyindex = 0 | 1 | 4 = 5 |
| 2834 | */ |
| 2835 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2836 | |
| 2837 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 2838 | continue; |
| 2839 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2840 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 2841 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2842 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 2843 | } else { |
| 2844 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 2845 | * so the line that contains logic is marked via comments |
| 2846 | */ |
| 2847 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 2848 | i = -1; |
| 2849 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 2850 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2851 | ASN1_STRING *value; |
| 2852 | value = X509_NAME_ENTRY_get_data(entry); |
| 2853 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2854 | /* Important line is here */ |
| 2855 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2856 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2857 | OPENSSL_free(str); |
| 2858 | str = NULL; |
| 2859 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2860 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2861 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2862 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2863 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2864 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 2865 | if (names) { |
| 2866 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 2867 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2868 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2869 | if (name->type == GEN_DNS) { |
| 2870 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 2871 | /* Important line is here */ |
| 2872 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2873 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2874 | OPENSSL_free(str); |
| 2875 | str = NULL; |
| 2876 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | } |
| 2881 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2882 | } |
| 2883 | |
| 2884 | /* If no files found, return error */ |
| 2885 | if (eb_is_empty(&sni_keytypes_map)) { |
| 2886 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 2887 | err && *err ? *err : "", path); |
| 2888 | rv = 1; |
| 2889 | goto end; |
| 2890 | } |
| 2891 | |
| 2892 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 2893 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 2894 | * and add each CTX to the SNI tree |
| 2895 | * |
| 2896 | * Some math here: |
| 2897 | * There are 2^n - 1 possibile combinations, each unique |
| 2898 | * combination is denoted by the key in the map. Each key |
| 2899 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 2900 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 2901 | * entry in the array to correspond to the unique combo (key) |
| 2902 | * associated with i. This unique key combo (i) will be associated |
| 2903 | * with combos[i-1] |
| 2904 | */ |
| 2905 | |
| 2906 | node = ebmb_first(&sni_keytypes_map); |
| 2907 | while (node) { |
| 2908 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2909 | char cur_file[MAXPATHLEN+1]; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2910 | |
| 2911 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 2912 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 2913 | cur_ctx = key_combos[i-1].ctx; |
| 2914 | |
| 2915 | if (cur_ctx == NULL) { |
| 2916 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2917 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2918 | if (cur_ctx == NULL) { |
| 2919 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 2920 | err && *err ? *err : ""); |
| 2921 | rv = 1; |
| 2922 | goto end; |
| 2923 | } |
| 2924 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2925 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2926 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2927 | if (i & (1<<n)) { |
| 2928 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2929 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2930 | 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] | 2931 | SSL_CTX_free(cur_ctx); |
| 2932 | rv = 1; |
| 2933 | goto end; |
| 2934 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2935 | |
| 2936 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 2937 | /* Load OCSP Info into context */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2938 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2939 | if (err) |
| 2940 | 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] | 2941 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2942 | SSL_CTX_free(cur_ctx); |
| 2943 | rv = 1; |
| 2944 | goto end; |
| 2945 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 2946 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 2947 | ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2948 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | /* Load DH params into the ctx to support DHE keys */ |
| 2953 | #ifndef OPENSSL_NO_DH |
| 2954 | if (ssl_dh_ptr_index >= 0) |
| 2955 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 2956 | |
| 2957 | rv = ssl_sock_load_dh_params(cur_ctx, NULL); |
| 2958 | if (rv < 0) { |
| 2959 | if (err) |
| 2960 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 2961 | *err ? *err : "", path); |
| 2962 | rv = 1; |
| 2963 | goto end; |
| 2964 | } |
| 2965 | #endif |
| 2966 | |
| 2967 | /* Update key_combos */ |
| 2968 | key_combos[i-1].ctx = cur_ctx; |
| 2969 | } |
| 2970 | |
| 2971 | /* Update SNI Tree */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2972 | key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf, |
| 2973 | TLSEXT_signature_anonymous, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2974 | node = ebmb_next(node); |
| 2975 | } |
| 2976 | |
| 2977 | |
| 2978 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 2979 | if (!bind_conf->default_ctx) { |
| 2980 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 2981 | if (key_combos[i].ctx) { |
| 2982 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2983 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2984 | break; |
| 2985 | } |
| 2986 | } |
| 2987 | } |
| 2988 | |
| 2989 | end: |
| 2990 | |
| 2991 | if (names) |
| 2992 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 2993 | |
| 2994 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 2995 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 2996 | |
| 2997 | node = ebmb_first(&sni_keytypes_map); |
| 2998 | while (node) { |
| 2999 | next = ebmb_next(node); |
| 3000 | ebmb_delete(node); |
| 3001 | node = next; |
| 3002 | } |
| 3003 | |
| 3004 | return rv; |
| 3005 | } |
| 3006 | #else |
| 3007 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3008 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3009 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3010 | { |
| 3011 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3012 | err && *err ? *err : "", path, strerror(errno)); |
| 3013 | return 1; |
| 3014 | } |
| 3015 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3016 | #endif /* #if OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
| 3017 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3018 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 3019 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 3020 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3021 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, |
| 3022 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3023 | { |
| 3024 | BIO *in; |
| 3025 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3026 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3027 | int ret = -1; |
| 3028 | int order = 0; |
| 3029 | X509_NAME *xname; |
| 3030 | char *str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3031 | pem_password_cb *passwd_cb; |
| 3032 | void *passwd_cb_userdata; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3033 | EVP_PKEY *pkey; |
| 3034 | uint8_t key_sig = TLSEXT_signature_anonymous; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3035 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3036 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3037 | STACK_OF(GENERAL_NAME) *names; |
| 3038 | #endif |
| 3039 | |
| 3040 | in = BIO_new(BIO_s_file()); |
| 3041 | if (in == NULL) |
| 3042 | goto end; |
| 3043 | |
| 3044 | if (BIO_read_filename(in, file) <= 0) |
| 3045 | goto end; |
| 3046 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3047 | |
| 3048 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 3049 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 3050 | |
| 3051 | 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] | 3052 | if (x == NULL) |
| 3053 | goto end; |
| 3054 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3055 | pkey = X509_get_pubkey(x); |
| 3056 | if (pkey) { |
| 3057 | switch(EVP_PKEY_base_id(pkey)) { |
| 3058 | case EVP_PKEY_RSA: |
| 3059 | key_sig = TLSEXT_signature_rsa; |
| 3060 | break; |
| 3061 | case EVP_PKEY_EC: |
| 3062 | key_sig = TLSEXT_signature_ecdsa; |
| 3063 | break; |
| 3064 | } |
| 3065 | EVP_PKEY_free(pkey); |
| 3066 | } |
| 3067 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3068 | if (fcount) { |
| 3069 | while (fcount--) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3070 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, key_sig, sni_filter[fcount], order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3071 | } |
| 3072 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3073 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3074 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 3075 | if (names) { |
| 3076 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3077 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3078 | if (name->type == GEN_DNS) { |
| 3079 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3080 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, key_sig, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3081 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3082 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3083 | } |
| 3084 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3085 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3086 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3087 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3088 | xname = X509_get_subject_name(x); |
| 3089 | i = -1; |
| 3090 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3091 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3092 | ASN1_STRING *value; |
| 3093 | |
| 3094 | value = X509_NAME_ENTRY_get_data(entry); |
| 3095 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3096 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, key_sig, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3097 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3098 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 3103 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 3104 | goto end; |
| 3105 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3106 | #ifdef SSL_CTX_clear_extra_chain_certs |
| 3107 | SSL_CTX_clear_extra_chain_certs(ctx); |
| 3108 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3109 | if (ctx->extra_certs != NULL) { |
| 3110 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 3111 | ctx->extra_certs = NULL; |
| 3112 | } |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3113 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3114 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3115 | 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] | 3116 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3117 | X509_free(ca); |
| 3118 | goto end; |
| 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | err = ERR_get_error(); |
| 3123 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3124 | /* we successfully reached the last cert in the file */ |
| 3125 | ret = 1; |
| 3126 | } |
| 3127 | ERR_clear_error(); |
| 3128 | |
| 3129 | end: |
| 3130 | if (x) |
| 3131 | X509_free(x); |
| 3132 | |
| 3133 | if (in) |
| 3134 | BIO_free(in); |
| 3135 | |
| 3136 | return ret; |
| 3137 | } |
| 3138 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3139 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3140 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3141 | { |
| 3142 | int ret; |
| 3143 | SSL_CTX *ctx; |
| 3144 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3145 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3146 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3147 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3148 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3149 | return 1; |
| 3150 | } |
| 3151 | |
| 3152 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3153 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 3154 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3155 | SSL_CTX_free(ctx); |
| 3156 | return 1; |
| 3157 | } |
| 3158 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3159 | 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] | 3160 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3161 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 3162 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3163 | if (ret < 0) /* serious error, must do that ourselves */ |
| 3164 | SSL_CTX_free(ctx); |
| 3165 | return 1; |
| 3166 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 3167 | |
| 3168 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3169 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3170 | err && *err ? *err : "", path); |
| 3171 | return 1; |
| 3172 | } |
| 3173 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3174 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3175 | * the tree, so it will be discovered and cleaned in time. |
| 3176 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3177 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3178 | /* store a NULL pointer to indicate we have not yet loaded |
| 3179 | a custom DH param file */ |
| 3180 | if (ssl_dh_ptr_index >= 0) { |
| 3181 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3182 | } |
| 3183 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3184 | ret = ssl_sock_load_dh_params(ctx, path); |
| 3185 | if (ret < 0) { |
| 3186 | if (err) |
| 3187 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3188 | *err ? *err : "", path); |
| 3189 | return 1; |
| 3190 | } |
| 3191 | #endif |
| 3192 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 3193 | #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] | 3194 | ret = ssl_sock_load_ocsp(ctx, path); |
| 3195 | if (ret < 0) { |
| 3196 | if (err) |
| 3197 | 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", |
| 3198 | *err ? *err : "", path); |
| 3199 | return 1; |
| 3200 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3201 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3202 | ssl_sock_set_ocsp_response_from_file(ctx, path); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3203 | #endif |
| 3204 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 3205 | #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] | 3206 | if (sctl_ex_index >= 0) { |
| 3207 | ret = ssl_sock_load_sctl(ctx, path); |
| 3208 | if (ret < 0) { |
| 3209 | if (err) |
| 3210 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 3211 | *err ? *err : "", path); |
| 3212 | return 1; |
| 3213 | } |
| 3214 | } |
| 3215 | #endif |
| 3216 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3217 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3218 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3219 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3220 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3221 | return 1; |
| 3222 | } |
| 3223 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3224 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3225 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3226 | bind_conf->default_ssl_conf = ssl_conf; |
| 3227 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3228 | |
| 3229 | return 0; |
| 3230 | } |
| 3231 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3232 | 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] | 3233 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3234 | struct dirent **de_list; |
| 3235 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3236 | DIR *dir; |
| 3237 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 3238 | char *end; |
| 3239 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3240 | int cfgerr = 0; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3241 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3242 | int is_bundle; |
| 3243 | int j; |
| 3244 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3245 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3246 | if (stat(path, &buf) == 0) { |
| 3247 | dir = opendir(path); |
| 3248 | if (!dir) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3249 | 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] | 3250 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3251 | /* strip trailing slashes, including first one */ |
| 3252 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 3253 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3254 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3255 | n = scandir(path, &de_list, 0, alphasort); |
| 3256 | if (n < 0) { |
| 3257 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 3258 | err && *err ? *err : "", path, strerror(errno)); |
| 3259 | cfgerr++; |
| 3260 | } |
| 3261 | else { |
| 3262 | for (i = 0; i < n; i++) { |
| 3263 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 3264 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3265 | end = strrchr(de->d_name, '.'); |
| 3266 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 3267 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3268 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3269 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 3270 | if (stat(fp, &buf) != 0) { |
| 3271 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3272 | err && *err ? *err : "", fp, strerror(errno)); |
| 3273 | cfgerr++; |
| 3274 | goto ignore_entry; |
| 3275 | } |
| 3276 | if (!S_ISREG(buf.st_mode)) |
| 3277 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3278 | |
| 3279 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3280 | is_bundle = 0; |
| 3281 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 3282 | |
| 3283 | if (end) { |
| 3284 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 3285 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 3286 | is_bundle = 1; |
| 3287 | break; |
| 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | if (is_bundle) { |
| 3292 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 3293 | int dp_len; |
| 3294 | |
| 3295 | dp_len = end - de->d_name; |
| 3296 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 3297 | |
| 3298 | /* increment i and free de until we get to a non-bundle cert |
| 3299 | * Note here that we look at de_list[i + 1] before freeing de |
| 3300 | * this is important since ignore_entry will free de |
| 3301 | */ |
| 3302 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 3303 | free(de); |
| 3304 | i++; |
| 3305 | de = de_list[i]; |
| 3306 | } |
| 3307 | |
| 3308 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3309 | ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3310 | |
| 3311 | /* Successfully processed the bundle */ |
| 3312 | goto ignore_entry; |
| 3313 | } |
| 3314 | } |
| 3315 | |
| 3316 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3317 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3318 | ignore_entry: |
| 3319 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3320 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3321 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3322 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3323 | closedir(dir); |
| 3324 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3325 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3326 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3327 | cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3328 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3329 | return cfgerr; |
| 3330 | } |
| 3331 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3332 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3333 | * done once. Zero is returned if the operation fails. No error is returned |
| 3334 | * if the random is said as not implemented, because we expect that openssl |
| 3335 | * will use another method once needed. |
| 3336 | */ |
| 3337 | static int ssl_initialize_random() |
| 3338 | { |
| 3339 | unsigned char random; |
| 3340 | static int random_initialized = 0; |
| 3341 | |
| 3342 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3343 | random_initialized = 1; |
| 3344 | |
| 3345 | return random_initialized; |
| 3346 | } |
| 3347 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3348 | /* release ssl bind conf */ |
| 3349 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3350 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3351 | if (conf) { |
| 3352 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 3353 | free(conf->npn_str); |
| 3354 | conf->npn_str = NULL; |
| 3355 | #endif |
| 3356 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 3357 | free(conf->alpn_str); |
| 3358 | conf->alpn_str = NULL; |
| 3359 | #endif |
| 3360 | free(conf->ca_file); |
| 3361 | conf->ca_file = NULL; |
| 3362 | free(conf->crl_file); |
| 3363 | conf->crl_file = NULL; |
| 3364 | free(conf->ciphers); |
| 3365 | conf->ciphers = NULL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3366 | free(conf->curves); |
| 3367 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3368 | free(conf->ecdhe); |
| 3369 | conf->ecdhe = NULL; |
| 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 3374 | { |
| 3375 | char thisline[CRT_LINESIZE]; |
| 3376 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3377 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3378 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3379 | int linenum = 0; |
| 3380 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3381 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3382 | if ((f = fopen(file, "r")) == NULL) { |
| 3383 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3384 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3385 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3386 | |
| 3387 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3388 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3389 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3390 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3391 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3392 | char *crt_path; |
| 3393 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3394 | |
| 3395 | linenum++; |
| 3396 | end = line + strlen(line); |
| 3397 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 3398 | /* Check if we reached the limit and the last char is not \n. |
| 3399 | * Watch out for the last line without the terminating '\n'! |
| 3400 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3401 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 3402 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3403 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3404 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3405 | } |
| 3406 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3407 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3408 | newarg = 1; |
| 3409 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3410 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 3411 | /* end of string, end of loop */ |
| 3412 | *line = 0; |
| 3413 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3414 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3415 | newarg = 1; |
| 3416 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3417 | } else if (*line == '[') { |
| 3418 | if (ssl_b) { |
| 3419 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 3420 | cfgerr = 1; |
| 3421 | break; |
| 3422 | } |
| 3423 | if (!arg) { |
| 3424 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 3425 | cfgerr = 1; |
| 3426 | break; |
| 3427 | } |
| 3428 | ssl_b = arg; |
| 3429 | newarg = 1; |
| 3430 | *line = 0; |
| 3431 | } else if (*line == ']') { |
| 3432 | if (ssl_e) { |
| 3433 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3434 | cfgerr = 1; |
| 3435 | break; |
| 3436 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3437 | if (!ssl_b) { |
| 3438 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 3439 | cfgerr = 1; |
| 3440 | break; |
| 3441 | } |
| 3442 | ssl_e = arg; |
| 3443 | newarg = 1; |
| 3444 | *line = 0; |
| 3445 | } else if (newarg) { |
| 3446 | if (arg == MAX_CRT_ARGS) { |
| 3447 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 3448 | cfgerr = 1; |
| 3449 | break; |
| 3450 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3451 | newarg = 0; |
| 3452 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3453 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3454 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3455 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3456 | if (cfgerr) |
| 3457 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3458 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3459 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3460 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3461 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3462 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3463 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3464 | crt_path = args[0]; |
| 3465 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 3466 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 3467 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 3468 | crt_path, linenum, file); |
| 3469 | cfgerr = 1; |
| 3470 | break; |
| 3471 | } |
| 3472 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 3473 | crt_path = path; |
| 3474 | } |
| 3475 | |
| 3476 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 3477 | cur_arg = ssl_b ? ssl_b : 1; |
| 3478 | while (cur_arg < ssl_e) { |
| 3479 | newarg = 0; |
| 3480 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 3481 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 3482 | newarg = 1; |
| 3483 | cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
| 3484 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 3485 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 3486 | args[cur_arg], linenum, file); |
| 3487 | cfgerr = 1; |
| 3488 | } |
| 3489 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 3490 | break; |
| 3491 | } |
| 3492 | } |
| 3493 | if (!cfgerr && !newarg) { |
| 3494 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 3495 | args[cur_arg], linenum, file); |
| 3496 | cfgerr = 1; |
| 3497 | break; |
| 3498 | } |
| 3499 | } |
| 3500 | if (cfgerr) { |
| 3501 | ssl_sock_free_ssl_conf(ssl_conf); |
| 3502 | free(ssl_conf); |
| 3503 | ssl_conf = NULL; |
| 3504 | break; |
| 3505 | } |
| 3506 | |
| 3507 | if (stat(crt_path, &buf) == 0) { |
| 3508 | cfgerr = ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf, |
| 3509 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3510 | } else { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3511 | cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf, |
| 3512 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3513 | } |
| 3514 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3515 | if (cfgerr) { |
| 3516 | 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] | 3517 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3518 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3519 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3520 | fclose(f); |
| 3521 | return cfgerr; |
| 3522 | } |
| 3523 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3524 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3525 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3526 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3527 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3528 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3529 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3530 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3531 | SSL_OP_NO_SSLv2 | |
| 3532 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3533 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3534 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3535 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
| 3536 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3537 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3538 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3539 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3540 | SSL_MODE_RELEASE_BUFFERS | |
| 3541 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3542 | struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3543 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3544 | int flags = MC_SSL_O_ALL; |
| 3545 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3546 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3547 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3548 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3549 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3550 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
| 3551 | Warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3552 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3553 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3554 | else |
| 3555 | flags = conf_ssl_methods->flags; |
| 3556 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3557 | min = conf_ssl_methods->min; |
| 3558 | max = conf_ssl_methods->max; |
| 3559 | /* start with TLSv10 to remove SSLv3 per default */ |
| 3560 | if (!min && (!max || max >= CONF_TLSV10)) |
| 3561 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3562 | /* 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] | 3563 | if (min) |
| 3564 | flags |= (methodVersions[min].flag - 1); |
| 3565 | if (max) |
| 3566 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3567 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3568 | min = max = CONF_TLSV_NONE; |
| 3569 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3570 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3571 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3572 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3573 | if (min) { |
| 3574 | if (hole) { |
| 3575 | Warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 3576 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3577 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3578 | methodVersions[hole].name); |
| 3579 | hole = 0; |
| 3580 | } |
| 3581 | max = i; |
| 3582 | } |
| 3583 | else { |
| 3584 | min = max = i; |
| 3585 | } |
| 3586 | } |
| 3587 | else { |
| 3588 | if (min) |
| 3589 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3590 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3591 | if (!min) { |
| 3592 | Alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3593 | 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] | 3594 | cfgerr += 1; |
| 3595 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3596 | /* save real min/max in bind_conf */ |
| 3597 | conf_ssl_methods->min = min; |
| 3598 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3599 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3600 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) && !defined(OPENSSL_IS_BORINGSSL) |
| 3601 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
| 3602 | precautionary measure to avoid any suprise with older openssl version. */ |
| 3603 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3604 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3605 | else |
| 3606 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3607 | if (flags & methodVersions[i].flag) |
| 3608 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3609 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3610 | /* 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] | 3611 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3612 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3613 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3614 | |
| 3615 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3616 | options |= SSL_OP_NO_TICKET; |
| 3617 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3618 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 3619 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3620 | |
| 3621 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 3622 | if (global_ssl.async) |
| 3623 | mode |= SSL_MODE_ASYNC; |
| 3624 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3625 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3626 | if (global_ssl.life_time) |
| 3627 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3628 | |
| 3629 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3630 | #ifdef OPENSSL_IS_BORINGSSL |
| 3631 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3632 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 3633 | #else |
| 3634 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 3635 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 3636 | #endif |
| 3637 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3638 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
| 3642 | { |
| 3643 | struct proxy *curproxy = bind_conf->frontend; |
| 3644 | int cfgerr = 0; |
| 3645 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3646 | struct ssl_bind_conf *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3647 | const char *conf_ciphers; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3648 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3649 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3650 | if (ssl_conf) { |
| 3651 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 3652 | int i, min, max; |
| 3653 | int flags = MC_SSL_O_ALL; |
| 3654 | |
| 3655 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 3656 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_methods.min; |
| 3657 | max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_methods.max; |
| 3658 | if (min) |
| 3659 | flags |= (methodVersions[min].flag - 1); |
| 3660 | if (max) |
| 3661 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 3662 | min = max = CONF_TLSV_NONE; |
| 3663 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3664 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 3665 | if (min) |
| 3666 | max = i; |
| 3667 | else |
| 3668 | min = max = i; |
| 3669 | } |
| 3670 | /* save real min/max */ |
| 3671 | conf_ssl_methods->min = min; |
| 3672 | conf_ssl_methods->max = max; |
| 3673 | if (!min) { |
| 3674 | Alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3675 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3676 | cfgerr += 1; |
| 3677 | } |
| 3678 | } |
| 3679 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3680 | 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] | 3681 | case SSL_SOCK_VERIFY_NONE: |
| 3682 | verify = SSL_VERIFY_NONE; |
| 3683 | break; |
| 3684 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 3685 | verify = SSL_VERIFY_PEER; |
| 3686 | break; |
| 3687 | case SSL_SOCK_VERIFY_REQUIRED: |
| 3688 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 3689 | break; |
| 3690 | } |
| 3691 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 3692 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3693 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 3694 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 3695 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3696 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3697 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3698 | Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3699 | 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] | 3700 | cfgerr++; |
| 3701 | } |
| 3702 | /* set CA names fo client cert request, function returns void */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3703 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file)); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3704 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3705 | else { |
| 3706 | Alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 3707 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3708 | cfgerr++; |
| 3709 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 3710 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3711 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3712 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 3713 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3714 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3715 | Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3716 | 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] | 3717 | cfgerr++; |
| 3718 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 3719 | else { |
| 3720 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 3721 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3722 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 3723 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3724 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3725 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 3726 | #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] | 3727 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 3728 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
| 3729 | Alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 3730 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3731 | cfgerr++; |
| 3732 | } |
| 3733 | } |
| 3734 | #endif |
| 3735 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3736 | shared_context_set_cache(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3737 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 3738 | if (conf_ciphers && |
| 3739 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3740 | Alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3741 | 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] | 3742 | cfgerr++; |
| 3743 | } |
| 3744 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 3745 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3746 | /* If tune.ssl.default-dh-param has not been set, |
| 3747 | neither has ssl-default-dh-file and no static DH |
| 3748 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3749 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3750 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3751 | (ssl_dh_ptr_index == -1 || |
| 3752 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 3753 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 3754 | const SSL_CIPHER * cipher = NULL; |
| 3755 | char cipher_description[128]; |
| 3756 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 3757 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 3758 | which is not ephemeral DH. */ |
| 3759 | const char dhe_description[] = " Kx=DH "; |
| 3760 | const char dhe_export_description[] = " Kx=DH("; |
| 3761 | int idx = 0; |
| 3762 | int dhe_found = 0; |
| 3763 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 3764 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3765 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3766 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3767 | if (ssl) { |
| 3768 | ciphers = SSL_get_ciphers(ssl); |
| 3769 | |
| 3770 | if (ciphers) { |
| 3771 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 3772 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 3773 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 3774 | if (strstr(cipher_description, dhe_description) != NULL || |
| 3775 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 3776 | dhe_found = 1; |
| 3777 | break; |
| 3778 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 3779 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3780 | } |
| 3781 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3782 | SSL_free(ssl); |
| 3783 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 3784 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3785 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 3786 | if (dhe_found) { |
| 3787 | 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] | 3788 | } |
| 3789 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3790 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3791 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3792 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3793 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3794 | if (local_dh_1024 == NULL) { |
| 3795 | local_dh_1024 = ssl_get_dh_1024(); |
| 3796 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3797 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3798 | if (local_dh_2048 == NULL) { |
| 3799 | local_dh_2048 = ssl_get_dh_2048(); |
| 3800 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3801 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3802 | if (local_dh_4096 == NULL) { |
| 3803 | local_dh_4096 = ssl_get_dh_4096(); |
| 3804 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3805 | } |
| 3806 | } |
| 3807 | } |
| 3808 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3809 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3810 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 3811 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3812 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 3813 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3814 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3815 | #ifdef OPENSSL_NPN_NEGOTIATED |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3816 | ssl_conf_cur = NULL; |
| 3817 | if (ssl_conf && ssl_conf->npn_str) |
| 3818 | ssl_conf_cur = ssl_conf; |
| 3819 | else if (bind_conf->ssl_conf.npn_str) |
| 3820 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 3821 | if (ssl_conf_cur) |
| 3822 | 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] | 3823 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 3824 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3825 | ssl_conf_cur = NULL; |
| 3826 | if (ssl_conf && ssl_conf->alpn_str) |
| 3827 | ssl_conf_cur = ssl_conf; |
| 3828 | else if (bind_conf->ssl_conf.alpn_str) |
| 3829 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 3830 | if (ssl_conf_cur) |
| 3831 | 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] | 3832 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3833 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3834 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 3835 | if (conf_curves) { |
| 3836 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
| 3837 | Alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 3838 | curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3839 | cfgerr++; |
| 3840 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 3841 | #if defined(SSL_CTX_set_ecdh_auto) |
| 3842 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
| 3843 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3844 | } |
| 3845 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3846 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3847 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3848 | int i; |
| 3849 | EC_KEY *ecdh; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3850 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 3851 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3852 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3853 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3854 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
| 3855 | Alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3856 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3857 | cfgerr++; |
| 3858 | } |
| 3859 | else { |
| 3860 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 3861 | EC_KEY_free(ecdh); |
| 3862 | } |
| 3863 | } |
| 3864 | #endif |
| 3865 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3866 | return cfgerr; |
| 3867 | } |
| 3868 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3869 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 3870 | { |
| 3871 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 3872 | size_t prefixlen, suffixlen; |
| 3873 | |
| 3874 | /* Trivial case */ |
| 3875 | if (strcmp(pattern, hostname) == 0) |
| 3876 | return 1; |
| 3877 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3878 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 3879 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 3880 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 3881 | pattern_wildcard = NULL; |
| 3882 | pattern_left_label_end = pattern; |
| 3883 | while (*pattern_left_label_end != '.') { |
| 3884 | switch (*pattern_left_label_end) { |
| 3885 | case 0: |
| 3886 | /* End of label not found */ |
| 3887 | return 0; |
| 3888 | case '*': |
| 3889 | /* If there is more than one wildcards */ |
| 3890 | if (pattern_wildcard) |
| 3891 | return 0; |
| 3892 | pattern_wildcard = pattern_left_label_end; |
| 3893 | break; |
| 3894 | } |
| 3895 | pattern_left_label_end++; |
| 3896 | } |
| 3897 | |
| 3898 | /* If it's not trivial and there is no wildcard, it can't |
| 3899 | * match */ |
| 3900 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3901 | return 0; |
| 3902 | |
| 3903 | /* Make sure all labels match except the leftmost */ |
| 3904 | hostname_left_label_end = strchr(hostname, '.'); |
| 3905 | if (!hostname_left_label_end |
| 3906 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 3907 | return 0; |
| 3908 | |
| 3909 | /* Make sure the leftmost label of the hostname is long enough |
| 3910 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 3911 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3912 | return 0; |
| 3913 | |
| 3914 | /* Finally compare the string on either side of the |
| 3915 | * wildcard */ |
| 3916 | prefixlen = pattern_wildcard - pattern; |
| 3917 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 3918 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 3919 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3920 | return 0; |
| 3921 | |
| 3922 | return 1; |
| 3923 | } |
| 3924 | |
| 3925 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 3926 | { |
| 3927 | SSL *ssl; |
| 3928 | struct connection *conn; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 3929 | const char *servername; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3930 | |
| 3931 | int depth; |
| 3932 | X509 *cert; |
| 3933 | STACK_OF(GENERAL_NAME) *alt_names; |
| 3934 | int i; |
| 3935 | X509_NAME *cert_subject; |
| 3936 | char *str; |
| 3937 | |
| 3938 | if (ok == 0) |
| 3939 | return ok; |
| 3940 | |
| 3941 | ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3942 | conn = SSL_get_app_data(ssl); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3943 | |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 3944 | /* we're checking against the configured "verifyhost" directive if |
| 3945 | * present, or against the SNI used on this connection if present. |
| 3946 | * If neither is set, the verification is OK. |
| 3947 | */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3948 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 3949 | if (!servername) { |
| 3950 | SSL_SESSION *ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 3951 | if (!ssl_sess) |
| 3952 | return ok; |
| 3953 | |
| 3954 | servername = SSL_SESSION_get0_hostname(ssl_sess); |
| 3955 | if (!servername) |
| 3956 | return ok; |
| 3957 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3958 | |
| 3959 | /* We only need to verify the CN on the actual server cert, |
| 3960 | * not the indirect CAs */ |
| 3961 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 3962 | if (depth != 0) |
| 3963 | return ok; |
| 3964 | |
| 3965 | /* At this point, the cert is *not* OK unless we can find a |
| 3966 | * hostname match */ |
| 3967 | ok = 0; |
| 3968 | |
| 3969 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 3970 | /* It seems like this might happen if verify peer isn't set */ |
| 3971 | if (!cert) |
| 3972 | return ok; |
| 3973 | |
| 3974 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 3975 | if (alt_names) { |
| 3976 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 3977 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 3978 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 3979 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 3980 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 3981 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3982 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 3983 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3984 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 3985 | OPENSSL_free(str); |
| 3986 | } |
| 3987 | } |
| 3988 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 3989 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | cert_subject = X509_get_subject_name(cert); |
| 3993 | i = -1; |
| 3994 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 3995 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3996 | ASN1_STRING *value; |
| 3997 | value = X509_NAME_ENTRY_get_data(entry); |
| 3998 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3999 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4000 | OPENSSL_free(str); |
| 4001 | } |
| 4002 | } |
| 4003 | |
| 4004 | return ok; |
| 4005 | } |
| 4006 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4007 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4008 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4009 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4010 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4011 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4012 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4013 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4014 | SSL_OP_NO_SSLv2 | |
| 4015 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4016 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4017 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4018 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4019 | SSL_MODE_RELEASE_BUFFERS | |
| 4020 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4021 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4022 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4023 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4024 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4025 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4026 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4027 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4028 | if (!ssl_initialize_random()) { |
| 4029 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 4030 | cfgerr++; |
| 4031 | } |
| 4032 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4033 | /* Automatic memory computations need to know we use SSL there */ |
| 4034 | global.ssl_used_backend = 1; |
| 4035 | |
| 4036 | /* Initiate SSL context for current server */ |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4037 | srv->ssl_ctx.reused_sess = NULL; |
| 4038 | if (srv->use_ssl) |
| 4039 | srv->xprt = &ssl_sock; |
| 4040 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 4041 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4042 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4043 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4044 | if (!ctx) { |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4045 | Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 4046 | proxy_type_str(curproxy), curproxy->id, |
| 4047 | srv->id); |
| 4048 | cfgerr++; |
| 4049 | return cfgerr; |
| 4050 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4051 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4052 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
| 4053 | Warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 4054 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4055 | proxy_type_str(curproxy), curproxy->id, srv->id); |
| 4056 | else |
| 4057 | flags = conf_ssl_methods->flags; |
| 4058 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4059 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 4060 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4061 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4062 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4063 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4064 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4065 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4066 | min = max = CONF_TLSV_NONE; |
| 4067 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4068 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4069 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4070 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4071 | if (min) { |
| 4072 | if (hole) { |
| 4073 | Warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 4074 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4075 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4076 | methodVersions[hole].name); |
| 4077 | hole = 0; |
| 4078 | } |
| 4079 | max = i; |
| 4080 | } |
| 4081 | else { |
| 4082 | min = max = i; |
| 4083 | } |
| 4084 | } |
| 4085 | else { |
| 4086 | if (min) |
| 4087 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4088 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4089 | if (!min) { |
| 4090 | Alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4091 | proxy_type_str(curproxy), curproxy->id, srv->id); |
| 4092 | cfgerr += 1; |
| 4093 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4094 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4095 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) && !defined(OPENSSL_IS_BORINGSSL) |
| 4096 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
| 4097 | precautionary measure to avoid any suprise with older openssl version. */ |
| 4098 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4099 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4100 | else |
| 4101 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4102 | if (flags & methodVersions[i].flag) |
| 4103 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4104 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4105 | /* 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] | 4106 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4107 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4108 | #endif |
| 4109 | |
| 4110 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4111 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4112 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4113 | |
| 4114 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 4115 | if (global_ssl.async) |
| 4116 | mode |= SSL_MODE_ASYNC; |
| 4117 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4118 | SSL_CTX_set_mode(ctx, mode); |
| 4119 | srv->ssl_ctx.ctx = ctx; |
| 4120 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4121 | if (srv->ssl_ctx.client_crt) { |
| 4122 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
| 4123 | Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4124 | proxy_type_str(curproxy), curproxy->id, |
| 4125 | srv->id, srv->ssl_ctx.client_crt); |
| 4126 | cfgerr++; |
| 4127 | } |
| 4128 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
| 4129 | Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4130 | proxy_type_str(curproxy), curproxy->id, |
| 4131 | srv->id, srv->ssl_ctx.client_crt); |
| 4132 | cfgerr++; |
| 4133 | } |
| 4134 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
| 4135 | Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4136 | proxy_type_str(curproxy), curproxy->id, |
| 4137 | srv->id, srv->ssl_ctx.client_crt); |
| 4138 | cfgerr++; |
| 4139 | } |
| 4140 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4141 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4142 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4143 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4144 | switch (srv->ssl_ctx.verify) { |
| 4145 | case SSL_SOCK_VERIFY_NONE: |
| 4146 | verify = SSL_VERIFY_NONE; |
| 4147 | break; |
| 4148 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4149 | verify = SSL_VERIFY_PEER; |
| 4150 | break; |
| 4151 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4152 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4153 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4154 | (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] | 4155 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4156 | if (srv->ssl_ctx.ca_file) { |
| 4157 | /* load CAfile to verify */ |
| 4158 | if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) { |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 4159 | Alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n", |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4160 | curproxy->id, srv->id, |
| 4161 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
| 4162 | cfgerr++; |
| 4163 | } |
| 4164 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4165 | else { |
| 4166 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 4167 | 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", |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4168 | curproxy->id, srv->id, |
| 4169 | srv->conf.file, srv->conf.line); |
| 4170 | else |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 4171 | Alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4172 | curproxy->id, srv->id, |
| 4173 | srv->conf.file, srv->conf.line); |
| 4174 | cfgerr++; |
| 4175 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4176 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4177 | if (srv->ssl_ctx.crl_file) { |
| 4178 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4179 | |
| 4180 | if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) { |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 4181 | Alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4182 | curproxy->id, srv->id, |
| 4183 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
| 4184 | cfgerr++; |
| 4185 | } |
| 4186 | else { |
| 4187 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4188 | } |
| 4189 | } |
| 4190 | #endif |
| 4191 | } |
| 4192 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4193 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF); |
| 4194 | if (srv->ssl_ctx.ciphers && |
| 4195 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
| 4196 | Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4197 | curproxy->id, srv->id, |
| 4198 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
| 4199 | cfgerr++; |
| 4200 | } |
| 4201 | |
| 4202 | return cfgerr; |
| 4203 | } |
| 4204 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4205 | /* 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] | 4206 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4207 | * encountered. |
| 4208 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4209 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4210 | { |
| 4211 | struct ebmb_node *node; |
| 4212 | struct sni_ctx *sni; |
| 4213 | int err = 0; |
| 4214 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4215 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4216 | return 0; |
| 4217 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4218 | /* Automatic memory computations need to know we use SSL there */ |
| 4219 | global.ssl_used_frontend = 1; |
| 4220 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4221 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4222 | if (!ssl_initialize_random()) { |
| 4223 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 4224 | err++; |
| 4225 | } |
| 4226 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4227 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4228 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4229 | /* It should not be necessary to call this function, but it's |
| 4230 | necessary first to check and move all initialisation related |
| 4231 | to initial_ctx in ssl_sock_initial_ctx. */ |
| 4232 | err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx); |
| 4233 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4234 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4235 | 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] | 4236 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4237 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4238 | while (node) { |
| 4239 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4240 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4241 | /* only initialize the CTX on its first occurrence and |
| 4242 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4243 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4244 | node = ebmb_next(node); |
| 4245 | } |
| 4246 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4247 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4248 | while (node) { |
| 4249 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4250 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4251 | /* only initialize the CTX on its first occurrence and |
| 4252 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4253 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4254 | node = ebmb_next(node); |
| 4255 | } |
| 4256 | return err; |
| 4257 | } |
| 4258 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4259 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4260 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4261 | * alerts are directly emitted since the rest of the stack does it below. |
| 4262 | */ |
| 4263 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4264 | { |
| 4265 | struct proxy *px = bind_conf->frontend; |
| 4266 | int alloc_ctx; |
| 4267 | int err; |
| 4268 | |
| 4269 | if (!bind_conf->is_ssl) { |
| 4270 | if (bind_conf->default_ctx) { |
| 4271 | Warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 4272 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4273 | } |
| 4274 | return 0; |
| 4275 | } |
| 4276 | if (!bind_conf->default_ctx) { |
| 4277 | Alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 4278 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4279 | return -1; |
| 4280 | } |
| 4281 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4282 | alloc_ctx = shared_context_init(global.tune.sslcachesize, (!global_ssl.private_cache && (global.nbproc > 1)) ? 1 : 0); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4283 | if (alloc_ctx < 0) { |
| 4284 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 4285 | 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"); |
| 4286 | else |
| 4287 | Alert("Unable to allocate SSL session cache.\n"); |
| 4288 | return -1; |
| 4289 | } |
| 4290 | |
| 4291 | err = 0; |
| 4292 | /* initialize all certificate contexts */ |
| 4293 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 4294 | |
| 4295 | /* initialize CA variables if the certificates generation is enabled */ |
| 4296 | err += ssl_sock_load_ca(bind_conf); |
| 4297 | |
| 4298 | return -err; |
| 4299 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4300 | |
| 4301 | /* release ssl context allocated for servers. */ |
| 4302 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 4303 | { |
| 4304 | if (srv->ssl_ctx.ctx) |
| 4305 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 4306 | } |
| 4307 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4308 | /* 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] | 4309 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 4310 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4311 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4312 | { |
| 4313 | struct ebmb_node *node, *back; |
| 4314 | struct sni_ctx *sni; |
| 4315 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4316 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4317 | return; |
| 4318 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4319 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4320 | while (node) { |
| 4321 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4322 | back = ebmb_next(node); |
| 4323 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4324 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4325 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4326 | ssl_sock_free_ssl_conf(sni->conf); |
| 4327 | free(sni->conf); |
| 4328 | sni->conf = NULL; |
| 4329 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4330 | free(sni); |
| 4331 | node = back; |
| 4332 | } |
| 4333 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4334 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4335 | while (node) { |
| 4336 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4337 | back = ebmb_next(node); |
| 4338 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4339 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4340 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4341 | ssl_sock_free_ssl_conf(sni->conf); |
| 4342 | free(sni->conf); |
| 4343 | sni->conf = NULL; |
| 4344 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4345 | free(sni); |
| 4346 | node = back; |
| 4347 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4348 | SSL_CTX_free(bind_conf->initial_ctx); |
| 4349 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4350 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4351 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4352 | } |
| 4353 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4354 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 4355 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 4356 | { |
| 4357 | ssl_sock_free_ca(bind_conf); |
| 4358 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4359 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4360 | free(bind_conf->ca_sign_file); |
| 4361 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4362 | if (bind_conf->keys_ref) { |
| 4363 | free(bind_conf->keys_ref->filename); |
| 4364 | free(bind_conf->keys_ref->tlskeys); |
| 4365 | LIST_DEL(&bind_conf->keys_ref->list); |
| 4366 | free(bind_conf->keys_ref); |
| 4367 | } |
| 4368 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4369 | bind_conf->ca_sign_pass = NULL; |
| 4370 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4371 | } |
| 4372 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4373 | /* Load CA cert file and private key used to generate certificates */ |
| 4374 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4375 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4376 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4377 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4378 | FILE *fp; |
| 4379 | X509 *cacert = NULL; |
| 4380 | EVP_PKEY *capkey = NULL; |
| 4381 | int err = 0; |
| 4382 | |
| 4383 | if (!bind_conf || !bind_conf->generate_certs) |
| 4384 | return err; |
| 4385 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4386 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4387 | if (global_ssl.ctx_cache) |
| 4388 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4389 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 4390 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4391 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4392 | if (!bind_conf->ca_sign_file) { |
| 4393 | Alert("Proxy '%s': cannot enable certificate generation, " |
| 4394 | "no CA certificate File configured at [%s:%d].\n", |
| 4395 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4396 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4397 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4398 | |
| 4399 | /* read in the CA certificate */ |
| 4400 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
| 4401 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4402 | 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] | 4403 | goto load_error; |
| 4404 | } |
| 4405 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
| 4406 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4407 | 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] | 4408 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4409 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4410 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4411 | if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) { |
| 4412 | Alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 4413 | 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] | 4414 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4415 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4416 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4417 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4418 | bind_conf->ca_sign_cert = cacert; |
| 4419 | bind_conf->ca_sign_pkey = capkey; |
| 4420 | return err; |
| 4421 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4422 | read_error: |
| 4423 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4424 | if (capkey) EVP_PKEY_free(capkey); |
| 4425 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4426 | load_error: |
| 4427 | bind_conf->generate_certs = 0; |
| 4428 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4429 | return err; |
| 4430 | } |
| 4431 | |
| 4432 | /* Release CA cert and private key used to generate certificated */ |
| 4433 | void |
| 4434 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 4435 | { |
| 4436 | if (!bind_conf) |
| 4437 | return; |
| 4438 | |
| 4439 | if (bind_conf->ca_sign_pkey) |
| 4440 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 4441 | if (bind_conf->ca_sign_cert) |
| 4442 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 4443 | bind_conf->ca_sign_pkey = NULL; |
| 4444 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4445 | } |
| 4446 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4447 | /* |
| 4448 | * This function is called if SSL * context is not yet allocated. The function |
| 4449 | * is designed to be called before any other data-layer operation and sets the |
| 4450 | * handshake flag on the connection. It is safe to call it multiple times. |
| 4451 | * It returns 0 on success and -1 in error case. |
| 4452 | */ |
| 4453 | static int ssl_sock_init(struct connection *conn) |
| 4454 | { |
| 4455 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4456 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4457 | return 0; |
| 4458 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4459 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4460 | return 0; |
| 4461 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4462 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 4463 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4464 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4465 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4466 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4467 | /* If it is in client mode initiate SSL session |
| 4468 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4469 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4470 | int may_retry = 1; |
| 4471 | |
| 4472 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4473 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4474 | conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4475 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4476 | if (may_retry--) { |
| 4477 | pool_gc2(); |
| 4478 | goto retry_connect; |
| 4479 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4480 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4481 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4482 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4483 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4484 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4485 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 4486 | SSL_free(conn->xprt_ctx); |
| 4487 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4488 | if (may_retry--) { |
| 4489 | pool_gc2(); |
| 4490 | goto retry_connect; |
| 4491 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4492 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4493 | return -1; |
| 4494 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4495 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4496 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4497 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 4498 | SSL_free(conn->xprt_ctx); |
| 4499 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4500 | if (may_retry--) { |
| 4501 | pool_gc2(); |
| 4502 | goto retry_connect; |
| 4503 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4504 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4505 | return -1; |
| 4506 | } |
| 4507 | |
| 4508 | SSL_set_connect_state(conn->xprt_ctx); |
| 4509 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 4510 | if(!SSL_set_session(conn->xprt_ctx, objt_server(conn->target)->ssl_ctx.reused_sess)) { |
| 4511 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 4512 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 4513 | } |
| 4514 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4515 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4516 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4517 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4518 | |
| 4519 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 4520 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4521 | return 0; |
| 4522 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4523 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4524 | int may_retry = 1; |
| 4525 | |
| 4526 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4527 | /* Alloc a new SSL session ctx */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4528 | 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] | 4529 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4530 | if (may_retry--) { |
| 4531 | pool_gc2(); |
| 4532 | goto retry_accept; |
| 4533 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4534 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4535 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4536 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4537 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4538 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4539 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 4540 | SSL_free(conn->xprt_ctx); |
| 4541 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4542 | if (may_retry--) { |
| 4543 | pool_gc2(); |
| 4544 | goto retry_accept; |
| 4545 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4546 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4547 | return -1; |
| 4548 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4549 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4550 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4551 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 4552 | SSL_free(conn->xprt_ctx); |
| 4553 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4554 | if (may_retry--) { |
| 4555 | pool_gc2(); |
| 4556 | goto retry_accept; |
| 4557 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4558 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4559 | return -1; |
| 4560 | } |
| 4561 | |
| 4562 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4563 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4564 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4565 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4566 | |
| 4567 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 4568 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4569 | return 0; |
| 4570 | } |
| 4571 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4572 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4573 | return -1; |
| 4574 | } |
| 4575 | |
| 4576 | |
| 4577 | /* This is the callback which is used when an SSL handshake is pending. It |
| 4578 | * updates the FD status if it wants some polling before being called again. |
| 4579 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 4580 | * otherwise it returns non-zero and removes itself from the connection's |
| 4581 | * flags (the bit is provided in <flag> by the caller). |
| 4582 | */ |
| 4583 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 4584 | { |
| 4585 | int ret; |
| 4586 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4587 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4588 | return 0; |
| 4589 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4590 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4591 | goto out_error; |
| 4592 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4593 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 4594 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 4595 | * Usually SSL_write and SSL_read are used and process implicitly |
| 4596 | * the reneg handshake. |
| 4597 | * Here we use SSL_peek as a workaround for reneg. |
| 4598 | */ |
| 4599 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4600 | char c; |
| 4601 | |
| 4602 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 4603 | if (ret <= 0) { |
| 4604 | /* handshake may have not been completed, let's find why */ |
| 4605 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4606 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4607 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 4608 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 4609 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4610 | __conn_sock_want_send(conn); |
| 4611 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4612 | return 0; |
| 4613 | } |
| 4614 | else if (ret == SSL_ERROR_WANT_READ) { |
| 4615 | /* handshake may have been completed but we have |
| 4616 | * no more data to read. |
| 4617 | */ |
| 4618 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4619 | ret = 1; |
| 4620 | goto reneg_ok; |
| 4621 | } |
| 4622 | /* SSL handshake needs to read, L4 connection is ready */ |
| 4623 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 4624 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4625 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4626 | __conn_sock_want_recv(conn); |
| 4627 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4628 | return 0; |
| 4629 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4630 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 4631 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 4632 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4633 | return 0; |
| 4634 | } |
| 4635 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4636 | else if (ret == SSL_ERROR_SYSCALL) { |
| 4637 | /* if errno is null, then connection was successfully established */ |
| 4638 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 4639 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4640 | if (!conn->err_code) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4641 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 4642 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4643 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4644 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 4645 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4646 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 4647 | empty_handshake = state == TLS_ST_BEFORE; |
| 4648 | #else |
| 4649 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
| 4650 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4651 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4652 | if (!errno) { |
| 4653 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4654 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4655 | else |
| 4656 | conn->err_code = CO_ER_SSL_EMPTY; |
| 4657 | } |
| 4658 | else { |
| 4659 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4660 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4661 | else |
| 4662 | conn->err_code = CO_ER_SSL_ABORT; |
| 4663 | } |
| 4664 | } |
| 4665 | else { |
| 4666 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4667 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4668 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4669 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4670 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4671 | #endif |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4672 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4673 | goto out_error; |
| 4674 | } |
| 4675 | else { |
| 4676 | /* Fail on all other handshake errors */ |
| 4677 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 4678 | * buffer, causing an RST to be emitted upon close() on |
| 4679 | * TCP sockets. We first try to drain possibly pending |
| 4680 | * data to avoid this as much as possible. |
| 4681 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 4682 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4683 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 4684 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 4685 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4686 | goto out_error; |
| 4687 | } |
| 4688 | } |
| 4689 | /* read some data: consider handshake completed */ |
| 4690 | goto reneg_ok; |
| 4691 | } |
| 4692 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4693 | ret = SSL_do_handshake(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4694 | if (ret != 1) { |
| 4695 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4696 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4697 | |
| 4698 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 4699 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 4700 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4701 | __conn_sock_want_send(conn); |
| 4702 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4703 | return 0; |
| 4704 | } |
| 4705 | else if (ret == SSL_ERROR_WANT_READ) { |
| 4706 | /* SSL handshake needs to read, L4 connection is ready */ |
| 4707 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 4708 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4709 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4710 | __conn_sock_want_recv(conn); |
| 4711 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4712 | return 0; |
| 4713 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4714 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 4715 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 4716 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4717 | return 0; |
| 4718 | } |
| 4719 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 4720 | else if (ret == SSL_ERROR_SYSCALL) { |
| 4721 | /* if errno is null, then connection was successfully established */ |
| 4722 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 4723 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4724 | if (!conn->err_code) { |
| 4725 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 4726 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4727 | #else |
| 4728 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 4729 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4730 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 4731 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4732 | #else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4733 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4734 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4735 | if (empty_handshake) { |
| 4736 | if (!errno) { |
| 4737 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4738 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4739 | else |
| 4740 | conn->err_code = CO_ER_SSL_EMPTY; |
| 4741 | } |
| 4742 | else { |
| 4743 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4744 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4745 | else |
| 4746 | conn->err_code = CO_ER_SSL_ABORT; |
| 4747 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4748 | } |
| 4749 | else { |
| 4750 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4751 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4752 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4753 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4754 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4755 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4756 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 4757 | goto out_error; |
| 4758 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4759 | else { |
| 4760 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 4761 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 4762 | * buffer, causing an RST to be emitted upon close() on |
| 4763 | * TCP sockets. We first try to drain possibly pending |
| 4764 | * data to avoid this as much as possible. |
| 4765 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 4766 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4767 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 4768 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 4769 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4770 | goto out_error; |
| 4771 | } |
| 4772 | } |
| 4773 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4774 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 4775 | |
| 4776 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 4777 | /* ASYNC engine API doesn't support moving read/write |
| 4778 | * buffers. So we disable ASYNC mode right after |
| 4779 | * the handshake to avoid buffer oveflows. |
| 4780 | */ |
| 4781 | if (global_ssl.async) |
| 4782 | SSL_clear_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 4783 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4784 | /* Handshake succeeded */ |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 4785 | if (!SSL_session_reused(conn->xprt_ctx)) { |
| 4786 | if (objt_server(conn->target)) { |
| 4787 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 4788 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 4789 | global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr; |
| 4790 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4791 | /* check if session was reused, if not store current session on server for reuse */ |
Willy Tarreau | 30fd4bd | 2016-12-22 21:54:21 +0100 | [diff] [blame] | 4792 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4793 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
Willy Tarreau | 30fd4bd | 2016-12-22 21:54:21 +0100 | [diff] [blame] | 4794 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 4795 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4796 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 4797 | if (!(objt_server(conn->target)->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) |
| 4798 | objt_server(conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4799 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 4800 | else { |
| 4801 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 4802 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 4803 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 4804 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | /* The connection is now established at both layers, it's time to leave */ |
| 4808 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 4809 | return 1; |
| 4810 | |
| 4811 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4812 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4813 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4814 | ERR_clear_error(); |
| 4815 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 4816 | /* free resumed session if exists */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4817 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 4818 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 4819 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 4820 | } |
| 4821 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4822 | /* Fail on all other handshake errors */ |
| 4823 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4824 | if (!conn->err_code) |
| 4825 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4826 | return 0; |
| 4827 | } |
| 4828 | |
| 4829 | /* 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] | 4830 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4831 | * buffer wraps, in which case a second call may be performed. The connection's |
| 4832 | * flags are updated with whatever special event is detected (error, read0, |
| 4833 | * empty). The caller is responsible for taking care of those events and |
| 4834 | * avoiding the call if inappropriate. The function does not call the |
| 4835 | * connection's polling update function, so the caller is responsible for this. |
| 4836 | */ |
| 4837 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 4838 | { |
| 4839 | int ret, done = 0; |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 4840 | int try; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4841 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4842 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4843 | goto out_error; |
| 4844 | |
| 4845 | if (conn->flags & CO_FL_HANDSHAKE) |
| 4846 | /* a handshake was requested */ |
| 4847 | return 0; |
| 4848 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 4849 | /* let's realign the buffer to optimize I/O */ |
| 4850 | if (buffer_empty(buf)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4851 | buf->p = buf->data; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4852 | |
| 4853 | /* read the largest possible block. For this, we perform only one call |
| 4854 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 4855 | * in which case we accept to do it once again. A new attempt is made on |
| 4856 | * EINTR too. |
| 4857 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 4858 | while (count > 0) { |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 4859 | /* first check if we have some room after p+i */ |
| 4860 | try = buf->data + buf->size - (buf->p + buf->i); |
| 4861 | /* otherwise continue between data and p-o */ |
| 4862 | if (try <= 0) { |
| 4863 | try = buf->p - (buf->data + buf->o); |
| 4864 | if (try <= 0) |
| 4865 | break; |
| 4866 | } |
| 4867 | if (try > count) |
| 4868 | try = count; |
| 4869 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4870 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4871 | if (conn->flags & CO_FL_ERROR) { |
| 4872 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4873 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4874 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4875 | if (ret > 0) { |
| 4876 | buf->i += ret; |
| 4877 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4878 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4879 | } |
| 4880 | else if (ret == 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4881 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 4882 | if (ret != SSL_ERROR_ZERO_RETURN) { |
Emeric Brun | 1c64686 | 2012-12-14 12:33:41 +0100 | [diff] [blame] | 4883 | /* error on protocol or underlying transport */ |
| 4884 | if ((ret != SSL_ERROR_SYSCALL) |
| 4885 | || (errno && (errno != EAGAIN))) |
| 4886 | conn->flags |= CO_FL_ERROR; |
| 4887 | |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4888 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4889 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4890 | ERR_clear_error(); |
| 4891 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4892 | goto read0; |
| 4893 | } |
| 4894 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4895 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4896 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4897 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4898 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4899 | __conn_sock_want_send(conn); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 4900 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 4901 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 4902 | if (global_ssl.async) |
| 4903 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 4904 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4905 | break; |
| 4906 | } |
| 4907 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 4908 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4909 | /* handshake is running, and it may need to re-enable read */ |
| 4910 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 4911 | __conn_sock_want_recv(conn); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 4912 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 4913 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 4914 | if (global_ssl.async) |
| 4915 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 4916 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 4917 | break; |
| 4918 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4919 | /* we need to poll for retry a read later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4920 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4921 | break; |
| 4922 | } |
| 4923 | /* otherwise it's a real error */ |
| 4924 | goto out_error; |
| 4925 | } |
| 4926 | } |
| 4927 | return done; |
| 4928 | |
| 4929 | read0: |
| 4930 | conn_sock_read0(conn); |
| 4931 | return done; |
| 4932 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4933 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4934 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4935 | ERR_clear_error(); |
| 4936 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4937 | conn->flags |= CO_FL_ERROR; |
| 4938 | return done; |
| 4939 | } |
| 4940 | |
| 4941 | |
| 4942 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 4943 | * <flags> may contain some CO_SFL_* flags to hint the system about other |
| 4944 | * pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4945 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 4946 | * a second call may be performed. The connection's flags are updated with |
| 4947 | * whatever special event is detected (error, empty). The caller is responsible |
| 4948 | * for taking care of those events and avoiding the call if inappropriate. The |
| 4949 | * function does not call the connection's polling update function, so the caller |
| 4950 | * is responsible for this. |
| 4951 | */ |
| 4952 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 4953 | { |
| 4954 | int ret, try, done; |
| 4955 | |
| 4956 | done = 0; |
| 4957 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4958 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4959 | goto out_error; |
| 4960 | |
| 4961 | if (conn->flags & CO_FL_HANDSHAKE) |
| 4962 | /* a handshake was requested */ |
| 4963 | return 0; |
| 4964 | |
| 4965 | /* send the largest possible block. For this we perform only one call |
| 4966 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 4967 | * in which case we accept to do it once again. |
| 4968 | */ |
| 4969 | while (buf->o) { |
Kevin Hester | cad8234 | 2013-05-30 15:12:41 -0700 | [diff] [blame] | 4970 | try = bo_contig_data(buf); |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 4971 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 4972 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4973 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4974 | global_ssl.max_record && try > global_ssl.max_record) { |
| 4975 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4976 | } |
| 4977 | else { |
| 4978 | /* we need to keep the information about the fact that |
| 4979 | * we're not limiting the upcoming send(), because if it |
| 4980 | * fails, we'll have to retry with at least as many data. |
| 4981 | */ |
| 4982 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 4983 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 4984 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4985 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4986 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4987 | if (conn->flags & CO_FL_ERROR) { |
| 4988 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4989 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4990 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4991 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4992 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
| 4993 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4994 | buf->o -= ret; |
| 4995 | done += ret; |
| 4996 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 4997 | if (likely(buffer_empty(buf))) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4998 | /* optimize data alignment in the buffer */ |
| 4999 | buf->p = buf->data; |
| 5000 | |
| 5001 | /* if the system buffer is full, don't insist */ |
| 5002 | if (ret < try) |
| 5003 | break; |
| 5004 | } |
| 5005 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5006 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5007 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5008 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5009 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5010 | /* handshake is running, and it may need to re-enable write */ |
| 5011 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5012 | __conn_sock_want_send(conn); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5013 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 5014 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5015 | if (global_ssl.async) |
| 5016 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5017 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5018 | break; |
| 5019 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5020 | /* we need to poll to retry a write later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5021 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5022 | break; |
| 5023 | } |
| 5024 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5025 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5026 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5027 | __conn_sock_want_recv(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5028 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5029 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5030 | if (global_ssl.async) |
| 5031 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5032 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5033 | break; |
| 5034 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5035 | goto out_error; |
| 5036 | } |
| 5037 | } |
| 5038 | return done; |
| 5039 | |
| 5040 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5041 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5042 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5043 | ERR_clear_error(); |
| 5044 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5045 | conn->flags |= CO_FL_ERROR; |
| 5046 | return done; |
| 5047 | } |
| 5048 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5049 | static void ssl_sock_close(struct connection *conn) { |
| 5050 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5051 | if (conn->xprt_ctx) { |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5052 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5053 | if (global_ssl.async) { |
| 5054 | OSSL_ASYNC_FD all_fd[32], afd; |
| 5055 | size_t num_all_fds = 0; |
| 5056 | int i; |
| 5057 | |
| 5058 | SSL_get_all_async_fds(conn->xprt_ctx, NULL, &num_all_fds); |
| 5059 | if (num_all_fds > 32) { |
| 5060 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 5061 | return; |
| 5062 | } |
| 5063 | |
| 5064 | SSL_get_all_async_fds(conn->xprt_ctx, all_fd, &num_all_fds); |
| 5065 | |
| 5066 | /* If an async job is pending, we must try to |
| 5067 | to catch the end using polling before calling |
| 5068 | SSL_free */ |
| 5069 | if (num_all_fds && SSL_waiting_for_async(conn->xprt_ctx)) { |
| 5070 | for (i=0 ; i < num_all_fds ; i++) { |
| 5071 | /* switch on an handler designed to |
| 5072 | * handle the SSL_free |
| 5073 | */ |
| 5074 | afd = all_fd[i]; |
| 5075 | fdtab[afd].iocb = ssl_async_fd_free; |
| 5076 | fdtab[afd].owner = conn->xprt_ctx; |
| 5077 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 5078 | /* To ensure that the fd cache won't be used |
| 5079 | * and we'll catch a real RD event. |
| 5080 | */ |
| 5081 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5082 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5083 | conn->xprt_ctx = NULL; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5084 | jobs++; |
| 5085 | return; |
| 5086 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5087 | /* Else we can remove the fds from the fdtab |
| 5088 | * and call SSL_free. |
| 5089 | * note: we do a fd_remove and not a delete |
| 5090 | * because the fd is owned by the engine. |
| 5091 | * the engine is responsible to close |
| 5092 | */ |
| 5093 | for (i=0 ; i < num_all_fds ; i++) |
| 5094 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5095 | } |
| 5096 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5097 | SSL_free(conn->xprt_ctx); |
| 5098 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5099 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5100 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 5104 | * any case, flags the connection as reusable if no handshake was in progress. |
| 5105 | */ |
| 5106 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 5107 | { |
| 5108 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5109 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5110 | if (!clean) |
| 5111 | /* don't sent notify on SSL_shutdown */ |
Willy Tarreau | e3cc3a3 | 2017-02-13 11:12:29 +0100 | [diff] [blame] | 5112 | SSL_set_quiet_shutdown(conn->xprt_ctx, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5113 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5114 | if (SSL_shutdown(conn->xprt_ctx) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5115 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5116 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5117 | ERR_clear_error(); |
| 5118 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5119 | } |
| 5120 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5121 | /* used for logging, may be changed for a sample fetch later */ |
| 5122 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 5123 | { |
| 5124 | if (!conn->xprt && !conn->xprt_ctx) |
| 5125 | return NULL; |
| 5126 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 5127 | } |
| 5128 | |
| 5129 | /* used for logging, may be changed for a sample fetch later */ |
| 5130 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 5131 | { |
| 5132 | if (!conn->xprt && !conn->xprt_ctx) |
| 5133 | return NULL; |
| 5134 | return SSL_get_version(conn->xprt_ctx); |
| 5135 | } |
| 5136 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5137 | /* Extract a serial from a cert, and copy it to a chunk. |
| 5138 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 5139 | * -1 if output is not large enough. |
| 5140 | */ |
| 5141 | static int |
| 5142 | ssl_sock_get_serial(X509 *crt, struct chunk *out) |
| 5143 | { |
| 5144 | ASN1_INTEGER *serial; |
| 5145 | |
| 5146 | serial = X509_get_serialNumber(crt); |
| 5147 | if (!serial) |
| 5148 | return 0; |
| 5149 | |
| 5150 | if (out->size < serial->length) |
| 5151 | return -1; |
| 5152 | |
| 5153 | memcpy(out->str, serial->data, serial->length); |
| 5154 | out->len = serial->length; |
| 5155 | return 1; |
| 5156 | } |
| 5157 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5158 | /* Extract a cert to der, and copy it to a chunk. |
| 5159 | * Returns 1 if cert is found and copied, 0 on der convertion failure and |
| 5160 | * -1 if output is not large enough. |
| 5161 | */ |
| 5162 | static int |
| 5163 | ssl_sock_crt2der(X509 *crt, struct chunk *out) |
| 5164 | { |
| 5165 | int len; |
| 5166 | unsigned char *p = (unsigned char *)out->str;; |
| 5167 | |
| 5168 | len =i2d_X509(crt, NULL); |
| 5169 | if (len <= 0) |
| 5170 | return 1; |
| 5171 | |
| 5172 | if (out->size < len) |
| 5173 | return -1; |
| 5174 | |
| 5175 | i2d_X509(crt,&p); |
| 5176 | out->len = len; |
| 5177 | return 1; |
| 5178 | } |
| 5179 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5180 | |
| 5181 | /* Copy Date in ASN1_UTCTIME format in struct chunk out. |
| 5182 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 5183 | * and -1 if output is not large enough. |
| 5184 | */ |
| 5185 | static int |
| 5186 | ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out) |
| 5187 | { |
| 5188 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 5189 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 5190 | |
| 5191 | if (gentm->length < 12) |
| 5192 | return 0; |
| 5193 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 5194 | return 0; |
| 5195 | if (out->size < gentm->length-2) |
| 5196 | return -1; |
| 5197 | |
| 5198 | memcpy(out->str, gentm->data+2, gentm->length-2); |
| 5199 | out->len = gentm->length-2; |
| 5200 | return 1; |
| 5201 | } |
| 5202 | else if (tm->type == V_ASN1_UTCTIME) { |
| 5203 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 5204 | |
| 5205 | if (utctm->length < 10) |
| 5206 | return 0; |
| 5207 | if (utctm->data[0] >= 0x35) |
| 5208 | return 0; |
| 5209 | if (out->size < utctm->length) |
| 5210 | return -1; |
| 5211 | |
| 5212 | memcpy(out->str, utctm->data, utctm->length); |
| 5213 | out->len = utctm->length; |
| 5214 | return 1; |
| 5215 | } |
| 5216 | |
| 5217 | return 0; |
| 5218 | } |
| 5219 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5220 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 5221 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 5222 | */ |
| 5223 | static int |
| 5224 | ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out) |
| 5225 | { |
| 5226 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5227 | ASN1_OBJECT *obj; |
| 5228 | ASN1_STRING *data; |
| 5229 | const unsigned char *data_ptr; |
| 5230 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5231 | int i, j, n; |
| 5232 | int cur = 0; |
| 5233 | const char *s; |
| 5234 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5235 | int name_count; |
| 5236 | |
| 5237 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5238 | |
| 5239 | out->len = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5240 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5241 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5242 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5243 | else |
| 5244 | j = i; |
| 5245 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5246 | ne = X509_NAME_get_entry(a, j); |
| 5247 | obj = X509_NAME_ENTRY_get_object(ne); |
| 5248 | data = X509_NAME_ENTRY_get_data(ne); |
| 5249 | data_ptr = ASN1_STRING_get0_data(data); |
| 5250 | data_len = ASN1_STRING_length(data); |
| 5251 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5252 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5253 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5254 | s = tmp; |
| 5255 | } |
| 5256 | |
| 5257 | if (chunk_strcasecmp(entry, s) != 0) |
| 5258 | continue; |
| 5259 | |
| 5260 | if (pos < 0) |
| 5261 | cur--; |
| 5262 | else |
| 5263 | cur++; |
| 5264 | |
| 5265 | if (cur != pos) |
| 5266 | continue; |
| 5267 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5268 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5269 | return -1; |
| 5270 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5271 | memcpy(out->str, data_ptr, data_len); |
| 5272 | out->len = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5273 | return 1; |
| 5274 | } |
| 5275 | |
| 5276 | return 0; |
| 5277 | |
| 5278 | } |
| 5279 | |
| 5280 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 5281 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 5282 | */ |
| 5283 | static int |
| 5284 | ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out) |
| 5285 | { |
| 5286 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5287 | ASN1_OBJECT *obj; |
| 5288 | ASN1_STRING *data; |
| 5289 | const unsigned char *data_ptr; |
| 5290 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5291 | int i, n, ln; |
| 5292 | int l = 0; |
| 5293 | const char *s; |
| 5294 | char *p; |
| 5295 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5296 | int name_count; |
| 5297 | |
| 5298 | |
| 5299 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5300 | |
| 5301 | out->len = 0; |
| 5302 | p = out->str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5303 | for (i = 0; i < name_count; i++) { |
| 5304 | ne = X509_NAME_get_entry(a, i); |
| 5305 | obj = X509_NAME_ENTRY_get_object(ne); |
| 5306 | data = X509_NAME_ENTRY_get_data(ne); |
| 5307 | data_ptr = ASN1_STRING_get0_data(data); |
| 5308 | data_len = ASN1_STRING_length(data); |
| 5309 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5310 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5311 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5312 | s = tmp; |
| 5313 | } |
| 5314 | ln = strlen(s); |
| 5315 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5316 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5317 | if (l > out->size) |
| 5318 | return -1; |
| 5319 | out->len = l; |
| 5320 | |
| 5321 | *(p++)='/'; |
| 5322 | memcpy(p, s, ln); |
| 5323 | p += ln; |
| 5324 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5325 | memcpy(p, data_ptr, data_len); |
| 5326 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5327 | } |
| 5328 | |
| 5329 | if (!out->len) |
| 5330 | return 0; |
| 5331 | |
| 5332 | return 1; |
| 5333 | } |
| 5334 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5335 | char *ssl_sock_get_version(struct connection *conn) |
| 5336 | { |
| 5337 | if (!ssl_sock_is_ssl(conn)) |
| 5338 | return NULL; |
| 5339 | |
| 5340 | return (char *)SSL_get_version(conn->xprt_ctx); |
| 5341 | } |
| 5342 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5343 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 5344 | * to disable SNI. |
| 5345 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5346 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 5347 | { |
| 5348 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5349 | char *prev_name; |
| 5350 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5351 | if (!ssl_sock_is_ssl(conn)) |
| 5352 | return; |
| 5353 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5354 | /* if the SNI changes, we must destroy the reusable context so that a |
| 5355 | * new connection will present a new SNI. As an optimization we could |
| 5356 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 5357 | * server. |
| 5358 | */ |
| 5359 | prev_name = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 5360 | if ((!prev_name && hostname) || |
| 5361 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
| 5362 | SSL_set_session(conn->xprt_ctx, NULL); |
| 5363 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5364 | SSL_set_tlsext_host_name(conn->xprt_ctx, hostname); |
| 5365 | #endif |
| 5366 | } |
| 5367 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5368 | /* Extract peer certificate's common name into the chunk dest |
| 5369 | * Returns |
| 5370 | * the len of the extracted common name |
| 5371 | * or 0 if no CN found in DN |
| 5372 | * or -1 on error case (i.e. no peer certificate) |
| 5373 | */ |
| 5374 | int ssl_sock_get_remote_common_name(struct connection *conn, struct chunk *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5375 | { |
| 5376 | X509 *crt = NULL; |
| 5377 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5378 | const char find_cn[] = "CN"; |
| 5379 | const struct chunk find_cn_chunk = { |
| 5380 | .str = (char *)&find_cn, |
| 5381 | .len = sizeof(find_cn)-1 |
| 5382 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5383 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5384 | |
| 5385 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5386 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5387 | |
| 5388 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5389 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5390 | if (!crt) |
| 5391 | goto out; |
| 5392 | |
| 5393 | name = X509_get_subject_name(crt); |
| 5394 | if (!name) |
| 5395 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5396 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5397 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 5398 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5399 | if (crt) |
| 5400 | X509_free(crt); |
| 5401 | |
| 5402 | return result; |
| 5403 | } |
| 5404 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 5405 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 5406 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 5407 | { |
| 5408 | X509 *crt = NULL; |
| 5409 | |
| 5410 | if (!ssl_sock_is_ssl(conn)) |
| 5411 | return 0; |
| 5412 | |
| 5413 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5414 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5415 | if (!crt) |
| 5416 | return 0; |
| 5417 | |
| 5418 | X509_free(crt); |
| 5419 | return 1; |
| 5420 | } |
| 5421 | |
| 5422 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 5423 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5424 | { |
| 5425 | if (!ssl_sock_is_ssl(conn)) |
| 5426 | return 0; |
| 5427 | |
| 5428 | return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
| 5429 | } |
| 5430 | |
| 5431 | /* returns result from SSL verify */ |
| 5432 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 5433 | { |
| 5434 | if (!ssl_sock_is_ssl(conn)) |
| 5435 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 5436 | |
| 5437 | return (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
| 5438 | } |
| 5439 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 5440 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 5441 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 5442 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 5443 | * freed by the caller. NPN is also checked if available since older versions |
| 5444 | * of openssl (1.0.1) which are more common in field only support this one. |
| 5445 | */ |
| 5446 | static int ssl_sock_get_alpn(const struct connection *conn, const char **str, int *len) |
| 5447 | { |
| 5448 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5449 | return 0; |
| 5450 | |
| 5451 | *str = NULL; |
| 5452 | |
| 5453 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 5454 | SSL_get0_alpn_selected(conn->xprt_ctx, (const unsigned char **)str, (unsigned *)len); |
| 5455 | if (*str) |
| 5456 | return 1; |
| 5457 | #endif |
| 5458 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 5459 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, (const unsigned char **)str, (unsigned *)len); |
| 5460 | if (*str) |
| 5461 | return 1; |
| 5462 | #endif |
| 5463 | return 0; |
| 5464 | } |
| 5465 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5466 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 5467 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 5468 | /* boolean, returns true if client cert was present */ |
| 5469 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5470 | 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] | 5471 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5472 | struct connection *conn; |
| 5473 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5474 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5475 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 5476 | return 0; |
| 5477 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5478 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 5479 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5480 | return 0; |
| 5481 | } |
| 5482 | |
| 5483 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5484 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5485 | 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] | 5486 | |
| 5487 | return 1; |
| 5488 | } |
| 5489 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5490 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 5491 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5492 | * should be use. |
| 5493 | */ |
| 5494 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5495 | 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] | 5496 | { |
| 5497 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 5498 | X509 *crt = NULL; |
| 5499 | int ret = 0; |
| 5500 | struct chunk *smp_trash; |
| 5501 | struct connection *conn; |
| 5502 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5503 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5504 | if (!conn || conn->xprt != &ssl_sock) |
| 5505 | return 0; |
| 5506 | |
| 5507 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 5508 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5509 | return 0; |
| 5510 | } |
| 5511 | |
| 5512 | if (cert_peer) |
| 5513 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5514 | else |
| 5515 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5516 | |
| 5517 | if (!crt) |
| 5518 | goto out; |
| 5519 | |
| 5520 | smp_trash = get_trash_chunk(); |
| 5521 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 5522 | goto out; |
| 5523 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5524 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5525 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5526 | ret = 1; |
| 5527 | out: |
| 5528 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5529 | if (cert_peer && crt) |
| 5530 | X509_free(crt); |
| 5531 | return ret; |
| 5532 | } |
| 5533 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5534 | /* binary, returns serial of certificate in a binary chunk. |
| 5535 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5536 | * should be use. |
| 5537 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5538 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5539 | 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] | 5540 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5541 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5542 | X509 *crt = NULL; |
| 5543 | int ret = 0; |
| 5544 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5545 | struct connection *conn; |
| 5546 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5547 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5548 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5549 | return 0; |
| 5550 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5551 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5552 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5553 | return 0; |
| 5554 | } |
| 5555 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5556 | if (cert_peer) |
| 5557 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5558 | else |
| 5559 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5560 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5561 | if (!crt) |
| 5562 | goto out; |
| 5563 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5564 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5565 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 5566 | goto out; |
| 5567 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5568 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5569 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5570 | ret = 1; |
| 5571 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5572 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5573 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5574 | X509_free(crt); |
| 5575 | return ret; |
| 5576 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 5577 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5578 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 5579 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5580 | * should be use. |
| 5581 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5582 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5583 | 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] | 5584 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5585 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5586 | X509 *crt = NULL; |
| 5587 | const EVP_MD *digest; |
| 5588 | int ret = 0; |
| 5589 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5590 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5591 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5592 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5593 | if (!conn || conn->xprt != &ssl_sock) |
| 5594 | return 0; |
| 5595 | |
| 5596 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5597 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5598 | return 0; |
| 5599 | } |
| 5600 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5601 | if (cert_peer) |
| 5602 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5603 | else |
| 5604 | crt = SSL_get_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5605 | if (!crt) |
| 5606 | goto out; |
| 5607 | |
| 5608 | smp_trash = get_trash_chunk(); |
| 5609 | digest = EVP_sha1(); |
| 5610 | X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len); |
| 5611 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5612 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5613 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5614 | ret = 1; |
| 5615 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5616 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5617 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5618 | X509_free(crt); |
| 5619 | return ret; |
| 5620 | } |
| 5621 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5622 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 5623 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5624 | * should be use. |
| 5625 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5626 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5627 | 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] | 5628 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5629 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5630 | X509 *crt = NULL; |
| 5631 | int ret = 0; |
| 5632 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5633 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5634 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5635 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5636 | if (!conn || conn->xprt != &ssl_sock) |
| 5637 | return 0; |
| 5638 | |
| 5639 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5640 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5641 | return 0; |
| 5642 | } |
| 5643 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5644 | if (cert_peer) |
| 5645 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5646 | else |
| 5647 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5648 | if (!crt) |
| 5649 | goto out; |
| 5650 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5651 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5652 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 5653 | goto out; |
| 5654 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5655 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5656 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5657 | ret = 1; |
| 5658 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5659 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5660 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5661 | X509_free(crt); |
| 5662 | return ret; |
| 5663 | } |
| 5664 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5665 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 5666 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5667 | * should be use. |
| 5668 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5669 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5670 | 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] | 5671 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5672 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5673 | X509 *crt = NULL; |
| 5674 | X509_NAME *name; |
| 5675 | int ret = 0; |
| 5676 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5677 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5678 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5679 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5680 | if (!conn || conn->xprt != &ssl_sock) |
| 5681 | return 0; |
| 5682 | |
| 5683 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5684 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5685 | return 0; |
| 5686 | } |
| 5687 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5688 | if (cert_peer) |
| 5689 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5690 | else |
| 5691 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5692 | if (!crt) |
| 5693 | goto out; |
| 5694 | |
| 5695 | name = X509_get_issuer_name(crt); |
| 5696 | if (!name) |
| 5697 | goto out; |
| 5698 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5699 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5700 | if (args && args[0].type == ARGT_STR) { |
| 5701 | int pos = 1; |
| 5702 | |
| 5703 | if (args[1].type == ARGT_SINT) |
| 5704 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5705 | |
| 5706 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 5707 | goto out; |
| 5708 | } |
| 5709 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 5710 | goto out; |
| 5711 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5712 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5713 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5714 | ret = 1; |
| 5715 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5716 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5717 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5718 | X509_free(crt); |
| 5719 | return ret; |
| 5720 | } |
| 5721 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5722 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 5723 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5724 | * should be use. |
| 5725 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5726 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5727 | 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] | 5728 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5729 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5730 | X509 *crt = NULL; |
| 5731 | int ret = 0; |
| 5732 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5733 | struct connection *conn; |
| 5734 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5735 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5736 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5737 | return 0; |
| 5738 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5739 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5740 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5741 | return 0; |
| 5742 | } |
| 5743 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5744 | if (cert_peer) |
| 5745 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5746 | else |
| 5747 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5748 | if (!crt) |
| 5749 | goto out; |
| 5750 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5751 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5752 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 5753 | goto out; |
| 5754 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5755 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5756 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5757 | ret = 1; |
| 5758 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5759 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5760 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5761 | X509_free(crt); |
| 5762 | return ret; |
| 5763 | } |
| 5764 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5765 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 5766 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5767 | * should be use. |
| 5768 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5769 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5770 | 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] | 5771 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5772 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5773 | X509 *crt = NULL; |
| 5774 | X509_NAME *name; |
| 5775 | int ret = 0; |
| 5776 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5777 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5778 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5779 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5780 | if (!conn || conn->xprt != &ssl_sock) |
| 5781 | return 0; |
| 5782 | |
| 5783 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5784 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5785 | return 0; |
| 5786 | } |
| 5787 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5788 | if (cert_peer) |
| 5789 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5790 | else |
| 5791 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5792 | if (!crt) |
| 5793 | goto out; |
| 5794 | |
| 5795 | name = X509_get_subject_name(crt); |
| 5796 | if (!name) |
| 5797 | goto out; |
| 5798 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5799 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5800 | if (args && args[0].type == ARGT_STR) { |
| 5801 | int pos = 1; |
| 5802 | |
| 5803 | if (args[1].type == ARGT_SINT) |
| 5804 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5805 | |
| 5806 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 5807 | goto out; |
| 5808 | } |
| 5809 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 5810 | goto out; |
| 5811 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5812 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5813 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5814 | ret = 1; |
| 5815 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5816 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5817 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5818 | X509_free(crt); |
| 5819 | return ret; |
| 5820 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5821 | |
| 5822 | /* integer, returns true if current session use a client certificate */ |
| 5823 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5824 | 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] | 5825 | { |
| 5826 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5827 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5828 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5829 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5830 | if (!conn || conn->xprt != &ssl_sock) |
| 5831 | return 0; |
| 5832 | |
| 5833 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5834 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5835 | return 0; |
| 5836 | } |
| 5837 | |
| 5838 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5839 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5840 | if (crt) { |
| 5841 | X509_free(crt); |
| 5842 | } |
| 5843 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5844 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5845 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5846 | return 1; |
| 5847 | } |
| 5848 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5849 | /* integer, returns the certificate version |
| 5850 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5851 | * should be use. |
| 5852 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5853 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5854 | 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] | 5855 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5856 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5857 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5858 | struct connection *conn; |
| 5859 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5860 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5861 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5862 | return 0; |
| 5863 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5864 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5865 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5866 | return 0; |
| 5867 | } |
| 5868 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5869 | if (cert_peer) |
| 5870 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5871 | else |
| 5872 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5873 | if (!crt) |
| 5874 | return 0; |
| 5875 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5876 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5877 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5878 | if (cert_peer) |
| 5879 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5880 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5881 | |
| 5882 | return 1; |
| 5883 | } |
| 5884 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5885 | /* string, returns the certificate's signature algorithm. |
| 5886 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5887 | * should be use. |
| 5888 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5889 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5890 | 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] | 5891 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5892 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5893 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5894 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5895 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5896 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5897 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5898 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5899 | if (!conn || conn->xprt != &ssl_sock) |
| 5900 | return 0; |
| 5901 | |
| 5902 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5903 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5904 | return 0; |
| 5905 | } |
| 5906 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5907 | if (cert_peer) |
| 5908 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5909 | else |
| 5910 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5911 | if (!crt) |
| 5912 | return 0; |
| 5913 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5914 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 5915 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5916 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5917 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 5918 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5919 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5920 | if (cert_peer) |
| 5921 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5922 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 5923 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5924 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5925 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5926 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5927 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5928 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5929 | if (cert_peer) |
| 5930 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5931 | |
| 5932 | return 1; |
| 5933 | } |
| 5934 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5935 | /* string, returns the certificate's key algorithm. |
| 5936 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5937 | * should be use. |
| 5938 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5939 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5940 | 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] | 5941 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5942 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5943 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5944 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5945 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5946 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5947 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5948 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5949 | if (!conn || conn->xprt != &ssl_sock) |
| 5950 | return 0; |
| 5951 | |
| 5952 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5953 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5954 | return 0; |
| 5955 | } |
| 5956 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5957 | if (cert_peer) |
| 5958 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5959 | else |
| 5960 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5961 | if (!crt) |
| 5962 | return 0; |
| 5963 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5964 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 5965 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5966 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5967 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 5968 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5969 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5970 | if (cert_peer) |
| 5971 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5972 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 5973 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5974 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5975 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5976 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5977 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5978 | if (cert_peer) |
| 5979 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5980 | |
| 5981 | return 1; |
| 5982 | } |
| 5983 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5984 | /* boolean, returns true if front conn. transport layer is SSL. |
| 5985 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5986 | * char is 'b'. |
| 5987 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5988 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5989 | 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] | 5990 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5991 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5992 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5993 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5994 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5995 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5996 | return 1; |
| 5997 | } |
| 5998 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5999 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6000 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6001 | 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] | 6002 | { |
| 6003 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6004 | struct connection *conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6005 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6006 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6007 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6008 | conn->xprt_ctx && |
| 6009 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6010 | return 1; |
| 6011 | #else |
| 6012 | return 0; |
| 6013 | #endif |
| 6014 | } |
| 6015 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6016 | /* boolean, returns true if client session has been resumed */ |
| 6017 | static int |
| 6018 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6019 | { |
| 6020 | struct connection *conn = objt_conn(smp->sess->origin); |
| 6021 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6022 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6023 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6024 | conn->xprt_ctx && |
| 6025 | SSL_session_reused(conn->xprt_ctx); |
| 6026 | return 1; |
| 6027 | } |
| 6028 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6029 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 6030 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6031 | * char is 'b'. |
| 6032 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6033 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6034 | 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] | 6035 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6036 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6037 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6038 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6039 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6040 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6041 | return 0; |
| 6042 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6043 | smp->data.u.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
| 6044 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6045 | return 0; |
| 6046 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6047 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6048 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6049 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6050 | |
| 6051 | return 1; |
| 6052 | } |
| 6053 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6054 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 6055 | * is SSL. |
| 6056 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6057 | * char is 'b'. |
| 6058 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6059 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6060 | 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] | 6061 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6062 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6063 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6064 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6065 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6066 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6067 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6068 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6069 | return 0; |
| 6070 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 6071 | if (!SSL_get_cipher_bits(conn->xprt_ctx, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6072 | return 0; |
| 6073 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6074 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6075 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6076 | |
| 6077 | return 1; |
| 6078 | } |
| 6079 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6080 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 6081 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6082 | * char is 'b'. |
| 6083 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6084 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6085 | 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] | 6086 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6087 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6088 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6089 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6090 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6091 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6092 | return 0; |
| 6093 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6094 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
| 6095 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6096 | return 0; |
| 6097 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6098 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6099 | |
| 6100 | return 1; |
| 6101 | } |
| 6102 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6103 | #ifdef OPENSSL_NPN_NEGOTIATED |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6104 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6105 | 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] | 6106 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6107 | struct connection *conn; |
| 6108 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6109 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6110 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6111 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6112 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6113 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6114 | return 0; |
| 6115 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6116 | smp->data.u.str.str = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6117 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6118 | (const unsigned char **)&smp->data.u.str.str, (unsigned *)&smp->data.u.str.len); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6119 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6120 | if (!smp->data.u.str.str) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6121 | return 0; |
| 6122 | |
| 6123 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6124 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6125 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6126 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6127 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6128 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6129 | 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] | 6130 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6131 | struct connection *conn; |
| 6132 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6133 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6134 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6135 | |
Willy Tarreau | e26bf05 | 2015-05-12 10:30:12 +0200 | [diff] [blame] | 6136 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6137 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6138 | return 0; |
| 6139 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6140 | smp->data.u.str.str = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6141 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6142 | (const unsigned char **)&smp->data.u.str.str, (unsigned *)&smp->data.u.str.len); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6143 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6144 | if (!smp->data.u.str.str) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6145 | return 0; |
| 6146 | |
| 6147 | return 1; |
| 6148 | } |
| 6149 | #endif |
| 6150 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6151 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 6152 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6153 | * char is 'b'. |
| 6154 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6155 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6156 | 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] | 6157 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6158 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6159 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6160 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6161 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6162 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6163 | return 0; |
| 6164 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6165 | smp->data.u.str.str = (char *)SSL_get_version(conn->xprt_ctx); |
| 6166 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6167 | return 0; |
| 6168 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6169 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6170 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6171 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6172 | |
| 6173 | return 1; |
| 6174 | } |
| 6175 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6176 | /* 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] | 6177 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6178 | * char is 'b'. |
| 6179 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6180 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6181 | 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] | 6182 | { |
| 6183 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6184 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6185 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6186 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6187 | SSL_SESSION *ssl_sess; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6188 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6189 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6190 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6191 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6192 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6193 | return 0; |
| 6194 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6195 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 6196 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6197 | return 0; |
| 6198 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6199 | smp->data.u.str.str = (char *)SSL_SESSION_get_id(ssl_sess, (unsigned int *)&smp->data.u.str.len); |
| 6200 | if (!smp->data.u.str.str || !smp->data.u.str.len) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6201 | return 0; |
| 6202 | |
| 6203 | return 1; |
| 6204 | #else |
| 6205 | return 0; |
| 6206 | #endif |
| 6207 | } |
| 6208 | |
| 6209 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6210 | 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] | 6211 | { |
| 6212 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6213 | struct connection *conn; |
| 6214 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6215 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6216 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6217 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6218 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6219 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6220 | return 0; |
| 6221 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6222 | smp->data.u.str.str = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 6223 | if (!smp->data.u.str.str) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 6224 | return 0; |
| 6225 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6226 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6227 | return 1; |
| 6228 | #else |
| 6229 | return 0; |
| 6230 | #endif |
| 6231 | } |
| 6232 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6233 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6234 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6235 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6236 | struct connection *conn; |
| 6237 | struct ssl_capture *capture; |
| 6238 | |
| 6239 | conn = objt_conn(smp->sess->origin); |
| 6240 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6241 | return 0; |
| 6242 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6243 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6244 | if (!capture) |
| 6245 | return 0; |
| 6246 | |
| 6247 | smp->flags = SMP_F_CONST; |
| 6248 | smp->data.type = SMP_T_BIN; |
| 6249 | smp->data.u.str.str = capture->ciphersuite; |
| 6250 | smp->data.u.str.len = capture->ciphersuite_len; |
| 6251 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6252 | } |
| 6253 | |
| 6254 | static int |
| 6255 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6256 | { |
| 6257 | struct chunk *data; |
| 6258 | |
| 6259 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 6260 | return 0; |
| 6261 | |
| 6262 | data = get_trash_chunk(); |
| 6263 | dump_binary(data, smp->data.u.str.str, smp->data.u.str.len); |
| 6264 | smp->data.type = SMP_T_BIN; |
| 6265 | smp->data.u.str = *data; |
| 6266 | return 1; |
| 6267 | } |
| 6268 | |
| 6269 | static int |
| 6270 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6271 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6272 | struct connection *conn; |
| 6273 | struct ssl_capture *capture; |
| 6274 | |
| 6275 | conn = objt_conn(smp->sess->origin); |
| 6276 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6277 | return 0; |
| 6278 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6279 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6280 | if (!capture) |
| 6281 | return 0; |
| 6282 | |
| 6283 | smp->data.type = SMP_T_SINT; |
| 6284 | smp->data.u.sint = capture->xxh64; |
| 6285 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6286 | } |
| 6287 | |
| 6288 | static int |
| 6289 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6290 | { |
| 6291 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && !defined(OPENSSL_NO_SSL_TRACE) |
| 6292 | struct chunk *data; |
| 6293 | SSL_CIPHER cipher; |
| 6294 | int i; |
| 6295 | const char *str; |
| 6296 | unsigned char *bin; |
| 6297 | |
| 6298 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 6299 | return 0; |
| 6300 | |
| 6301 | /* The cipher algorith must not be SSL_SSLV2, because this |
| 6302 | * SSL version seems to not have the same cipher encoding, |
| 6303 | * and it is not supported by OpenSSL. Unfortunately, the |
| 6304 | * #define SSL_SSLV2, SSL_SSLV3 and others are not available |
| 6305 | * with standard defines. We just set the variable to 0, |
| 6306 | * ensure that the match with SSL_SSLV2 fails. |
| 6307 | */ |
| 6308 | cipher.algorithm_ssl = 0; |
| 6309 | |
| 6310 | data = get_trash_chunk(); |
| 6311 | for (i = 0; i + 1 < smp->data.u.str.len; i += 2) { |
| 6312 | bin = (unsigned char *)smp->data.u.str.str + i; |
| 6313 | cipher.id = (unsigned int)(bin[0] << 8) | bin[1]; |
| 6314 | str = SSL_CIPHER_standard_name(&cipher); |
| 6315 | if (!str || strcmp(str, "UNKNOWN") == 0) |
| 6316 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", (unsigned int)cipher.id); |
| 6317 | else |
| 6318 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 6319 | } |
| 6320 | smp->data.type = SMP_T_STR; |
| 6321 | smp->data.u.str = *data; |
| 6322 | return 1; |
| 6323 | #else |
| 6324 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 6325 | #endif |
| 6326 | } |
| 6327 | |
| 6328 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6329 | 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] | 6330 | { |
| 6331 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6332 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6333 | smp->strm ? smp->strm->si[1].end : NULL); |
| 6334 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6335 | int finished_len; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6336 | struct chunk *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6337 | |
| 6338 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6339 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6340 | return 0; |
| 6341 | |
| 6342 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 6343 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6344 | return 0; |
| 6345 | } |
| 6346 | |
| 6347 | finished_trash = get_trash_chunk(); |
| 6348 | if (!SSL_session_reused(conn->xprt_ctx)) |
| 6349 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 6350 | else |
| 6351 | finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 6352 | |
| 6353 | if (!finished_len) |
| 6354 | return 0; |
| 6355 | |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 6356 | finished_trash->len = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6357 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6358 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6359 | |
| 6360 | return 1; |
| 6361 | #else |
| 6362 | return 0; |
| 6363 | #endif |
| 6364 | } |
| 6365 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6366 | /* 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] | 6367 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6368 | 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] | 6369 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6370 | struct connection *conn; |
| 6371 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6372 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6373 | if (!conn || conn->xprt != &ssl_sock) |
| 6374 | return 0; |
| 6375 | |
| 6376 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6377 | smp->flags = SMP_F_MAY_CHANGE; |
| 6378 | return 0; |
| 6379 | } |
| 6380 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6381 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6382 | 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] | 6383 | smp->flags = 0; |
| 6384 | |
| 6385 | return 1; |
| 6386 | } |
| 6387 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6388 | /* 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] | 6389 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6390 | 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] | 6391 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6392 | struct connection *conn; |
| 6393 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6394 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6395 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6396 | return 0; |
| 6397 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6398 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6399 | smp->flags = SMP_F_MAY_CHANGE; |
| 6400 | return 0; |
| 6401 | } |
| 6402 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6403 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6404 | 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] | 6405 | smp->flags = 0; |
| 6406 | |
| 6407 | return 1; |
| 6408 | } |
| 6409 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6410 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6411 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6412 | 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] | 6413 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6414 | struct connection *conn; |
| 6415 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6416 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6417 | if (!conn || conn->xprt != &ssl_sock) |
| 6418 | return 0; |
| 6419 | |
| 6420 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6421 | smp->flags = SMP_F_MAY_CHANGE; |
| 6422 | return 0; |
| 6423 | } |
| 6424 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6425 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6426 | 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] | 6427 | smp->flags = 0; |
| 6428 | |
| 6429 | return 1; |
| 6430 | } |
| 6431 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6432 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 6433 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6434 | 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] | 6435 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6436 | struct connection *conn; |
| 6437 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6438 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6439 | if (!conn || conn->xprt != &ssl_sock) |
| 6440 | return 0; |
| 6441 | |
| 6442 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 6443 | smp->flags = SMP_F_MAY_CHANGE; |
| 6444 | return 0; |
| 6445 | } |
| 6446 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6447 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 6448 | return 0; |
| 6449 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6450 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6451 | 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] | 6452 | smp->flags = 0; |
| 6453 | |
| 6454 | return 1; |
| 6455 | } |
| 6456 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 6457 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6458 | 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] | 6459 | { |
| 6460 | if (!*args[cur_arg + 1]) { |
| 6461 | if (err) |
| 6462 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 6463 | return ERR_ALERT | ERR_FATAL; |
| 6464 | } |
| 6465 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6466 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6467 | 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] | 6468 | else |
| 6469 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 6470 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6471 | return 0; |
| 6472 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6473 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6474 | { |
| 6475 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 6476 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6477 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6478 | /* parse the "ca-sign-file" bind keyword */ |
| 6479 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6480 | { |
| 6481 | if (!*args[cur_arg + 1]) { |
| 6482 | if (err) |
| 6483 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 6484 | return ERR_ALERT | ERR_FATAL; |
| 6485 | } |
| 6486 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6487 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6488 | 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] | 6489 | else |
| 6490 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 6491 | |
| 6492 | return 0; |
| 6493 | } |
| 6494 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 6495 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6496 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6497 | { |
| 6498 | if (!*args[cur_arg + 1]) { |
| 6499 | if (err) |
| 6500 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 6501 | return ERR_ALERT | ERR_FATAL; |
| 6502 | } |
| 6503 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 6504 | return 0; |
| 6505 | } |
| 6506 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6507 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6508 | 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] | 6509 | { |
| 6510 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 6511 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6512 | return ERR_ALERT | ERR_FATAL; |
| 6513 | } |
| 6514 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 6515 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6516 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6517 | return 0; |
| 6518 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6519 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6520 | { |
| 6521 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 6522 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6523 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6524 | 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] | 6525 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 6526 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 6527 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6528 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 6529 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6530 | return ERR_ALERT | ERR_FATAL; |
| 6531 | } |
| 6532 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6533 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 6534 | 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] | 6535 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 6536 | return ERR_ALERT | ERR_FATAL; |
| 6537 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6538 | 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] | 6539 | if (ssl_sock_load_cert(path, conf, err) > 0) |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 6540 | return ERR_ALERT | ERR_FATAL; |
| 6541 | |
| 6542 | return 0; |
| 6543 | } |
| 6544 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6545 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6546 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6547 | |
| 6548 | return 0; |
| 6549 | } |
| 6550 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 6551 | /* parse the "crt-list" bind keyword */ |
| 6552 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6553 | { |
| 6554 | if (!*args[cur_arg + 1]) { |
| 6555 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 6556 | return ERR_ALERT | ERR_FATAL; |
| 6557 | } |
| 6558 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6559 | 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] | 6560 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 6561 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 6562 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 6563 | |
| 6564 | return 0; |
| 6565 | } |
| 6566 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 6567 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6568 | 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] | 6569 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 6570 | #ifndef X509_V_FLAG_CRL_CHECK |
| 6571 | if (err) |
| 6572 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 6573 | return ERR_ALERT | ERR_FATAL; |
| 6574 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6575 | if (!*args[cur_arg + 1]) { |
| 6576 | if (err) |
| 6577 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 6578 | return ERR_ALERT | ERR_FATAL; |
| 6579 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6580 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6581 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6582 | 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] | 6583 | else |
| 6584 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 6585 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6586 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 6587 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6588 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6589 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6590 | { |
| 6591 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 6592 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6593 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 6594 | /* parse the "curves" bind keyword keyword */ |
| 6595 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 6596 | { |
| 6597 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 6598 | if (!*args[cur_arg + 1]) { |
| 6599 | if (err) |
| 6600 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 6601 | return ERR_ALERT | ERR_FATAL; |
| 6602 | } |
| 6603 | conf->curves = strdup(args[cur_arg + 1]); |
| 6604 | return 0; |
| 6605 | #else |
| 6606 | if (err) |
| 6607 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 6608 | return ERR_ALERT | ERR_FATAL; |
| 6609 | #endif |
| 6610 | } |
| 6611 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6612 | { |
| 6613 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 6614 | } |
| 6615 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 6616 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6617 | 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] | 6618 | { |
| 6619 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 6620 | if (err) |
| 6621 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 6622 | return ERR_ALERT | ERR_FATAL; |
| 6623 | #elif defined(OPENSSL_NO_ECDH) |
| 6624 | if (err) |
| 6625 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 6626 | return ERR_ALERT | ERR_FATAL; |
| 6627 | #else |
| 6628 | if (!*args[cur_arg + 1]) { |
| 6629 | if (err) |
| 6630 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 6631 | return ERR_ALERT | ERR_FATAL; |
| 6632 | } |
| 6633 | |
| 6634 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6635 | |
| 6636 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6637 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6638 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6639 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6640 | { |
| 6641 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 6642 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6643 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 6644 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 6645 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6646 | { |
| 6647 | int code; |
| 6648 | char *p = args[cur_arg + 1]; |
| 6649 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 6650 | |
| 6651 | if (!*p) { |
| 6652 | if (err) |
| 6653 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 6654 | return ERR_ALERT | ERR_FATAL; |
| 6655 | } |
| 6656 | |
| 6657 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 6658 | ignerr = &conf->ca_ignerr; |
| 6659 | |
| 6660 | if (strcmp(p, "all") == 0) { |
| 6661 | *ignerr = ~0ULL; |
| 6662 | return 0; |
| 6663 | } |
| 6664 | |
| 6665 | while (p) { |
| 6666 | code = atoi(p); |
| 6667 | if ((code <= 0) || (code > 63)) { |
| 6668 | if (err) |
| 6669 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 6670 | args[cur_arg], code, args[cur_arg + 1]); |
| 6671 | return ERR_ALERT | ERR_FATAL; |
| 6672 | } |
| 6673 | *ignerr |= 1ULL << code; |
| 6674 | p = strchr(p, ','); |
| 6675 | if (p) |
| 6676 | p++; |
| 6677 | } |
| 6678 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6679 | return 0; |
| 6680 | } |
| 6681 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6682 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 6683 | 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] | 6684 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6685 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6686 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6687 | p = strchr(arg, '-'); |
| 6688 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6689 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6690 | p++; |
| 6691 | if (!strcmp(p, "sslv3")) |
| 6692 | v = CONF_SSLV3; |
| 6693 | else if (!strcmp(p, "tlsv10")) |
| 6694 | v = CONF_TLSV10; |
| 6695 | else if (!strcmp(p, "tlsv11")) |
| 6696 | v = CONF_TLSV11; |
| 6697 | else if (!strcmp(p, "tlsv12")) |
| 6698 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 6699 | else if (!strcmp(p, "tlsv13")) |
| 6700 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6701 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6702 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6703 | if (!strncmp(arg, "no-", 3)) |
| 6704 | methods->flags |= methodVersions[v].flag; |
| 6705 | else if (!strncmp(arg, "force-", 6)) |
| 6706 | methods->min = methods->max = v; |
| 6707 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6708 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 6709 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6710 | fail: |
| 6711 | if (err) |
| 6712 | memprintf(err, "'%s' : option not implemented", arg); |
| 6713 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 6714 | } |
| 6715 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6716 | 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] | 6717 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6718 | return parse_tls_method_options(args[cur_arg], &conf->ssl_methods, err); |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6719 | } |
| 6720 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6721 | 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] | 6722 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6723 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 6724 | } |
| 6725 | |
| 6726 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 6727 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 6728 | { |
| 6729 | uint16_t i, v = 0; |
| 6730 | char *argv = args[cur_arg + 1]; |
| 6731 | if (!*argv) { |
| 6732 | if (err) |
| 6733 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
| 6734 | return ERR_ALERT | ERR_FATAL; |
| 6735 | } |
| 6736 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 6737 | if (!strcmp(argv, methodVersions[i].name)) |
| 6738 | v = i; |
| 6739 | if (!v) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6740 | if (err) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6741 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6742 | return ERR_ALERT | ERR_FATAL; |
| 6743 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6744 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 6745 | methods->min = v; |
| 6746 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 6747 | methods->max = v; |
| 6748 | else { |
| 6749 | if (err) |
| 6750 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
| 6751 | return ERR_ALERT | ERR_FATAL; |
| 6752 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6753 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6754 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6755 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 6756 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 6757 | { |
| 6758 | #if !defined(OPENSSL_IS_BORINGSSL) |
| 6759 | Warning("crt-list: ssl-min-ver and ssl-max-ver are not supported with this Openssl version (skipped).\n"); |
| 6760 | #endif |
| 6761 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 6762 | } |
| 6763 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6764 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6765 | { |
| 6766 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 6767 | } |
| 6768 | |
| 6769 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6770 | { |
| 6771 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 6772 | } |
| 6773 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 6774 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6775 | 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] | 6776 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 6777 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 6778 | return 0; |
| 6779 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 6780 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6781 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6782 | 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] | 6783 | { |
| 6784 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 6785 | char *p1, *p2; |
| 6786 | |
| 6787 | if (!*args[cur_arg + 1]) { |
| 6788 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 6789 | return ERR_ALERT | ERR_FATAL; |
| 6790 | } |
| 6791 | |
| 6792 | free(conf->npn_str); |
| 6793 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 6794 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 6795 | * so we reuse each comma to store the next <len> and need |
| 6796 | * one more for the end of the string. |
| 6797 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6798 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 6799 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6800 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 6801 | |
| 6802 | /* replace commas with the name length */ |
| 6803 | p1 = conf->npn_str; |
| 6804 | p2 = p1 + 1; |
| 6805 | while (1) { |
| 6806 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 6807 | if (!p2) |
| 6808 | p2 = p1 + 1 + strlen(p1 + 1); |
| 6809 | |
| 6810 | if (p2 - (p1 + 1) > 255) { |
| 6811 | *p2 = '\0'; |
| 6812 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 6813 | return ERR_ALERT | ERR_FATAL; |
| 6814 | } |
| 6815 | |
| 6816 | *p1 = p2 - (p1 + 1); |
| 6817 | p1 = p2; |
| 6818 | |
| 6819 | if (!*p2) |
| 6820 | break; |
| 6821 | |
| 6822 | *(p2++) = '\0'; |
| 6823 | } |
| 6824 | return 0; |
| 6825 | #else |
| 6826 | if (err) |
| 6827 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 6828 | return ERR_ALERT | ERR_FATAL; |
| 6829 | #endif |
| 6830 | } |
| 6831 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6832 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6833 | { |
| 6834 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 6835 | } |
| 6836 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6837 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6838 | 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] | 6839 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6840 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6841 | char *p1, *p2; |
| 6842 | |
| 6843 | if (!*args[cur_arg + 1]) { |
| 6844 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 6845 | return ERR_ALERT | ERR_FATAL; |
| 6846 | } |
| 6847 | |
| 6848 | free(conf->alpn_str); |
| 6849 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 6850 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 6851 | * so we reuse each comma to store the next <len> and need |
| 6852 | * one more for the end of the string. |
| 6853 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6854 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 6855 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6856 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 6857 | |
| 6858 | /* replace commas with the name length */ |
| 6859 | p1 = conf->alpn_str; |
| 6860 | p2 = p1 + 1; |
| 6861 | while (1) { |
| 6862 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 6863 | if (!p2) |
| 6864 | p2 = p1 + 1 + strlen(p1 + 1); |
| 6865 | |
| 6866 | if (p2 - (p1 + 1) > 255) { |
| 6867 | *p2 = '\0'; |
| 6868 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 6869 | return ERR_ALERT | ERR_FATAL; |
| 6870 | } |
| 6871 | |
| 6872 | *p1 = p2 - (p1 + 1); |
| 6873 | p1 = p2; |
| 6874 | |
| 6875 | if (!*p2) |
| 6876 | break; |
| 6877 | |
| 6878 | *(p2++) = '\0'; |
| 6879 | } |
| 6880 | return 0; |
| 6881 | #else |
| 6882 | if (err) |
| 6883 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 6884 | return ERR_ALERT | ERR_FATAL; |
| 6885 | #endif |
| 6886 | } |
| 6887 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6888 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6889 | { |
| 6890 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 6891 | } |
| 6892 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6893 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6894 | 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] | 6895 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 6896 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6897 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 6898 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6899 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 6900 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6901 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6902 | conf->ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 6903 | if (!conf->ssl_methods.min) |
| 6904 | conf->ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 6905 | if (!conf->ssl_methods.max) |
| 6906 | conf->ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 6907 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6908 | return 0; |
| 6909 | } |
| 6910 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 6911 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 6912 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6913 | { |
| 6914 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 6915 | return 0; |
| 6916 | } |
| 6917 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6918 | /* parse the "generate-certificates" bind keyword */ |
| 6919 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6920 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6921 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6922 | conf->generate_certs = 1; |
| 6923 | #else |
| 6924 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 6925 | err && *err ? *err : ""); |
| 6926 | #endif |
| 6927 | return 0; |
| 6928 | } |
| 6929 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 6930 | /* parse the "strict-sni" bind keyword */ |
| 6931 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6932 | { |
| 6933 | conf->strict_sni = 1; |
| 6934 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6935 | } |
| 6936 | |
| 6937 | /* parse the "tls-ticket-keys" bind keyword */ |
| 6938 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6939 | { |
| 6940 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6941 | FILE *f; |
| 6942 | int i = 0; |
| 6943 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6944 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6945 | |
| 6946 | if (!*args[cur_arg + 1]) { |
| 6947 | if (err) |
| 6948 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 6949 | return ERR_ALERT | ERR_FATAL; |
| 6950 | } |
| 6951 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 6952 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
| 6953 | if(keys_ref) { |
| 6954 | conf->keys_ref = keys_ref; |
| 6955 | return 0; |
| 6956 | } |
| 6957 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 6958 | keys_ref = malloc(sizeof(*keys_ref)); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6959 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct tls_sess_key)); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6960 | |
| 6961 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 6962 | if (err) |
| 6963 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 6964 | return ERR_ALERT | ERR_FATAL; |
| 6965 | } |
| 6966 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6967 | keys_ref->filename = strdup(args[cur_arg + 1]); |
| 6968 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6969 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 6970 | int len = strlen(thisline); |
| 6971 | /* Strip newline characters from the end */ |
| 6972 | if(thisline[len - 1] == '\n') |
| 6973 | thisline[--len] = 0; |
| 6974 | |
| 6975 | if(thisline[len - 1] == '\r') |
| 6976 | thisline[--len] = 0; |
| 6977 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6978 | if (base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(struct tls_sess_key)) != sizeof(struct tls_sess_key)) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6979 | if (err) |
| 6980 | 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] | 6981 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6982 | return ERR_ALERT | ERR_FATAL; |
| 6983 | } |
| 6984 | i++; |
| 6985 | } |
| 6986 | |
| 6987 | if (i < TLS_TICKETS_NO) { |
| 6988 | if (err) |
| 6989 | 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] | 6990 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6991 | return ERR_ALERT | ERR_FATAL; |
| 6992 | } |
| 6993 | |
| 6994 | fclose(f); |
| 6995 | |
| 6996 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 6997 | i -= 2; |
| 6998 | 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] | 6999 | keys_ref->unique_id = -1; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7000 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7001 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7002 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 7003 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7004 | return 0; |
| 7005 | #else |
| 7006 | if (err) |
| 7007 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 7008 | return ERR_ALERT | ERR_FATAL; |
| 7009 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 7010 | } |
| 7011 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7012 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7013 | 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] | 7014 | { |
| 7015 | if (!*args[cur_arg + 1]) { |
| 7016 | if (err) |
| 7017 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 7018 | return ERR_ALERT | ERR_FATAL; |
| 7019 | } |
| 7020 | |
| 7021 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7022 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7023 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7024 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7025 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7026 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7027 | else { |
| 7028 | if (err) |
| 7029 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 7030 | args[cur_arg], args[cur_arg + 1]); |
| 7031 | return ERR_ALERT | ERR_FATAL; |
| 7032 | } |
| 7033 | |
| 7034 | return 0; |
| 7035 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7036 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7037 | { |
| 7038 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 7039 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7040 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7041 | /************** "server" keywords ****************/ |
| 7042 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7043 | /* parse the "ca-file" server keyword */ |
| 7044 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7045 | { |
| 7046 | if (!*args[*cur_arg + 1]) { |
| 7047 | if (err) |
| 7048 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 7049 | return ERR_ALERT | ERR_FATAL; |
| 7050 | } |
| 7051 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7052 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7053 | 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] | 7054 | else |
| 7055 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 7056 | |
| 7057 | return 0; |
| 7058 | } |
| 7059 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7060 | /* parse the "check-ssl" server keyword */ |
| 7061 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7062 | { |
| 7063 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7064 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 7065 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
| 7066 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7067 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 7068 | if (!newsrv->ssl_ctx.methods.min) |
| 7069 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 7070 | if (!newsrv->ssl_ctx.methods.max) |
| 7071 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 7072 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7073 | return 0; |
| 7074 | } |
| 7075 | |
| 7076 | /* parse the "ciphers" server keyword */ |
| 7077 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7078 | { |
| 7079 | if (!*args[*cur_arg + 1]) { |
| 7080 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 7081 | return ERR_ALERT | ERR_FATAL; |
| 7082 | } |
| 7083 | |
| 7084 | free(newsrv->ssl_ctx.ciphers); |
| 7085 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 7086 | return 0; |
| 7087 | } |
| 7088 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7089 | /* parse the "crl-file" server keyword */ |
| 7090 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7091 | { |
| 7092 | #ifndef X509_V_FLAG_CRL_CHECK |
| 7093 | if (err) |
| 7094 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 7095 | return ERR_ALERT | ERR_FATAL; |
| 7096 | #else |
| 7097 | if (!*args[*cur_arg + 1]) { |
| 7098 | if (err) |
| 7099 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 7100 | return ERR_ALERT | ERR_FATAL; |
| 7101 | } |
| 7102 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7103 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7104 | 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] | 7105 | else |
| 7106 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 7107 | |
| 7108 | return 0; |
| 7109 | #endif |
| 7110 | } |
| 7111 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 7112 | /* parse the "crt" server keyword */ |
| 7113 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7114 | { |
| 7115 | if (!*args[*cur_arg + 1]) { |
| 7116 | if (err) |
| 7117 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 7118 | return ERR_ALERT | ERR_FATAL; |
| 7119 | } |
| 7120 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7121 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
| 7122 | memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 7123 | else |
| 7124 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 7125 | |
| 7126 | return 0; |
| 7127 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7128 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 7129 | /* parse the "no-check-ssl" server keyword */ |
| 7130 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7131 | { |
| 7132 | newsrv->check.use_ssl = 0; |
| 7133 | free(newsrv->ssl_ctx.ciphers); |
| 7134 | newsrv->ssl_ctx.ciphers = NULL; |
| 7135 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 7136 | return 0; |
| 7137 | } |
| 7138 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 7139 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 7140 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7141 | { |
| 7142 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 7143 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 7144 | return 0; |
| 7145 | } |
| 7146 | |
| 7147 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 7148 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7149 | { |
| 7150 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 7151 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 7152 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 7153 | return 0; |
| 7154 | } |
| 7155 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 7156 | /* parse the "no-ssl" server keyword */ |
| 7157 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7158 | { |
| 7159 | newsrv->use_ssl = 0; |
| 7160 | free(newsrv->ssl_ctx.ciphers); |
| 7161 | newsrv->ssl_ctx.ciphers = NULL; |
| 7162 | return 0; |
| 7163 | } |
| 7164 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 7165 | /* parse the "no-ssl-reuse" server keyword */ |
| 7166 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7167 | { |
| 7168 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 7169 | return 0; |
| 7170 | } |
| 7171 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7172 | /* parse the "no-tls-tickets" server keyword */ |
| 7173 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7174 | { |
| 7175 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 7176 | return 0; |
| 7177 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7178 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 7179 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7180 | { |
| 7181 | newsrv->pp_opts |= SRV_PP_V2; |
| 7182 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 7183 | return 0; |
| 7184 | } |
| 7185 | |
| 7186 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 7187 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7188 | { |
| 7189 | newsrv->pp_opts |= SRV_PP_V2; |
| 7190 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 7191 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 7192 | return 0; |
| 7193 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7194 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7195 | /* parse the "sni" server keyword */ |
| 7196 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7197 | { |
| 7198 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7199 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 7200 | return ERR_ALERT | ERR_FATAL; |
| 7201 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7202 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7203 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7204 | arg = args[*cur_arg + 1]; |
| 7205 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7206 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 7207 | return ERR_ALERT | ERR_FATAL; |
| 7208 | } |
| 7209 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7210 | free(newsrv->sni_expr); |
| 7211 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7212 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7213 | return 0; |
| 7214 | #endif |
| 7215 | } |
| 7216 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7217 | /* parse the "ssl" server keyword */ |
| 7218 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7219 | { |
| 7220 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7221 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 7222 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7223 | return 0; |
| 7224 | } |
| 7225 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 7226 | /* parse the "ssl-reuse" server keyword */ |
| 7227 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7228 | { |
| 7229 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 7230 | return 0; |
| 7231 | } |
| 7232 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 7233 | /* parse the "tls-tickets" server keyword */ |
| 7234 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7235 | { |
| 7236 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 7237 | return 0; |
| 7238 | } |
| 7239 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7240 | /* parse the "verify" server keyword */ |
| 7241 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7242 | { |
| 7243 | if (!*args[*cur_arg + 1]) { |
| 7244 | if (err) |
| 7245 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 7246 | return ERR_ALERT | ERR_FATAL; |
| 7247 | } |
| 7248 | |
| 7249 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7250 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7251 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7252 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7253 | else { |
| 7254 | if (err) |
| 7255 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 7256 | args[*cur_arg], args[*cur_arg + 1]); |
| 7257 | return ERR_ALERT | ERR_FATAL; |
| 7258 | } |
| 7259 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 7260 | return 0; |
| 7261 | } |
| 7262 | |
| 7263 | /* parse the "verifyhost" server keyword */ |
| 7264 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7265 | { |
| 7266 | if (!*args[*cur_arg + 1]) { |
| 7267 | if (err) |
| 7268 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 7269 | return ERR_ALERT | ERR_FATAL; |
| 7270 | } |
| 7271 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 7272 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 7273 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 7274 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7275 | return 0; |
| 7276 | } |
| 7277 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7278 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 7279 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 7280 | struct proxy *defpx, const char *file, int line, |
| 7281 | char **err) { |
| 7282 | int i = 1; |
| 7283 | |
| 7284 | if (*(args[i]) == 0) { |
| 7285 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 7286 | return -1; |
| 7287 | } |
| 7288 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7289 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7290 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 7291 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 7292 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7293 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 7294 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 7295 | i++; |
| 7296 | else { |
| 7297 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 7298 | return -1; |
| 7299 | } |
| 7300 | } |
| 7301 | 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] | 7302 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 7303 | return -1; |
| 7304 | } |
| 7305 | i++; |
| 7306 | } |
| 7307 | return 0; |
| 7308 | } |
| 7309 | |
| 7310 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 7311 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 7312 | struct proxy *defpx, const char *file, int line, |
| 7313 | char **err) { |
| 7314 | int i = 1; |
| 7315 | |
| 7316 | if (*(args[i]) == 0) { |
| 7317 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 7318 | return -1; |
| 7319 | } |
| 7320 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7321 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7322 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7323 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 7324 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 7325 | i++; |
| 7326 | else { |
| 7327 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 7328 | return -1; |
| 7329 | } |
| 7330 | } |
| 7331 | 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] | 7332 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 7333 | return -1; |
| 7334 | } |
| 7335 | i++; |
| 7336 | } |
| 7337 | return 0; |
| 7338 | } |
| 7339 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 7340 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 7341 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7342 | */ |
| 7343 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 7344 | struct proxy *defpx, const char *file, int line, |
| 7345 | char **err) |
| 7346 | { |
| 7347 | char **target; |
| 7348 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7349 | 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] | 7350 | |
| 7351 | if (too_many_args(1, args, err, NULL)) |
| 7352 | return -1; |
| 7353 | |
| 7354 | if (*target) { |
| 7355 | memprintf(err, "'%s' already specified.", args[0]); |
| 7356 | return -1; |
| 7357 | } |
| 7358 | |
| 7359 | if (*(args[1]) == 0) { |
| 7360 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 7361 | return -1; |
| 7362 | } |
| 7363 | *target = strdup(args[1]); |
| 7364 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7365 | } |
| 7366 | |
| 7367 | /* parse the "ssl-mode-async" keyword in global section. |
| 7368 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7369 | */ |
| 7370 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 7371 | struct proxy *defpx, const char *file, int line, |
| 7372 | char **err) |
| 7373 | { |
| 7374 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 7375 | global_ssl.async = 1; |
| 7376 | return 0; |
| 7377 | #else |
| 7378 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 7379 | return -1; |
| 7380 | #endif |
| 7381 | } |
| 7382 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7383 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7384 | static int ssl_check_async_engine_count(void) { |
| 7385 | int err_code = 0; |
| 7386 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7387 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
| 7388 | Alert("ssl-mode-async only supports a maximum of 32 engines.\n"); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7389 | err_code = ERR_ABORT; |
| 7390 | } |
| 7391 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 7392 | } |
| 7393 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7394 | /* parse the "ssl-engine" keyword in global section. |
| 7395 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7396 | */ |
| 7397 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 7398 | struct proxy *defpx, const char *file, int line, |
| 7399 | char **err) |
| 7400 | { |
| 7401 | char *algo; |
| 7402 | int ret = -1; |
| 7403 | |
| 7404 | if (*(args[1]) == 0) { |
| 7405 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 7406 | return ret; |
| 7407 | } |
| 7408 | |
| 7409 | if (*(args[2]) == 0) { |
| 7410 | /* if no list of algorithms is given, it defaults to ALL */ |
| 7411 | algo = strdup("ALL"); |
| 7412 | goto add_engine; |
| 7413 | } |
| 7414 | |
| 7415 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 7416 | if (strcmp(args[2], "algo") != 0) { |
| 7417 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 7418 | return ret; |
| 7419 | } |
| 7420 | |
| 7421 | if (*(args[3]) == 0) { |
| 7422 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 7423 | return ret; |
| 7424 | } |
| 7425 | algo = strdup(args[3]); |
| 7426 | |
| 7427 | add_engine: |
| 7428 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 7429 | openssl_engines_initialized++; |
| 7430 | ret = 0; |
| 7431 | } |
| 7432 | free(algo); |
| 7433 | return ret; |
| 7434 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7435 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7436 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 7437 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 7438 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 7439 | */ |
| 7440 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 7441 | struct proxy *defpx, const char *file, int line, |
| 7442 | char **err) |
| 7443 | { |
| 7444 | char **target; |
| 7445 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7446 | 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] | 7447 | |
| 7448 | if (too_many_args(1, args, err, NULL)) |
| 7449 | return -1; |
| 7450 | |
| 7451 | if (*(args[1]) == 0) { |
| 7452 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 7453 | return -1; |
| 7454 | } |
| 7455 | |
| 7456 | free(*target); |
| 7457 | *target = strdup(args[1]); |
| 7458 | return 0; |
| 7459 | } |
| 7460 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7461 | /* parse various global tune.ssl settings consisting in positive integers. |
| 7462 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7463 | */ |
| 7464 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 7465 | struct proxy *defpx, const char *file, int line, |
| 7466 | char **err) |
| 7467 | { |
| 7468 | int *target; |
| 7469 | |
| 7470 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 7471 | target = &global.tune.sslcachesize; |
| 7472 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7473 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7474 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7475 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 7476 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 7477 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7478 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 7479 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7480 | else { |
| 7481 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 7482 | return -1; |
| 7483 | } |
| 7484 | |
| 7485 | if (too_many_args(1, args, err, NULL)) |
| 7486 | return -1; |
| 7487 | |
| 7488 | if (*(args[1]) == 0) { |
| 7489 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 7490 | return -1; |
| 7491 | } |
| 7492 | |
| 7493 | *target = atoi(args[1]); |
| 7494 | if (*target < 0) { |
| 7495 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 7496 | return -1; |
| 7497 | } |
| 7498 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7499 | } |
| 7500 | |
| 7501 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 7502 | struct proxy *defpx, const char *file, int line, |
| 7503 | char **err) |
| 7504 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7505 | int ret; |
| 7506 | |
| 7507 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 7508 | if (ret != 0) |
| 7509 | return ret; |
| 7510 | |
| 7511 | if (pool2_ssl_capture) { |
| 7512 | memprintf(err, "'%s' is already configured.", args[0]); |
| 7513 | return -1; |
| 7514 | } |
| 7515 | |
| 7516 | pool2_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 7517 | if (!pool2_ssl_capture) { |
| 7518 | memprintf(err, "Out of memory error."); |
| 7519 | return -1; |
| 7520 | } |
| 7521 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7522 | } |
| 7523 | |
| 7524 | /* parse "ssl.force-private-cache". |
| 7525 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7526 | */ |
| 7527 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 7528 | struct proxy *defpx, const char *file, int line, |
| 7529 | char **err) |
| 7530 | { |
| 7531 | if (too_many_args(0, args, err, NULL)) |
| 7532 | return -1; |
| 7533 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7534 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7535 | return 0; |
| 7536 | } |
| 7537 | |
| 7538 | /* parse "ssl.lifetime". |
| 7539 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7540 | */ |
| 7541 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 7542 | struct proxy *defpx, const char *file, int line, |
| 7543 | char **err) |
| 7544 | { |
| 7545 | const char *res; |
| 7546 | |
| 7547 | if (too_many_args(1, args, err, NULL)) |
| 7548 | return -1; |
| 7549 | |
| 7550 | if (*(args[1]) == 0) { |
| 7551 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 7552 | return -1; |
| 7553 | } |
| 7554 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7555 | 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] | 7556 | if (res) { |
| 7557 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 7558 | return -1; |
| 7559 | } |
| 7560 | return 0; |
| 7561 | } |
| 7562 | |
| 7563 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 7564 | /* parse "ssl-dh-param-file". |
| 7565 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7566 | */ |
| 7567 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 7568 | struct proxy *defpx, const char *file, int line, |
| 7569 | char **err) |
| 7570 | { |
| 7571 | if (too_many_args(1, args, err, NULL)) |
| 7572 | return -1; |
| 7573 | |
| 7574 | if (*(args[1]) == 0) { |
| 7575 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 7576 | return -1; |
| 7577 | } |
| 7578 | |
| 7579 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 7580 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 7581 | return -1; |
| 7582 | } |
| 7583 | return 0; |
| 7584 | } |
| 7585 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7586 | /* parse "ssl.default-dh-param". |
| 7587 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7588 | */ |
| 7589 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 7590 | struct proxy *defpx, const char *file, int line, |
| 7591 | char **err) |
| 7592 | { |
| 7593 | if (too_many_args(1, args, err, NULL)) |
| 7594 | return -1; |
| 7595 | |
| 7596 | if (*(args[1]) == 0) { |
| 7597 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 7598 | return -1; |
| 7599 | } |
| 7600 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7601 | global_ssl.default_dh_param = atoi(args[1]); |
| 7602 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7603 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 7604 | return -1; |
| 7605 | } |
| 7606 | return 0; |
| 7607 | } |
| 7608 | #endif |
| 7609 | |
| 7610 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7611 | /* This function is used with TLS ticket keys management. It permits to browse |
| 7612 | * each reference. The variable <getnext> must contain the current node, |
| 7613 | * <end> point to the root node. |
| 7614 | */ |
| 7615 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7616 | static inline |
| 7617 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 7618 | { |
| 7619 | struct tls_keys_ref *ref = getnext; |
| 7620 | |
| 7621 | while (1) { |
| 7622 | |
| 7623 | /* Get next list entry. */ |
| 7624 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 7625 | |
| 7626 | /* If the entry is the last of the list, return NULL. */ |
| 7627 | if (&ref->list == end) |
| 7628 | return NULL; |
| 7629 | |
| 7630 | return ref; |
| 7631 | } |
| 7632 | } |
| 7633 | |
| 7634 | static inline |
| 7635 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 7636 | { |
| 7637 | int id; |
| 7638 | char *error; |
| 7639 | |
| 7640 | /* If the reference starts by a '#', this is numeric id. */ |
| 7641 | if (reference[0] == '#') { |
| 7642 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 7643 | id = strtol(reference + 1, &error, 10); |
| 7644 | if (*error != '\0') |
| 7645 | return NULL; |
| 7646 | |
| 7647 | /* Perform the unique id lookup. */ |
| 7648 | return tlskeys_ref_lookupid(id); |
| 7649 | } |
| 7650 | |
| 7651 | /* Perform the string lookup. */ |
| 7652 | return tlskeys_ref_lookup(reference); |
| 7653 | } |
| 7654 | #endif |
| 7655 | |
| 7656 | |
| 7657 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7658 | |
| 7659 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 7660 | |
| 7661 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 7662 | return cli_io_handler_tlskeys_files(appctx); |
| 7663 | } |
| 7664 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7665 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 7666 | * (next index to be dumped), and cli.p0 (next key reference). |
| 7667 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7668 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 7669 | |
| 7670 | struct stream_interface *si = appctx->owner; |
| 7671 | |
| 7672 | switch (appctx->st2) { |
| 7673 | case STAT_ST_INIT: |
| 7674 | /* Display the column headers. If the message cannot be sent, |
| 7675 | * quit the fucntion with returning 0. The function is called |
| 7676 | * later and restart at the state "STAT_ST_INIT". |
| 7677 | */ |
| 7678 | chunk_reset(&trash); |
| 7679 | |
| 7680 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 7681 | chunk_appendf(&trash, "# id secret\n"); |
| 7682 | else |
| 7683 | chunk_appendf(&trash, "# id (file)\n"); |
| 7684 | |
| 7685 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 7686 | si_applet_cant_put(si); |
| 7687 | return 0; |
| 7688 | } |
| 7689 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7690 | /* Now, we start the browsing of the references lists. |
| 7691 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 7692 | * available field of this pointer is <list>. It is used with the function |
| 7693 | * tlskeys_list_get_next() for retruning the first available entry |
| 7694 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7695 | if (appctx->ctx.cli.p0 == NULL) { |
| 7696 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 7697 | 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] | 7698 | } |
| 7699 | |
| 7700 | appctx->st2 = STAT_ST_LIST; |
| 7701 | /* fall through */ |
| 7702 | |
| 7703 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7704 | while (appctx->ctx.cli.p0) { |
| 7705 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
| 7706 | int head = ref->tls_ticket_enc_index; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7707 | |
| 7708 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7709 | 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] | 7710 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7711 | |
| 7712 | if (appctx->ctx.cli.i1 == 0) |
| 7713 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 7714 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7715 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7716 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7717 | struct chunk *t2 = get_trash_chunk(); |
| 7718 | |
| 7719 | chunk_reset(t2); |
| 7720 | /* should never fail here because we dump only a key in the t2 buffer */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7721 | t2->len = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7722 | sizeof(struct tls_sess_key), t2->str, t2->size); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7723 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, t2->str); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7724 | |
| 7725 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 7726 | /* let's try again later from this stream. We add ourselves into |
| 7727 | * this stream's users so that it can remove us upon termination. |
| 7728 | */ |
| 7729 | si_applet_cant_put(si); |
| 7730 | return 0; |
| 7731 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7732 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7733 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7734 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7735 | } |
| 7736 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 7737 | /* let's try again later from this stream. We add ourselves into |
| 7738 | * this stream's users so that it can remove us upon termination. |
| 7739 | */ |
| 7740 | si_applet_cant_put(si); |
| 7741 | return 0; |
| 7742 | } |
| 7743 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7744 | 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] | 7745 | break; |
| 7746 | |
| 7747 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7748 | 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] | 7749 | } |
| 7750 | |
| 7751 | appctx->st2 = STAT_ST_FIN; |
| 7752 | /* fall through */ |
| 7753 | |
| 7754 | default: |
| 7755 | appctx->st2 = STAT_ST_FIN; |
| 7756 | return 1; |
| 7757 | } |
| 7758 | return 0; |
| 7759 | } |
| 7760 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7761 | /* sets cli.i0 to non-zero if only file lists should be dumped */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7762 | static int cli_parse_show_tlskeys(char **args, struct appctx *appctx, void *private) |
| 7763 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7764 | /* no parameter, shows only file list */ |
| 7765 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7766 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7767 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 7768 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7769 | } |
| 7770 | |
| 7771 | if (args[2][0] == '*') { |
| 7772 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7773 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7774 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7775 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
| 7776 | if (!appctx->ctx.cli.p0) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7777 | 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] | 7778 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7779 | return 1; |
| 7780 | } |
| 7781 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7782 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 7783 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7784 | } |
| 7785 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7786 | static int cli_parse_set_tlskeys(char **args, struct appctx *appctx, void *private) |
| 7787 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7788 | struct tls_keys_ref *ref; |
| 7789 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7790 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 7791 | if (!*args[3] || !*args[4]) { |
| 7792 | 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] | 7793 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7794 | return 1; |
| 7795 | } |
| 7796 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7797 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 7798 | if (!ref) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7799 | 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] | 7800 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7801 | return 1; |
| 7802 | } |
| 7803 | |
| 7804 | trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size); |
| 7805 | if (trash.len != sizeof(struct tls_sess_key)) { |
| 7806 | 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] | 7807 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7808 | return 1; |
| 7809 | } |
| 7810 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7811 | memcpy(ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len); |
| 7812 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7813 | |
| 7814 | appctx->ctx.cli.msg = "TLS ticket key updated!"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 7815 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7816 | return 1; |
| 7817 | |
| 7818 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7819 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7820 | |
| 7821 | static int cli_parse_set_ocspresponse(char **args, struct appctx *appctx, void *private) |
| 7822 | { |
| 7823 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 7824 | char *err = NULL; |
| 7825 | |
| 7826 | /* Expect one parameter: the new response in base64 encoding */ |
| 7827 | if (!*args[3]) { |
| 7828 | 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] | 7829 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7830 | return 1; |
| 7831 | } |
| 7832 | |
| 7833 | trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size); |
| 7834 | if (trash.len < 0) { |
| 7835 | 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] | 7836 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7837 | return 1; |
| 7838 | } |
| 7839 | |
| 7840 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
| 7841 | if (err) { |
| 7842 | memprintf(&err, "%s.\n", err); |
| 7843 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 7844 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7845 | } |
| 7846 | return 1; |
| 7847 | } |
| 7848 | appctx->ctx.cli.msg = "OCSP Response updated!"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 7849 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7850 | return 1; |
| 7851 | #else |
| 7852 | 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] | 7853 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7854 | return 1; |
| 7855 | #endif |
| 7856 | |
| 7857 | } |
| 7858 | |
| 7859 | /* register cli keywords */ |
| 7860 | static struct cli_kw_list cli_kws = {{ },{ |
| 7861 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7862 | { { "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 }, |
| 7863 | { { "set", "ssl", "tls-keys", NULL }, NULL, cli_parse_set_tlskeys, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7864 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7865 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7866 | { { NULL }, NULL, NULL, NULL } |
| 7867 | }}; |
| 7868 | |
| 7869 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7870 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7871 | * Please take care of keeping this list alphabetically sorted. |
| 7872 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 7873 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7874 | { "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] | 7875 | { "ssl_bc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV }, |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7876 | { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 7877 | { "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] | 7878 | { "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] | 7879 | { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV }, |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7880 | { "ssl_bc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 7881 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 7882 | { "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] | 7883 | { "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] | 7884 | { "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] | 7885 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7886 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7887 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7888 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7889 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7890 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7891 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 7892 | { "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] | 7893 | { "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] | 7894 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 7895 | { "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] | 7896 | { "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] | 7897 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7898 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7899 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7900 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7901 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7902 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7903 | { "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] | 7904 | { "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] | 7905 | { "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] | 7906 | { "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] | 7907 | { "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] | 7908 | { "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] | 7909 | { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 7910 | { "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] | 7911 | { "ssl_fc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7912 | #ifdef OPENSSL_NPN_NEGOTIATED |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7913 | { "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] | 7914 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7915 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7916 | { "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] | 7917 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7918 | { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 7919 | { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 7920 | { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7921 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 7922 | { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7923 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7924 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 7925 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7926 | { "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] | 7927 | { NULL, NULL, 0, 0, 0 }, |
| 7928 | }}; |
| 7929 | |
| 7930 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7931 | * Please take care of keeping this list alphabetically sorted. |
| 7932 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 7933 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 7934 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 7935 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 7936 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7937 | }}; |
| 7938 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7939 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7940 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 7941 | * all code contributors. |
| 7942 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 7943 | * the config parser can report an appropriate error when a known keyword was |
| 7944 | * not enabled. |
| 7945 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7946 | static struct ssl_bind_kw ssl_bind_kws[] = { |
| 7947 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 7948 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 7949 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 7950 | { "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] | 7951 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7952 | { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7953 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 7954 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 7955 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7956 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 7957 | { NULL, NULL, 0 }, |
| 7958 | }; |
| 7959 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 7960 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7961 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 7962 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 7963 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 7964 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 7965 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 7966 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 7967 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 7968 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 7969 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 7970 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 7971 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 7972 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 7973 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 7974 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 7975 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 7976 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 7977 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7978 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
| 7979 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 7980 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 7981 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 7982 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 7983 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7984 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 7985 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7986 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 7987 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7988 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 7989 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 7990 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 7991 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 7992 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7993 | { NULL, NULL, 0 }, |
| 7994 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7995 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7996 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7997 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 7998 | * all code contributors. |
| 7999 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 8000 | * the config parser can report an appropriate error when a known keyword was |
| 8001 | * not enabled. |
| 8002 | */ |
| 8003 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8004 | { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */ |
| 8005 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 8006 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
| 8007 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 8008 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 8009 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 8010 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 8011 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 8012 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 8013 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 8014 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 8015 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 8016 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 8017 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 8018 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 8019 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 8020 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 8021 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 8022 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 8023 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 8024 | { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */ |
| 8025 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 8026 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 8027 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 8028 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 8029 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 8030 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 8031 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 8032 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 8033 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 8034 | { "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] | 8035 | { NULL, NULL, 0, 0 }, |
| 8036 | }}; |
| 8037 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8038 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8039 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 8040 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 8041 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8042 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 8043 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 8044 | #ifndef OPENSSL_NO_DH |
| 8045 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 8046 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8047 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8048 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8049 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8050 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8051 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 8052 | #ifndef OPENSSL_NO_DH |
| 8053 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 8054 | #endif |
| 8055 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 8056 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 8057 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 8058 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8059 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8060 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 8061 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8062 | { 0, NULL, NULL }, |
| 8063 | }}; |
| 8064 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 8065 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 8066 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8067 | .snd_buf = ssl_sock_from_buf, |
| 8068 | .rcv_buf = ssl_sock_to_buf, |
| 8069 | .rcv_pipe = NULL, |
| 8070 | .snd_pipe = NULL, |
| 8071 | .shutr = NULL, |
| 8072 | .shutw = ssl_sock_shutw, |
| 8073 | .close = ssl_sock_close, |
| 8074 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 8075 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 8076 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 8077 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 8078 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 8079 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 8080 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8081 | }; |
| 8082 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 8083 | #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] | 8084 | |
| 8085 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8086 | { |
| 8087 | if (ptr) { |
| 8088 | chunk_destroy(ptr); |
| 8089 | free(ptr); |
| 8090 | } |
| 8091 | } |
| 8092 | |
| 8093 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8094 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8095 | { |
| 8096 | pool_free2(pool2_ssl_capture, ptr); |
| 8097 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 8098 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8099 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8100 | static void __ssl_sock_init(void) |
| 8101 | { |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8102 | char *ptr; |
| 8103 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8104 | STACK_OF(SSL_COMP)* cm; |
| 8105 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8106 | if (global_ssl.listen_default_ciphers) |
| 8107 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 8108 | if (global_ssl.connect_default_ciphers) |
| 8109 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 8110 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 8111 | xprt_register(XPRT_SSL, &ssl_sock); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8112 | SSL_library_init(); |
| 8113 | cm = SSL_COMP_get_compression_methods(); |
| 8114 | sk_SSL_COMP_zero(cm); |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 8115 | #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] | 8116 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 8117 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8118 | ssl_capture_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8119 | sample_register_fetches(&sample_fetch_keywords); |
| 8120 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8121 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8122 | srv_register_keywords(&srv_kws); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8123 | cfg_register_keywords(&cfg_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8124 | cli_register_kw(&cli_kws); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8125 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8126 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8127 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8128 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 8129 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8130 | hap_register_post_check(tlskeys_finalize_config); |
| 8131 | #endif |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 8132 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8133 | ptr = NULL; |
| 8134 | memprintf(&ptr, "Built with OpenSSL version : " |
| 8135 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 8136 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8137 | #else /* OPENSSL_IS_BORINGSSL */ |
| 8138 | OPENSSL_VERSION_TEXT |
| 8139 | "\nRunning on OpenSSL version : %s%s", |
| 8140 | SSLeay_version(SSLEAY_VERSION), |
| 8141 | ((OPENSSL_VERSION_NUMBER ^ SSLeay()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
| 8142 | #endif |
| 8143 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
| 8144 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 8145 | "no (library version too old)" |
| 8146 | #elif defined(OPENSSL_NO_TLSEXT) |
| 8147 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 8148 | #else |
| 8149 | "yes" |
| 8150 | #endif |
| 8151 | "", ptr); |
| 8152 | |
| 8153 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 8154 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 8155 | "yes" |
| 8156 | #else |
| 8157 | #ifdef OPENSSL_NO_TLSEXT |
| 8158 | "no (because of OPENSSL_NO_TLSEXT)" |
| 8159 | #else |
| 8160 | "no (version might be too old, 0.9.8f min needed)" |
| 8161 | #endif |
| 8162 | #endif |
| 8163 | "", ptr); |
| 8164 | |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 8165 | memprintf(&ptr, "%s\nOpenSSL library supports : " |
| 8166 | #if SSL_OP_NO_SSLv3 |
| 8167 | "SSLv3 " |
| 8168 | #endif |
| 8169 | #if SSL_OP_NO_TLSv1 |
| 8170 | "TLSv1.0 " |
| 8171 | #endif |
| 8172 | #if SSL_OP_NO_TLSv1_1 |
| 8173 | "TLSv1.1 " |
| 8174 | #endif |
| 8175 | #if SSL_OP_NO_TLSv1_2 |
| 8176 | "TLSv1.2 " |
| 8177 | #endif |
| 8178 | #if SSL_OP_NO_TLSv1_3 |
| 8179 | "TLSv1.3" |
| 8180 | #endif |
| 8181 | "", ptr); |
| 8182 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8183 | hap_register_build_opts(ptr, 1); |
| 8184 | |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 8185 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 8186 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 8187 | |
| 8188 | #ifndef OPENSSL_NO_DH |
| 8189 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8190 | hap_register_post_deinit(ssl_free_dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 8191 | #endif |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8192 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8193 | hap_register_post_deinit(ssl_free_engines); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8194 | #endif |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 8195 | /* Load SSL string for the verbose & debug mode. */ |
| 8196 | ERR_load_SSL_strings(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8197 | } |
| 8198 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8199 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8200 | void ssl_free_engines(void) { |
| 8201 | struct ssl_engine_list *wl, *wlb; |
| 8202 | /* free up engine list */ |
| 8203 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 8204 | ENGINE_finish(wl->e); |
| 8205 | ENGINE_free(wl->e); |
| 8206 | LIST_DEL(&wl->list); |
| 8207 | free(wl); |
| 8208 | } |
| 8209 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8210 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8211 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8212 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8213 | void ssl_free_dh(void) { |
| 8214 | if (local_dh_1024) { |
| 8215 | DH_free(local_dh_1024); |
| 8216 | local_dh_1024 = NULL; |
| 8217 | } |
| 8218 | if (local_dh_2048) { |
| 8219 | DH_free(local_dh_2048); |
| 8220 | local_dh_2048 = NULL; |
| 8221 | } |
| 8222 | if (local_dh_4096) { |
| 8223 | DH_free(local_dh_4096); |
| 8224 | local_dh_4096 = NULL; |
| 8225 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 8226 | if (global_dh) { |
| 8227 | DH_free(global_dh); |
| 8228 | global_dh = NULL; |
| 8229 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8230 | } |
| 8231 | #endif |
| 8232 | |
| 8233 | __attribute__((destructor)) |
| 8234 | static void __ssl_sock_deinit(void) |
| 8235 | { |
| 8236 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
| 8237 | lru64_destroy(ssl_ctx_lru_tree); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8238 | #endif |
| 8239 | |
| 8240 | ERR_remove_state(0); |
| 8241 | ERR_free_strings(); |
| 8242 | |
| 8243 | EVP_cleanup(); |
| 8244 | |
| 8245 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 8246 | CRYPTO_cleanup_all_ex_data(); |
| 8247 | #endif |
| 8248 | } |
| 8249 | |
| 8250 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8251 | /* |
| 8252 | * Local variables: |
| 8253 | * c-indent-level: 8 |
| 8254 | * c-basic-offset: 8 |
| 8255 | * End: |
| 8256 | */ |