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> |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 49 | #include <openssl/hmac.h> |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 50 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 51 | #include <openssl/ocsp.h> |
| 52 | #endif |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 53 | #ifndef OPENSSL_NO_DH |
| 54 | #include <openssl/dh.h> |
| 55 | #endif |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 56 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 57 | #include <openssl/engine.h> |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 58 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 59 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 60 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 61 | #include <openssl/async.h> |
| 62 | #endif |
| 63 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 64 | #include <import/lru.h> |
| 65 | #include <import/xxhash.h> |
| 66 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 67 | #include <common/buffer.h> |
| 68 | #include <common/compat.h> |
| 69 | #include <common/config.h> |
| 70 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 71 | #include <common/errors.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 72 | #include <common/standard.h> |
| 73 | #include <common/ticks.h> |
| 74 | #include <common/time.h> |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 75 | #include <common/cfgparse.h> |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 76 | #include <common/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 77 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 78 | #include <ebsttree.h> |
| 79 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 80 | #include <types/applet.h> |
| 81 | #include <types/cli.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 82 | #include <types/global.h> |
| 83 | #include <types/ssl_sock.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 84 | #include <types/stats.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 85 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 86 | #include <proto/acl.h> |
| 87 | #include <proto/arg.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 88 | #include <proto/channel.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 89 | #include <proto/connection.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 90 | #include <proto/cli.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 91 | #include <proto/fd.h> |
| 92 | #include <proto/freq_ctr.h> |
| 93 | #include <proto/frontend.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 94 | #include <proto/listener.h> |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 95 | #include <proto/openssl-compat.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 96 | #include <proto/pattern.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 97 | #include <proto/proto_tcp.h> |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 98 | #include <proto/proto_http.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 99 | #include <proto/server.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 100 | #include <proto/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 101 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 102 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 103 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 104 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 105 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 106 | #include <proto/task.h> |
| 107 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 108 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 109 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 110 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 111 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 112 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 113 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 114 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 115 | |
| 116 | /* Verify errors macros */ |
| 117 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 118 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 119 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 120 | |
| 121 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 122 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 123 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 124 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 125 | /* Supported hash function for TLS tickets */ |
| 126 | #ifdef OPENSSL_NO_SHA256 |
| 127 | #define HASH_FUNCT EVP_sha1 |
| 128 | #else |
| 129 | #define HASH_FUNCT EVP_sha256 |
| 130 | #endif /* OPENSSL_NO_SHA256 */ |
| 131 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 132 | /* ssl_methods flags for ssl options */ |
| 133 | #define MC_SSL_O_ALL 0x0000 |
| 134 | #define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */ |
| 135 | #define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */ |
| 136 | #define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */ |
| 137 | #define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 138 | #define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 139 | |
| 140 | /* ssl_methods versions */ |
| 141 | enum { |
| 142 | CONF_TLSV_NONE = 0, |
| 143 | CONF_TLSV_MIN = 1, |
| 144 | CONF_SSLV3 = 1, |
| 145 | CONF_TLSV10 = 2, |
| 146 | CONF_TLSV11 = 3, |
| 147 | CONF_TLSV12 = 4, |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 148 | CONF_TLSV13 = 5, |
| 149 | CONF_TLSV_MAX = 5, |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 150 | }; |
| 151 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 152 | /* server and bind verify method, it uses a global value as default */ |
| 153 | enum { |
| 154 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 155 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 156 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 157 | SSL_SOCK_VERIFY_NONE = 3, |
| 158 | }; |
| 159 | |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 160 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 161 | int sslconns = 0; |
| 162 | int totalsslconns = 0; |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 163 | static struct xprt_ops ssl_sock; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 164 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 165 | static struct { |
| 166 | char *crt_base; /* base directory path for certificates */ |
| 167 | char *ca_base; /* base directory path for CAs and CRLs */ |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 168 | int async; /* whether we use ssl async mode */ |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 169 | int default_early_data; /* Shall we default to allow early data */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 170 | |
| 171 | char *listen_default_ciphers; |
| 172 | char *connect_default_ciphers; |
| 173 | int listen_default_ssloptions; |
| 174 | int connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 175 | struct tls_version_filter listen_default_sslmethods; |
| 176 | struct tls_version_filter connect_default_sslmethods; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 177 | |
| 178 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 179 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 180 | unsigned int max_record; /* SSL max record size */ |
| 181 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 182 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 183 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 184 | } global_ssl = { |
| 185 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 186 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 187 | #endif |
| 188 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 189 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 190 | #endif |
| 191 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 192 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 193 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 194 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 195 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 196 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 197 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 198 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 199 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 200 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 201 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 202 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 203 | #endif |
| 204 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 205 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 206 | .capture_cipherlist = 0, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 207 | }; |
| 208 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 209 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 210 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 211 | unsigned long long int xxh64; |
| 212 | unsigned char ciphersuite_len; |
| 213 | char ciphersuite[0]; |
| 214 | }; |
| 215 | struct pool_head *pool2_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 216 | static int ssl_capture_ptr_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 217 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 218 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 219 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 220 | #endif |
| 221 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 222 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 223 | static unsigned int openssl_engines_initialized; |
| 224 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 225 | struct ssl_engine_list { |
| 226 | struct list list; |
| 227 | ENGINE *e; |
| 228 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 229 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 230 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 231 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 232 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 233 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 234 | static DH *local_dh_1024 = NULL; |
| 235 | static DH *local_dh_2048 = NULL; |
| 236 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 237 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 238 | #endif /* OPENSSL_NO_DH */ |
| 239 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 240 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 241 | /* X509V3 Extensions that will be added on generated certificates */ |
| 242 | #define X509V3_EXT_SIZE 5 |
| 243 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 244 | "basicConstraints", |
| 245 | "nsComment", |
| 246 | "subjectKeyIdentifier", |
| 247 | "authorityKeyIdentifier", |
| 248 | "keyUsage", |
| 249 | }; |
| 250 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 251 | "CA:FALSE", |
| 252 | "\"OpenSSL Generated Certificate\"", |
| 253 | "hash", |
| 254 | "keyid,issuer:always", |
| 255 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 256 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 257 | /* LRU cache to store generated certificate */ |
| 258 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 259 | static unsigned int ssl_ctx_lru_seed = 0; |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 260 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 261 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 262 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 263 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 264 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 265 | /* The order here matters for picking a default context, |
| 266 | * keep the most common keytype at the bottom of the list |
| 267 | */ |
| 268 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 269 | "dsa", |
| 270 | "ecdsa", |
| 271 | "rsa" |
| 272 | }; |
| 273 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 274 | #else |
| 275 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 276 | #endif |
| 277 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 278 | static struct shared_context *ssl_shctx; /* ssl shared session cache */ |
| 279 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 280 | |
| 281 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 282 | |
| 283 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 284 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 285 | |
| 286 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 287 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 288 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 289 | /* |
| 290 | * This function gives the detail of the SSL error. It is used only |
| 291 | * if the debug mode and the verbose mode are activated. It dump all |
| 292 | * the SSL error until the stack was empty. |
| 293 | */ |
| 294 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 295 | { |
| 296 | unsigned long ret; |
| 297 | |
| 298 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 299 | while(1) { |
| 300 | ret = ERR_get_error(); |
| 301 | if (ret == 0) |
| 302 | return; |
| 303 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 304 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 305 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 310 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 311 | /* |
| 312 | * struct alignment works here such that the key.key is the same as key_data |
| 313 | * Do not change the placement of key_data |
| 314 | */ |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 315 | struct certificate_ocsp { |
| 316 | struct ebmb_node key; |
| 317 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 318 | struct chunk response; |
| 319 | long expire; |
| 320 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 321 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 322 | struct ocsp_cbk_arg { |
| 323 | int is_single; |
| 324 | int single_kt; |
| 325 | union { |
| 326 | struct certificate_ocsp *s_ocsp; |
| 327 | /* |
| 328 | * m_ocsp will have multiple entries dependent on key type |
| 329 | * Entry 0 - DSA |
| 330 | * Entry 1 - ECDSA |
| 331 | * Entry 2 - RSA |
| 332 | */ |
| 333 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 334 | }; |
| 335 | }; |
| 336 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 337 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 338 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 339 | { |
| 340 | int err_code = ERR_ABORT; |
| 341 | ENGINE *engine; |
| 342 | struct ssl_engine_list *el; |
| 343 | |
| 344 | /* grab the structural reference to the engine */ |
| 345 | engine = ENGINE_by_id(engine_id); |
| 346 | if (engine == NULL) { |
| 347 | Alert("ssl-engine %s: failed to get structural reference\n", engine_id); |
| 348 | goto fail_get; |
| 349 | } |
| 350 | |
| 351 | if (!ENGINE_init(engine)) { |
| 352 | /* the engine couldn't initialise, release it */ |
| 353 | Alert("ssl-engine %s: failed to initialize\n", engine_id); |
| 354 | goto fail_init; |
| 355 | } |
| 356 | |
| 357 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
| 358 | Alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id); |
| 359 | goto fail_set_method; |
| 360 | } |
| 361 | |
| 362 | el = calloc(1, sizeof(*el)); |
| 363 | el->e = engine; |
| 364 | LIST_ADD(&openssl_engines, &el->list); |
| 365 | return 0; |
| 366 | |
| 367 | fail_set_method: |
| 368 | /* release the functional reference from ENGINE_init() */ |
| 369 | ENGINE_finish(engine); |
| 370 | |
| 371 | fail_init: |
| 372 | /* release the structural reference from ENGINE_by_id() */ |
| 373 | ENGINE_free(engine); |
| 374 | |
| 375 | fail_get: |
| 376 | return err_code; |
| 377 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 378 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 379 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 380 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 381 | /* |
| 382 | * openssl async fd handler |
| 383 | */ |
| 384 | static void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 385 | { |
| 386 | struct connection *conn = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 387 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 388 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 389 | * to poll this fd until it is requested |
| 390 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 391 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 392 | fd_cant_recv(fd); |
| 393 | |
| 394 | /* crypto engine is available, let's notify the associated |
| 395 | * connection that it can pursue its processing. |
| 396 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 397 | __conn_sock_want_recv(conn); |
| 398 | __conn_sock_want_send(conn); |
| 399 | conn_update_sock_polling(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 402 | /* |
| 403 | * openssl async delayed SSL_free handler |
| 404 | */ |
| 405 | static void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 406 | { |
| 407 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 408 | OSSL_ASYNC_FD all_fd[32]; |
| 409 | size_t num_all_fds = 0; |
| 410 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 411 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 412 | /* We suppose that the async job for a same SSL * |
| 413 | * are serialized. So if we are awake it is |
| 414 | * because the running job has just finished |
| 415 | * and we can remove all async fds safely |
| 416 | */ |
| 417 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 418 | if (num_all_fds > 32) { |
| 419 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 424 | for (i=0 ; i < num_all_fds ; i++) |
| 425 | fd_remove(all_fd[i]); |
| 426 | |
| 427 | /* 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] | 428 | SSL_free(ssl); |
| 429 | sslconns--; |
| 430 | jobs--; |
| 431 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 432 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 433 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 434 | * and enable/disable polling for async 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 | static void inline ssl_async_process_fds(struct connection *conn, SSL *ssl) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 437 | { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 438 | OSSL_ASYNC_FD add_fd[32], afd; |
| 439 | OSSL_ASYNC_FD del_fd[32]; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 440 | size_t num_add_fds = 0; |
| 441 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 442 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 443 | |
| 444 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 445 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 446 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 447 | 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] | 448 | return; |
| 449 | } |
| 450 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 451 | 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] | 452 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 453 | /* We remove unused fds from the fdtab */ |
| 454 | for (i=0 ; i < num_del_fds ; i++) |
| 455 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 456 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 457 | /* We add new fds to the fdtab */ |
| 458 | for (i=0 ; i < num_add_fds ; i++) { |
| 459 | afd = add_fd[i]; |
| 460 | fdtab[afd].owner = conn; |
| 461 | fdtab[afd].iocb = ssl_async_fd_handler; |
| 462 | fd_insert(afd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 465 | num_add_fds = 0; |
| 466 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 467 | if (num_add_fds > 32) { |
| 468 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 469 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 470 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 471 | |
| 472 | /* We activate the polling for all known async fds */ |
| 473 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 474 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 475 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 476 | /* To ensure that the fd cache won't be used |
| 477 | * We'll prefer to catch a real RD event |
| 478 | * because handling an EAGAIN on this fd will |
| 479 | * result in a context switch and also |
| 480 | * some engines uses a fd in blocking mode. |
| 481 | */ |
| 482 | fd_cant_recv(add_fd[i]); |
| 483 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 484 | |
| 485 | /* We must also prevent the conn_handler |
| 486 | * to be called until a read event was |
| 487 | * polled on an async fd |
| 488 | */ |
| 489 | __conn_sock_stop_both(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 490 | } |
| 491 | #endif |
| 492 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 493 | /* |
| 494 | * This function returns the number of seconds elapsed |
| 495 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 496 | * date presented un ASN1_GENERALIZEDTIME. |
| 497 | * |
| 498 | * In parsing error case, it returns -1. |
| 499 | */ |
| 500 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 501 | { |
| 502 | long epoch; |
| 503 | char *p, *end; |
| 504 | const unsigned short month_offset[12] = { |
| 505 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 506 | }; |
| 507 | int year, month; |
| 508 | |
| 509 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 510 | |
| 511 | p = (char *)d->data; |
| 512 | end = p + d->length; |
| 513 | |
| 514 | if (end - p < 4) return -1; |
| 515 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 516 | p += 4; |
| 517 | if (end - p < 2) return -1; |
| 518 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 519 | if (month < 1 || month > 12) return -1; |
| 520 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 521 | We consider leap years and the current month (<marsh or not) */ |
| 522 | epoch = ( ((year - 1970) * 365) |
| 523 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 524 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 525 | + month_offset[month-1] |
| 526 | ) * 24 * 60 * 60; |
| 527 | p += 2; |
| 528 | if (end - p < 2) return -1; |
| 529 | /* Add the number of seconds of completed days of current month */ |
| 530 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 531 | p += 2; |
| 532 | if (end - p < 2) return -1; |
| 533 | /* Add the completed hours of the current day */ |
| 534 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 535 | p += 2; |
| 536 | if (end - p < 2) return -1; |
| 537 | /* Add the completed minutes of the current hour */ |
| 538 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 539 | p += 2; |
| 540 | if (p == end) return -1; |
| 541 | /* Test if there is available seconds */ |
| 542 | if (p[0] < '0' || p[0] > '9') |
| 543 | goto nosec; |
| 544 | if (end - p < 2) return -1; |
| 545 | /* Add the seconds of the current minute */ |
| 546 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 547 | p += 2; |
| 548 | if (p == end) return -1; |
| 549 | /* Ignore seconds float part if present */ |
| 550 | if (p[0] == '.') { |
| 551 | do { |
| 552 | if (++p == end) return -1; |
| 553 | } while (p[0] >= '0' && p[0] <= '9'); |
| 554 | } |
| 555 | |
| 556 | nosec: |
| 557 | if (p[0] == 'Z') { |
| 558 | if (end - p != 1) return -1; |
| 559 | return epoch; |
| 560 | } |
| 561 | else if (p[0] == '+') { |
| 562 | if (end - p != 5) return -1; |
| 563 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 564 | return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 565 | } |
| 566 | else if (p[0] == '-') { |
| 567 | if (end - p != 5) return -1; |
| 568 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 569 | return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | return -1; |
| 573 | } |
| 574 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 575 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 576 | |
| 577 | /* This function starts to check if the OCSP response (in DER format) contained |
| 578 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 579 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 580 | * contained in the OCSP Response and exits on error if no match. |
| 581 | * If it's a valid OCSP Response: |
| 582 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 583 | * pointed by 'ocsp'. |
| 584 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 585 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 586 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 587 | * already present in the container, it will be overwritten. |
| 588 | * |
| 589 | * Note: OCSP response containing more than one OCSP Single response is not |
| 590 | * considered valid. |
| 591 | * |
| 592 | * Returns 0 on success, 1 in error case. |
| 593 | */ |
| 594 | static int ssl_sock_load_ocsp_response(struct chunk *ocsp_response, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 595 | { |
| 596 | OCSP_RESPONSE *resp; |
| 597 | OCSP_BASICRESP *bs = NULL; |
| 598 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 599 | OCSP_CERTID *id; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 600 | unsigned char *p = (unsigned char *)ocsp_response->str; |
| 601 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 602 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 603 | int reason; |
| 604 | int ret = 1; |
| 605 | |
| 606 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, ocsp_response->len); |
| 607 | if (!resp) { |
| 608 | memprintf(err, "Unable to parse OCSP response"); |
| 609 | goto out; |
| 610 | } |
| 611 | |
| 612 | rc = OCSP_response_status(resp); |
| 613 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 614 | memprintf(err, "OCSP response status not successful"); |
| 615 | goto out; |
| 616 | } |
| 617 | |
| 618 | bs = OCSP_response_get1_basic(resp); |
| 619 | if (!bs) { |
| 620 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 621 | goto out; |
| 622 | } |
| 623 | |
| 624 | count_sr = OCSP_resp_count(bs); |
| 625 | if (count_sr > 1) { |
| 626 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 627 | goto out; |
| 628 | } |
| 629 | |
| 630 | sr = OCSP_resp_get0(bs, 0); |
| 631 | if (!sr) { |
| 632 | memprintf(err, "Failed to get OCSP single response"); |
| 633 | goto out; |
| 634 | } |
| 635 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 636 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 637 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 638 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 639 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 640 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 641 | goto out; |
| 642 | } |
| 643 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 644 | if (!nextupd) { |
| 645 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 646 | goto out; |
| 647 | } |
| 648 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 649 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 650 | if (!rc) { |
| 651 | memprintf(err, "OCSP single response: no longer valid."); |
| 652 | goto out; |
| 653 | } |
| 654 | |
| 655 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 656 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 657 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 658 | goto out; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | if (!ocsp) { |
| 663 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 664 | unsigned char *p; |
| 665 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 666 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 667 | if (!rc) { |
| 668 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 669 | goto out; |
| 670 | } |
| 671 | |
| 672 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 673 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 674 | goto out; |
| 675 | } |
| 676 | |
| 677 | p = key; |
| 678 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 679 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 680 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 681 | if (!ocsp) { |
| 682 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 683 | goto out; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /* According to comments on "chunk_dup", the |
| 688 | previous chunk buffer will be freed */ |
| 689 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 690 | memprintf(err, "OCSP response: Memory allocation error"); |
| 691 | goto out; |
| 692 | } |
| 693 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 694 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 695 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 696 | ret = 0; |
| 697 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 698 | ERR_clear_error(); |
| 699 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 700 | if (bs) |
| 701 | OCSP_BASICRESP_free(bs); |
| 702 | |
| 703 | if (resp) |
| 704 | OCSP_RESPONSE_free(resp); |
| 705 | |
| 706 | return ret; |
| 707 | } |
| 708 | /* |
| 709 | * External function use to update the OCSP response in the OCSP response's |
| 710 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 711 | * to update in DER format. |
| 712 | * |
| 713 | * Returns 0 on success, 1 in error case. |
| 714 | */ |
| 715 | int ssl_sock_update_ocsp_response(struct chunk *ocsp_response, char **err) |
| 716 | { |
| 717 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * This function load the OCSP Resonse in DER format contained in file at |
| 722 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 723 | * |
| 724 | * Returns 0 on success, 1 in error case. |
| 725 | */ |
| 726 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 727 | { |
| 728 | int fd = -1; |
| 729 | int r = 0; |
| 730 | int ret = 1; |
| 731 | |
| 732 | fd = open(ocsp_path, O_RDONLY); |
| 733 | if (fd == -1) { |
| 734 | memprintf(err, "Error opening OCSP response file"); |
| 735 | goto end; |
| 736 | } |
| 737 | |
| 738 | trash.len = 0; |
| 739 | while (trash.len < trash.size) { |
| 740 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 741 | if (r < 0) { |
| 742 | if (errno == EINTR) |
| 743 | continue; |
| 744 | |
| 745 | memprintf(err, "Error reading OCSP response from file"); |
| 746 | goto end; |
| 747 | } |
| 748 | else if (r == 0) { |
| 749 | break; |
| 750 | } |
| 751 | trash.len += r; |
| 752 | } |
| 753 | |
| 754 | close(fd); |
| 755 | fd = -1; |
| 756 | |
| 757 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 758 | end: |
| 759 | if (fd != -1) |
| 760 | close(fd); |
| 761 | |
| 762 | return ret; |
| 763 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 764 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 765 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 766 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 767 | 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) |
| 768 | { |
| 769 | struct tls_sess_key *keys; |
| 770 | struct connection *conn; |
| 771 | int head; |
| 772 | int i; |
| 773 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 774 | conn = SSL_get_app_data(s); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 775 | keys = objt_listener(conn->target)->bind_conf->keys_ref->tlskeys; |
| 776 | 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] | 777 | |
| 778 | if (enc) { |
| 779 | memcpy(key_name, keys[head].name, 16); |
| 780 | |
| 781 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
| 782 | return -1; |
| 783 | |
| 784 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].aes_key, iv)) |
| 785 | return -1; |
| 786 | |
| 787 | HMAC_Init_ex(hctx, keys[head].hmac_key, 16, HASH_FUNCT(), NULL); |
| 788 | |
| 789 | return 1; |
| 790 | } else { |
| 791 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 792 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 793 | goto found; |
| 794 | } |
| 795 | return 0; |
| 796 | |
| 797 | found: |
| 798 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].hmac_key, 16, HASH_FUNCT(), NULL); |
| 799 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].aes_key, iv)) |
| 800 | return -1; |
| 801 | /* 2 for key renewal, 1 if current key is still valid */ |
| 802 | return i ? 2 : 1; |
| 803 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 807 | { |
| 808 | struct tls_keys_ref *ref; |
| 809 | |
| 810 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 811 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 812 | return ref; |
| 813 | return NULL; |
| 814 | } |
| 815 | |
| 816 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 817 | { |
| 818 | struct tls_keys_ref *ref; |
| 819 | |
| 820 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 821 | if (ref->unique_id == unique_id) |
| 822 | return ref; |
| 823 | return NULL; |
| 824 | } |
| 825 | |
| 826 | int ssl_sock_update_tlskey(char *filename, struct chunk *tlskey, char **err) { |
| 827 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 828 | |
| 829 | if(!ref) { |
| 830 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 831 | return 1; |
| 832 | } |
| 833 | |
Pradeep Jindal | cc79b00 | 2015-08-20 18:25:17 +0530 | [diff] [blame] | 834 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), tlskey->str, tlskey->len); |
| 835 | 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] | 836 | |
| 837 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 838 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 839 | |
| 840 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 841 | * automatic ids. It's called just after the basic checks. It returns |
| 842 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 843 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 844 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 845 | { |
| 846 | int i = 0; |
| 847 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 848 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 849 | |
| 850 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 851 | if (ref->unique_id == -1) { |
| 852 | /* Look for the first free id. */ |
| 853 | while (1) { |
| 854 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 855 | if (ref2->unique_id == i) { |
| 856 | i++; |
| 857 | break; |
| 858 | } |
| 859 | } |
| 860 | if (&ref2->list == &tlskeys_reference) |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | /* Uses the unique id and increment it for the next entry. */ |
| 865 | ref->unique_id = i; |
| 866 | i++; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | /* This sort the reference list by id. */ |
| 871 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 872 | LIST_DEL(&ref->list); |
| 873 | list_for_each_entry(ref3, &tkr, list) { |
| 874 | if (ref->unique_id < ref3->unique_id) { |
| 875 | LIST_ADDQ(&ref3->list, &ref->list); |
| 876 | break; |
| 877 | } |
| 878 | } |
| 879 | if (&ref3->list == &tkr) |
| 880 | LIST_ADDQ(&tkr, &ref->list); |
| 881 | } |
| 882 | |
| 883 | /* swap root */ |
| 884 | LIST_ADD(&tkr, &tlskeys_reference); |
| 885 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 886 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 887 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 888 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 889 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 890 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 891 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 892 | { |
| 893 | switch (evp_keytype) { |
| 894 | case EVP_PKEY_RSA: |
| 895 | return 2; |
| 896 | case EVP_PKEY_DSA: |
| 897 | return 0; |
| 898 | case EVP_PKEY_EC: |
| 899 | return 1; |
| 900 | } |
| 901 | |
| 902 | return -1; |
| 903 | } |
| 904 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 905 | /* |
| 906 | * Callback used to set OCSP status extension content in server hello. |
| 907 | */ |
| 908 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 909 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 910 | struct certificate_ocsp *ocsp; |
| 911 | struct ocsp_cbk_arg *ocsp_arg; |
| 912 | char *ssl_buf; |
| 913 | EVP_PKEY *ssl_pkey; |
| 914 | int key_type; |
| 915 | int index; |
| 916 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 917 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 918 | |
| 919 | ssl_pkey = SSL_get_privatekey(ssl); |
| 920 | if (!ssl_pkey) |
| 921 | return SSL_TLSEXT_ERR_NOACK; |
| 922 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 923 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 924 | |
| 925 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 926 | ocsp = ocsp_arg->s_ocsp; |
| 927 | else { |
| 928 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 929 | * the certificate type |
| 930 | */ |
| 931 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 932 | |
| 933 | if (index < 0) |
| 934 | return SSL_TLSEXT_ERR_NOACK; |
| 935 | |
| 936 | ocsp = ocsp_arg->m_ocsp[index]; |
| 937 | |
| 938 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 939 | |
| 940 | if (!ocsp || |
| 941 | !ocsp->response.str || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 942 | !ocsp->response.len || |
| 943 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 944 | return SSL_TLSEXT_ERR_NOACK; |
| 945 | |
| 946 | ssl_buf = OPENSSL_malloc(ocsp->response.len); |
| 947 | if (!ssl_buf) |
| 948 | return SSL_TLSEXT_ERR_NOACK; |
| 949 | |
| 950 | memcpy(ssl_buf, ocsp->response.str, ocsp->response.len); |
| 951 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.len); |
| 952 | |
| 953 | return SSL_TLSEXT_ERR_OK; |
| 954 | } |
| 955 | |
| 956 | /* |
| 957 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 958 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 959 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 960 | * It should be present in the certificate's extra chain builded from file |
| 961 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 962 | * named 'cert_path' suffixed using '.issuer'. |
| 963 | * |
| 964 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 965 | * response. If file is empty or content is not a valid OCSP response, |
| 966 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 967 | * is displayed). |
| 968 | * |
| 969 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
| 970 | * succesfully enabled, or -1 in other error case. |
| 971 | */ |
| 972 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 973 | { |
| 974 | |
| 975 | BIO *in = NULL; |
| 976 | X509 *x, *xi = NULL, *issuer = NULL; |
| 977 | STACK_OF(X509) *chain = NULL; |
| 978 | OCSP_CERTID *cid = NULL; |
| 979 | SSL *ssl; |
| 980 | char ocsp_path[MAXPATHLEN+1]; |
| 981 | int i, ret = -1; |
| 982 | struct stat st; |
| 983 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 984 | char *warn = NULL; |
| 985 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 986 | pem_password_cb *passwd_cb; |
| 987 | void *passwd_cb_userdata; |
| 988 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 989 | |
| 990 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 991 | |
| 992 | if (stat(ocsp_path, &st)) |
| 993 | return 1; |
| 994 | |
| 995 | ssl = SSL_new(ctx); |
| 996 | if (!ssl) |
| 997 | goto out; |
| 998 | |
| 999 | x = SSL_get_certificate(ssl); |
| 1000 | if (!x) |
| 1001 | goto out; |
| 1002 | |
| 1003 | /* Try to lookup for issuer in certificate extra chain */ |
| 1004 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 1005 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 1006 | #else |
| 1007 | chain = ctx->extra_certs; |
| 1008 | #endif |
| 1009 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1010 | issuer = sk_X509_value(chain, i); |
| 1011 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 1012 | break; |
| 1013 | else |
| 1014 | issuer = NULL; |
| 1015 | } |
| 1016 | |
| 1017 | /* If not found try to load issuer from a suffixed file */ |
| 1018 | if (!issuer) { |
| 1019 | char issuer_path[MAXPATHLEN+1]; |
| 1020 | |
| 1021 | in = BIO_new(BIO_s_file()); |
| 1022 | if (!in) |
| 1023 | goto out; |
| 1024 | |
| 1025 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 1026 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 1027 | goto out; |
| 1028 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1029 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 1030 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 1031 | |
| 1032 | 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] | 1033 | if (!xi) |
| 1034 | goto out; |
| 1035 | |
| 1036 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 1037 | goto out; |
| 1038 | |
| 1039 | issuer = xi; |
| 1040 | } |
| 1041 | |
| 1042 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1043 | if (!cid) |
| 1044 | goto out; |
| 1045 | |
| 1046 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1047 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1048 | goto out; |
| 1049 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1050 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1051 | if (!ocsp) |
| 1052 | goto out; |
| 1053 | |
| 1054 | p = ocsp->key_data; |
| 1055 | i2d_OCSP_CERTID(cid, &p); |
| 1056 | |
| 1057 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1058 | if (iocsp == ocsp) |
| 1059 | ocsp = NULL; |
| 1060 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1061 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1062 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1063 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1064 | #endif |
| 1065 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1066 | |
| 1067 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1068 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1069 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1070 | |
| 1071 | cb_arg->is_single = 1; |
| 1072 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1073 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1074 | pkey = X509_get_pubkey(x); |
| 1075 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1076 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1077 | |
| 1078 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1079 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1080 | } else { |
| 1081 | /* |
| 1082 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1083 | * Update that cb_arg with the new cert's staple |
| 1084 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1085 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1086 | struct certificate_ocsp *tmp_ocsp; |
| 1087 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1088 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1089 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1090 | |
| 1091 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1092 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1093 | #else |
| 1094 | cb_arg = ctx->tlsext_status_arg; |
| 1095 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1096 | |
| 1097 | /* |
| 1098 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1099 | * the order of operations below matter, take care when changing it |
| 1100 | */ |
| 1101 | tmp_ocsp = cb_arg->s_ocsp; |
| 1102 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1103 | cb_arg->s_ocsp = NULL; |
| 1104 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1105 | cb_arg->is_single = 0; |
| 1106 | cb_arg->single_kt = 0; |
| 1107 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1108 | pkey = X509_get_pubkey(x); |
| 1109 | key_type = EVP_PKEY_base_id(pkey); |
| 1110 | EVP_PKEY_free(pkey); |
| 1111 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1112 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1113 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1114 | cb_arg->m_ocsp[index] = iocsp; |
| 1115 | |
| 1116 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1117 | |
| 1118 | ret = 0; |
| 1119 | |
| 1120 | warn = NULL; |
| 1121 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 1122 | memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure"); |
| 1123 | Warning("%s.\n", warn); |
| 1124 | } |
| 1125 | |
| 1126 | out: |
| 1127 | if (ssl) |
| 1128 | SSL_free(ssl); |
| 1129 | |
| 1130 | if (in) |
| 1131 | BIO_free(in); |
| 1132 | |
| 1133 | if (xi) |
| 1134 | X509_free(xi); |
| 1135 | |
| 1136 | if (cid) |
| 1137 | OCSP_CERTID_free(cid); |
| 1138 | |
| 1139 | if (ocsp) |
| 1140 | free(ocsp); |
| 1141 | |
| 1142 | if (warn) |
| 1143 | free(warn); |
| 1144 | |
| 1145 | |
| 1146 | return ret; |
| 1147 | } |
| 1148 | |
| 1149 | #endif |
| 1150 | |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1151 | #ifdef OPENSSL_IS_BORINGSSL |
| 1152 | static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path) |
| 1153 | { |
| 1154 | char ocsp_path[MAXPATHLEN+1]; |
| 1155 | struct stat st; |
| 1156 | int fd = -1, r = 0; |
| 1157 | |
| 1158 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1159 | if (stat(ocsp_path, &st)) |
| 1160 | return 0; |
| 1161 | |
| 1162 | fd = open(ocsp_path, O_RDONLY); |
| 1163 | if (fd == -1) { |
| 1164 | Warning("Error opening OCSP response file %s.\n", ocsp_path); |
| 1165 | return -1; |
| 1166 | } |
| 1167 | |
| 1168 | trash.len = 0; |
| 1169 | while (trash.len < trash.size) { |
| 1170 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 1171 | if (r < 0) { |
| 1172 | if (errno == EINTR) |
| 1173 | continue; |
| 1174 | Warning("Error reading OCSP response from file %s.\n", ocsp_path); |
| 1175 | close(fd); |
| 1176 | return -1; |
| 1177 | } |
| 1178 | else if (r == 0) { |
| 1179 | break; |
| 1180 | } |
| 1181 | trash.len += r; |
| 1182 | } |
| 1183 | close(fd); |
| 1184 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)trash.str, trash.len); |
| 1185 | } |
| 1186 | #endif |
| 1187 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 1188 | #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] | 1189 | |
| 1190 | #define CT_EXTENSION_TYPE 18 |
| 1191 | |
| 1192 | static int sctl_ex_index = -1; |
| 1193 | |
| 1194 | /* |
| 1195 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1196 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1197 | * is performed. |
| 1198 | */ |
| 1199 | static int ssl_sock_parse_sctl(struct chunk *sctl) |
| 1200 | { |
| 1201 | int ret = 1; |
| 1202 | int len, pos, sct_len; |
| 1203 | unsigned char *data; |
| 1204 | |
| 1205 | if (sctl->len < 2) |
| 1206 | goto out; |
| 1207 | |
| 1208 | data = (unsigned char *)sctl->str; |
| 1209 | len = (data[0] << 8) | data[1]; |
| 1210 | |
| 1211 | if (len + 2 != sctl->len) |
| 1212 | goto out; |
| 1213 | |
| 1214 | data = data + 2; |
| 1215 | pos = 0; |
| 1216 | while (pos < len) { |
| 1217 | if (len - pos < 2) |
| 1218 | goto out; |
| 1219 | |
| 1220 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1221 | if (pos + sct_len + 2 > len) |
| 1222 | goto out; |
| 1223 | |
| 1224 | pos += sct_len + 2; |
| 1225 | } |
| 1226 | |
| 1227 | ret = 0; |
| 1228 | |
| 1229 | out: |
| 1230 | return ret; |
| 1231 | } |
| 1232 | |
| 1233 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, struct chunk **sctl) |
| 1234 | { |
| 1235 | int fd = -1; |
| 1236 | int r = 0; |
| 1237 | int ret = 1; |
| 1238 | |
| 1239 | *sctl = NULL; |
| 1240 | |
| 1241 | fd = open(sctl_path, O_RDONLY); |
| 1242 | if (fd == -1) |
| 1243 | goto end; |
| 1244 | |
| 1245 | trash.len = 0; |
| 1246 | while (trash.len < trash.size) { |
| 1247 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 1248 | if (r < 0) { |
| 1249 | if (errno == EINTR) |
| 1250 | continue; |
| 1251 | |
| 1252 | goto end; |
| 1253 | } |
| 1254 | else if (r == 0) { |
| 1255 | break; |
| 1256 | } |
| 1257 | trash.len += r; |
| 1258 | } |
| 1259 | |
| 1260 | ret = ssl_sock_parse_sctl(&trash); |
| 1261 | if (ret) |
| 1262 | goto end; |
| 1263 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1264 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1265 | if (!chunk_dup(*sctl, &trash)) { |
| 1266 | free(*sctl); |
| 1267 | *sctl = NULL; |
| 1268 | goto end; |
| 1269 | } |
| 1270 | |
| 1271 | end: |
| 1272 | if (fd != -1) |
| 1273 | close(fd); |
| 1274 | |
| 1275 | return ret; |
| 1276 | } |
| 1277 | |
| 1278 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1279 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1280 | struct chunk *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1281 | |
| 1282 | *out = (unsigned char *)sctl->str; |
| 1283 | *outlen = sctl->len; |
| 1284 | |
| 1285 | return 1; |
| 1286 | } |
| 1287 | |
| 1288 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1289 | { |
| 1290 | return 1; |
| 1291 | } |
| 1292 | |
| 1293 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 1294 | { |
| 1295 | char sctl_path[MAXPATHLEN+1]; |
| 1296 | int ret = -1; |
| 1297 | struct stat st; |
| 1298 | struct chunk *sctl = NULL; |
| 1299 | |
| 1300 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 1301 | |
| 1302 | if (stat(sctl_path, &st)) |
| 1303 | return 1; |
| 1304 | |
| 1305 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 1306 | goto out; |
| 1307 | |
| 1308 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 1309 | free(sctl); |
| 1310 | goto out; |
| 1311 | } |
| 1312 | |
| 1313 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1314 | |
| 1315 | ret = 0; |
| 1316 | |
| 1317 | out: |
| 1318 | return ret; |
| 1319 | } |
| 1320 | |
| 1321 | #endif |
| 1322 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1323 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1324 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1325 | struct connection *conn = SSL_get_app_data(ssl); |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1326 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1327 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1328 | |
| 1329 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1330 | /* Disable renegotiation (CVE-2009-3555) */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 1331 | if ((conn->flags & (CO_FL_CONNECTED | CO_FL_EARLY_SSL_HS)) == CO_FL_CONNECTED) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1332 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1333 | conn->err_code = CO_ER_SSL_RENEG; |
| 1334 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1335 | } |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1336 | |
| 1337 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 1338 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 1339 | /* Long certificate chains optimz |
| 1340 | If write and read bios are differents, we |
| 1341 | consider that the buffering was activated, |
| 1342 | so we rise the output buffer size from 4k |
| 1343 | to 16k */ |
| 1344 | write_bio = SSL_get_wbio(ssl); |
| 1345 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1346 | BIO_set_write_buffer_size(write_bio, 16384); |
| 1347 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 1348 | } |
| 1349 | } |
| 1350 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1351 | } |
| 1352 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1353 | /* Callback is called for each certificate of the chain during a verify |
| 1354 | ok is set to 1 if preverify detect no error on current certificate. |
| 1355 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1356 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1357 | { |
| 1358 | SSL *ssl; |
| 1359 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1360 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1361 | |
| 1362 | 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] | 1363 | conn = SSL_get_app_data(ssl); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1364 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1365 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1366 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1367 | if (ok) /* no errors */ |
| 1368 | return ok; |
| 1369 | |
| 1370 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1371 | err = X509_STORE_CTX_get_error(x_store); |
| 1372 | |
| 1373 | /* check if CA error needs to be ignored */ |
| 1374 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1375 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 1376 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1377 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1378 | } |
| 1379 | |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1380 | 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] | 1381 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1382 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1383 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1384 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1385 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1386 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1387 | return 0; |
| 1388 | } |
| 1389 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1390 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 1391 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1392 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1393 | /* check if certificate error needs to be ignored */ |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1394 | 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] | 1395 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1396 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1397 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1398 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1399 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1400 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1401 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1402 | } |
| 1403 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1404 | static inline |
| 1405 | 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] | 1406 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1407 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1408 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1409 | unsigned char *msg; |
| 1410 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1411 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1412 | |
| 1413 | /* This function is called for "from client" and "to server" |
| 1414 | * connections. The combination of write_p == 0 and content_type == 22 |
| 1415 | * is only avalaible during "from client" connection. |
| 1416 | */ |
| 1417 | |
| 1418 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1419 | * otherwise it is set to 1. |
| 1420 | */ |
| 1421 | if (write_p != 0) |
| 1422 | return; |
| 1423 | |
| 1424 | /* content_type contains the type of message received or sent |
| 1425 | * according with the SSL/TLS protocol spec. This message is |
| 1426 | * encoded with one byte. The value 256 (two bytes) is used |
| 1427 | * for designing the SSL/TLS record layer. According with the |
| 1428 | * rfc6101, the expected message (other than 256) are: |
| 1429 | * - change_cipher_spec(20) |
| 1430 | * - alert(21) |
| 1431 | * - handshake(22) |
| 1432 | * - application_data(23) |
| 1433 | * - (255) |
| 1434 | * We are interessed by the handshake and specially the client |
| 1435 | * hello. |
| 1436 | */ |
| 1437 | if (content_type != 22) |
| 1438 | return; |
| 1439 | |
| 1440 | /* The message length is at least 4 bytes, containing the |
| 1441 | * message type and the message length. |
| 1442 | */ |
| 1443 | if (len < 4) |
| 1444 | return; |
| 1445 | |
| 1446 | /* First byte of the handshake message id the type of |
| 1447 | * message. The konwn types are: |
| 1448 | * - hello_request(0) |
| 1449 | * - client_hello(1) |
| 1450 | * - server_hello(2) |
| 1451 | * - certificate(11) |
| 1452 | * - server_key_exchange (12) |
| 1453 | * - certificate_request(13) |
| 1454 | * - server_hello_done(14) |
| 1455 | * We are interested by the client hello. |
| 1456 | */ |
| 1457 | msg = (unsigned char *)buf; |
| 1458 | if (msg[0] != 1) |
| 1459 | return; |
| 1460 | |
| 1461 | /* Next three bytes are the length of the message. The total length |
| 1462 | * must be this decoded length + 4. If the length given as argument |
| 1463 | * is not the same, we abort the protocol dissector. |
| 1464 | */ |
| 1465 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1466 | if (len < rec_len + 4) |
| 1467 | return; |
| 1468 | msg += 4; |
| 1469 | end = msg + rec_len; |
| 1470 | if (end < msg) |
| 1471 | return; |
| 1472 | |
| 1473 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1474 | * for minor, the random, composed by 4 bytes for the unix time and |
| 1475 | * 28 bytes for unix payload, and them 1 byte for the session id. So |
| 1476 | * we jump 1 + 1 + 4 + 28 + 1 bytes. |
| 1477 | */ |
| 1478 | msg += 1 + 1 + 4 + 28 + 1; |
| 1479 | if (msg > end) |
| 1480 | return; |
| 1481 | |
| 1482 | /* Next two bytes are the ciphersuite length. */ |
| 1483 | if (msg + 2 > end) |
| 1484 | return; |
| 1485 | rec_len = (msg[0] << 8) + msg[1]; |
| 1486 | msg += 2; |
| 1487 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1488 | return; |
| 1489 | |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1490 | capture = pool_alloc_dirty(pool2_ssl_capture); |
| 1491 | if (!capture) |
| 1492 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1493 | /* Compute the xxh64 of the ciphersuite. */ |
| 1494 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1495 | |
| 1496 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1497 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1498 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1499 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1500 | |
| 1501 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1502 | } |
| 1503 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1504 | /* Callback is called for ssl protocol analyse */ |
| 1505 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1506 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1507 | #ifdef TLS1_RT_HEARTBEAT |
| 1508 | /* test heartbeat received (write_p is set to 0 |
| 1509 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1510 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1511 | struct connection *conn = SSL_get_app_data(ssl); |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1512 | const unsigned char *p = buf; |
| 1513 | unsigned int payload; |
| 1514 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1515 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1516 | |
| 1517 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1518 | if (*p != TLS1_HB_REQUEST) |
| 1519 | return; |
| 1520 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1521 | 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] | 1522 | goto kill_it; |
| 1523 | |
| 1524 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1525 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1526 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1527 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1528 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1529 | * advertised payload is larger than the advertised packet |
| 1530 | * length, so we have garbage in the buffer between the |
| 1531 | * payload and the end of the buffer (p+len). We can't know |
| 1532 | * if the SSL stack is patched, and we don't know if we can |
| 1533 | * safely wipe out the area between p+3+len and payload. |
| 1534 | * So instead, we prevent the response from being sent by |
| 1535 | * setting the max_send_fragment to 0 and we report an SSL |
| 1536 | * error, which will kill this connection. It will be reported |
| 1537 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1538 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1539 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1540 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1541 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1542 | return; |
| 1543 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1544 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1545 | if (global_ssl.capture_cipherlist > 0) |
| 1546 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1547 | } |
| 1548 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1549 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 1550 | /* This callback is used so that the server advertises the list of |
| 1551 | * negociable protocols for NPN. |
| 1552 | */ |
| 1553 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1554 | unsigned int *len, void *arg) |
| 1555 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1556 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1557 | |
| 1558 | *data = (const unsigned char *)conf->npn_str; |
| 1559 | *len = conf->npn_len; |
| 1560 | return SSL_TLSEXT_ERR_OK; |
| 1561 | } |
| 1562 | #endif |
| 1563 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1564 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1565 | /* This callback is used so that the server advertises the list of |
| 1566 | * negociable protocols for ALPN. |
| 1567 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1568 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1569 | unsigned char *outlen, |
| 1570 | const unsigned char *server, |
| 1571 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1572 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1573 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1574 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1575 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1576 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1577 | return SSL_TLSEXT_ERR_NOACK; |
| 1578 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1579 | return SSL_TLSEXT_ERR_OK; |
| 1580 | } |
| 1581 | #endif |
| 1582 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1583 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1584 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1585 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1586 | /* Create a X509 certificate with the specified servername and serial. This |
| 1587 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1588 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1589 | 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] | 1590 | { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1591 | static unsigned int serial = 0; |
| 1592 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1593 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1594 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1595 | SSL_CTX *ssl_ctx = NULL; |
| 1596 | X509 *newcrt = NULL; |
| 1597 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1598 | SSL *tmp_ssl = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1599 | X509_NAME *name; |
| 1600 | const EVP_MD *digest; |
| 1601 | X509V3_CTX ctx; |
| 1602 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1603 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1604 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1605 | /* Get the private key of the default certificate and use it */ |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1606 | #if (OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined LIBRESSL_VERSION_NUMBER) |
| 1607 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1608 | #else |
| 1609 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1610 | if (tmp_ssl) |
| 1611 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1612 | #endif |
| 1613 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1614 | goto mkcert_error; |
| 1615 | |
| 1616 | /* Create the certificate */ |
| 1617 | if (!(newcrt = X509_new())) |
| 1618 | goto mkcert_error; |
| 1619 | |
| 1620 | /* Set version number for the certificate (X509v3) and the serial |
| 1621 | * number */ |
| 1622 | if (X509_set_version(newcrt, 2L) != 1) |
| 1623 | goto mkcert_error; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1624 | if (!serial) |
| 1625 | serial = now_ms; |
| 1626 | ASN1_INTEGER_set(X509_get_serialNumber(newcrt), serial++); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1627 | |
| 1628 | /* Set duration for the certificate */ |
| 1629 | if (!X509_gmtime_adj(X509_get_notBefore(newcrt), (long)-60*60*24) || |
| 1630 | !X509_gmtime_adj(X509_get_notAfter(newcrt),(long)60*60*24*365)) |
| 1631 | goto mkcert_error; |
| 1632 | |
| 1633 | /* set public key in the certificate */ |
| 1634 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1635 | goto mkcert_error; |
| 1636 | |
| 1637 | /* Set issuer name from the CA */ |
| 1638 | if (!(name = X509_get_subject_name(cacert))) |
| 1639 | goto mkcert_error; |
| 1640 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1641 | goto mkcert_error; |
| 1642 | |
| 1643 | /* Set the subject name using the same, but the CN */ |
| 1644 | name = X509_NAME_dup(name); |
| 1645 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1646 | (const unsigned char *)servername, |
| 1647 | -1, -1, 0) != 1) { |
| 1648 | X509_NAME_free(name); |
| 1649 | goto mkcert_error; |
| 1650 | } |
| 1651 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1652 | X509_NAME_free(name); |
| 1653 | goto mkcert_error; |
| 1654 | } |
| 1655 | X509_NAME_free(name); |
| 1656 | |
| 1657 | /* Add x509v3 extensions as specified */ |
| 1658 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1659 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1660 | X509_EXTENSION *ext; |
| 1661 | |
| 1662 | if (!(ext = X509V3_EXT_conf(NULL, &ctx, x509v3_ext_names[i], x509v3_ext_values[i]))) |
| 1663 | goto mkcert_error; |
| 1664 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1665 | X509_EXTENSION_free(ext); |
| 1666 | goto mkcert_error; |
| 1667 | } |
| 1668 | X509_EXTENSION_free(ext); |
| 1669 | } |
| 1670 | |
| 1671 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1672 | |
| 1673 | key_type = EVP_PKEY_base_id(capkey); |
| 1674 | |
| 1675 | if (key_type == EVP_PKEY_DSA) |
| 1676 | digest = EVP_sha1(); |
| 1677 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1678 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1679 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1680 | digest = EVP_sha256(); |
| 1681 | else { |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1682 | #if (OPENSSL_VERSION_NUMBER >= 0x1000000fL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1683 | int nid; |
| 1684 | |
| 1685 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1686 | goto mkcert_error; |
| 1687 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1688 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1689 | #else |
| 1690 | goto mkcert_error; |
| 1691 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1692 | } |
| 1693 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1694 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1695 | goto mkcert_error; |
| 1696 | |
| 1697 | /* Create and set the new SSL_CTX */ |
| 1698 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1699 | goto mkcert_error; |
| 1700 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1701 | goto mkcert_error; |
| 1702 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1703 | goto mkcert_error; |
| 1704 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1705 | goto mkcert_error; |
| 1706 | |
| 1707 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1708 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1709 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1710 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1711 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1712 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1713 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1714 | 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] | 1715 | EC_KEY *ecc; |
| 1716 | int nid; |
| 1717 | |
| 1718 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1719 | goto end; |
| 1720 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1721 | goto end; |
| 1722 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1723 | EC_KEY_free(ecc); |
| 1724 | } |
| 1725 | #endif |
| 1726 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1727 | return ssl_ctx; |
| 1728 | |
| 1729 | mkcert_error: |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1730 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1731 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1732 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1733 | return NULL; |
| 1734 | } |
| 1735 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1736 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1737 | 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] | 1738 | { |
| 1739 | struct bind_conf *bind_conf = objt_listener(conn->target)->bind_conf; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1740 | |
| 1741 | return ssl_sock_do_create_cert(servername, bind_conf, conn->xprt_ctx); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1742 | } |
| 1743 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1744 | /* Do a lookup for a certificate in the LRU cache used to store generated |
| 1745 | * certificates. */ |
| 1746 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1747 | 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] | 1748 | { |
| 1749 | struct lru64 *lru = NULL; |
| 1750 | |
| 1751 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1752 | 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] | 1753 | if (lru && lru->domain) |
| 1754 | return (SSL_CTX *)lru->data; |
| 1755 | } |
| 1756 | return NULL; |
| 1757 | } |
| 1758 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1759 | /* Set a certificate int the LRU cache used to store generated |
| 1760 | * certificate. Return 0 on success, otherwise -1 */ |
| 1761 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1762 | 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] | 1763 | { |
| 1764 | struct lru64 *lru = NULL; |
| 1765 | |
| 1766 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1767 | 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] | 1768 | if (!lru) |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1769 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1770 | if (lru->domain && lru->data) |
| 1771 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1772 | 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] | 1773 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1774 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1775 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1776 | } |
| 1777 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1778 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1779 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1780 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1781 | { |
| 1782 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 1783 | } |
| 1784 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1785 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 1786 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 1787 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1788 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1789 | 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] | 1790 | { |
| 1791 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1792 | SSL_CTX *ssl_ctx = NULL; |
| 1793 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1794 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1795 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1796 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1797 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1798 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1799 | if (lru && lru->domain) |
| 1800 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1801 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1802 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1803 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1804 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1805 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1806 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1807 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1808 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1809 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1810 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 1811 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 1812 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1813 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1814 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1815 | return 0; |
| 1816 | } |
| 1817 | static int |
| 1818 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 1819 | { |
| 1820 | unsigned int key; |
| 1821 | SSL_CTX *ssl_ctx = NULL; |
| 1822 | struct connection *conn = SSL_get_app_data(ssl); |
| 1823 | |
| 1824 | conn_get_to_addr(conn); |
| 1825 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
| 1826 | key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to)); |
| 1827 | ssl_ctx = ssl_sock_get_generated_cert(key, bind_conf); |
| 1828 | if (ssl_ctx) { |
| 1829 | /* switch ctx */ |
| 1830 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 1831 | return 1; |
| 1832 | } |
| 1833 | } |
| 1834 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1835 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1836 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1837 | |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1838 | |
| 1839 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 1840 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 1841 | #endif |
| 1842 | |
| 1843 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 1844 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
| 1845 | #define SSL_renegotiate_pending(arg) 0 |
| 1846 | #endif |
| 1847 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 1848 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1849 | #endif |
| 1850 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 1851 | #define SSL_OP_NO_TICKET 0 |
| 1852 | #endif |
| 1853 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 1854 | #define SSL_OP_NO_COMPRESSION 0 |
| 1855 | #endif |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 1856 | #ifdef OPENSSL_NO_SSL3 /* SSLv3 support removed */ |
| 1857 | #undef SSL_OP_NO_SSLv3 |
| 1858 | #define SSL_OP_NO_SSLv3 0 |
| 1859 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1860 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 1861 | #define SSL_OP_NO_TLSv1_1 0 |
| 1862 | #endif |
| 1863 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 1864 | #define SSL_OP_NO_TLSv1_2 0 |
| 1865 | #endif |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 1866 | #ifndef SSL_OP_NO_TLSv1_3 /* needs OpenSSL >= 1.1.1 */ |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1867 | #define SSL_OP_NO_TLSv1_3 0 |
| 1868 | #endif |
| 1869 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 1870 | #define SSL_OP_SINGLE_DH_USE 0 |
| 1871 | #endif |
| 1872 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 1873 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1874 | #endif |
| 1875 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 1876 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 1877 | #endif |
| 1878 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 1879 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 1880 | #endif |
| 1881 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 1882 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1883 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 1884 | |
| 1885 | 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] | 1886 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 1887 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1888 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1889 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 1890 | #endif |
| 1891 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1892 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 1893 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1894 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 1895 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1896 | 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] | 1897 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1898 | 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] | 1899 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 1900 | #endif |
| 1901 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1902 | 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] | 1903 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1904 | 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] | 1905 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 1906 | #endif |
| 1907 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1908 | /* TLS 1.2 is the last supported version in this context. */ |
| 1909 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 1910 | /* Unusable in this context. */ |
| 1911 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 1912 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 1913 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 1914 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 1915 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1916 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1917 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 1918 | |
| 1919 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 1920 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1921 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 1922 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1923 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 1924 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 1925 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 1926 | } |
| 1927 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 1928 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1929 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 1930 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1931 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 1932 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 1933 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 1934 | } |
| 1935 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 1936 | 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] | 1937 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 1938 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1939 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 1940 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 1941 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 1942 | } |
| 1943 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 1944 | 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] | 1945 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 1946 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1947 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 1948 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 1949 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 1950 | } |
| 1951 | 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] | 1952 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1953 | 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] | 1954 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 1955 | #endif |
| 1956 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1957 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 1958 | #if SSL_OP_NO_TLSv1_3 |
| 1959 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 1960 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1961 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1962 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1963 | #endif |
| 1964 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 1965 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1966 | |
| 1967 | static struct { |
| 1968 | int option; |
| 1969 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1970 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 1971 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1972 | const char *name; |
| 1973 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1974 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 1975 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 1976 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 1977 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 1978 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 1979 | {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] | 1980 | }; |
| 1981 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 1982 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 1983 | { |
| 1984 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 1985 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 1986 | SSL_set_SSL_CTX(ssl, ctx); |
| 1987 | } |
| 1988 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 1989 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1990 | |
| 1991 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 1992 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1993 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1994 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1995 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1996 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 1997 | return SSL_TLSEXT_ERR_OK; |
| 1998 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1999 | } |
| 2000 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2001 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2002 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2003 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2004 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2005 | #else |
| 2006 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2007 | { |
| 2008 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2009 | struct connection *conn; |
| 2010 | struct bind_conf *s; |
| 2011 | const uint8_t *extension_data; |
| 2012 | size_t extension_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2013 | int has_rsa = 0, has_ecdsa = 0, has_ecdsa_sig = 0; |
| 2014 | |
| 2015 | char *wildp = NULL; |
| 2016 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2017 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2018 | struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2019 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2020 | int i; |
| 2021 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2022 | conn = SSL_get_app_data(ssl); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2023 | s = objt_listener(conn->target)->bind_conf; |
| 2024 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2025 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2026 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2027 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2028 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2029 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2030 | #else |
| 2031 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2032 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2033 | /* |
| 2034 | * The server_name extension was given too much extensibility when it |
| 2035 | * was written, so parsing the normal case is a bit complex. |
| 2036 | */ |
| 2037 | size_t len; |
| 2038 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2039 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2040 | /* Extract the length of the supplied list of names. */ |
| 2041 | len = (*extension_data++) << 8; |
| 2042 | len |= *extension_data++; |
| 2043 | if (len + 2 != extension_len) |
| 2044 | goto abort; |
| 2045 | /* |
| 2046 | * The list in practice only has a single element, so we only consider |
| 2047 | * the first one. |
| 2048 | */ |
| 2049 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2050 | goto abort; |
| 2051 | extension_len = len - 1; |
| 2052 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2053 | if (extension_len <= 2) |
| 2054 | goto abort; |
| 2055 | len = (*extension_data++) << 8; |
| 2056 | len |= *extension_data++; |
| 2057 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2058 | || memchr(extension_data, 0, len) != NULL) |
| 2059 | goto abort; |
| 2060 | servername = extension_data; |
| 2061 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2062 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2063 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2064 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2065 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2066 | } |
| 2067 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2068 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2069 | if (!s->strict_sni) { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2070 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2071 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2072 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2073 | goto abort; |
| 2074 | } |
| 2075 | |
| 2076 | /* extract/check clientHello informations */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2077 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2078 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2079 | #else |
| 2080 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2081 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2082 | uint8_t sign; |
| 2083 | size_t len; |
| 2084 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2085 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2086 | len = (*extension_data++) << 8; |
| 2087 | len |= *extension_data++; |
| 2088 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2089 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2090 | if (len % 2 != 0) |
| 2091 | goto abort; |
| 2092 | for (; len > 0; len -= 2) { |
| 2093 | extension_data++; /* hash */ |
| 2094 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2095 | switch (sign) { |
| 2096 | case TLSEXT_signature_rsa: |
| 2097 | has_rsa = 1; |
| 2098 | break; |
| 2099 | case TLSEXT_signature_ecdsa: |
| 2100 | has_ecdsa_sig = 1; |
| 2101 | break; |
| 2102 | default: |
| 2103 | continue; |
| 2104 | } |
| 2105 | if (has_ecdsa_sig && has_rsa) |
| 2106 | break; |
| 2107 | } |
| 2108 | } else { |
| 2109 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLS 1.2) */ |
| 2110 | has_rsa = 1; |
| 2111 | } |
| 2112 | if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2113 | const SSL_CIPHER *cipher; |
| 2114 | size_t len; |
| 2115 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2116 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2117 | len = ctx->cipher_suites_len; |
| 2118 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2119 | #else |
| 2120 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2121 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2122 | if (len % 2 != 0) |
| 2123 | goto abort; |
| 2124 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2125 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2126 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2127 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2128 | #else |
| 2129 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2130 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2131 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2132 | has_ecdsa = 1; |
| 2133 | break; |
| 2134 | } |
| 2135 | } |
| 2136 | } |
| 2137 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2138 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2139 | trash.str[i] = tolower(servername[i]); |
| 2140 | if (!wildp && (trash.str[i] == '.')) |
| 2141 | wildp = &trash.str[i]; |
| 2142 | } |
| 2143 | trash.str[i] = 0; |
| 2144 | |
| 2145 | /* lookup in full qualified names */ |
| 2146 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 2147 | |
| 2148 | /* lookup a not neg filter */ |
| 2149 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2150 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2151 | switch(container_of(n, struct sni_ctx, name)->key_sig) { |
| 2152 | case TLSEXT_signature_ecdsa: |
| 2153 | if (has_ecdsa) { |
| 2154 | node_ecdsa = n; |
| 2155 | goto find_one; |
| 2156 | } |
| 2157 | break; |
| 2158 | case TLSEXT_signature_rsa: |
| 2159 | if (has_rsa && !node_rsa) { |
| 2160 | node_rsa = n; |
| 2161 | if (!has_ecdsa) |
| 2162 | goto find_one; |
| 2163 | } |
| 2164 | break; |
| 2165 | default: /* TLSEXT_signature_anonymous */ |
| 2166 | if (!node_anonymous) |
| 2167 | node_anonymous = n; |
| 2168 | break; |
| 2169 | } |
| 2170 | } |
| 2171 | } |
| 2172 | if (wildp) { |
| 2173 | /* lookup in wildcards names */ |
| 2174 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2175 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2176 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2177 | switch(container_of(n, struct sni_ctx, name)->key_sig) { |
| 2178 | case TLSEXT_signature_ecdsa: |
| 2179 | if (has_ecdsa) { |
| 2180 | node_ecdsa = n; |
| 2181 | goto find_one; |
| 2182 | } |
| 2183 | break; |
| 2184 | case TLSEXT_signature_rsa: |
| 2185 | if (has_rsa && !node_rsa) { |
| 2186 | node_rsa = n; |
| 2187 | if (!has_ecdsa) |
| 2188 | goto find_one; |
| 2189 | } |
| 2190 | break; |
| 2191 | default: /* TLSEXT_signature_anonymous */ |
| 2192 | if (!node_anonymous) |
| 2193 | node_anonymous = n; |
| 2194 | break; |
| 2195 | } |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | find_one: |
| 2200 | /* select by key_signature priority order */ |
| 2201 | node = node_ecdsa ? node_ecdsa : (node_rsa ? node_rsa : node_anonymous); |
| 2202 | |
| 2203 | if (node) { |
| 2204 | /* switch ctx */ |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 2205 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2206 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
| 2207 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2208 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2209 | if (conf->early_data) |
| 2210 | allow_early = 1; |
| 2211 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2212 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2213 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2214 | if (s->generate_certs && ssl_sock_generate_certificate(trash.str, s, ssl)) { |
| 2215 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2216 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2217 | } |
| 2218 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2219 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2220 | /* no certificate match, is the default_ctx */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2221 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2222 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2223 | allow_early: |
| 2224 | #ifdef OPENSSL_IS_BORINGSSL |
| 2225 | if (allow_early) |
| 2226 | SSL_set_early_data_enabled(ssl, 1); |
| 2227 | #else |
| 2228 | if (!allow_early) |
| 2229 | SSL_set_max_early_data(ssl, 0); |
| 2230 | #endif |
| 2231 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2232 | abort: |
| 2233 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2234 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2235 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2236 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2237 | #else |
| 2238 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2239 | return 0; |
| 2240 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2244 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2245 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2246 | * warning when no match is found, which implies the default (first) cert |
| 2247 | * will keep being used. |
| 2248 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2249 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2250 | { |
| 2251 | const char *servername; |
| 2252 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2253 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2254 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2255 | int i; |
| 2256 | (void)al; /* shut gcc stupid warning */ |
| 2257 | |
| 2258 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2259 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2260 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2261 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2262 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2263 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2264 | if (s->strict_sni) |
| 2265 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2266 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2267 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2268 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2269 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2270 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2271 | if (!servername[i]) |
| 2272 | break; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2273 | trash.str[i] = tolower(servername[i]); |
| 2274 | if (!wildp && (trash.str[i] == '.')) |
| 2275 | wildp = &trash.str[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2276 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2277 | trash.str[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2278 | |
| 2279 | /* lookup in full qualified names */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2280 | node = ebst_lookup(&s->sni_ctx, trash.str); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2281 | |
| 2282 | /* lookup a not neg filter */ |
| 2283 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2284 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2285 | node = n; |
| 2286 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2287 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2288 | } |
| 2289 | if (!node && wildp) { |
| 2290 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2291 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2292 | } |
| 2293 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2294 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2295 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2296 | /* switch ctx done in ssl_sock_generate_certificate */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2297 | return SSL_TLSEXT_ERR_OK; |
| 2298 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2299 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2300 | if (s->strict_sni) |
| 2301 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2302 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2303 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2307 | 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] | 2308 | return SSL_TLSEXT_ERR_OK; |
| 2309 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2310 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2311 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2312 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2313 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2314 | |
| 2315 | static DH * ssl_get_dh_1024(void) |
| 2316 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2317 | static unsigned char dh1024_p[]={ |
| 2318 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2319 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2320 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2321 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2322 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2323 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2324 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2325 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2326 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2327 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2328 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2329 | }; |
| 2330 | static unsigned char dh1024_g[]={ |
| 2331 | 0x02, |
| 2332 | }; |
| 2333 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2334 | BIGNUM *p; |
| 2335 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2336 | DH *dh = DH_new(); |
| 2337 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2338 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2339 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2340 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2341 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2342 | DH_free(dh); |
| 2343 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2344 | } else { |
| 2345 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2346 | } |
| 2347 | } |
| 2348 | return dh; |
| 2349 | } |
| 2350 | |
| 2351 | static DH *ssl_get_dh_2048(void) |
| 2352 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2353 | static unsigned char dh2048_p[]={ |
| 2354 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2355 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2356 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2357 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2358 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2359 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2360 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2361 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2362 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2363 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2364 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2365 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2366 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2367 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2368 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2369 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2370 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2371 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2372 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2373 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2374 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2375 | 0xB7,0x1F,0x77,0xF3, |
| 2376 | }; |
| 2377 | static unsigned char dh2048_g[]={ |
| 2378 | 0x02, |
| 2379 | }; |
| 2380 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2381 | BIGNUM *p; |
| 2382 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2383 | DH *dh = DH_new(); |
| 2384 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2385 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2386 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2387 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2388 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2389 | DH_free(dh); |
| 2390 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2391 | } else { |
| 2392 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2393 | } |
| 2394 | } |
| 2395 | return dh; |
| 2396 | } |
| 2397 | |
| 2398 | static DH *ssl_get_dh_4096(void) |
| 2399 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2400 | static unsigned char dh4096_p[]={ |
| 2401 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2402 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2403 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2404 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2405 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2406 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2407 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2408 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2409 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2410 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2411 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2412 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2413 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2414 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2415 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2416 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2417 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2418 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2419 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2420 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2421 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2422 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2423 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2424 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2425 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2426 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2427 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2428 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2429 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2430 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2431 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2432 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2433 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2434 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2435 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2436 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2437 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2438 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2439 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2440 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2441 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2442 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2443 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2444 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2445 | static unsigned char dh4096_g[]={ |
| 2446 | 0x02, |
| 2447 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2448 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2449 | BIGNUM *p; |
| 2450 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2451 | DH *dh = DH_new(); |
| 2452 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2453 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2454 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2455 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2456 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2457 | DH_free(dh); |
| 2458 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2459 | } else { |
| 2460 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2461 | } |
| 2462 | } |
| 2463 | return dh; |
| 2464 | } |
| 2465 | |
| 2466 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2467 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2468 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2469 | { |
| 2470 | DH *dh = NULL; |
| 2471 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2472 | int type; |
| 2473 | |
| 2474 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2475 | |
| 2476 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2477 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2478 | */ |
| 2479 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2480 | keylen = EVP_PKEY_bits(pkey); |
| 2481 | } |
| 2482 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2483 | if (keylen > global_ssl.default_dh_param) { |
| 2484 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2485 | } |
| 2486 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2487 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2488 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2489 | } |
| 2490 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2491 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2492 | } |
| 2493 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2494 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | return dh; |
| 2498 | } |
| 2499 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2500 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2501 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2502 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2503 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2504 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2505 | if (in == NULL) |
| 2506 | goto end; |
| 2507 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2508 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2509 | goto end; |
| 2510 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2511 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2512 | |
| 2513 | end: |
| 2514 | if (in) |
| 2515 | BIO_free(in); |
| 2516 | |
| 2517 | return dh; |
| 2518 | } |
| 2519 | |
| 2520 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2521 | { |
| 2522 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2523 | |
| 2524 | if (global_dh) { |
| 2525 | return 0; |
| 2526 | } |
| 2527 | |
| 2528 | return -1; |
| 2529 | } |
| 2530 | |
| 2531 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 2532 | if an error occured, and 0 if parameter not found. */ |
| 2533 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 2534 | { |
| 2535 | int ret = -1; |
| 2536 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 2537 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2538 | if (dh) { |
| 2539 | ret = 1; |
| 2540 | SSL_CTX_set_tmp_dh(ctx, dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2541 | |
| 2542 | if (ssl_dh_ptr_index >= 0) { |
| 2543 | /* store a pointer to the DH params to avoid complaining about |
| 2544 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2545 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2546 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2547 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2548 | else if (global_dh) { |
| 2549 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 2550 | ret = 0; /* DH params not found */ |
| 2551 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2552 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2553 | /* Clear openssl global errors stack */ |
| 2554 | ERR_clear_error(); |
| 2555 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2556 | if (global_ssl.default_dh_param <= 1024) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2557 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 2558 | if (local_dh_1024 == NULL) |
| 2559 | local_dh_1024 = ssl_get_dh_1024(); |
| 2560 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2561 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2562 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2563 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2564 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2565 | } |
| 2566 | else { |
| 2567 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2568 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2569 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2570 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2571 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2572 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2573 | end: |
| 2574 | if (dh) |
| 2575 | DH_free(dh); |
| 2576 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2577 | return ret; |
| 2578 | } |
| 2579 | #endif |
| 2580 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2581 | static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, struct ssl_bind_conf *conf, |
| 2582 | uint8_t key_sig, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2583 | { |
| 2584 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2585 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2586 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2587 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2588 | if (*name == '!') { |
| 2589 | neg = 1; |
| 2590 | name++; |
| 2591 | } |
| 2592 | if (*name == '*') { |
| 2593 | wild = 1; |
| 2594 | name++; |
| 2595 | } |
| 2596 | /* !* filter is a nop */ |
| 2597 | if (neg && wild) |
| 2598 | return order; |
| 2599 | if (*name) { |
| 2600 | int j, len; |
| 2601 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2602 | for (j = 0; j < len && j < trash.size; j++) |
| 2603 | trash.str[j] = tolower(name[j]); |
| 2604 | if (j >= trash.size) |
| 2605 | return order; |
| 2606 | trash.str[j] = 0; |
| 2607 | |
| 2608 | /* Check for duplicates. */ |
| 2609 | if (wild) |
| 2610 | node = ebst_lookup(&s->sni_w_ctx, trash.str); |
| 2611 | else |
| 2612 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 2613 | for (; node; node = ebmb_next_dup(node)) { |
| 2614 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2615 | if (sc->ctx == ctx && sc->conf == conf && |
| 2616 | sc->key_sig == key_sig && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2617 | return order; |
| 2618 | } |
| 2619 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2620 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2621 | if (!sc) |
| 2622 | return order; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2623 | memcpy(sc->name.key, trash.str, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2624 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2625 | sc->conf = conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2626 | sc->key_sig = key_sig; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2627 | sc->order = order++; |
| 2628 | sc->neg = neg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2629 | if (wild) |
| 2630 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 2631 | else |
| 2632 | ebst_insert(&s->sni_ctx, &sc->name); |
| 2633 | } |
| 2634 | return order; |
| 2635 | } |
| 2636 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2637 | |
| 2638 | /* The following code is used for loading multiple crt files into |
| 2639 | * SSL_CTX's based on CN/SAN |
| 2640 | */ |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 2641 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(LIBRESSL_VERSION_NUMBER) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2642 | /* This is used to preload the certifcate, private key |
| 2643 | * and Cert Chain of a file passed in via the crt |
| 2644 | * argument |
| 2645 | * |
| 2646 | * This way, we do not have to read the file multiple times |
| 2647 | */ |
| 2648 | struct cert_key_and_chain { |
| 2649 | X509 *cert; |
| 2650 | EVP_PKEY *key; |
| 2651 | unsigned int num_chain_certs; |
| 2652 | /* This is an array of X509 pointers */ |
| 2653 | X509 **chain_certs; |
| 2654 | }; |
| 2655 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2656 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 2657 | |
| 2658 | struct key_combo_ctx { |
| 2659 | SSL_CTX *ctx; |
| 2660 | int order; |
| 2661 | }; |
| 2662 | |
| 2663 | /* Map used for processing multiple keypairs for a single purpose |
| 2664 | * |
| 2665 | * This maps CN/SNI name to certificate type |
| 2666 | */ |
| 2667 | struct sni_keytype { |
| 2668 | int keytypes; /* BITMASK for keytypes */ |
| 2669 | struct ebmb_node name; /* node holding the servername value */ |
| 2670 | }; |
| 2671 | |
| 2672 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2673 | /* Frees the contents of a cert_key_and_chain |
| 2674 | */ |
| 2675 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2676 | { |
| 2677 | int i; |
| 2678 | |
| 2679 | if (!ckch) |
| 2680 | return; |
| 2681 | |
| 2682 | /* Free the certificate and set pointer to NULL */ |
| 2683 | if (ckch->cert) |
| 2684 | X509_free(ckch->cert); |
| 2685 | ckch->cert = NULL; |
| 2686 | |
| 2687 | /* Free the key and set pointer to NULL */ |
| 2688 | if (ckch->key) |
| 2689 | EVP_PKEY_free(ckch->key); |
| 2690 | ckch->key = NULL; |
| 2691 | |
| 2692 | /* Free each certificate in the chain */ |
| 2693 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2694 | if (ckch->chain_certs[i]) |
| 2695 | X509_free(ckch->chain_certs[i]); |
| 2696 | } |
| 2697 | |
| 2698 | /* Free the chain obj itself and set to NULL */ |
| 2699 | if (ckch->num_chain_certs > 0) { |
| 2700 | free(ckch->chain_certs); |
| 2701 | ckch->num_chain_certs = 0; |
| 2702 | ckch->chain_certs = NULL; |
| 2703 | } |
| 2704 | |
| 2705 | } |
| 2706 | |
| 2707 | /* checks if a key and cert exists in the ckch |
| 2708 | */ |
| 2709 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 2710 | { |
| 2711 | return (ckch->cert != NULL && ckch->key != NULL); |
| 2712 | } |
| 2713 | |
| 2714 | |
| 2715 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 2716 | * This allows us to carry the contents of the file without having to |
| 2717 | * read the file multiple times. |
| 2718 | * |
| 2719 | * returns: |
| 2720 | * 0 on Success |
| 2721 | * 1 on SSL Failure |
| 2722 | * 2 on file not found |
| 2723 | */ |
| 2724 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 2725 | { |
| 2726 | |
| 2727 | BIO *in; |
| 2728 | X509 *ca = NULL; |
| 2729 | int ret = 1; |
| 2730 | |
| 2731 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2732 | |
| 2733 | in = BIO_new(BIO_s_file()); |
| 2734 | if (in == NULL) |
| 2735 | goto end; |
| 2736 | |
| 2737 | if (BIO_read_filename(in, path) <= 0) |
| 2738 | goto end; |
| 2739 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2740 | /* Read Private Key */ |
| 2741 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 2742 | if (ckch->key == NULL) { |
| 2743 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 2744 | err && *err ? *err : "", path); |
| 2745 | goto end; |
| 2746 | } |
| 2747 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2748 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 2749 | if (BIO_reset(in) == -1) { |
| 2750 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 2751 | err && *err ? *err : "", path); |
| 2752 | goto end; |
| 2753 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2754 | |
| 2755 | /* Read Certificate */ |
| 2756 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 2757 | if (ckch->cert == NULL) { |
| 2758 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 2759 | err && *err ? *err : "", path); |
| 2760 | goto end; |
| 2761 | } |
| 2762 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2763 | /* Read Certificate Chain */ |
| 2764 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 2765 | /* Grow the chain certs */ |
| 2766 | ckch->num_chain_certs++; |
| 2767 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 2768 | |
| 2769 | /* use - 1 here since we just incremented it above */ |
| 2770 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 2771 | } |
| 2772 | ret = ERR_get_error(); |
| 2773 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 2774 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 2775 | err && *err ? *err : "", path); |
| 2776 | ret = 1; |
| 2777 | goto end; |
| 2778 | } |
| 2779 | |
| 2780 | ret = 0; |
| 2781 | |
| 2782 | end: |
| 2783 | |
| 2784 | ERR_clear_error(); |
| 2785 | if (in) |
| 2786 | BIO_free(in); |
| 2787 | |
| 2788 | /* Something went wrong in one of the reads */ |
| 2789 | if (ret != 0) |
| 2790 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2791 | |
| 2792 | return ret; |
| 2793 | } |
| 2794 | |
| 2795 | /* Loads the info in ckch into ctx |
| 2796 | * Currently, this does not process any information about ocsp, dhparams or |
| 2797 | * sctl |
| 2798 | * Returns |
| 2799 | * 0 on success |
| 2800 | * 1 on failure |
| 2801 | */ |
| 2802 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2803 | { |
| 2804 | int i = 0; |
| 2805 | |
| 2806 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2807 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2808 | err && *err ? *err : "", path); |
| 2809 | return 1; |
| 2810 | } |
| 2811 | |
| 2812 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 2813 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 2814 | err && *err ? *err : "", path); |
| 2815 | return 1; |
| 2816 | } |
| 2817 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2818 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 2819 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2820 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2821 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 2822 | err && *err ? *err : "", (i+1), path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2823 | return 1; |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2828 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2829 | err && *err ? *err : "", path); |
| 2830 | return 1; |
| 2831 | } |
| 2832 | |
| 2833 | return 0; |
| 2834 | } |
| 2835 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2836 | |
| 2837 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 2838 | { |
| 2839 | struct sni_keytype *s_kt = NULL; |
| 2840 | struct ebmb_node *node; |
| 2841 | int i; |
| 2842 | |
| 2843 | for (i = 0; i < trash.size; i++) { |
| 2844 | if (!str[i]) |
| 2845 | break; |
| 2846 | trash.str[i] = tolower(str[i]); |
| 2847 | } |
| 2848 | trash.str[i] = 0; |
| 2849 | node = ebst_lookup(sni_keytypes, trash.str); |
| 2850 | if (!node) { |
| 2851 | /* CN not found in tree */ |
| 2852 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 2853 | /* Using memcpy here instead of strncpy. |
| 2854 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 2855 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 2856 | */ |
| 2857 | memcpy(s_kt->name.key, trash.str, i+1); |
| 2858 | s_kt->keytypes = 0; |
| 2859 | ebst_insert(sni_keytypes, &s_kt->name); |
| 2860 | } else { |
| 2861 | /* CN found in tree */ |
| 2862 | s_kt = container_of(node, struct sni_keytype, name); |
| 2863 | } |
| 2864 | |
| 2865 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 2866 | s_kt->keytypes |= 1<<key_index; |
| 2867 | |
| 2868 | } |
| 2869 | |
| 2870 | |
| 2871 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 2872 | * If any are found, group these files into a set of SSL_CTX* |
| 2873 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 2874 | * |
| 2875 | * This will allow the user to explictly group multiple cert/keys for a single purpose |
| 2876 | * |
| 2877 | * Returns |
| 2878 | * 0 on success |
| 2879 | * 1 on failure |
| 2880 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2881 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2882 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2883 | { |
| 2884 | char fp[MAXPATHLEN+1] = {0}; |
| 2885 | int n = 0; |
| 2886 | int i = 0; |
| 2887 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 2888 | struct eb_root sni_keytypes_map = { {0} }; |
| 2889 | struct ebmb_node *node; |
| 2890 | struct ebmb_node *next; |
| 2891 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 2892 | * of keytypes |
| 2893 | */ |
| 2894 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 2895 | int rv = 0; |
| 2896 | X509_NAME *xname = NULL; |
| 2897 | char *str = NULL; |
| 2898 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2899 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 2900 | #endif |
| 2901 | |
| 2902 | /* Load all possible certs and keys */ |
| 2903 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2904 | struct stat buf; |
| 2905 | |
| 2906 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2907 | if (stat(fp, &buf) == 0) { |
| 2908 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
| 2909 | rv = 1; |
| 2910 | goto end; |
| 2911 | } |
| 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | /* Process each ckch and update keytypes for each CN/SAN |
| 2916 | * for example, if CN/SAN www.a.com is associated with |
| 2917 | * certs with keytype 0 and 2, then at the end of the loop, |
| 2918 | * www.a.com will have: |
| 2919 | * keyindex = 0 | 1 | 4 = 5 |
| 2920 | */ |
| 2921 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2922 | |
| 2923 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 2924 | continue; |
| 2925 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2926 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 2927 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2928 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 2929 | } else { |
| 2930 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 2931 | * so the line that contains logic is marked via comments |
| 2932 | */ |
| 2933 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 2934 | i = -1; |
| 2935 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 2936 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2937 | ASN1_STRING *value; |
| 2938 | value = X509_NAME_ENTRY_get_data(entry); |
| 2939 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2940 | /* Important line is here */ |
| 2941 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2942 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2943 | OPENSSL_free(str); |
| 2944 | str = NULL; |
| 2945 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2946 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2947 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2948 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2949 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2950 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 2951 | if (names) { |
| 2952 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 2953 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2954 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2955 | if (name->type == GEN_DNS) { |
| 2956 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 2957 | /* Important line is here */ |
| 2958 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2959 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2960 | OPENSSL_free(str); |
| 2961 | str = NULL; |
| 2962 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2963 | } |
| 2964 | } |
| 2965 | } |
| 2966 | } |
| 2967 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2968 | } |
| 2969 | |
| 2970 | /* If no files found, return error */ |
| 2971 | if (eb_is_empty(&sni_keytypes_map)) { |
| 2972 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 2973 | err && *err ? *err : "", path); |
| 2974 | rv = 1; |
| 2975 | goto end; |
| 2976 | } |
| 2977 | |
| 2978 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 2979 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 2980 | * and add each CTX to the SNI tree |
| 2981 | * |
| 2982 | * Some math here: |
| 2983 | * There are 2^n - 1 possibile combinations, each unique |
| 2984 | * combination is denoted by the key in the map. Each key |
| 2985 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 2986 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 2987 | * entry in the array to correspond to the unique combo (key) |
| 2988 | * associated with i. This unique key combo (i) will be associated |
| 2989 | * with combos[i-1] |
| 2990 | */ |
| 2991 | |
| 2992 | node = ebmb_first(&sni_keytypes_map); |
| 2993 | while (node) { |
| 2994 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2995 | char cur_file[MAXPATHLEN+1]; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2996 | |
| 2997 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 2998 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 2999 | cur_ctx = key_combos[i-1].ctx; |
| 3000 | |
| 3001 | if (cur_ctx == NULL) { |
| 3002 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3003 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3004 | if (cur_ctx == NULL) { |
| 3005 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3006 | err && *err ? *err : ""); |
| 3007 | rv = 1; |
| 3008 | goto end; |
| 3009 | } |
| 3010 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3011 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3012 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3013 | if (i & (1<<n)) { |
| 3014 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3015 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3016 | 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] | 3017 | SSL_CTX_free(cur_ctx); |
| 3018 | rv = 1; |
| 3019 | goto end; |
| 3020 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3021 | |
| 3022 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 3023 | /* Load OCSP Info into context */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3024 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3025 | if (err) |
| 3026 | 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] | 3027 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3028 | SSL_CTX_free(cur_ctx); |
| 3029 | rv = 1; |
| 3030 | goto end; |
| 3031 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3032 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3033 | ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3034 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | /* Load DH params into the ctx to support DHE keys */ |
| 3039 | #ifndef OPENSSL_NO_DH |
| 3040 | if (ssl_dh_ptr_index >= 0) |
| 3041 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 3042 | |
| 3043 | rv = ssl_sock_load_dh_params(cur_ctx, NULL); |
| 3044 | if (rv < 0) { |
| 3045 | if (err) |
| 3046 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3047 | *err ? *err : "", path); |
| 3048 | rv = 1; |
| 3049 | goto end; |
| 3050 | } |
| 3051 | #endif |
| 3052 | |
| 3053 | /* Update key_combos */ |
| 3054 | key_combos[i-1].ctx = cur_ctx; |
| 3055 | } |
| 3056 | |
| 3057 | /* Update SNI Tree */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3058 | key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf, |
| 3059 | TLSEXT_signature_anonymous, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3060 | node = ebmb_next(node); |
| 3061 | } |
| 3062 | |
| 3063 | |
| 3064 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 3065 | if (!bind_conf->default_ctx) { |
| 3066 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 3067 | if (key_combos[i].ctx) { |
| 3068 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3069 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3070 | break; |
| 3071 | } |
| 3072 | } |
| 3073 | } |
| 3074 | |
| 3075 | end: |
| 3076 | |
| 3077 | if (names) |
| 3078 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 3079 | |
| 3080 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 3081 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 3082 | |
| 3083 | node = ebmb_first(&sni_keytypes_map); |
| 3084 | while (node) { |
| 3085 | next = ebmb_next(node); |
| 3086 | ebmb_delete(node); |
| 3087 | node = next; |
| 3088 | } |
| 3089 | |
| 3090 | return rv; |
| 3091 | } |
| 3092 | #else |
| 3093 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3094 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3095 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3096 | { |
| 3097 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3098 | err && *err ? *err : "", path, strerror(errno)); |
| 3099 | return 1; |
| 3100 | } |
| 3101 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3102 | #endif /* #if OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
| 3103 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3104 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 3105 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 3106 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3107 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, |
| 3108 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3109 | { |
| 3110 | BIO *in; |
| 3111 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3112 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3113 | int ret = -1; |
| 3114 | int order = 0; |
| 3115 | X509_NAME *xname; |
| 3116 | char *str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3117 | pem_password_cb *passwd_cb; |
| 3118 | void *passwd_cb_userdata; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3119 | EVP_PKEY *pkey; |
| 3120 | uint8_t key_sig = TLSEXT_signature_anonymous; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3121 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3122 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3123 | STACK_OF(GENERAL_NAME) *names; |
| 3124 | #endif |
| 3125 | |
| 3126 | in = BIO_new(BIO_s_file()); |
| 3127 | if (in == NULL) |
| 3128 | goto end; |
| 3129 | |
| 3130 | if (BIO_read_filename(in, file) <= 0) |
| 3131 | goto end; |
| 3132 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3133 | |
| 3134 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 3135 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 3136 | |
| 3137 | 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] | 3138 | if (x == NULL) |
| 3139 | goto end; |
| 3140 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3141 | pkey = X509_get_pubkey(x); |
| 3142 | if (pkey) { |
| 3143 | switch(EVP_PKEY_base_id(pkey)) { |
| 3144 | case EVP_PKEY_RSA: |
| 3145 | key_sig = TLSEXT_signature_rsa; |
| 3146 | break; |
| 3147 | case EVP_PKEY_EC: |
| 3148 | key_sig = TLSEXT_signature_ecdsa; |
| 3149 | break; |
| 3150 | } |
| 3151 | EVP_PKEY_free(pkey); |
| 3152 | } |
| 3153 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3154 | if (fcount) { |
| 3155 | while (fcount--) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3156 | 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] | 3157 | } |
| 3158 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3159 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3160 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 3161 | if (names) { |
| 3162 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3163 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3164 | if (name->type == GEN_DNS) { |
| 3165 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3166 | 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] | 3167 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3168 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3169 | } |
| 3170 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3171 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3172 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3173 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3174 | xname = X509_get_subject_name(x); |
| 3175 | i = -1; |
| 3176 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3177 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3178 | ASN1_STRING *value; |
| 3179 | |
| 3180 | value = X509_NAME_ENTRY_get_data(entry); |
| 3181 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3182 | 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] | 3183 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3184 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 3189 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 3190 | goto end; |
| 3191 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3192 | #ifdef SSL_CTX_clear_extra_chain_certs |
| 3193 | SSL_CTX_clear_extra_chain_certs(ctx); |
| 3194 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3195 | if (ctx->extra_certs != NULL) { |
| 3196 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 3197 | ctx->extra_certs = NULL; |
| 3198 | } |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3199 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3200 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3201 | 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] | 3202 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3203 | X509_free(ca); |
| 3204 | goto end; |
| 3205 | } |
| 3206 | } |
| 3207 | |
| 3208 | err = ERR_get_error(); |
| 3209 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3210 | /* we successfully reached the last cert in the file */ |
| 3211 | ret = 1; |
| 3212 | } |
| 3213 | ERR_clear_error(); |
| 3214 | |
| 3215 | end: |
| 3216 | if (x) |
| 3217 | X509_free(x); |
| 3218 | |
| 3219 | if (in) |
| 3220 | BIO_free(in); |
| 3221 | |
| 3222 | return ret; |
| 3223 | } |
| 3224 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3225 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3226 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3227 | { |
| 3228 | int ret; |
| 3229 | SSL_CTX *ctx; |
| 3230 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3231 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3232 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3233 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3234 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3235 | return 1; |
| 3236 | } |
| 3237 | |
| 3238 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3239 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 3240 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3241 | SSL_CTX_free(ctx); |
| 3242 | return 1; |
| 3243 | } |
| 3244 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3245 | 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] | 3246 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3247 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 3248 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3249 | if (ret < 0) /* serious error, must do that ourselves */ |
| 3250 | SSL_CTX_free(ctx); |
| 3251 | return 1; |
| 3252 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 3253 | |
| 3254 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3255 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3256 | err && *err ? *err : "", path); |
| 3257 | return 1; |
| 3258 | } |
| 3259 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3260 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3261 | * the tree, so it will be discovered and cleaned in time. |
| 3262 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3263 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3264 | /* store a NULL pointer to indicate we have not yet loaded |
| 3265 | a custom DH param file */ |
| 3266 | if (ssl_dh_ptr_index >= 0) { |
| 3267 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3268 | } |
| 3269 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3270 | ret = ssl_sock_load_dh_params(ctx, path); |
| 3271 | if (ret < 0) { |
| 3272 | if (err) |
| 3273 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3274 | *err ? *err : "", path); |
| 3275 | return 1; |
| 3276 | } |
| 3277 | #endif |
| 3278 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 3279 | #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] | 3280 | ret = ssl_sock_load_ocsp(ctx, path); |
| 3281 | if (ret < 0) { |
| 3282 | if (err) |
| 3283 | 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", |
| 3284 | *err ? *err : "", path); |
| 3285 | return 1; |
| 3286 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3287 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3288 | ssl_sock_set_ocsp_response_from_file(ctx, path); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3289 | #endif |
| 3290 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 3291 | #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] | 3292 | if (sctl_ex_index >= 0) { |
| 3293 | ret = ssl_sock_load_sctl(ctx, path); |
| 3294 | if (ret < 0) { |
| 3295 | if (err) |
| 3296 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 3297 | *err ? *err : "", path); |
| 3298 | return 1; |
| 3299 | } |
| 3300 | } |
| 3301 | #endif |
| 3302 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3303 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3304 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3305 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3306 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3307 | return 1; |
| 3308 | } |
| 3309 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3310 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3311 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3312 | bind_conf->default_ssl_conf = ssl_conf; |
| 3313 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3314 | |
| 3315 | return 0; |
| 3316 | } |
| 3317 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3318 | 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] | 3319 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3320 | struct dirent **de_list; |
| 3321 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3322 | DIR *dir; |
| 3323 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 3324 | char *end; |
| 3325 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3326 | int cfgerr = 0; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3327 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3328 | int is_bundle; |
| 3329 | int j; |
| 3330 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3331 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3332 | if (stat(path, &buf) == 0) { |
| 3333 | dir = opendir(path); |
| 3334 | if (!dir) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3335 | 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] | 3336 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3337 | /* strip trailing slashes, including first one */ |
| 3338 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 3339 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3340 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3341 | n = scandir(path, &de_list, 0, alphasort); |
| 3342 | if (n < 0) { |
| 3343 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 3344 | err && *err ? *err : "", path, strerror(errno)); |
| 3345 | cfgerr++; |
| 3346 | } |
| 3347 | else { |
| 3348 | for (i = 0; i < n; i++) { |
| 3349 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 3350 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3351 | end = strrchr(de->d_name, '.'); |
| 3352 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 3353 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3354 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3355 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 3356 | if (stat(fp, &buf) != 0) { |
| 3357 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3358 | err && *err ? *err : "", fp, strerror(errno)); |
| 3359 | cfgerr++; |
| 3360 | goto ignore_entry; |
| 3361 | } |
| 3362 | if (!S_ISREG(buf.st_mode)) |
| 3363 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3364 | |
| 3365 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3366 | is_bundle = 0; |
| 3367 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 3368 | |
| 3369 | if (end) { |
| 3370 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 3371 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 3372 | is_bundle = 1; |
| 3373 | break; |
| 3374 | } |
| 3375 | } |
| 3376 | |
| 3377 | if (is_bundle) { |
| 3378 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 3379 | int dp_len; |
| 3380 | |
| 3381 | dp_len = end - de->d_name; |
| 3382 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 3383 | |
| 3384 | /* increment i and free de until we get to a non-bundle cert |
| 3385 | * Note here that we look at de_list[i + 1] before freeing de |
| 3386 | * this is important since ignore_entry will free de |
| 3387 | */ |
| 3388 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 3389 | free(de); |
| 3390 | i++; |
| 3391 | de = de_list[i]; |
| 3392 | } |
| 3393 | |
| 3394 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3395 | ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3396 | |
| 3397 | /* Successfully processed the bundle */ |
| 3398 | goto ignore_entry; |
| 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3403 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3404 | ignore_entry: |
| 3405 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3406 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3407 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3408 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3409 | closedir(dir); |
| 3410 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3411 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3412 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3413 | cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3414 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3415 | return cfgerr; |
| 3416 | } |
| 3417 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3418 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3419 | * done once. Zero is returned if the operation fails. No error is returned |
| 3420 | * if the random is said as not implemented, because we expect that openssl |
| 3421 | * will use another method once needed. |
| 3422 | */ |
| 3423 | static int ssl_initialize_random() |
| 3424 | { |
| 3425 | unsigned char random; |
| 3426 | static int random_initialized = 0; |
| 3427 | |
| 3428 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3429 | random_initialized = 1; |
| 3430 | |
| 3431 | return random_initialized; |
| 3432 | } |
| 3433 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3434 | /* release ssl bind conf */ |
| 3435 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3436 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3437 | if (conf) { |
| 3438 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 3439 | free(conf->npn_str); |
| 3440 | conf->npn_str = NULL; |
| 3441 | #endif |
| 3442 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 3443 | free(conf->alpn_str); |
| 3444 | conf->alpn_str = NULL; |
| 3445 | #endif |
| 3446 | free(conf->ca_file); |
| 3447 | conf->ca_file = NULL; |
| 3448 | free(conf->crl_file); |
| 3449 | conf->crl_file = NULL; |
| 3450 | free(conf->ciphers); |
| 3451 | conf->ciphers = NULL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3452 | free(conf->curves); |
| 3453 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3454 | free(conf->ecdhe); |
| 3455 | conf->ecdhe = NULL; |
| 3456 | } |
| 3457 | } |
| 3458 | |
| 3459 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 3460 | { |
| 3461 | char thisline[CRT_LINESIZE]; |
| 3462 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3463 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3464 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3465 | int linenum = 0; |
| 3466 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3467 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3468 | if ((f = fopen(file, "r")) == NULL) { |
| 3469 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3470 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3471 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3472 | |
| 3473 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3474 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3475 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3476 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3477 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3478 | char *crt_path; |
| 3479 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3480 | |
| 3481 | linenum++; |
| 3482 | end = line + strlen(line); |
| 3483 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 3484 | /* Check if we reached the limit and the last char is not \n. |
| 3485 | * Watch out for the last line without the terminating '\n'! |
| 3486 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3487 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 3488 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3489 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3490 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3491 | } |
| 3492 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3493 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3494 | newarg = 1; |
| 3495 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3496 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 3497 | /* end of string, end of loop */ |
| 3498 | *line = 0; |
| 3499 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3500 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3501 | newarg = 1; |
| 3502 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3503 | } else if (*line == '[') { |
| 3504 | if (ssl_b) { |
| 3505 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 3506 | cfgerr = 1; |
| 3507 | break; |
| 3508 | } |
| 3509 | if (!arg) { |
| 3510 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 3511 | cfgerr = 1; |
| 3512 | break; |
| 3513 | } |
| 3514 | ssl_b = arg; |
| 3515 | newarg = 1; |
| 3516 | *line = 0; |
| 3517 | } else if (*line == ']') { |
| 3518 | if (ssl_e) { |
| 3519 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3520 | cfgerr = 1; |
| 3521 | break; |
| 3522 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3523 | if (!ssl_b) { |
| 3524 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 3525 | cfgerr = 1; |
| 3526 | break; |
| 3527 | } |
| 3528 | ssl_e = arg; |
| 3529 | newarg = 1; |
| 3530 | *line = 0; |
| 3531 | } else if (newarg) { |
| 3532 | if (arg == MAX_CRT_ARGS) { |
| 3533 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 3534 | cfgerr = 1; |
| 3535 | break; |
| 3536 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3537 | newarg = 0; |
| 3538 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3539 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3540 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3541 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3542 | if (cfgerr) |
| 3543 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3544 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3545 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3546 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3547 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3548 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3549 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3550 | crt_path = args[0]; |
| 3551 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 3552 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 3553 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 3554 | crt_path, linenum, file); |
| 3555 | cfgerr = 1; |
| 3556 | break; |
| 3557 | } |
| 3558 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 3559 | crt_path = path; |
| 3560 | } |
| 3561 | |
| 3562 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 3563 | cur_arg = ssl_b ? ssl_b : 1; |
| 3564 | while (cur_arg < ssl_e) { |
| 3565 | newarg = 0; |
| 3566 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 3567 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 3568 | newarg = 1; |
| 3569 | cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
| 3570 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 3571 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 3572 | args[cur_arg], linenum, file); |
| 3573 | cfgerr = 1; |
| 3574 | } |
| 3575 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 3576 | break; |
| 3577 | } |
| 3578 | } |
| 3579 | if (!cfgerr && !newarg) { |
| 3580 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 3581 | args[cur_arg], linenum, file); |
| 3582 | cfgerr = 1; |
| 3583 | break; |
| 3584 | } |
| 3585 | } |
| 3586 | if (cfgerr) { |
| 3587 | ssl_sock_free_ssl_conf(ssl_conf); |
| 3588 | free(ssl_conf); |
| 3589 | ssl_conf = NULL; |
| 3590 | break; |
| 3591 | } |
| 3592 | |
| 3593 | if (stat(crt_path, &buf) == 0) { |
| 3594 | cfgerr = ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf, |
| 3595 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3596 | } else { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3597 | cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf, |
| 3598 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3599 | } |
| 3600 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3601 | if (cfgerr) { |
| 3602 | 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] | 3603 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3604 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3605 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3606 | fclose(f); |
| 3607 | return cfgerr; |
| 3608 | } |
| 3609 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3610 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3611 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3612 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3613 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3614 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3615 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3616 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3617 | SSL_OP_NO_SSLv2 | |
| 3618 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3619 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3620 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3621 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
| 3622 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3623 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3624 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3625 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3626 | SSL_MODE_RELEASE_BUFFERS | |
| 3627 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3628 | struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3629 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3630 | int flags = MC_SSL_O_ALL; |
| 3631 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3632 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3633 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3634 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3635 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3636 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
| 3637 | Warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3638 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3639 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3640 | else |
| 3641 | flags = conf_ssl_methods->flags; |
| 3642 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3643 | min = conf_ssl_methods->min; |
| 3644 | max = conf_ssl_methods->max; |
| 3645 | /* start with TLSv10 to remove SSLv3 per default */ |
| 3646 | if (!min && (!max || max >= CONF_TLSV10)) |
| 3647 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3648 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3649 | if (min) |
| 3650 | flags |= (methodVersions[min].flag - 1); |
| 3651 | if (max) |
| 3652 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3653 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3654 | min = max = CONF_TLSV_NONE; |
| 3655 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3656 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3657 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3658 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3659 | if (min) { |
| 3660 | if (hole) { |
| 3661 | 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] | 3662 | "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] | 3663 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3664 | methodVersions[hole].name); |
| 3665 | hole = 0; |
| 3666 | } |
| 3667 | max = i; |
| 3668 | } |
| 3669 | else { |
| 3670 | min = max = i; |
| 3671 | } |
| 3672 | } |
| 3673 | else { |
| 3674 | if (min) |
| 3675 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3676 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3677 | if (!min) { |
| 3678 | 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] | 3679 | 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] | 3680 | cfgerr += 1; |
| 3681 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3682 | /* save real min/max in bind_conf */ |
| 3683 | conf_ssl_methods->min = min; |
| 3684 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3685 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 3686 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3687 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
| 3688 | precautionary measure to avoid any suprise with older openssl version. */ |
| 3689 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3690 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3691 | else |
| 3692 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3693 | if (flags & methodVersions[i].flag) |
| 3694 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3695 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3696 | /* 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] | 3697 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3698 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3699 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3700 | |
| 3701 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3702 | options |= SSL_OP_NO_TICKET; |
| 3703 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3704 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 3705 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3706 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 3707 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3708 | if (global_ssl.async) |
| 3709 | mode |= SSL_MODE_ASYNC; |
| 3710 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3711 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3712 | if (global_ssl.life_time) |
| 3713 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3714 | |
| 3715 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3716 | #ifdef OPENSSL_IS_BORINGSSL |
| 3717 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3718 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3719 | #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 3720 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 3721 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3722 | #else |
| 3723 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3724 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 3725 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3726 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3727 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3728 | } |
| 3729 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3730 | |
| 3731 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 3732 | { |
| 3733 | if (first == block) { |
| 3734 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3735 | if (first->len > 0) |
| 3736 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 3737 | } |
| 3738 | } |
| 3739 | |
| 3740 | /* return first block from sh_ssl_sess */ |
| 3741 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 3742 | { |
| 3743 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 3744 | |
| 3745 | } |
| 3746 | |
| 3747 | /* store a session into the cache |
| 3748 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 3749 | * data: asn1 encoded session |
| 3750 | * data_len: asn1 encoded session length |
| 3751 | * Returns 1 id session was stored (else 0) |
| 3752 | */ |
| 3753 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 3754 | { |
| 3755 | struct shared_block *first; |
| 3756 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 3757 | |
| 3758 | first = shctx_row_reserve_hot(ssl_shctx, data_len + sizeof(struct sh_ssl_sess_hdr)); |
| 3759 | if (!first) { |
| 3760 | /* Could not retrieve enough free blocks to store that session */ |
| 3761 | return 0; |
| 3762 | } |
| 3763 | |
| 3764 | /* STORE the key in the first elem */ |
| 3765 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3766 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 3767 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3768 | |
| 3769 | /* it returns the already existing node |
| 3770 | or current node if none, never returns null */ |
| 3771 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 3772 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 3773 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 3774 | /* release the reserved row */ |
| 3775 | shctx_row_dec_hot(ssl_shctx, first); |
| 3776 | /* replace the previous session already in the tree */ |
| 3777 | sh_ssl_sess = oldsh_ssl_sess; |
| 3778 | /* ignore the previous session data, only use the header */ |
| 3779 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 3780 | shctx_row_inc_hot(ssl_shctx, first); |
| 3781 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3782 | } |
| 3783 | |
| 3784 | if (shctx_row_data_append(ssl_shctx, first, data, data_len) < 0) |
| 3785 | return 0; |
| 3786 | |
| 3787 | return 1; |
| 3788 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3789 | |
| 3790 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3791 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3792 | { |
| 3793 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 3794 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 3795 | unsigned char *p; |
| 3796 | int data_len; |
| 3797 | unsigned int sid_length, sid_ctx_length; |
| 3798 | const unsigned char *sid_data; |
| 3799 | const unsigned char *sid_ctx_data; |
| 3800 | |
| 3801 | /* Session id is already stored in to key and session id is known |
| 3802 | * so we dont store it to keep size. |
| 3803 | */ |
| 3804 | |
| 3805 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 3806 | sid_ctx_data = SSL_SESSION_get0_id_context(sess, &sid_ctx_length); |
| 3807 | SSL_SESSION_set1_id(sess, sid_data, 0); |
| 3808 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, 0); |
| 3809 | |
| 3810 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 3811 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 3812 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 3813 | goto err; |
| 3814 | |
| 3815 | p = encsess; |
| 3816 | |
| 3817 | /* process ASN1 session encoding before the lock */ |
| 3818 | i2d_SSL_SESSION(sess, &p); |
| 3819 | |
| 3820 | memcpy(encid, sid_data, sid_length); |
| 3821 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 3822 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 3823 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3824 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3825 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3826 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3827 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3828 | err: |
| 3829 | /* reset original length values */ |
| 3830 | SSL_SESSION_set1_id(sess, sid_data, sid_length); |
| 3831 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, sid_ctx_length); |
| 3832 | |
| 3833 | return 0; /* do not increment session reference count */ |
| 3834 | } |
| 3835 | |
| 3836 | /* SSL callback used on lookup an existing session cause none found in internal cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3837 | SSL_SESSION *sh_ssl_sess_get_cb(SSL *ssl, __OPENSSL_110_CONST__ unsigned char *key, int key_len, int *do_copy) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3838 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3839 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3840 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 3841 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3842 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3843 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3844 | |
| 3845 | global.shctx_lookups++; |
| 3846 | |
| 3847 | /* allow the session to be freed automatically by openssl */ |
| 3848 | *do_copy = 0; |
| 3849 | |
| 3850 | /* tree key is zeros padded sessionid */ |
| 3851 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3852 | memcpy(tmpkey, key, key_len); |
| 3853 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 3854 | key = tmpkey; |
| 3855 | } |
| 3856 | |
| 3857 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3858 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3859 | |
| 3860 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3861 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 3862 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3863 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3864 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3865 | global.shctx_misses++; |
| 3866 | return NULL; |
| 3867 | } |
| 3868 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3869 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 3870 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3871 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3872 | shctx_row_data_get(ssl_shctx, first, data, sizeof(struct sh_ssl_sess_hdr), first->len-sizeof(struct sh_ssl_sess_hdr)); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3873 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3874 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3875 | |
| 3876 | /* decode ASN1 session */ |
| 3877 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3878 | sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr)); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3879 | /* Reset session id and session id contenxt */ |
| 3880 | if (sess) { |
| 3881 | SSL_SESSION_set1_id(sess, key, key_len); |
| 3882 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 3883 | } |
| 3884 | |
| 3885 | return sess; |
| 3886 | } |
| 3887 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3888 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3889 | /* SSL callback used to signal session is no more used in internal cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3890 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3891 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3892 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3893 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 3894 | unsigned int sid_length; |
| 3895 | const unsigned char *sid_data; |
| 3896 | (void)ctx; |
| 3897 | |
| 3898 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 3899 | /* tree key is zeros padded sessionid */ |
| 3900 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3901 | memcpy(tmpkey, sid_data, sid_length); |
| 3902 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 3903 | sid_data = tmpkey; |
| 3904 | } |
| 3905 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3906 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3907 | |
| 3908 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3909 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 3910 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3911 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3912 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3913 | } |
| 3914 | |
| 3915 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3916 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3917 | } |
| 3918 | |
| 3919 | /* Set session cache mode to server and disable openssl internal cache. |
| 3920 | * Set shared cache callbacks on an ssl context. |
| 3921 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3922 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3923 | { |
| 3924 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 3925 | |
| 3926 | if (!ssl_shctx) { |
| 3927 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 3928 | return; |
| 3929 | } |
| 3930 | |
| 3931 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 3932 | SSL_SESS_CACHE_NO_INTERNAL | |
| 3933 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 3934 | |
| 3935 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3936 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 3937 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 3938 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3939 | } |
| 3940 | |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3941 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
| 3942 | { |
| 3943 | struct proxy *curproxy = bind_conf->frontend; |
| 3944 | int cfgerr = 0; |
| 3945 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3946 | struct ssl_bind_conf *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3947 | const char *conf_ciphers; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3948 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3949 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3950 | if (ssl_conf) { |
| 3951 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 3952 | int i, min, max; |
| 3953 | int flags = MC_SSL_O_ALL; |
| 3954 | |
| 3955 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3956 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 3957 | max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3958 | if (min) |
| 3959 | flags |= (methodVersions[min].flag - 1); |
| 3960 | if (max) |
| 3961 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 3962 | min = max = CONF_TLSV_NONE; |
| 3963 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3964 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 3965 | if (min) |
| 3966 | max = i; |
| 3967 | else |
| 3968 | min = max = i; |
| 3969 | } |
| 3970 | /* save real min/max */ |
| 3971 | conf_ssl_methods->min = min; |
| 3972 | conf_ssl_methods->max = max; |
| 3973 | if (!min) { |
| 3974 | Alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3975 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3976 | cfgerr += 1; |
| 3977 | } |
| 3978 | } |
| 3979 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3980 | 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] | 3981 | case SSL_SOCK_VERIFY_NONE: |
| 3982 | verify = SSL_VERIFY_NONE; |
| 3983 | break; |
| 3984 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 3985 | verify = SSL_VERIFY_PEER; |
| 3986 | break; |
| 3987 | case SSL_SOCK_VERIFY_REQUIRED: |
| 3988 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 3989 | break; |
| 3990 | } |
| 3991 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 3992 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3993 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 3994 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 3995 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3996 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3997 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3998 | 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] | 3999 | 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] | 4000 | cfgerr++; |
| 4001 | } |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 4002 | if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) { |
| 4003 | /* set CA names for client cert request, function returns void */ |
| 4004 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file)); |
| 4005 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4006 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4007 | else { |
| 4008 | Alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4009 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4010 | cfgerr++; |
| 4011 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4012 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4013 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4014 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4015 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4016 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4017 | 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] | 4018 | 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] | 4019 | cfgerr++; |
| 4020 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4021 | else { |
| 4022 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4023 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4024 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4025 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4026 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4027 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4028 | #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] | 4029 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4030 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
| 4031 | Alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4032 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4033 | cfgerr++; |
| 4034 | } |
| 4035 | } |
| 4036 | #endif |
| 4037 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4038 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4039 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4040 | if (conf_ciphers && |
| 4041 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4042 | 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] | 4043 | 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] | 4044 | cfgerr++; |
| 4045 | } |
| 4046 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4047 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4048 | /* If tune.ssl.default-dh-param has not been set, |
| 4049 | neither has ssl-default-dh-file and no static DH |
| 4050 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4051 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4052 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4053 | (ssl_dh_ptr_index == -1 || |
| 4054 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4055 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 4056 | const SSL_CIPHER * cipher = NULL; |
| 4057 | char cipher_description[128]; |
| 4058 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 4059 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 4060 | which is not ephemeral DH. */ |
| 4061 | const char dhe_description[] = " Kx=DH "; |
| 4062 | const char dhe_export_description[] = " Kx=DH("; |
| 4063 | int idx = 0; |
| 4064 | int dhe_found = 0; |
| 4065 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4066 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4067 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4068 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4069 | if (ssl) { |
| 4070 | ciphers = SSL_get_ciphers(ssl); |
| 4071 | |
| 4072 | if (ciphers) { |
| 4073 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 4074 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 4075 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 4076 | if (strstr(cipher_description, dhe_description) != NULL || |
| 4077 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 4078 | dhe_found = 1; |
| 4079 | break; |
| 4080 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 4081 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4082 | } |
| 4083 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4084 | SSL_free(ssl); |
| 4085 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4086 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4087 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4088 | if (dhe_found) { |
| 4089 | 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] | 4090 | } |
| 4091 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4092 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4093 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4094 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4095 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4096 | if (local_dh_1024 == NULL) { |
| 4097 | local_dh_1024 = ssl_get_dh_1024(); |
| 4098 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4099 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4100 | if (local_dh_2048 == NULL) { |
| 4101 | local_dh_2048 = ssl_get_dh_2048(); |
| 4102 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4103 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4104 | if (local_dh_4096 == NULL) { |
| 4105 | local_dh_4096 = ssl_get_dh_4096(); |
| 4106 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4107 | } |
| 4108 | } |
| 4109 | } |
| 4110 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4111 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4112 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4113 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4114 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4115 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4116 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 4117 | #ifdef OPENSSL_NPN_NEGOTIATED |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4118 | ssl_conf_cur = NULL; |
| 4119 | if (ssl_conf && ssl_conf->npn_str) |
| 4120 | ssl_conf_cur = ssl_conf; |
| 4121 | else if (bind_conf->ssl_conf.npn_str) |
| 4122 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4123 | if (ssl_conf_cur) |
| 4124 | 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] | 4125 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4126 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4127 | ssl_conf_cur = NULL; |
| 4128 | if (ssl_conf && ssl_conf->alpn_str) |
| 4129 | ssl_conf_cur = ssl_conf; |
| 4130 | else if (bind_conf->ssl_conf.alpn_str) |
| 4131 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4132 | if (ssl_conf_cur) |
| 4133 | 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] | 4134 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4135 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 4136 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4137 | if (conf_curves) { |
| 4138 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
| 4139 | Alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4140 | curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4141 | cfgerr++; |
| 4142 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4143 | #if defined(SSL_CTX_set_ecdh_auto) |
| 4144 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
| 4145 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4146 | } |
| 4147 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4148 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4149 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4150 | int i; |
| 4151 | EC_KEY *ecdh; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4152 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4153 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4154 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4155 | NULL); |
| 4156 | |
| 4157 | if (ecdhe == NULL) { |
| 4158 | SSL_CTX_set_dh_auto(ctx, 1); |
| 4159 | return cfgerr; |
| 4160 | } |
| 4161 | #else |
| 4162 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4163 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4164 | ECDHE_DEFAULT_CURVE); |
| 4165 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4166 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4167 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4168 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
| 4169 | 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] | 4170 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4171 | cfgerr++; |
| 4172 | } |
| 4173 | else { |
| 4174 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4175 | EC_KEY_free(ecdh); |
| 4176 | } |
| 4177 | } |
| 4178 | #endif |
| 4179 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4180 | return cfgerr; |
| 4181 | } |
| 4182 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4183 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4184 | { |
| 4185 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4186 | size_t prefixlen, suffixlen; |
| 4187 | |
| 4188 | /* Trivial case */ |
| 4189 | if (strcmp(pattern, hostname) == 0) |
| 4190 | return 1; |
| 4191 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4192 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4193 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4194 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4195 | pattern_wildcard = NULL; |
| 4196 | pattern_left_label_end = pattern; |
| 4197 | while (*pattern_left_label_end != '.') { |
| 4198 | switch (*pattern_left_label_end) { |
| 4199 | case 0: |
| 4200 | /* End of label not found */ |
| 4201 | return 0; |
| 4202 | case '*': |
| 4203 | /* If there is more than one wildcards */ |
| 4204 | if (pattern_wildcard) |
| 4205 | return 0; |
| 4206 | pattern_wildcard = pattern_left_label_end; |
| 4207 | break; |
| 4208 | } |
| 4209 | pattern_left_label_end++; |
| 4210 | } |
| 4211 | |
| 4212 | /* If it's not trivial and there is no wildcard, it can't |
| 4213 | * match */ |
| 4214 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4215 | return 0; |
| 4216 | |
| 4217 | /* Make sure all labels match except the leftmost */ |
| 4218 | hostname_left_label_end = strchr(hostname, '.'); |
| 4219 | if (!hostname_left_label_end |
| 4220 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 4221 | return 0; |
| 4222 | |
| 4223 | /* Make sure the leftmost label of the hostname is long enough |
| 4224 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4225 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4226 | return 0; |
| 4227 | |
| 4228 | /* Finally compare the string on either side of the |
| 4229 | * wildcard */ |
| 4230 | prefixlen = pattern_wildcard - pattern; |
| 4231 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4232 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 4233 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4234 | return 0; |
| 4235 | |
| 4236 | return 1; |
| 4237 | } |
| 4238 | |
| 4239 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4240 | { |
| 4241 | SSL *ssl; |
| 4242 | struct connection *conn; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4243 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4244 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4245 | |
| 4246 | int depth; |
| 4247 | X509 *cert; |
| 4248 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4249 | int i; |
| 4250 | X509_NAME *cert_subject; |
| 4251 | char *str; |
| 4252 | |
| 4253 | if (ok == 0) |
| 4254 | return ok; |
| 4255 | |
| 4256 | 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] | 4257 | conn = SSL_get_app_data(ssl); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4258 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4259 | /* We're checking if the provided hostnames match the desired one. The |
| 4260 | * desired hostname comes from the SNI we presented if any, or if not |
| 4261 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4262 | * directive. If neither is set, we don't care about the name so the |
| 4263 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4264 | */ |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4265 | servername = SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4266 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4267 | if (!servername) { |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4268 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4269 | if (!servername) |
| 4270 | return ok; |
| 4271 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4272 | |
| 4273 | /* We only need to verify the CN on the actual server cert, |
| 4274 | * not the indirect CAs */ |
| 4275 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4276 | if (depth != 0) |
| 4277 | return ok; |
| 4278 | |
| 4279 | /* At this point, the cert is *not* OK unless we can find a |
| 4280 | * hostname match */ |
| 4281 | ok = 0; |
| 4282 | |
| 4283 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4284 | /* It seems like this might happen if verify peer isn't set */ |
| 4285 | if (!cert) |
| 4286 | return ok; |
| 4287 | |
| 4288 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4289 | if (alt_names) { |
| 4290 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4291 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4292 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4293 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 4294 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4295 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4296 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4297 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4298 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4299 | OPENSSL_free(str); |
| 4300 | } |
| 4301 | } |
| 4302 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4303 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4304 | } |
| 4305 | |
| 4306 | cert_subject = X509_get_subject_name(cert); |
| 4307 | i = -1; |
| 4308 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4309 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4310 | ASN1_STRING *value; |
| 4311 | value = X509_NAME_ENTRY_get_data(entry); |
| 4312 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4313 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4314 | OPENSSL_free(str); |
| 4315 | } |
| 4316 | } |
| 4317 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4318 | /* report the mismatch and indicate if SNI was used or not */ |
| 4319 | if (!ok && !conn->err_code) |
| 4320 | conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4321 | return ok; |
| 4322 | } |
| 4323 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4324 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4325 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4326 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4327 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4328 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4329 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4330 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4331 | SSL_OP_NO_SSLv2 | |
| 4332 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4333 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4334 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4335 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4336 | SSL_MODE_RELEASE_BUFFERS | |
| 4337 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4338 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4339 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4340 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4341 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4342 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4343 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4344 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4345 | if (!ssl_initialize_random()) { |
| 4346 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 4347 | cfgerr++; |
| 4348 | } |
| 4349 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4350 | /* Automatic memory computations need to know we use SSL there */ |
| 4351 | global.ssl_used_backend = 1; |
| 4352 | |
| 4353 | /* Initiate SSL context for current server */ |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4354 | srv->ssl_ctx.reused_sess = NULL; |
| 4355 | if (srv->use_ssl) |
| 4356 | srv->xprt = &ssl_sock; |
| 4357 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 4358 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4359 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4360 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4361 | if (!ctx) { |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4362 | Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 4363 | proxy_type_str(curproxy), curproxy->id, |
| 4364 | srv->id); |
| 4365 | cfgerr++; |
| 4366 | return cfgerr; |
| 4367 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4368 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4369 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
| 4370 | Warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 4371 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4372 | proxy_type_str(curproxy), curproxy->id, srv->id); |
| 4373 | else |
| 4374 | flags = conf_ssl_methods->flags; |
| 4375 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4376 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 4377 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4378 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4379 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4380 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4381 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4382 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4383 | min = max = CONF_TLSV_NONE; |
| 4384 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4385 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4386 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4387 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4388 | if (min) { |
| 4389 | if (hole) { |
| 4390 | 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] | 4391 | "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] | 4392 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4393 | methodVersions[hole].name); |
| 4394 | hole = 0; |
| 4395 | } |
| 4396 | max = i; |
| 4397 | } |
| 4398 | else { |
| 4399 | min = max = i; |
| 4400 | } |
| 4401 | } |
| 4402 | else { |
| 4403 | if (min) |
| 4404 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4405 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4406 | if (!min) { |
| 4407 | Alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4408 | proxy_type_str(curproxy), curproxy->id, srv->id); |
| 4409 | cfgerr += 1; |
| 4410 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4411 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 4412 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4413 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
| 4414 | precautionary measure to avoid any suprise with older openssl version. */ |
| 4415 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4416 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4417 | else |
| 4418 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4419 | if (flags & methodVersions[i].flag) |
| 4420 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4421 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4422 | /* 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] | 4423 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4424 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4425 | #endif |
| 4426 | |
| 4427 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4428 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4429 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4430 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 4431 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4432 | if (global_ssl.async) |
| 4433 | mode |= SSL_MODE_ASYNC; |
| 4434 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4435 | SSL_CTX_set_mode(ctx, mode); |
| 4436 | srv->ssl_ctx.ctx = ctx; |
| 4437 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4438 | if (srv->ssl_ctx.client_crt) { |
| 4439 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
| 4440 | Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4441 | proxy_type_str(curproxy), curproxy->id, |
| 4442 | srv->id, srv->ssl_ctx.client_crt); |
| 4443 | cfgerr++; |
| 4444 | } |
| 4445 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
| 4446 | Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4447 | proxy_type_str(curproxy), curproxy->id, |
| 4448 | srv->id, srv->ssl_ctx.client_crt); |
| 4449 | cfgerr++; |
| 4450 | } |
| 4451 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
| 4452 | Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4453 | proxy_type_str(curproxy), curproxy->id, |
| 4454 | srv->id, srv->ssl_ctx.client_crt); |
| 4455 | cfgerr++; |
| 4456 | } |
| 4457 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4458 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4459 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4460 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4461 | switch (srv->ssl_ctx.verify) { |
| 4462 | case SSL_SOCK_VERIFY_NONE: |
| 4463 | verify = SSL_VERIFY_NONE; |
| 4464 | break; |
| 4465 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4466 | verify = SSL_VERIFY_PEER; |
| 4467 | break; |
| 4468 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4469 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4470 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4471 | (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4472 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4473 | if (srv->ssl_ctx.ca_file) { |
| 4474 | /* load CAfile to verify */ |
| 4475 | 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] | 4476 | 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] | 4477 | curproxy->id, srv->id, |
| 4478 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
| 4479 | cfgerr++; |
| 4480 | } |
| 4481 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4482 | else { |
| 4483 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 4484 | 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] | 4485 | curproxy->id, srv->id, |
| 4486 | srv->conf.file, srv->conf.line); |
| 4487 | else |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 4488 | 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] | 4489 | curproxy->id, srv->id, |
| 4490 | srv->conf.file, srv->conf.line); |
| 4491 | cfgerr++; |
| 4492 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4493 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4494 | if (srv->ssl_ctx.crl_file) { |
| 4495 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4496 | |
| 4497 | 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] | 4498 | 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] | 4499 | curproxy->id, srv->id, |
| 4500 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
| 4501 | cfgerr++; |
| 4502 | } |
| 4503 | else { |
| 4504 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4505 | } |
| 4506 | } |
| 4507 | #endif |
| 4508 | } |
| 4509 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4510 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF); |
| 4511 | if (srv->ssl_ctx.ciphers && |
| 4512 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
| 4513 | Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4514 | curproxy->id, srv->id, |
| 4515 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
| 4516 | cfgerr++; |
| 4517 | } |
| 4518 | |
| 4519 | return cfgerr; |
| 4520 | } |
| 4521 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4522 | /* 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] | 4523 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4524 | * encountered. |
| 4525 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4526 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4527 | { |
| 4528 | struct ebmb_node *node; |
| 4529 | struct sni_ctx *sni; |
| 4530 | int err = 0; |
| 4531 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4532 | /* Automatic memory computations need to know we use SSL there */ |
| 4533 | global.ssl_used_frontend = 1; |
| 4534 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4535 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4536 | if (!ssl_initialize_random()) { |
| 4537 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 4538 | err++; |
| 4539 | } |
| 4540 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4541 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4542 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4543 | /* It should not be necessary to call this function, but it's |
| 4544 | necessary first to check and move all initialisation related |
| 4545 | to initial_ctx in ssl_sock_initial_ctx. */ |
| 4546 | err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx); |
| 4547 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4548 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4549 | 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] | 4550 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4551 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4552 | while (node) { |
| 4553 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4554 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4555 | /* only initialize the CTX on its first occurrence and |
| 4556 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4557 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4558 | node = ebmb_next(node); |
| 4559 | } |
| 4560 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4561 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4562 | while (node) { |
| 4563 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4564 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4565 | /* only initialize the CTX on its first occurrence and |
| 4566 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4567 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4568 | node = ebmb_next(node); |
| 4569 | } |
| 4570 | return err; |
| 4571 | } |
| 4572 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4573 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4574 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4575 | * alerts are directly emitted since the rest of the stack does it below. |
| 4576 | */ |
| 4577 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4578 | { |
| 4579 | struct proxy *px = bind_conf->frontend; |
| 4580 | int alloc_ctx; |
| 4581 | int err; |
| 4582 | |
| 4583 | if (!bind_conf->is_ssl) { |
| 4584 | if (bind_conf->default_ctx) { |
| 4585 | Warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 4586 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4587 | } |
| 4588 | return 0; |
| 4589 | } |
| 4590 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4591 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
| 4592 | Warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 4593 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4594 | } |
| 4595 | else { |
| 4596 | Alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 4597 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4598 | return -1; |
| 4599 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4600 | } |
| 4601 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4602 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, sizeof(*sh_ssl_sess_tree), (!global_ssl.private_cache && (global.nbproc > 1)) ? 1 : 0); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4603 | if (alloc_ctx < 0) { |
| 4604 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 4605 | 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"); |
| 4606 | else |
| 4607 | Alert("Unable to allocate SSL session cache.\n"); |
| 4608 | return -1; |
| 4609 | } |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4610 | /* free block callback */ |
| 4611 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 4612 | /* init the root tree within the extra space */ |
| 4613 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 4614 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4615 | |
| 4616 | err = 0; |
| 4617 | /* initialize all certificate contexts */ |
| 4618 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 4619 | |
| 4620 | /* initialize CA variables if the certificates generation is enabled */ |
| 4621 | err += ssl_sock_load_ca(bind_conf); |
| 4622 | |
| 4623 | return -err; |
| 4624 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4625 | |
| 4626 | /* release ssl context allocated for servers. */ |
| 4627 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 4628 | { |
| 4629 | if (srv->ssl_ctx.ctx) |
| 4630 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 4631 | } |
| 4632 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4633 | /* 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] | 4634 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 4635 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4636 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4637 | { |
| 4638 | struct ebmb_node *node, *back; |
| 4639 | struct sni_ctx *sni; |
| 4640 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4641 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4642 | while (node) { |
| 4643 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4644 | back = ebmb_next(node); |
| 4645 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4646 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4647 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4648 | ssl_sock_free_ssl_conf(sni->conf); |
| 4649 | free(sni->conf); |
| 4650 | sni->conf = NULL; |
| 4651 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4652 | free(sni); |
| 4653 | node = back; |
| 4654 | } |
| 4655 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4656 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4657 | while (node) { |
| 4658 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4659 | back = ebmb_next(node); |
| 4660 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4661 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4662 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4663 | ssl_sock_free_ssl_conf(sni->conf); |
| 4664 | free(sni->conf); |
| 4665 | sni->conf = NULL; |
| 4666 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4667 | free(sni); |
| 4668 | node = back; |
| 4669 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4670 | SSL_CTX_free(bind_conf->initial_ctx); |
| 4671 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4672 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4673 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4674 | } |
| 4675 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4676 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 4677 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 4678 | { |
| 4679 | ssl_sock_free_ca(bind_conf); |
| 4680 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4681 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4682 | free(bind_conf->ca_sign_file); |
| 4683 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4684 | if (bind_conf->keys_ref) { |
| 4685 | free(bind_conf->keys_ref->filename); |
| 4686 | free(bind_conf->keys_ref->tlskeys); |
| 4687 | LIST_DEL(&bind_conf->keys_ref->list); |
| 4688 | free(bind_conf->keys_ref); |
| 4689 | } |
| 4690 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4691 | bind_conf->ca_sign_pass = NULL; |
| 4692 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4693 | } |
| 4694 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4695 | /* Load CA cert file and private key used to generate certificates */ |
| 4696 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4697 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4698 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4699 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4700 | FILE *fp; |
| 4701 | X509 *cacert = NULL; |
| 4702 | EVP_PKEY *capkey = NULL; |
| 4703 | int err = 0; |
| 4704 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 4705 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4706 | return err; |
| 4707 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4708 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4709 | if (global_ssl.ctx_cache) |
| 4710 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4711 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 4712 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4713 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4714 | if (!bind_conf->ca_sign_file) { |
| 4715 | Alert("Proxy '%s': cannot enable certificate generation, " |
| 4716 | "no CA certificate File configured at [%s:%d].\n", |
| 4717 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4718 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4719 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4720 | |
| 4721 | /* read in the CA certificate */ |
| 4722 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
| 4723 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4724 | 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] | 4725 | goto load_error; |
| 4726 | } |
| 4727 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
| 4728 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4729 | 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] | 4730 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4731 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4732 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4733 | if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) { |
| 4734 | Alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 4735 | 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] | 4736 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4737 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4738 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4739 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4740 | bind_conf->ca_sign_cert = cacert; |
| 4741 | bind_conf->ca_sign_pkey = capkey; |
| 4742 | return err; |
| 4743 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4744 | read_error: |
| 4745 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4746 | if (capkey) EVP_PKEY_free(capkey); |
| 4747 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4748 | load_error: |
| 4749 | bind_conf->generate_certs = 0; |
| 4750 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4751 | return err; |
| 4752 | } |
| 4753 | |
| 4754 | /* Release CA cert and private key used to generate certificated */ |
| 4755 | void |
| 4756 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 4757 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4758 | if (bind_conf->ca_sign_pkey) |
| 4759 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 4760 | if (bind_conf->ca_sign_cert) |
| 4761 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 4762 | bind_conf->ca_sign_pkey = NULL; |
| 4763 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4764 | } |
| 4765 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4766 | /* |
| 4767 | * This function is called if SSL * context is not yet allocated. The function |
| 4768 | * is designed to be called before any other data-layer operation and sets the |
| 4769 | * handshake flag on the connection. It is safe to call it multiple times. |
| 4770 | * It returns 0 on success and -1 in error case. |
| 4771 | */ |
| 4772 | static int ssl_sock_init(struct connection *conn) |
| 4773 | { |
| 4774 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4775 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4776 | return 0; |
| 4777 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4778 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4779 | return 0; |
| 4780 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4781 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 4782 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4783 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4784 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4785 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4786 | /* If it is in client mode initiate SSL session |
| 4787 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4788 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4789 | int may_retry = 1; |
| 4790 | |
| 4791 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4792 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4793 | conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4794 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4795 | if (may_retry--) { |
| 4796 | pool_gc2(); |
| 4797 | goto retry_connect; |
| 4798 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4799 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4800 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4801 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4802 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4803 | /* set fd on SSL session context */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 4804 | if (!SSL_set_fd(conn->xprt_ctx, conn->handle.fd)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4805 | SSL_free(conn->xprt_ctx); |
| 4806 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4807 | if (may_retry--) { |
| 4808 | pool_gc2(); |
| 4809 | goto retry_connect; |
| 4810 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4811 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4812 | return -1; |
| 4813 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4814 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4815 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4816 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 4817 | SSL_free(conn->xprt_ctx); |
| 4818 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4819 | if (may_retry--) { |
| 4820 | pool_gc2(); |
| 4821 | goto retry_connect; |
| 4822 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4823 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4824 | return -1; |
| 4825 | } |
| 4826 | |
| 4827 | SSL_set_connect_state(conn->xprt_ctx); |
| 4828 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 4829 | if(!SSL_set_session(conn->xprt_ctx, objt_server(conn->target)->ssl_ctx.reused_sess)) { |
| 4830 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 4831 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 4832 | } |
| 4833 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4834 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4835 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4836 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4837 | |
| 4838 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 4839 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4840 | return 0; |
| 4841 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4842 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4843 | int may_retry = 1; |
| 4844 | |
| 4845 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4846 | /* Alloc a new SSL session ctx */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4847 | 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] | 4848 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4849 | if (may_retry--) { |
| 4850 | pool_gc2(); |
| 4851 | goto retry_accept; |
| 4852 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4853 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4854 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4855 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4856 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4857 | /* set fd on SSL session context */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 4858 | if (!SSL_set_fd(conn->xprt_ctx, conn->handle.fd)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4859 | SSL_free(conn->xprt_ctx); |
| 4860 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4861 | if (may_retry--) { |
| 4862 | pool_gc2(); |
| 4863 | goto retry_accept; |
| 4864 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4865 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4866 | return -1; |
| 4867 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4868 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4869 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4870 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 4871 | SSL_free(conn->xprt_ctx); |
| 4872 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4873 | if (may_retry--) { |
| 4874 | pool_gc2(); |
| 4875 | goto retry_accept; |
| 4876 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4877 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4878 | return -1; |
| 4879 | } |
| 4880 | |
| 4881 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4882 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4883 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4884 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4885 | #if OPENSSL_VERSION_NUMBER >= 0x0101000L |
| 4886 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 4887 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4888 | |
| 4889 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 4890 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4891 | return 0; |
| 4892 | } |
| 4893 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4894 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4895 | return -1; |
| 4896 | } |
| 4897 | |
| 4898 | |
| 4899 | /* This is the callback which is used when an SSL handshake is pending. It |
| 4900 | * updates the FD status if it wants some polling before being called again. |
| 4901 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 4902 | * otherwise it returns non-zero and removes itself from the connection's |
| 4903 | * flags (the bit is provided in <flag> by the caller). |
| 4904 | */ |
| 4905 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 4906 | { |
| 4907 | int ret; |
| 4908 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4909 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4910 | return 0; |
| 4911 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4912 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4913 | goto out_error; |
| 4914 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4915 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L |
| 4916 | /* |
| 4917 | * Check if we have early data. If we do, we have to read them |
| 4918 | * before SSL_do_handshake() is called, And there's no way to |
| 4919 | * detect early data, except to try to read them |
| 4920 | */ |
| 4921 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 4922 | size_t read_data; |
| 4923 | |
| 4924 | ret = SSL_read_early_data(conn->xprt_ctx, &conn->tmp_early_data, |
| 4925 | 1, &read_data); |
| 4926 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 4927 | goto check_error; |
| 4928 | if (ret == SSL_READ_EARLY_DATA_SUCCESS) { |
| 4929 | conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN); |
| 4930 | return 1; |
| 4931 | } else |
| 4932 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 4933 | } |
| 4934 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4935 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 4936 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 4937 | * Usually SSL_write and SSL_read are used and process implicitly |
| 4938 | * the reneg handshake. |
| 4939 | * Here we use SSL_peek as a workaround for reneg. |
| 4940 | */ |
| 4941 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4942 | char c; |
| 4943 | |
| 4944 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 4945 | if (ret <= 0) { |
| 4946 | /* handshake may have not been completed, let's find why */ |
| 4947 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4948 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4949 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 4950 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 4951 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4952 | __conn_sock_want_send(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 4953 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4954 | return 0; |
| 4955 | } |
| 4956 | else if (ret == SSL_ERROR_WANT_READ) { |
| 4957 | /* handshake may have been completed but we have |
| 4958 | * no more data to read. |
| 4959 | */ |
| 4960 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4961 | ret = 1; |
| 4962 | goto reneg_ok; |
| 4963 | } |
| 4964 | /* SSL handshake needs to read, L4 connection is ready */ |
| 4965 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 4966 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4967 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4968 | __conn_sock_want_recv(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 4969 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4970 | return 0; |
| 4971 | } |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 4972 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4973 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 4974 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4975 | return 0; |
| 4976 | } |
| 4977 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4978 | else if (ret == SSL_ERROR_SYSCALL) { |
| 4979 | /* if errno is null, then connection was successfully established */ |
| 4980 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 4981 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4982 | if (!conn->err_code) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4983 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 4984 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4985 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4986 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 4987 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4988 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 4989 | empty_handshake = state == TLS_ST_BEFORE; |
| 4990 | #else |
| 4991 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
| 4992 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4993 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4994 | if (!errno) { |
| 4995 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4996 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4997 | else |
| 4998 | conn->err_code = CO_ER_SSL_EMPTY; |
| 4999 | } |
| 5000 | else { |
| 5001 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5002 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5003 | else |
| 5004 | conn->err_code = CO_ER_SSL_ABORT; |
| 5005 | } |
| 5006 | } |
| 5007 | else { |
| 5008 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5009 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5010 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5011 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5012 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5013 | #endif |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5014 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5015 | goto out_error; |
| 5016 | } |
| 5017 | else { |
| 5018 | /* Fail on all other handshake errors */ |
| 5019 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5020 | * buffer, causing an RST to be emitted upon close() on |
| 5021 | * TCP sockets. We first try to drain possibly pending |
| 5022 | * data to avoid this as much as possible. |
| 5023 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5024 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5025 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5026 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 5027 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5028 | goto out_error; |
| 5029 | } |
| 5030 | } |
| 5031 | /* read some data: consider handshake completed */ |
| 5032 | goto reneg_ok; |
| 5033 | } |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5034 | ret = SSL_do_handshake(conn->xprt_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5035 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5036 | if (ret != 1) { |
| 5037 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5038 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5039 | |
| 5040 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5041 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 5042 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5043 | __conn_sock_want_send(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5044 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5045 | return 0; |
| 5046 | } |
| 5047 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5048 | /* SSL handshake needs to read, L4 connection is ready */ |
| 5049 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 5050 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5051 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5052 | __conn_sock_want_recv(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5053 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5054 | return 0; |
| 5055 | } |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5056 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5057 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5058 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5059 | return 0; |
| 5060 | } |
| 5061 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5062 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5063 | /* if errno is null, then connection was successfully established */ |
| 5064 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5065 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5066 | if (!conn->err_code) { |
| 5067 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 5068 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5069 | #else |
| 5070 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 5071 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5072 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 5073 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5074 | #else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5075 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5076 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5077 | if (empty_handshake) { |
| 5078 | if (!errno) { |
| 5079 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5080 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5081 | else |
| 5082 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5083 | } |
| 5084 | else { |
| 5085 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5086 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5087 | else |
| 5088 | conn->err_code = CO_ER_SSL_ABORT; |
| 5089 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5090 | } |
| 5091 | else { |
| 5092 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5093 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5094 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5095 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5096 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5097 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5098 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5099 | goto out_error; |
| 5100 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5101 | else { |
| 5102 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5103 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5104 | * buffer, causing an RST to be emitted upon close() on |
| 5105 | * TCP sockets. We first try to drain possibly pending |
| 5106 | * data to avoid this as much as possible. |
| 5107 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5108 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5109 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5110 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 5111 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5112 | goto out_error; |
| 5113 | } |
| 5114 | } |
| 5115 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5116 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5117 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5118 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5119 | /* ASYNC engine API doesn't support moving read/write |
| 5120 | * buffers. So we disable ASYNC mode right after |
| 5121 | * the handshake to avoid buffer oveflows. |
| 5122 | */ |
| 5123 | if (global_ssl.async) |
| 5124 | SSL_clear_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5125 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5126 | /* Handshake succeeded */ |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5127 | if (!SSL_session_reused(conn->xprt_ctx)) { |
| 5128 | if (objt_server(conn->target)) { |
| 5129 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5130 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5131 | global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr; |
| 5132 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5133 | /* 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] | 5134 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5135 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
Willy Tarreau | 30fd4bd | 2016-12-22 21:54:21 +0100 | [diff] [blame] | 5136 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 5137 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5138 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 5139 | if (!(objt_server(conn->target)->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) |
| 5140 | 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] | 5141 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5142 | else { |
| 5143 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5144 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5145 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5146 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5147 | } |
| 5148 | |
| 5149 | /* The connection is now established at both layers, it's time to leave */ |
| 5150 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5151 | return 1; |
| 5152 | |
| 5153 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5154 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5155 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5156 | ERR_clear_error(); |
| 5157 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5158 | /* free resumed session if exists */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5159 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 5160 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 5161 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5162 | } |
| 5163 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5164 | /* Fail on all other handshake errors */ |
| 5165 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5166 | if (!conn->err_code) |
| 5167 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5168 | return 0; |
| 5169 | } |
| 5170 | |
| 5171 | /* 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] | 5172 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5173 | * buffer wraps, in which case a second call may be performed. The connection's |
| 5174 | * flags are updated with whatever special event is detected (error, read0, |
| 5175 | * empty). The caller is responsible for taking care of those events and |
| 5176 | * avoiding the call if inappropriate. The function does not call the |
| 5177 | * connection's polling update function, so the caller is responsible for this. |
| 5178 | */ |
| 5179 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 5180 | { |
| 5181 | int ret, done = 0; |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5182 | int try; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5183 | |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5184 | conn_refresh_polling_flags(conn); |
| 5185 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5186 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5187 | goto out_error; |
| 5188 | |
| 5189 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5190 | /* a handshake was requested */ |
| 5191 | return 0; |
| 5192 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5193 | /* let's realign the buffer to optimize I/O */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5194 | if (buffer_empty(buf)) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5195 | buf->p = buf->data; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5196 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5197 | /* |
| 5198 | * If we're done reading the early data, and we're using |
| 5199 | * a new buffer, then we know for sure we're not tainted |
| 5200 | * with early data anymore |
| 5201 | */ |
| 5202 | if ((conn->flags & (CO_FL_EARLY_SSL_HS |CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) |
| 5203 | conn->flags &= ~CO_FL_EARLY_DATA; |
| 5204 | #endif |
| 5205 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5206 | |
| 5207 | /* read the largest possible block. For this, we perform only one call |
| 5208 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 5209 | * in which case we accept to do it once again. A new attempt is made on |
| 5210 | * EINTR too. |
| 5211 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 5212 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5213 | int need_out = 0; |
| 5214 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5215 | /* first check if we have some room after p+i */ |
| 5216 | try = buf->data + buf->size - (buf->p + buf->i); |
| 5217 | /* otherwise continue between data and p-o */ |
| 5218 | if (try <= 0) { |
| 5219 | try = buf->p - (buf->data + buf->o); |
| 5220 | if (try <= 0) |
| 5221 | break; |
| 5222 | } |
| 5223 | if (try > count) |
| 5224 | try = count; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5225 | if (((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_EARLY_SSL_HS) && |
| 5226 | conn->tmp_early_data != -1) { |
| 5227 | *bi_end(buf) = conn->tmp_early_data; |
| 5228 | done++; |
| 5229 | try--; |
| 5230 | count--; |
| 5231 | buf->i++; |
| 5232 | conn->tmp_early_data = -1; |
| 5233 | continue; |
| 5234 | } |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5235 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5236 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5237 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5238 | size_t read_length; |
| 5239 | |
| 5240 | ret = SSL_read_early_data(conn->xprt_ctx, |
| 5241 | bi_end(buf), try, &read_length); |
| 5242 | if (read_length > 0) |
| 5243 | conn->flags |= CO_FL_EARLY_DATA; |
| 5244 | if (ret == SSL_READ_EARLY_DATA_SUCCESS || |
| 5245 | ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5246 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5247 | /* |
| 5248 | * We're done reading the early data, |
| 5249 | * let's make the handshake |
| 5250 | */ |
| 5251 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5252 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5253 | need_out = 1; |
| 5254 | if (read_length == 0) |
| 5255 | break; |
| 5256 | } |
| 5257 | ret = read_length; |
| 5258 | } |
| 5259 | } else |
| 5260 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5261 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5262 | if (conn->flags & CO_FL_ERROR) { |
| 5263 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5264 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5265 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5266 | if (ret > 0) { |
| 5267 | buf->i += ret; |
| 5268 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5269 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5270 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5271 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5272 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5273 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5274 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5275 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5276 | __conn_sock_want_send(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5277 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5278 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5279 | if (global_ssl.async) |
| 5280 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5281 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5282 | break; |
| 5283 | } |
| 5284 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5285 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5286 | /* handshake is running, and it may need to re-enable read */ |
| 5287 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5288 | __conn_sock_want_recv(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5289 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5290 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5291 | if (global_ssl.async) |
| 5292 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5293 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5294 | break; |
| 5295 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5296 | /* we need to poll for retry a read later */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5297 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5298 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5299 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 5300 | goto read0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5301 | /* otherwise it's a real error */ |
| 5302 | goto out_error; |
| 5303 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5304 | if (need_out) |
| 5305 | break; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5306 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5307 | leave: |
| 5308 | conn_cond_update_sock_polling(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5309 | return done; |
| 5310 | |
| 5311 | read0: |
| 5312 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5313 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5314 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5315 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5316 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5317 | ERR_clear_error(); |
| 5318 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5319 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5320 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5321 | } |
| 5322 | |
| 5323 | |
| 5324 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 5325 | * <flags> may contain some CO_SFL_* flags to hint the system about other |
| 5326 | * pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5327 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5328 | * a second call may be performed. The connection's flags are updated with |
| 5329 | * whatever special event is detected (error, empty). The caller is responsible |
| 5330 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5331 | * function does not call the connection's polling update function, so the caller |
| 5332 | * is responsible for this. |
| 5333 | */ |
| 5334 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 5335 | { |
| 5336 | int ret, try, done; |
| 5337 | |
| 5338 | done = 0; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5339 | conn_refresh_polling_flags(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5340 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5341 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5342 | goto out_error; |
| 5343 | |
| 5344 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5345 | /* a handshake was requested */ |
| 5346 | return 0; |
| 5347 | |
| 5348 | /* send the largest possible block. For this we perform only one call |
| 5349 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5350 | * in which case we accept to do it once again. |
| 5351 | */ |
| 5352 | while (buf->o) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5353 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5354 | size_t written_data; |
| 5355 | #endif |
| 5356 | |
Kevin Hester | cad8234 | 2013-05-30 15:12:41 -0700 | [diff] [blame] | 5357 | try = bo_contig_data(buf); |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5358 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 5359 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5360 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5361 | global_ssl.max_record && try > global_ssl.max_record) { |
| 5362 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5363 | } |
| 5364 | else { |
| 5365 | /* we need to keep the information about the fact that |
| 5366 | * we're not limiting the upcoming send(), because if it |
| 5367 | * fails, we'll have to retry with at least as many data. |
| 5368 | */ |
| 5369 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 5370 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5371 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5372 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5373 | if (!SSL_is_init_finished(conn->xprt_ctx)) { |
| 5374 | unsigned int max_early; |
| 5375 | |
| 5376 | if (conn->tmp_early_data == -1) |
| 5377 | conn->tmp_early_data = 0; |
| 5378 | |
| 5379 | max_early = SSL_get_max_early_data(conn->xprt_ctx); |
| 5380 | if (try + conn->tmp_early_data > max_early) { |
| 5381 | try -= (try + conn->tmp_early_data) - max_early; |
| 5382 | if (try <= 0) |
| 5383 | break; |
| 5384 | } |
| 5385 | ret = SSL_write_early_data(conn->xprt_ctx, bo_ptr(buf), try, &written_data); |
| 5386 | if (ret == 1) { |
| 5387 | ret = written_data; |
| 5388 | conn->tmp_early_data += ret; |
| 5389 | } |
| 5390 | |
| 5391 | } else |
| 5392 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5393 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5394 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5395 | if (conn->flags & CO_FL_ERROR) { |
| 5396 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5397 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5398 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5399 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5400 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
| 5401 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5402 | buf->o -= ret; |
| 5403 | done += ret; |
| 5404 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 5405 | if (likely(buffer_empty(buf))) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5406 | /* optimize data alignment in the buffer */ |
| 5407 | buf->p = buf->data; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5408 | } |
| 5409 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5410 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5411 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5412 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5413 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5414 | /* handshake is running, and it may need to re-enable write */ |
| 5415 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5416 | __conn_sock_want_send(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5417 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5418 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5419 | if (global_ssl.async) |
| 5420 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5421 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5422 | break; |
| 5423 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5424 | /* we need to poll to retry a write later */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5425 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5426 | break; |
| 5427 | } |
| 5428 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5429 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5430 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5431 | __conn_sock_want_recv(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5432 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5433 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5434 | if (global_ssl.async) |
| 5435 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5436 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5437 | break; |
| 5438 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5439 | goto out_error; |
| 5440 | } |
| 5441 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5442 | leave: |
| 5443 | conn_cond_update_sock_polling(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5444 | return done; |
| 5445 | |
| 5446 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5447 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5448 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5449 | ERR_clear_error(); |
| 5450 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5451 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5452 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5453 | } |
| 5454 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5455 | static void ssl_sock_close(struct connection *conn) { |
| 5456 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5457 | if (conn->xprt_ctx) { |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5458 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5459 | if (global_ssl.async) { |
| 5460 | OSSL_ASYNC_FD all_fd[32], afd; |
| 5461 | size_t num_all_fds = 0; |
| 5462 | int i; |
| 5463 | |
| 5464 | SSL_get_all_async_fds(conn->xprt_ctx, NULL, &num_all_fds); |
| 5465 | if (num_all_fds > 32) { |
| 5466 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 5467 | return; |
| 5468 | } |
| 5469 | |
| 5470 | SSL_get_all_async_fds(conn->xprt_ctx, all_fd, &num_all_fds); |
| 5471 | |
| 5472 | /* If an async job is pending, we must try to |
| 5473 | to catch the end using polling before calling |
| 5474 | SSL_free */ |
| 5475 | if (num_all_fds && SSL_waiting_for_async(conn->xprt_ctx)) { |
| 5476 | for (i=0 ; i < num_all_fds ; i++) { |
| 5477 | /* switch on an handler designed to |
| 5478 | * handle the SSL_free |
| 5479 | */ |
| 5480 | afd = all_fd[i]; |
| 5481 | fdtab[afd].iocb = ssl_async_fd_free; |
| 5482 | fdtab[afd].owner = conn->xprt_ctx; |
| 5483 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 5484 | /* To ensure that the fd cache won't be used |
| 5485 | * and we'll catch a real RD event. |
| 5486 | */ |
| 5487 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5488 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5489 | conn->xprt_ctx = NULL; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5490 | jobs++; |
| 5491 | return; |
| 5492 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5493 | /* Else we can remove the fds from the fdtab |
| 5494 | * and call SSL_free. |
| 5495 | * note: we do a fd_remove and not a delete |
| 5496 | * because the fd is owned by the engine. |
| 5497 | * the engine is responsible to close |
| 5498 | */ |
| 5499 | for (i=0 ; i < num_all_fds ; i++) |
| 5500 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5501 | } |
| 5502 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5503 | SSL_free(conn->xprt_ctx); |
| 5504 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5505 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5506 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5507 | } |
| 5508 | |
| 5509 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 5510 | * any case, flags the connection as reusable if no handshake was in progress. |
| 5511 | */ |
| 5512 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 5513 | { |
| 5514 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5515 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5516 | if (!clean) |
| 5517 | /* don't sent notify on SSL_shutdown */ |
Willy Tarreau | e3cc3a3 | 2017-02-13 11:12:29 +0100 | [diff] [blame] | 5518 | SSL_set_quiet_shutdown(conn->xprt_ctx, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5519 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5520 | if (SSL_shutdown(conn->xprt_ctx) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5521 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5522 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5523 | ERR_clear_error(); |
| 5524 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5525 | } |
| 5526 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5527 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5528 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 5529 | { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5530 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5531 | return NULL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5532 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5533 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 5534 | } |
| 5535 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5536 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5537 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 5538 | { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5539 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5540 | return NULL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5541 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5542 | return SSL_get_version(conn->xprt_ctx); |
| 5543 | } |
| 5544 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5545 | /* Extract a serial from a cert, and copy it to a chunk. |
| 5546 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 5547 | * -1 if output is not large enough. |
| 5548 | */ |
| 5549 | static int |
| 5550 | ssl_sock_get_serial(X509 *crt, struct chunk *out) |
| 5551 | { |
| 5552 | ASN1_INTEGER *serial; |
| 5553 | |
| 5554 | serial = X509_get_serialNumber(crt); |
| 5555 | if (!serial) |
| 5556 | return 0; |
| 5557 | |
| 5558 | if (out->size < serial->length) |
| 5559 | return -1; |
| 5560 | |
| 5561 | memcpy(out->str, serial->data, serial->length); |
| 5562 | out->len = serial->length; |
| 5563 | return 1; |
| 5564 | } |
| 5565 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5566 | /* Extract a cert to der, and copy it to a chunk. |
| 5567 | * Returns 1 if cert is found and copied, 0 on der convertion failure and |
| 5568 | * -1 if output is not large enough. |
| 5569 | */ |
| 5570 | static int |
| 5571 | ssl_sock_crt2der(X509 *crt, struct chunk *out) |
| 5572 | { |
| 5573 | int len; |
| 5574 | unsigned char *p = (unsigned char *)out->str;; |
| 5575 | |
| 5576 | len =i2d_X509(crt, NULL); |
| 5577 | if (len <= 0) |
| 5578 | return 1; |
| 5579 | |
| 5580 | if (out->size < len) |
| 5581 | return -1; |
| 5582 | |
| 5583 | i2d_X509(crt,&p); |
| 5584 | out->len = len; |
| 5585 | return 1; |
| 5586 | } |
| 5587 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5588 | |
| 5589 | /* Copy Date in ASN1_UTCTIME format in struct chunk out. |
| 5590 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 5591 | * and -1 if output is not large enough. |
| 5592 | */ |
| 5593 | static int |
| 5594 | ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out) |
| 5595 | { |
| 5596 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 5597 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 5598 | |
| 5599 | if (gentm->length < 12) |
| 5600 | return 0; |
| 5601 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 5602 | return 0; |
| 5603 | if (out->size < gentm->length-2) |
| 5604 | return -1; |
| 5605 | |
| 5606 | memcpy(out->str, gentm->data+2, gentm->length-2); |
| 5607 | out->len = gentm->length-2; |
| 5608 | return 1; |
| 5609 | } |
| 5610 | else if (tm->type == V_ASN1_UTCTIME) { |
| 5611 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 5612 | |
| 5613 | if (utctm->length < 10) |
| 5614 | return 0; |
| 5615 | if (utctm->data[0] >= 0x35) |
| 5616 | return 0; |
| 5617 | if (out->size < utctm->length) |
| 5618 | return -1; |
| 5619 | |
| 5620 | memcpy(out->str, utctm->data, utctm->length); |
| 5621 | out->len = utctm->length; |
| 5622 | return 1; |
| 5623 | } |
| 5624 | |
| 5625 | return 0; |
| 5626 | } |
| 5627 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5628 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 5629 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 5630 | */ |
| 5631 | static int |
| 5632 | ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out) |
| 5633 | { |
| 5634 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5635 | ASN1_OBJECT *obj; |
| 5636 | ASN1_STRING *data; |
| 5637 | const unsigned char *data_ptr; |
| 5638 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5639 | int i, j, n; |
| 5640 | int cur = 0; |
| 5641 | const char *s; |
| 5642 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5643 | int name_count; |
| 5644 | |
| 5645 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5646 | |
| 5647 | out->len = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5648 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5649 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5650 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5651 | else |
| 5652 | j = i; |
| 5653 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5654 | ne = X509_NAME_get_entry(a, j); |
| 5655 | obj = X509_NAME_ENTRY_get_object(ne); |
| 5656 | data = X509_NAME_ENTRY_get_data(ne); |
| 5657 | data_ptr = ASN1_STRING_get0_data(data); |
| 5658 | data_len = ASN1_STRING_length(data); |
| 5659 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5660 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5661 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5662 | s = tmp; |
| 5663 | } |
| 5664 | |
| 5665 | if (chunk_strcasecmp(entry, s) != 0) |
| 5666 | continue; |
| 5667 | |
| 5668 | if (pos < 0) |
| 5669 | cur--; |
| 5670 | else |
| 5671 | cur++; |
| 5672 | |
| 5673 | if (cur != pos) |
| 5674 | continue; |
| 5675 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5676 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5677 | return -1; |
| 5678 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5679 | memcpy(out->str, data_ptr, data_len); |
| 5680 | out->len = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5681 | return 1; |
| 5682 | } |
| 5683 | |
| 5684 | return 0; |
| 5685 | |
| 5686 | } |
| 5687 | |
| 5688 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 5689 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 5690 | */ |
| 5691 | static int |
| 5692 | ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out) |
| 5693 | { |
| 5694 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5695 | ASN1_OBJECT *obj; |
| 5696 | ASN1_STRING *data; |
| 5697 | const unsigned char *data_ptr; |
| 5698 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5699 | int i, n, ln; |
| 5700 | int l = 0; |
| 5701 | const char *s; |
| 5702 | char *p; |
| 5703 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5704 | int name_count; |
| 5705 | |
| 5706 | |
| 5707 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5708 | |
| 5709 | out->len = 0; |
| 5710 | p = out->str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5711 | for (i = 0; i < name_count; i++) { |
| 5712 | ne = X509_NAME_get_entry(a, i); |
| 5713 | obj = X509_NAME_ENTRY_get_object(ne); |
| 5714 | data = X509_NAME_ENTRY_get_data(ne); |
| 5715 | data_ptr = ASN1_STRING_get0_data(data); |
| 5716 | data_len = ASN1_STRING_length(data); |
| 5717 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5718 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5719 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5720 | s = tmp; |
| 5721 | } |
| 5722 | ln = strlen(s); |
| 5723 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5724 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5725 | if (l > out->size) |
| 5726 | return -1; |
| 5727 | out->len = l; |
| 5728 | |
| 5729 | *(p++)='/'; |
| 5730 | memcpy(p, s, ln); |
| 5731 | p += ln; |
| 5732 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5733 | memcpy(p, data_ptr, data_len); |
| 5734 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5735 | } |
| 5736 | |
| 5737 | if (!out->len) |
| 5738 | return 0; |
| 5739 | |
| 5740 | return 1; |
| 5741 | } |
| 5742 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5743 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 5744 | * to disable SNI. |
| 5745 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5746 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 5747 | { |
| 5748 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5749 | char *prev_name; |
| 5750 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5751 | if (!ssl_sock_is_ssl(conn)) |
| 5752 | return; |
| 5753 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5754 | /* if the SNI changes, we must destroy the reusable context so that a |
| 5755 | * new connection will present a new SNI. As an optimization we could |
| 5756 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 5757 | * server. |
| 5758 | */ |
| 5759 | prev_name = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 5760 | if ((!prev_name && hostname) || |
| 5761 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
| 5762 | SSL_set_session(conn->xprt_ctx, NULL); |
| 5763 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5764 | SSL_set_tlsext_host_name(conn->xprt_ctx, hostname); |
| 5765 | #endif |
| 5766 | } |
| 5767 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5768 | /* Extract peer certificate's common name into the chunk dest |
| 5769 | * Returns |
| 5770 | * the len of the extracted common name |
| 5771 | * or 0 if no CN found in DN |
| 5772 | * or -1 on error case (i.e. no peer certificate) |
| 5773 | */ |
| 5774 | 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] | 5775 | { |
| 5776 | X509 *crt = NULL; |
| 5777 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5778 | const char find_cn[] = "CN"; |
| 5779 | const struct chunk find_cn_chunk = { |
| 5780 | .str = (char *)&find_cn, |
| 5781 | .len = sizeof(find_cn)-1 |
| 5782 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5783 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5784 | |
| 5785 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5786 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5787 | |
| 5788 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5789 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5790 | if (!crt) |
| 5791 | goto out; |
| 5792 | |
| 5793 | name = X509_get_subject_name(crt); |
| 5794 | if (!name) |
| 5795 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5796 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5797 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 5798 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5799 | if (crt) |
| 5800 | X509_free(crt); |
| 5801 | |
| 5802 | return result; |
| 5803 | } |
| 5804 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 5805 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 5806 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 5807 | { |
| 5808 | X509 *crt = NULL; |
| 5809 | |
| 5810 | if (!ssl_sock_is_ssl(conn)) |
| 5811 | return 0; |
| 5812 | |
| 5813 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5814 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5815 | if (!crt) |
| 5816 | return 0; |
| 5817 | |
| 5818 | X509_free(crt); |
| 5819 | return 1; |
| 5820 | } |
| 5821 | |
| 5822 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 5823 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5824 | { |
| 5825 | if (!ssl_sock_is_ssl(conn)) |
| 5826 | return 0; |
| 5827 | |
| 5828 | return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
| 5829 | } |
| 5830 | |
| 5831 | /* returns result from SSL verify */ |
| 5832 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 5833 | { |
| 5834 | if (!ssl_sock_is_ssl(conn)) |
| 5835 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 5836 | |
| 5837 | return (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
| 5838 | } |
| 5839 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 5840 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 5841 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 5842 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 5843 | * freed by the caller. NPN is also checked if available since older versions |
| 5844 | * of openssl (1.0.1) which are more common in field only support this one. |
| 5845 | */ |
| 5846 | static int ssl_sock_get_alpn(const struct connection *conn, const char **str, int *len) |
| 5847 | { |
| 5848 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5849 | return 0; |
| 5850 | |
| 5851 | *str = NULL; |
| 5852 | |
| 5853 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 5854 | SSL_get0_alpn_selected(conn->xprt_ctx, (const unsigned char **)str, (unsigned *)len); |
| 5855 | if (*str) |
| 5856 | return 1; |
| 5857 | #endif |
| 5858 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 5859 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, (const unsigned char **)str, (unsigned *)len); |
| 5860 | if (*str) |
| 5861 | return 1; |
| 5862 | #endif |
| 5863 | return 0; |
| 5864 | } |
| 5865 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5866 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 5867 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 5868 | static int |
| 5869 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 5870 | { |
| 5871 | struct connection *conn; |
| 5872 | |
| 5873 | conn = objt_conn(smp->sess->origin); |
| 5874 | if (!conn || conn->xprt != &ssl_sock) |
| 5875 | return 0; |
| 5876 | |
| 5877 | smp->flags = 0; |
| 5878 | smp->data.type = SMP_T_BOOL; |
| 5879 | smp->data.u.sint = (conn->flags & CO_FL_EARLY_DATA) ? 1 : 0; |
| 5880 | |
| 5881 | return 1; |
| 5882 | } |
| 5883 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 5884 | /* boolean, returns true if client cert was present */ |
| 5885 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5886 | 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] | 5887 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5888 | struct connection *conn; |
| 5889 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5890 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5891 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 5892 | return 0; |
| 5893 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5894 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 5895 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5896 | return 0; |
| 5897 | } |
| 5898 | |
| 5899 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5900 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5901 | 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] | 5902 | |
| 5903 | return 1; |
| 5904 | } |
| 5905 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5906 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 5907 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5908 | * should be use. |
| 5909 | */ |
| 5910 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5911 | 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] | 5912 | { |
| 5913 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 5914 | X509 *crt = NULL; |
| 5915 | int ret = 0; |
| 5916 | struct chunk *smp_trash; |
| 5917 | struct connection *conn; |
| 5918 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5919 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5920 | if (!conn || conn->xprt != &ssl_sock) |
| 5921 | return 0; |
| 5922 | |
| 5923 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 5924 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5925 | return 0; |
| 5926 | } |
| 5927 | |
| 5928 | if (cert_peer) |
| 5929 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5930 | else |
| 5931 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5932 | |
| 5933 | if (!crt) |
| 5934 | goto out; |
| 5935 | |
| 5936 | smp_trash = get_trash_chunk(); |
| 5937 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 5938 | goto out; |
| 5939 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5940 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5941 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5942 | ret = 1; |
| 5943 | out: |
| 5944 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5945 | if (cert_peer && crt) |
| 5946 | X509_free(crt); |
| 5947 | return ret; |
| 5948 | } |
| 5949 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5950 | /* binary, returns serial of certificate in a binary chunk. |
| 5951 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5952 | * should be use. |
| 5953 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5954 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5955 | 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] | 5956 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5957 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5958 | X509 *crt = NULL; |
| 5959 | int ret = 0; |
| 5960 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5961 | struct connection *conn; |
| 5962 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5963 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5964 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5965 | return 0; |
| 5966 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5967 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5968 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5969 | return 0; |
| 5970 | } |
| 5971 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5972 | if (cert_peer) |
| 5973 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5974 | else |
| 5975 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5976 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5977 | if (!crt) |
| 5978 | goto out; |
| 5979 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5980 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5981 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 5982 | goto out; |
| 5983 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5984 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5985 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5986 | ret = 1; |
| 5987 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5988 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5989 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5990 | X509_free(crt); |
| 5991 | return ret; |
| 5992 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 5993 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5994 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 5995 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5996 | * should be use. |
| 5997 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5998 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5999 | 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] | 6000 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6001 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6002 | X509 *crt = NULL; |
| 6003 | const EVP_MD *digest; |
| 6004 | int ret = 0; |
| 6005 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6006 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6007 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6008 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6009 | if (!conn || conn->xprt != &ssl_sock) |
| 6010 | return 0; |
| 6011 | |
| 6012 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6013 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6014 | return 0; |
| 6015 | } |
| 6016 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6017 | if (cert_peer) |
| 6018 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6019 | else |
| 6020 | crt = SSL_get_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6021 | if (!crt) |
| 6022 | goto out; |
| 6023 | |
| 6024 | smp_trash = get_trash_chunk(); |
| 6025 | digest = EVP_sha1(); |
| 6026 | X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len); |
| 6027 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6028 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6029 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6030 | ret = 1; |
| 6031 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6032 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6033 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6034 | X509_free(crt); |
| 6035 | return ret; |
| 6036 | } |
| 6037 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6038 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 6039 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6040 | * should be use. |
| 6041 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6042 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6043 | 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] | 6044 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6045 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6046 | X509 *crt = NULL; |
| 6047 | int ret = 0; |
| 6048 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6049 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6050 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6051 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6052 | if (!conn || conn->xprt != &ssl_sock) |
| 6053 | return 0; |
| 6054 | |
| 6055 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6056 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6057 | return 0; |
| 6058 | } |
| 6059 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6060 | if (cert_peer) |
| 6061 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6062 | else |
| 6063 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6064 | if (!crt) |
| 6065 | goto out; |
| 6066 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6067 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6068 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 6069 | goto out; |
| 6070 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6071 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6072 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6073 | ret = 1; |
| 6074 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6075 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6076 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6077 | X509_free(crt); |
| 6078 | return ret; |
| 6079 | } |
| 6080 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6081 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 6082 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6083 | * should be use. |
| 6084 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6085 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6086 | 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] | 6087 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6088 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6089 | X509 *crt = NULL; |
| 6090 | X509_NAME *name; |
| 6091 | int ret = 0; |
| 6092 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6093 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6094 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6095 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6096 | if (!conn || conn->xprt != &ssl_sock) |
| 6097 | return 0; |
| 6098 | |
| 6099 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6100 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6101 | return 0; |
| 6102 | } |
| 6103 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6104 | if (cert_peer) |
| 6105 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6106 | else |
| 6107 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6108 | if (!crt) |
| 6109 | goto out; |
| 6110 | |
| 6111 | name = X509_get_issuer_name(crt); |
| 6112 | if (!name) |
| 6113 | goto out; |
| 6114 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6115 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6116 | if (args && args[0].type == ARGT_STR) { |
| 6117 | int pos = 1; |
| 6118 | |
| 6119 | if (args[1].type == ARGT_SINT) |
| 6120 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6121 | |
| 6122 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6123 | goto out; |
| 6124 | } |
| 6125 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6126 | goto out; |
| 6127 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6128 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6129 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6130 | ret = 1; |
| 6131 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6132 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6133 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6134 | X509_free(crt); |
| 6135 | return ret; |
| 6136 | } |
| 6137 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6138 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 6139 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6140 | * should be use. |
| 6141 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6142 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6143 | 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] | 6144 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6145 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6146 | X509 *crt = NULL; |
| 6147 | int ret = 0; |
| 6148 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6149 | struct connection *conn; |
| 6150 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6151 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6152 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6153 | return 0; |
| 6154 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6155 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6156 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6157 | return 0; |
| 6158 | } |
| 6159 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6160 | if (cert_peer) |
| 6161 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6162 | else |
| 6163 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6164 | if (!crt) |
| 6165 | goto out; |
| 6166 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6167 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6168 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 6169 | goto out; |
| 6170 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6171 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6172 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6173 | ret = 1; |
| 6174 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6175 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6176 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6177 | X509_free(crt); |
| 6178 | return ret; |
| 6179 | } |
| 6180 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6181 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 6182 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6183 | * should be use. |
| 6184 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6185 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6186 | 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] | 6187 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6188 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6189 | X509 *crt = NULL; |
| 6190 | X509_NAME *name; |
| 6191 | int ret = 0; |
| 6192 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6193 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6194 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6195 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6196 | if (!conn || conn->xprt != &ssl_sock) |
| 6197 | return 0; |
| 6198 | |
| 6199 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6200 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6201 | return 0; |
| 6202 | } |
| 6203 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6204 | if (cert_peer) |
| 6205 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6206 | else |
| 6207 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6208 | if (!crt) |
| 6209 | goto out; |
| 6210 | |
| 6211 | name = X509_get_subject_name(crt); |
| 6212 | if (!name) |
| 6213 | goto out; |
| 6214 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6215 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6216 | if (args && args[0].type == ARGT_STR) { |
| 6217 | int pos = 1; |
| 6218 | |
| 6219 | if (args[1].type == ARGT_SINT) |
| 6220 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6221 | |
| 6222 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6223 | goto out; |
| 6224 | } |
| 6225 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6226 | goto out; |
| 6227 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6228 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6229 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6230 | ret = 1; |
| 6231 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6232 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6233 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6234 | X509_free(crt); |
| 6235 | return ret; |
| 6236 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6237 | |
| 6238 | /* integer, returns true if current session use a client certificate */ |
| 6239 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6240 | 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] | 6241 | { |
| 6242 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6243 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6244 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6245 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6246 | if (!conn || conn->xprt != &ssl_sock) |
| 6247 | return 0; |
| 6248 | |
| 6249 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6250 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6251 | return 0; |
| 6252 | } |
| 6253 | |
| 6254 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6255 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6256 | if (crt) { |
| 6257 | X509_free(crt); |
| 6258 | } |
| 6259 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6260 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6261 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6262 | return 1; |
| 6263 | } |
| 6264 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6265 | /* integer, returns the certificate version |
| 6266 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6267 | * should be use. |
| 6268 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6269 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6270 | 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] | 6271 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6272 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6273 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6274 | struct connection *conn; |
| 6275 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6276 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6277 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6278 | return 0; |
| 6279 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6280 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6281 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6282 | return 0; |
| 6283 | } |
| 6284 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6285 | if (cert_peer) |
| 6286 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6287 | else |
| 6288 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6289 | if (!crt) |
| 6290 | return 0; |
| 6291 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6292 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6293 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6294 | if (cert_peer) |
| 6295 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6296 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6297 | |
| 6298 | return 1; |
| 6299 | } |
| 6300 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6301 | /* string, returns the certificate's signature algorithm. |
| 6302 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6303 | * should be use. |
| 6304 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6305 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6306 | 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] | 6307 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6308 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6309 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6310 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6311 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6312 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6313 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6314 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6315 | if (!conn || conn->xprt != &ssl_sock) |
| 6316 | return 0; |
| 6317 | |
| 6318 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6319 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6320 | return 0; |
| 6321 | } |
| 6322 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6323 | if (cert_peer) |
| 6324 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6325 | else |
| 6326 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6327 | if (!crt) |
| 6328 | return 0; |
| 6329 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6330 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6331 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6332 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6333 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 6334 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6335 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6336 | if (cert_peer) |
| 6337 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6338 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 6339 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6340 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6341 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6342 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6343 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6344 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6345 | if (cert_peer) |
| 6346 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6347 | |
| 6348 | return 1; |
| 6349 | } |
| 6350 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6351 | /* string, returns the certificate's key algorithm. |
| 6352 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6353 | * should be use. |
| 6354 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6355 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6356 | 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] | 6357 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6358 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6359 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6360 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6361 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6362 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6363 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6364 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6365 | if (!conn || conn->xprt != &ssl_sock) |
| 6366 | return 0; |
| 6367 | |
| 6368 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6369 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6370 | return 0; |
| 6371 | } |
| 6372 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6373 | if (cert_peer) |
| 6374 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6375 | else |
| 6376 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6377 | if (!crt) |
| 6378 | return 0; |
| 6379 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6380 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 6381 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6382 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6383 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 6384 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6385 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6386 | if (cert_peer) |
| 6387 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6388 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 6389 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6390 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6391 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6392 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6393 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6394 | if (cert_peer) |
| 6395 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6396 | |
| 6397 | return 1; |
| 6398 | } |
| 6399 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6400 | /* boolean, returns true if front conn. transport layer is SSL. |
| 6401 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6402 | * char is 'b'. |
| 6403 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6404 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6405 | 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] | 6406 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6407 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6408 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6409 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6410 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6411 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6412 | return 1; |
| 6413 | } |
| 6414 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6415 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6416 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6417 | 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] | 6418 | { |
| 6419 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6420 | struct connection *conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6421 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6422 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6423 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6424 | conn->xprt_ctx && |
| 6425 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6426 | return 1; |
| 6427 | #else |
| 6428 | return 0; |
| 6429 | #endif |
| 6430 | } |
| 6431 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6432 | /* boolean, returns true if client session has been resumed */ |
| 6433 | static int |
| 6434 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6435 | { |
| 6436 | struct connection *conn = objt_conn(smp->sess->origin); |
| 6437 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6438 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6439 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6440 | conn->xprt_ctx && |
| 6441 | SSL_session_reused(conn->xprt_ctx); |
| 6442 | return 1; |
| 6443 | } |
| 6444 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6445 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 6446 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6447 | * char is 'b'. |
| 6448 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6449 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6450 | 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] | 6451 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6452 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6453 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6454 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6455 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6456 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6457 | return 0; |
| 6458 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6459 | smp->data.u.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
| 6460 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6461 | return 0; |
| 6462 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6463 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6464 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6465 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6466 | |
| 6467 | return 1; |
| 6468 | } |
| 6469 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6470 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 6471 | * is SSL. |
| 6472 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6473 | * char is 'b'. |
| 6474 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6475 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6476 | 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] | 6477 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6478 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6479 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6480 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6481 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6482 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6483 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6484 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6485 | return 0; |
| 6486 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 6487 | if (!SSL_get_cipher_bits(conn->xprt_ctx, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6488 | return 0; |
| 6489 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6490 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6491 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6492 | |
| 6493 | return 1; |
| 6494 | } |
| 6495 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6496 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 6497 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6498 | * char is 'b'. |
| 6499 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6500 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6501 | 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] | 6502 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6503 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6504 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6505 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6506 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6507 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6508 | return 0; |
| 6509 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6510 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
| 6511 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6512 | return 0; |
| 6513 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6514 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6515 | |
| 6516 | return 1; |
| 6517 | } |
| 6518 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6519 | #ifdef OPENSSL_NPN_NEGOTIATED |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6520 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6521 | 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] | 6522 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6523 | struct connection *conn; |
| 6524 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6525 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6526 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6527 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6528 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6529 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6530 | return 0; |
| 6531 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6532 | smp->data.u.str.str = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6533 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6534 | (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] | 6535 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6536 | if (!smp->data.u.str.str) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6537 | return 0; |
| 6538 | |
| 6539 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6540 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6541 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6542 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6543 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6544 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6545 | 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] | 6546 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6547 | struct connection *conn; |
| 6548 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6549 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6550 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6551 | |
Willy Tarreau | e26bf05 | 2015-05-12 10:30:12 +0200 | [diff] [blame] | 6552 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6553 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6554 | return 0; |
| 6555 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6556 | smp->data.u.str.str = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6557 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6558 | (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] | 6559 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6560 | if (!smp->data.u.str.str) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6561 | return 0; |
| 6562 | |
| 6563 | return 1; |
| 6564 | } |
| 6565 | #endif |
| 6566 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6567 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 6568 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6569 | * char is 'b'. |
| 6570 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6571 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6572 | 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] | 6573 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6574 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6575 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6576 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6577 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6578 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6579 | return 0; |
| 6580 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6581 | smp->data.u.str.str = (char *)SSL_get_version(conn->xprt_ctx); |
| 6582 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6583 | return 0; |
| 6584 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6585 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6586 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6587 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6588 | |
| 6589 | return 1; |
| 6590 | } |
| 6591 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6592 | /* 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] | 6593 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6594 | * char is 'b'. |
| 6595 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6596 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6597 | 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] | 6598 | { |
| 6599 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6600 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6601 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6602 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6603 | SSL_SESSION *ssl_sess; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6604 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6605 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6606 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6607 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6608 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6609 | return 0; |
| 6610 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6611 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 6612 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6613 | return 0; |
| 6614 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6615 | smp->data.u.str.str = (char *)SSL_SESSION_get_id(ssl_sess, (unsigned int *)&smp->data.u.str.len); |
| 6616 | if (!smp->data.u.str.str || !smp->data.u.str.len) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6617 | return 0; |
| 6618 | |
| 6619 | return 1; |
| 6620 | #else |
| 6621 | return 0; |
| 6622 | #endif |
| 6623 | } |
| 6624 | |
| 6625 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6626 | 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] | 6627 | { |
| 6628 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6629 | struct connection *conn; |
| 6630 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6631 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6632 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6633 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6634 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6635 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6636 | return 0; |
| 6637 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6638 | smp->data.u.str.str = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 6639 | if (!smp->data.u.str.str) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 6640 | return 0; |
| 6641 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6642 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6643 | return 1; |
| 6644 | #else |
| 6645 | return 0; |
| 6646 | #endif |
| 6647 | } |
| 6648 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6649 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6650 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6651 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6652 | struct connection *conn; |
| 6653 | struct ssl_capture *capture; |
| 6654 | |
| 6655 | conn = objt_conn(smp->sess->origin); |
| 6656 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6657 | return 0; |
| 6658 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6659 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6660 | if (!capture) |
| 6661 | return 0; |
| 6662 | |
| 6663 | smp->flags = SMP_F_CONST; |
| 6664 | smp->data.type = SMP_T_BIN; |
| 6665 | smp->data.u.str.str = capture->ciphersuite; |
| 6666 | smp->data.u.str.len = capture->ciphersuite_len; |
| 6667 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6668 | } |
| 6669 | |
| 6670 | static int |
| 6671 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6672 | { |
| 6673 | struct chunk *data; |
| 6674 | |
| 6675 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 6676 | return 0; |
| 6677 | |
| 6678 | data = get_trash_chunk(); |
| 6679 | dump_binary(data, smp->data.u.str.str, smp->data.u.str.len); |
| 6680 | smp->data.type = SMP_T_BIN; |
| 6681 | smp->data.u.str = *data; |
| 6682 | return 1; |
| 6683 | } |
| 6684 | |
| 6685 | static int |
| 6686 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6687 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6688 | struct connection *conn; |
| 6689 | struct ssl_capture *capture; |
| 6690 | |
| 6691 | conn = objt_conn(smp->sess->origin); |
| 6692 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6693 | return 0; |
| 6694 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6695 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6696 | if (!capture) |
| 6697 | return 0; |
| 6698 | |
| 6699 | smp->data.type = SMP_T_SINT; |
| 6700 | smp->data.u.sint = capture->xxh64; |
| 6701 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6702 | } |
| 6703 | |
| 6704 | static int |
| 6705 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6706 | { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 6707 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6708 | struct chunk *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6709 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6710 | |
| 6711 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 6712 | return 0; |
| 6713 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6714 | data = get_trash_chunk(); |
| 6715 | for (i = 0; i + 1 < smp->data.u.str.len; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 6716 | const char *str; |
| 6717 | const SSL_CIPHER *cipher; |
| 6718 | const unsigned char *bin = (const unsigned char *)smp->data.u.str.str + i; |
| 6719 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 6720 | #if defined(OPENSSL_IS_BORINGSSL) |
| 6721 | cipher = SSL_get_cipher_by_value(id); |
| 6722 | #else |
| 6723 | struct connection *conn = objt_conn(smp->sess->origin); |
| 6724 | cipher = SSL_CIPHER_find(conn->xprt_ctx, bin); |
| 6725 | #endif |
| 6726 | str = SSL_CIPHER_get_name(cipher); |
| 6727 | if (!str || strcmp(str, "(NONE)") == 0) |
| 6728 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6729 | else |
| 6730 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 6731 | } |
| 6732 | smp->data.type = SMP_T_STR; |
| 6733 | smp->data.u.str = *data; |
| 6734 | return 1; |
| 6735 | #else |
| 6736 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 6737 | #endif |
| 6738 | } |
| 6739 | |
| 6740 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6741 | 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] | 6742 | { |
| 6743 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6744 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 6745 | smp->strm ? smp->strm->si[1].end : NULL); |
| 6746 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6747 | int finished_len; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6748 | struct chunk *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6749 | |
| 6750 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6751 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6752 | return 0; |
| 6753 | |
| 6754 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 6755 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6756 | return 0; |
| 6757 | } |
| 6758 | |
| 6759 | finished_trash = get_trash_chunk(); |
| 6760 | if (!SSL_session_reused(conn->xprt_ctx)) |
| 6761 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 6762 | else |
| 6763 | finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 6764 | |
| 6765 | if (!finished_len) |
| 6766 | return 0; |
| 6767 | |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 6768 | finished_trash->len = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6769 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6770 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6771 | |
| 6772 | return 1; |
| 6773 | #else |
| 6774 | return 0; |
| 6775 | #endif |
| 6776 | } |
| 6777 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6778 | /* 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] | 6779 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6780 | 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] | 6781 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6782 | struct connection *conn; |
| 6783 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6784 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6785 | if (!conn || conn->xprt != &ssl_sock) |
| 6786 | return 0; |
| 6787 | |
| 6788 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6789 | smp->flags = SMP_F_MAY_CHANGE; |
| 6790 | return 0; |
| 6791 | } |
| 6792 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6793 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6794 | 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] | 6795 | smp->flags = 0; |
| 6796 | |
| 6797 | return 1; |
| 6798 | } |
| 6799 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6800 | /* 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] | 6801 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6802 | 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] | 6803 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6804 | struct connection *conn; |
| 6805 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6806 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6807 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6808 | return 0; |
| 6809 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6810 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6811 | smp->flags = SMP_F_MAY_CHANGE; |
| 6812 | return 0; |
| 6813 | } |
| 6814 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6815 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6816 | 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] | 6817 | smp->flags = 0; |
| 6818 | |
| 6819 | return 1; |
| 6820 | } |
| 6821 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6822 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6823 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6824 | 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] | 6825 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6826 | struct connection *conn; |
| 6827 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6828 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6829 | if (!conn || conn->xprt != &ssl_sock) |
| 6830 | return 0; |
| 6831 | |
| 6832 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 6833 | smp->flags = SMP_F_MAY_CHANGE; |
| 6834 | return 0; |
| 6835 | } |
| 6836 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6837 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6838 | 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] | 6839 | smp->flags = 0; |
| 6840 | |
| 6841 | return 1; |
| 6842 | } |
| 6843 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6844 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 6845 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6846 | 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] | 6847 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6848 | struct connection *conn; |
| 6849 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6850 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6851 | if (!conn || conn->xprt != &ssl_sock) |
| 6852 | return 0; |
| 6853 | |
| 6854 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 6855 | smp->flags = SMP_F_MAY_CHANGE; |
| 6856 | return 0; |
| 6857 | } |
| 6858 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6859 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 6860 | return 0; |
| 6861 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6862 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6863 | 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] | 6864 | smp->flags = 0; |
| 6865 | |
| 6866 | return 1; |
| 6867 | } |
| 6868 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 6869 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6870 | 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] | 6871 | { |
| 6872 | if (!*args[cur_arg + 1]) { |
| 6873 | if (err) |
| 6874 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 6875 | return ERR_ALERT | ERR_FATAL; |
| 6876 | } |
| 6877 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6878 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6879 | 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] | 6880 | else |
| 6881 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 6882 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6883 | return 0; |
| 6884 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6885 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6886 | { |
| 6887 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 6888 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6889 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6890 | /* parse the "ca-sign-file" bind keyword */ |
| 6891 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6892 | { |
| 6893 | if (!*args[cur_arg + 1]) { |
| 6894 | if (err) |
| 6895 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 6896 | return ERR_ALERT | ERR_FATAL; |
| 6897 | } |
| 6898 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6899 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6900 | 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] | 6901 | else |
| 6902 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 6903 | |
| 6904 | return 0; |
| 6905 | } |
| 6906 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 6907 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6908 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6909 | { |
| 6910 | if (!*args[cur_arg + 1]) { |
| 6911 | if (err) |
| 6912 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 6913 | return ERR_ALERT | ERR_FATAL; |
| 6914 | } |
| 6915 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 6916 | return 0; |
| 6917 | } |
| 6918 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6919 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6920 | 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] | 6921 | { |
| 6922 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 6923 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6924 | return ERR_ALERT | ERR_FATAL; |
| 6925 | } |
| 6926 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 6927 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6928 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6929 | return 0; |
| 6930 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6931 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6932 | { |
| 6933 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 6934 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6935 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6936 | 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] | 6937 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 6938 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 6939 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6940 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 6941 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6942 | return ERR_ALERT | ERR_FATAL; |
| 6943 | } |
| 6944 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6945 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 6946 | 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] | 6947 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 6948 | return ERR_ALERT | ERR_FATAL; |
| 6949 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6950 | 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] | 6951 | if (ssl_sock_load_cert(path, conf, err) > 0) |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 6952 | return ERR_ALERT | ERR_FATAL; |
| 6953 | |
| 6954 | return 0; |
| 6955 | } |
| 6956 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 6957 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6958 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6959 | |
| 6960 | return 0; |
| 6961 | } |
| 6962 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 6963 | /* parse the "crt-list" bind keyword */ |
| 6964 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6965 | { |
| 6966 | if (!*args[cur_arg + 1]) { |
| 6967 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 6968 | return ERR_ALERT | ERR_FATAL; |
| 6969 | } |
| 6970 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6971 | 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] | 6972 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 6973 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 6974 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 6975 | |
| 6976 | return 0; |
| 6977 | } |
| 6978 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 6979 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6980 | 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] | 6981 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 6982 | #ifndef X509_V_FLAG_CRL_CHECK |
| 6983 | if (err) |
| 6984 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 6985 | return ERR_ALERT | ERR_FATAL; |
| 6986 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6987 | if (!*args[cur_arg + 1]) { |
| 6988 | if (err) |
| 6989 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 6990 | return ERR_ALERT | ERR_FATAL; |
| 6991 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6992 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6993 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6994 | 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] | 6995 | else |
| 6996 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 6997 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6998 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 6999 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7000 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7001 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7002 | { |
| 7003 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7004 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7005 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 7006 | /* parse the "curves" bind keyword keyword */ |
| 7007 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7008 | { |
| 7009 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 7010 | if (!*args[cur_arg + 1]) { |
| 7011 | if (err) |
| 7012 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 7013 | return ERR_ALERT | ERR_FATAL; |
| 7014 | } |
| 7015 | conf->curves = strdup(args[cur_arg + 1]); |
| 7016 | return 0; |
| 7017 | #else |
| 7018 | if (err) |
| 7019 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 7020 | return ERR_ALERT | ERR_FATAL; |
| 7021 | #endif |
| 7022 | } |
| 7023 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7024 | { |
| 7025 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 7026 | } |
| 7027 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7028 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7029 | 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] | 7030 | { |
| 7031 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 7032 | if (err) |
| 7033 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 7034 | return ERR_ALERT | ERR_FATAL; |
| 7035 | #elif defined(OPENSSL_NO_ECDH) |
| 7036 | if (err) |
| 7037 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 7038 | return ERR_ALERT | ERR_FATAL; |
| 7039 | #else |
| 7040 | if (!*args[cur_arg + 1]) { |
| 7041 | if (err) |
| 7042 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 7043 | return ERR_ALERT | ERR_FATAL; |
| 7044 | } |
| 7045 | |
| 7046 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7047 | |
| 7048 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7049 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7050 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7051 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7052 | { |
| 7053 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 7054 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7055 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7056 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7057 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7058 | { |
| 7059 | int code; |
| 7060 | char *p = args[cur_arg + 1]; |
| 7061 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 7062 | |
| 7063 | if (!*p) { |
| 7064 | if (err) |
| 7065 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 7066 | return ERR_ALERT | ERR_FATAL; |
| 7067 | } |
| 7068 | |
| 7069 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 7070 | ignerr = &conf->ca_ignerr; |
| 7071 | |
| 7072 | if (strcmp(p, "all") == 0) { |
| 7073 | *ignerr = ~0ULL; |
| 7074 | return 0; |
| 7075 | } |
| 7076 | |
| 7077 | while (p) { |
| 7078 | code = atoi(p); |
| 7079 | if ((code <= 0) || (code > 63)) { |
| 7080 | if (err) |
| 7081 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 7082 | args[cur_arg], code, args[cur_arg + 1]); |
| 7083 | return ERR_ALERT | ERR_FATAL; |
| 7084 | } |
| 7085 | *ignerr |= 1ULL << code; |
| 7086 | p = strchr(p, ','); |
| 7087 | if (p) |
| 7088 | p++; |
| 7089 | } |
| 7090 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7091 | return 0; |
| 7092 | } |
| 7093 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7094 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 7095 | 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] | 7096 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7097 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7098 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7099 | p = strchr(arg, '-'); |
| 7100 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7101 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7102 | p++; |
| 7103 | if (!strcmp(p, "sslv3")) |
| 7104 | v = CONF_SSLV3; |
| 7105 | else if (!strcmp(p, "tlsv10")) |
| 7106 | v = CONF_TLSV10; |
| 7107 | else if (!strcmp(p, "tlsv11")) |
| 7108 | v = CONF_TLSV11; |
| 7109 | else if (!strcmp(p, "tlsv12")) |
| 7110 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 7111 | else if (!strcmp(p, "tlsv13")) |
| 7112 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7113 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7114 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7115 | if (!strncmp(arg, "no-", 3)) |
| 7116 | methods->flags |= methodVersions[v].flag; |
| 7117 | else if (!strncmp(arg, "force-", 6)) |
| 7118 | methods->min = methods->max = v; |
| 7119 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7120 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7121 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7122 | fail: |
| 7123 | if (err) |
| 7124 | memprintf(err, "'%s' : option not implemented", arg); |
| 7125 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7126 | } |
| 7127 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7128 | 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] | 7129 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7130 | return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err); |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7131 | } |
| 7132 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7133 | 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] | 7134 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7135 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 7136 | } |
| 7137 | |
| 7138 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 7139 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 7140 | { |
| 7141 | uint16_t i, v = 0; |
| 7142 | char *argv = args[cur_arg + 1]; |
| 7143 | if (!*argv) { |
| 7144 | if (err) |
| 7145 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
| 7146 | return ERR_ALERT | ERR_FATAL; |
| 7147 | } |
| 7148 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 7149 | if (!strcmp(argv, methodVersions[i].name)) |
| 7150 | v = i; |
| 7151 | if (!v) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7152 | if (err) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7153 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7154 | return ERR_ALERT | ERR_FATAL; |
| 7155 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7156 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 7157 | methods->min = v; |
| 7158 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 7159 | methods->max = v; |
| 7160 | else { |
| 7161 | if (err) |
| 7162 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
| 7163 | return ERR_ALERT | ERR_FATAL; |
| 7164 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7165 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7166 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7167 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 7168 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7169 | { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 7170 | #if (OPENSSL_VERSION_NUMBER < 0x10101000L) || !defined(OPENSSL_IS_BORINGSSL) |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 7171 | Warning("crt-list: ssl-min-ver and ssl-max-ver are not supported with this Openssl version (skipped).\n"); |
| 7172 | #endif |
| 7173 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 7174 | } |
| 7175 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7176 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7177 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7178 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7179 | } |
| 7180 | |
| 7181 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7182 | { |
| 7183 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 7184 | } |
| 7185 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7186 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 7187 | 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] | 7188 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 7189 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7190 | return 0; |
| 7191 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7192 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7193 | /* parse the "allow-0rtt" bind keyword */ |
| 7194 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7195 | { |
| 7196 | conf->early_data = 1; |
| 7197 | return 0; |
| 7198 | } |
| 7199 | |
| 7200 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7201 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 7202 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7203 | return 0; |
| 7204 | } |
| 7205 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7206 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7207 | 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] | 7208 | { |
| 7209 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 7210 | char *p1, *p2; |
| 7211 | |
| 7212 | if (!*args[cur_arg + 1]) { |
| 7213 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 7214 | return ERR_ALERT | ERR_FATAL; |
| 7215 | } |
| 7216 | |
| 7217 | free(conf->npn_str); |
| 7218 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 7219 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 7220 | * so we reuse each comma to store the next <len> and need |
| 7221 | * one more for the end of the string. |
| 7222 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7223 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 7224 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7225 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 7226 | |
| 7227 | /* replace commas with the name length */ |
| 7228 | p1 = conf->npn_str; |
| 7229 | p2 = p1 + 1; |
| 7230 | while (1) { |
| 7231 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 7232 | if (!p2) |
| 7233 | p2 = p1 + 1 + strlen(p1 + 1); |
| 7234 | |
| 7235 | if (p2 - (p1 + 1) > 255) { |
| 7236 | *p2 = '\0'; |
| 7237 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 7238 | return ERR_ALERT | ERR_FATAL; |
| 7239 | } |
| 7240 | |
| 7241 | *p1 = p2 - (p1 + 1); |
| 7242 | p1 = p2; |
| 7243 | |
| 7244 | if (!*p2) |
| 7245 | break; |
| 7246 | |
| 7247 | *(p2++) = '\0'; |
| 7248 | } |
| 7249 | return 0; |
| 7250 | #else |
| 7251 | if (err) |
| 7252 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 7253 | return ERR_ALERT | ERR_FATAL; |
| 7254 | #endif |
| 7255 | } |
| 7256 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7257 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7258 | { |
| 7259 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 7260 | } |
| 7261 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7262 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7263 | 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] | 7264 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7265 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7266 | char *p1, *p2; |
| 7267 | |
| 7268 | if (!*args[cur_arg + 1]) { |
| 7269 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 7270 | return ERR_ALERT | ERR_FATAL; |
| 7271 | } |
| 7272 | |
| 7273 | free(conf->alpn_str); |
| 7274 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 7275 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 7276 | * so we reuse each comma to store the next <len> and need |
| 7277 | * one more for the end of the string. |
| 7278 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7279 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 7280 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7281 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 7282 | |
| 7283 | /* replace commas with the name length */ |
| 7284 | p1 = conf->alpn_str; |
| 7285 | p2 = p1 + 1; |
| 7286 | while (1) { |
| 7287 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 7288 | if (!p2) |
| 7289 | p2 = p1 + 1 + strlen(p1 + 1); |
| 7290 | |
| 7291 | if (p2 - (p1 + 1) > 255) { |
| 7292 | *p2 = '\0'; |
| 7293 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 7294 | return ERR_ALERT | ERR_FATAL; |
| 7295 | } |
| 7296 | |
| 7297 | *p1 = p2 - (p1 + 1); |
| 7298 | p1 = p2; |
| 7299 | |
| 7300 | if (!*p2) |
| 7301 | break; |
| 7302 | |
| 7303 | *(p2++) = '\0'; |
| 7304 | } |
| 7305 | return 0; |
| 7306 | #else |
| 7307 | if (err) |
| 7308 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 7309 | return ERR_ALERT | ERR_FATAL; |
| 7310 | #endif |
| 7311 | } |
| 7312 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7313 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7314 | { |
| 7315 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 7316 | } |
| 7317 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7318 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7319 | 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] | 7320 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 7321 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7322 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7323 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7324 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 7325 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 7326 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7327 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 7328 | conf->ssl_conf.early_data = global_ssl.default_early_data; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7329 | if (!conf->ssl_conf.ssl_methods.min) |
| 7330 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 7331 | if (!conf->ssl_conf.ssl_methods.max) |
| 7332 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7333 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7334 | return 0; |
| 7335 | } |
| 7336 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 7337 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 7338 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7339 | { |
| 7340 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 7341 | return 0; |
| 7342 | } |
| 7343 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7344 | /* parse the "generate-certificates" bind keyword */ |
| 7345 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7346 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7347 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7348 | conf->generate_certs = 1; |
| 7349 | #else |
| 7350 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 7351 | err && *err ? *err : ""); |
| 7352 | #endif |
| 7353 | return 0; |
| 7354 | } |
| 7355 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 7356 | /* parse the "strict-sni" bind keyword */ |
| 7357 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7358 | { |
| 7359 | conf->strict_sni = 1; |
| 7360 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7361 | } |
| 7362 | |
| 7363 | /* parse the "tls-ticket-keys" bind keyword */ |
| 7364 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7365 | { |
| 7366 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7367 | FILE *f; |
| 7368 | int i = 0; |
| 7369 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7370 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7371 | |
| 7372 | if (!*args[cur_arg + 1]) { |
| 7373 | if (err) |
| 7374 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 7375 | return ERR_ALERT | ERR_FATAL; |
| 7376 | } |
| 7377 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7378 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
| 7379 | if(keys_ref) { |
| 7380 | conf->keys_ref = keys_ref; |
| 7381 | return 0; |
| 7382 | } |
| 7383 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 7384 | keys_ref = malloc(sizeof(*keys_ref)); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7385 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct tls_sess_key)); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7386 | |
| 7387 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 7388 | if (err) |
| 7389 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 7390 | return ERR_ALERT | ERR_FATAL; |
| 7391 | } |
| 7392 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7393 | keys_ref->filename = strdup(args[cur_arg + 1]); |
| 7394 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7395 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 7396 | int len = strlen(thisline); |
| 7397 | /* Strip newline characters from the end */ |
| 7398 | if(thisline[len - 1] == '\n') |
| 7399 | thisline[--len] = 0; |
| 7400 | |
| 7401 | if(thisline[len - 1] == '\r') |
| 7402 | thisline[--len] = 0; |
| 7403 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7404 | 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] | 7405 | if (err) |
| 7406 | 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] | 7407 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7408 | return ERR_ALERT | ERR_FATAL; |
| 7409 | } |
| 7410 | i++; |
| 7411 | } |
| 7412 | |
| 7413 | if (i < TLS_TICKETS_NO) { |
| 7414 | if (err) |
| 7415 | 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] | 7416 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7417 | return ERR_ALERT | ERR_FATAL; |
| 7418 | } |
| 7419 | |
| 7420 | fclose(f); |
| 7421 | |
| 7422 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 7423 | i -= 2; |
| 7424 | 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] | 7425 | keys_ref->unique_id = -1; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7426 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7427 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7428 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 7429 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7430 | return 0; |
| 7431 | #else |
| 7432 | if (err) |
| 7433 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 7434 | return ERR_ALERT | ERR_FATAL; |
| 7435 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 7436 | } |
| 7437 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7438 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7439 | 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] | 7440 | { |
| 7441 | if (!*args[cur_arg + 1]) { |
| 7442 | if (err) |
| 7443 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 7444 | return ERR_ALERT | ERR_FATAL; |
| 7445 | } |
| 7446 | |
| 7447 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7448 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7449 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7450 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7451 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7452 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7453 | else { |
| 7454 | if (err) |
| 7455 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 7456 | args[cur_arg], args[cur_arg + 1]); |
| 7457 | return ERR_ALERT | ERR_FATAL; |
| 7458 | } |
| 7459 | |
| 7460 | return 0; |
| 7461 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7462 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7463 | { |
| 7464 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 7465 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7466 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 7467 | /* parse the "no-ca-names" bind keyword */ |
| 7468 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7469 | { |
| 7470 | conf->no_ca_names = 1; |
| 7471 | return 0; |
| 7472 | } |
| 7473 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7474 | { |
| 7475 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 7476 | } |
| 7477 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7478 | /************** "server" keywords ****************/ |
| 7479 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7480 | /* parse the "ca-file" server keyword */ |
| 7481 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7482 | { |
| 7483 | if (!*args[*cur_arg + 1]) { |
| 7484 | if (err) |
| 7485 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 7486 | return ERR_ALERT | ERR_FATAL; |
| 7487 | } |
| 7488 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7489 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7490 | 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] | 7491 | else |
| 7492 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 7493 | |
| 7494 | return 0; |
| 7495 | } |
| 7496 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 7497 | /* parse the "check-sni" server keyword */ |
| 7498 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7499 | { |
| 7500 | if (!*args[*cur_arg + 1]) { |
| 7501 | if (err) |
| 7502 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
| 7503 | return ERR_ALERT | ERR_FATAL; |
| 7504 | } |
| 7505 | |
| 7506 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 7507 | if (!newsrv->check.sni) { |
| 7508 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 7509 | return ERR_ALERT | ERR_FATAL; |
| 7510 | } |
| 7511 | return 0; |
| 7512 | |
| 7513 | } |
| 7514 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7515 | /* parse the "check-ssl" server keyword */ |
| 7516 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7517 | { |
| 7518 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7519 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 7520 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
| 7521 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7522 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 7523 | if (!newsrv->ssl_ctx.methods.min) |
| 7524 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 7525 | if (!newsrv->ssl_ctx.methods.max) |
| 7526 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 7527 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7528 | return 0; |
| 7529 | } |
| 7530 | |
| 7531 | /* parse the "ciphers" server keyword */ |
| 7532 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7533 | { |
| 7534 | if (!*args[*cur_arg + 1]) { |
| 7535 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 7536 | return ERR_ALERT | ERR_FATAL; |
| 7537 | } |
| 7538 | |
| 7539 | free(newsrv->ssl_ctx.ciphers); |
| 7540 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 7541 | return 0; |
| 7542 | } |
| 7543 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7544 | /* parse the "crl-file" server keyword */ |
| 7545 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7546 | { |
| 7547 | #ifndef X509_V_FLAG_CRL_CHECK |
| 7548 | if (err) |
| 7549 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 7550 | return ERR_ALERT | ERR_FATAL; |
| 7551 | #else |
| 7552 | if (!*args[*cur_arg + 1]) { |
| 7553 | if (err) |
| 7554 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 7555 | return ERR_ALERT | ERR_FATAL; |
| 7556 | } |
| 7557 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7558 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7559 | 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] | 7560 | else |
| 7561 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 7562 | |
| 7563 | return 0; |
| 7564 | #endif |
| 7565 | } |
| 7566 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 7567 | /* parse the "crt" server keyword */ |
| 7568 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7569 | { |
| 7570 | if (!*args[*cur_arg + 1]) { |
| 7571 | if (err) |
| 7572 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 7573 | return ERR_ALERT | ERR_FATAL; |
| 7574 | } |
| 7575 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7576 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
| 7577 | 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] | 7578 | else |
| 7579 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 7580 | |
| 7581 | return 0; |
| 7582 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7583 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 7584 | /* parse the "no-check-ssl" server keyword */ |
| 7585 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7586 | { |
| 7587 | newsrv->check.use_ssl = 0; |
| 7588 | free(newsrv->ssl_ctx.ciphers); |
| 7589 | newsrv->ssl_ctx.ciphers = NULL; |
| 7590 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 7591 | return 0; |
| 7592 | } |
| 7593 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 7594 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 7595 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7596 | { |
| 7597 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 7598 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 7599 | return 0; |
| 7600 | } |
| 7601 | |
| 7602 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 7603 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7604 | { |
| 7605 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 7606 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 7607 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 7608 | return 0; |
| 7609 | } |
| 7610 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 7611 | /* parse the "no-ssl" server keyword */ |
| 7612 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7613 | { |
| 7614 | newsrv->use_ssl = 0; |
| 7615 | free(newsrv->ssl_ctx.ciphers); |
| 7616 | newsrv->ssl_ctx.ciphers = NULL; |
| 7617 | return 0; |
| 7618 | } |
| 7619 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 7620 | /* parse the "no-ssl-reuse" server keyword */ |
| 7621 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7622 | { |
| 7623 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 7624 | return 0; |
| 7625 | } |
| 7626 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7627 | /* parse the "no-tls-tickets" server keyword */ |
| 7628 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7629 | { |
| 7630 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 7631 | return 0; |
| 7632 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7633 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 7634 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7635 | { |
| 7636 | newsrv->pp_opts |= SRV_PP_V2; |
| 7637 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 7638 | return 0; |
| 7639 | } |
| 7640 | |
| 7641 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 7642 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7643 | { |
| 7644 | newsrv->pp_opts |= SRV_PP_V2; |
| 7645 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 7646 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 7647 | return 0; |
| 7648 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7649 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7650 | /* parse the "sni" server keyword */ |
| 7651 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7652 | { |
| 7653 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7654 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 7655 | return ERR_ALERT | ERR_FATAL; |
| 7656 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7657 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7658 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7659 | arg = args[*cur_arg + 1]; |
| 7660 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7661 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 7662 | return ERR_ALERT | ERR_FATAL; |
| 7663 | } |
| 7664 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7665 | free(newsrv->sni_expr); |
| 7666 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7667 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7668 | return 0; |
| 7669 | #endif |
| 7670 | } |
| 7671 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7672 | /* parse the "ssl" server keyword */ |
| 7673 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7674 | { |
| 7675 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7676 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 7677 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7678 | return 0; |
| 7679 | } |
| 7680 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 7681 | /* parse the "ssl-reuse" server keyword */ |
| 7682 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7683 | { |
| 7684 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 7685 | return 0; |
| 7686 | } |
| 7687 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 7688 | /* parse the "tls-tickets" server keyword */ |
| 7689 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7690 | { |
| 7691 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 7692 | return 0; |
| 7693 | } |
| 7694 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7695 | /* parse the "verify" server keyword */ |
| 7696 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7697 | { |
| 7698 | if (!*args[*cur_arg + 1]) { |
| 7699 | if (err) |
| 7700 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 7701 | return ERR_ALERT | ERR_FATAL; |
| 7702 | } |
| 7703 | |
| 7704 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7705 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7706 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7707 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7708 | else { |
| 7709 | if (err) |
| 7710 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 7711 | args[*cur_arg], args[*cur_arg + 1]); |
| 7712 | return ERR_ALERT | ERR_FATAL; |
| 7713 | } |
| 7714 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 7715 | return 0; |
| 7716 | } |
| 7717 | |
| 7718 | /* parse the "verifyhost" server keyword */ |
| 7719 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7720 | { |
| 7721 | if (!*args[*cur_arg + 1]) { |
| 7722 | if (err) |
| 7723 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 7724 | return ERR_ALERT | ERR_FATAL; |
| 7725 | } |
| 7726 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 7727 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 7728 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 7729 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7730 | return 0; |
| 7731 | } |
| 7732 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7733 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 7734 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 7735 | struct proxy *defpx, const char *file, int line, |
| 7736 | char **err) { |
| 7737 | int i = 1; |
| 7738 | |
| 7739 | if (*(args[i]) == 0) { |
| 7740 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 7741 | return -1; |
| 7742 | } |
| 7743 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7744 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7745 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 7746 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 7747 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7748 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 7749 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 7750 | i++; |
| 7751 | else { |
| 7752 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 7753 | return -1; |
| 7754 | } |
| 7755 | } |
| 7756 | 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] | 7757 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 7758 | return -1; |
| 7759 | } |
| 7760 | i++; |
| 7761 | } |
| 7762 | return 0; |
| 7763 | } |
| 7764 | |
| 7765 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 7766 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 7767 | struct proxy *defpx, const char *file, int line, |
| 7768 | char **err) { |
| 7769 | int i = 1; |
| 7770 | |
| 7771 | if (*(args[i]) == 0) { |
| 7772 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 7773 | return -1; |
| 7774 | } |
| 7775 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7776 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7777 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7778 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 7779 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 7780 | i++; |
| 7781 | else { |
| 7782 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 7783 | return -1; |
| 7784 | } |
| 7785 | } |
| 7786 | 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] | 7787 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 7788 | return -1; |
| 7789 | } |
| 7790 | i++; |
| 7791 | } |
| 7792 | return 0; |
| 7793 | } |
| 7794 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 7795 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 7796 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7797 | */ |
| 7798 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 7799 | struct proxy *defpx, const char *file, int line, |
| 7800 | char **err) |
| 7801 | { |
| 7802 | char **target; |
| 7803 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7804 | 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] | 7805 | |
| 7806 | if (too_many_args(1, args, err, NULL)) |
| 7807 | return -1; |
| 7808 | |
| 7809 | if (*target) { |
| 7810 | memprintf(err, "'%s' already specified.", args[0]); |
| 7811 | return -1; |
| 7812 | } |
| 7813 | |
| 7814 | if (*(args[1]) == 0) { |
| 7815 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 7816 | return -1; |
| 7817 | } |
| 7818 | *target = strdup(args[1]); |
| 7819 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7820 | } |
| 7821 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 7822 | /* parse the "ssl-allow-0rtt" keyword in global section. |
| 7823 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7824 | */ |
| 7825 | static int ssl_parse_global_ssl_allow_0rtt(char **args, int section_type, |
| 7826 | struct proxy *curpx, struct proxy *defpx, const char *file, int line, |
| 7827 | char **err) |
| 7828 | { |
| 7829 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 7830 | global_ssl.default_early_data = 1; |
| 7831 | return 0; |
| 7832 | #else |
| 7833 | memprintf(err, "'%s': openssl library does not early data", args[0]); |
| 7834 | return -1; |
| 7835 | #endif |
| 7836 | |
| 7837 | } |
| 7838 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7839 | /* parse the "ssl-mode-async" keyword in global section. |
| 7840 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7841 | */ |
| 7842 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 7843 | struct proxy *defpx, const char *file, int line, |
| 7844 | char **err) |
| 7845 | { |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 7846 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7847 | global_ssl.async = 1; |
| 7848 | return 0; |
| 7849 | #else |
| 7850 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 7851 | return -1; |
| 7852 | #endif |
| 7853 | } |
| 7854 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7855 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7856 | static int ssl_check_async_engine_count(void) { |
| 7857 | int err_code = 0; |
| 7858 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7859 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
| 7860 | Alert("ssl-mode-async only supports a maximum of 32 engines.\n"); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7861 | err_code = ERR_ABORT; |
| 7862 | } |
| 7863 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 7864 | } |
| 7865 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7866 | /* parse the "ssl-engine" keyword in global section. |
| 7867 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7868 | */ |
| 7869 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 7870 | struct proxy *defpx, const char *file, int line, |
| 7871 | char **err) |
| 7872 | { |
| 7873 | char *algo; |
| 7874 | int ret = -1; |
| 7875 | |
| 7876 | if (*(args[1]) == 0) { |
| 7877 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 7878 | return ret; |
| 7879 | } |
| 7880 | |
| 7881 | if (*(args[2]) == 0) { |
| 7882 | /* if no list of algorithms is given, it defaults to ALL */ |
| 7883 | algo = strdup("ALL"); |
| 7884 | goto add_engine; |
| 7885 | } |
| 7886 | |
| 7887 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 7888 | if (strcmp(args[2], "algo") != 0) { |
| 7889 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 7890 | return ret; |
| 7891 | } |
| 7892 | |
| 7893 | if (*(args[3]) == 0) { |
| 7894 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 7895 | return ret; |
| 7896 | } |
| 7897 | algo = strdup(args[3]); |
| 7898 | |
| 7899 | add_engine: |
| 7900 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 7901 | openssl_engines_initialized++; |
| 7902 | ret = 0; |
| 7903 | } |
| 7904 | free(algo); |
| 7905 | return ret; |
| 7906 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7907 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7908 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 7909 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 7910 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 7911 | */ |
| 7912 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 7913 | struct proxy *defpx, const char *file, int line, |
| 7914 | char **err) |
| 7915 | { |
| 7916 | char **target; |
| 7917 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7918 | 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] | 7919 | |
| 7920 | if (too_many_args(1, args, err, NULL)) |
| 7921 | return -1; |
| 7922 | |
| 7923 | if (*(args[1]) == 0) { |
| 7924 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 7925 | return -1; |
| 7926 | } |
| 7927 | |
| 7928 | free(*target); |
| 7929 | *target = strdup(args[1]); |
| 7930 | return 0; |
| 7931 | } |
| 7932 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7933 | /* parse various global tune.ssl settings consisting in positive integers. |
| 7934 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7935 | */ |
| 7936 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 7937 | struct proxy *defpx, const char *file, int line, |
| 7938 | char **err) |
| 7939 | { |
| 7940 | int *target; |
| 7941 | |
| 7942 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 7943 | target = &global.tune.sslcachesize; |
| 7944 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7945 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7946 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7947 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 7948 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 7949 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7950 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 7951 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7952 | else { |
| 7953 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 7954 | return -1; |
| 7955 | } |
| 7956 | |
| 7957 | if (too_many_args(1, args, err, NULL)) |
| 7958 | return -1; |
| 7959 | |
| 7960 | if (*(args[1]) == 0) { |
| 7961 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 7962 | return -1; |
| 7963 | } |
| 7964 | |
| 7965 | *target = atoi(args[1]); |
| 7966 | if (*target < 0) { |
| 7967 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 7968 | return -1; |
| 7969 | } |
| 7970 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7971 | } |
| 7972 | |
| 7973 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 7974 | struct proxy *defpx, const char *file, int line, |
| 7975 | char **err) |
| 7976 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7977 | int ret; |
| 7978 | |
| 7979 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 7980 | if (ret != 0) |
| 7981 | return ret; |
| 7982 | |
| 7983 | if (pool2_ssl_capture) { |
| 7984 | memprintf(err, "'%s' is already configured.", args[0]); |
| 7985 | return -1; |
| 7986 | } |
| 7987 | |
| 7988 | pool2_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 7989 | if (!pool2_ssl_capture) { |
| 7990 | memprintf(err, "Out of memory error."); |
| 7991 | return -1; |
| 7992 | } |
| 7993 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7994 | } |
| 7995 | |
| 7996 | /* parse "ssl.force-private-cache". |
| 7997 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 7998 | */ |
| 7999 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 8000 | struct proxy *defpx, const char *file, int line, |
| 8001 | char **err) |
| 8002 | { |
| 8003 | if (too_many_args(0, args, err, NULL)) |
| 8004 | return -1; |
| 8005 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8006 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8007 | return 0; |
| 8008 | } |
| 8009 | |
| 8010 | /* parse "ssl.lifetime". |
| 8011 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8012 | */ |
| 8013 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 8014 | struct proxy *defpx, const char *file, int line, |
| 8015 | char **err) |
| 8016 | { |
| 8017 | const char *res; |
| 8018 | |
| 8019 | if (too_many_args(1, args, err, NULL)) |
| 8020 | return -1; |
| 8021 | |
| 8022 | if (*(args[1]) == 0) { |
| 8023 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 8024 | return -1; |
| 8025 | } |
| 8026 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8027 | 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] | 8028 | if (res) { |
| 8029 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 8030 | return -1; |
| 8031 | } |
| 8032 | return 0; |
| 8033 | } |
| 8034 | |
| 8035 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 8036 | /* parse "ssl-dh-param-file". |
| 8037 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8038 | */ |
| 8039 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 8040 | struct proxy *defpx, const char *file, int line, |
| 8041 | char **err) |
| 8042 | { |
| 8043 | if (too_many_args(1, args, err, NULL)) |
| 8044 | return -1; |
| 8045 | |
| 8046 | if (*(args[1]) == 0) { |
| 8047 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 8048 | return -1; |
| 8049 | } |
| 8050 | |
| 8051 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 8052 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 8053 | return -1; |
| 8054 | } |
| 8055 | return 0; |
| 8056 | } |
| 8057 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8058 | /* parse "ssl.default-dh-param". |
| 8059 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8060 | */ |
| 8061 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 8062 | struct proxy *defpx, const char *file, int line, |
| 8063 | char **err) |
| 8064 | { |
| 8065 | if (too_many_args(1, args, err, NULL)) |
| 8066 | return -1; |
| 8067 | |
| 8068 | if (*(args[1]) == 0) { |
| 8069 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 8070 | return -1; |
| 8071 | } |
| 8072 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8073 | global_ssl.default_dh_param = atoi(args[1]); |
| 8074 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8075 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 8076 | return -1; |
| 8077 | } |
| 8078 | return 0; |
| 8079 | } |
| 8080 | #endif |
| 8081 | |
| 8082 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8083 | /* This function is used with TLS ticket keys management. It permits to browse |
| 8084 | * each reference. The variable <getnext> must contain the current node, |
| 8085 | * <end> point to the root node. |
| 8086 | */ |
| 8087 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8088 | static inline |
| 8089 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 8090 | { |
| 8091 | struct tls_keys_ref *ref = getnext; |
| 8092 | |
| 8093 | while (1) { |
| 8094 | |
| 8095 | /* Get next list entry. */ |
| 8096 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 8097 | |
| 8098 | /* If the entry is the last of the list, return NULL. */ |
| 8099 | if (&ref->list == end) |
| 8100 | return NULL; |
| 8101 | |
| 8102 | return ref; |
| 8103 | } |
| 8104 | } |
| 8105 | |
| 8106 | static inline |
| 8107 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 8108 | { |
| 8109 | int id; |
| 8110 | char *error; |
| 8111 | |
| 8112 | /* If the reference starts by a '#', this is numeric id. */ |
| 8113 | if (reference[0] == '#') { |
| 8114 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 8115 | id = strtol(reference + 1, &error, 10); |
| 8116 | if (*error != '\0') |
| 8117 | return NULL; |
| 8118 | |
| 8119 | /* Perform the unique id lookup. */ |
| 8120 | return tlskeys_ref_lookupid(id); |
| 8121 | } |
| 8122 | |
| 8123 | /* Perform the string lookup. */ |
| 8124 | return tlskeys_ref_lookup(reference); |
| 8125 | } |
| 8126 | #endif |
| 8127 | |
| 8128 | |
| 8129 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8130 | |
| 8131 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 8132 | |
| 8133 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 8134 | return cli_io_handler_tlskeys_files(appctx); |
| 8135 | } |
| 8136 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8137 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 8138 | * (next index to be dumped), and cli.p0 (next key reference). |
| 8139 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8140 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 8141 | |
| 8142 | struct stream_interface *si = appctx->owner; |
| 8143 | |
| 8144 | switch (appctx->st2) { |
| 8145 | case STAT_ST_INIT: |
| 8146 | /* Display the column headers. If the message cannot be sent, |
| 8147 | * quit the fucntion with returning 0. The function is called |
| 8148 | * later and restart at the state "STAT_ST_INIT". |
| 8149 | */ |
| 8150 | chunk_reset(&trash); |
| 8151 | |
| 8152 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 8153 | chunk_appendf(&trash, "# id secret\n"); |
| 8154 | else |
| 8155 | chunk_appendf(&trash, "# id (file)\n"); |
| 8156 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8157 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8158 | si_applet_cant_put(si); |
| 8159 | return 0; |
| 8160 | } |
| 8161 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8162 | /* Now, we start the browsing of the references lists. |
| 8163 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 8164 | * available field of this pointer is <list>. It is used with the function |
| 8165 | * tlskeys_list_get_next() for retruning the first available entry |
| 8166 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8167 | if (appctx->ctx.cli.p0 == NULL) { |
| 8168 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 8169 | 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] | 8170 | } |
| 8171 | |
| 8172 | appctx->st2 = STAT_ST_LIST; |
| 8173 | /* fall through */ |
| 8174 | |
| 8175 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8176 | while (appctx->ctx.cli.p0) { |
| 8177 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
| 8178 | int head = ref->tls_ticket_enc_index; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8179 | |
| 8180 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8181 | 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] | 8182 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8183 | |
| 8184 | if (appctx->ctx.cli.i1 == 0) |
| 8185 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 8186 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8187 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8188 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8189 | struct chunk *t2 = get_trash_chunk(); |
| 8190 | |
| 8191 | chunk_reset(t2); |
| 8192 | /* 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] | 8193 | 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] | 8194 | sizeof(struct tls_sess_key), t2->str, t2->size); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8195 | 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] | 8196 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8197 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8198 | /* let's try again later from this stream. We add ourselves into |
| 8199 | * this stream's users so that it can remove us upon termination. |
| 8200 | */ |
| 8201 | si_applet_cant_put(si); |
| 8202 | return 0; |
| 8203 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8204 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8205 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8206 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8207 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8208 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8209 | /* let's try again later from this stream. We add ourselves into |
| 8210 | * this stream's users so that it can remove us upon termination. |
| 8211 | */ |
| 8212 | si_applet_cant_put(si); |
| 8213 | return 0; |
| 8214 | } |
| 8215 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8216 | 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] | 8217 | break; |
| 8218 | |
| 8219 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8220 | 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] | 8221 | } |
| 8222 | |
| 8223 | appctx->st2 = STAT_ST_FIN; |
| 8224 | /* fall through */ |
| 8225 | |
| 8226 | default: |
| 8227 | appctx->st2 = STAT_ST_FIN; |
| 8228 | return 1; |
| 8229 | } |
| 8230 | return 0; |
| 8231 | } |
| 8232 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8233 | /* 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] | 8234 | static int cli_parse_show_tlskeys(char **args, struct appctx *appctx, void *private) |
| 8235 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8236 | /* no parameter, shows only file list */ |
| 8237 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8238 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8239 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 8240 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8241 | } |
| 8242 | |
| 8243 | if (args[2][0] == '*') { |
| 8244 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8245 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8246 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8247 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
| 8248 | if (!appctx->ctx.cli.p0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8249 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8250 | 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] | 8251 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8252 | return 1; |
| 8253 | } |
| 8254 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8255 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 8256 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8257 | } |
| 8258 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8259 | static int cli_parse_set_tlskeys(char **args, struct appctx *appctx, void *private) |
| 8260 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8261 | struct tls_keys_ref *ref; |
| 8262 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8263 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 8264 | if (!*args[3] || !*args[4]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8265 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8266 | 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] | 8267 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8268 | return 1; |
| 8269 | } |
| 8270 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8271 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 8272 | if (!ref) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8273 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8274 | 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] | 8275 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8276 | return 1; |
| 8277 | } |
| 8278 | |
| 8279 | trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size); |
| 8280 | if (trash.len != sizeof(struct tls_sess_key)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8281 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8282 | 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] | 8283 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8284 | return 1; |
| 8285 | } |
| 8286 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8287 | memcpy(ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len); |
| 8288 | 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] | 8289 | |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8290 | appctx->ctx.cli.severity = LOG_INFO; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8291 | appctx->ctx.cli.msg = "TLS ticket key updated!"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 8292 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8293 | return 1; |
| 8294 | |
| 8295 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 8296 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8297 | |
| 8298 | static int cli_parse_set_ocspresponse(char **args, struct appctx *appctx, void *private) |
| 8299 | { |
| 8300 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 8301 | char *err = NULL; |
| 8302 | |
| 8303 | /* Expect one parameter: the new response in base64 encoding */ |
| 8304 | if (!*args[3]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8305 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8306 | 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] | 8307 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8308 | return 1; |
| 8309 | } |
| 8310 | |
| 8311 | trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size); |
| 8312 | if (trash.len < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8313 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8314 | 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] | 8315 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8316 | return 1; |
| 8317 | } |
| 8318 | |
| 8319 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
| 8320 | if (err) { |
| 8321 | memprintf(&err, "%s.\n", err); |
| 8322 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 8323 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8324 | } |
| 8325 | return 1; |
| 8326 | } |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8327 | appctx->ctx.cli.severity = LOG_INFO; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8328 | appctx->ctx.cli.msg = "OCSP Response updated!"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 8329 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8330 | return 1; |
| 8331 | #else |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8332 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8333 | 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] | 8334 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8335 | return 1; |
| 8336 | #endif |
| 8337 | |
| 8338 | } |
| 8339 | |
| 8340 | /* register cli keywords */ |
| 8341 | static struct cli_kw_list cli_kws = {{ },{ |
| 8342 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8343 | { { "show", "tls-keys", NULL }, "show tls-keys [id|*]: show tls keys references or dump tls ticket keys when id specified", cli_parse_show_tlskeys, NULL }, |
Lukas Tribus | f4bbc43 | 2017-10-24 12:26:31 +0200 | [diff] [blame] | 8344 | { { "set", "ssl", "tls-key", NULL }, "set ssl tls-key [id|keyfile] <tlskey>: set the next TLS key for the <id> or <keyfile> listener to <tlskey>", cli_parse_set_tlskeys, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8345 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 8346 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8347 | { { NULL }, NULL, NULL, NULL } |
| 8348 | }}; |
| 8349 | |
| 8350 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8351 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8352 | * Please take care of keeping this list alphabetically sorted. |
| 8353 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 8354 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8355 | { "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] | 8356 | { "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] | 8357 | { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 8358 | { "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] | 8359 | { "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] | 8360 | { "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] | 8361 | { "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] | 8362 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 8363 | { "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] | 8364 | { "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] | 8365 | { "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] | 8366 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8367 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8368 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8369 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8370 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8371 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8372 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 8373 | { "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] | 8374 | { "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] | 8375 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 8376 | { "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] | 8377 | { "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] | 8378 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8379 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8380 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8381 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8382 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8383 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8384 | { "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] | 8385 | { "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] | 8386 | { "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] | 8387 | { "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] | 8388 | { "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] | 8389 | { "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] | 8390 | { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8391 | { "ssl_fc_has_early", smp_fetch_ssl_fc_has_early, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 8392 | { "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] | 8393 | { "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] | 8394 | #ifdef OPENSSL_NPN_NEGOTIATED |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8395 | { "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] | 8396 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 8397 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8398 | { "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] | 8399 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8400 | { "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] | 8401 | { "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] | 8402 | { "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] | 8403 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 8404 | { "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] | 8405 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8406 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 8407 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8408 | { "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] | 8409 | { NULL, NULL, 0, 0, 0 }, |
| 8410 | }}; |
| 8411 | |
| 8412 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8413 | * Please take care of keeping this list alphabetically sorted. |
| 8414 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 8415 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 8416 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 8417 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 8418 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8419 | }}; |
| 8420 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8421 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8422 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 8423 | * all code contributors. |
| 8424 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 8425 | * the config parser can report an appropriate error when a known keyword was |
| 8426 | * not enabled. |
| 8427 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8428 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8429 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8430 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 8431 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 8432 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 8433 | { "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] | 8434 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8435 | { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 8436 | { "no-ca-names", ssl_bind_parse_no_ca_names, 0 }, /* do not send ca names to clients (ca_file related) */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8437 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 8438 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 8439 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8440 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 8441 | { NULL, NULL, 0 }, |
| 8442 | }; |
| 8443 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 8444 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8445 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8446 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 8447 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 8448 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 8449 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 8450 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 8451 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 8452 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 8453 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 8454 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 8455 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 8456 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 8457 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 8458 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 8459 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 8460 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 8461 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 8462 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8463 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 8464 | { "no-ca-names", bind_parse_no_ca_names, 0 }, /* do not send ca names to clients (ca_file related) */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8465 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 8466 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 8467 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 8468 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 8469 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8470 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 8471 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8472 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 8473 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8474 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 8475 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 8476 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 8477 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 8478 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8479 | { NULL, NULL, 0 }, |
| 8480 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8481 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8482 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8483 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 8484 | * all code contributors. |
| 8485 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 8486 | * the config parser can report an appropriate error when a known keyword was |
| 8487 | * not enabled. |
| 8488 | */ |
| 8489 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8490 | { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */ |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 8491 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8492 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 8493 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
| 8494 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 8495 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 8496 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 8497 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 8498 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 8499 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 8500 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 8501 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 8502 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 8503 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 8504 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 8505 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 8506 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 8507 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 8508 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 8509 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 8510 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 8511 | { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */ |
| 8512 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 8513 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 8514 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 8515 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 8516 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 8517 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 8518 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 8519 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 8520 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 8521 | { "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] | 8522 | { NULL, NULL, 0, 0 }, |
| 8523 | }}; |
| 8524 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8525 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8526 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 8527 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 8528 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 8529 | { CFG_GLOBAL, "ssl-allow-0rtt", ssl_parse_global_ssl_allow_0rtt }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8530 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 8531 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 8532 | #ifndef OPENSSL_NO_DH |
| 8533 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 8534 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8535 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8536 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8537 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8538 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8539 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 8540 | #ifndef OPENSSL_NO_DH |
| 8541 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 8542 | #endif |
| 8543 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 8544 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 8545 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 8546 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8547 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8548 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 8549 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8550 | { 0, NULL, NULL }, |
| 8551 | }}; |
| 8552 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 8553 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 8554 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8555 | .snd_buf = ssl_sock_from_buf, |
| 8556 | .rcv_buf = ssl_sock_to_buf, |
| 8557 | .rcv_pipe = NULL, |
| 8558 | .snd_pipe = NULL, |
| 8559 | .shutr = NULL, |
| 8560 | .shutw = ssl_sock_shutw, |
| 8561 | .close = ssl_sock_close, |
| 8562 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 8563 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 8564 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 8565 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 8566 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 8567 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 8568 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8569 | }; |
| 8570 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8571 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 8572 | struct session *sess, struct stream *s, int flags) |
| 8573 | { |
| 8574 | struct connection *conn; |
| 8575 | |
| 8576 | conn = objt_conn(sess->origin); |
| 8577 | |
| 8578 | if (conn) { |
| 8579 | if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) { |
| 8580 | s->req.flags |= CF_READ_NULL; |
| 8581 | return ACT_RET_YIELD; |
| 8582 | } |
| 8583 | } |
| 8584 | return (ACT_RET_CONT); |
| 8585 | } |
| 8586 | |
| 8587 | static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err) |
| 8588 | { |
| 8589 | rule->action_ptr = ssl_action_wait_for_hs; |
| 8590 | |
| 8591 | return ACT_RET_PRS_OK; |
| 8592 | } |
| 8593 | |
| 8594 | static struct action_kw_list http_req_actions = {ILH, { |
| 8595 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 8596 | { /* END */ } |
| 8597 | }}; |
| 8598 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 8599 | #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] | 8600 | |
| 8601 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8602 | { |
| 8603 | if (ptr) { |
| 8604 | chunk_destroy(ptr); |
| 8605 | free(ptr); |
| 8606 | } |
| 8607 | } |
| 8608 | |
| 8609 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8610 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8611 | { |
| 8612 | pool_free2(pool2_ssl_capture, ptr); |
| 8613 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 8614 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8615 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8616 | static void __ssl_sock_init(void) |
| 8617 | { |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8618 | char *ptr; |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 8619 | int i; |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8620 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8621 | STACK_OF(SSL_COMP)* cm; |
| 8622 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8623 | if (global_ssl.listen_default_ciphers) |
| 8624 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 8625 | if (global_ssl.connect_default_ciphers) |
| 8626 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 8627 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 8628 | xprt_register(XPRT_SSL, &ssl_sock); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8629 | SSL_library_init(); |
| 8630 | cm = SSL_COMP_get_compression_methods(); |
| 8631 | sk_SSL_COMP_zero(cm); |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 8632 | #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] | 8633 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 8634 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8635 | 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] | 8636 | sample_register_fetches(&sample_fetch_keywords); |
| 8637 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8638 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8639 | srv_register_keywords(&srv_kws); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8640 | cfg_register_keywords(&cfg_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8641 | cli_register_kw(&cli_kws); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8642 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8643 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8644 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8645 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 8646 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8647 | hap_register_post_check(tlskeys_finalize_config); |
| 8648 | #endif |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 8649 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8650 | ptr = NULL; |
| 8651 | memprintf(&ptr, "Built with OpenSSL version : " |
| 8652 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 8653 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8654 | #else /* OPENSSL_IS_BORINGSSL */ |
| 8655 | OPENSSL_VERSION_TEXT |
| 8656 | "\nRunning on OpenSSL version : %s%s", |
| 8657 | SSLeay_version(SSLEAY_VERSION), |
| 8658 | ((OPENSSL_VERSION_NUMBER ^ SSLeay()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
| 8659 | #endif |
| 8660 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
| 8661 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 8662 | "no (library version too old)" |
| 8663 | #elif defined(OPENSSL_NO_TLSEXT) |
| 8664 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 8665 | #else |
| 8666 | "yes" |
| 8667 | #endif |
| 8668 | "", ptr); |
| 8669 | |
| 8670 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 8671 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 8672 | "yes" |
| 8673 | #else |
| 8674 | #ifdef OPENSSL_NO_TLSEXT |
| 8675 | "no (because of OPENSSL_NO_TLSEXT)" |
| 8676 | #else |
| 8677 | "no (version might be too old, 0.9.8f min needed)" |
| 8678 | #endif |
| 8679 | #endif |
| 8680 | "", ptr); |
| 8681 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 8682 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 8683 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 8684 | if (methodVersions[i].option) |
| 8685 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 8686 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8687 | hap_register_build_opts(ptr, 1); |
| 8688 | |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 8689 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 8690 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 8691 | |
| 8692 | #ifndef OPENSSL_NO_DH |
| 8693 | 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] | 8694 | hap_register_post_deinit(ssl_free_dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 8695 | #endif |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8696 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8697 | hap_register_post_deinit(ssl_free_engines); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8698 | #endif |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 8699 | /* Load SSL string for the verbose & debug mode. */ |
| 8700 | ERR_load_SSL_strings(); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8701 | |
| 8702 | http_req_keywords_register(&http_req_actions); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8703 | } |
| 8704 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8705 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8706 | void ssl_free_engines(void) { |
| 8707 | struct ssl_engine_list *wl, *wlb; |
| 8708 | /* free up engine list */ |
| 8709 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 8710 | ENGINE_finish(wl->e); |
| 8711 | ENGINE_free(wl->e); |
| 8712 | LIST_DEL(&wl->list); |
| 8713 | free(wl); |
| 8714 | } |
| 8715 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8716 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8717 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8718 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8719 | void ssl_free_dh(void) { |
| 8720 | if (local_dh_1024) { |
| 8721 | DH_free(local_dh_1024); |
| 8722 | local_dh_1024 = NULL; |
| 8723 | } |
| 8724 | if (local_dh_2048) { |
| 8725 | DH_free(local_dh_2048); |
| 8726 | local_dh_2048 = NULL; |
| 8727 | } |
| 8728 | if (local_dh_4096) { |
| 8729 | DH_free(local_dh_4096); |
| 8730 | local_dh_4096 = NULL; |
| 8731 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 8732 | if (global_dh) { |
| 8733 | DH_free(global_dh); |
| 8734 | global_dh = NULL; |
| 8735 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8736 | } |
| 8737 | #endif |
| 8738 | |
| 8739 | __attribute__((destructor)) |
| 8740 | static void __ssl_sock_deinit(void) |
| 8741 | { |
| 8742 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
| 8743 | lru64_destroy(ssl_ctx_lru_tree); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8744 | #endif |
| 8745 | |
| 8746 | ERR_remove_state(0); |
| 8747 | ERR_free_strings(); |
| 8748 | |
| 8749 | EVP_cleanup(); |
| 8750 | |
| 8751 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 8752 | CRYPTO_cleanup_all_ex_data(); |
| 8753 | #endif |
| 8754 | } |
| 8755 | |
| 8756 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8757 | /* |
| 8758 | * Local variables: |
| 8759 | * c-indent-level: 8 |
| 8760 | * c-basic-offset: 8 |
| 8761 | * End: |
| 8762 | */ |