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; |
| 372 | int conn_fd = conn->t.sock.fd; |
| 373 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 374 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 375 | * to poll this fd until it is requested |
| 376 | */ |
| 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 | */ |
| 382 | conn_fd_handler(conn_fd); |
| 383 | } |
| 384 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 385 | /* |
| 386 | * openssl async delayed SSL_free handler |
| 387 | */ |
| 388 | static void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 389 | { |
| 390 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 391 | OSSL_ASYNC_FD all_fd[32]; |
| 392 | size_t num_all_fds = 0; |
| 393 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 394 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 395 | /* We suppose that the async job for a same SSL * |
| 396 | * are serialized. So if we are awake it is |
| 397 | * because the running job has just finished |
| 398 | * and we can remove all async fds safely |
| 399 | */ |
| 400 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 401 | if (num_all_fds > 32) { |
| 402 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 407 | for (i=0 ; i < num_all_fds ; i++) |
| 408 | fd_remove(all_fd[i]); |
| 409 | |
| 410 | /* 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] | 411 | SSL_free(ssl); |
| 412 | sslconns--; |
| 413 | jobs--; |
| 414 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 415 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 416 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 417 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 418 | */ |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 419 | static void inline ssl_async_process_fds(struct connection *conn, SSL *ssl) |
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 | OSSL_ASYNC_FD add_fd[32], afd; |
| 422 | OSSL_ASYNC_FD del_fd[32]; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 423 | size_t num_add_fds = 0; |
| 424 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 425 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 426 | |
| 427 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 428 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 429 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 430 | 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] | 431 | return; |
| 432 | } |
| 433 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 434 | 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] | 435 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 436 | /* We remove unused fds from the fdtab */ |
| 437 | for (i=0 ; i < num_del_fds ; i++) |
| 438 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 439 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 440 | /* We add new fds to the fdtab */ |
| 441 | for (i=0 ; i < num_add_fds ; i++) { |
| 442 | afd = add_fd[i]; |
| 443 | fdtab[afd].owner = conn; |
| 444 | fdtab[afd].iocb = ssl_async_fd_handler; |
| 445 | fd_insert(afd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 448 | num_add_fds = 0; |
| 449 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 450 | if (num_add_fds > 32) { |
| 451 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 452 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 453 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 454 | |
| 455 | /* We activate the polling for all known async fds */ |
| 456 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
| 457 | for (i=0 ; i < num_add_fds ; i++) |
| 458 | fd_want_recv(add_fd[i]); |
| 459 | |
| 460 | /* We must also prevent the conn_handler |
| 461 | * to be called until a read event was |
| 462 | * polled on an async fd |
| 463 | */ |
| 464 | __conn_sock_stop_both(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 465 | } |
| 466 | #endif |
| 467 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 468 | /* |
| 469 | * This function returns the number of seconds elapsed |
| 470 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 471 | * date presented un ASN1_GENERALIZEDTIME. |
| 472 | * |
| 473 | * In parsing error case, it returns -1. |
| 474 | */ |
| 475 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 476 | { |
| 477 | long epoch; |
| 478 | char *p, *end; |
| 479 | const unsigned short month_offset[12] = { |
| 480 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 481 | }; |
| 482 | int year, month; |
| 483 | |
| 484 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 485 | |
| 486 | p = (char *)d->data; |
| 487 | end = p + d->length; |
| 488 | |
| 489 | if (end - p < 4) return -1; |
| 490 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 491 | p += 4; |
| 492 | if (end - p < 2) return -1; |
| 493 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 494 | if (month < 1 || month > 12) return -1; |
| 495 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 496 | We consider leap years and the current month (<marsh or not) */ |
| 497 | epoch = ( ((year - 1970) * 365) |
| 498 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 499 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 500 | + month_offset[month-1] |
| 501 | ) * 24 * 60 * 60; |
| 502 | p += 2; |
| 503 | if (end - p < 2) return -1; |
| 504 | /* Add the number of seconds of completed days of current month */ |
| 505 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 506 | p += 2; |
| 507 | if (end - p < 2) return -1; |
| 508 | /* Add the completed hours of the current day */ |
| 509 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 510 | p += 2; |
| 511 | if (end - p < 2) return -1; |
| 512 | /* Add the completed minutes of the current hour */ |
| 513 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 514 | p += 2; |
| 515 | if (p == end) return -1; |
| 516 | /* Test if there is available seconds */ |
| 517 | if (p[0] < '0' || p[0] > '9') |
| 518 | goto nosec; |
| 519 | if (end - p < 2) return -1; |
| 520 | /* Add the seconds of the current minute */ |
| 521 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 522 | p += 2; |
| 523 | if (p == end) return -1; |
| 524 | /* Ignore seconds float part if present */ |
| 525 | if (p[0] == '.') { |
| 526 | do { |
| 527 | if (++p == end) return -1; |
| 528 | } while (p[0] >= '0' && p[0] <= '9'); |
| 529 | } |
| 530 | |
| 531 | nosec: |
| 532 | if (p[0] == 'Z') { |
| 533 | if (end - p != 1) return -1; |
| 534 | return epoch; |
| 535 | } |
| 536 | else if (p[0] == '+') { |
| 537 | if (end - p != 5) return -1; |
| 538 | /* Apply timezone offset */ |
| 539 | return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 540 | } |
| 541 | else if (p[0] == '-') { |
| 542 | if (end - p != 5) return -1; |
| 543 | /* Apply timezone offset */ |
| 544 | return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 545 | } |
| 546 | |
| 547 | return -1; |
| 548 | } |
| 549 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 550 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 551 | |
| 552 | /* This function starts to check if the OCSP response (in DER format) contained |
| 553 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 554 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 555 | * contained in the OCSP Response and exits on error if no match. |
| 556 | * If it's a valid OCSP Response: |
| 557 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 558 | * pointed by 'ocsp'. |
| 559 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 560 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 561 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 562 | * already present in the container, it will be overwritten. |
| 563 | * |
| 564 | * Note: OCSP response containing more than one OCSP Single response is not |
| 565 | * considered valid. |
| 566 | * |
| 567 | * Returns 0 on success, 1 in error case. |
| 568 | */ |
| 569 | static int ssl_sock_load_ocsp_response(struct chunk *ocsp_response, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 570 | { |
| 571 | OCSP_RESPONSE *resp; |
| 572 | OCSP_BASICRESP *bs = NULL; |
| 573 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 574 | OCSP_CERTID *id; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 575 | unsigned char *p = (unsigned char *)ocsp_response->str; |
| 576 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 577 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 578 | int reason; |
| 579 | int ret = 1; |
| 580 | |
| 581 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, ocsp_response->len); |
| 582 | if (!resp) { |
| 583 | memprintf(err, "Unable to parse OCSP response"); |
| 584 | goto out; |
| 585 | } |
| 586 | |
| 587 | rc = OCSP_response_status(resp); |
| 588 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 589 | memprintf(err, "OCSP response status not successful"); |
| 590 | goto out; |
| 591 | } |
| 592 | |
| 593 | bs = OCSP_response_get1_basic(resp); |
| 594 | if (!bs) { |
| 595 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 596 | goto out; |
| 597 | } |
| 598 | |
| 599 | count_sr = OCSP_resp_count(bs); |
| 600 | if (count_sr > 1) { |
| 601 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 602 | goto out; |
| 603 | } |
| 604 | |
| 605 | sr = OCSP_resp_get0(bs, 0); |
| 606 | if (!sr) { |
| 607 | memprintf(err, "Failed to get OCSP single response"); |
| 608 | goto out; |
| 609 | } |
| 610 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 611 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 612 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 613 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
| 614 | if (rc != V_OCSP_CERTSTATUS_GOOD) { |
| 615 | memprintf(err, "OCSP single response: certificate status not good"); |
| 616 | goto out; |
| 617 | } |
| 618 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 619 | if (!nextupd) { |
| 620 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 621 | goto out; |
| 622 | } |
| 623 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 624 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 625 | if (!rc) { |
| 626 | memprintf(err, "OCSP single response: no longer valid."); |
| 627 | goto out; |
| 628 | } |
| 629 | |
| 630 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 631 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 632 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 633 | goto out; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | if (!ocsp) { |
| 638 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 639 | unsigned char *p; |
| 640 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 641 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 642 | if (!rc) { |
| 643 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 644 | goto out; |
| 645 | } |
| 646 | |
| 647 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 648 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 649 | goto out; |
| 650 | } |
| 651 | |
| 652 | p = key; |
| 653 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 654 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 655 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 656 | if (!ocsp) { |
| 657 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 658 | goto out; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /* According to comments on "chunk_dup", the |
| 663 | previous chunk buffer will be freed */ |
| 664 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 665 | memprintf(err, "OCSP response: Memory allocation error"); |
| 666 | goto out; |
| 667 | } |
| 668 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 669 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 670 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 671 | ret = 0; |
| 672 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 673 | ERR_clear_error(); |
| 674 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 675 | if (bs) |
| 676 | OCSP_BASICRESP_free(bs); |
| 677 | |
| 678 | if (resp) |
| 679 | OCSP_RESPONSE_free(resp); |
| 680 | |
| 681 | return ret; |
| 682 | } |
| 683 | /* |
| 684 | * External function use to update the OCSP response in the OCSP response's |
| 685 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 686 | * to update in DER format. |
| 687 | * |
| 688 | * Returns 0 on success, 1 in error case. |
| 689 | */ |
| 690 | int ssl_sock_update_ocsp_response(struct chunk *ocsp_response, char **err) |
| 691 | { |
| 692 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * This function load the OCSP Resonse in DER format contained in file at |
| 697 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 698 | * |
| 699 | * Returns 0 on success, 1 in error case. |
| 700 | */ |
| 701 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 702 | { |
| 703 | int fd = -1; |
| 704 | int r = 0; |
| 705 | int ret = 1; |
| 706 | |
| 707 | fd = open(ocsp_path, O_RDONLY); |
| 708 | if (fd == -1) { |
| 709 | memprintf(err, "Error opening OCSP response file"); |
| 710 | goto end; |
| 711 | } |
| 712 | |
| 713 | trash.len = 0; |
| 714 | while (trash.len < trash.size) { |
| 715 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 716 | if (r < 0) { |
| 717 | if (errno == EINTR) |
| 718 | continue; |
| 719 | |
| 720 | memprintf(err, "Error reading OCSP response from file"); |
| 721 | goto end; |
| 722 | } |
| 723 | else if (r == 0) { |
| 724 | break; |
| 725 | } |
| 726 | trash.len += r; |
| 727 | } |
| 728 | |
| 729 | close(fd); |
| 730 | fd = -1; |
| 731 | |
| 732 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 733 | end: |
| 734 | if (fd != -1) |
| 735 | close(fd); |
| 736 | |
| 737 | return ret; |
| 738 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 739 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 740 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 741 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 742 | 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) |
| 743 | { |
| 744 | struct tls_sess_key *keys; |
| 745 | struct connection *conn; |
| 746 | int head; |
| 747 | int i; |
| 748 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 749 | conn = SSL_get_app_data(s); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 750 | keys = objt_listener(conn->target)->bind_conf->keys_ref->tlskeys; |
| 751 | 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] | 752 | |
| 753 | if (enc) { |
| 754 | memcpy(key_name, keys[head].name, 16); |
| 755 | |
| 756 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
| 757 | return -1; |
| 758 | |
| 759 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].aes_key, iv)) |
| 760 | return -1; |
| 761 | |
| 762 | HMAC_Init_ex(hctx, keys[head].hmac_key, 16, HASH_FUNCT(), NULL); |
| 763 | |
| 764 | return 1; |
| 765 | } else { |
| 766 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 767 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 768 | goto found; |
| 769 | } |
| 770 | return 0; |
| 771 | |
| 772 | found: |
| 773 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].hmac_key, 16, HASH_FUNCT(), NULL); |
| 774 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].aes_key, iv)) |
| 775 | return -1; |
| 776 | /* 2 for key renewal, 1 if current key is still valid */ |
| 777 | return i ? 2 : 1; |
| 778 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 782 | { |
| 783 | struct tls_keys_ref *ref; |
| 784 | |
| 785 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 786 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 787 | return ref; |
| 788 | return NULL; |
| 789 | } |
| 790 | |
| 791 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 792 | { |
| 793 | struct tls_keys_ref *ref; |
| 794 | |
| 795 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 796 | if (ref->unique_id == unique_id) |
| 797 | return ref; |
| 798 | return NULL; |
| 799 | } |
| 800 | |
| 801 | int ssl_sock_update_tlskey(char *filename, struct chunk *tlskey, char **err) { |
| 802 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 803 | |
| 804 | if(!ref) { |
| 805 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 806 | return 1; |
| 807 | } |
| 808 | |
Pradeep Jindal | cc79b00 | 2015-08-20 18:25:17 +0530 | [diff] [blame] | 809 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), tlskey->str, tlskey->len); |
| 810 | 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] | 811 | |
| 812 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 813 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 814 | |
| 815 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 816 | * automatic ids. It's called just after the basic checks. It returns |
| 817 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 818 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 819 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 820 | { |
| 821 | int i = 0; |
| 822 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 823 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 824 | |
| 825 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 826 | if (ref->unique_id == -1) { |
| 827 | /* Look for the first free id. */ |
| 828 | while (1) { |
| 829 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 830 | if (ref2->unique_id == i) { |
| 831 | i++; |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | if (&ref2->list == &tlskeys_reference) |
| 836 | break; |
| 837 | } |
| 838 | |
| 839 | /* Uses the unique id and increment it for the next entry. */ |
| 840 | ref->unique_id = i; |
| 841 | i++; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | /* This sort the reference list by id. */ |
| 846 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 847 | LIST_DEL(&ref->list); |
| 848 | list_for_each_entry(ref3, &tkr, list) { |
| 849 | if (ref->unique_id < ref3->unique_id) { |
| 850 | LIST_ADDQ(&ref3->list, &ref->list); |
| 851 | break; |
| 852 | } |
| 853 | } |
| 854 | if (&ref3->list == &tkr) |
| 855 | LIST_ADDQ(&tkr, &ref->list); |
| 856 | } |
| 857 | |
| 858 | /* swap root */ |
| 859 | LIST_ADD(&tkr, &tlskeys_reference); |
| 860 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 861 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 862 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 863 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 864 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 865 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 866 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 867 | { |
| 868 | switch (evp_keytype) { |
| 869 | case EVP_PKEY_RSA: |
| 870 | return 2; |
| 871 | case EVP_PKEY_DSA: |
| 872 | return 0; |
| 873 | case EVP_PKEY_EC: |
| 874 | return 1; |
| 875 | } |
| 876 | |
| 877 | return -1; |
| 878 | } |
| 879 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 880 | /* |
| 881 | * Callback used to set OCSP status extension content in server hello. |
| 882 | */ |
| 883 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 884 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 885 | struct certificate_ocsp *ocsp; |
| 886 | struct ocsp_cbk_arg *ocsp_arg; |
| 887 | char *ssl_buf; |
| 888 | EVP_PKEY *ssl_pkey; |
| 889 | int key_type; |
| 890 | int index; |
| 891 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 892 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 893 | |
| 894 | ssl_pkey = SSL_get_privatekey(ssl); |
| 895 | if (!ssl_pkey) |
| 896 | return SSL_TLSEXT_ERR_NOACK; |
| 897 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 898 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 899 | |
| 900 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 901 | ocsp = ocsp_arg->s_ocsp; |
| 902 | else { |
| 903 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 904 | * the certificate type |
| 905 | */ |
| 906 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 907 | |
| 908 | if (index < 0) |
| 909 | return SSL_TLSEXT_ERR_NOACK; |
| 910 | |
| 911 | ocsp = ocsp_arg->m_ocsp[index]; |
| 912 | |
| 913 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 914 | |
| 915 | if (!ocsp || |
| 916 | !ocsp->response.str || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 917 | !ocsp->response.len || |
| 918 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 919 | return SSL_TLSEXT_ERR_NOACK; |
| 920 | |
| 921 | ssl_buf = OPENSSL_malloc(ocsp->response.len); |
| 922 | if (!ssl_buf) |
| 923 | return SSL_TLSEXT_ERR_NOACK; |
| 924 | |
| 925 | memcpy(ssl_buf, ocsp->response.str, ocsp->response.len); |
| 926 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.len); |
| 927 | |
| 928 | return SSL_TLSEXT_ERR_OK; |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 933 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 934 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 935 | * It should be present in the certificate's extra chain builded from file |
| 936 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 937 | * named 'cert_path' suffixed using '.issuer'. |
| 938 | * |
| 939 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 940 | * response. If file is empty or content is not a valid OCSP response, |
| 941 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 942 | * is displayed). |
| 943 | * |
| 944 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
| 945 | * succesfully enabled, or -1 in other error case. |
| 946 | */ |
| 947 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 948 | { |
| 949 | |
| 950 | BIO *in = NULL; |
| 951 | X509 *x, *xi = NULL, *issuer = NULL; |
| 952 | STACK_OF(X509) *chain = NULL; |
| 953 | OCSP_CERTID *cid = NULL; |
| 954 | SSL *ssl; |
| 955 | char ocsp_path[MAXPATHLEN+1]; |
| 956 | int i, ret = -1; |
| 957 | struct stat st; |
| 958 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 959 | char *warn = NULL; |
| 960 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 961 | pem_password_cb *passwd_cb; |
| 962 | void *passwd_cb_userdata; |
| 963 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 964 | |
| 965 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 966 | |
| 967 | if (stat(ocsp_path, &st)) |
| 968 | return 1; |
| 969 | |
| 970 | ssl = SSL_new(ctx); |
| 971 | if (!ssl) |
| 972 | goto out; |
| 973 | |
| 974 | x = SSL_get_certificate(ssl); |
| 975 | if (!x) |
| 976 | goto out; |
| 977 | |
| 978 | /* Try to lookup for issuer in certificate extra chain */ |
| 979 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 980 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 981 | #else |
| 982 | chain = ctx->extra_certs; |
| 983 | #endif |
| 984 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 985 | issuer = sk_X509_value(chain, i); |
| 986 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 987 | break; |
| 988 | else |
| 989 | issuer = NULL; |
| 990 | } |
| 991 | |
| 992 | /* If not found try to load issuer from a suffixed file */ |
| 993 | if (!issuer) { |
| 994 | char issuer_path[MAXPATHLEN+1]; |
| 995 | |
| 996 | in = BIO_new(BIO_s_file()); |
| 997 | if (!in) |
| 998 | goto out; |
| 999 | |
| 1000 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 1001 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 1002 | goto out; |
| 1003 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1004 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 1005 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 1006 | |
| 1007 | 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] | 1008 | if (!xi) |
| 1009 | goto out; |
| 1010 | |
| 1011 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 1012 | goto out; |
| 1013 | |
| 1014 | issuer = xi; |
| 1015 | } |
| 1016 | |
| 1017 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1018 | if (!cid) |
| 1019 | goto out; |
| 1020 | |
| 1021 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1022 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1023 | goto out; |
| 1024 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1025 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1026 | if (!ocsp) |
| 1027 | goto out; |
| 1028 | |
| 1029 | p = ocsp->key_data; |
| 1030 | i2d_OCSP_CERTID(cid, &p); |
| 1031 | |
| 1032 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1033 | if (iocsp == ocsp) |
| 1034 | ocsp = NULL; |
| 1035 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1036 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1037 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1038 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1039 | #endif |
| 1040 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1041 | |
| 1042 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1043 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1044 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1045 | |
| 1046 | cb_arg->is_single = 1; |
| 1047 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1048 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1049 | pkey = X509_get_pubkey(x); |
| 1050 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1051 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1052 | |
| 1053 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1054 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1055 | } else { |
| 1056 | /* |
| 1057 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1058 | * Update that cb_arg with the new cert's staple |
| 1059 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1060 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1061 | struct certificate_ocsp *tmp_ocsp; |
| 1062 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1063 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1064 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1065 | |
| 1066 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1067 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1068 | #else |
| 1069 | cb_arg = ctx->tlsext_status_arg; |
| 1070 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1071 | |
| 1072 | /* |
| 1073 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1074 | * the order of operations below matter, take care when changing it |
| 1075 | */ |
| 1076 | tmp_ocsp = cb_arg->s_ocsp; |
| 1077 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1078 | cb_arg->s_ocsp = NULL; |
| 1079 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1080 | cb_arg->is_single = 0; |
| 1081 | cb_arg->single_kt = 0; |
| 1082 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1083 | pkey = X509_get_pubkey(x); |
| 1084 | key_type = EVP_PKEY_base_id(pkey); |
| 1085 | EVP_PKEY_free(pkey); |
| 1086 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1087 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1088 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1089 | cb_arg->m_ocsp[index] = iocsp; |
| 1090 | |
| 1091 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1092 | |
| 1093 | ret = 0; |
| 1094 | |
| 1095 | warn = NULL; |
| 1096 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 1097 | memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure"); |
| 1098 | Warning("%s.\n", warn); |
| 1099 | } |
| 1100 | |
| 1101 | out: |
| 1102 | if (ssl) |
| 1103 | SSL_free(ssl); |
| 1104 | |
| 1105 | if (in) |
| 1106 | BIO_free(in); |
| 1107 | |
| 1108 | if (xi) |
| 1109 | X509_free(xi); |
| 1110 | |
| 1111 | if (cid) |
| 1112 | OCSP_CERTID_free(cid); |
| 1113 | |
| 1114 | if (ocsp) |
| 1115 | free(ocsp); |
| 1116 | |
| 1117 | if (warn) |
| 1118 | free(warn); |
| 1119 | |
| 1120 | |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
| 1124 | #endif |
| 1125 | |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1126 | #ifdef OPENSSL_IS_BORINGSSL |
| 1127 | static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path) |
| 1128 | { |
| 1129 | char ocsp_path[MAXPATHLEN+1]; |
| 1130 | struct stat st; |
| 1131 | int fd = -1, r = 0; |
| 1132 | |
| 1133 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1134 | if (stat(ocsp_path, &st)) |
| 1135 | return 0; |
| 1136 | |
| 1137 | fd = open(ocsp_path, O_RDONLY); |
| 1138 | if (fd == -1) { |
| 1139 | Warning("Error opening OCSP response file %s.\n", ocsp_path); |
| 1140 | return -1; |
| 1141 | } |
| 1142 | |
| 1143 | trash.len = 0; |
| 1144 | while (trash.len < trash.size) { |
| 1145 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 1146 | if (r < 0) { |
| 1147 | if (errno == EINTR) |
| 1148 | continue; |
| 1149 | Warning("Error reading OCSP response from file %s.\n", ocsp_path); |
| 1150 | close(fd); |
| 1151 | return -1; |
| 1152 | } |
| 1153 | else if (r == 0) { |
| 1154 | break; |
| 1155 | } |
| 1156 | trash.len += r; |
| 1157 | } |
| 1158 | close(fd); |
| 1159 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)trash.str, trash.len); |
| 1160 | } |
| 1161 | #endif |
| 1162 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 1163 | #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] | 1164 | |
| 1165 | #define CT_EXTENSION_TYPE 18 |
| 1166 | |
| 1167 | static int sctl_ex_index = -1; |
| 1168 | |
| 1169 | /* |
| 1170 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1171 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1172 | * is performed. |
| 1173 | */ |
| 1174 | static int ssl_sock_parse_sctl(struct chunk *sctl) |
| 1175 | { |
| 1176 | int ret = 1; |
| 1177 | int len, pos, sct_len; |
| 1178 | unsigned char *data; |
| 1179 | |
| 1180 | if (sctl->len < 2) |
| 1181 | goto out; |
| 1182 | |
| 1183 | data = (unsigned char *)sctl->str; |
| 1184 | len = (data[0] << 8) | data[1]; |
| 1185 | |
| 1186 | if (len + 2 != sctl->len) |
| 1187 | goto out; |
| 1188 | |
| 1189 | data = data + 2; |
| 1190 | pos = 0; |
| 1191 | while (pos < len) { |
| 1192 | if (len - pos < 2) |
| 1193 | goto out; |
| 1194 | |
| 1195 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1196 | if (pos + sct_len + 2 > len) |
| 1197 | goto out; |
| 1198 | |
| 1199 | pos += sct_len + 2; |
| 1200 | } |
| 1201 | |
| 1202 | ret = 0; |
| 1203 | |
| 1204 | out: |
| 1205 | return ret; |
| 1206 | } |
| 1207 | |
| 1208 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, struct chunk **sctl) |
| 1209 | { |
| 1210 | int fd = -1; |
| 1211 | int r = 0; |
| 1212 | int ret = 1; |
| 1213 | |
| 1214 | *sctl = NULL; |
| 1215 | |
| 1216 | fd = open(sctl_path, O_RDONLY); |
| 1217 | if (fd == -1) |
| 1218 | goto end; |
| 1219 | |
| 1220 | trash.len = 0; |
| 1221 | while (trash.len < trash.size) { |
| 1222 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 1223 | if (r < 0) { |
| 1224 | if (errno == EINTR) |
| 1225 | continue; |
| 1226 | |
| 1227 | goto end; |
| 1228 | } |
| 1229 | else if (r == 0) { |
| 1230 | break; |
| 1231 | } |
| 1232 | trash.len += r; |
| 1233 | } |
| 1234 | |
| 1235 | ret = ssl_sock_parse_sctl(&trash); |
| 1236 | if (ret) |
| 1237 | goto end; |
| 1238 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1239 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1240 | if (!chunk_dup(*sctl, &trash)) { |
| 1241 | free(*sctl); |
| 1242 | *sctl = NULL; |
| 1243 | goto end; |
| 1244 | } |
| 1245 | |
| 1246 | end: |
| 1247 | if (fd != -1) |
| 1248 | close(fd); |
| 1249 | |
| 1250 | return ret; |
| 1251 | } |
| 1252 | |
| 1253 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1254 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1255 | struct chunk *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1256 | |
| 1257 | *out = (unsigned char *)sctl->str; |
| 1258 | *outlen = sctl->len; |
| 1259 | |
| 1260 | return 1; |
| 1261 | } |
| 1262 | |
| 1263 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1264 | { |
| 1265 | return 1; |
| 1266 | } |
| 1267 | |
| 1268 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 1269 | { |
| 1270 | char sctl_path[MAXPATHLEN+1]; |
| 1271 | int ret = -1; |
| 1272 | struct stat st; |
| 1273 | struct chunk *sctl = NULL; |
| 1274 | |
| 1275 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 1276 | |
| 1277 | if (stat(sctl_path, &st)) |
| 1278 | return 1; |
| 1279 | |
| 1280 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 1281 | goto out; |
| 1282 | |
| 1283 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 1284 | free(sctl); |
| 1285 | goto out; |
| 1286 | } |
| 1287 | |
| 1288 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1289 | |
| 1290 | ret = 0; |
| 1291 | |
| 1292 | out: |
| 1293 | return ret; |
| 1294 | } |
| 1295 | |
| 1296 | #endif |
| 1297 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1298 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1299 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1300 | struct connection *conn = SSL_get_app_data(ssl); |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1301 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1302 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1303 | |
| 1304 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1305 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1306 | if (conn->flags & CO_FL_CONNECTED) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1307 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1308 | conn->err_code = CO_ER_SSL_RENEG; |
| 1309 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1310 | } |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1311 | |
| 1312 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 1313 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 1314 | /* Long certificate chains optimz |
| 1315 | If write and read bios are differents, we |
| 1316 | consider that the buffering was activated, |
| 1317 | so we rise the output buffer size from 4k |
| 1318 | to 16k */ |
| 1319 | write_bio = SSL_get_wbio(ssl); |
| 1320 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1321 | BIO_set_write_buffer_size(write_bio, 16384); |
| 1322 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 1323 | } |
| 1324 | } |
| 1325 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1326 | } |
| 1327 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1328 | /* Callback is called for each certificate of the chain during a verify |
| 1329 | ok is set to 1 if preverify detect no error on current certificate. |
| 1330 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1331 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1332 | { |
| 1333 | SSL *ssl; |
| 1334 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1335 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1336 | |
| 1337 | 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] | 1338 | conn = SSL_get_app_data(ssl); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1339 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1340 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1341 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1342 | if (ok) /* no errors */ |
| 1343 | return ok; |
| 1344 | |
| 1345 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1346 | err = X509_STORE_CTX_get_error(x_store); |
| 1347 | |
| 1348 | /* check if CA error needs to be ignored */ |
| 1349 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1350 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 1351 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1352 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1353 | } |
| 1354 | |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1355 | 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] | 1356 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1357 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1358 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1359 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1360 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1361 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1362 | return 0; |
| 1363 | } |
| 1364 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1365 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 1366 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1367 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1368 | /* check if certificate error needs to be ignored */ |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1369 | 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] | 1370 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1371 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1372 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1373 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1374 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1375 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1376 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1377 | } |
| 1378 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1379 | static inline |
| 1380 | 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] | 1381 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1382 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1383 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1384 | unsigned char *msg; |
| 1385 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1386 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1387 | |
| 1388 | /* This function is called for "from client" and "to server" |
| 1389 | * connections. The combination of write_p == 0 and content_type == 22 |
| 1390 | * is only avalaible during "from client" connection. |
| 1391 | */ |
| 1392 | |
| 1393 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1394 | * otherwise it is set to 1. |
| 1395 | */ |
| 1396 | if (write_p != 0) |
| 1397 | return; |
| 1398 | |
| 1399 | /* content_type contains the type of message received or sent |
| 1400 | * according with the SSL/TLS protocol spec. This message is |
| 1401 | * encoded with one byte. The value 256 (two bytes) is used |
| 1402 | * for designing the SSL/TLS record layer. According with the |
| 1403 | * rfc6101, the expected message (other than 256) are: |
| 1404 | * - change_cipher_spec(20) |
| 1405 | * - alert(21) |
| 1406 | * - handshake(22) |
| 1407 | * - application_data(23) |
| 1408 | * - (255) |
| 1409 | * We are interessed by the handshake and specially the client |
| 1410 | * hello. |
| 1411 | */ |
| 1412 | if (content_type != 22) |
| 1413 | return; |
| 1414 | |
| 1415 | /* The message length is at least 4 bytes, containing the |
| 1416 | * message type and the message length. |
| 1417 | */ |
| 1418 | if (len < 4) |
| 1419 | return; |
| 1420 | |
| 1421 | /* First byte of the handshake message id the type of |
| 1422 | * message. The konwn types are: |
| 1423 | * - hello_request(0) |
| 1424 | * - client_hello(1) |
| 1425 | * - server_hello(2) |
| 1426 | * - certificate(11) |
| 1427 | * - server_key_exchange (12) |
| 1428 | * - certificate_request(13) |
| 1429 | * - server_hello_done(14) |
| 1430 | * We are interested by the client hello. |
| 1431 | */ |
| 1432 | msg = (unsigned char *)buf; |
| 1433 | if (msg[0] != 1) |
| 1434 | return; |
| 1435 | |
| 1436 | /* Next three bytes are the length of the message. The total length |
| 1437 | * must be this decoded length + 4. If the length given as argument |
| 1438 | * is not the same, we abort the protocol dissector. |
| 1439 | */ |
| 1440 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1441 | if (len < rec_len + 4) |
| 1442 | return; |
| 1443 | msg += 4; |
| 1444 | end = msg + rec_len; |
| 1445 | if (end < msg) |
| 1446 | return; |
| 1447 | |
| 1448 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1449 | * for minor, the random, composed by 4 bytes for the unix time and |
| 1450 | * 28 bytes for unix payload, and them 1 byte for the session id. So |
| 1451 | * we jump 1 + 1 + 4 + 28 + 1 bytes. |
| 1452 | */ |
| 1453 | msg += 1 + 1 + 4 + 28 + 1; |
| 1454 | if (msg > end) |
| 1455 | return; |
| 1456 | |
| 1457 | /* Next two bytes are the ciphersuite length. */ |
| 1458 | if (msg + 2 > end) |
| 1459 | return; |
| 1460 | rec_len = (msg[0] << 8) + msg[1]; |
| 1461 | msg += 2; |
| 1462 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1463 | return; |
| 1464 | |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1465 | capture = pool_alloc_dirty(pool2_ssl_capture); |
| 1466 | if (!capture) |
| 1467 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1468 | /* Compute the xxh64 of the ciphersuite. */ |
| 1469 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1470 | |
| 1471 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1472 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1473 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1474 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1475 | |
| 1476 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1477 | } |
| 1478 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1479 | /* Callback is called for ssl protocol analyse */ |
| 1480 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1481 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1482 | #ifdef TLS1_RT_HEARTBEAT |
| 1483 | /* test heartbeat received (write_p is set to 0 |
| 1484 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1485 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1486 | struct connection *conn = SSL_get_app_data(ssl); |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1487 | const unsigned char *p = buf; |
| 1488 | unsigned int payload; |
| 1489 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1490 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1491 | |
| 1492 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1493 | if (*p != TLS1_HB_REQUEST) |
| 1494 | return; |
| 1495 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1496 | 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] | 1497 | goto kill_it; |
| 1498 | |
| 1499 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1500 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1501 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1502 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1503 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1504 | * advertised payload is larger than the advertised packet |
| 1505 | * length, so we have garbage in the buffer between the |
| 1506 | * payload and the end of the buffer (p+len). We can't know |
| 1507 | * if the SSL stack is patched, and we don't know if we can |
| 1508 | * safely wipe out the area between p+3+len and payload. |
| 1509 | * So instead, we prevent the response from being sent by |
| 1510 | * setting the max_send_fragment to 0 and we report an SSL |
| 1511 | * error, which will kill this connection. It will be reported |
| 1512 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1513 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1514 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1515 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1516 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1517 | return; |
| 1518 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1519 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1520 | if (global_ssl.capture_cipherlist > 0) |
| 1521 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1522 | } |
| 1523 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1524 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 1525 | /* This callback is used so that the server advertises the list of |
| 1526 | * negociable protocols for NPN. |
| 1527 | */ |
| 1528 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1529 | unsigned int *len, void *arg) |
| 1530 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1531 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1532 | |
| 1533 | *data = (const unsigned char *)conf->npn_str; |
| 1534 | *len = conf->npn_len; |
| 1535 | return SSL_TLSEXT_ERR_OK; |
| 1536 | } |
| 1537 | #endif |
| 1538 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1539 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1540 | /* This callback is used so that the server advertises the list of |
| 1541 | * negociable protocols for ALPN. |
| 1542 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1543 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1544 | unsigned char *outlen, |
| 1545 | const unsigned char *server, |
| 1546 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1547 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1548 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1549 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1550 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1551 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1552 | return SSL_TLSEXT_ERR_NOACK; |
| 1553 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1554 | return SSL_TLSEXT_ERR_OK; |
| 1555 | } |
| 1556 | #endif |
| 1557 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1558 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1559 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1560 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1561 | /* Create a X509 certificate with the specified servername and serial. This |
| 1562 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1563 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1564 | 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] | 1565 | { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1566 | static unsigned int serial = 0; |
| 1567 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1568 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1569 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1570 | SSL_CTX *ssl_ctx = NULL; |
| 1571 | X509 *newcrt = NULL; |
| 1572 | EVP_PKEY *pkey = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1573 | X509_NAME *name; |
| 1574 | const EVP_MD *digest; |
| 1575 | X509V3_CTX ctx; |
| 1576 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1577 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1578 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1579 | /* Get the private key of the defautl certificate and use it */ |
| 1580 | if (!(pkey = SSL_get_privatekey(ssl))) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1581 | goto mkcert_error; |
| 1582 | |
| 1583 | /* Create the certificate */ |
| 1584 | if (!(newcrt = X509_new())) |
| 1585 | goto mkcert_error; |
| 1586 | |
| 1587 | /* Set version number for the certificate (X509v3) and the serial |
| 1588 | * number */ |
| 1589 | if (X509_set_version(newcrt, 2L) != 1) |
| 1590 | goto mkcert_error; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1591 | if (!serial) |
| 1592 | serial = now_ms; |
| 1593 | ASN1_INTEGER_set(X509_get_serialNumber(newcrt), serial++); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1594 | |
| 1595 | /* Set duration for the certificate */ |
| 1596 | if (!X509_gmtime_adj(X509_get_notBefore(newcrt), (long)-60*60*24) || |
| 1597 | !X509_gmtime_adj(X509_get_notAfter(newcrt),(long)60*60*24*365)) |
| 1598 | goto mkcert_error; |
| 1599 | |
| 1600 | /* set public key in the certificate */ |
| 1601 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1602 | goto mkcert_error; |
| 1603 | |
| 1604 | /* Set issuer name from the CA */ |
| 1605 | if (!(name = X509_get_subject_name(cacert))) |
| 1606 | goto mkcert_error; |
| 1607 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1608 | goto mkcert_error; |
| 1609 | |
| 1610 | /* Set the subject name using the same, but the CN */ |
| 1611 | name = X509_NAME_dup(name); |
| 1612 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1613 | (const unsigned char *)servername, |
| 1614 | -1, -1, 0) != 1) { |
| 1615 | X509_NAME_free(name); |
| 1616 | goto mkcert_error; |
| 1617 | } |
| 1618 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1619 | X509_NAME_free(name); |
| 1620 | goto mkcert_error; |
| 1621 | } |
| 1622 | X509_NAME_free(name); |
| 1623 | |
| 1624 | /* Add x509v3 extensions as specified */ |
| 1625 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1626 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1627 | X509_EXTENSION *ext; |
| 1628 | |
| 1629 | if (!(ext = X509V3_EXT_conf(NULL, &ctx, x509v3_ext_names[i], x509v3_ext_values[i]))) |
| 1630 | goto mkcert_error; |
| 1631 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1632 | X509_EXTENSION_free(ext); |
| 1633 | goto mkcert_error; |
| 1634 | } |
| 1635 | X509_EXTENSION_free(ext); |
| 1636 | } |
| 1637 | |
| 1638 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1639 | |
| 1640 | key_type = EVP_PKEY_base_id(capkey); |
| 1641 | |
| 1642 | if (key_type == EVP_PKEY_DSA) |
| 1643 | digest = EVP_sha1(); |
| 1644 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1645 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1646 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1647 | digest = EVP_sha256(); |
| 1648 | else { |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1649 | #if (OPENSSL_VERSION_NUMBER >= 0x1000000fL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1650 | int nid; |
| 1651 | |
| 1652 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1653 | goto mkcert_error; |
| 1654 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1655 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1656 | #else |
| 1657 | goto mkcert_error; |
| 1658 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1659 | } |
| 1660 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1661 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1662 | goto mkcert_error; |
| 1663 | |
| 1664 | /* Create and set the new SSL_CTX */ |
| 1665 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1666 | goto mkcert_error; |
| 1667 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1668 | goto mkcert_error; |
| 1669 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1670 | goto mkcert_error; |
| 1671 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1672 | goto mkcert_error; |
| 1673 | |
| 1674 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1675 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1676 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1677 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1678 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1679 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1680 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1681 | 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] | 1682 | EC_KEY *ecc; |
| 1683 | int nid; |
| 1684 | |
| 1685 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1686 | goto end; |
| 1687 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1688 | goto end; |
| 1689 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1690 | EC_KEY_free(ecc); |
| 1691 | } |
| 1692 | #endif |
| 1693 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1694 | return ssl_ctx; |
| 1695 | |
| 1696 | mkcert_error: |
| 1697 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1698 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1699 | return NULL; |
| 1700 | } |
| 1701 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1702 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1703 | 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] | 1704 | { |
| 1705 | struct bind_conf *bind_conf = objt_listener(conn->target)->bind_conf; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1706 | |
| 1707 | return ssl_sock_do_create_cert(servername, bind_conf, conn->xprt_ctx); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1708 | } |
| 1709 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1710 | /* Do a lookup for a certificate in the LRU cache used to store generated |
| 1711 | * certificates. */ |
| 1712 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1713 | 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] | 1714 | { |
| 1715 | struct lru64 *lru = NULL; |
| 1716 | |
| 1717 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1718 | 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] | 1719 | if (lru && lru->domain) |
| 1720 | return (SSL_CTX *)lru->data; |
| 1721 | } |
| 1722 | return NULL; |
| 1723 | } |
| 1724 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1725 | /* Set a certificate int the LRU cache used to store generated |
| 1726 | * certificate. Return 0 on success, otherwise -1 */ |
| 1727 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1728 | 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] | 1729 | { |
| 1730 | struct lru64 *lru = NULL; |
| 1731 | |
| 1732 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1733 | 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] | 1734 | if (!lru) |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1735 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1736 | if (lru->domain && lru->data) |
| 1737 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1738 | 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] | 1739 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1740 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1741 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1742 | } |
| 1743 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1744 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1745 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1746 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1747 | { |
| 1748 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 1749 | } |
| 1750 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1751 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 1752 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 1753 | */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1754 | static SSL_CTX * |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1755 | 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] | 1756 | { |
| 1757 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1758 | SSL_CTX *ssl_ctx = NULL; |
| 1759 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1760 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1761 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1762 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1763 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1764 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1765 | if (lru && lru->domain) |
| 1766 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1767 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1768 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1769 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1770 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1771 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1772 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1773 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1774 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1775 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 1776 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 1777 | SSL_CTX_free(ssl_ctx); |
| 1778 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1779 | return ssl_ctx; |
| 1780 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1781 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1782 | |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1783 | |
| 1784 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 1785 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 1786 | #endif |
| 1787 | |
| 1788 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 1789 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
| 1790 | #define SSL_renegotiate_pending(arg) 0 |
| 1791 | #endif |
| 1792 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 1793 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1794 | #endif |
| 1795 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 1796 | #define SSL_OP_NO_TICKET 0 |
| 1797 | #endif |
| 1798 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 1799 | #define SSL_OP_NO_COMPRESSION 0 |
| 1800 | #endif |
| 1801 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 1802 | #define SSL_OP_NO_TLSv1_1 0 |
| 1803 | #endif |
| 1804 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 1805 | #define SSL_OP_NO_TLSv1_2 0 |
| 1806 | #endif |
| 1807 | #ifndef SSL_OP_NO_TLSv1_3 /* dev */ |
| 1808 | #define SSL_OP_NO_TLSv1_3 0 |
| 1809 | #endif |
| 1810 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 1811 | #define SSL_OP_SINGLE_DH_USE 0 |
| 1812 | #endif |
| 1813 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 1814 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1815 | #endif |
| 1816 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 1817 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 1818 | #endif |
| 1819 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 1820 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 1821 | #endif |
| 1822 | |
| 1823 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1824 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 1825 | |
| 1826 | 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] | 1827 | { |
| 1828 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1829 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1830 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 1831 | #endif |
| 1832 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1833 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 1834 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1835 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 1836 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1837 | 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] | 1838 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1839 | 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] | 1840 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 1841 | #endif |
| 1842 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1843 | 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] | 1844 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1845 | 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] | 1846 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 1847 | #endif |
| 1848 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1849 | /* TLS 1.2 is the last supported version in this context. */ |
| 1850 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 1851 | /* Unusable in this context. */ |
| 1852 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 1853 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 1854 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 1855 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 1856 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1857 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1858 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 1859 | |
| 1860 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 1861 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1862 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 1863 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1864 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 1865 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 1866 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 1867 | } |
| 1868 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 1869 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1870 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 1871 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1872 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 1873 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 1874 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 1875 | } |
| 1876 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 1877 | 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] | 1878 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 1879 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1880 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 1881 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 1882 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 1883 | } |
| 1884 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 1885 | 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] | 1886 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 1887 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1888 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 1889 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 1890 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 1891 | } |
| 1892 | 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] | 1893 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1894 | 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] | 1895 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1896 | #endif |
| 1897 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1898 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 1899 | #if SSL_OP_NO_TLSv1_3 |
| 1900 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 1901 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1902 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1903 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1904 | #endif |
| 1905 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 1906 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1907 | |
| 1908 | static struct { |
| 1909 | int option; |
| 1910 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1911 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 1912 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1913 | const char *name; |
| 1914 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 1915 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 1916 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 1917 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 1918 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 1919 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 1920 | {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] | 1921 | }; |
| 1922 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 1923 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 1924 | { |
| 1925 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 1926 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 1927 | SSL_set_SSL_CTX(ssl, ctx); |
| 1928 | } |
| 1929 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1930 | #ifdef OPENSSL_IS_BORINGSSL |
| 1931 | |
| 1932 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 1933 | { |
| 1934 | (void)al; /* shut gcc stupid warning */ |
| 1935 | (void)priv; |
| 1936 | |
| 1937 | if (!SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name)) |
| 1938 | return SSL_TLSEXT_ERR_NOACK; |
| 1939 | return SSL_TLSEXT_ERR_OK; |
| 1940 | } |
| 1941 | |
| 1942 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 1943 | { |
| 1944 | struct connection *conn; |
| 1945 | struct bind_conf *s; |
| 1946 | const uint8_t *extension_data; |
| 1947 | size_t extension_len; |
| 1948 | CBS extension, cipher_suites, server_name_list, host_name, sig_algs; |
| 1949 | const SSL_CIPHER *cipher; |
| 1950 | uint16_t cipher_suite; |
| 1951 | uint8_t name_type, hash, sign; |
| 1952 | int has_rsa = 0, has_ecdsa = 0, has_ecdsa_sig = 0; |
| 1953 | |
| 1954 | char *wildp = NULL; |
| 1955 | const uint8_t *servername; |
| 1956 | struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL; |
| 1957 | int i; |
| 1958 | |
| 1959 | conn = SSL_get_app_data(ctx->ssl); |
| 1960 | s = objt_listener(conn->target)->bind_conf; |
| 1961 | |
| 1962 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 1963 | &extension_data, &extension_len)) { |
| 1964 | CBS_init(&extension, extension_data, extension_len); |
| 1965 | |
| 1966 | if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) |
| 1967 | || !CBS_get_u8(&server_name_list, &name_type) |
| 1968 | /* Although the server_name extension was intended to be extensible to |
| 1969 | * new name types and multiple names, OpenSSL 1.0.x had a bug which meant |
| 1970 | * different name types will cause an error. Further, RFC 4366 originally |
| 1971 | * defined syntax inextensibly. RFC 6066 corrected this mistake, but |
| 1972 | * adding new name types is no longer feasible. |
| 1973 | * |
| 1974 | * Act as if the extensibility does not exist to simplify parsing. */ |
| 1975 | || !CBS_get_u16_length_prefixed(&server_name_list, &host_name) |
| 1976 | || CBS_len(&server_name_list) != 0 |
| 1977 | || CBS_len(&extension) != 0 |
| 1978 | || name_type != TLSEXT_NAMETYPE_host_name |
| 1979 | || CBS_len(&host_name) == 0 |
| 1980 | || CBS_len(&host_name) > TLSEXT_MAXLEN_host_name |
| 1981 | || CBS_contains_zero_byte(&host_name)) { |
| 1982 | goto abort; |
| 1983 | } |
| 1984 | } else { |
| 1985 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 1986 | if (!s->strict_sni) { |
| 1987 | ssl_sock_switchctx_set(ctx->ssl, s->default_ctx); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1988 | return 1; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 1989 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1990 | goto abort; |
| 1991 | } |
| 1992 | |
| 1993 | /* extract/check clientHello informations */ |
| 1994 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 1995 | CBS_init(&extension, extension_data, extension_len); |
| 1996 | |
| 1997 | if (!CBS_get_u16_length_prefixed(&extension, &sig_algs) |
| 1998 | || CBS_len(&sig_algs) == 0 |
| 1999 | || CBS_len(&extension) != 0) { |
| 2000 | goto abort; |
| 2001 | } |
| 2002 | if (CBS_len(&sig_algs) % 2 != 0) { |
| 2003 | goto abort; |
| 2004 | } |
| 2005 | while (CBS_len(&sig_algs) != 0) { |
| 2006 | if (!CBS_get_u8(&sig_algs, &hash) |
| 2007 | || !CBS_get_u8(&sig_algs, &sign)) { |
| 2008 | goto abort; |
| 2009 | } |
| 2010 | switch (sign) { |
| 2011 | case TLSEXT_signature_rsa: |
| 2012 | has_rsa = 1; |
| 2013 | break; |
| 2014 | case TLSEXT_signature_ecdsa: |
| 2015 | has_ecdsa_sig = 1; |
| 2016 | break; |
| 2017 | default: |
| 2018 | continue; |
| 2019 | } |
| 2020 | if (has_ecdsa_sig && has_rsa) |
| 2021 | break; |
| 2022 | } |
| 2023 | } else { |
| 2024 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLS 1.2) */ |
| 2025 | has_rsa = 1; |
| 2026 | } |
| 2027 | if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */ |
| 2028 | CBS_init(&cipher_suites, ctx->cipher_suites, ctx->cipher_suites_len); |
| 2029 | |
| 2030 | while (CBS_len(&cipher_suites) != 0) { |
| 2031 | if (!CBS_get_u16(&cipher_suites, &cipher_suite)) { |
| 2032 | goto abort; |
| 2033 | } |
| 2034 | cipher = SSL_get_cipher_by_value(cipher_suite); |
| 2035 | if (cipher && SSL_CIPHER_is_ECDSA(cipher)) { |
| 2036 | has_ecdsa = 1; |
| 2037 | break; |
| 2038 | } |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | servername = CBS_data(&host_name); |
| 2043 | for (i = 0; i < trash.size && i < CBS_len(&host_name); i++) { |
| 2044 | trash.str[i] = tolower(servername[i]); |
| 2045 | if (!wildp && (trash.str[i] == '.')) |
| 2046 | wildp = &trash.str[i]; |
| 2047 | } |
| 2048 | trash.str[i] = 0; |
| 2049 | |
| 2050 | /* lookup in full qualified names */ |
| 2051 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 2052 | |
| 2053 | /* lookup a not neg filter */ |
| 2054 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2055 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2056 | switch(container_of(n, struct sni_ctx, name)->key_sig) { |
| 2057 | case TLSEXT_signature_ecdsa: |
| 2058 | if (has_ecdsa) { |
| 2059 | node_ecdsa = n; |
| 2060 | goto find_one; |
| 2061 | } |
| 2062 | break; |
| 2063 | case TLSEXT_signature_rsa: |
| 2064 | if (has_rsa && !node_rsa) { |
| 2065 | node_rsa = n; |
| 2066 | if (!has_ecdsa) |
| 2067 | goto find_one; |
| 2068 | } |
| 2069 | break; |
| 2070 | default: /* TLSEXT_signature_anonymous */ |
| 2071 | if (!node_anonymous) |
| 2072 | node_anonymous = n; |
| 2073 | break; |
| 2074 | } |
| 2075 | } |
| 2076 | } |
| 2077 | if (wildp) { |
| 2078 | /* lookup in wildcards names */ |
| 2079 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2080 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2081 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2082 | switch(container_of(n, struct sni_ctx, name)->key_sig) { |
| 2083 | case TLSEXT_signature_ecdsa: |
| 2084 | if (has_ecdsa) { |
| 2085 | node_ecdsa = n; |
| 2086 | goto find_one; |
| 2087 | } |
| 2088 | break; |
| 2089 | case TLSEXT_signature_rsa: |
| 2090 | if (has_rsa && !node_rsa) { |
| 2091 | node_rsa = n; |
| 2092 | if (!has_ecdsa) |
| 2093 | goto find_one; |
| 2094 | } |
| 2095 | break; |
| 2096 | default: /* TLSEXT_signature_anonymous */ |
| 2097 | if (!node_anonymous) |
| 2098 | node_anonymous = n; |
| 2099 | break; |
| 2100 | } |
| 2101 | } |
| 2102 | } |
| 2103 | } |
| 2104 | find_one: |
| 2105 | /* select by key_signature priority order */ |
| 2106 | node = node_ecdsa ? node_ecdsa : (node_rsa ? node_rsa : node_anonymous); |
| 2107 | |
| 2108 | if (node) { |
| 2109 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2110 | ssl_sock_switchctx_set(ctx->ssl, container_of(node, struct sni_ctx, name)->ctx); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2111 | return 1; |
| 2112 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2113 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2114 | /* no certificate match, is the default_ctx */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2115 | ssl_sock_switchctx_set(ctx->ssl, s->default_ctx); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2116 | return 1; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2117 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2118 | abort: |
| 2119 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2120 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 2121 | return -1; |
| 2122 | } |
| 2123 | |
| 2124 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2125 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2126 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2127 | * warning when no match is found, which implies the default (first) cert |
| 2128 | * will keep being used. |
| 2129 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2130 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2131 | { |
| 2132 | const char *servername; |
| 2133 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2134 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2135 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2136 | int i; |
| 2137 | (void)al; /* shut gcc stupid warning */ |
| 2138 | |
| 2139 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2140 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2141 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 2142 | if (s->generate_certs) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2143 | struct connection *conn = SSL_get_app_data(ssl); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2144 | unsigned int key; |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 2145 | SSL_CTX *ctx; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2146 | |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 2147 | conn_get_to_addr(conn); |
| 2148 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2149 | key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to)); |
| 2150 | ctx = ssl_sock_get_generated_cert(key, s); |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 2151 | if (ctx) { |
| 2152 | /* switch ctx */ |
| 2153 | SSL_set_SSL_CTX(ssl, ctx); |
| 2154 | return SSL_TLSEXT_ERR_OK; |
| 2155 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2156 | } |
| 2157 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2158 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2159 | if (s->strict_sni) |
| 2160 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2161 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2162 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2163 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2164 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2165 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2166 | if (!servername[i]) |
| 2167 | break; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2168 | trash.str[i] = tolower(servername[i]); |
| 2169 | if (!wildp && (trash.str[i] == '.')) |
| 2170 | wildp = &trash.str[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2171 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2172 | trash.str[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2173 | |
| 2174 | /* lookup in full qualified names */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2175 | node = ebst_lookup(&s->sni_ctx, trash.str); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2176 | |
| 2177 | /* lookup a not neg filter */ |
| 2178 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2179 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2180 | node = n; |
| 2181 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2182 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2183 | } |
| 2184 | if (!node && wildp) { |
| 2185 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2186 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2187 | } |
| 2188 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2189 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2190 | SSL_CTX *ctx; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2191 | if (s->generate_certs && |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2192 | (ctx = ssl_sock_generate_certificate(servername, s, ssl))) { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2193 | /* switch ctx */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2194 | return SSL_TLSEXT_ERR_OK; |
| 2195 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2196 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2197 | if (s->strict_sni) |
| 2198 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2199 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2200 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2204 | 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] | 2205 | return SSL_TLSEXT_ERR_OK; |
| 2206 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2207 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2208 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2209 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2210 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2211 | |
| 2212 | static DH * ssl_get_dh_1024(void) |
| 2213 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2214 | static unsigned char dh1024_p[]={ |
| 2215 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2216 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2217 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2218 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2219 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2220 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2221 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2222 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2223 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2224 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2225 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2226 | }; |
| 2227 | static unsigned char dh1024_g[]={ |
| 2228 | 0x02, |
| 2229 | }; |
| 2230 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2231 | BIGNUM *p; |
| 2232 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2233 | DH *dh = DH_new(); |
| 2234 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2235 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2236 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2237 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2238 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2239 | DH_free(dh); |
| 2240 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2241 | } else { |
| 2242 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | return dh; |
| 2246 | } |
| 2247 | |
| 2248 | static DH *ssl_get_dh_2048(void) |
| 2249 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2250 | static unsigned char dh2048_p[]={ |
| 2251 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2252 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2253 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2254 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2255 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2256 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2257 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2258 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2259 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2260 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2261 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2262 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2263 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2264 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2265 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2266 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2267 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2268 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2269 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2270 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2271 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2272 | 0xB7,0x1F,0x77,0xF3, |
| 2273 | }; |
| 2274 | static unsigned char dh2048_g[]={ |
| 2275 | 0x02, |
| 2276 | }; |
| 2277 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2278 | BIGNUM *p; |
| 2279 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2280 | DH *dh = DH_new(); |
| 2281 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2282 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2283 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2284 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2285 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2286 | DH_free(dh); |
| 2287 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2288 | } else { |
| 2289 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2290 | } |
| 2291 | } |
| 2292 | return dh; |
| 2293 | } |
| 2294 | |
| 2295 | static DH *ssl_get_dh_4096(void) |
| 2296 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2297 | static unsigned char dh4096_p[]={ |
| 2298 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2299 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2300 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2301 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2302 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2303 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2304 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2305 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2306 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2307 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2308 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2309 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2310 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2311 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2312 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2313 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2314 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2315 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2316 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2317 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2318 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2319 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2320 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2321 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2322 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2323 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2324 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2325 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2326 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2327 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2328 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2329 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2330 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2331 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2332 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2333 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2334 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2335 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2336 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2337 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2338 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2339 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2340 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2341 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2342 | static unsigned char dh4096_g[]={ |
| 2343 | 0x02, |
| 2344 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2345 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2346 | BIGNUM *p; |
| 2347 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2348 | DH *dh = DH_new(); |
| 2349 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2350 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2351 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2352 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2353 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2354 | DH_free(dh); |
| 2355 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2356 | } else { |
| 2357 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2358 | } |
| 2359 | } |
| 2360 | return dh; |
| 2361 | } |
| 2362 | |
| 2363 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2364 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2365 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2366 | { |
| 2367 | DH *dh = NULL; |
| 2368 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2369 | int type; |
| 2370 | |
| 2371 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2372 | |
| 2373 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2374 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2375 | */ |
| 2376 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2377 | keylen = EVP_PKEY_bits(pkey); |
| 2378 | } |
| 2379 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2380 | if (keylen > global_ssl.default_dh_param) { |
| 2381 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2382 | } |
| 2383 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2384 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2385 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2386 | } |
| 2387 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2388 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2389 | } |
| 2390 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2391 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | return dh; |
| 2395 | } |
| 2396 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2397 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2398 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2399 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2400 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2401 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2402 | if (in == NULL) |
| 2403 | goto end; |
| 2404 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2405 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2406 | goto end; |
| 2407 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2408 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2409 | |
| 2410 | end: |
| 2411 | if (in) |
| 2412 | BIO_free(in); |
| 2413 | |
| 2414 | return dh; |
| 2415 | } |
| 2416 | |
| 2417 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2418 | { |
| 2419 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2420 | |
| 2421 | if (global_dh) { |
| 2422 | return 0; |
| 2423 | } |
| 2424 | |
| 2425 | return -1; |
| 2426 | } |
| 2427 | |
| 2428 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 2429 | if an error occured, and 0 if parameter not found. */ |
| 2430 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 2431 | { |
| 2432 | int ret = -1; |
| 2433 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 2434 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2435 | if (dh) { |
| 2436 | ret = 1; |
| 2437 | SSL_CTX_set_tmp_dh(ctx, dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2438 | |
| 2439 | if (ssl_dh_ptr_index >= 0) { |
| 2440 | /* store a pointer to the DH params to avoid complaining about |
| 2441 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2442 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2443 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2444 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2445 | else if (global_dh) { |
| 2446 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 2447 | ret = 0; /* DH params not found */ |
| 2448 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2449 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2450 | /* Clear openssl global errors stack */ |
| 2451 | ERR_clear_error(); |
| 2452 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2453 | if (global_ssl.default_dh_param <= 1024) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2454 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 2455 | if (local_dh_1024 == NULL) |
| 2456 | local_dh_1024 = ssl_get_dh_1024(); |
| 2457 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2458 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2459 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2460 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2461 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2462 | } |
| 2463 | else { |
| 2464 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2465 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2466 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2467 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2468 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2469 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2470 | end: |
| 2471 | if (dh) |
| 2472 | DH_free(dh); |
| 2473 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2474 | return ret; |
| 2475 | } |
| 2476 | #endif |
| 2477 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2478 | static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, struct ssl_bind_conf *conf, |
| 2479 | uint8_t key_sig, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2480 | { |
| 2481 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2482 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2483 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2484 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2485 | if (*name == '!') { |
| 2486 | neg = 1; |
| 2487 | name++; |
| 2488 | } |
| 2489 | if (*name == '*') { |
| 2490 | wild = 1; |
| 2491 | name++; |
| 2492 | } |
| 2493 | /* !* filter is a nop */ |
| 2494 | if (neg && wild) |
| 2495 | return order; |
| 2496 | if (*name) { |
| 2497 | int j, len; |
| 2498 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2499 | for (j = 0; j < len && j < trash.size; j++) |
| 2500 | trash.str[j] = tolower(name[j]); |
| 2501 | if (j >= trash.size) |
| 2502 | return order; |
| 2503 | trash.str[j] = 0; |
| 2504 | |
| 2505 | /* Check for duplicates. */ |
| 2506 | if (wild) |
| 2507 | node = ebst_lookup(&s->sni_w_ctx, trash.str); |
| 2508 | else |
| 2509 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 2510 | for (; node; node = ebmb_next_dup(node)) { |
| 2511 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2512 | if (sc->ctx == ctx && sc->conf == conf && |
| 2513 | sc->key_sig == key_sig && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2514 | return order; |
| 2515 | } |
| 2516 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2517 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2518 | if (!sc) |
| 2519 | return order; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2520 | memcpy(sc->name.key, trash.str, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2521 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2522 | sc->conf = conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2523 | sc->key_sig = key_sig; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2524 | sc->order = order++; |
| 2525 | sc->neg = neg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2526 | if (wild) |
| 2527 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 2528 | else |
| 2529 | ebst_insert(&s->sni_ctx, &sc->name); |
| 2530 | } |
| 2531 | return order; |
| 2532 | } |
| 2533 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2534 | |
| 2535 | /* The following code is used for loading multiple crt files into |
| 2536 | * SSL_CTX's based on CN/SAN |
| 2537 | */ |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 2538 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(LIBRESSL_VERSION_NUMBER) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2539 | /* This is used to preload the certifcate, private key |
| 2540 | * and Cert Chain of a file passed in via the crt |
| 2541 | * argument |
| 2542 | * |
| 2543 | * This way, we do not have to read the file multiple times |
| 2544 | */ |
| 2545 | struct cert_key_and_chain { |
| 2546 | X509 *cert; |
| 2547 | EVP_PKEY *key; |
| 2548 | unsigned int num_chain_certs; |
| 2549 | /* This is an array of X509 pointers */ |
| 2550 | X509 **chain_certs; |
| 2551 | }; |
| 2552 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2553 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 2554 | |
| 2555 | struct key_combo_ctx { |
| 2556 | SSL_CTX *ctx; |
| 2557 | int order; |
| 2558 | }; |
| 2559 | |
| 2560 | /* Map used for processing multiple keypairs for a single purpose |
| 2561 | * |
| 2562 | * This maps CN/SNI name to certificate type |
| 2563 | */ |
| 2564 | struct sni_keytype { |
| 2565 | int keytypes; /* BITMASK for keytypes */ |
| 2566 | struct ebmb_node name; /* node holding the servername value */ |
| 2567 | }; |
| 2568 | |
| 2569 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2570 | /* Frees the contents of a cert_key_and_chain |
| 2571 | */ |
| 2572 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2573 | { |
| 2574 | int i; |
| 2575 | |
| 2576 | if (!ckch) |
| 2577 | return; |
| 2578 | |
| 2579 | /* Free the certificate and set pointer to NULL */ |
| 2580 | if (ckch->cert) |
| 2581 | X509_free(ckch->cert); |
| 2582 | ckch->cert = NULL; |
| 2583 | |
| 2584 | /* Free the key and set pointer to NULL */ |
| 2585 | if (ckch->key) |
| 2586 | EVP_PKEY_free(ckch->key); |
| 2587 | ckch->key = NULL; |
| 2588 | |
| 2589 | /* Free each certificate in the chain */ |
| 2590 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2591 | if (ckch->chain_certs[i]) |
| 2592 | X509_free(ckch->chain_certs[i]); |
| 2593 | } |
| 2594 | |
| 2595 | /* Free the chain obj itself and set to NULL */ |
| 2596 | if (ckch->num_chain_certs > 0) { |
| 2597 | free(ckch->chain_certs); |
| 2598 | ckch->num_chain_certs = 0; |
| 2599 | ckch->chain_certs = NULL; |
| 2600 | } |
| 2601 | |
| 2602 | } |
| 2603 | |
| 2604 | /* checks if a key and cert exists in the ckch |
| 2605 | */ |
| 2606 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 2607 | { |
| 2608 | return (ckch->cert != NULL && ckch->key != NULL); |
| 2609 | } |
| 2610 | |
| 2611 | |
| 2612 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 2613 | * This allows us to carry the contents of the file without having to |
| 2614 | * read the file multiple times. |
| 2615 | * |
| 2616 | * returns: |
| 2617 | * 0 on Success |
| 2618 | * 1 on SSL Failure |
| 2619 | * 2 on file not found |
| 2620 | */ |
| 2621 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 2622 | { |
| 2623 | |
| 2624 | BIO *in; |
| 2625 | X509 *ca = NULL; |
| 2626 | int ret = 1; |
| 2627 | |
| 2628 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2629 | |
| 2630 | in = BIO_new(BIO_s_file()); |
| 2631 | if (in == NULL) |
| 2632 | goto end; |
| 2633 | |
| 2634 | if (BIO_read_filename(in, path) <= 0) |
| 2635 | goto end; |
| 2636 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2637 | /* Read Private Key */ |
| 2638 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 2639 | if (ckch->key == NULL) { |
| 2640 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 2641 | err && *err ? *err : "", path); |
| 2642 | goto end; |
| 2643 | } |
| 2644 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2645 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 2646 | if (BIO_reset(in) == -1) { |
| 2647 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 2648 | err && *err ? *err : "", path); |
| 2649 | goto end; |
| 2650 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2651 | |
| 2652 | /* Read Certificate */ |
| 2653 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 2654 | if (ckch->cert == NULL) { |
| 2655 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 2656 | err && *err ? *err : "", path); |
| 2657 | goto end; |
| 2658 | } |
| 2659 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2660 | /* Read Certificate Chain */ |
| 2661 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 2662 | /* Grow the chain certs */ |
| 2663 | ckch->num_chain_certs++; |
| 2664 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 2665 | |
| 2666 | /* use - 1 here since we just incremented it above */ |
| 2667 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 2668 | } |
| 2669 | ret = ERR_get_error(); |
| 2670 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 2671 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 2672 | err && *err ? *err : "", path); |
| 2673 | ret = 1; |
| 2674 | goto end; |
| 2675 | } |
| 2676 | |
| 2677 | ret = 0; |
| 2678 | |
| 2679 | end: |
| 2680 | |
| 2681 | ERR_clear_error(); |
| 2682 | if (in) |
| 2683 | BIO_free(in); |
| 2684 | |
| 2685 | /* Something went wrong in one of the reads */ |
| 2686 | if (ret != 0) |
| 2687 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2688 | |
| 2689 | return ret; |
| 2690 | } |
| 2691 | |
| 2692 | /* Loads the info in ckch into ctx |
| 2693 | * Currently, this does not process any information about ocsp, dhparams or |
| 2694 | * sctl |
| 2695 | * Returns |
| 2696 | * 0 on success |
| 2697 | * 1 on failure |
| 2698 | */ |
| 2699 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2700 | { |
| 2701 | int i = 0; |
| 2702 | |
| 2703 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2704 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2705 | err && *err ? *err : "", path); |
| 2706 | return 1; |
| 2707 | } |
| 2708 | |
| 2709 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 2710 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 2711 | err && *err ? *err : "", path); |
| 2712 | return 1; |
| 2713 | } |
| 2714 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2715 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 2716 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2717 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2718 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 2719 | err && *err ? *err : "", (i+1), path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2720 | return 1; |
| 2721 | } |
| 2722 | } |
| 2723 | |
| 2724 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2725 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2726 | err && *err ? *err : "", path); |
| 2727 | return 1; |
| 2728 | } |
| 2729 | |
| 2730 | return 0; |
| 2731 | } |
| 2732 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2733 | |
| 2734 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 2735 | { |
| 2736 | struct sni_keytype *s_kt = NULL; |
| 2737 | struct ebmb_node *node; |
| 2738 | int i; |
| 2739 | |
| 2740 | for (i = 0; i < trash.size; i++) { |
| 2741 | if (!str[i]) |
| 2742 | break; |
| 2743 | trash.str[i] = tolower(str[i]); |
| 2744 | } |
| 2745 | trash.str[i] = 0; |
| 2746 | node = ebst_lookup(sni_keytypes, trash.str); |
| 2747 | if (!node) { |
| 2748 | /* CN not found in tree */ |
| 2749 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 2750 | /* Using memcpy here instead of strncpy. |
| 2751 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 2752 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 2753 | */ |
| 2754 | memcpy(s_kt->name.key, trash.str, i+1); |
| 2755 | s_kt->keytypes = 0; |
| 2756 | ebst_insert(sni_keytypes, &s_kt->name); |
| 2757 | } else { |
| 2758 | /* CN found in tree */ |
| 2759 | s_kt = container_of(node, struct sni_keytype, name); |
| 2760 | } |
| 2761 | |
| 2762 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 2763 | s_kt->keytypes |= 1<<key_index; |
| 2764 | |
| 2765 | } |
| 2766 | |
| 2767 | |
| 2768 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 2769 | * If any are found, group these files into a set of SSL_CTX* |
| 2770 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 2771 | * |
| 2772 | * This will allow the user to explictly group multiple cert/keys for a single purpose |
| 2773 | * |
| 2774 | * Returns |
| 2775 | * 0 on success |
| 2776 | * 1 on failure |
| 2777 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2778 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2779 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2780 | { |
| 2781 | char fp[MAXPATHLEN+1] = {0}; |
| 2782 | int n = 0; |
| 2783 | int i = 0; |
| 2784 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 2785 | struct eb_root sni_keytypes_map = { {0} }; |
| 2786 | struct ebmb_node *node; |
| 2787 | struct ebmb_node *next; |
| 2788 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 2789 | * of keytypes |
| 2790 | */ |
| 2791 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 2792 | int rv = 0; |
| 2793 | X509_NAME *xname = NULL; |
| 2794 | char *str = NULL; |
| 2795 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2796 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 2797 | #endif |
| 2798 | |
| 2799 | /* Load all possible certs and keys */ |
| 2800 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2801 | struct stat buf; |
| 2802 | |
| 2803 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2804 | if (stat(fp, &buf) == 0) { |
| 2805 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
| 2806 | rv = 1; |
| 2807 | goto end; |
| 2808 | } |
| 2809 | } |
| 2810 | } |
| 2811 | |
| 2812 | /* Process each ckch and update keytypes for each CN/SAN |
| 2813 | * for example, if CN/SAN www.a.com is associated with |
| 2814 | * certs with keytype 0 and 2, then at the end of the loop, |
| 2815 | * www.a.com will have: |
| 2816 | * keyindex = 0 | 1 | 4 = 5 |
| 2817 | */ |
| 2818 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2819 | |
| 2820 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 2821 | continue; |
| 2822 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2823 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 2824 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2825 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 2826 | } else { |
| 2827 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 2828 | * so the line that contains logic is marked via comments |
| 2829 | */ |
| 2830 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 2831 | i = -1; |
| 2832 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 2833 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2834 | ASN1_STRING *value; |
| 2835 | value = X509_NAME_ENTRY_get_data(entry); |
| 2836 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2837 | /* Important line is here */ |
| 2838 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2839 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2840 | OPENSSL_free(str); |
| 2841 | str = NULL; |
| 2842 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2843 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2844 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2845 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2846 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2847 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 2848 | if (names) { |
| 2849 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 2850 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2851 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2852 | if (name->type == GEN_DNS) { |
| 2853 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 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 | } |
| 2861 | } |
| 2862 | } |
| 2863 | } |
| 2864 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2865 | } |
| 2866 | |
| 2867 | /* If no files found, return error */ |
| 2868 | if (eb_is_empty(&sni_keytypes_map)) { |
| 2869 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 2870 | err && *err ? *err : "", path); |
| 2871 | rv = 1; |
| 2872 | goto end; |
| 2873 | } |
| 2874 | |
| 2875 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 2876 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 2877 | * and add each CTX to the SNI tree |
| 2878 | * |
| 2879 | * Some math here: |
| 2880 | * There are 2^n - 1 possibile combinations, each unique |
| 2881 | * combination is denoted by the key in the map. Each key |
| 2882 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 2883 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 2884 | * entry in the array to correspond to the unique combo (key) |
| 2885 | * associated with i. This unique key combo (i) will be associated |
| 2886 | * with combos[i-1] |
| 2887 | */ |
| 2888 | |
| 2889 | node = ebmb_first(&sni_keytypes_map); |
| 2890 | while (node) { |
| 2891 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2892 | char cur_file[MAXPATHLEN+1]; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2893 | |
| 2894 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 2895 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 2896 | cur_ctx = key_combos[i-1].ctx; |
| 2897 | |
| 2898 | if (cur_ctx == NULL) { |
| 2899 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2900 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2901 | if (cur_ctx == NULL) { |
| 2902 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 2903 | err && *err ? *err : ""); |
| 2904 | rv = 1; |
| 2905 | goto end; |
| 2906 | } |
| 2907 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2908 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2909 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2910 | if (i & (1<<n)) { |
| 2911 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2912 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2913 | 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] | 2914 | SSL_CTX_free(cur_ctx); |
| 2915 | rv = 1; |
| 2916 | goto end; |
| 2917 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2918 | |
| 2919 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 2920 | /* Load OCSP Info into context */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2921 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2922 | if (err) |
| 2923 | 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] | 2924 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2925 | SSL_CTX_free(cur_ctx); |
| 2926 | rv = 1; |
| 2927 | goto end; |
| 2928 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 2929 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 2930 | ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2931 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2932 | } |
| 2933 | } |
| 2934 | |
| 2935 | /* Load DH params into the ctx to support DHE keys */ |
| 2936 | #ifndef OPENSSL_NO_DH |
| 2937 | if (ssl_dh_ptr_index >= 0) |
| 2938 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 2939 | |
| 2940 | rv = ssl_sock_load_dh_params(cur_ctx, NULL); |
| 2941 | if (rv < 0) { |
| 2942 | if (err) |
| 2943 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 2944 | *err ? *err : "", path); |
| 2945 | rv = 1; |
| 2946 | goto end; |
| 2947 | } |
| 2948 | #endif |
| 2949 | |
| 2950 | /* Update key_combos */ |
| 2951 | key_combos[i-1].ctx = cur_ctx; |
| 2952 | } |
| 2953 | |
| 2954 | /* Update SNI Tree */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2955 | key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf, |
| 2956 | TLSEXT_signature_anonymous, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2957 | node = ebmb_next(node); |
| 2958 | } |
| 2959 | |
| 2960 | |
| 2961 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 2962 | if (!bind_conf->default_ctx) { |
| 2963 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 2964 | if (key_combos[i].ctx) { |
| 2965 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2966 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2967 | break; |
| 2968 | } |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | end: |
| 2973 | |
| 2974 | if (names) |
| 2975 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 2976 | |
| 2977 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 2978 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 2979 | |
| 2980 | node = ebmb_first(&sni_keytypes_map); |
| 2981 | while (node) { |
| 2982 | next = ebmb_next(node); |
| 2983 | ebmb_delete(node); |
| 2984 | node = next; |
| 2985 | } |
| 2986 | |
| 2987 | return rv; |
| 2988 | } |
| 2989 | #else |
| 2990 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2991 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2992 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2993 | { |
| 2994 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 2995 | err && *err ? *err : "", path, strerror(errno)); |
| 2996 | return 1; |
| 2997 | } |
| 2998 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2999 | #endif /* #if OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
| 3000 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3001 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 3002 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 3003 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3004 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, |
| 3005 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3006 | { |
| 3007 | BIO *in; |
| 3008 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3009 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3010 | int ret = -1; |
| 3011 | int order = 0; |
| 3012 | X509_NAME *xname; |
| 3013 | char *str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3014 | pem_password_cb *passwd_cb; |
| 3015 | void *passwd_cb_userdata; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3016 | EVP_PKEY *pkey; |
| 3017 | uint8_t key_sig = TLSEXT_signature_anonymous; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3018 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3019 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3020 | STACK_OF(GENERAL_NAME) *names; |
| 3021 | #endif |
| 3022 | |
| 3023 | in = BIO_new(BIO_s_file()); |
| 3024 | if (in == NULL) |
| 3025 | goto end; |
| 3026 | |
| 3027 | if (BIO_read_filename(in, file) <= 0) |
| 3028 | goto end; |
| 3029 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3030 | |
| 3031 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 3032 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 3033 | |
| 3034 | 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] | 3035 | if (x == NULL) |
| 3036 | goto end; |
| 3037 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3038 | pkey = X509_get_pubkey(x); |
| 3039 | if (pkey) { |
| 3040 | switch(EVP_PKEY_base_id(pkey)) { |
| 3041 | case EVP_PKEY_RSA: |
| 3042 | key_sig = TLSEXT_signature_rsa; |
| 3043 | break; |
| 3044 | case EVP_PKEY_EC: |
| 3045 | key_sig = TLSEXT_signature_ecdsa; |
| 3046 | break; |
| 3047 | } |
| 3048 | EVP_PKEY_free(pkey); |
| 3049 | } |
| 3050 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3051 | if (fcount) { |
| 3052 | while (fcount--) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3053 | 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] | 3054 | } |
| 3055 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3056 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3057 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 3058 | if (names) { |
| 3059 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3060 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3061 | if (name->type == GEN_DNS) { |
| 3062 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3063 | 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] | 3064 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3065 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3066 | } |
| 3067 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3068 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3069 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3070 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3071 | xname = X509_get_subject_name(x); |
| 3072 | i = -1; |
| 3073 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3074 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3075 | ASN1_STRING *value; |
| 3076 | |
| 3077 | value = X509_NAME_ENTRY_get_data(entry); |
| 3078 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3079 | 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] | 3080 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3081 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 3086 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 3087 | goto end; |
| 3088 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3089 | #ifdef SSL_CTX_clear_extra_chain_certs |
| 3090 | SSL_CTX_clear_extra_chain_certs(ctx); |
| 3091 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3092 | if (ctx->extra_certs != NULL) { |
| 3093 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 3094 | ctx->extra_certs = NULL; |
| 3095 | } |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3096 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3097 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3098 | 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] | 3099 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3100 | X509_free(ca); |
| 3101 | goto end; |
| 3102 | } |
| 3103 | } |
| 3104 | |
| 3105 | err = ERR_get_error(); |
| 3106 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3107 | /* we successfully reached the last cert in the file */ |
| 3108 | ret = 1; |
| 3109 | } |
| 3110 | ERR_clear_error(); |
| 3111 | |
| 3112 | end: |
| 3113 | if (x) |
| 3114 | X509_free(x); |
| 3115 | |
| 3116 | if (in) |
| 3117 | BIO_free(in); |
| 3118 | |
| 3119 | return ret; |
| 3120 | } |
| 3121 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3122 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3123 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3124 | { |
| 3125 | int ret; |
| 3126 | SSL_CTX *ctx; |
| 3127 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3128 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3129 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3130 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3131 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3132 | return 1; |
| 3133 | } |
| 3134 | |
| 3135 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3136 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 3137 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3138 | SSL_CTX_free(ctx); |
| 3139 | return 1; |
| 3140 | } |
| 3141 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3142 | 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] | 3143 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3144 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 3145 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3146 | if (ret < 0) /* serious error, must do that ourselves */ |
| 3147 | SSL_CTX_free(ctx); |
| 3148 | return 1; |
| 3149 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 3150 | |
| 3151 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3152 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3153 | err && *err ? *err : "", path); |
| 3154 | return 1; |
| 3155 | } |
| 3156 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3157 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3158 | * the tree, so it will be discovered and cleaned in time. |
| 3159 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3160 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3161 | /* store a NULL pointer to indicate we have not yet loaded |
| 3162 | a custom DH param file */ |
| 3163 | if (ssl_dh_ptr_index >= 0) { |
| 3164 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3165 | } |
| 3166 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3167 | ret = ssl_sock_load_dh_params(ctx, path); |
| 3168 | if (ret < 0) { |
| 3169 | if (err) |
| 3170 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3171 | *err ? *err : "", path); |
| 3172 | return 1; |
| 3173 | } |
| 3174 | #endif |
| 3175 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 3176 | #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] | 3177 | ret = ssl_sock_load_ocsp(ctx, path); |
| 3178 | if (ret < 0) { |
| 3179 | if (err) |
| 3180 | 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", |
| 3181 | *err ? *err : "", path); |
| 3182 | return 1; |
| 3183 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3184 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3185 | ssl_sock_set_ocsp_response_from_file(ctx, path); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3186 | #endif |
| 3187 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 3188 | #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] | 3189 | if (sctl_ex_index >= 0) { |
| 3190 | ret = ssl_sock_load_sctl(ctx, path); |
| 3191 | if (ret < 0) { |
| 3192 | if (err) |
| 3193 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 3194 | *err ? *err : "", path); |
| 3195 | return 1; |
| 3196 | } |
| 3197 | } |
| 3198 | #endif |
| 3199 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3200 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3201 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3202 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3203 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3204 | return 1; |
| 3205 | } |
| 3206 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3207 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3208 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3209 | bind_conf->default_ssl_conf = ssl_conf; |
| 3210 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3211 | |
| 3212 | return 0; |
| 3213 | } |
| 3214 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3215 | 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] | 3216 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3217 | struct dirent **de_list; |
| 3218 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3219 | DIR *dir; |
| 3220 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 3221 | char *end; |
| 3222 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3223 | int cfgerr = 0; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3224 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3225 | int is_bundle; |
| 3226 | int j; |
| 3227 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3228 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3229 | if (stat(path, &buf) == 0) { |
| 3230 | dir = opendir(path); |
| 3231 | if (!dir) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3232 | 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] | 3233 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3234 | /* strip trailing slashes, including first one */ |
| 3235 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 3236 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3237 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3238 | n = scandir(path, &de_list, 0, alphasort); |
| 3239 | if (n < 0) { |
| 3240 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 3241 | err && *err ? *err : "", path, strerror(errno)); |
| 3242 | cfgerr++; |
| 3243 | } |
| 3244 | else { |
| 3245 | for (i = 0; i < n; i++) { |
| 3246 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 3247 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3248 | end = strrchr(de->d_name, '.'); |
| 3249 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 3250 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3251 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3252 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 3253 | if (stat(fp, &buf) != 0) { |
| 3254 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3255 | err && *err ? *err : "", fp, strerror(errno)); |
| 3256 | cfgerr++; |
| 3257 | goto ignore_entry; |
| 3258 | } |
| 3259 | if (!S_ISREG(buf.st_mode)) |
| 3260 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3261 | |
| 3262 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3263 | is_bundle = 0; |
| 3264 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 3265 | |
| 3266 | if (end) { |
| 3267 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 3268 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 3269 | is_bundle = 1; |
| 3270 | break; |
| 3271 | } |
| 3272 | } |
| 3273 | |
| 3274 | if (is_bundle) { |
| 3275 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 3276 | int dp_len; |
| 3277 | |
| 3278 | dp_len = end - de->d_name; |
| 3279 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 3280 | |
| 3281 | /* increment i and free de until we get to a non-bundle cert |
| 3282 | * Note here that we look at de_list[i + 1] before freeing de |
| 3283 | * this is important since ignore_entry will free de |
| 3284 | */ |
| 3285 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 3286 | free(de); |
| 3287 | i++; |
| 3288 | de = de_list[i]; |
| 3289 | } |
| 3290 | |
| 3291 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3292 | ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3293 | |
| 3294 | /* Successfully processed the bundle */ |
| 3295 | goto ignore_entry; |
| 3296 | } |
| 3297 | } |
| 3298 | |
| 3299 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3300 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3301 | ignore_entry: |
| 3302 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3303 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3304 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3305 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3306 | closedir(dir); |
| 3307 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3308 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3309 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3310 | cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3311 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3312 | return cfgerr; |
| 3313 | } |
| 3314 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3315 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3316 | * done once. Zero is returned if the operation fails. No error is returned |
| 3317 | * if the random is said as not implemented, because we expect that openssl |
| 3318 | * will use another method once needed. |
| 3319 | */ |
| 3320 | static int ssl_initialize_random() |
| 3321 | { |
| 3322 | unsigned char random; |
| 3323 | static int random_initialized = 0; |
| 3324 | |
| 3325 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3326 | random_initialized = 1; |
| 3327 | |
| 3328 | return random_initialized; |
| 3329 | } |
| 3330 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3331 | /* release ssl bind conf */ |
| 3332 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3333 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3334 | if (conf) { |
| 3335 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 3336 | free(conf->npn_str); |
| 3337 | conf->npn_str = NULL; |
| 3338 | #endif |
| 3339 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 3340 | free(conf->alpn_str); |
| 3341 | conf->alpn_str = NULL; |
| 3342 | #endif |
| 3343 | free(conf->ca_file); |
| 3344 | conf->ca_file = NULL; |
| 3345 | free(conf->crl_file); |
| 3346 | conf->crl_file = NULL; |
| 3347 | free(conf->ciphers); |
| 3348 | conf->ciphers = NULL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3349 | free(conf->curves); |
| 3350 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3351 | free(conf->ecdhe); |
| 3352 | conf->ecdhe = NULL; |
| 3353 | } |
| 3354 | } |
| 3355 | |
| 3356 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 3357 | { |
| 3358 | char thisline[CRT_LINESIZE]; |
| 3359 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3360 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3361 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3362 | int linenum = 0; |
| 3363 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3364 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3365 | if ((f = fopen(file, "r")) == NULL) { |
| 3366 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3367 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3368 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3369 | |
| 3370 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3371 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3372 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3373 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3374 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3375 | char *crt_path; |
| 3376 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3377 | |
| 3378 | linenum++; |
| 3379 | end = line + strlen(line); |
| 3380 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 3381 | /* Check if we reached the limit and the last char is not \n. |
| 3382 | * Watch out for the last line without the terminating '\n'! |
| 3383 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3384 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 3385 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3386 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3387 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3388 | } |
| 3389 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3390 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3391 | newarg = 1; |
| 3392 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3393 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 3394 | /* end of string, end of loop */ |
| 3395 | *line = 0; |
| 3396 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3397 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3398 | newarg = 1; |
| 3399 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3400 | } else if (*line == '[') { |
| 3401 | if (ssl_b) { |
| 3402 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 3403 | cfgerr = 1; |
| 3404 | break; |
| 3405 | } |
| 3406 | if (!arg) { |
| 3407 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 3408 | cfgerr = 1; |
| 3409 | break; |
| 3410 | } |
| 3411 | ssl_b = arg; |
| 3412 | newarg = 1; |
| 3413 | *line = 0; |
| 3414 | } else if (*line == ']') { |
| 3415 | if (ssl_e) { |
| 3416 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3417 | cfgerr = 1; |
| 3418 | break; |
| 3419 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3420 | if (!ssl_b) { |
| 3421 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 3422 | cfgerr = 1; |
| 3423 | break; |
| 3424 | } |
| 3425 | ssl_e = arg; |
| 3426 | newarg = 1; |
| 3427 | *line = 0; |
| 3428 | } else if (newarg) { |
| 3429 | if (arg == MAX_CRT_ARGS) { |
| 3430 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 3431 | cfgerr = 1; |
| 3432 | break; |
| 3433 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3434 | newarg = 0; |
| 3435 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3436 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3437 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3438 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3439 | if (cfgerr) |
| 3440 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3441 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3442 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3443 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3444 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3445 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3446 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3447 | crt_path = args[0]; |
| 3448 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 3449 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 3450 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 3451 | crt_path, linenum, file); |
| 3452 | cfgerr = 1; |
| 3453 | break; |
| 3454 | } |
| 3455 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 3456 | crt_path = path; |
| 3457 | } |
| 3458 | |
| 3459 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 3460 | cur_arg = ssl_b ? ssl_b : 1; |
| 3461 | while (cur_arg < ssl_e) { |
| 3462 | newarg = 0; |
| 3463 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 3464 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 3465 | newarg = 1; |
| 3466 | cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
| 3467 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 3468 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 3469 | args[cur_arg], linenum, file); |
| 3470 | cfgerr = 1; |
| 3471 | } |
| 3472 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 3473 | break; |
| 3474 | } |
| 3475 | } |
| 3476 | if (!cfgerr && !newarg) { |
| 3477 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 3478 | args[cur_arg], linenum, file); |
| 3479 | cfgerr = 1; |
| 3480 | break; |
| 3481 | } |
| 3482 | } |
| 3483 | if (cfgerr) { |
| 3484 | ssl_sock_free_ssl_conf(ssl_conf); |
| 3485 | free(ssl_conf); |
| 3486 | ssl_conf = NULL; |
| 3487 | break; |
| 3488 | } |
| 3489 | |
| 3490 | if (stat(crt_path, &buf) == 0) { |
| 3491 | cfgerr = ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf, |
| 3492 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3493 | } else { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3494 | cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf, |
| 3495 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3496 | } |
| 3497 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3498 | if (cfgerr) { |
| 3499 | 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] | 3500 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3501 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3502 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3503 | fclose(f); |
| 3504 | return cfgerr; |
| 3505 | } |
| 3506 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3507 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3508 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3509 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3510 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3511 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3512 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3513 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3514 | SSL_OP_NO_SSLv2 | |
| 3515 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3516 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3517 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3518 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
| 3519 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3520 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3521 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3522 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3523 | SSL_MODE_RELEASE_BUFFERS | |
| 3524 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3525 | struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3526 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3527 | int flags = MC_SSL_O_ALL; |
| 3528 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3529 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3530 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3531 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3532 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3533 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
| 3534 | Warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3535 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3536 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3537 | else |
| 3538 | flags = conf_ssl_methods->flags; |
| 3539 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 3540 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3541 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3542 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3543 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3544 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3545 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 3546 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3547 | min = max = CONF_TLSV_NONE; |
| 3548 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3549 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3550 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3551 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3552 | if (min) { |
| 3553 | if (hole) { |
| 3554 | 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] | 3555 | "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] | 3556 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3557 | methodVersions[hole].name); |
| 3558 | hole = 0; |
| 3559 | } |
| 3560 | max = i; |
| 3561 | } |
| 3562 | else { |
| 3563 | min = max = i; |
| 3564 | } |
| 3565 | } |
| 3566 | else { |
| 3567 | if (min) |
| 3568 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3569 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3570 | if (!min) { |
| 3571 | 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] | 3572 | 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] | 3573 | cfgerr += 1; |
| 3574 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3575 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3576 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) && !defined(OPENSSL_IS_BORINGSSL) |
| 3577 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
| 3578 | precautionary measure to avoid any suprise with older openssl version. */ |
| 3579 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 3580 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3581 | else |
| 3582 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3583 | if (flags & methodVersions[i].flag) |
| 3584 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3585 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3586 | /* 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^] | 3587 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3588 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3589 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3590 | |
| 3591 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3592 | options |= SSL_OP_NO_TICKET; |
| 3593 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3594 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 3595 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3596 | |
| 3597 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 3598 | if (global_ssl.async) |
| 3599 | mode |= SSL_MODE_ASYNC; |
| 3600 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3601 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3602 | if (global_ssl.life_time) |
| 3603 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3604 | |
| 3605 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3606 | #ifdef OPENSSL_IS_BORINGSSL |
| 3607 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3608 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 3609 | #else |
| 3610 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 3611 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 3612 | #endif |
| 3613 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3614 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3615 | } |
| 3616 | |
| 3617 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
| 3618 | { |
| 3619 | struct proxy *curproxy = bind_conf->frontend; |
| 3620 | int cfgerr = 0; |
| 3621 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3622 | struct ssl_bind_conf *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3623 | const char *conf_ciphers; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3624 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3625 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3626 | 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] | 3627 | case SSL_SOCK_VERIFY_NONE: |
| 3628 | verify = SSL_VERIFY_NONE; |
| 3629 | break; |
| 3630 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 3631 | verify = SSL_VERIFY_PEER; |
| 3632 | break; |
| 3633 | case SSL_SOCK_VERIFY_REQUIRED: |
| 3634 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 3635 | break; |
| 3636 | } |
| 3637 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 3638 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3639 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 3640 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 3641 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3642 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3643 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3644 | 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] | 3645 | 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] | 3646 | cfgerr++; |
| 3647 | } |
| 3648 | /* set CA names fo client cert request, function returns void */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3649 | 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] | 3650 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3651 | else { |
| 3652 | Alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 3653 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3654 | cfgerr++; |
| 3655 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 3656 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3657 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3658 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 3659 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3660 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3661 | 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] | 3662 | 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] | 3663 | cfgerr++; |
| 3664 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 3665 | else { |
| 3666 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 3667 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3668 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 3669 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3670 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3671 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 3672 | #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] | 3673 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 3674 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
| 3675 | Alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 3676 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3677 | cfgerr++; |
| 3678 | } |
| 3679 | } |
| 3680 | #endif |
| 3681 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3682 | shared_context_set_cache(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3683 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 3684 | if (conf_ciphers && |
| 3685 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3686 | 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] | 3687 | 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] | 3688 | cfgerr++; |
| 3689 | } |
| 3690 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 3691 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3692 | /* If tune.ssl.default-dh-param has not been set, |
| 3693 | neither has ssl-default-dh-file and no static DH |
| 3694 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3695 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3696 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3697 | (ssl_dh_ptr_index == -1 || |
| 3698 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 3699 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 3700 | const SSL_CIPHER * cipher = NULL; |
| 3701 | char cipher_description[128]; |
| 3702 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 3703 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 3704 | which is not ephemeral DH. */ |
| 3705 | const char dhe_description[] = " Kx=DH "; |
| 3706 | const char dhe_export_description[] = " Kx=DH("; |
| 3707 | int idx = 0; |
| 3708 | int dhe_found = 0; |
| 3709 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 3710 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3711 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3712 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3713 | if (ssl) { |
| 3714 | ciphers = SSL_get_ciphers(ssl); |
| 3715 | |
| 3716 | if (ciphers) { |
| 3717 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 3718 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 3719 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 3720 | if (strstr(cipher_description, dhe_description) != NULL || |
| 3721 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 3722 | dhe_found = 1; |
| 3723 | break; |
| 3724 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 3725 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3726 | } |
| 3727 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3728 | SSL_free(ssl); |
| 3729 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 3730 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3731 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 3732 | if (dhe_found) { |
| 3733 | 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] | 3734 | } |
| 3735 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3736 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3737 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3738 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3739 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3740 | if (local_dh_1024 == NULL) { |
| 3741 | local_dh_1024 = ssl_get_dh_1024(); |
| 3742 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3743 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3744 | if (local_dh_2048 == NULL) { |
| 3745 | local_dh_2048 = ssl_get_dh_2048(); |
| 3746 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3747 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3748 | if (local_dh_4096 == NULL) { |
| 3749 | local_dh_4096 = ssl_get_dh_4096(); |
| 3750 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3751 | } |
| 3752 | } |
| 3753 | } |
| 3754 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3755 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3756 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 3757 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3758 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 3759 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3760 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3761 | #ifdef OPENSSL_NPN_NEGOTIATED |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3762 | ssl_conf_cur = NULL; |
| 3763 | if (ssl_conf && ssl_conf->npn_str) |
| 3764 | ssl_conf_cur = ssl_conf; |
| 3765 | else if (bind_conf->ssl_conf.npn_str) |
| 3766 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 3767 | if (ssl_conf_cur) |
| 3768 | 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] | 3769 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 3770 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3771 | ssl_conf_cur = NULL; |
| 3772 | if (ssl_conf && ssl_conf->alpn_str) |
| 3773 | ssl_conf_cur = ssl_conf; |
| 3774 | else if (bind_conf->ssl_conf.alpn_str) |
| 3775 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 3776 | if (ssl_conf_cur) |
| 3777 | 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] | 3778 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3779 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3780 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 3781 | if (conf_curves) { |
| 3782 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
| 3783 | Alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 3784 | curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3785 | cfgerr++; |
| 3786 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 3787 | #if defined(SSL_CTX_set_ecdh_auto) |
| 3788 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
| 3789 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3790 | } |
| 3791 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3792 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3793 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3794 | int i; |
| 3795 | EC_KEY *ecdh; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3796 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 3797 | (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] | 3798 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3799 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3800 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
| 3801 | 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] | 3802 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3803 | cfgerr++; |
| 3804 | } |
| 3805 | else { |
| 3806 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 3807 | EC_KEY_free(ecdh); |
| 3808 | } |
| 3809 | } |
| 3810 | #endif |
| 3811 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3812 | return cfgerr; |
| 3813 | } |
| 3814 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3815 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 3816 | { |
| 3817 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 3818 | size_t prefixlen, suffixlen; |
| 3819 | |
| 3820 | /* Trivial case */ |
| 3821 | if (strcmp(pattern, hostname) == 0) |
| 3822 | return 1; |
| 3823 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3824 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 3825 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 3826 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 3827 | pattern_wildcard = NULL; |
| 3828 | pattern_left_label_end = pattern; |
| 3829 | while (*pattern_left_label_end != '.') { |
| 3830 | switch (*pattern_left_label_end) { |
| 3831 | case 0: |
| 3832 | /* End of label not found */ |
| 3833 | return 0; |
| 3834 | case '*': |
| 3835 | /* If there is more than one wildcards */ |
| 3836 | if (pattern_wildcard) |
| 3837 | return 0; |
| 3838 | pattern_wildcard = pattern_left_label_end; |
| 3839 | break; |
| 3840 | } |
| 3841 | pattern_left_label_end++; |
| 3842 | } |
| 3843 | |
| 3844 | /* If it's not trivial and there is no wildcard, it can't |
| 3845 | * match */ |
| 3846 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3847 | return 0; |
| 3848 | |
| 3849 | /* Make sure all labels match except the leftmost */ |
| 3850 | hostname_left_label_end = strchr(hostname, '.'); |
| 3851 | if (!hostname_left_label_end |
| 3852 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 3853 | return 0; |
| 3854 | |
| 3855 | /* Make sure the leftmost label of the hostname is long enough |
| 3856 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 3857 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3858 | return 0; |
| 3859 | |
| 3860 | /* Finally compare the string on either side of the |
| 3861 | * wildcard */ |
| 3862 | prefixlen = pattern_wildcard - pattern; |
| 3863 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 3864 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 3865 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3866 | return 0; |
| 3867 | |
| 3868 | return 1; |
| 3869 | } |
| 3870 | |
| 3871 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 3872 | { |
| 3873 | SSL *ssl; |
| 3874 | struct connection *conn; |
| 3875 | char *servername; |
| 3876 | |
| 3877 | int depth; |
| 3878 | X509 *cert; |
| 3879 | STACK_OF(GENERAL_NAME) *alt_names; |
| 3880 | int i; |
| 3881 | X509_NAME *cert_subject; |
| 3882 | char *str; |
| 3883 | |
| 3884 | if (ok == 0) |
| 3885 | return ok; |
| 3886 | |
| 3887 | 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] | 3888 | conn = SSL_get_app_data(ssl); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3889 | |
| 3890 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
| 3891 | |
| 3892 | /* We only need to verify the CN on the actual server cert, |
| 3893 | * not the indirect CAs */ |
| 3894 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 3895 | if (depth != 0) |
| 3896 | return ok; |
| 3897 | |
| 3898 | /* At this point, the cert is *not* OK unless we can find a |
| 3899 | * hostname match */ |
| 3900 | ok = 0; |
| 3901 | |
| 3902 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 3903 | /* It seems like this might happen if verify peer isn't set */ |
| 3904 | if (!cert) |
| 3905 | return ok; |
| 3906 | |
| 3907 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 3908 | if (alt_names) { |
| 3909 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 3910 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 3911 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 3912 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 3913 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 3914 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3915 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 3916 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3917 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 3918 | OPENSSL_free(str); |
| 3919 | } |
| 3920 | } |
| 3921 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 3922 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3923 | } |
| 3924 | |
| 3925 | cert_subject = X509_get_subject_name(cert); |
| 3926 | i = -1; |
| 3927 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 3928 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3929 | ASN1_STRING *value; |
| 3930 | value = X509_NAME_ENTRY_get_data(entry); |
| 3931 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3932 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 3933 | OPENSSL_free(str); |
| 3934 | } |
| 3935 | } |
| 3936 | |
| 3937 | return ok; |
| 3938 | } |
| 3939 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3940 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3941 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3942 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3943 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3944 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 3945 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3946 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3947 | SSL_OP_NO_SSLv2 | |
| 3948 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 3949 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3950 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3951 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3952 | SSL_MODE_RELEASE_BUFFERS | |
| 3953 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3954 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3955 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3956 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3957 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3958 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3959 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3960 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 3961 | if (!ssl_initialize_random()) { |
| 3962 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 3963 | cfgerr++; |
| 3964 | } |
| 3965 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 3966 | /* Automatic memory computations need to know we use SSL there */ |
| 3967 | global.ssl_used_backend = 1; |
| 3968 | |
| 3969 | /* Initiate SSL context for current server */ |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3970 | srv->ssl_ctx.reused_sess = NULL; |
| 3971 | if (srv->use_ssl) |
| 3972 | srv->xprt = &ssl_sock; |
| 3973 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 3974 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3975 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3976 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3977 | if (!ctx) { |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3978 | Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 3979 | proxy_type_str(curproxy), curproxy->id, |
| 3980 | srv->id); |
| 3981 | cfgerr++; |
| 3982 | return cfgerr; |
| 3983 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3984 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3985 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
| 3986 | Warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 3987 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3988 | proxy_type_str(curproxy), curproxy->id, srv->id); |
| 3989 | else |
| 3990 | flags = conf_ssl_methods->flags; |
| 3991 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3992 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 3993 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3994 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3995 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3996 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3997 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 3998 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3999 | min = max = CONF_TLSV_NONE; |
| 4000 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4001 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4002 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4003 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4004 | if (min) { |
| 4005 | if (hole) { |
| 4006 | 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] | 4007 | "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] | 4008 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4009 | methodVersions[hole].name); |
| 4010 | hole = 0; |
| 4011 | } |
| 4012 | max = i; |
| 4013 | } |
| 4014 | else { |
| 4015 | min = max = i; |
| 4016 | } |
| 4017 | } |
| 4018 | else { |
| 4019 | if (min) |
| 4020 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4021 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4022 | if (!min) { |
| 4023 | Alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4024 | proxy_type_str(curproxy), curproxy->id, srv->id); |
| 4025 | cfgerr += 1; |
| 4026 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4027 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4028 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) && !defined(OPENSSL_IS_BORINGSSL) |
| 4029 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
| 4030 | precautionary measure to avoid any suprise with older openssl version. */ |
| 4031 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame^] | 4032 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4033 | else |
| 4034 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4035 | if (flags & methodVersions[i].flag) |
| 4036 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4037 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4038 | /* 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^] | 4039 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4040 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4041 | #endif |
| 4042 | |
| 4043 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4044 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4045 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4046 | |
| 4047 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 4048 | if (global_ssl.async) |
| 4049 | mode |= SSL_MODE_ASYNC; |
| 4050 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4051 | SSL_CTX_set_mode(ctx, mode); |
| 4052 | srv->ssl_ctx.ctx = ctx; |
| 4053 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4054 | if (srv->ssl_ctx.client_crt) { |
| 4055 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
| 4056 | Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4057 | proxy_type_str(curproxy), curproxy->id, |
| 4058 | srv->id, srv->ssl_ctx.client_crt); |
| 4059 | cfgerr++; |
| 4060 | } |
| 4061 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
| 4062 | Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4063 | proxy_type_str(curproxy), curproxy->id, |
| 4064 | srv->id, srv->ssl_ctx.client_crt); |
| 4065 | cfgerr++; |
| 4066 | } |
| 4067 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
| 4068 | Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4069 | proxy_type_str(curproxy), curproxy->id, |
| 4070 | srv->id, srv->ssl_ctx.client_crt); |
| 4071 | cfgerr++; |
| 4072 | } |
| 4073 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4074 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4075 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4076 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4077 | switch (srv->ssl_ctx.verify) { |
| 4078 | case SSL_SOCK_VERIFY_NONE: |
| 4079 | verify = SSL_VERIFY_NONE; |
| 4080 | break; |
| 4081 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4082 | verify = SSL_VERIFY_PEER; |
| 4083 | break; |
| 4084 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4085 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4086 | verify, |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4087 | srv->ssl_ctx.verify_host ? ssl_sock_srv_verifycbk : NULL); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4088 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4089 | if (srv->ssl_ctx.ca_file) { |
| 4090 | /* load CAfile to verify */ |
| 4091 | 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] | 4092 | 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] | 4093 | curproxy->id, srv->id, |
| 4094 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
| 4095 | cfgerr++; |
| 4096 | } |
| 4097 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4098 | else { |
| 4099 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 4100 | 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] | 4101 | curproxy->id, srv->id, |
| 4102 | srv->conf.file, srv->conf.line); |
| 4103 | else |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 4104 | 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] | 4105 | curproxy->id, srv->id, |
| 4106 | srv->conf.file, srv->conf.line); |
| 4107 | cfgerr++; |
| 4108 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4109 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4110 | if (srv->ssl_ctx.crl_file) { |
| 4111 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4112 | |
| 4113 | 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] | 4114 | 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] | 4115 | curproxy->id, srv->id, |
| 4116 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
| 4117 | cfgerr++; |
| 4118 | } |
| 4119 | else { |
| 4120 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4121 | } |
| 4122 | } |
| 4123 | #endif |
| 4124 | } |
| 4125 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4126 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF); |
| 4127 | if (srv->ssl_ctx.ciphers && |
| 4128 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
| 4129 | Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4130 | curproxy->id, srv->id, |
| 4131 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
| 4132 | cfgerr++; |
| 4133 | } |
| 4134 | |
| 4135 | return cfgerr; |
| 4136 | } |
| 4137 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4138 | /* 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] | 4139 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4140 | * encountered. |
| 4141 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4142 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4143 | { |
| 4144 | struct ebmb_node *node; |
| 4145 | struct sni_ctx *sni; |
| 4146 | int err = 0; |
| 4147 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4148 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4149 | return 0; |
| 4150 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4151 | /* Automatic memory computations need to know we use SSL there */ |
| 4152 | global.ssl_used_frontend = 1; |
| 4153 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4154 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4155 | if (!ssl_initialize_random()) { |
| 4156 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 4157 | err++; |
| 4158 | } |
| 4159 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4160 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4161 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4162 | /* It should not be necessary to call this function, but it's |
| 4163 | necessary first to check and move all initialisation related |
| 4164 | to initial_ctx in ssl_sock_initial_ctx. */ |
| 4165 | err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx); |
| 4166 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4167 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4168 | 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] | 4169 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4170 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4171 | while (node) { |
| 4172 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4173 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4174 | /* only initialize the CTX on its first occurrence and |
| 4175 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4176 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4177 | node = ebmb_next(node); |
| 4178 | } |
| 4179 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4180 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4181 | while (node) { |
| 4182 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4183 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4184 | /* only initialize the CTX on its first occurrence and |
| 4185 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4186 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4187 | node = ebmb_next(node); |
| 4188 | } |
| 4189 | return err; |
| 4190 | } |
| 4191 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4192 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4193 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4194 | * alerts are directly emitted since the rest of the stack does it below. |
| 4195 | */ |
| 4196 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4197 | { |
| 4198 | struct proxy *px = bind_conf->frontend; |
| 4199 | int alloc_ctx; |
| 4200 | int err; |
| 4201 | |
| 4202 | if (!bind_conf->is_ssl) { |
| 4203 | if (bind_conf->default_ctx) { |
| 4204 | Warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 4205 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4206 | } |
| 4207 | return 0; |
| 4208 | } |
| 4209 | if (!bind_conf->default_ctx) { |
| 4210 | Alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 4211 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4212 | return -1; |
| 4213 | } |
| 4214 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4215 | 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] | 4216 | if (alloc_ctx < 0) { |
| 4217 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 4218 | 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"); |
| 4219 | else |
| 4220 | Alert("Unable to allocate SSL session cache.\n"); |
| 4221 | return -1; |
| 4222 | } |
| 4223 | |
| 4224 | err = 0; |
| 4225 | /* initialize all certificate contexts */ |
| 4226 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 4227 | |
| 4228 | /* initialize CA variables if the certificates generation is enabled */ |
| 4229 | err += ssl_sock_load_ca(bind_conf); |
| 4230 | |
| 4231 | return -err; |
| 4232 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4233 | |
| 4234 | /* release ssl context allocated for servers. */ |
| 4235 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 4236 | { |
| 4237 | if (srv->ssl_ctx.ctx) |
| 4238 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 4239 | } |
| 4240 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4241 | /* 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] | 4242 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 4243 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4244 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4245 | { |
| 4246 | struct ebmb_node *node, *back; |
| 4247 | struct sni_ctx *sni; |
| 4248 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4249 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4250 | return; |
| 4251 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4252 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4253 | while (node) { |
| 4254 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4255 | back = ebmb_next(node); |
| 4256 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4257 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4258 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4259 | ssl_sock_free_ssl_conf(sni->conf); |
| 4260 | free(sni->conf); |
| 4261 | sni->conf = NULL; |
| 4262 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4263 | free(sni); |
| 4264 | node = back; |
| 4265 | } |
| 4266 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4267 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4268 | while (node) { |
| 4269 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4270 | back = ebmb_next(node); |
| 4271 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4272 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4273 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4274 | ssl_sock_free_ssl_conf(sni->conf); |
| 4275 | free(sni->conf); |
| 4276 | sni->conf = NULL; |
| 4277 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4278 | free(sni); |
| 4279 | node = back; |
| 4280 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4281 | SSL_CTX_free(bind_conf->initial_ctx); |
| 4282 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4283 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4284 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4285 | } |
| 4286 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4287 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 4288 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 4289 | { |
| 4290 | ssl_sock_free_ca(bind_conf); |
| 4291 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4292 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4293 | free(bind_conf->ca_sign_file); |
| 4294 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4295 | if (bind_conf->keys_ref) { |
| 4296 | free(bind_conf->keys_ref->filename); |
| 4297 | free(bind_conf->keys_ref->tlskeys); |
| 4298 | LIST_DEL(&bind_conf->keys_ref->list); |
| 4299 | free(bind_conf->keys_ref); |
| 4300 | } |
| 4301 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4302 | bind_conf->ca_sign_pass = NULL; |
| 4303 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4304 | } |
| 4305 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4306 | /* Load CA cert file and private key used to generate certificates */ |
| 4307 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4308 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4309 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4310 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4311 | FILE *fp; |
| 4312 | X509 *cacert = NULL; |
| 4313 | EVP_PKEY *capkey = NULL; |
| 4314 | int err = 0; |
| 4315 | |
| 4316 | if (!bind_conf || !bind_conf->generate_certs) |
| 4317 | return err; |
| 4318 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4319 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4320 | if (global_ssl.ctx_cache) |
| 4321 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4322 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 4323 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4324 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4325 | if (!bind_conf->ca_sign_file) { |
| 4326 | Alert("Proxy '%s': cannot enable certificate generation, " |
| 4327 | "no CA certificate File configured at [%s:%d].\n", |
| 4328 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4329 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4330 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4331 | |
| 4332 | /* read in the CA certificate */ |
| 4333 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
| 4334 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4335 | 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] | 4336 | goto load_error; |
| 4337 | } |
| 4338 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
| 4339 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4340 | 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] | 4341 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4342 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4343 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4344 | if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) { |
| 4345 | Alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 4346 | 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] | 4347 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4348 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4349 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4350 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4351 | bind_conf->ca_sign_cert = cacert; |
| 4352 | bind_conf->ca_sign_pkey = capkey; |
| 4353 | return err; |
| 4354 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4355 | read_error: |
| 4356 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4357 | if (capkey) EVP_PKEY_free(capkey); |
| 4358 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4359 | load_error: |
| 4360 | bind_conf->generate_certs = 0; |
| 4361 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4362 | return err; |
| 4363 | } |
| 4364 | |
| 4365 | /* Release CA cert and private key used to generate certificated */ |
| 4366 | void |
| 4367 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 4368 | { |
| 4369 | if (!bind_conf) |
| 4370 | return; |
| 4371 | |
| 4372 | if (bind_conf->ca_sign_pkey) |
| 4373 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 4374 | if (bind_conf->ca_sign_cert) |
| 4375 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 4376 | bind_conf->ca_sign_pkey = NULL; |
| 4377 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4378 | } |
| 4379 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4380 | /* |
| 4381 | * This function is called if SSL * context is not yet allocated. The function |
| 4382 | * is designed to be called before any other data-layer operation and sets the |
| 4383 | * handshake flag on the connection. It is safe to call it multiple times. |
| 4384 | * It returns 0 on success and -1 in error case. |
| 4385 | */ |
| 4386 | static int ssl_sock_init(struct connection *conn) |
| 4387 | { |
| 4388 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4389 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4390 | return 0; |
| 4391 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4392 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4393 | return 0; |
| 4394 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4395 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 4396 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4397 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4398 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4399 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4400 | /* If it is in client mode initiate SSL session |
| 4401 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4402 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4403 | int may_retry = 1; |
| 4404 | |
| 4405 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4406 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4407 | conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4408 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4409 | if (may_retry--) { |
| 4410 | pool_gc2(); |
| 4411 | goto retry_connect; |
| 4412 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4413 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4414 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4415 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4416 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4417 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4418 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 4419 | SSL_free(conn->xprt_ctx); |
| 4420 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4421 | if (may_retry--) { |
| 4422 | pool_gc2(); |
| 4423 | goto retry_connect; |
| 4424 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4425 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4426 | return -1; |
| 4427 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4428 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4429 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4430 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 4431 | SSL_free(conn->xprt_ctx); |
| 4432 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4433 | if (may_retry--) { |
| 4434 | pool_gc2(); |
| 4435 | goto retry_connect; |
| 4436 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4437 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4438 | return -1; |
| 4439 | } |
| 4440 | |
| 4441 | SSL_set_connect_state(conn->xprt_ctx); |
| 4442 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 4443 | if(!SSL_set_session(conn->xprt_ctx, objt_server(conn->target)->ssl_ctx.reused_sess)) { |
| 4444 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 4445 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 4446 | } |
| 4447 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4448 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4449 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4450 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4451 | |
| 4452 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 4453 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4454 | return 0; |
| 4455 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4456 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4457 | int may_retry = 1; |
| 4458 | |
| 4459 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4460 | /* Alloc a new SSL session ctx */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4461 | 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] | 4462 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4463 | if (may_retry--) { |
| 4464 | pool_gc2(); |
| 4465 | goto retry_accept; |
| 4466 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4467 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4468 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4469 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4470 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4471 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4472 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 4473 | SSL_free(conn->xprt_ctx); |
| 4474 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4475 | if (may_retry--) { |
| 4476 | pool_gc2(); |
| 4477 | goto retry_accept; |
| 4478 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4479 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4480 | return -1; |
| 4481 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4482 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4483 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4484 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 4485 | SSL_free(conn->xprt_ctx); |
| 4486 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4487 | if (may_retry--) { |
| 4488 | pool_gc2(); |
| 4489 | goto retry_accept; |
| 4490 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4491 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4492 | return -1; |
| 4493 | } |
| 4494 | |
| 4495 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4496 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4497 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4498 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4499 | |
| 4500 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 4501 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4502 | return 0; |
| 4503 | } |
| 4504 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4505 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4506 | return -1; |
| 4507 | } |
| 4508 | |
| 4509 | |
| 4510 | /* This is the callback which is used when an SSL handshake is pending. It |
| 4511 | * updates the FD status if it wants some polling before being called again. |
| 4512 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 4513 | * otherwise it returns non-zero and removes itself from the connection's |
| 4514 | * flags (the bit is provided in <flag> by the caller). |
| 4515 | */ |
| 4516 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 4517 | { |
| 4518 | int ret; |
| 4519 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4520 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4521 | return 0; |
| 4522 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4523 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4524 | goto out_error; |
| 4525 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4526 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 4527 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 4528 | * Usually SSL_write and SSL_read are used and process implicitly |
| 4529 | * the reneg handshake. |
| 4530 | * Here we use SSL_peek as a workaround for reneg. |
| 4531 | */ |
| 4532 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4533 | char c; |
| 4534 | |
| 4535 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 4536 | if (ret <= 0) { |
| 4537 | /* handshake may have not been completed, let's find why */ |
| 4538 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4539 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4540 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 4541 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 4542 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4543 | __conn_sock_want_send(conn); |
| 4544 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4545 | return 0; |
| 4546 | } |
| 4547 | else if (ret == SSL_ERROR_WANT_READ) { |
| 4548 | /* handshake may have been completed but we have |
| 4549 | * no more data to read. |
| 4550 | */ |
| 4551 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4552 | ret = 1; |
| 4553 | goto reneg_ok; |
| 4554 | } |
| 4555 | /* SSL handshake needs to read, L4 connection is ready */ |
| 4556 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 4557 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4558 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4559 | __conn_sock_want_recv(conn); |
| 4560 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4561 | return 0; |
| 4562 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4563 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 4564 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 4565 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4566 | return 0; |
| 4567 | } |
| 4568 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4569 | else if (ret == SSL_ERROR_SYSCALL) { |
| 4570 | /* if errno is null, then connection was successfully established */ |
| 4571 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 4572 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4573 | if (!conn->err_code) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4574 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 4575 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4576 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4577 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 4578 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4579 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 4580 | empty_handshake = state == TLS_ST_BEFORE; |
| 4581 | #else |
| 4582 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
| 4583 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4584 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4585 | if (!errno) { |
| 4586 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4587 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4588 | else |
| 4589 | conn->err_code = CO_ER_SSL_EMPTY; |
| 4590 | } |
| 4591 | else { |
| 4592 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4593 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4594 | else |
| 4595 | conn->err_code = CO_ER_SSL_ABORT; |
| 4596 | } |
| 4597 | } |
| 4598 | else { |
| 4599 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4600 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4601 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4602 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4603 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4604 | #endif |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4605 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4606 | goto out_error; |
| 4607 | } |
| 4608 | else { |
| 4609 | /* Fail on all other handshake errors */ |
| 4610 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 4611 | * buffer, causing an RST to be emitted upon close() on |
| 4612 | * TCP sockets. We first try to drain possibly pending |
| 4613 | * data to avoid this as much as possible. |
| 4614 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 4615 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4616 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 4617 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 4618 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4619 | goto out_error; |
| 4620 | } |
| 4621 | } |
| 4622 | /* read some data: consider handshake completed */ |
| 4623 | goto reneg_ok; |
| 4624 | } |
| 4625 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4626 | ret = SSL_do_handshake(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4627 | if (ret != 1) { |
| 4628 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4629 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4630 | |
| 4631 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 4632 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 4633 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4634 | __conn_sock_want_send(conn); |
| 4635 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4636 | return 0; |
| 4637 | } |
| 4638 | else if (ret == SSL_ERROR_WANT_READ) { |
| 4639 | /* SSL handshake needs to read, L4 connection is ready */ |
| 4640 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 4641 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4642 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4643 | __conn_sock_want_recv(conn); |
| 4644 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4645 | return 0; |
| 4646 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4647 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 4648 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 4649 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4650 | return 0; |
| 4651 | } |
| 4652 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 4653 | else if (ret == SSL_ERROR_SYSCALL) { |
| 4654 | /* if errno is null, then connection was successfully established */ |
| 4655 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 4656 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4657 | if (!conn->err_code) { |
| 4658 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 4659 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4660 | #else |
| 4661 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 4662 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4663 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 4664 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4665 | #else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4666 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4667 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4668 | if (empty_handshake) { |
| 4669 | if (!errno) { |
| 4670 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4671 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4672 | else |
| 4673 | conn->err_code = CO_ER_SSL_EMPTY; |
| 4674 | } |
| 4675 | else { |
| 4676 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4677 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4678 | else |
| 4679 | conn->err_code = CO_ER_SSL_ABORT; |
| 4680 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4681 | } |
| 4682 | else { |
| 4683 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4684 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4685 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4686 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4687 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4688 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4689 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 4690 | goto out_error; |
| 4691 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4692 | else { |
| 4693 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 4694 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 4695 | * buffer, causing an RST to be emitted upon close() on |
| 4696 | * TCP sockets. We first try to drain possibly pending |
| 4697 | * data to avoid this as much as possible. |
| 4698 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 4699 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4700 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 4701 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 4702 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4703 | goto out_error; |
| 4704 | } |
| 4705 | } |
| 4706 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4707 | reneg_ok: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4708 | /* Handshake succeeded */ |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 4709 | if (!SSL_session_reused(conn->xprt_ctx)) { |
| 4710 | if (objt_server(conn->target)) { |
| 4711 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 4712 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 4713 | global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr; |
| 4714 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4715 | /* 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] | 4716 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4717 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
Willy Tarreau | 30fd4bd | 2016-12-22 21:54:21 +0100 | [diff] [blame] | 4718 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 4719 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4720 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 4721 | if (!(objt_server(conn->target)->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) |
| 4722 | 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] | 4723 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 4724 | else { |
| 4725 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 4726 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 4727 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 4728 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4729 | } |
| 4730 | |
| 4731 | /* The connection is now established at both layers, it's time to leave */ |
| 4732 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 4733 | return 1; |
| 4734 | |
| 4735 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4736 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4737 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4738 | ERR_clear_error(); |
| 4739 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 4740 | /* free resumed session if exists */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4741 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 4742 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 4743 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 4744 | } |
| 4745 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4746 | /* Fail on all other handshake errors */ |
| 4747 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4748 | if (!conn->err_code) |
| 4749 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4750 | return 0; |
| 4751 | } |
| 4752 | |
| 4753 | /* 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] | 4754 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4755 | * buffer wraps, in which case a second call may be performed. The connection's |
| 4756 | * flags are updated with whatever special event is detected (error, read0, |
| 4757 | * empty). The caller is responsible for taking care of those events and |
| 4758 | * avoiding the call if inappropriate. The function does not call the |
| 4759 | * connection's polling update function, so the caller is responsible for this. |
| 4760 | */ |
| 4761 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 4762 | { |
| 4763 | int ret, done = 0; |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 4764 | int try; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4765 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4766 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4767 | goto out_error; |
| 4768 | |
| 4769 | if (conn->flags & CO_FL_HANDSHAKE) |
| 4770 | /* a handshake was requested */ |
| 4771 | return 0; |
| 4772 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 4773 | /* let's realign the buffer to optimize I/O */ |
| 4774 | if (buffer_empty(buf)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4775 | buf->p = buf->data; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4776 | |
| 4777 | /* read the largest possible block. For this, we perform only one call |
| 4778 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 4779 | * in which case we accept to do it once again. A new attempt is made on |
| 4780 | * EINTR too. |
| 4781 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 4782 | while (count > 0) { |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 4783 | /* first check if we have some room after p+i */ |
| 4784 | try = buf->data + buf->size - (buf->p + buf->i); |
| 4785 | /* otherwise continue between data and p-o */ |
| 4786 | if (try <= 0) { |
| 4787 | try = buf->p - (buf->data + buf->o); |
| 4788 | if (try <= 0) |
| 4789 | break; |
| 4790 | } |
| 4791 | if (try > count) |
| 4792 | try = count; |
| 4793 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4794 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4795 | if (conn->flags & CO_FL_ERROR) { |
| 4796 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4797 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4798 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4799 | if (ret > 0) { |
| 4800 | buf->i += ret; |
| 4801 | done += ret; |
| 4802 | if (ret < try) |
| 4803 | break; |
| 4804 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4805 | } |
| 4806 | else if (ret == 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4807 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 4808 | if (ret != SSL_ERROR_ZERO_RETURN) { |
Emeric Brun | 1c64686 | 2012-12-14 12:33:41 +0100 | [diff] [blame] | 4809 | /* error on protocol or underlying transport */ |
| 4810 | if ((ret != SSL_ERROR_SYSCALL) |
| 4811 | || (errno && (errno != EAGAIN))) |
| 4812 | conn->flags |= CO_FL_ERROR; |
| 4813 | |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4814 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4815 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4816 | ERR_clear_error(); |
| 4817 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4818 | goto read0; |
| 4819 | } |
| 4820 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4821 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4822 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4823 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4824 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4825 | __conn_sock_want_send(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4826 | break; |
| 4827 | } |
| 4828 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 4829 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4830 | /* handshake is running, and it may need to re-enable read */ |
| 4831 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 4832 | __conn_sock_want_recv(conn); |
| 4833 | break; |
| 4834 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4835 | /* we need to poll for retry a read later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4836 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4837 | break; |
| 4838 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4839 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 4840 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 4841 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4842 | break; |
| 4843 | } |
| 4844 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4845 | /* otherwise it's a real error */ |
| 4846 | goto out_error; |
| 4847 | } |
| 4848 | } |
| 4849 | return done; |
| 4850 | |
| 4851 | read0: |
| 4852 | conn_sock_read0(conn); |
| 4853 | return done; |
| 4854 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4855 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4856 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4857 | ERR_clear_error(); |
| 4858 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4859 | conn->flags |= CO_FL_ERROR; |
| 4860 | return done; |
| 4861 | } |
| 4862 | |
| 4863 | |
| 4864 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 4865 | * <flags> may contain some CO_SFL_* flags to hint the system about other |
| 4866 | * pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4867 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 4868 | * a second call may be performed. The connection's flags are updated with |
| 4869 | * whatever special event is detected (error, empty). The caller is responsible |
| 4870 | * for taking care of those events and avoiding the call if inappropriate. The |
| 4871 | * function does not call the connection's polling update function, so the caller |
| 4872 | * is responsible for this. |
| 4873 | */ |
| 4874 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 4875 | { |
| 4876 | int ret, try, done; |
| 4877 | |
| 4878 | done = 0; |
| 4879 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4880 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4881 | goto out_error; |
| 4882 | |
| 4883 | if (conn->flags & CO_FL_HANDSHAKE) |
| 4884 | /* a handshake was requested */ |
| 4885 | return 0; |
| 4886 | |
| 4887 | /* send the largest possible block. For this we perform only one call |
| 4888 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 4889 | * in which case we accept to do it once again. |
| 4890 | */ |
| 4891 | while (buf->o) { |
Kevin Hester | cad8234 | 2013-05-30 15:12:41 -0700 | [diff] [blame] | 4892 | try = bo_contig_data(buf); |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 4893 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 4894 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4895 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4896 | global_ssl.max_record && try > global_ssl.max_record) { |
| 4897 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4898 | } |
| 4899 | else { |
| 4900 | /* we need to keep the information about the fact that |
| 4901 | * we're not limiting the upcoming send(), because if it |
| 4902 | * fails, we'll have to retry with at least as many data. |
| 4903 | */ |
| 4904 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 4905 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 4906 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4907 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4908 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4909 | if (conn->flags & CO_FL_ERROR) { |
| 4910 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4911 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4912 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4913 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4914 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
| 4915 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4916 | buf->o -= ret; |
| 4917 | done += ret; |
| 4918 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 4919 | if (likely(buffer_empty(buf))) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4920 | /* optimize data alignment in the buffer */ |
| 4921 | buf->p = buf->data; |
| 4922 | |
| 4923 | /* if the system buffer is full, don't insist */ |
| 4924 | if (ret < try) |
| 4925 | break; |
| 4926 | } |
| 4927 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4928 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4929 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4930 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 4931 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4932 | /* handshake is running, and it may need to re-enable write */ |
| 4933 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 4934 | __conn_sock_want_send(conn); |
| 4935 | break; |
| 4936 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4937 | /* we need to poll to retry a write later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4938 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4939 | break; |
| 4940 | } |
| 4941 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4942 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4943 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4944 | __conn_sock_want_recv(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4945 | break; |
| 4946 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4947 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 4948 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 4949 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4950 | break; |
| 4951 | } |
| 4952 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4953 | goto out_error; |
| 4954 | } |
| 4955 | } |
| 4956 | return done; |
| 4957 | |
| 4958 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4959 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4960 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4961 | ERR_clear_error(); |
| 4962 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4963 | conn->flags |= CO_FL_ERROR; |
| 4964 | return done; |
| 4965 | } |
| 4966 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4967 | static void ssl_sock_close(struct connection *conn) { |
| 4968 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4969 | if (conn->xprt_ctx) { |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4970 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 4971 | if (global_ssl.async) { |
| 4972 | OSSL_ASYNC_FD all_fd[32], afd; |
| 4973 | size_t num_all_fds = 0; |
| 4974 | int i; |
| 4975 | |
| 4976 | SSL_get_all_async_fds(conn->xprt_ctx, NULL, &num_all_fds); |
| 4977 | if (num_all_fds > 32) { |
| 4978 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 4979 | return; |
| 4980 | } |
| 4981 | |
| 4982 | SSL_get_all_async_fds(conn->xprt_ctx, all_fd, &num_all_fds); |
| 4983 | |
| 4984 | /* If an async job is pending, we must try to |
| 4985 | to catch the end using polling before calling |
| 4986 | SSL_free */ |
| 4987 | if (num_all_fds && SSL_waiting_for_async(conn->xprt_ctx)) { |
| 4988 | for (i=0 ; i < num_all_fds ; i++) { |
| 4989 | /* switch on an handler designed to |
| 4990 | * handle the SSL_free |
| 4991 | */ |
| 4992 | afd = all_fd[i]; |
| 4993 | fdtab[afd].iocb = ssl_async_fd_free; |
| 4994 | fdtab[afd].owner = conn->xprt_ctx; |
| 4995 | fd_want_recv(afd); |
| 4996 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4997 | conn->xprt_ctx = NULL; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4998 | jobs++; |
| 4999 | return; |
| 5000 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5001 | /* Else we can remove the fds from the fdtab |
| 5002 | * and call SSL_free. |
| 5003 | * note: we do a fd_remove and not a delete |
| 5004 | * because the fd is owned by the engine. |
| 5005 | * the engine is responsible to close |
| 5006 | */ |
| 5007 | for (i=0 ; i < num_all_fds ; i++) |
| 5008 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5009 | } |
| 5010 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5011 | SSL_free(conn->xprt_ctx); |
| 5012 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5013 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5014 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 5018 | * any case, flags the connection as reusable if no handshake was in progress. |
| 5019 | */ |
| 5020 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 5021 | { |
| 5022 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5023 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5024 | if (!clean) |
| 5025 | /* don't sent notify on SSL_shutdown */ |
Willy Tarreau | e3cc3a3 | 2017-02-13 11:12:29 +0100 | [diff] [blame] | 5026 | SSL_set_quiet_shutdown(conn->xprt_ctx, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5027 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5028 | if (SSL_shutdown(conn->xprt_ctx) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5029 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5030 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5031 | ERR_clear_error(); |
| 5032 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5033 | } |
| 5034 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5035 | /* used for logging, may be changed for a sample fetch later */ |
| 5036 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 5037 | { |
| 5038 | if (!conn->xprt && !conn->xprt_ctx) |
| 5039 | return NULL; |
| 5040 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 5041 | } |
| 5042 | |
| 5043 | /* used for logging, may be changed for a sample fetch later */ |
| 5044 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 5045 | { |
| 5046 | if (!conn->xprt && !conn->xprt_ctx) |
| 5047 | return NULL; |
| 5048 | return SSL_get_version(conn->xprt_ctx); |
| 5049 | } |
| 5050 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5051 | /* Extract a serial from a cert, and copy it to a chunk. |
| 5052 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 5053 | * -1 if output is not large enough. |
| 5054 | */ |
| 5055 | static int |
| 5056 | ssl_sock_get_serial(X509 *crt, struct chunk *out) |
| 5057 | { |
| 5058 | ASN1_INTEGER *serial; |
| 5059 | |
| 5060 | serial = X509_get_serialNumber(crt); |
| 5061 | if (!serial) |
| 5062 | return 0; |
| 5063 | |
| 5064 | if (out->size < serial->length) |
| 5065 | return -1; |
| 5066 | |
| 5067 | memcpy(out->str, serial->data, serial->length); |
| 5068 | out->len = serial->length; |
| 5069 | return 1; |
| 5070 | } |
| 5071 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5072 | /* Extract a cert to der, and copy it to a chunk. |
| 5073 | * Returns 1 if cert is found and copied, 0 on der convertion failure and |
| 5074 | * -1 if output is not large enough. |
| 5075 | */ |
| 5076 | static int |
| 5077 | ssl_sock_crt2der(X509 *crt, struct chunk *out) |
| 5078 | { |
| 5079 | int len; |
| 5080 | unsigned char *p = (unsigned char *)out->str;; |
| 5081 | |
| 5082 | len =i2d_X509(crt, NULL); |
| 5083 | if (len <= 0) |
| 5084 | return 1; |
| 5085 | |
| 5086 | if (out->size < len) |
| 5087 | return -1; |
| 5088 | |
| 5089 | i2d_X509(crt,&p); |
| 5090 | out->len = len; |
| 5091 | return 1; |
| 5092 | } |
| 5093 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5094 | |
| 5095 | /* Copy Date in ASN1_UTCTIME format in struct chunk out. |
| 5096 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 5097 | * and -1 if output is not large enough. |
| 5098 | */ |
| 5099 | static int |
| 5100 | ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out) |
| 5101 | { |
| 5102 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 5103 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 5104 | |
| 5105 | if (gentm->length < 12) |
| 5106 | return 0; |
| 5107 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 5108 | return 0; |
| 5109 | if (out->size < gentm->length-2) |
| 5110 | return -1; |
| 5111 | |
| 5112 | memcpy(out->str, gentm->data+2, gentm->length-2); |
| 5113 | out->len = gentm->length-2; |
| 5114 | return 1; |
| 5115 | } |
| 5116 | else if (tm->type == V_ASN1_UTCTIME) { |
| 5117 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 5118 | |
| 5119 | if (utctm->length < 10) |
| 5120 | return 0; |
| 5121 | if (utctm->data[0] >= 0x35) |
| 5122 | return 0; |
| 5123 | if (out->size < utctm->length) |
| 5124 | return -1; |
| 5125 | |
| 5126 | memcpy(out->str, utctm->data, utctm->length); |
| 5127 | out->len = utctm->length; |
| 5128 | return 1; |
| 5129 | } |
| 5130 | |
| 5131 | return 0; |
| 5132 | } |
| 5133 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5134 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 5135 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 5136 | */ |
| 5137 | static int |
| 5138 | ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out) |
| 5139 | { |
| 5140 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5141 | ASN1_OBJECT *obj; |
| 5142 | ASN1_STRING *data; |
| 5143 | const unsigned char *data_ptr; |
| 5144 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5145 | int i, j, n; |
| 5146 | int cur = 0; |
| 5147 | const char *s; |
| 5148 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5149 | int name_count; |
| 5150 | |
| 5151 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5152 | |
| 5153 | out->len = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5154 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5155 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5156 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5157 | else |
| 5158 | j = i; |
| 5159 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5160 | ne = X509_NAME_get_entry(a, j); |
| 5161 | obj = X509_NAME_ENTRY_get_object(ne); |
| 5162 | data = X509_NAME_ENTRY_get_data(ne); |
| 5163 | data_ptr = ASN1_STRING_get0_data(data); |
| 5164 | data_len = ASN1_STRING_length(data); |
| 5165 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5166 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5167 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5168 | s = tmp; |
| 5169 | } |
| 5170 | |
| 5171 | if (chunk_strcasecmp(entry, s) != 0) |
| 5172 | continue; |
| 5173 | |
| 5174 | if (pos < 0) |
| 5175 | cur--; |
| 5176 | else |
| 5177 | cur++; |
| 5178 | |
| 5179 | if (cur != pos) |
| 5180 | continue; |
| 5181 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5182 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5183 | return -1; |
| 5184 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5185 | memcpy(out->str, data_ptr, data_len); |
| 5186 | out->len = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5187 | return 1; |
| 5188 | } |
| 5189 | |
| 5190 | return 0; |
| 5191 | |
| 5192 | } |
| 5193 | |
| 5194 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 5195 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 5196 | */ |
| 5197 | static int |
| 5198 | ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out) |
| 5199 | { |
| 5200 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5201 | ASN1_OBJECT *obj; |
| 5202 | ASN1_STRING *data; |
| 5203 | const unsigned char *data_ptr; |
| 5204 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5205 | int i, n, ln; |
| 5206 | int l = 0; |
| 5207 | const char *s; |
| 5208 | char *p; |
| 5209 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5210 | int name_count; |
| 5211 | |
| 5212 | |
| 5213 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5214 | |
| 5215 | out->len = 0; |
| 5216 | p = out->str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5217 | for (i = 0; i < name_count; i++) { |
| 5218 | ne = X509_NAME_get_entry(a, i); |
| 5219 | obj = X509_NAME_ENTRY_get_object(ne); |
| 5220 | data = X509_NAME_ENTRY_get_data(ne); |
| 5221 | data_ptr = ASN1_STRING_get0_data(data); |
| 5222 | data_len = ASN1_STRING_length(data); |
| 5223 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5224 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5225 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5226 | s = tmp; |
| 5227 | } |
| 5228 | ln = strlen(s); |
| 5229 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5230 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5231 | if (l > out->size) |
| 5232 | return -1; |
| 5233 | out->len = l; |
| 5234 | |
| 5235 | *(p++)='/'; |
| 5236 | memcpy(p, s, ln); |
| 5237 | p += ln; |
| 5238 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5239 | memcpy(p, data_ptr, data_len); |
| 5240 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5241 | } |
| 5242 | |
| 5243 | if (!out->len) |
| 5244 | return 0; |
| 5245 | |
| 5246 | return 1; |
| 5247 | } |
| 5248 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5249 | char *ssl_sock_get_version(struct connection *conn) |
| 5250 | { |
| 5251 | if (!ssl_sock_is_ssl(conn)) |
| 5252 | return NULL; |
| 5253 | |
| 5254 | return (char *)SSL_get_version(conn->xprt_ctx); |
| 5255 | } |
| 5256 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5257 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 5258 | * to disable SNI. |
| 5259 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5260 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 5261 | { |
| 5262 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5263 | char *prev_name; |
| 5264 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5265 | if (!ssl_sock_is_ssl(conn)) |
| 5266 | return; |
| 5267 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5268 | /* if the SNI changes, we must destroy the reusable context so that a |
| 5269 | * new connection will present a new SNI. As an optimization we could |
| 5270 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 5271 | * server. |
| 5272 | */ |
| 5273 | prev_name = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 5274 | if ((!prev_name && hostname) || |
| 5275 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
| 5276 | SSL_set_session(conn->xprt_ctx, NULL); |
| 5277 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5278 | SSL_set_tlsext_host_name(conn->xprt_ctx, hostname); |
| 5279 | #endif |
| 5280 | } |
| 5281 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5282 | /* Extract peer certificate's common name into the chunk dest |
| 5283 | * Returns |
| 5284 | * the len of the extracted common name |
| 5285 | * or 0 if no CN found in DN |
| 5286 | * or -1 on error case (i.e. no peer certificate) |
| 5287 | */ |
| 5288 | 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] | 5289 | { |
| 5290 | X509 *crt = NULL; |
| 5291 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5292 | const char find_cn[] = "CN"; |
| 5293 | const struct chunk find_cn_chunk = { |
| 5294 | .str = (char *)&find_cn, |
| 5295 | .len = sizeof(find_cn)-1 |
| 5296 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5297 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5298 | |
| 5299 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5300 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5301 | |
| 5302 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5303 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5304 | if (!crt) |
| 5305 | goto out; |
| 5306 | |
| 5307 | name = X509_get_subject_name(crt); |
| 5308 | if (!name) |
| 5309 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5310 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5311 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 5312 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5313 | if (crt) |
| 5314 | X509_free(crt); |
| 5315 | |
| 5316 | return result; |
| 5317 | } |
| 5318 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 5319 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 5320 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 5321 | { |
| 5322 | X509 *crt = NULL; |
| 5323 | |
| 5324 | if (!ssl_sock_is_ssl(conn)) |
| 5325 | return 0; |
| 5326 | |
| 5327 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5328 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5329 | if (!crt) |
| 5330 | return 0; |
| 5331 | |
| 5332 | X509_free(crt); |
| 5333 | return 1; |
| 5334 | } |
| 5335 | |
| 5336 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 5337 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5338 | { |
| 5339 | if (!ssl_sock_is_ssl(conn)) |
| 5340 | return 0; |
| 5341 | |
| 5342 | return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
| 5343 | } |
| 5344 | |
| 5345 | /* returns result from SSL verify */ |
| 5346 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 5347 | { |
| 5348 | if (!ssl_sock_is_ssl(conn)) |
| 5349 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 5350 | |
| 5351 | return (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
| 5352 | } |
| 5353 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5354 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 5355 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 5356 | /* boolean, returns true if client cert was present */ |
| 5357 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5358 | 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] | 5359 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5360 | struct connection *conn; |
| 5361 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5362 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5363 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 5364 | return 0; |
| 5365 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5366 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 5367 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5368 | return 0; |
| 5369 | } |
| 5370 | |
| 5371 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5372 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5373 | 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] | 5374 | |
| 5375 | return 1; |
| 5376 | } |
| 5377 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5378 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 5379 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5380 | * should be use. |
| 5381 | */ |
| 5382 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5383 | 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] | 5384 | { |
| 5385 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 5386 | X509 *crt = NULL; |
| 5387 | int ret = 0; |
| 5388 | struct chunk *smp_trash; |
| 5389 | struct connection *conn; |
| 5390 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5391 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5392 | if (!conn || conn->xprt != &ssl_sock) |
| 5393 | return 0; |
| 5394 | |
| 5395 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 5396 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5397 | return 0; |
| 5398 | } |
| 5399 | |
| 5400 | if (cert_peer) |
| 5401 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5402 | else |
| 5403 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5404 | |
| 5405 | if (!crt) |
| 5406 | goto out; |
| 5407 | |
| 5408 | smp_trash = get_trash_chunk(); |
| 5409 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 5410 | goto out; |
| 5411 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5412 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5413 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5414 | ret = 1; |
| 5415 | out: |
| 5416 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5417 | if (cert_peer && crt) |
| 5418 | X509_free(crt); |
| 5419 | return ret; |
| 5420 | } |
| 5421 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5422 | /* binary, returns serial of certificate in a binary chunk. |
| 5423 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5424 | * should be use. |
| 5425 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5426 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5427 | 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] | 5428 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5429 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5430 | X509 *crt = NULL; |
| 5431 | int ret = 0; |
| 5432 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5433 | struct connection *conn; |
| 5434 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5435 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5436 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5437 | return 0; |
| 5438 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5439 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5440 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5441 | return 0; |
| 5442 | } |
| 5443 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5444 | if (cert_peer) |
| 5445 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5446 | else |
| 5447 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5448 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5449 | if (!crt) |
| 5450 | goto out; |
| 5451 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5452 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5453 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 5454 | goto out; |
| 5455 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5456 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5457 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5458 | ret = 1; |
| 5459 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5460 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5461 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5462 | X509_free(crt); |
| 5463 | return ret; |
| 5464 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 5465 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5466 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 5467 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5468 | * should be use. |
| 5469 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5470 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5471 | 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] | 5472 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5473 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5474 | X509 *crt = NULL; |
| 5475 | const EVP_MD *digest; |
| 5476 | int ret = 0; |
| 5477 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5478 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5479 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5480 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5481 | if (!conn || conn->xprt != &ssl_sock) |
| 5482 | return 0; |
| 5483 | |
| 5484 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5485 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5486 | return 0; |
| 5487 | } |
| 5488 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5489 | if (cert_peer) |
| 5490 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5491 | else |
| 5492 | crt = SSL_get_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5493 | if (!crt) |
| 5494 | goto out; |
| 5495 | |
| 5496 | smp_trash = get_trash_chunk(); |
| 5497 | digest = EVP_sha1(); |
| 5498 | X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len); |
| 5499 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5500 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5501 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5502 | ret = 1; |
| 5503 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5504 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5505 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5506 | X509_free(crt); |
| 5507 | return ret; |
| 5508 | } |
| 5509 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5510 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 5511 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5512 | * should be use. |
| 5513 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5514 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5515 | 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] | 5516 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5517 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5518 | X509 *crt = NULL; |
| 5519 | int ret = 0; |
| 5520 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5521 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5522 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5523 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5524 | if (!conn || conn->xprt != &ssl_sock) |
| 5525 | return 0; |
| 5526 | |
| 5527 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5528 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5529 | return 0; |
| 5530 | } |
| 5531 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5532 | if (cert_peer) |
| 5533 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5534 | else |
| 5535 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5536 | if (!crt) |
| 5537 | goto out; |
| 5538 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5539 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5540 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 5541 | goto out; |
| 5542 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5543 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5544 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5545 | ret = 1; |
| 5546 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5547 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5548 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5549 | X509_free(crt); |
| 5550 | return ret; |
| 5551 | } |
| 5552 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5553 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 5554 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5555 | * should be use. |
| 5556 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5557 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5558 | 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] | 5559 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5560 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5561 | X509 *crt = NULL; |
| 5562 | X509_NAME *name; |
| 5563 | int ret = 0; |
| 5564 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5565 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5566 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5567 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5568 | if (!conn || conn->xprt != &ssl_sock) |
| 5569 | return 0; |
| 5570 | |
| 5571 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5572 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5573 | return 0; |
| 5574 | } |
| 5575 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5576 | if (cert_peer) |
| 5577 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5578 | else |
| 5579 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5580 | if (!crt) |
| 5581 | goto out; |
| 5582 | |
| 5583 | name = X509_get_issuer_name(crt); |
| 5584 | if (!name) |
| 5585 | goto out; |
| 5586 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5587 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5588 | if (args && args[0].type == ARGT_STR) { |
| 5589 | int pos = 1; |
| 5590 | |
| 5591 | if (args[1].type == ARGT_SINT) |
| 5592 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5593 | |
| 5594 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 5595 | goto out; |
| 5596 | } |
| 5597 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 5598 | goto out; |
| 5599 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5600 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5601 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5602 | ret = 1; |
| 5603 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5604 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5605 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5606 | X509_free(crt); |
| 5607 | return ret; |
| 5608 | } |
| 5609 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5610 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 5611 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5612 | * should be use. |
| 5613 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5614 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5615 | 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] | 5616 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5617 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5618 | X509 *crt = NULL; |
| 5619 | int ret = 0; |
| 5620 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5621 | struct connection *conn; |
| 5622 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5623 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5624 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5625 | return 0; |
| 5626 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5627 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5628 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5629 | return 0; |
| 5630 | } |
| 5631 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5632 | if (cert_peer) |
| 5633 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5634 | else |
| 5635 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5636 | if (!crt) |
| 5637 | goto out; |
| 5638 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5639 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5640 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 5641 | goto out; |
| 5642 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5643 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5644 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5645 | ret = 1; |
| 5646 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5647 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5648 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5649 | X509_free(crt); |
| 5650 | return ret; |
| 5651 | } |
| 5652 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5653 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 5654 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5655 | * should be use. |
| 5656 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5657 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5658 | 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] | 5659 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5660 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5661 | X509 *crt = NULL; |
| 5662 | X509_NAME *name; |
| 5663 | int ret = 0; |
| 5664 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5665 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5666 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5667 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5668 | if (!conn || conn->xprt != &ssl_sock) |
| 5669 | return 0; |
| 5670 | |
| 5671 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5672 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5673 | return 0; |
| 5674 | } |
| 5675 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5676 | if (cert_peer) |
| 5677 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5678 | else |
| 5679 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5680 | if (!crt) |
| 5681 | goto out; |
| 5682 | |
| 5683 | name = X509_get_subject_name(crt); |
| 5684 | if (!name) |
| 5685 | goto out; |
| 5686 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5687 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5688 | if (args && args[0].type == ARGT_STR) { |
| 5689 | int pos = 1; |
| 5690 | |
| 5691 | if (args[1].type == ARGT_SINT) |
| 5692 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5693 | |
| 5694 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 5695 | goto out; |
| 5696 | } |
| 5697 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 5698 | goto out; |
| 5699 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5700 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5701 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5702 | ret = 1; |
| 5703 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5704 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5705 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5706 | X509_free(crt); |
| 5707 | return ret; |
| 5708 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5709 | |
| 5710 | /* integer, returns true if current session use a client certificate */ |
| 5711 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5712 | 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] | 5713 | { |
| 5714 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5715 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5716 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5717 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5718 | if (!conn || conn->xprt != &ssl_sock) |
| 5719 | return 0; |
| 5720 | |
| 5721 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5722 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5723 | return 0; |
| 5724 | } |
| 5725 | |
| 5726 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5727 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5728 | if (crt) { |
| 5729 | X509_free(crt); |
| 5730 | } |
| 5731 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5732 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5733 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5734 | return 1; |
| 5735 | } |
| 5736 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5737 | /* integer, returns the certificate version |
| 5738 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5739 | * should be use. |
| 5740 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5741 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5742 | 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] | 5743 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5744 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5745 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5746 | struct connection *conn; |
| 5747 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5748 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5749 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5750 | return 0; |
| 5751 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5752 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5753 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5754 | return 0; |
| 5755 | } |
| 5756 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5757 | if (cert_peer) |
| 5758 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5759 | else |
| 5760 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5761 | if (!crt) |
| 5762 | return 0; |
| 5763 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5764 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5765 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5766 | if (cert_peer) |
| 5767 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5768 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5769 | |
| 5770 | return 1; |
| 5771 | } |
| 5772 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5773 | /* string, returns the certificate's signature algorithm. |
| 5774 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5775 | * should be use. |
| 5776 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5777 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5778 | 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] | 5779 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5780 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5781 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5782 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5783 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5784 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5785 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5786 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5787 | if (!conn || conn->xprt != &ssl_sock) |
| 5788 | return 0; |
| 5789 | |
| 5790 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5791 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5792 | return 0; |
| 5793 | } |
| 5794 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5795 | if (cert_peer) |
| 5796 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5797 | else |
| 5798 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5799 | if (!crt) |
| 5800 | return 0; |
| 5801 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5802 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 5803 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5804 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5805 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 5806 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5807 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5808 | if (cert_peer) |
| 5809 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5810 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 5811 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5812 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5813 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5814 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5815 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5816 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5817 | if (cert_peer) |
| 5818 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5819 | |
| 5820 | return 1; |
| 5821 | } |
| 5822 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5823 | /* string, returns the certificate's key algorithm. |
| 5824 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5825 | * should be use. |
| 5826 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5827 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5828 | 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] | 5829 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5830 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5831 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5832 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5833 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5834 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5835 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5836 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5837 | if (!conn || conn->xprt != &ssl_sock) |
| 5838 | return 0; |
| 5839 | |
| 5840 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5841 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5842 | return 0; |
| 5843 | } |
| 5844 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5845 | if (cert_peer) |
| 5846 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5847 | else |
| 5848 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5849 | if (!crt) |
| 5850 | return 0; |
| 5851 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5852 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 5853 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5854 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5855 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 5856 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5857 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5858 | if (cert_peer) |
| 5859 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5860 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 5861 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5862 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5863 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5864 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5865 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5866 | if (cert_peer) |
| 5867 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5868 | |
| 5869 | return 1; |
| 5870 | } |
| 5871 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5872 | /* boolean, returns true if front conn. transport layer is SSL. |
| 5873 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5874 | * char is 'b'. |
| 5875 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5876 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5877 | 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] | 5878 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5879 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5880 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5881 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5882 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5883 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5884 | return 1; |
| 5885 | } |
| 5886 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5887 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5888 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5889 | 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] | 5890 | { |
| 5891 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5892 | struct connection *conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5893 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5894 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5895 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5896 | conn->xprt_ctx && |
| 5897 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5898 | return 1; |
| 5899 | #else |
| 5900 | return 0; |
| 5901 | #endif |
| 5902 | } |
| 5903 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 5904 | /* boolean, returns true if client session has been resumed */ |
| 5905 | static int |
| 5906 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 5907 | { |
| 5908 | struct connection *conn = objt_conn(smp->sess->origin); |
| 5909 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5910 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5911 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 5912 | conn->xprt_ctx && |
| 5913 | SSL_session_reused(conn->xprt_ctx); |
| 5914 | return 1; |
| 5915 | } |
| 5916 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5917 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 5918 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5919 | * char is 'b'. |
| 5920 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5921 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5922 | 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] | 5923 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5924 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5925 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5926 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5927 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5928 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5929 | return 0; |
| 5930 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5931 | smp->data.u.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
| 5932 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5933 | return 0; |
| 5934 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5935 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5936 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5937 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5938 | |
| 5939 | return 1; |
| 5940 | } |
| 5941 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5942 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 5943 | * is SSL. |
| 5944 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5945 | * char is 'b'. |
| 5946 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5947 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5948 | 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] | 5949 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5950 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5951 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5952 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5953 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5954 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5955 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5956 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5957 | return 0; |
| 5958 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 5959 | if (!SSL_get_cipher_bits(conn->xprt_ctx, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5960 | return 0; |
| 5961 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5962 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5963 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5964 | |
| 5965 | return 1; |
| 5966 | } |
| 5967 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5968 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 5969 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5970 | * char is 'b'. |
| 5971 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5972 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5973 | 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] | 5974 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5975 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5976 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5977 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5978 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5979 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5980 | return 0; |
| 5981 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5982 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
| 5983 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5984 | return 0; |
| 5985 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5986 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5987 | |
| 5988 | return 1; |
| 5989 | } |
| 5990 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5991 | #ifdef OPENSSL_NPN_NEGOTIATED |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5992 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5993 | 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] | 5994 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5995 | struct connection *conn; |
| 5996 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5997 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5998 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5999 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6000 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6001 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6002 | return 0; |
| 6003 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6004 | smp->data.u.str.str = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6005 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6006 | (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] | 6007 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6008 | if (!smp->data.u.str.str) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6009 | return 0; |
| 6010 | |
| 6011 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6012 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6013 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6014 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6015 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6016 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6017 | 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] | 6018 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6019 | struct connection *conn; |
| 6020 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6021 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6022 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6023 | |
Willy Tarreau | e26bf05 | 2015-05-12 10:30:12 +0200 | [diff] [blame] | 6024 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6025 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6026 | return 0; |
| 6027 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6028 | smp->data.u.str.str = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6029 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6030 | (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] | 6031 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6032 | if (!smp->data.u.str.str) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6033 | return 0; |
| 6034 | |
| 6035 | return 1; |
| 6036 | } |
| 6037 | #endif |
| 6038 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6039 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 6040 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6041 | * char is 'b'. |
| 6042 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6043 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6044 | 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] | 6045 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6046 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6047 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6048 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6049 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6050 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6051 | return 0; |
| 6052 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6053 | smp->data.u.str.str = (char *)SSL_get_version(conn->xprt_ctx); |
| 6054 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6055 | return 0; |
| 6056 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6057 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6058 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6059 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6060 | |
| 6061 | return 1; |
| 6062 | } |
| 6063 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6064 | /* 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] | 6065 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6066 | * char is 'b'. |
| 6067 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6068 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6069 | 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] | 6070 | { |
| 6071 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6072 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6073 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6074 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6075 | SSL_SESSION *ssl_sess; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6076 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6077 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6078 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6079 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6080 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6081 | return 0; |
| 6082 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6083 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 6084 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6085 | return 0; |
| 6086 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6087 | smp->data.u.str.str = (char *)SSL_SESSION_get_id(ssl_sess, (unsigned int *)&smp->data.u.str.len); |
| 6088 | if (!smp->data.u.str.str || !smp->data.u.str.len) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6089 | return 0; |
| 6090 | |
| 6091 | return 1; |
| 6092 | #else |
| 6093 | return 0; |
| 6094 | #endif |
| 6095 | } |
| 6096 | |
| 6097 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6098 | 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] | 6099 | { |
| 6100 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6101 | struct connection *conn; |
| 6102 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6103 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6104 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6105 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6106 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6107 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6108 | return 0; |
| 6109 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6110 | smp->data.u.str.str = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 6111 | if (!smp->data.u.str.str) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 6112 | return 0; |
| 6113 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6114 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6115 | return 1; |
| 6116 | #else |
| 6117 | return 0; |
| 6118 | #endif |
| 6119 | } |
| 6120 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6121 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6122 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6123 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6124 | struct connection *conn; |
| 6125 | struct ssl_capture *capture; |
| 6126 | |
| 6127 | conn = objt_conn(smp->sess->origin); |
| 6128 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6129 | return 0; |
| 6130 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6131 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6132 | if (!capture) |
| 6133 | return 0; |
| 6134 | |
| 6135 | smp->flags = SMP_F_CONST; |
| 6136 | smp->data.type = SMP_T_BIN; |
| 6137 | smp->data.u.str.str = capture->ciphersuite; |
| 6138 | smp->data.u.str.len = capture->ciphersuite_len; |
| 6139 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6140 | } |
| 6141 | |
| 6142 | static int |
| 6143 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6144 | { |
| 6145 | struct chunk *data; |
| 6146 | |
| 6147 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 6148 | return 0; |
| 6149 | |
| 6150 | data = get_trash_chunk(); |
| 6151 | dump_binary(data, smp->data.u.str.str, smp->data.u.str.len); |
| 6152 | smp->data.type = SMP_T_BIN; |
| 6153 | smp->data.u.str = *data; |
| 6154 | return 1; |
| 6155 | } |
| 6156 | |
| 6157 | static int |
| 6158 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6159 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6160 | struct connection *conn; |
| 6161 | struct ssl_capture *capture; |
| 6162 | |
| 6163 | conn = objt_conn(smp->sess->origin); |
| 6164 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6165 | return 0; |
| 6166 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6167 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6168 | if (!capture) |
| 6169 | return 0; |
| 6170 | |
| 6171 | smp->data.type = SMP_T_SINT; |
| 6172 | smp->data.u.sint = capture->xxh64; |
| 6173 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6174 | } |
| 6175 | |
| 6176 | static int |
| 6177 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6178 | { |
| 6179 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && !defined(OPENSSL_NO_SSL_TRACE) |
| 6180 | struct chunk *data; |
| 6181 | SSL_CIPHER cipher; |
| 6182 | int i; |
| 6183 | const char *str; |
| 6184 | unsigned char *bin; |
| 6185 | |
| 6186 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 6187 | return 0; |
| 6188 | |
| 6189 | /* The cipher algorith must not be SSL_SSLV2, because this |
| 6190 | * SSL version seems to not have the same cipher encoding, |
| 6191 | * and it is not supported by OpenSSL. Unfortunately, the |
| 6192 | * #define SSL_SSLV2, SSL_SSLV3 and others are not available |
| 6193 | * with standard defines. We just set the variable to 0, |
| 6194 | * ensure that the match with SSL_SSLV2 fails. |
| 6195 | */ |
| 6196 | cipher.algorithm_ssl = 0; |
| 6197 | |
| 6198 | data = get_trash_chunk(); |
| 6199 | for (i = 0; i + 1 < smp->data.u.str.len; i += 2) { |
| 6200 | bin = (unsigned char *)smp->data.u.str.str + i; |
| 6201 | cipher.id = (unsigned int)(bin[0] << 8) | bin[1]; |
| 6202 | str = SSL_CIPHER_standard_name(&cipher); |
| 6203 | if (!str || strcmp(str, "UNKNOWN") == 0) |
| 6204 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", (unsigned int)cipher.id); |
| 6205 | else |
| 6206 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 6207 | } |
| 6208 | smp->data.type = SMP_T_STR; |
| 6209 | smp->data.u.str = *data; |
| 6210 | return 1; |
| 6211 | #else |
| 6212 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 6213 | #endif |
| 6214 | } |
| 6215 | |
| 6216 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6217 | 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] | 6218 | { |
| 6219 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6220 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6221 | smp->strm ? smp->strm->si[1].end : NULL); |
| 6222 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6223 | int finished_len; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6224 | struct chunk *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6225 | |
| 6226 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6227 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6228 | return 0; |
| 6229 | |
| 6230 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 6231 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6232 | return 0; |
| 6233 | } |
| 6234 | |
| 6235 | finished_trash = get_trash_chunk(); |
| 6236 | if (!SSL_session_reused(conn->xprt_ctx)) |
| 6237 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 6238 | else |
| 6239 | finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 6240 | |
| 6241 | if (!finished_len) |
| 6242 | return 0; |
| 6243 | |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 6244 | finished_trash->len = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6245 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6246 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6247 | |
| 6248 | return 1; |
| 6249 | #else |
| 6250 | return 0; |
| 6251 | #endif |
| 6252 | } |
| 6253 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6254 | /* 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] | 6255 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6256 | 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] | 6257 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6258 | struct connection *conn; |
| 6259 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6260 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6261 | if (!conn || conn->xprt != &ssl_sock) |
| 6262 | return 0; |
| 6263 | |
| 6264 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6265 | smp->flags = SMP_F_MAY_CHANGE; |
| 6266 | return 0; |
| 6267 | } |
| 6268 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6269 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6270 | 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] | 6271 | smp->flags = 0; |
| 6272 | |
| 6273 | return 1; |
| 6274 | } |
| 6275 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6276 | /* 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] | 6277 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6278 | 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] | 6279 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6280 | struct connection *conn; |
| 6281 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6282 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6283 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6284 | return 0; |
| 6285 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6286 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6287 | smp->flags = SMP_F_MAY_CHANGE; |
| 6288 | return 0; |
| 6289 | } |
| 6290 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6291 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6292 | 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] | 6293 | smp->flags = 0; |
| 6294 | |
| 6295 | return 1; |
| 6296 | } |
| 6297 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6298 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6299 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6300 | 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] | 6301 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6302 | struct connection *conn; |
| 6303 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6304 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6305 | if (!conn || conn->xprt != &ssl_sock) |
| 6306 | return 0; |
| 6307 | |
| 6308 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6309 | smp->flags = SMP_F_MAY_CHANGE; |
| 6310 | return 0; |
| 6311 | } |
| 6312 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6313 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6314 | 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] | 6315 | smp->flags = 0; |
| 6316 | |
| 6317 | return 1; |
| 6318 | } |
| 6319 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6320 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 6321 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6322 | 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] | 6323 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6324 | struct connection *conn; |
| 6325 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6326 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6327 | if (!conn || conn->xprt != &ssl_sock) |
| 6328 | return 0; |
| 6329 | |
| 6330 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 6331 | smp->flags = SMP_F_MAY_CHANGE; |
| 6332 | return 0; |
| 6333 | } |
| 6334 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6335 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 6336 | return 0; |
| 6337 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6338 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6339 | 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] | 6340 | smp->flags = 0; |
| 6341 | |
| 6342 | return 1; |
| 6343 | } |
| 6344 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 6345 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6346 | 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] | 6347 | { |
| 6348 | if (!*args[cur_arg + 1]) { |
| 6349 | if (err) |
| 6350 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 6351 | return ERR_ALERT | ERR_FATAL; |
| 6352 | } |
| 6353 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6354 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6355 | 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] | 6356 | else |
| 6357 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 6358 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6359 | return 0; |
| 6360 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6361 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6362 | { |
| 6363 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 6364 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6365 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6366 | /* parse the "ca-sign-file" bind keyword */ |
| 6367 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6368 | { |
| 6369 | if (!*args[cur_arg + 1]) { |
| 6370 | if (err) |
| 6371 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 6372 | return ERR_ALERT | ERR_FATAL; |
| 6373 | } |
| 6374 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6375 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6376 | 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] | 6377 | else |
| 6378 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 6379 | |
| 6380 | return 0; |
| 6381 | } |
| 6382 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 6383 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6384 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6385 | { |
| 6386 | if (!*args[cur_arg + 1]) { |
| 6387 | if (err) |
| 6388 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 6389 | return ERR_ALERT | ERR_FATAL; |
| 6390 | } |
| 6391 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 6392 | return 0; |
| 6393 | } |
| 6394 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6395 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6396 | 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] | 6397 | { |
| 6398 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 6399 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6400 | return ERR_ALERT | ERR_FATAL; |
| 6401 | } |
| 6402 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 6403 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6404 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6405 | return 0; |
| 6406 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6407 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6408 | { |
| 6409 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 6410 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6411 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6412 | 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] | 6413 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 6414 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 6415 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6416 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 6417 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6418 | return ERR_ALERT | ERR_FATAL; |
| 6419 | } |
| 6420 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6421 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 6422 | 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] | 6423 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 6424 | return ERR_ALERT | ERR_FATAL; |
| 6425 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6426 | 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] | 6427 | if (ssl_sock_load_cert(path, conf, err) > 0) |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 6428 | return ERR_ALERT | ERR_FATAL; |
| 6429 | |
| 6430 | return 0; |
| 6431 | } |
| 6432 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6433 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6434 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6435 | |
| 6436 | return 0; |
| 6437 | } |
| 6438 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 6439 | /* parse the "crt-list" bind keyword */ |
| 6440 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6441 | { |
| 6442 | if (!*args[cur_arg + 1]) { |
| 6443 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 6444 | return ERR_ALERT | ERR_FATAL; |
| 6445 | } |
| 6446 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6447 | 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] | 6448 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 6449 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 6450 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 6451 | |
| 6452 | return 0; |
| 6453 | } |
| 6454 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 6455 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6456 | 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] | 6457 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 6458 | #ifndef X509_V_FLAG_CRL_CHECK |
| 6459 | if (err) |
| 6460 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 6461 | return ERR_ALERT | ERR_FATAL; |
| 6462 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6463 | if (!*args[cur_arg + 1]) { |
| 6464 | if (err) |
| 6465 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 6466 | return ERR_ALERT | ERR_FATAL; |
| 6467 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6468 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6469 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6470 | 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] | 6471 | else |
| 6472 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 6473 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6474 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 6475 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6476 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6477 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6478 | { |
| 6479 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 6480 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6481 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 6482 | /* parse the "curves" bind keyword keyword */ |
| 6483 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 6484 | { |
| 6485 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 6486 | if (!*args[cur_arg + 1]) { |
| 6487 | if (err) |
| 6488 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 6489 | return ERR_ALERT | ERR_FATAL; |
| 6490 | } |
| 6491 | conf->curves = strdup(args[cur_arg + 1]); |
| 6492 | return 0; |
| 6493 | #else |
| 6494 | if (err) |
| 6495 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 6496 | return ERR_ALERT | ERR_FATAL; |
| 6497 | #endif |
| 6498 | } |
| 6499 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6500 | { |
| 6501 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 6502 | } |
| 6503 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 6504 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6505 | 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] | 6506 | { |
| 6507 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 6508 | if (err) |
| 6509 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 6510 | return ERR_ALERT | ERR_FATAL; |
| 6511 | #elif defined(OPENSSL_NO_ECDH) |
| 6512 | if (err) |
| 6513 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 6514 | return ERR_ALERT | ERR_FATAL; |
| 6515 | #else |
| 6516 | if (!*args[cur_arg + 1]) { |
| 6517 | if (err) |
| 6518 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 6519 | return ERR_ALERT | ERR_FATAL; |
| 6520 | } |
| 6521 | |
| 6522 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6523 | |
| 6524 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6525 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6526 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6527 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6528 | { |
| 6529 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 6530 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6531 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 6532 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 6533 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6534 | { |
| 6535 | int code; |
| 6536 | char *p = args[cur_arg + 1]; |
| 6537 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 6538 | |
| 6539 | if (!*p) { |
| 6540 | if (err) |
| 6541 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 6542 | return ERR_ALERT | ERR_FATAL; |
| 6543 | } |
| 6544 | |
| 6545 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 6546 | ignerr = &conf->ca_ignerr; |
| 6547 | |
| 6548 | if (strcmp(p, "all") == 0) { |
| 6549 | *ignerr = ~0ULL; |
| 6550 | return 0; |
| 6551 | } |
| 6552 | |
| 6553 | while (p) { |
| 6554 | code = atoi(p); |
| 6555 | if ((code <= 0) || (code > 63)) { |
| 6556 | if (err) |
| 6557 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 6558 | args[cur_arg], code, args[cur_arg + 1]); |
| 6559 | return ERR_ALERT | ERR_FATAL; |
| 6560 | } |
| 6561 | *ignerr |= 1ULL << code; |
| 6562 | p = strchr(p, ','); |
| 6563 | if (p) |
| 6564 | p++; |
| 6565 | } |
| 6566 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6567 | return 0; |
| 6568 | } |
| 6569 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6570 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 6571 | 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] | 6572 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6573 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6574 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6575 | p = strchr(arg, '-'); |
| 6576 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6577 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6578 | p++; |
| 6579 | if (!strcmp(p, "sslv3")) |
| 6580 | v = CONF_SSLV3; |
| 6581 | else if (!strcmp(p, "tlsv10")) |
| 6582 | v = CONF_TLSV10; |
| 6583 | else if (!strcmp(p, "tlsv11")) |
| 6584 | v = CONF_TLSV11; |
| 6585 | else if (!strcmp(p, "tlsv12")) |
| 6586 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 6587 | else if (!strcmp(p, "tlsv13")) |
| 6588 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6589 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6590 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6591 | if (!strncmp(arg, "no-", 3)) |
| 6592 | methods->flags |= methodVersions[v].flag; |
| 6593 | else if (!strncmp(arg, "force-", 6)) |
| 6594 | methods->min = methods->max = v; |
| 6595 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6596 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 6597 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6598 | fail: |
| 6599 | if (err) |
| 6600 | memprintf(err, "'%s' : option not implemented", arg); |
| 6601 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 6602 | } |
| 6603 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6604 | 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] | 6605 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6606 | return parse_tls_method_options(args[cur_arg], &conf->ssl_methods, err); |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6607 | } |
| 6608 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6609 | 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] | 6610 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6611 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 6612 | } |
| 6613 | |
| 6614 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 6615 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 6616 | { |
| 6617 | uint16_t i, v = 0; |
| 6618 | char *argv = args[cur_arg + 1]; |
| 6619 | if (!*argv) { |
| 6620 | if (err) |
| 6621 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
| 6622 | return ERR_ALERT | ERR_FATAL; |
| 6623 | } |
| 6624 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 6625 | if (!strcmp(argv, methodVersions[i].name)) |
| 6626 | v = i; |
| 6627 | if (!v) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6628 | if (err) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6629 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6630 | return ERR_ALERT | ERR_FATAL; |
| 6631 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6632 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 6633 | methods->min = v; |
| 6634 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 6635 | methods->max = v; |
| 6636 | else { |
| 6637 | if (err) |
| 6638 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
| 6639 | return ERR_ALERT | ERR_FATAL; |
| 6640 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6641 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6642 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6643 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 6644 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6645 | { |
| 6646 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 6647 | } |
| 6648 | |
| 6649 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6650 | { |
| 6651 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 6652 | } |
| 6653 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 6654 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6655 | 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] | 6656 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 6657 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 6658 | return 0; |
| 6659 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 6660 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6661 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6662 | 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] | 6663 | { |
| 6664 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 6665 | char *p1, *p2; |
| 6666 | |
| 6667 | if (!*args[cur_arg + 1]) { |
| 6668 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 6669 | return ERR_ALERT | ERR_FATAL; |
| 6670 | } |
| 6671 | |
| 6672 | free(conf->npn_str); |
| 6673 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 6674 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 6675 | * so we reuse each comma to store the next <len> and need |
| 6676 | * one more for the end of the string. |
| 6677 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6678 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 6679 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6680 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 6681 | |
| 6682 | /* replace commas with the name length */ |
| 6683 | p1 = conf->npn_str; |
| 6684 | p2 = p1 + 1; |
| 6685 | while (1) { |
| 6686 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 6687 | if (!p2) |
| 6688 | p2 = p1 + 1 + strlen(p1 + 1); |
| 6689 | |
| 6690 | if (p2 - (p1 + 1) > 255) { |
| 6691 | *p2 = '\0'; |
| 6692 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 6693 | return ERR_ALERT | ERR_FATAL; |
| 6694 | } |
| 6695 | |
| 6696 | *p1 = p2 - (p1 + 1); |
| 6697 | p1 = p2; |
| 6698 | |
| 6699 | if (!*p2) |
| 6700 | break; |
| 6701 | |
| 6702 | *(p2++) = '\0'; |
| 6703 | } |
| 6704 | return 0; |
| 6705 | #else |
| 6706 | if (err) |
| 6707 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 6708 | return ERR_ALERT | ERR_FATAL; |
| 6709 | #endif |
| 6710 | } |
| 6711 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6712 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6713 | { |
| 6714 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 6715 | } |
| 6716 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6717 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6718 | 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] | 6719 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6720 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6721 | char *p1, *p2; |
| 6722 | |
| 6723 | if (!*args[cur_arg + 1]) { |
| 6724 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 6725 | return ERR_ALERT | ERR_FATAL; |
| 6726 | } |
| 6727 | |
| 6728 | free(conf->alpn_str); |
| 6729 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 6730 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 6731 | * so we reuse each comma to store the next <len> and need |
| 6732 | * one more for the end of the string. |
| 6733 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6734 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 6735 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6736 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 6737 | |
| 6738 | /* replace commas with the name length */ |
| 6739 | p1 = conf->alpn_str; |
| 6740 | p2 = p1 + 1; |
| 6741 | while (1) { |
| 6742 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 6743 | if (!p2) |
| 6744 | p2 = p1 + 1 + strlen(p1 + 1); |
| 6745 | |
| 6746 | if (p2 - (p1 + 1) > 255) { |
| 6747 | *p2 = '\0'; |
| 6748 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 6749 | return ERR_ALERT | ERR_FATAL; |
| 6750 | } |
| 6751 | |
| 6752 | *p1 = p2 - (p1 + 1); |
| 6753 | p1 = p2; |
| 6754 | |
| 6755 | if (!*p2) |
| 6756 | break; |
| 6757 | |
| 6758 | *(p2++) = '\0'; |
| 6759 | } |
| 6760 | return 0; |
| 6761 | #else |
| 6762 | if (err) |
| 6763 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 6764 | return ERR_ALERT | ERR_FATAL; |
| 6765 | #endif |
| 6766 | } |
| 6767 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6768 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6769 | { |
| 6770 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 6771 | } |
| 6772 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6773 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6774 | 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] | 6775 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 6776 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6777 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 6778 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6779 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 6780 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6781 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6782 | conf->ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 6783 | if (!conf->ssl_methods.min) |
| 6784 | conf->ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 6785 | if (!conf->ssl_methods.max) |
| 6786 | conf->ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 6787 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6788 | return 0; |
| 6789 | } |
| 6790 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 6791 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 6792 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6793 | { |
| 6794 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 6795 | return 0; |
| 6796 | } |
| 6797 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6798 | /* parse the "generate-certificates" bind keyword */ |
| 6799 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6800 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6801 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6802 | conf->generate_certs = 1; |
| 6803 | #else |
| 6804 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 6805 | err && *err ? *err : ""); |
| 6806 | #endif |
| 6807 | return 0; |
| 6808 | } |
| 6809 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 6810 | /* parse the "strict-sni" bind keyword */ |
| 6811 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6812 | { |
| 6813 | conf->strict_sni = 1; |
| 6814 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6815 | } |
| 6816 | |
| 6817 | /* parse the "tls-ticket-keys" bind keyword */ |
| 6818 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6819 | { |
| 6820 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6821 | FILE *f; |
| 6822 | int i = 0; |
| 6823 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6824 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6825 | |
| 6826 | if (!*args[cur_arg + 1]) { |
| 6827 | if (err) |
| 6828 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 6829 | return ERR_ALERT | ERR_FATAL; |
| 6830 | } |
| 6831 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 6832 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
| 6833 | if(keys_ref) { |
| 6834 | conf->keys_ref = keys_ref; |
| 6835 | return 0; |
| 6836 | } |
| 6837 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 6838 | keys_ref = malloc(sizeof(*keys_ref)); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6839 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct tls_sess_key)); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6840 | |
| 6841 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 6842 | if (err) |
| 6843 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 6844 | return ERR_ALERT | ERR_FATAL; |
| 6845 | } |
| 6846 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6847 | keys_ref->filename = strdup(args[cur_arg + 1]); |
| 6848 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6849 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 6850 | int len = strlen(thisline); |
| 6851 | /* Strip newline characters from the end */ |
| 6852 | if(thisline[len - 1] == '\n') |
| 6853 | thisline[--len] = 0; |
| 6854 | |
| 6855 | if(thisline[len - 1] == '\r') |
| 6856 | thisline[--len] = 0; |
| 6857 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6858 | 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] | 6859 | if (err) |
| 6860 | 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] | 6861 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6862 | return ERR_ALERT | ERR_FATAL; |
| 6863 | } |
| 6864 | i++; |
| 6865 | } |
| 6866 | |
| 6867 | if (i < TLS_TICKETS_NO) { |
| 6868 | if (err) |
| 6869 | 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] | 6870 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6871 | return ERR_ALERT | ERR_FATAL; |
| 6872 | } |
| 6873 | |
| 6874 | fclose(f); |
| 6875 | |
| 6876 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 6877 | i -= 2; |
| 6878 | 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] | 6879 | keys_ref->unique_id = -1; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6880 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6881 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 6882 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 6883 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6884 | return 0; |
| 6885 | #else |
| 6886 | if (err) |
| 6887 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 6888 | return ERR_ALERT | ERR_FATAL; |
| 6889 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 6890 | } |
| 6891 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6892 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6893 | 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] | 6894 | { |
| 6895 | if (!*args[cur_arg + 1]) { |
| 6896 | if (err) |
| 6897 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 6898 | return ERR_ALERT | ERR_FATAL; |
| 6899 | } |
| 6900 | |
| 6901 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6902 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6903 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6904 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6905 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6906 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6907 | else { |
| 6908 | if (err) |
| 6909 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 6910 | args[cur_arg], args[cur_arg + 1]); |
| 6911 | return ERR_ALERT | ERR_FATAL; |
| 6912 | } |
| 6913 | |
| 6914 | return 0; |
| 6915 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6916 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6917 | { |
| 6918 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 6919 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6920 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6921 | /************** "server" keywords ****************/ |
| 6922 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6923 | /* parse the "ca-file" server keyword */ |
| 6924 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6925 | { |
| 6926 | if (!*args[*cur_arg + 1]) { |
| 6927 | if (err) |
| 6928 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 6929 | return ERR_ALERT | ERR_FATAL; |
| 6930 | } |
| 6931 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6932 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6933 | 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] | 6934 | else |
| 6935 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 6936 | |
| 6937 | return 0; |
| 6938 | } |
| 6939 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6940 | /* parse the "check-ssl" server keyword */ |
| 6941 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6942 | { |
| 6943 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6944 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 6945 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
| 6946 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 6947 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 6948 | if (!newsrv->ssl_ctx.methods.min) |
| 6949 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 6950 | if (!newsrv->ssl_ctx.methods.max) |
| 6951 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 6952 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6953 | return 0; |
| 6954 | } |
| 6955 | |
| 6956 | /* parse the "ciphers" server keyword */ |
| 6957 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6958 | { |
| 6959 | if (!*args[*cur_arg + 1]) { |
| 6960 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 6961 | return ERR_ALERT | ERR_FATAL; |
| 6962 | } |
| 6963 | |
| 6964 | free(newsrv->ssl_ctx.ciphers); |
| 6965 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 6966 | return 0; |
| 6967 | } |
| 6968 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6969 | /* parse the "crl-file" server keyword */ |
| 6970 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6971 | { |
| 6972 | #ifndef X509_V_FLAG_CRL_CHECK |
| 6973 | if (err) |
| 6974 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 6975 | return ERR_ALERT | ERR_FATAL; |
| 6976 | #else |
| 6977 | if (!*args[*cur_arg + 1]) { |
| 6978 | if (err) |
| 6979 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 6980 | return ERR_ALERT | ERR_FATAL; |
| 6981 | } |
| 6982 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6983 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6984 | 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] | 6985 | else |
| 6986 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 6987 | |
| 6988 | return 0; |
| 6989 | #endif |
| 6990 | } |
| 6991 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6992 | /* parse the "crt" server keyword */ |
| 6993 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6994 | { |
| 6995 | if (!*args[*cur_arg + 1]) { |
| 6996 | if (err) |
| 6997 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 6998 | return ERR_ALERT | ERR_FATAL; |
| 6999 | } |
| 7000 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7001 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
| 7002 | 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] | 7003 | else |
| 7004 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 7005 | |
| 7006 | return 0; |
| 7007 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7008 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 7009 | /* parse the "no-check-ssl" server keyword */ |
| 7010 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7011 | { |
| 7012 | newsrv->check.use_ssl = 0; |
| 7013 | free(newsrv->ssl_ctx.ciphers); |
| 7014 | newsrv->ssl_ctx.ciphers = NULL; |
| 7015 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 7016 | return 0; |
| 7017 | } |
| 7018 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 7019 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 7020 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7021 | { |
| 7022 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 7023 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 7024 | return 0; |
| 7025 | } |
| 7026 | |
| 7027 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 7028 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7029 | { |
| 7030 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 7031 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 7032 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 7033 | return 0; |
| 7034 | } |
| 7035 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 7036 | /* parse the "no-ssl" server keyword */ |
| 7037 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7038 | { |
| 7039 | newsrv->use_ssl = 0; |
| 7040 | free(newsrv->ssl_ctx.ciphers); |
| 7041 | newsrv->ssl_ctx.ciphers = NULL; |
| 7042 | return 0; |
| 7043 | } |
| 7044 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 7045 | /* parse the "no-ssl-reuse" server keyword */ |
| 7046 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7047 | { |
| 7048 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 7049 | return 0; |
| 7050 | } |
| 7051 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7052 | /* parse the "no-tls-tickets" server keyword */ |
| 7053 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7054 | { |
| 7055 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 7056 | return 0; |
| 7057 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7058 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 7059 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7060 | { |
| 7061 | newsrv->pp_opts |= SRV_PP_V2; |
| 7062 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 7063 | return 0; |
| 7064 | } |
| 7065 | |
| 7066 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 7067 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7068 | { |
| 7069 | newsrv->pp_opts |= SRV_PP_V2; |
| 7070 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 7071 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 7072 | return 0; |
| 7073 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7074 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7075 | /* parse the "sni" server keyword */ |
| 7076 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7077 | { |
| 7078 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7079 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 7080 | return ERR_ALERT | ERR_FATAL; |
| 7081 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7082 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7083 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7084 | arg = args[*cur_arg + 1]; |
| 7085 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7086 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 7087 | return ERR_ALERT | ERR_FATAL; |
| 7088 | } |
| 7089 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7090 | free(newsrv->sni_expr); |
| 7091 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7092 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7093 | return 0; |
| 7094 | #endif |
| 7095 | } |
| 7096 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7097 | /* parse the "ssl" server keyword */ |
| 7098 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7099 | { |
| 7100 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7101 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 7102 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7103 | return 0; |
| 7104 | } |
| 7105 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 7106 | /* parse the "ssl-reuse" server keyword */ |
| 7107 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7108 | { |
| 7109 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 7110 | return 0; |
| 7111 | } |
| 7112 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 7113 | /* parse the "tls-tickets" server keyword */ |
| 7114 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7115 | { |
| 7116 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 7117 | return 0; |
| 7118 | } |
| 7119 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7120 | /* parse the "verify" server keyword */ |
| 7121 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7122 | { |
| 7123 | if (!*args[*cur_arg + 1]) { |
| 7124 | if (err) |
| 7125 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 7126 | return ERR_ALERT | ERR_FATAL; |
| 7127 | } |
| 7128 | |
| 7129 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7130 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7131 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7132 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7133 | else { |
| 7134 | if (err) |
| 7135 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 7136 | args[*cur_arg], args[*cur_arg + 1]); |
| 7137 | return ERR_ALERT | ERR_FATAL; |
| 7138 | } |
| 7139 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 7140 | return 0; |
| 7141 | } |
| 7142 | |
| 7143 | /* parse the "verifyhost" server keyword */ |
| 7144 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7145 | { |
| 7146 | if (!*args[*cur_arg + 1]) { |
| 7147 | if (err) |
| 7148 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 7149 | return ERR_ALERT | ERR_FATAL; |
| 7150 | } |
| 7151 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 7152 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 7153 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 7154 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7155 | return 0; |
| 7156 | } |
| 7157 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7158 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 7159 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 7160 | struct proxy *defpx, const char *file, int line, |
| 7161 | char **err) { |
| 7162 | int i = 1; |
| 7163 | |
| 7164 | if (*(args[i]) == 0) { |
| 7165 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 7166 | return -1; |
| 7167 | } |
| 7168 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7169 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7170 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 7171 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 7172 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7173 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 7174 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 7175 | i++; |
| 7176 | else { |
| 7177 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 7178 | return -1; |
| 7179 | } |
| 7180 | } |
| 7181 | 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] | 7182 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 7183 | return -1; |
| 7184 | } |
| 7185 | i++; |
| 7186 | } |
| 7187 | return 0; |
| 7188 | } |
| 7189 | |
| 7190 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 7191 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 7192 | struct proxy *defpx, const char *file, int line, |
| 7193 | char **err) { |
| 7194 | int i = 1; |
| 7195 | |
| 7196 | if (*(args[i]) == 0) { |
| 7197 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 7198 | return -1; |
| 7199 | } |
| 7200 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7201 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7202 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7203 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 7204 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 7205 | i++; |
| 7206 | else { |
| 7207 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 7208 | return -1; |
| 7209 | } |
| 7210 | } |
| 7211 | 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] | 7212 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 7213 | return -1; |
| 7214 | } |
| 7215 | i++; |
| 7216 | } |
| 7217 | return 0; |
| 7218 | } |
| 7219 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 7220 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 7221 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7222 | */ |
| 7223 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 7224 | struct proxy *defpx, const char *file, int line, |
| 7225 | char **err) |
| 7226 | { |
| 7227 | char **target; |
| 7228 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7229 | 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] | 7230 | |
| 7231 | if (too_many_args(1, args, err, NULL)) |
| 7232 | return -1; |
| 7233 | |
| 7234 | if (*target) { |
| 7235 | memprintf(err, "'%s' already specified.", args[0]); |
| 7236 | return -1; |
| 7237 | } |
| 7238 | |
| 7239 | if (*(args[1]) == 0) { |
| 7240 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 7241 | return -1; |
| 7242 | } |
| 7243 | *target = strdup(args[1]); |
| 7244 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7245 | } |
| 7246 | |
| 7247 | /* parse the "ssl-mode-async" keyword in global section. |
| 7248 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7249 | */ |
| 7250 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 7251 | struct proxy *defpx, const char *file, int line, |
| 7252 | char **err) |
| 7253 | { |
| 7254 | #if OPENSSL_VERSION_NUMBER >= 0x1010000fL |
| 7255 | global_ssl.async = 1; |
| 7256 | return 0; |
| 7257 | #else |
| 7258 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 7259 | return -1; |
| 7260 | #endif |
| 7261 | } |
| 7262 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7263 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7264 | static int ssl_check_async_engine_count(void) { |
| 7265 | int err_code = 0; |
| 7266 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7267 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
| 7268 | Alert("ssl-mode-async only supports a maximum of 32 engines.\n"); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7269 | err_code = ERR_ABORT; |
| 7270 | } |
| 7271 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 7272 | } |
| 7273 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7274 | /* parse the "ssl-engine" keyword in global section. |
| 7275 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7276 | */ |
| 7277 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 7278 | struct proxy *defpx, const char *file, int line, |
| 7279 | char **err) |
| 7280 | { |
| 7281 | char *algo; |
| 7282 | int ret = -1; |
| 7283 | |
| 7284 | if (*(args[1]) == 0) { |
| 7285 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 7286 | return ret; |
| 7287 | } |
| 7288 | |
| 7289 | if (*(args[2]) == 0) { |
| 7290 | /* if no list of algorithms is given, it defaults to ALL */ |
| 7291 | algo = strdup("ALL"); |
| 7292 | goto add_engine; |
| 7293 | } |
| 7294 | |
| 7295 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 7296 | if (strcmp(args[2], "algo") != 0) { |
| 7297 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 7298 | return ret; |
| 7299 | } |
| 7300 | |
| 7301 | if (*(args[3]) == 0) { |
| 7302 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 7303 | return ret; |
| 7304 | } |
| 7305 | algo = strdup(args[3]); |
| 7306 | |
| 7307 | add_engine: |
| 7308 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 7309 | openssl_engines_initialized++; |
| 7310 | ret = 0; |
| 7311 | } |
| 7312 | free(algo); |
| 7313 | return ret; |
| 7314 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7315 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7316 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 7317 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 7318 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 7319 | */ |
| 7320 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 7321 | struct proxy *defpx, const char *file, int line, |
| 7322 | char **err) |
| 7323 | { |
| 7324 | char **target; |
| 7325 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7326 | 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] | 7327 | |
| 7328 | if (too_many_args(1, args, err, NULL)) |
| 7329 | return -1; |
| 7330 | |
| 7331 | if (*(args[1]) == 0) { |
| 7332 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 7333 | return -1; |
| 7334 | } |
| 7335 | |
| 7336 | free(*target); |
| 7337 | *target = strdup(args[1]); |
| 7338 | return 0; |
| 7339 | } |
| 7340 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7341 | /* parse various global tune.ssl settings consisting in positive integers. |
| 7342 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7343 | */ |
| 7344 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 7345 | struct proxy *defpx, const char *file, int line, |
| 7346 | char **err) |
| 7347 | { |
| 7348 | int *target; |
| 7349 | |
| 7350 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 7351 | target = &global.tune.sslcachesize; |
| 7352 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7353 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7354 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7355 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 7356 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 7357 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7358 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 7359 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7360 | else { |
| 7361 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 7362 | return -1; |
| 7363 | } |
| 7364 | |
| 7365 | if (too_many_args(1, args, err, NULL)) |
| 7366 | return -1; |
| 7367 | |
| 7368 | if (*(args[1]) == 0) { |
| 7369 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 7370 | return -1; |
| 7371 | } |
| 7372 | |
| 7373 | *target = atoi(args[1]); |
| 7374 | if (*target < 0) { |
| 7375 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 7376 | return -1; |
| 7377 | } |
| 7378 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7379 | } |
| 7380 | |
| 7381 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 7382 | struct proxy *defpx, const char *file, int line, |
| 7383 | char **err) |
| 7384 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7385 | int ret; |
| 7386 | |
| 7387 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 7388 | if (ret != 0) |
| 7389 | return ret; |
| 7390 | |
| 7391 | if (pool2_ssl_capture) { |
| 7392 | memprintf(err, "'%s' is already configured.", args[0]); |
| 7393 | return -1; |
| 7394 | } |
| 7395 | |
| 7396 | pool2_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 7397 | if (!pool2_ssl_capture) { |
| 7398 | memprintf(err, "Out of memory error."); |
| 7399 | return -1; |
| 7400 | } |
| 7401 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7402 | } |
| 7403 | |
| 7404 | /* parse "ssl.force-private-cache". |
| 7405 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7406 | */ |
| 7407 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 7408 | struct proxy *defpx, const char *file, int line, |
| 7409 | char **err) |
| 7410 | { |
| 7411 | if (too_many_args(0, args, err, NULL)) |
| 7412 | return -1; |
| 7413 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7414 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7415 | return 0; |
| 7416 | } |
| 7417 | |
| 7418 | /* parse "ssl.lifetime". |
| 7419 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7420 | */ |
| 7421 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 7422 | struct proxy *defpx, const char *file, int line, |
| 7423 | char **err) |
| 7424 | { |
| 7425 | const char *res; |
| 7426 | |
| 7427 | if (too_many_args(1, args, err, NULL)) |
| 7428 | return -1; |
| 7429 | |
| 7430 | if (*(args[1]) == 0) { |
| 7431 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 7432 | return -1; |
| 7433 | } |
| 7434 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7435 | 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] | 7436 | if (res) { |
| 7437 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 7438 | return -1; |
| 7439 | } |
| 7440 | return 0; |
| 7441 | } |
| 7442 | |
| 7443 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 7444 | /* parse "ssl-dh-param-file". |
| 7445 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7446 | */ |
| 7447 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 7448 | struct proxy *defpx, const char *file, int line, |
| 7449 | char **err) |
| 7450 | { |
| 7451 | if (too_many_args(1, args, err, NULL)) |
| 7452 | return -1; |
| 7453 | |
| 7454 | if (*(args[1]) == 0) { |
| 7455 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 7456 | return -1; |
| 7457 | } |
| 7458 | |
| 7459 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 7460 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 7461 | return -1; |
| 7462 | } |
| 7463 | return 0; |
| 7464 | } |
| 7465 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7466 | /* parse "ssl.default-dh-param". |
| 7467 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7468 | */ |
| 7469 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 7470 | struct proxy *defpx, const char *file, int line, |
| 7471 | char **err) |
| 7472 | { |
| 7473 | if (too_many_args(1, args, err, NULL)) |
| 7474 | return -1; |
| 7475 | |
| 7476 | if (*(args[1]) == 0) { |
| 7477 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 7478 | return -1; |
| 7479 | } |
| 7480 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7481 | global_ssl.default_dh_param = atoi(args[1]); |
| 7482 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7483 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 7484 | return -1; |
| 7485 | } |
| 7486 | return 0; |
| 7487 | } |
| 7488 | #endif |
| 7489 | |
| 7490 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7491 | /* This function is used with TLS ticket keys management. It permits to browse |
| 7492 | * each reference. The variable <getnext> must contain the current node, |
| 7493 | * <end> point to the root node. |
| 7494 | */ |
| 7495 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7496 | static inline |
| 7497 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 7498 | { |
| 7499 | struct tls_keys_ref *ref = getnext; |
| 7500 | |
| 7501 | while (1) { |
| 7502 | |
| 7503 | /* Get next list entry. */ |
| 7504 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 7505 | |
| 7506 | /* If the entry is the last of the list, return NULL. */ |
| 7507 | if (&ref->list == end) |
| 7508 | return NULL; |
| 7509 | |
| 7510 | return ref; |
| 7511 | } |
| 7512 | } |
| 7513 | |
| 7514 | static inline |
| 7515 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 7516 | { |
| 7517 | int id; |
| 7518 | char *error; |
| 7519 | |
| 7520 | /* If the reference starts by a '#', this is numeric id. */ |
| 7521 | if (reference[0] == '#') { |
| 7522 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 7523 | id = strtol(reference + 1, &error, 10); |
| 7524 | if (*error != '\0') |
| 7525 | return NULL; |
| 7526 | |
| 7527 | /* Perform the unique id lookup. */ |
| 7528 | return tlskeys_ref_lookupid(id); |
| 7529 | } |
| 7530 | |
| 7531 | /* Perform the string lookup. */ |
| 7532 | return tlskeys_ref_lookup(reference); |
| 7533 | } |
| 7534 | #endif |
| 7535 | |
| 7536 | |
| 7537 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7538 | |
| 7539 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 7540 | |
| 7541 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 7542 | return cli_io_handler_tlskeys_files(appctx); |
| 7543 | } |
| 7544 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7545 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 7546 | * (next index to be dumped), and cli.p0 (next key reference). |
| 7547 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7548 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 7549 | |
| 7550 | struct stream_interface *si = appctx->owner; |
| 7551 | |
| 7552 | switch (appctx->st2) { |
| 7553 | case STAT_ST_INIT: |
| 7554 | /* Display the column headers. If the message cannot be sent, |
| 7555 | * quit the fucntion with returning 0. The function is called |
| 7556 | * later and restart at the state "STAT_ST_INIT". |
| 7557 | */ |
| 7558 | chunk_reset(&trash); |
| 7559 | |
| 7560 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 7561 | chunk_appendf(&trash, "# id secret\n"); |
| 7562 | else |
| 7563 | chunk_appendf(&trash, "# id (file)\n"); |
| 7564 | |
| 7565 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 7566 | si_applet_cant_put(si); |
| 7567 | return 0; |
| 7568 | } |
| 7569 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7570 | /* Now, we start the browsing of the references lists. |
| 7571 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 7572 | * available field of this pointer is <list>. It is used with the function |
| 7573 | * tlskeys_list_get_next() for retruning the first available entry |
| 7574 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7575 | if (appctx->ctx.cli.p0 == NULL) { |
| 7576 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 7577 | 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] | 7578 | } |
| 7579 | |
| 7580 | appctx->st2 = STAT_ST_LIST; |
| 7581 | /* fall through */ |
| 7582 | |
| 7583 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7584 | while (appctx->ctx.cli.p0) { |
| 7585 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
| 7586 | int head = ref->tls_ticket_enc_index; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7587 | |
| 7588 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7589 | 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] | 7590 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7591 | |
| 7592 | if (appctx->ctx.cli.i1 == 0) |
| 7593 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 7594 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7595 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7596 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7597 | struct chunk *t2 = get_trash_chunk(); |
| 7598 | |
| 7599 | chunk_reset(t2); |
| 7600 | /* 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] | 7601 | 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] | 7602 | sizeof(struct tls_sess_key), t2->str, t2->size); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7603 | 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] | 7604 | |
| 7605 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 7606 | /* let's try again later from this stream. We add ourselves into |
| 7607 | * this stream's users so that it can remove us upon termination. |
| 7608 | */ |
| 7609 | si_applet_cant_put(si); |
| 7610 | return 0; |
| 7611 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7612 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7613 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7614 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7615 | } |
| 7616 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 7617 | /* let's try again later from this stream. We add ourselves into |
| 7618 | * this stream's users so that it can remove us upon termination. |
| 7619 | */ |
| 7620 | si_applet_cant_put(si); |
| 7621 | return 0; |
| 7622 | } |
| 7623 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7624 | 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] | 7625 | break; |
| 7626 | |
| 7627 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7628 | 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] | 7629 | } |
| 7630 | |
| 7631 | appctx->st2 = STAT_ST_FIN; |
| 7632 | /* fall through */ |
| 7633 | |
| 7634 | default: |
| 7635 | appctx->st2 = STAT_ST_FIN; |
| 7636 | return 1; |
| 7637 | } |
| 7638 | return 0; |
| 7639 | } |
| 7640 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7641 | /* 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] | 7642 | static int cli_parse_show_tlskeys(char **args, struct appctx *appctx, void *private) |
| 7643 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7644 | /* no parameter, shows only file list */ |
| 7645 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7646 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7647 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 7648 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7649 | } |
| 7650 | |
| 7651 | if (args[2][0] == '*') { |
| 7652 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7653 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7654 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7655 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
| 7656 | if (!appctx->ctx.cli.p0) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7657 | 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] | 7658 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7659 | return 1; |
| 7660 | } |
| 7661 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7662 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 7663 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7664 | } |
| 7665 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7666 | static int cli_parse_set_tlskeys(char **args, struct appctx *appctx, void *private) |
| 7667 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7668 | struct tls_keys_ref *ref; |
| 7669 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7670 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 7671 | if (!*args[3] || !*args[4]) { |
| 7672 | 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] | 7673 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7674 | return 1; |
| 7675 | } |
| 7676 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7677 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 7678 | if (!ref) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7679 | 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] | 7680 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7681 | return 1; |
| 7682 | } |
| 7683 | |
| 7684 | trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size); |
| 7685 | if (trash.len != sizeof(struct tls_sess_key)) { |
| 7686 | 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] | 7687 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7688 | return 1; |
| 7689 | } |
| 7690 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7691 | memcpy(ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len); |
| 7692 | 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] | 7693 | |
| 7694 | appctx->ctx.cli.msg = "TLS ticket key updated!"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 7695 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7696 | return 1; |
| 7697 | |
| 7698 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7699 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7700 | |
| 7701 | static int cli_parse_set_ocspresponse(char **args, struct appctx *appctx, void *private) |
| 7702 | { |
| 7703 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 7704 | char *err = NULL; |
| 7705 | |
| 7706 | /* Expect one parameter: the new response in base64 encoding */ |
| 7707 | if (!*args[3]) { |
| 7708 | 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] | 7709 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7710 | return 1; |
| 7711 | } |
| 7712 | |
| 7713 | trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size); |
| 7714 | if (trash.len < 0) { |
| 7715 | 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] | 7716 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7717 | return 1; |
| 7718 | } |
| 7719 | |
| 7720 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
| 7721 | if (err) { |
| 7722 | memprintf(&err, "%s.\n", err); |
| 7723 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 7724 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7725 | } |
| 7726 | return 1; |
| 7727 | } |
| 7728 | appctx->ctx.cli.msg = "OCSP Response updated!"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 7729 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7730 | return 1; |
| 7731 | #else |
| 7732 | 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] | 7733 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7734 | return 1; |
| 7735 | #endif |
| 7736 | |
| 7737 | } |
| 7738 | |
| 7739 | /* register cli keywords */ |
| 7740 | static struct cli_kw_list cli_kws = {{ },{ |
| 7741 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7742 | { { "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 }, |
| 7743 | { { "set", "ssl", "tls-keys", NULL }, NULL, cli_parse_set_tlskeys, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7744 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7745 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7746 | { { NULL }, NULL, NULL, NULL } |
| 7747 | }}; |
| 7748 | |
| 7749 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7750 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7751 | * Please take care of keeping this list alphabetically sorted. |
| 7752 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 7753 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7754 | { "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] | 7755 | { "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] | 7756 | { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 7757 | { "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] | 7758 | { "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] | 7759 | { "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] | 7760 | { "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] | 7761 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 7762 | { "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] | 7763 | { "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] | 7764 | { "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] | 7765 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7766 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7767 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7768 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7769 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7770 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7771 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 7772 | { "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] | 7773 | { "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] | 7774 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 7775 | { "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] | 7776 | { "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] | 7777 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7778 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7779 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7780 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7781 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7782 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7783 | { "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] | 7784 | { "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] | 7785 | { "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] | 7786 | { "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] | 7787 | { "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] | 7788 | { "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] | 7789 | { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 7790 | { "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] | 7791 | { "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] | 7792 | #ifdef OPENSSL_NPN_NEGOTIATED |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7793 | { "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] | 7794 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7795 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7796 | { "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] | 7797 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7798 | { "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] | 7799 | { "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] | 7800 | { "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] | 7801 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 7802 | { "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] | 7803 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7804 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 7805 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7806 | { "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] | 7807 | { NULL, NULL, 0, 0, 0 }, |
| 7808 | }}; |
| 7809 | |
| 7810 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7811 | * Please take care of keeping this list alphabetically sorted. |
| 7812 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 7813 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 7814 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 7815 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 7816 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7817 | }}; |
| 7818 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7819 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7820 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 7821 | * all code contributors. |
| 7822 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 7823 | * the config parser can report an appropriate error when a known keyword was |
| 7824 | * not enabled. |
| 7825 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7826 | static struct ssl_bind_kw ssl_bind_kws[] = { |
| 7827 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 7828 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 7829 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 7830 | { "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] | 7831 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7832 | { "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] | 7833 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 7834 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 7835 | { NULL, NULL, 0 }, |
| 7836 | }; |
| 7837 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 7838 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7839 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 7840 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 7841 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 7842 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 7843 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 7844 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 7845 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 7846 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 7847 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 7848 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 7849 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 7850 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 7851 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 7852 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 7853 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 7854 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 7855 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7856 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
| 7857 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 7858 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 7859 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 7860 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 7861 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7862 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 7863 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7864 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 7865 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7866 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 7867 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 7868 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 7869 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 7870 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7871 | { NULL, NULL, 0 }, |
| 7872 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7873 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7874 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7875 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 7876 | * all code contributors. |
| 7877 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 7878 | * the config parser can report an appropriate error when a known keyword was |
| 7879 | * not enabled. |
| 7880 | */ |
| 7881 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7882 | { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */ |
| 7883 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 7884 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
| 7885 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 7886 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 7887 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 7888 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 7889 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 7890 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 7891 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 7892 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 7893 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 7894 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 7895 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 7896 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 7897 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 7898 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 7899 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 7900 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 7901 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 7902 | { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */ |
| 7903 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 7904 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 7905 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 7906 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 7907 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 7908 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 7909 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 7910 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 7911 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 7912 | { "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] | 7913 | { NULL, NULL, 0, 0 }, |
| 7914 | }}; |
| 7915 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7916 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 7917 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 7918 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 7919 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7920 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 7921 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 7922 | #ifndef OPENSSL_NO_DH |
| 7923 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 7924 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7925 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7926 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7927 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7928 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7929 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 7930 | #ifndef OPENSSL_NO_DH |
| 7931 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 7932 | #endif |
| 7933 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 7934 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 7935 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 7936 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7937 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 7938 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 7939 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7940 | { 0, NULL, NULL }, |
| 7941 | }}; |
| 7942 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 7943 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 7944 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7945 | .snd_buf = ssl_sock_from_buf, |
| 7946 | .rcv_buf = ssl_sock_to_buf, |
| 7947 | .rcv_pipe = NULL, |
| 7948 | .snd_pipe = NULL, |
| 7949 | .shutr = NULL, |
| 7950 | .shutw = ssl_sock_shutw, |
| 7951 | .close = ssl_sock_close, |
| 7952 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 7953 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 7954 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 7955 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 7956 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 7957 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7958 | }; |
| 7959 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 7960 | #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] | 7961 | |
| 7962 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7963 | { |
| 7964 | if (ptr) { |
| 7965 | chunk_destroy(ptr); |
| 7966 | free(ptr); |
| 7967 | } |
| 7968 | } |
| 7969 | |
| 7970 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7971 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7972 | { |
| 7973 | pool_free2(pool2_ssl_capture, ptr); |
| 7974 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 7975 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7976 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7977 | static void __ssl_sock_init(void) |
| 7978 | { |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7979 | char *ptr; |
| 7980 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7981 | STACK_OF(SSL_COMP)* cm; |
| 7982 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7983 | if (global_ssl.listen_default_ciphers) |
| 7984 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 7985 | if (global_ssl.connect_default_ciphers) |
| 7986 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 7987 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 7988 | xprt_register(XPRT_SSL, &ssl_sock); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7989 | SSL_library_init(); |
| 7990 | cm = SSL_COMP_get_compression_methods(); |
| 7991 | sk_SSL_COMP_zero(cm); |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 7992 | #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] | 7993 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 7994 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7995 | 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] | 7996 | sample_register_fetches(&sample_fetch_keywords); |
| 7997 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7998 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7999 | srv_register_keywords(&srv_kws); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8000 | cfg_register_keywords(&cfg_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8001 | cli_register_kw(&cli_kws); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8002 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8003 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8004 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8005 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 8006 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8007 | hap_register_post_check(tlskeys_finalize_config); |
| 8008 | #endif |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 8009 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8010 | ptr = NULL; |
| 8011 | memprintf(&ptr, "Built with OpenSSL version : " |
| 8012 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 8013 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8014 | #else /* OPENSSL_IS_BORINGSSL */ |
| 8015 | OPENSSL_VERSION_TEXT |
| 8016 | "\nRunning on OpenSSL version : %s%s", |
| 8017 | SSLeay_version(SSLEAY_VERSION), |
| 8018 | ((OPENSSL_VERSION_NUMBER ^ SSLeay()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
| 8019 | #endif |
| 8020 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
| 8021 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 8022 | "no (library version too old)" |
| 8023 | #elif defined(OPENSSL_NO_TLSEXT) |
| 8024 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 8025 | #else |
| 8026 | "yes" |
| 8027 | #endif |
| 8028 | "", ptr); |
| 8029 | |
| 8030 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 8031 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 8032 | "yes" |
| 8033 | #else |
| 8034 | #ifdef OPENSSL_NO_TLSEXT |
| 8035 | "no (because of OPENSSL_NO_TLSEXT)" |
| 8036 | #else |
| 8037 | "no (version might be too old, 0.9.8f min needed)" |
| 8038 | #endif |
| 8039 | #endif |
| 8040 | "", ptr); |
| 8041 | |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 8042 | memprintf(&ptr, "%s\nOpenSSL library supports : " |
| 8043 | #if SSL_OP_NO_SSLv3 |
| 8044 | "SSLv3 " |
| 8045 | #endif |
| 8046 | #if SSL_OP_NO_TLSv1 |
| 8047 | "TLSv1.0 " |
| 8048 | #endif |
| 8049 | #if SSL_OP_NO_TLSv1_1 |
| 8050 | "TLSv1.1 " |
| 8051 | #endif |
| 8052 | #if SSL_OP_NO_TLSv1_2 |
| 8053 | "TLSv1.2 " |
| 8054 | #endif |
| 8055 | #if SSL_OP_NO_TLSv1_3 |
| 8056 | "TLSv1.3" |
| 8057 | #endif |
| 8058 | "", ptr); |
| 8059 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8060 | hap_register_build_opts(ptr, 1); |
| 8061 | |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 8062 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 8063 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 8064 | |
| 8065 | #ifndef OPENSSL_NO_DH |
| 8066 | 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] | 8067 | hap_register_post_deinit(ssl_free_dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 8068 | #endif |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8069 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8070 | hap_register_post_deinit(ssl_free_engines); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8071 | #endif |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 8072 | /* Load SSL string for the verbose & debug mode. */ |
| 8073 | ERR_load_SSL_strings(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8074 | } |
| 8075 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8076 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8077 | void ssl_free_engines(void) { |
| 8078 | struct ssl_engine_list *wl, *wlb; |
| 8079 | /* free up engine list */ |
| 8080 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 8081 | ENGINE_finish(wl->e); |
| 8082 | ENGINE_free(wl->e); |
| 8083 | LIST_DEL(&wl->list); |
| 8084 | free(wl); |
| 8085 | } |
| 8086 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8087 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8088 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8089 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8090 | void ssl_free_dh(void) { |
| 8091 | if (local_dh_1024) { |
| 8092 | DH_free(local_dh_1024); |
| 8093 | local_dh_1024 = NULL; |
| 8094 | } |
| 8095 | if (local_dh_2048) { |
| 8096 | DH_free(local_dh_2048); |
| 8097 | local_dh_2048 = NULL; |
| 8098 | } |
| 8099 | if (local_dh_4096) { |
| 8100 | DH_free(local_dh_4096); |
| 8101 | local_dh_4096 = NULL; |
| 8102 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 8103 | if (global_dh) { |
| 8104 | DH_free(global_dh); |
| 8105 | global_dh = NULL; |
| 8106 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8107 | } |
| 8108 | #endif |
| 8109 | |
| 8110 | __attribute__((destructor)) |
| 8111 | static void __ssl_sock_deinit(void) |
| 8112 | { |
| 8113 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
| 8114 | lru64_destroy(ssl_ctx_lru_tree); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8115 | #endif |
| 8116 | |
| 8117 | ERR_remove_state(0); |
| 8118 | ERR_free_strings(); |
| 8119 | |
| 8120 | EVP_cleanup(); |
| 8121 | |
| 8122 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 8123 | CRYPTO_cleanup_all_ex_data(); |
| 8124 | #endif |
| 8125 | } |
| 8126 | |
| 8127 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8128 | /* |
| 8129 | * Local variables: |
| 8130 | * c-indent-level: 8 |
| 8131 | * c-basic-offset: 8 |
| 8132 | * End: |
| 8133 | */ |