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> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 46 | #include <openssl/err.h> |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 47 | #include <openssl/rand.h> |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 48 | #include <openssl/hmac.h> |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 49 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 50 | #include <openssl/ocsp.h> |
| 51 | #endif |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 52 | #ifndef OPENSSL_NO_DH |
| 53 | #include <openssl/dh.h> |
| 54 | #endif |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 55 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 56 | #include <openssl/engine.h> |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 57 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 58 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 59 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 60 | #include <openssl/async.h> |
| 61 | #endif |
| 62 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 63 | #include <import/lru.h> |
| 64 | #include <import/xxhash.h> |
| 65 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 66 | #include <common/buffer.h> |
| 67 | #include <common/compat.h> |
| 68 | #include <common/config.h> |
| 69 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 70 | #include <common/errors.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 71 | #include <common/standard.h> |
| 72 | #include <common/ticks.h> |
| 73 | #include <common/time.h> |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 74 | #include <common/cfgparse.h> |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 75 | #include <common/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 76 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 77 | #include <ebsttree.h> |
| 78 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 79 | #include <types/applet.h> |
| 80 | #include <types/cli.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 81 | #include <types/global.h> |
| 82 | #include <types/ssl_sock.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 83 | #include <types/stats.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 84 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 85 | #include <proto/acl.h> |
| 86 | #include <proto/arg.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 87 | #include <proto/channel.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 88 | #include <proto/connection.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 89 | #include <proto/cli.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 90 | #include <proto/fd.h> |
| 91 | #include <proto/freq_ctr.h> |
| 92 | #include <proto/frontend.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 93 | #include <proto/listener.h> |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 94 | #include <proto/openssl-compat.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 95 | #include <proto/pattern.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 96 | #include <proto/proto_tcp.h> |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 97 | #include <proto/proto_http.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 98 | #include <proto/server.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 99 | #include <proto/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 100 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 101 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 102 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 103 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 104 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 105 | #include <proto/task.h> |
| 106 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 107 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 108 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 109 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 110 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 111 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 112 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 113 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 114 | |
| 115 | /* Verify errors macros */ |
| 116 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 117 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 118 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 119 | |
| 120 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 121 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 122 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 123 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 124 | /* Supported hash function for TLS tickets */ |
| 125 | #ifdef OPENSSL_NO_SHA256 |
| 126 | #define HASH_FUNCT EVP_sha1 |
| 127 | #else |
| 128 | #define HASH_FUNCT EVP_sha256 |
| 129 | #endif /* OPENSSL_NO_SHA256 */ |
| 130 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 131 | /* ssl_methods flags for ssl options */ |
| 132 | #define MC_SSL_O_ALL 0x0000 |
| 133 | #define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */ |
| 134 | #define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */ |
| 135 | #define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */ |
| 136 | #define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 137 | #define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 138 | |
| 139 | /* ssl_methods versions */ |
| 140 | enum { |
| 141 | CONF_TLSV_NONE = 0, |
| 142 | CONF_TLSV_MIN = 1, |
| 143 | CONF_SSLV3 = 1, |
| 144 | CONF_TLSV10 = 2, |
| 145 | CONF_TLSV11 = 3, |
| 146 | CONF_TLSV12 = 4, |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 147 | CONF_TLSV13 = 5, |
| 148 | CONF_TLSV_MAX = 5, |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 149 | }; |
| 150 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 151 | /* server and bind verify method, it uses a global value as default */ |
| 152 | enum { |
| 153 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 154 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 155 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 156 | SSL_SOCK_VERIFY_NONE = 3, |
| 157 | }; |
| 158 | |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 159 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 160 | int sslconns = 0; |
| 161 | int totalsslconns = 0; |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 162 | static struct xprt_ops ssl_sock; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 163 | int nb_engines = 0; |
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 */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 169 | |
| 170 | char *listen_default_ciphers; |
| 171 | char *connect_default_ciphers; |
| 172 | int listen_default_ssloptions; |
| 173 | int connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 174 | struct tls_version_filter listen_default_sslmethods; |
| 175 | struct tls_version_filter connect_default_sslmethods; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 176 | |
| 177 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 178 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 179 | unsigned int max_record; /* SSL max record size */ |
| 180 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 181 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 182 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 183 | } global_ssl = { |
| 184 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 185 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 186 | #endif |
| 187 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 188 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 189 | #endif |
| 190 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 191 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 192 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 193 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 194 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 195 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 196 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 197 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 198 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 199 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 200 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 201 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 202 | #endif |
| 203 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 204 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 205 | .capture_cipherlist = 0, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 206 | }; |
| 207 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 208 | #ifdef USE_THREAD |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 209 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 210 | static HA_RWLOCK_T *ssl_rwlocks; |
| 211 | |
| 212 | |
| 213 | unsigned long ssl_id_function(void) |
| 214 | { |
| 215 | return (unsigned long)tid; |
| 216 | } |
| 217 | |
| 218 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 219 | { |
| 220 | if (mode & CRYPTO_LOCK) { |
| 221 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 222 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 223 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 224 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 225 | } |
| 226 | else { |
| 227 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 228 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 229 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 230 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | static int ssl_locking_init(void) |
| 235 | { |
| 236 | int i; |
| 237 | |
| 238 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 239 | if (!ssl_rwlocks) |
| 240 | return -1; |
| 241 | |
| 242 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 243 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 244 | |
| 245 | CRYPTO_set_id_callback(ssl_id_function); |
| 246 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 247 | |
| 248 | return 0; |
| 249 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 250 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 251 | #endif |
| 252 | |
| 253 | |
| 254 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 255 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 256 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 257 | unsigned long long int xxh64; |
| 258 | unsigned char ciphersuite_len; |
| 259 | char ciphersuite[0]; |
| 260 | }; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 261 | struct pool_head *pool_head_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 262 | static int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame^] | 263 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 264 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 265 | static int ssl_pkey_info_index = -1; |
| 266 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 267 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 268 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 269 | #endif |
| 270 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 271 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 272 | static unsigned int openssl_engines_initialized; |
| 273 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 274 | struct ssl_engine_list { |
| 275 | struct list list; |
| 276 | ENGINE *e; |
| 277 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 278 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 279 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 280 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 281 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 282 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 283 | static DH *local_dh_1024 = NULL; |
| 284 | static DH *local_dh_2048 = NULL; |
| 285 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 286 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 287 | #endif /* OPENSSL_NO_DH */ |
| 288 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 289 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 290 | /* X509V3 Extensions that will be added on generated certificates */ |
| 291 | #define X509V3_EXT_SIZE 5 |
| 292 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 293 | "basicConstraints", |
| 294 | "nsComment", |
| 295 | "subjectKeyIdentifier", |
| 296 | "authorityKeyIdentifier", |
| 297 | "keyUsage", |
| 298 | }; |
| 299 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 300 | "CA:FALSE", |
| 301 | "\"OpenSSL Generated Certificate\"", |
| 302 | "hash", |
| 303 | "keyid,issuer:always", |
| 304 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 305 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 306 | /* LRU cache to store generated certificate */ |
| 307 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 308 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 309 | static unsigned int ssl_ctx_serial; |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 310 | __decl_hathreads(static HA_RWLOCK_T ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 311 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 312 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 313 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 314 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 315 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 316 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 317 | /* The order here matters for picking a default context, |
| 318 | * keep the most common keytype at the bottom of the list |
| 319 | */ |
| 320 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 321 | "dsa", |
| 322 | "ecdsa", |
| 323 | "rsa" |
| 324 | }; |
| 325 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 326 | #else |
| 327 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 328 | #endif |
| 329 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 330 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 331 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 332 | |
| 333 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 334 | |
| 335 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 336 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 337 | |
| 338 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 339 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 340 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 341 | /* |
| 342 | * This function gives the detail of the SSL error. It is used only |
| 343 | * if the debug mode and the verbose mode are activated. It dump all |
| 344 | * the SSL error until the stack was empty. |
| 345 | */ |
| 346 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 347 | { |
| 348 | unsigned long ret; |
| 349 | |
| 350 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 351 | while(1) { |
| 352 | ret = ERR_get_error(); |
| 353 | if (ret == 0) |
| 354 | return; |
| 355 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 356 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 357 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 362 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 363 | /* |
| 364 | * struct alignment works here such that the key.key is the same as key_data |
| 365 | * Do not change the placement of key_data |
| 366 | */ |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 367 | struct certificate_ocsp { |
| 368 | struct ebmb_node key; |
| 369 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 370 | struct chunk response; |
| 371 | long expire; |
| 372 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 373 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 374 | struct ocsp_cbk_arg { |
| 375 | int is_single; |
| 376 | int single_kt; |
| 377 | union { |
| 378 | struct certificate_ocsp *s_ocsp; |
| 379 | /* |
| 380 | * m_ocsp will have multiple entries dependent on key type |
| 381 | * Entry 0 - DSA |
| 382 | * Entry 1 - ECDSA |
| 383 | * Entry 2 - RSA |
| 384 | */ |
| 385 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 386 | }; |
| 387 | }; |
| 388 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 389 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 390 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 391 | { |
| 392 | int err_code = ERR_ABORT; |
| 393 | ENGINE *engine; |
| 394 | struct ssl_engine_list *el; |
| 395 | |
| 396 | /* grab the structural reference to the engine */ |
| 397 | engine = ENGINE_by_id(engine_id); |
| 398 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 399 | ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 400 | goto fail_get; |
| 401 | } |
| 402 | |
| 403 | if (!ENGINE_init(engine)) { |
| 404 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 405 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 406 | goto fail_init; |
| 407 | } |
| 408 | |
| 409 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 410 | ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 411 | goto fail_set_method; |
| 412 | } |
| 413 | |
| 414 | el = calloc(1, sizeof(*el)); |
| 415 | el->e = engine; |
| 416 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 417 | nb_engines++; |
| 418 | if (global_ssl.async) |
| 419 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 420 | return 0; |
| 421 | |
| 422 | fail_set_method: |
| 423 | /* release the functional reference from ENGINE_init() */ |
| 424 | ENGINE_finish(engine); |
| 425 | |
| 426 | fail_init: |
| 427 | /* release the structural reference from ENGINE_by_id() */ |
| 428 | ENGINE_free(engine); |
| 429 | |
| 430 | fail_get: |
| 431 | return err_code; |
| 432 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 433 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 434 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 435 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 436 | /* |
| 437 | * openssl async fd handler |
| 438 | */ |
| 439 | static void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 440 | { |
| 441 | struct connection *conn = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 442 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 443 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 444 | * to poll this fd until it is requested |
| 445 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 446 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 447 | fd_cant_recv(fd); |
| 448 | |
| 449 | /* crypto engine is available, let's notify the associated |
| 450 | * connection that it can pursue its processing. |
| 451 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 452 | __conn_sock_want_recv(conn); |
| 453 | __conn_sock_want_send(conn); |
| 454 | conn_update_sock_polling(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 457 | /* |
| 458 | * openssl async delayed SSL_free handler |
| 459 | */ |
| 460 | static void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 461 | { |
| 462 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 463 | OSSL_ASYNC_FD all_fd[32]; |
| 464 | size_t num_all_fds = 0; |
| 465 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 466 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 467 | /* We suppose that the async job for a same SSL * |
| 468 | * are serialized. So if we are awake it is |
| 469 | * because the running job has just finished |
| 470 | * and we can remove all async fds safely |
| 471 | */ |
| 472 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 473 | if (num_all_fds > 32) { |
| 474 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 479 | for (i=0 ; i < num_all_fds ; i++) |
| 480 | fd_remove(all_fd[i]); |
| 481 | |
| 482 | /* 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] | 483 | SSL_free(ssl); |
| 484 | sslconns--; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 485 | HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 486 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 487 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 488 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 489 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 490 | */ |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 491 | static void inline ssl_async_process_fds(struct connection *conn, SSL *ssl) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 492 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 493 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 494 | OSSL_ASYNC_FD del_fd[32]; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 495 | size_t num_add_fds = 0; |
| 496 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 497 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 498 | |
| 499 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 500 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 501 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 502 | 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] | 503 | return; |
| 504 | } |
| 505 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 506 | 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] | 507 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 508 | /* We remove unused fds from the fdtab */ |
| 509 | for (i=0 ; i < num_del_fds ; i++) |
| 510 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 511 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 512 | /* We add new fds to the fdtab */ |
| 513 | for (i=0 ; i < num_add_fds ; i++) { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 514 | fd_insert(add_fd[i], conn, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 517 | num_add_fds = 0; |
| 518 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 519 | if (num_add_fds > 32) { |
| 520 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 521 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 522 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 523 | |
| 524 | /* We activate the polling for all known async fds */ |
| 525 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 526 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 527 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 528 | /* To ensure that the fd cache won't be used |
| 529 | * We'll prefer to catch a real RD event |
| 530 | * because handling an EAGAIN on this fd will |
| 531 | * result in a context switch and also |
| 532 | * some engines uses a fd in blocking mode. |
| 533 | */ |
| 534 | fd_cant_recv(add_fd[i]); |
| 535 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 536 | |
| 537 | /* We must also prevent the conn_handler |
| 538 | * to be called until a read event was |
| 539 | * polled on an async fd |
| 540 | */ |
| 541 | __conn_sock_stop_both(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 542 | } |
| 543 | #endif |
| 544 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 545 | /* |
| 546 | * This function returns the number of seconds elapsed |
| 547 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 548 | * date presented un ASN1_GENERALIZEDTIME. |
| 549 | * |
| 550 | * In parsing error case, it returns -1. |
| 551 | */ |
| 552 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 553 | { |
| 554 | long epoch; |
| 555 | char *p, *end; |
| 556 | const unsigned short month_offset[12] = { |
| 557 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 558 | }; |
| 559 | int year, month; |
| 560 | |
| 561 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 562 | |
| 563 | p = (char *)d->data; |
| 564 | end = p + d->length; |
| 565 | |
| 566 | if (end - p < 4) return -1; |
| 567 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 568 | p += 4; |
| 569 | if (end - p < 2) return -1; |
| 570 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 571 | if (month < 1 || month > 12) return -1; |
| 572 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 573 | We consider leap years and the current month (<marsh or not) */ |
| 574 | epoch = ( ((year - 1970) * 365) |
| 575 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 576 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 577 | + month_offset[month-1] |
| 578 | ) * 24 * 60 * 60; |
| 579 | p += 2; |
| 580 | if (end - p < 2) return -1; |
| 581 | /* Add the number of seconds of completed days of current month */ |
| 582 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 583 | p += 2; |
| 584 | if (end - p < 2) return -1; |
| 585 | /* Add the completed hours of the current day */ |
| 586 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 587 | p += 2; |
| 588 | if (end - p < 2) return -1; |
| 589 | /* Add the completed minutes of the current hour */ |
| 590 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 591 | p += 2; |
| 592 | if (p == end) return -1; |
| 593 | /* Test if there is available seconds */ |
| 594 | if (p[0] < '0' || p[0] > '9') |
| 595 | goto nosec; |
| 596 | if (end - p < 2) return -1; |
| 597 | /* Add the seconds of the current minute */ |
| 598 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 599 | p += 2; |
| 600 | if (p == end) return -1; |
| 601 | /* Ignore seconds float part if present */ |
| 602 | if (p[0] == '.') { |
| 603 | do { |
| 604 | if (++p == end) return -1; |
| 605 | } while (p[0] >= '0' && p[0] <= '9'); |
| 606 | } |
| 607 | |
| 608 | nosec: |
| 609 | if (p[0] == 'Z') { |
| 610 | if (end - p != 1) return -1; |
| 611 | return epoch; |
| 612 | } |
| 613 | else if (p[0] == '+') { |
| 614 | if (end - p != 5) return -1; |
| 615 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 616 | 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] | 617 | } |
| 618 | else if (p[0] == '-') { |
| 619 | if (end - p != 5) return -1; |
| 620 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 621 | 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] | 622 | } |
| 623 | |
| 624 | return -1; |
| 625 | } |
| 626 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 627 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 628 | |
| 629 | /* This function starts to check if the OCSP response (in DER format) contained |
| 630 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 631 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 632 | * contained in the OCSP Response and exits on error if no match. |
| 633 | * If it's a valid OCSP Response: |
| 634 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 635 | * pointed by 'ocsp'. |
| 636 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 637 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 638 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 639 | * already present in the container, it will be overwritten. |
| 640 | * |
| 641 | * Note: OCSP response containing more than one OCSP Single response is not |
| 642 | * considered valid. |
| 643 | * |
| 644 | * Returns 0 on success, 1 in error case. |
| 645 | */ |
| 646 | static int ssl_sock_load_ocsp_response(struct chunk *ocsp_response, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 647 | { |
| 648 | OCSP_RESPONSE *resp; |
| 649 | OCSP_BASICRESP *bs = NULL; |
| 650 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 651 | OCSP_CERTID *id; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 652 | unsigned char *p = (unsigned char *)ocsp_response->str; |
| 653 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 654 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 655 | int reason; |
| 656 | int ret = 1; |
| 657 | |
| 658 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, ocsp_response->len); |
| 659 | if (!resp) { |
| 660 | memprintf(err, "Unable to parse OCSP response"); |
| 661 | goto out; |
| 662 | } |
| 663 | |
| 664 | rc = OCSP_response_status(resp); |
| 665 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 666 | memprintf(err, "OCSP response status not successful"); |
| 667 | goto out; |
| 668 | } |
| 669 | |
| 670 | bs = OCSP_response_get1_basic(resp); |
| 671 | if (!bs) { |
| 672 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 673 | goto out; |
| 674 | } |
| 675 | |
| 676 | count_sr = OCSP_resp_count(bs); |
| 677 | if (count_sr > 1) { |
| 678 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 679 | goto out; |
| 680 | } |
| 681 | |
| 682 | sr = OCSP_resp_get0(bs, 0); |
| 683 | if (!sr) { |
| 684 | memprintf(err, "Failed to get OCSP single response"); |
| 685 | goto out; |
| 686 | } |
| 687 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 688 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 689 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 690 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 691 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 692 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 693 | goto out; |
| 694 | } |
| 695 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 696 | if (!nextupd) { |
| 697 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 698 | goto out; |
| 699 | } |
| 700 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 701 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 702 | if (!rc) { |
| 703 | memprintf(err, "OCSP single response: no longer valid."); |
| 704 | goto out; |
| 705 | } |
| 706 | |
| 707 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 708 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 709 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 710 | goto out; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | if (!ocsp) { |
| 715 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 716 | unsigned char *p; |
| 717 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 718 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 719 | if (!rc) { |
| 720 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 721 | goto out; |
| 722 | } |
| 723 | |
| 724 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 725 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 726 | goto out; |
| 727 | } |
| 728 | |
| 729 | p = key; |
| 730 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 731 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 732 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 733 | if (!ocsp) { |
| 734 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 735 | goto out; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | /* According to comments on "chunk_dup", the |
| 740 | previous chunk buffer will be freed */ |
| 741 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 742 | memprintf(err, "OCSP response: Memory allocation error"); |
| 743 | goto out; |
| 744 | } |
| 745 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 746 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 747 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 748 | ret = 0; |
| 749 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 750 | ERR_clear_error(); |
| 751 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 752 | if (bs) |
| 753 | OCSP_BASICRESP_free(bs); |
| 754 | |
| 755 | if (resp) |
| 756 | OCSP_RESPONSE_free(resp); |
| 757 | |
| 758 | return ret; |
| 759 | } |
| 760 | /* |
| 761 | * External function use to update the OCSP response in the OCSP response's |
| 762 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 763 | * to update in DER format. |
| 764 | * |
| 765 | * Returns 0 on success, 1 in error case. |
| 766 | */ |
| 767 | int ssl_sock_update_ocsp_response(struct chunk *ocsp_response, char **err) |
| 768 | { |
| 769 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * This function load the OCSP Resonse in DER format contained in file at |
| 774 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 775 | * |
| 776 | * Returns 0 on success, 1 in error case. |
| 777 | */ |
| 778 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 779 | { |
| 780 | int fd = -1; |
| 781 | int r = 0; |
| 782 | int ret = 1; |
| 783 | |
| 784 | fd = open(ocsp_path, O_RDONLY); |
| 785 | if (fd == -1) { |
| 786 | memprintf(err, "Error opening OCSP response file"); |
| 787 | goto end; |
| 788 | } |
| 789 | |
| 790 | trash.len = 0; |
| 791 | while (trash.len < trash.size) { |
| 792 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 793 | if (r < 0) { |
| 794 | if (errno == EINTR) |
| 795 | continue; |
| 796 | |
| 797 | memprintf(err, "Error reading OCSP response from file"); |
| 798 | goto end; |
| 799 | } |
| 800 | else if (r == 0) { |
| 801 | break; |
| 802 | } |
| 803 | trash.len += r; |
| 804 | } |
| 805 | |
| 806 | close(fd); |
| 807 | fd = -1; |
| 808 | |
| 809 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 810 | end: |
| 811 | if (fd != -1) |
| 812 | close(fd); |
| 813 | |
| 814 | return ret; |
| 815 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 816 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 817 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 818 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 819 | 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) |
| 820 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 821 | struct tls_keys_ref *ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 822 | struct tls_sess_key *keys; |
| 823 | struct connection *conn; |
| 824 | int head; |
| 825 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 826 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 827 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame^] | 828 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 829 | ref = objt_listener(conn->target)->bind_conf->keys_ref; |
| 830 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 831 | |
| 832 | keys = ref->tlskeys; |
| 833 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 834 | |
| 835 | if (enc) { |
| 836 | memcpy(key_name, keys[head].name, 16); |
| 837 | |
| 838 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 839 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 840 | |
| 841 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].aes_key, iv)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 842 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 843 | |
| 844 | HMAC_Init_ex(hctx, keys[head].hmac_key, 16, HASH_FUNCT(), NULL); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 845 | ret = 1; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 846 | } else { |
| 847 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 848 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 849 | goto found; |
| 850 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 851 | ret = 0; |
| 852 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 853 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 854 | found: |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 855 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].hmac_key, 16, HASH_FUNCT(), NULL); |
| 856 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].aes_key, iv)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 857 | goto end; |
| 858 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 859 | /* 2 for key renewal, 1 if current key is still valid */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 860 | ret = i ? 2 : 1; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 861 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 862 | end: |
| 863 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 864 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 868 | { |
| 869 | struct tls_keys_ref *ref; |
| 870 | |
| 871 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 872 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 873 | return ref; |
| 874 | return NULL; |
| 875 | } |
| 876 | |
| 877 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 878 | { |
| 879 | struct tls_keys_ref *ref; |
| 880 | |
| 881 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 882 | if (ref->unique_id == unique_id) |
| 883 | return ref; |
| 884 | return NULL; |
| 885 | } |
| 886 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 887 | void ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, struct chunk *tlskey) |
| 888 | { |
| 889 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 890 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), tlskey->str, tlskey->len); |
| 891 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 892 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 893 | } |
| 894 | |
| 895 | int ssl_sock_update_tlskey(char *filename, struct chunk *tlskey, char **err) |
| 896 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 897 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 898 | |
| 899 | if(!ref) { |
| 900 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 901 | return 1; |
| 902 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 903 | ssl_sock_update_tlskey_ref(ref, tlskey); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 904 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 905 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 906 | |
| 907 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 908 | * automatic ids. It's called just after the basic checks. It returns |
| 909 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 910 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 911 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 912 | { |
| 913 | int i = 0; |
| 914 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 915 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 916 | |
| 917 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 918 | if (ref->unique_id == -1) { |
| 919 | /* Look for the first free id. */ |
| 920 | while (1) { |
| 921 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 922 | if (ref2->unique_id == i) { |
| 923 | i++; |
| 924 | break; |
| 925 | } |
| 926 | } |
| 927 | if (&ref2->list == &tlskeys_reference) |
| 928 | break; |
| 929 | } |
| 930 | |
| 931 | /* Uses the unique id and increment it for the next entry. */ |
| 932 | ref->unique_id = i; |
| 933 | i++; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | /* This sort the reference list by id. */ |
| 938 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 939 | LIST_DEL(&ref->list); |
| 940 | list_for_each_entry(ref3, &tkr, list) { |
| 941 | if (ref->unique_id < ref3->unique_id) { |
| 942 | LIST_ADDQ(&ref3->list, &ref->list); |
| 943 | break; |
| 944 | } |
| 945 | } |
| 946 | if (&ref3->list == &tkr) |
| 947 | LIST_ADDQ(&tkr, &ref->list); |
| 948 | } |
| 949 | |
| 950 | /* swap root */ |
| 951 | LIST_ADD(&tkr, &tlskeys_reference); |
| 952 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 953 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 954 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 955 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 956 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 957 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 958 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 959 | { |
| 960 | switch (evp_keytype) { |
| 961 | case EVP_PKEY_RSA: |
| 962 | return 2; |
| 963 | case EVP_PKEY_DSA: |
| 964 | return 0; |
| 965 | case EVP_PKEY_EC: |
| 966 | return 1; |
| 967 | } |
| 968 | |
| 969 | return -1; |
| 970 | } |
| 971 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 972 | /* |
| 973 | * Callback used to set OCSP status extension content in server hello. |
| 974 | */ |
| 975 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 976 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 977 | struct certificate_ocsp *ocsp; |
| 978 | struct ocsp_cbk_arg *ocsp_arg; |
| 979 | char *ssl_buf; |
| 980 | EVP_PKEY *ssl_pkey; |
| 981 | int key_type; |
| 982 | int index; |
| 983 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 984 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 985 | |
| 986 | ssl_pkey = SSL_get_privatekey(ssl); |
| 987 | if (!ssl_pkey) |
| 988 | return SSL_TLSEXT_ERR_NOACK; |
| 989 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 990 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 991 | |
| 992 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 993 | ocsp = ocsp_arg->s_ocsp; |
| 994 | else { |
| 995 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 996 | * the certificate type |
| 997 | */ |
| 998 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 999 | |
| 1000 | if (index < 0) |
| 1001 | return SSL_TLSEXT_ERR_NOACK; |
| 1002 | |
| 1003 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1004 | |
| 1005 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1006 | |
| 1007 | if (!ocsp || |
| 1008 | !ocsp->response.str || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1009 | !ocsp->response.len || |
| 1010 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1011 | return SSL_TLSEXT_ERR_NOACK; |
| 1012 | |
| 1013 | ssl_buf = OPENSSL_malloc(ocsp->response.len); |
| 1014 | if (!ssl_buf) |
| 1015 | return SSL_TLSEXT_ERR_NOACK; |
| 1016 | |
| 1017 | memcpy(ssl_buf, ocsp->response.str, ocsp->response.len); |
| 1018 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.len); |
| 1019 | |
| 1020 | return SSL_TLSEXT_ERR_OK; |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 1025 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 1026 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 1027 | * It should be present in the certificate's extra chain builded from file |
| 1028 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 1029 | * named 'cert_path' suffixed using '.issuer'. |
| 1030 | * |
| 1031 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 1032 | * response. If file is empty or content is not a valid OCSP response, |
| 1033 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 1034 | * is displayed). |
| 1035 | * |
| 1036 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
| 1037 | * succesfully enabled, or -1 in other error case. |
| 1038 | */ |
| 1039 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 1040 | { |
| 1041 | |
| 1042 | BIO *in = NULL; |
| 1043 | X509 *x, *xi = NULL, *issuer = NULL; |
| 1044 | STACK_OF(X509) *chain = NULL; |
| 1045 | OCSP_CERTID *cid = NULL; |
| 1046 | SSL *ssl; |
| 1047 | char ocsp_path[MAXPATHLEN+1]; |
| 1048 | int i, ret = -1; |
| 1049 | struct stat st; |
| 1050 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1051 | char *warn = NULL; |
| 1052 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1053 | pem_password_cb *passwd_cb; |
| 1054 | void *passwd_cb_userdata; |
| 1055 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1056 | |
| 1057 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1058 | |
| 1059 | if (stat(ocsp_path, &st)) |
| 1060 | return 1; |
| 1061 | |
| 1062 | ssl = SSL_new(ctx); |
| 1063 | if (!ssl) |
| 1064 | goto out; |
| 1065 | |
| 1066 | x = SSL_get_certificate(ssl); |
| 1067 | if (!x) |
| 1068 | goto out; |
| 1069 | |
| 1070 | /* Try to lookup for issuer in certificate extra chain */ |
| 1071 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 1072 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 1073 | #else |
| 1074 | chain = ctx->extra_certs; |
| 1075 | #endif |
| 1076 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1077 | issuer = sk_X509_value(chain, i); |
| 1078 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 1079 | break; |
| 1080 | else |
| 1081 | issuer = NULL; |
| 1082 | } |
| 1083 | |
| 1084 | /* If not found try to load issuer from a suffixed file */ |
| 1085 | if (!issuer) { |
| 1086 | char issuer_path[MAXPATHLEN+1]; |
| 1087 | |
| 1088 | in = BIO_new(BIO_s_file()); |
| 1089 | if (!in) |
| 1090 | goto out; |
| 1091 | |
| 1092 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 1093 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 1094 | goto out; |
| 1095 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1096 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 1097 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 1098 | |
| 1099 | 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] | 1100 | if (!xi) |
| 1101 | goto out; |
| 1102 | |
| 1103 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 1104 | goto out; |
| 1105 | |
| 1106 | issuer = xi; |
| 1107 | } |
| 1108 | |
| 1109 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1110 | if (!cid) |
| 1111 | goto out; |
| 1112 | |
| 1113 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1114 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1115 | goto out; |
| 1116 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1117 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1118 | if (!ocsp) |
| 1119 | goto out; |
| 1120 | |
| 1121 | p = ocsp->key_data; |
| 1122 | i2d_OCSP_CERTID(cid, &p); |
| 1123 | |
| 1124 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1125 | if (iocsp == ocsp) |
| 1126 | ocsp = NULL; |
| 1127 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1128 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1129 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1130 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1131 | #endif |
| 1132 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1133 | |
| 1134 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1135 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1136 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1137 | |
| 1138 | cb_arg->is_single = 1; |
| 1139 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1140 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1141 | pkey = X509_get_pubkey(x); |
| 1142 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1143 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1144 | |
| 1145 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1146 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1147 | } else { |
| 1148 | /* |
| 1149 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1150 | * Update that cb_arg with the new cert's staple |
| 1151 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1152 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1153 | struct certificate_ocsp *tmp_ocsp; |
| 1154 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1155 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1156 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1157 | |
| 1158 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1159 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1160 | #else |
| 1161 | cb_arg = ctx->tlsext_status_arg; |
| 1162 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1163 | |
| 1164 | /* |
| 1165 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1166 | * the order of operations below matter, take care when changing it |
| 1167 | */ |
| 1168 | tmp_ocsp = cb_arg->s_ocsp; |
| 1169 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1170 | cb_arg->s_ocsp = NULL; |
| 1171 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1172 | cb_arg->is_single = 0; |
| 1173 | cb_arg->single_kt = 0; |
| 1174 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1175 | pkey = X509_get_pubkey(x); |
| 1176 | key_type = EVP_PKEY_base_id(pkey); |
| 1177 | EVP_PKEY_free(pkey); |
| 1178 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1179 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1180 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1181 | cb_arg->m_ocsp[index] = iocsp; |
| 1182 | |
| 1183 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1184 | |
| 1185 | ret = 0; |
| 1186 | |
| 1187 | warn = NULL; |
| 1188 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 1189 | memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1190 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | out: |
| 1194 | if (ssl) |
| 1195 | SSL_free(ssl); |
| 1196 | |
| 1197 | if (in) |
| 1198 | BIO_free(in); |
| 1199 | |
| 1200 | if (xi) |
| 1201 | X509_free(xi); |
| 1202 | |
| 1203 | if (cid) |
| 1204 | OCSP_CERTID_free(cid); |
| 1205 | |
| 1206 | if (ocsp) |
| 1207 | free(ocsp); |
| 1208 | |
| 1209 | if (warn) |
| 1210 | free(warn); |
| 1211 | |
| 1212 | |
| 1213 | return ret; |
| 1214 | } |
| 1215 | |
| 1216 | #endif |
| 1217 | |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1218 | #ifdef OPENSSL_IS_BORINGSSL |
| 1219 | static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path) |
| 1220 | { |
| 1221 | char ocsp_path[MAXPATHLEN+1]; |
| 1222 | struct stat st; |
| 1223 | int fd = -1, r = 0; |
| 1224 | |
| 1225 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1226 | if (stat(ocsp_path, &st)) |
| 1227 | return 0; |
| 1228 | |
| 1229 | fd = open(ocsp_path, O_RDONLY); |
| 1230 | if (fd == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1231 | ha_warning("Error opening OCSP response file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1232 | return -1; |
| 1233 | } |
| 1234 | |
| 1235 | trash.len = 0; |
| 1236 | while (trash.len < trash.size) { |
| 1237 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 1238 | if (r < 0) { |
| 1239 | if (errno == EINTR) |
| 1240 | continue; |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1241 | ha_warning("Error reading OCSP response from file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1242 | close(fd); |
| 1243 | return -1; |
| 1244 | } |
| 1245 | else if (r == 0) { |
| 1246 | break; |
| 1247 | } |
| 1248 | trash.len += r; |
| 1249 | } |
| 1250 | close(fd); |
| 1251 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)trash.str, trash.len); |
| 1252 | } |
| 1253 | #endif |
| 1254 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 1255 | #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] | 1256 | |
| 1257 | #define CT_EXTENSION_TYPE 18 |
| 1258 | |
| 1259 | static int sctl_ex_index = -1; |
| 1260 | |
| 1261 | /* |
| 1262 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1263 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1264 | * is performed. |
| 1265 | */ |
| 1266 | static int ssl_sock_parse_sctl(struct chunk *sctl) |
| 1267 | { |
| 1268 | int ret = 1; |
| 1269 | int len, pos, sct_len; |
| 1270 | unsigned char *data; |
| 1271 | |
| 1272 | if (sctl->len < 2) |
| 1273 | goto out; |
| 1274 | |
| 1275 | data = (unsigned char *)sctl->str; |
| 1276 | len = (data[0] << 8) | data[1]; |
| 1277 | |
| 1278 | if (len + 2 != sctl->len) |
| 1279 | goto out; |
| 1280 | |
| 1281 | data = data + 2; |
| 1282 | pos = 0; |
| 1283 | while (pos < len) { |
| 1284 | if (len - pos < 2) |
| 1285 | goto out; |
| 1286 | |
| 1287 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1288 | if (pos + sct_len + 2 > len) |
| 1289 | goto out; |
| 1290 | |
| 1291 | pos += sct_len + 2; |
| 1292 | } |
| 1293 | |
| 1294 | ret = 0; |
| 1295 | |
| 1296 | out: |
| 1297 | return ret; |
| 1298 | } |
| 1299 | |
| 1300 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, struct chunk **sctl) |
| 1301 | { |
| 1302 | int fd = -1; |
| 1303 | int r = 0; |
| 1304 | int ret = 1; |
| 1305 | |
| 1306 | *sctl = NULL; |
| 1307 | |
| 1308 | fd = open(sctl_path, O_RDONLY); |
| 1309 | if (fd == -1) |
| 1310 | goto end; |
| 1311 | |
| 1312 | trash.len = 0; |
| 1313 | while (trash.len < trash.size) { |
| 1314 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 1315 | if (r < 0) { |
| 1316 | if (errno == EINTR) |
| 1317 | continue; |
| 1318 | |
| 1319 | goto end; |
| 1320 | } |
| 1321 | else if (r == 0) { |
| 1322 | break; |
| 1323 | } |
| 1324 | trash.len += r; |
| 1325 | } |
| 1326 | |
| 1327 | ret = ssl_sock_parse_sctl(&trash); |
| 1328 | if (ret) |
| 1329 | goto end; |
| 1330 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1331 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1332 | if (!chunk_dup(*sctl, &trash)) { |
| 1333 | free(*sctl); |
| 1334 | *sctl = NULL; |
| 1335 | goto end; |
| 1336 | } |
| 1337 | |
| 1338 | end: |
| 1339 | if (fd != -1) |
| 1340 | close(fd); |
| 1341 | |
| 1342 | return ret; |
| 1343 | } |
| 1344 | |
| 1345 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1346 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1347 | struct chunk *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1348 | |
| 1349 | *out = (unsigned char *)sctl->str; |
| 1350 | *outlen = sctl->len; |
| 1351 | |
| 1352 | return 1; |
| 1353 | } |
| 1354 | |
| 1355 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1356 | { |
| 1357 | return 1; |
| 1358 | } |
| 1359 | |
| 1360 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 1361 | { |
| 1362 | char sctl_path[MAXPATHLEN+1]; |
| 1363 | int ret = -1; |
| 1364 | struct stat st; |
| 1365 | struct chunk *sctl = NULL; |
| 1366 | |
| 1367 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 1368 | |
| 1369 | if (stat(sctl_path, &st)) |
| 1370 | return 1; |
| 1371 | |
| 1372 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 1373 | goto out; |
| 1374 | |
| 1375 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 1376 | free(sctl); |
| 1377 | goto out; |
| 1378 | } |
| 1379 | |
| 1380 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1381 | |
| 1382 | ret = 0; |
| 1383 | |
| 1384 | out: |
| 1385 | return ret; |
| 1386 | } |
| 1387 | |
| 1388 | #endif |
| 1389 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1390 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1391 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame^] | 1392 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1393 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1394 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1395 | |
| 1396 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1397 | /* Disable renegotiation (CVE-2009-3555) */ |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 1398 | if ((conn->flags & (CO_FL_CONNECTED | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_CONNECTED) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1399 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1400 | conn->err_code = CO_ER_SSL_RENEG; |
| 1401 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1402 | } |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1403 | |
| 1404 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 1405 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 1406 | /* Long certificate chains optimz |
| 1407 | If write and read bios are differents, we |
| 1408 | consider that the buffering was activated, |
| 1409 | so we rise the output buffer size from 4k |
| 1410 | to 16k */ |
| 1411 | write_bio = SSL_get_wbio(ssl); |
| 1412 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1413 | BIO_set_write_buffer_size(write_bio, 16384); |
| 1414 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 1415 | } |
| 1416 | } |
| 1417 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1418 | } |
| 1419 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1420 | /* Callback is called for each certificate of the chain during a verify |
| 1421 | ok is set to 1 if preverify detect no error on current certificate. |
| 1422 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1423 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1424 | { |
| 1425 | SSL *ssl; |
| 1426 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1427 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1428 | |
| 1429 | ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame^] | 1430 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1431 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1432 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1433 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1434 | if (ok) /* no errors */ |
| 1435 | return ok; |
| 1436 | |
| 1437 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1438 | err = X509_STORE_CTX_get_error(x_store); |
| 1439 | |
| 1440 | /* check if CA error needs to be ignored */ |
| 1441 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1442 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 1443 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1444 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1445 | } |
| 1446 | |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1447 | 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] | 1448 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1449 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1450 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1451 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1452 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1453 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1454 | return 0; |
| 1455 | } |
| 1456 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1457 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 1458 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1459 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1460 | /* check if certificate error needs to be ignored */ |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1461 | 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] | 1462 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1463 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1464 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1465 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1466 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1467 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1468 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1471 | static inline |
| 1472 | 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] | 1473 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1474 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1475 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1476 | unsigned char *msg; |
| 1477 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1478 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1479 | |
| 1480 | /* This function is called for "from client" and "to server" |
| 1481 | * connections. The combination of write_p == 0 and content_type == 22 |
| 1482 | * is only avalaible during "from client" connection. |
| 1483 | */ |
| 1484 | |
| 1485 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1486 | * otherwise it is set to 1. |
| 1487 | */ |
| 1488 | if (write_p != 0) |
| 1489 | return; |
| 1490 | |
| 1491 | /* content_type contains the type of message received or sent |
| 1492 | * according with the SSL/TLS protocol spec. This message is |
| 1493 | * encoded with one byte. The value 256 (two bytes) is used |
| 1494 | * for designing the SSL/TLS record layer. According with the |
| 1495 | * rfc6101, the expected message (other than 256) are: |
| 1496 | * - change_cipher_spec(20) |
| 1497 | * - alert(21) |
| 1498 | * - handshake(22) |
| 1499 | * - application_data(23) |
| 1500 | * - (255) |
| 1501 | * We are interessed by the handshake and specially the client |
| 1502 | * hello. |
| 1503 | */ |
| 1504 | if (content_type != 22) |
| 1505 | return; |
| 1506 | |
| 1507 | /* The message length is at least 4 bytes, containing the |
| 1508 | * message type and the message length. |
| 1509 | */ |
| 1510 | if (len < 4) |
| 1511 | return; |
| 1512 | |
| 1513 | /* First byte of the handshake message id the type of |
| 1514 | * message. The konwn types are: |
| 1515 | * - hello_request(0) |
| 1516 | * - client_hello(1) |
| 1517 | * - server_hello(2) |
| 1518 | * - certificate(11) |
| 1519 | * - server_key_exchange (12) |
| 1520 | * - certificate_request(13) |
| 1521 | * - server_hello_done(14) |
| 1522 | * We are interested by the client hello. |
| 1523 | */ |
| 1524 | msg = (unsigned char *)buf; |
| 1525 | if (msg[0] != 1) |
| 1526 | return; |
| 1527 | |
| 1528 | /* Next three bytes are the length of the message. The total length |
| 1529 | * must be this decoded length + 4. If the length given as argument |
| 1530 | * is not the same, we abort the protocol dissector. |
| 1531 | */ |
| 1532 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1533 | if (len < rec_len + 4) |
| 1534 | return; |
| 1535 | msg += 4; |
| 1536 | end = msg + rec_len; |
| 1537 | if (end < msg) |
| 1538 | return; |
| 1539 | |
| 1540 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1541 | * for minor, the random, composed by 4 bytes for the unix time and |
| 1542 | * 28 bytes for unix payload, and them 1 byte for the session id. So |
| 1543 | * we jump 1 + 1 + 4 + 28 + 1 bytes. |
| 1544 | */ |
| 1545 | msg += 1 + 1 + 4 + 28 + 1; |
| 1546 | if (msg > end) |
| 1547 | return; |
| 1548 | |
| 1549 | /* Next two bytes are the ciphersuite length. */ |
| 1550 | if (msg + 2 > end) |
| 1551 | return; |
| 1552 | rec_len = (msg[0] << 8) + msg[1]; |
| 1553 | msg += 2; |
| 1554 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1555 | return; |
| 1556 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1557 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1558 | if (!capture) |
| 1559 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1560 | /* Compute the xxh64 of the ciphersuite. */ |
| 1561 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1562 | |
| 1563 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1564 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1565 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1566 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1567 | |
| 1568 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1569 | } |
| 1570 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1571 | /* Callback is called for ssl protocol analyse */ |
| 1572 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1573 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1574 | #ifdef TLS1_RT_HEARTBEAT |
| 1575 | /* test heartbeat received (write_p is set to 0 |
| 1576 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1577 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame^] | 1578 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1579 | const unsigned char *p = buf; |
| 1580 | unsigned int payload; |
| 1581 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1582 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1583 | |
| 1584 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1585 | if (*p != TLS1_HB_REQUEST) |
| 1586 | return; |
| 1587 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1588 | 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] | 1589 | goto kill_it; |
| 1590 | |
| 1591 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1592 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1593 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1594 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1595 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1596 | * advertised payload is larger than the advertised packet |
| 1597 | * length, so we have garbage in the buffer between the |
| 1598 | * payload and the end of the buffer (p+len). We can't know |
| 1599 | * if the SSL stack is patched, and we don't know if we can |
| 1600 | * safely wipe out the area between p+3+len and payload. |
| 1601 | * So instead, we prevent the response from being sent by |
| 1602 | * setting the max_send_fragment to 0 and we report an SSL |
| 1603 | * error, which will kill this connection. It will be reported |
| 1604 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1605 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1606 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1607 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1608 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1609 | return; |
| 1610 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1611 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1612 | if (global_ssl.capture_cipherlist > 0) |
| 1613 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1614 | } |
| 1615 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1616 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1617 | /* This callback is used so that the server advertises the list of |
| 1618 | * negociable protocols for NPN. |
| 1619 | */ |
| 1620 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1621 | unsigned int *len, void *arg) |
| 1622 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1623 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1624 | |
| 1625 | *data = (const unsigned char *)conf->npn_str; |
| 1626 | *len = conf->npn_len; |
| 1627 | return SSL_TLSEXT_ERR_OK; |
| 1628 | } |
| 1629 | #endif |
| 1630 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1631 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1632 | /* This callback is used so that the server advertises the list of |
| 1633 | * negociable protocols for ALPN. |
| 1634 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1635 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1636 | unsigned char *outlen, |
| 1637 | const unsigned char *server, |
| 1638 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1639 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1640 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1641 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1642 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1643 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1644 | return SSL_TLSEXT_ERR_NOACK; |
| 1645 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1646 | return SSL_TLSEXT_ERR_OK; |
| 1647 | } |
| 1648 | #endif |
| 1649 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1650 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1651 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1652 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1653 | /* Create a X509 certificate with the specified servername and serial. This |
| 1654 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1655 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1656 | 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] | 1657 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1658 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1659 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1660 | SSL_CTX *ssl_ctx = NULL; |
| 1661 | X509 *newcrt = NULL; |
| 1662 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1663 | SSL *tmp_ssl = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1664 | X509_NAME *name; |
| 1665 | const EVP_MD *digest; |
| 1666 | X509V3_CTX ctx; |
| 1667 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1668 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1669 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1670 | /* Get the private key of the default certificate and use it */ |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1671 | #if (OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined LIBRESSL_VERSION_NUMBER) |
| 1672 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1673 | #else |
| 1674 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1675 | if (tmp_ssl) |
| 1676 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1677 | #endif |
| 1678 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1679 | goto mkcert_error; |
| 1680 | |
| 1681 | /* Create the certificate */ |
| 1682 | if (!(newcrt = X509_new())) |
| 1683 | goto mkcert_error; |
| 1684 | |
| 1685 | /* Set version number for the certificate (X509v3) and the serial |
| 1686 | * number */ |
| 1687 | if (X509_set_version(newcrt, 2L) != 1) |
| 1688 | goto mkcert_error; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1689 | ASN1_INTEGER_set(X509_get_serialNumber(newcrt), HA_ATOMIC_ADD(&ssl_ctx_serial, 1)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1690 | |
| 1691 | /* Set duration for the certificate */ |
| 1692 | if (!X509_gmtime_adj(X509_get_notBefore(newcrt), (long)-60*60*24) || |
| 1693 | !X509_gmtime_adj(X509_get_notAfter(newcrt),(long)60*60*24*365)) |
| 1694 | goto mkcert_error; |
| 1695 | |
| 1696 | /* set public key in the certificate */ |
| 1697 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1698 | goto mkcert_error; |
| 1699 | |
| 1700 | /* Set issuer name from the CA */ |
| 1701 | if (!(name = X509_get_subject_name(cacert))) |
| 1702 | goto mkcert_error; |
| 1703 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1704 | goto mkcert_error; |
| 1705 | |
| 1706 | /* Set the subject name using the same, but the CN */ |
| 1707 | name = X509_NAME_dup(name); |
| 1708 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1709 | (const unsigned char *)servername, |
| 1710 | -1, -1, 0) != 1) { |
| 1711 | X509_NAME_free(name); |
| 1712 | goto mkcert_error; |
| 1713 | } |
| 1714 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1715 | X509_NAME_free(name); |
| 1716 | goto mkcert_error; |
| 1717 | } |
| 1718 | X509_NAME_free(name); |
| 1719 | |
| 1720 | /* Add x509v3 extensions as specified */ |
| 1721 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1722 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1723 | X509_EXTENSION *ext; |
| 1724 | |
| 1725 | if (!(ext = X509V3_EXT_conf(NULL, &ctx, x509v3_ext_names[i], x509v3_ext_values[i]))) |
| 1726 | goto mkcert_error; |
| 1727 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1728 | X509_EXTENSION_free(ext); |
| 1729 | goto mkcert_error; |
| 1730 | } |
| 1731 | X509_EXTENSION_free(ext); |
| 1732 | } |
| 1733 | |
| 1734 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1735 | |
| 1736 | key_type = EVP_PKEY_base_id(capkey); |
| 1737 | |
| 1738 | if (key_type == EVP_PKEY_DSA) |
| 1739 | digest = EVP_sha1(); |
| 1740 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1741 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1742 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1743 | digest = EVP_sha256(); |
| 1744 | else { |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1745 | #if (OPENSSL_VERSION_NUMBER >= 0x1000000fL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1746 | int nid; |
| 1747 | |
| 1748 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1749 | goto mkcert_error; |
| 1750 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1751 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1752 | #else |
| 1753 | goto mkcert_error; |
| 1754 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1755 | } |
| 1756 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1757 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1758 | goto mkcert_error; |
| 1759 | |
| 1760 | /* Create and set the new SSL_CTX */ |
| 1761 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1762 | goto mkcert_error; |
| 1763 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1764 | goto mkcert_error; |
| 1765 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1766 | goto mkcert_error; |
| 1767 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1768 | goto mkcert_error; |
| 1769 | |
| 1770 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1771 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1772 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1773 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1774 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1775 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1776 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1777 | 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] | 1778 | EC_KEY *ecc; |
| 1779 | int nid; |
| 1780 | |
| 1781 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1782 | goto end; |
| 1783 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1784 | goto end; |
| 1785 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1786 | EC_KEY_free(ecc); |
| 1787 | } |
| 1788 | #endif |
| 1789 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1790 | return ssl_ctx; |
| 1791 | |
| 1792 | mkcert_error: |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1793 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1794 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1795 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1796 | return NULL; |
| 1797 | } |
| 1798 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1799 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1800 | 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] | 1801 | { |
| 1802 | struct bind_conf *bind_conf = objt_listener(conn->target)->bind_conf; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1803 | |
| 1804 | return ssl_sock_do_create_cert(servername, bind_conf, conn->xprt_ctx); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1805 | } |
| 1806 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1807 | /* Do a lookup for a certificate in the LRU cache used to store generated |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1808 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1809 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1810 | ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1811 | { |
| 1812 | struct lru64 *lru = NULL; |
| 1813 | |
| 1814 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1815 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1816 | lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1817 | if (lru && lru->domain) { |
| 1818 | if (ssl) |
| 1819 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1820 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1821 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1822 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1823 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1824 | } |
| 1825 | return NULL; |
| 1826 | } |
| 1827 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1828 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 1829 | * function is not thread-safe, it should only be used to check if a certificate |
| 1830 | * exists in the lru cache (with no warranty it will not be removed by another |
| 1831 | * thread). It is kept for backward compatibility. */ |
| 1832 | SSL_CTX * |
| 1833 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 1834 | { |
| 1835 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 1836 | } |
| 1837 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1838 | /* Set a certificate int the LRU cache used to store generated |
| 1839 | * certificate. Return 0 on success, otherwise -1 */ |
| 1840 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1841 | 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] | 1842 | { |
| 1843 | struct lru64 *lru = NULL; |
| 1844 | |
| 1845 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1846 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1847 | lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1848 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1849 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1850 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1851 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1852 | if (lru->domain && lru->data) |
| 1853 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1854 | lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1855 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1856 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1857 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1858 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1859 | } |
| 1860 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1861 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1862 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1863 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1864 | { |
| 1865 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 1866 | } |
| 1867 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1868 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 1869 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 1870 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1871 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1872 | 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] | 1873 | { |
| 1874 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1875 | SSL_CTX *ssl_ctx = NULL; |
| 1876 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1877 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1878 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1879 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1880 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1881 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1882 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1883 | if (lru && lru->domain) |
| 1884 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1885 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1886 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1887 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1888 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1889 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1890 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1891 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1892 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1893 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1894 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1895 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 1896 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 1897 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1898 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1899 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1900 | return 0; |
| 1901 | } |
| 1902 | static int |
| 1903 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 1904 | { |
| 1905 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame^] | 1906 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1907 | |
| 1908 | conn_get_to_addr(conn); |
| 1909 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
| 1910 | key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to)); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1911 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1912 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1913 | } |
| 1914 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1915 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1916 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1917 | |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1918 | |
| 1919 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 1920 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 1921 | #endif |
| 1922 | |
| 1923 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 1924 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
| 1925 | #define SSL_renegotiate_pending(arg) 0 |
| 1926 | #endif |
| 1927 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 1928 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1929 | #endif |
| 1930 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 1931 | #define SSL_OP_NO_TICKET 0 |
| 1932 | #endif |
| 1933 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 1934 | #define SSL_OP_NO_COMPRESSION 0 |
| 1935 | #endif |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 1936 | #ifdef OPENSSL_NO_SSL3 /* SSLv3 support removed */ |
| 1937 | #undef SSL_OP_NO_SSLv3 |
| 1938 | #define SSL_OP_NO_SSLv3 0 |
| 1939 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1940 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 1941 | #define SSL_OP_NO_TLSv1_1 0 |
| 1942 | #endif |
| 1943 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 1944 | #define SSL_OP_NO_TLSv1_2 0 |
| 1945 | #endif |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 1946 | #ifndef SSL_OP_NO_TLSv1_3 /* needs OpenSSL >= 1.1.1 */ |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1947 | #define SSL_OP_NO_TLSv1_3 0 |
| 1948 | #endif |
| 1949 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 1950 | #define SSL_OP_SINGLE_DH_USE 0 |
| 1951 | #endif |
| 1952 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 1953 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1954 | #endif |
| 1955 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 1956 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 1957 | #endif |
| 1958 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 1959 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 1960 | #endif |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 1961 | #ifndef SSL_OP_PRIORITIZE_CHACHA /* needs OpenSSL >= 1.1.1 */ |
| 1962 | #define SSL_OP_PRIORITIZE_CHACHA 0 |
| 1963 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1964 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 1965 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1966 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 1967 | |
| 1968 | 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] | 1969 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 1970 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1971 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1972 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 1973 | #endif |
| 1974 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1975 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 1976 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1977 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 1978 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1979 | 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] | 1980 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1981 | 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] | 1982 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 1983 | #endif |
| 1984 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1985 | 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] | 1986 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1987 | 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] | 1988 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 1989 | #endif |
| 1990 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1991 | /* TLS 1.2 is the last supported version in this context. */ |
| 1992 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 1993 | /* Unusable in this context. */ |
| 1994 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 1995 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 1996 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 1997 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 1998 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1999 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2000 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 2001 | |
| 2002 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2003 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2004 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2005 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2006 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2007 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2008 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2009 | } |
| 2010 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2011 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2012 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2013 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2014 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2015 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2016 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2017 | } |
| 2018 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2019 | 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] | 2020 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2021 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2022 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2023 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2024 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2025 | } |
| 2026 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2027 | 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] | 2028 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2029 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2030 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2031 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2032 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2033 | } |
| 2034 | 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] | 2035 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2036 | 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] | 2037 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2038 | #endif |
| 2039 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2040 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2041 | #if SSL_OP_NO_TLSv1_3 |
| 2042 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2043 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2044 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2045 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2046 | #endif |
| 2047 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2048 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2049 | |
| 2050 | static struct { |
| 2051 | int option; |
| 2052 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2053 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 2054 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2055 | const char *name; |
| 2056 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2057 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2058 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2059 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2060 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2061 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2062 | {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] | 2063 | }; |
| 2064 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2065 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2066 | { |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 2067 | struct pkey_info *pkinfo; |
| 2068 | |
| 2069 | pkinfo = SSL_CTX_get_ex_data(ctx, ssl_pkey_info_index); |
| 2070 | if (pkinfo) |
| 2071 | SSL_set_ex_data(ssl, ssl_pkey_info_index, pkinfo); |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2072 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2073 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2074 | SSL_set_SSL_CTX(ssl, ctx); |
| 2075 | } |
| 2076 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2077 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2078 | |
| 2079 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2080 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2081 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2082 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2083 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2084 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2085 | return SSL_TLSEXT_ERR_OK; |
| 2086 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2087 | } |
| 2088 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2089 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2090 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2091 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2092 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2093 | #else |
| 2094 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2095 | { |
| 2096 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2097 | struct connection *conn; |
| 2098 | struct bind_conf *s; |
| 2099 | const uint8_t *extension_data; |
| 2100 | size_t extension_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2101 | int has_rsa = 0, has_ecdsa = 0, has_ecdsa_sig = 0; |
| 2102 | |
| 2103 | char *wildp = NULL; |
| 2104 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2105 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2106 | 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] | 2107 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2108 | int i; |
| 2109 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame^] | 2110 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2111 | s = objt_listener(conn->target)->bind_conf; |
| 2112 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2113 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2114 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2115 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2116 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2117 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2118 | #else |
| 2119 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2120 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2121 | /* |
| 2122 | * The server_name extension was given too much extensibility when it |
| 2123 | * was written, so parsing the normal case is a bit complex. |
| 2124 | */ |
| 2125 | size_t len; |
| 2126 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2127 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2128 | /* Extract the length of the supplied list of names. */ |
| 2129 | len = (*extension_data++) << 8; |
| 2130 | len |= *extension_data++; |
| 2131 | if (len + 2 != extension_len) |
| 2132 | goto abort; |
| 2133 | /* |
| 2134 | * The list in practice only has a single element, so we only consider |
| 2135 | * the first one. |
| 2136 | */ |
| 2137 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2138 | goto abort; |
| 2139 | extension_len = len - 1; |
| 2140 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2141 | if (extension_len <= 2) |
| 2142 | goto abort; |
| 2143 | len = (*extension_data++) << 8; |
| 2144 | len |= *extension_data++; |
| 2145 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2146 | || memchr(extension_data, 0, len) != NULL) |
| 2147 | goto abort; |
| 2148 | servername = extension_data; |
| 2149 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2150 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2151 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2152 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2153 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2154 | } |
| 2155 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2156 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2157 | if (!s->strict_sni) { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2158 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2159 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2160 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2161 | goto abort; |
| 2162 | } |
| 2163 | |
| 2164 | /* extract/check clientHello informations */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2165 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2166 | 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] | 2167 | #else |
| 2168 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2169 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2170 | uint8_t sign; |
| 2171 | size_t len; |
| 2172 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2173 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2174 | len = (*extension_data++) << 8; |
| 2175 | len |= *extension_data++; |
| 2176 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2177 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2178 | if (len % 2 != 0) |
| 2179 | goto abort; |
| 2180 | for (; len > 0; len -= 2) { |
| 2181 | extension_data++; /* hash */ |
| 2182 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2183 | switch (sign) { |
| 2184 | case TLSEXT_signature_rsa: |
| 2185 | has_rsa = 1; |
| 2186 | break; |
| 2187 | case TLSEXT_signature_ecdsa: |
| 2188 | has_ecdsa_sig = 1; |
| 2189 | break; |
| 2190 | default: |
| 2191 | continue; |
| 2192 | } |
| 2193 | if (has_ecdsa_sig && has_rsa) |
| 2194 | break; |
| 2195 | } |
| 2196 | } else { |
| 2197 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLS 1.2) */ |
| 2198 | has_rsa = 1; |
| 2199 | } |
| 2200 | 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] | 2201 | const SSL_CIPHER *cipher; |
| 2202 | size_t len; |
| 2203 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2204 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2205 | len = ctx->cipher_suites_len; |
| 2206 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2207 | #else |
| 2208 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2209 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2210 | if (len % 2 != 0) |
| 2211 | goto abort; |
| 2212 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2213 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2214 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2215 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2216 | #else |
| 2217 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2218 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2219 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2220 | has_ecdsa = 1; |
| 2221 | break; |
| 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2226 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2227 | trash.str[i] = tolower(servername[i]); |
| 2228 | if (!wildp && (trash.str[i] == '.')) |
| 2229 | wildp = &trash.str[i]; |
| 2230 | } |
| 2231 | trash.str[i] = 0; |
| 2232 | |
| 2233 | /* lookup in full qualified names */ |
| 2234 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 2235 | |
| 2236 | /* lookup a not neg filter */ |
| 2237 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2238 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2239 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2240 | case TLSEXT_signature_ecdsa: |
| 2241 | if (has_ecdsa) { |
| 2242 | node_ecdsa = n; |
| 2243 | goto find_one; |
| 2244 | } |
| 2245 | break; |
| 2246 | case TLSEXT_signature_rsa: |
| 2247 | if (has_rsa && !node_rsa) { |
| 2248 | node_rsa = n; |
| 2249 | if (!has_ecdsa) |
| 2250 | goto find_one; |
| 2251 | } |
| 2252 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2253 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2254 | if (!node_anonymous) |
| 2255 | node_anonymous = n; |
| 2256 | break; |
| 2257 | } |
| 2258 | } |
| 2259 | } |
| 2260 | if (wildp) { |
| 2261 | /* lookup in wildcards names */ |
| 2262 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2263 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2264 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2265 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2266 | case TLSEXT_signature_ecdsa: |
| 2267 | if (has_ecdsa) { |
| 2268 | node_ecdsa = n; |
| 2269 | goto find_one; |
| 2270 | } |
| 2271 | break; |
| 2272 | case TLSEXT_signature_rsa: |
| 2273 | if (has_rsa && !node_rsa) { |
| 2274 | node_rsa = n; |
| 2275 | if (!has_ecdsa) |
| 2276 | goto find_one; |
| 2277 | } |
| 2278 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2279 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2280 | if (!node_anonymous) |
| 2281 | node_anonymous = n; |
| 2282 | break; |
| 2283 | } |
| 2284 | } |
| 2285 | } |
| 2286 | } |
| 2287 | find_one: |
| 2288 | /* select by key_signature priority order */ |
| 2289 | node = node_ecdsa ? node_ecdsa : (node_rsa ? node_rsa : node_anonymous); |
| 2290 | |
| 2291 | if (node) { |
| 2292 | /* switch ctx */ |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 2293 | 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] | 2294 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
Olivier Houchard | 35a63cc | 2017-11-02 19:04:38 +0100 | [diff] [blame] | 2295 | if (conf) { |
| 2296 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2297 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2298 | if (conf->early_data) |
| 2299 | allow_early = 1; |
| 2300 | } |
| 2301 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2302 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2303 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2304 | if (s->generate_certs && ssl_sock_generate_certificate(trash.str, s, ssl)) { |
| 2305 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2306 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2307 | } |
| 2308 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2309 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2310 | /* no certificate match, is the default_ctx */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2311 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2312 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2313 | allow_early: |
| 2314 | #ifdef OPENSSL_IS_BORINGSSL |
| 2315 | if (allow_early) |
| 2316 | SSL_set_early_data_enabled(ssl, 1); |
| 2317 | #else |
| 2318 | if (!allow_early) |
| 2319 | SSL_set_max_early_data(ssl, 0); |
| 2320 | #endif |
| 2321 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2322 | abort: |
| 2323 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2324 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2325 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2326 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2327 | #else |
| 2328 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2329 | return 0; |
| 2330 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2334 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2335 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2336 | * warning when no match is found, which implies the default (first) cert |
| 2337 | * will keep being used. |
| 2338 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2339 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2340 | { |
| 2341 | const char *servername; |
| 2342 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2343 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2344 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2345 | int i; |
| 2346 | (void)al; /* shut gcc stupid warning */ |
| 2347 | |
| 2348 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2349 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2350 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2351 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2352 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2353 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2354 | if (s->strict_sni) |
| 2355 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2356 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2357 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2358 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2359 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2360 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2361 | if (!servername[i]) |
| 2362 | break; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2363 | trash.str[i] = tolower(servername[i]); |
| 2364 | if (!wildp && (trash.str[i] == '.')) |
| 2365 | wildp = &trash.str[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2366 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2367 | trash.str[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2368 | |
| 2369 | /* lookup in full qualified names */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2370 | node = ebst_lookup(&s->sni_ctx, trash.str); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2371 | |
| 2372 | /* lookup a not neg filter */ |
| 2373 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2374 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2375 | node = n; |
| 2376 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2377 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2378 | } |
| 2379 | if (!node && wildp) { |
| 2380 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2381 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2382 | } |
| 2383 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2384 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2385 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2386 | /* switch ctx done in ssl_sock_generate_certificate */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2387 | return SSL_TLSEXT_ERR_OK; |
| 2388 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2389 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2390 | if (s->strict_sni) |
| 2391 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2392 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2393 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2397 | 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] | 2398 | return SSL_TLSEXT_ERR_OK; |
| 2399 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2400 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2401 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2402 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2403 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2404 | |
| 2405 | static DH * ssl_get_dh_1024(void) |
| 2406 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2407 | static unsigned char dh1024_p[]={ |
| 2408 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2409 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2410 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2411 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2412 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2413 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2414 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2415 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2416 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2417 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2418 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2419 | }; |
| 2420 | static unsigned char dh1024_g[]={ |
| 2421 | 0x02, |
| 2422 | }; |
| 2423 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2424 | BIGNUM *p; |
| 2425 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2426 | DH *dh = DH_new(); |
| 2427 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2428 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2429 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2430 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2431 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2432 | DH_free(dh); |
| 2433 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2434 | } else { |
| 2435 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | return dh; |
| 2439 | } |
| 2440 | |
| 2441 | static DH *ssl_get_dh_2048(void) |
| 2442 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2443 | static unsigned char dh2048_p[]={ |
| 2444 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2445 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2446 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2447 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2448 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2449 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2450 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2451 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2452 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2453 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2454 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2455 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2456 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2457 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2458 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2459 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2460 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2461 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2462 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2463 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2464 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2465 | 0xB7,0x1F,0x77,0xF3, |
| 2466 | }; |
| 2467 | static unsigned char dh2048_g[]={ |
| 2468 | 0x02, |
| 2469 | }; |
| 2470 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2471 | BIGNUM *p; |
| 2472 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2473 | DH *dh = DH_new(); |
| 2474 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2475 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2476 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2477 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2478 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2479 | DH_free(dh); |
| 2480 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2481 | } else { |
| 2482 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2483 | } |
| 2484 | } |
| 2485 | return dh; |
| 2486 | } |
| 2487 | |
| 2488 | static DH *ssl_get_dh_4096(void) |
| 2489 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2490 | static unsigned char dh4096_p[]={ |
| 2491 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2492 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2493 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2494 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2495 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2496 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2497 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2498 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2499 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2500 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2501 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2502 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2503 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2504 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2505 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2506 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2507 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2508 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2509 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2510 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2511 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2512 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2513 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2514 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2515 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2516 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2517 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2518 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2519 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2520 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2521 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2522 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2523 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2524 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2525 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2526 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2527 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2528 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2529 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2530 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2531 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2532 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2533 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2534 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2535 | static unsigned char dh4096_g[]={ |
| 2536 | 0x02, |
| 2537 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2538 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2539 | BIGNUM *p; |
| 2540 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2541 | DH *dh = DH_new(); |
| 2542 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2543 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2544 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2545 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2546 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2547 | DH_free(dh); |
| 2548 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2549 | } else { |
| 2550 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2551 | } |
| 2552 | } |
| 2553 | return dh; |
| 2554 | } |
| 2555 | |
| 2556 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2557 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2558 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2559 | { |
| 2560 | DH *dh = NULL; |
| 2561 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2562 | int type; |
| 2563 | |
| 2564 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2565 | |
| 2566 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2567 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2568 | */ |
| 2569 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2570 | keylen = EVP_PKEY_bits(pkey); |
| 2571 | } |
| 2572 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2573 | if (keylen > global_ssl.default_dh_param) { |
| 2574 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2575 | } |
| 2576 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2577 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2578 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2579 | } |
| 2580 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2581 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2582 | } |
| 2583 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2584 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | return dh; |
| 2588 | } |
| 2589 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2590 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2591 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2592 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2593 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2594 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2595 | if (in == NULL) |
| 2596 | goto end; |
| 2597 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2598 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2599 | goto end; |
| 2600 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2601 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2602 | |
| 2603 | end: |
| 2604 | if (in) |
| 2605 | BIO_free(in); |
| 2606 | |
| 2607 | return dh; |
| 2608 | } |
| 2609 | |
| 2610 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2611 | { |
| 2612 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2613 | |
| 2614 | if (global_dh) { |
| 2615 | return 0; |
| 2616 | } |
| 2617 | |
| 2618 | return -1; |
| 2619 | } |
| 2620 | |
| 2621 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 2622 | if an error occured, and 0 if parameter not found. */ |
| 2623 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 2624 | { |
| 2625 | int ret = -1; |
| 2626 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 2627 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2628 | if (dh) { |
| 2629 | ret = 1; |
| 2630 | SSL_CTX_set_tmp_dh(ctx, dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2631 | |
| 2632 | if (ssl_dh_ptr_index >= 0) { |
| 2633 | /* store a pointer to the DH params to avoid complaining about |
| 2634 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2635 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2636 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2637 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2638 | else if (global_dh) { |
| 2639 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 2640 | ret = 0; /* DH params not found */ |
| 2641 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2642 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2643 | /* Clear openssl global errors stack */ |
| 2644 | ERR_clear_error(); |
| 2645 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2646 | if (global_ssl.default_dh_param <= 1024) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2647 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 2648 | if (local_dh_1024 == NULL) |
| 2649 | local_dh_1024 = ssl_get_dh_1024(); |
| 2650 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2651 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2652 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2653 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2654 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2655 | } |
| 2656 | else { |
| 2657 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2658 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2659 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2660 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2661 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2662 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2663 | end: |
| 2664 | if (dh) |
| 2665 | DH_free(dh); |
| 2666 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2667 | return ret; |
| 2668 | } |
| 2669 | #endif |
| 2670 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2671 | static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, struct ssl_bind_conf *conf, |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2672 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2673 | { |
| 2674 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2675 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2676 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2677 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2678 | if (*name == '!') { |
| 2679 | neg = 1; |
| 2680 | name++; |
| 2681 | } |
| 2682 | if (*name == '*') { |
| 2683 | wild = 1; |
| 2684 | name++; |
| 2685 | } |
| 2686 | /* !* filter is a nop */ |
| 2687 | if (neg && wild) |
| 2688 | return order; |
| 2689 | if (*name) { |
| 2690 | int j, len; |
| 2691 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2692 | for (j = 0; j < len && j < trash.size; j++) |
| 2693 | trash.str[j] = tolower(name[j]); |
| 2694 | if (j >= trash.size) |
| 2695 | return order; |
| 2696 | trash.str[j] = 0; |
| 2697 | |
| 2698 | /* Check for duplicates. */ |
| 2699 | if (wild) |
| 2700 | node = ebst_lookup(&s->sni_w_ctx, trash.str); |
| 2701 | else |
| 2702 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 2703 | for (; node; node = ebmb_next_dup(node)) { |
| 2704 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2705 | if (sc->ctx == ctx && sc->conf == conf && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2706 | return order; |
| 2707 | } |
| 2708 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2709 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2710 | if (!sc) |
| 2711 | return order; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2712 | memcpy(sc->name.key, trash.str, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2713 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2714 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2715 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2716 | sc->order = order++; |
| 2717 | sc->neg = neg; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 2718 | if (kinfo.sig != TLSEXT_signature_anonymous) |
| 2719 | SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2720 | if (wild) |
| 2721 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 2722 | else |
| 2723 | ebst_insert(&s->sni_ctx, &sc->name); |
| 2724 | } |
| 2725 | return order; |
| 2726 | } |
| 2727 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2728 | |
| 2729 | /* The following code is used for loading multiple crt files into |
| 2730 | * SSL_CTX's based on CN/SAN |
| 2731 | */ |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 2732 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(LIBRESSL_VERSION_NUMBER) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2733 | /* This is used to preload the certifcate, private key |
| 2734 | * and Cert Chain of a file passed in via the crt |
| 2735 | * argument |
| 2736 | * |
| 2737 | * This way, we do not have to read the file multiple times |
| 2738 | */ |
| 2739 | struct cert_key_and_chain { |
| 2740 | X509 *cert; |
| 2741 | EVP_PKEY *key; |
| 2742 | unsigned int num_chain_certs; |
| 2743 | /* This is an array of X509 pointers */ |
| 2744 | X509 **chain_certs; |
| 2745 | }; |
| 2746 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2747 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 2748 | |
| 2749 | struct key_combo_ctx { |
| 2750 | SSL_CTX *ctx; |
| 2751 | int order; |
| 2752 | }; |
| 2753 | |
| 2754 | /* Map used for processing multiple keypairs for a single purpose |
| 2755 | * |
| 2756 | * This maps CN/SNI name to certificate type |
| 2757 | */ |
| 2758 | struct sni_keytype { |
| 2759 | int keytypes; /* BITMASK for keytypes */ |
| 2760 | struct ebmb_node name; /* node holding the servername value */ |
| 2761 | }; |
| 2762 | |
| 2763 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2764 | /* Frees the contents of a cert_key_and_chain |
| 2765 | */ |
| 2766 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2767 | { |
| 2768 | int i; |
| 2769 | |
| 2770 | if (!ckch) |
| 2771 | return; |
| 2772 | |
| 2773 | /* Free the certificate and set pointer to NULL */ |
| 2774 | if (ckch->cert) |
| 2775 | X509_free(ckch->cert); |
| 2776 | ckch->cert = NULL; |
| 2777 | |
| 2778 | /* Free the key and set pointer to NULL */ |
| 2779 | if (ckch->key) |
| 2780 | EVP_PKEY_free(ckch->key); |
| 2781 | ckch->key = NULL; |
| 2782 | |
| 2783 | /* Free each certificate in the chain */ |
| 2784 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2785 | if (ckch->chain_certs[i]) |
| 2786 | X509_free(ckch->chain_certs[i]); |
| 2787 | } |
| 2788 | |
| 2789 | /* Free the chain obj itself and set to NULL */ |
| 2790 | if (ckch->num_chain_certs > 0) { |
| 2791 | free(ckch->chain_certs); |
| 2792 | ckch->num_chain_certs = 0; |
| 2793 | ckch->chain_certs = NULL; |
| 2794 | } |
| 2795 | |
| 2796 | } |
| 2797 | |
| 2798 | /* checks if a key and cert exists in the ckch |
| 2799 | */ |
| 2800 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 2801 | { |
| 2802 | return (ckch->cert != NULL && ckch->key != NULL); |
| 2803 | } |
| 2804 | |
| 2805 | |
| 2806 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 2807 | * This allows us to carry the contents of the file without having to |
| 2808 | * read the file multiple times. |
| 2809 | * |
| 2810 | * returns: |
| 2811 | * 0 on Success |
| 2812 | * 1 on SSL Failure |
| 2813 | * 2 on file not found |
| 2814 | */ |
| 2815 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 2816 | { |
| 2817 | |
| 2818 | BIO *in; |
| 2819 | X509 *ca = NULL; |
| 2820 | int ret = 1; |
| 2821 | |
| 2822 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2823 | |
| 2824 | in = BIO_new(BIO_s_file()); |
| 2825 | if (in == NULL) |
| 2826 | goto end; |
| 2827 | |
| 2828 | if (BIO_read_filename(in, path) <= 0) |
| 2829 | goto end; |
| 2830 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2831 | /* Read Private Key */ |
| 2832 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 2833 | if (ckch->key == NULL) { |
| 2834 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 2835 | err && *err ? *err : "", path); |
| 2836 | goto end; |
| 2837 | } |
| 2838 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2839 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 2840 | if (BIO_reset(in) == -1) { |
| 2841 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 2842 | err && *err ? *err : "", path); |
| 2843 | goto end; |
| 2844 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2845 | |
| 2846 | /* Read Certificate */ |
| 2847 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 2848 | if (ckch->cert == NULL) { |
| 2849 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 2850 | err && *err ? *err : "", path); |
| 2851 | goto end; |
| 2852 | } |
| 2853 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2854 | /* Read Certificate Chain */ |
| 2855 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 2856 | /* Grow the chain certs */ |
| 2857 | ckch->num_chain_certs++; |
| 2858 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 2859 | |
| 2860 | /* use - 1 here since we just incremented it above */ |
| 2861 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 2862 | } |
| 2863 | ret = ERR_get_error(); |
| 2864 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 2865 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 2866 | err && *err ? *err : "", path); |
| 2867 | ret = 1; |
| 2868 | goto end; |
| 2869 | } |
| 2870 | |
| 2871 | ret = 0; |
| 2872 | |
| 2873 | end: |
| 2874 | |
| 2875 | ERR_clear_error(); |
| 2876 | if (in) |
| 2877 | BIO_free(in); |
| 2878 | |
| 2879 | /* Something went wrong in one of the reads */ |
| 2880 | if (ret != 0) |
| 2881 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2882 | |
| 2883 | return ret; |
| 2884 | } |
| 2885 | |
| 2886 | /* Loads the info in ckch into ctx |
| 2887 | * Currently, this does not process any information about ocsp, dhparams or |
| 2888 | * sctl |
| 2889 | * Returns |
| 2890 | * 0 on success |
| 2891 | * 1 on failure |
| 2892 | */ |
| 2893 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2894 | { |
| 2895 | int i = 0; |
| 2896 | |
| 2897 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2898 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2899 | err && *err ? *err : "", path); |
| 2900 | return 1; |
| 2901 | } |
| 2902 | |
| 2903 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 2904 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 2905 | err && *err ? *err : "", path); |
| 2906 | return 1; |
| 2907 | } |
| 2908 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2909 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 2910 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2911 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2912 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 2913 | err && *err ? *err : "", (i+1), path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2914 | return 1; |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2919 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2920 | err && *err ? *err : "", path); |
| 2921 | return 1; |
| 2922 | } |
| 2923 | |
| 2924 | return 0; |
| 2925 | } |
| 2926 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2927 | |
| 2928 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 2929 | { |
| 2930 | struct sni_keytype *s_kt = NULL; |
| 2931 | struct ebmb_node *node; |
| 2932 | int i; |
| 2933 | |
| 2934 | for (i = 0; i < trash.size; i++) { |
| 2935 | if (!str[i]) |
| 2936 | break; |
| 2937 | trash.str[i] = tolower(str[i]); |
| 2938 | } |
| 2939 | trash.str[i] = 0; |
| 2940 | node = ebst_lookup(sni_keytypes, trash.str); |
| 2941 | if (!node) { |
| 2942 | /* CN not found in tree */ |
| 2943 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 2944 | /* Using memcpy here instead of strncpy. |
| 2945 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 2946 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 2947 | */ |
| 2948 | memcpy(s_kt->name.key, trash.str, i+1); |
| 2949 | s_kt->keytypes = 0; |
| 2950 | ebst_insert(sni_keytypes, &s_kt->name); |
| 2951 | } else { |
| 2952 | /* CN found in tree */ |
| 2953 | s_kt = container_of(node, struct sni_keytype, name); |
| 2954 | } |
| 2955 | |
| 2956 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 2957 | s_kt->keytypes |= 1<<key_index; |
| 2958 | |
| 2959 | } |
| 2960 | |
| 2961 | |
| 2962 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 2963 | * If any are found, group these files into a set of SSL_CTX* |
| 2964 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 2965 | * |
| 2966 | * This will allow the user to explictly group multiple cert/keys for a single purpose |
| 2967 | * |
| 2968 | * Returns |
| 2969 | * 0 on success |
| 2970 | * 1 on failure |
| 2971 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2972 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2973 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2974 | { |
| 2975 | char fp[MAXPATHLEN+1] = {0}; |
| 2976 | int n = 0; |
| 2977 | int i = 0; |
| 2978 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 2979 | struct eb_root sni_keytypes_map = { {0} }; |
| 2980 | struct ebmb_node *node; |
| 2981 | struct ebmb_node *next; |
| 2982 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 2983 | * of keytypes |
| 2984 | */ |
| 2985 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 2986 | int rv = 0; |
| 2987 | X509_NAME *xname = NULL; |
| 2988 | char *str = NULL; |
| 2989 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2990 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 2991 | #endif |
| 2992 | |
| 2993 | /* Load all possible certs and keys */ |
| 2994 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2995 | struct stat buf; |
| 2996 | |
| 2997 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2998 | if (stat(fp, &buf) == 0) { |
| 2999 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
| 3000 | rv = 1; |
| 3001 | goto end; |
| 3002 | } |
| 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | /* Process each ckch and update keytypes for each CN/SAN |
| 3007 | * for example, if CN/SAN www.a.com is associated with |
| 3008 | * certs with keytype 0 and 2, then at the end of the loop, |
| 3009 | * www.a.com will have: |
| 3010 | * keyindex = 0 | 1 | 4 = 5 |
| 3011 | */ |
| 3012 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3013 | |
| 3014 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 3015 | continue; |
| 3016 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3017 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 3018 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3019 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 3020 | } else { |
| 3021 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 3022 | * so the line that contains logic is marked via comments |
| 3023 | */ |
| 3024 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 3025 | i = -1; |
| 3026 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3027 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3028 | ASN1_STRING *value; |
| 3029 | value = X509_NAME_ENTRY_get_data(entry); |
| 3030 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3031 | /* Important line is here */ |
| 3032 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3033 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3034 | OPENSSL_free(str); |
| 3035 | str = NULL; |
| 3036 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3037 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3038 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3039 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3040 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3041 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 3042 | if (names) { |
| 3043 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3044 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3045 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3046 | if (name->type == GEN_DNS) { |
| 3047 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 3048 | /* Important line is here */ |
| 3049 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3050 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3051 | OPENSSL_free(str); |
| 3052 | str = NULL; |
| 3053 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3054 | } |
| 3055 | } |
| 3056 | } |
| 3057 | } |
| 3058 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 3059 | } |
| 3060 | |
| 3061 | /* If no files found, return error */ |
| 3062 | if (eb_is_empty(&sni_keytypes_map)) { |
| 3063 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3064 | err && *err ? *err : "", path); |
| 3065 | rv = 1; |
| 3066 | goto end; |
| 3067 | } |
| 3068 | |
| 3069 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 3070 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 3071 | * and add each CTX to the SNI tree |
| 3072 | * |
| 3073 | * Some math here: |
| 3074 | * There are 2^n - 1 possibile combinations, each unique |
| 3075 | * combination is denoted by the key in the map. Each key |
| 3076 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 3077 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 3078 | * entry in the array to correspond to the unique combo (key) |
| 3079 | * associated with i. This unique key combo (i) will be associated |
| 3080 | * with combos[i-1] |
| 3081 | */ |
| 3082 | |
| 3083 | node = ebmb_first(&sni_keytypes_map); |
| 3084 | while (node) { |
| 3085 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3086 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3087 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3088 | |
| 3089 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 3090 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 3091 | cur_ctx = key_combos[i-1].ctx; |
| 3092 | |
| 3093 | if (cur_ctx == NULL) { |
| 3094 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3095 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3096 | if (cur_ctx == NULL) { |
| 3097 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3098 | err && *err ? *err : ""); |
| 3099 | rv = 1; |
| 3100 | goto end; |
| 3101 | } |
| 3102 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3103 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3104 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3105 | if (i & (1<<n)) { |
| 3106 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3107 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3108 | 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] | 3109 | SSL_CTX_free(cur_ctx); |
| 3110 | rv = 1; |
| 3111 | goto end; |
| 3112 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3113 | |
| 3114 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 3115 | /* Load OCSP Info into context */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3116 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3117 | if (err) |
| 3118 | 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] | 3119 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3120 | SSL_CTX_free(cur_ctx); |
| 3121 | rv = 1; |
| 3122 | goto end; |
| 3123 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3124 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3125 | ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3126 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3127 | } |
| 3128 | } |
| 3129 | |
| 3130 | /* Load DH params into the ctx to support DHE keys */ |
| 3131 | #ifndef OPENSSL_NO_DH |
| 3132 | if (ssl_dh_ptr_index >= 0) |
| 3133 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 3134 | |
| 3135 | rv = ssl_sock_load_dh_params(cur_ctx, NULL); |
| 3136 | if (rv < 0) { |
| 3137 | if (err) |
| 3138 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3139 | *err ? *err : "", path); |
| 3140 | rv = 1; |
| 3141 | goto end; |
| 3142 | } |
| 3143 | #endif |
| 3144 | |
| 3145 | /* Update key_combos */ |
| 3146 | key_combos[i-1].ctx = cur_ctx; |
| 3147 | } |
| 3148 | |
| 3149 | /* Update SNI Tree */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3150 | key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf, |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3151 | kinfo, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3152 | node = ebmb_next(node); |
| 3153 | } |
| 3154 | |
| 3155 | |
| 3156 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 3157 | if (!bind_conf->default_ctx) { |
| 3158 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 3159 | if (key_combos[i].ctx) { |
| 3160 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3161 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3162 | break; |
| 3163 | } |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | end: |
| 3168 | |
| 3169 | if (names) |
| 3170 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 3171 | |
| 3172 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 3173 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 3174 | |
| 3175 | node = ebmb_first(&sni_keytypes_map); |
| 3176 | while (node) { |
| 3177 | next = ebmb_next(node); |
| 3178 | ebmb_delete(node); |
| 3179 | node = next; |
| 3180 | } |
| 3181 | |
| 3182 | return rv; |
| 3183 | } |
| 3184 | #else |
| 3185 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3186 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3187 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3188 | { |
| 3189 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3190 | err && *err ? *err : "", path, strerror(errno)); |
| 3191 | return 1; |
| 3192 | } |
| 3193 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3194 | #endif /* #if OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
| 3195 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3196 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 3197 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 3198 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3199 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, |
| 3200 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3201 | { |
| 3202 | BIO *in; |
| 3203 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3204 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3205 | int ret = -1; |
| 3206 | int order = 0; |
| 3207 | X509_NAME *xname; |
| 3208 | char *str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3209 | pem_password_cb *passwd_cb; |
| 3210 | void *passwd_cb_userdata; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3211 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3212 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3213 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3214 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3215 | STACK_OF(GENERAL_NAME) *names; |
| 3216 | #endif |
| 3217 | |
| 3218 | in = BIO_new(BIO_s_file()); |
| 3219 | if (in == NULL) |
| 3220 | goto end; |
| 3221 | |
| 3222 | if (BIO_read_filename(in, file) <= 0) |
| 3223 | goto end; |
| 3224 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3225 | |
| 3226 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 3227 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 3228 | |
| 3229 | 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] | 3230 | if (x == NULL) |
| 3231 | goto end; |
| 3232 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3233 | pkey = X509_get_pubkey(x); |
| 3234 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3235 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3236 | switch(EVP_PKEY_base_id(pkey)) { |
| 3237 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3238 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3239 | break; |
| 3240 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3241 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3242 | break; |
| 3243 | case EVP_PKEY_DSA: |
| 3244 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3245 | break; |
| 3246 | } |
| 3247 | EVP_PKEY_free(pkey); |
| 3248 | } |
| 3249 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3250 | if (fcount) { |
| 3251 | while (fcount--) |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3252 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, sni_filter[fcount], order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3253 | } |
| 3254 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3255 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3256 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 3257 | if (names) { |
| 3258 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3259 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3260 | if (name->type == GEN_DNS) { |
| 3261 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3262 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3263 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3264 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3265 | } |
| 3266 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3267 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3268 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3269 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3270 | xname = X509_get_subject_name(x); |
| 3271 | i = -1; |
| 3272 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3273 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3274 | ASN1_STRING *value; |
| 3275 | |
| 3276 | value = X509_NAME_ENTRY_get_data(entry); |
| 3277 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3278 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3279 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3280 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 3285 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 3286 | goto end; |
| 3287 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3288 | #ifdef SSL_CTX_clear_extra_chain_certs |
| 3289 | SSL_CTX_clear_extra_chain_certs(ctx); |
| 3290 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3291 | if (ctx->extra_certs != NULL) { |
| 3292 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 3293 | ctx->extra_certs = NULL; |
| 3294 | } |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3295 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3296 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3297 | 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] | 3298 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3299 | X509_free(ca); |
| 3300 | goto end; |
| 3301 | } |
| 3302 | } |
| 3303 | |
| 3304 | err = ERR_get_error(); |
| 3305 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3306 | /* we successfully reached the last cert in the file */ |
| 3307 | ret = 1; |
| 3308 | } |
| 3309 | ERR_clear_error(); |
| 3310 | |
| 3311 | end: |
| 3312 | if (x) |
| 3313 | X509_free(x); |
| 3314 | |
| 3315 | if (in) |
| 3316 | BIO_free(in); |
| 3317 | |
| 3318 | return ret; |
| 3319 | } |
| 3320 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3321 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3322 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3323 | { |
| 3324 | int ret; |
| 3325 | SSL_CTX *ctx; |
| 3326 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3327 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3328 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3329 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3330 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3331 | return 1; |
| 3332 | } |
| 3333 | |
| 3334 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3335 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 3336 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3337 | SSL_CTX_free(ctx); |
| 3338 | return 1; |
| 3339 | } |
| 3340 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3341 | 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] | 3342 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3343 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 3344 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3345 | if (ret < 0) /* serious error, must do that ourselves */ |
| 3346 | SSL_CTX_free(ctx); |
| 3347 | return 1; |
| 3348 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 3349 | |
| 3350 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3351 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3352 | err && *err ? *err : "", path); |
| 3353 | return 1; |
| 3354 | } |
| 3355 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3356 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3357 | * the tree, so it will be discovered and cleaned in time. |
| 3358 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3359 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3360 | /* store a NULL pointer to indicate we have not yet loaded |
| 3361 | a custom DH param file */ |
| 3362 | if (ssl_dh_ptr_index >= 0) { |
| 3363 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3364 | } |
| 3365 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3366 | ret = ssl_sock_load_dh_params(ctx, path); |
| 3367 | if (ret < 0) { |
| 3368 | if (err) |
| 3369 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3370 | *err ? *err : "", path); |
| 3371 | return 1; |
| 3372 | } |
| 3373 | #endif |
| 3374 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 3375 | #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] | 3376 | ret = ssl_sock_load_ocsp(ctx, path); |
| 3377 | if (ret < 0) { |
| 3378 | if (err) |
| 3379 | 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", |
| 3380 | *err ? *err : "", path); |
| 3381 | return 1; |
| 3382 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3383 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3384 | ssl_sock_set_ocsp_response_from_file(ctx, path); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3385 | #endif |
| 3386 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 3387 | #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] | 3388 | if (sctl_ex_index >= 0) { |
| 3389 | ret = ssl_sock_load_sctl(ctx, path); |
| 3390 | if (ret < 0) { |
| 3391 | if (err) |
| 3392 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 3393 | *err ? *err : "", path); |
| 3394 | return 1; |
| 3395 | } |
| 3396 | } |
| 3397 | #endif |
| 3398 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3399 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3400 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3401 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3402 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3403 | return 1; |
| 3404 | } |
| 3405 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3406 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3407 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3408 | bind_conf->default_ssl_conf = ssl_conf; |
| 3409 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3410 | |
| 3411 | return 0; |
| 3412 | } |
| 3413 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3414 | 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] | 3415 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3416 | struct dirent **de_list; |
| 3417 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3418 | DIR *dir; |
| 3419 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 3420 | char *end; |
| 3421 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3422 | int cfgerr = 0; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3423 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3424 | int is_bundle; |
| 3425 | int j; |
| 3426 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3427 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3428 | if (stat(path, &buf) == 0) { |
| 3429 | dir = opendir(path); |
| 3430 | if (!dir) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3431 | 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] | 3432 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3433 | /* strip trailing slashes, including first one */ |
| 3434 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 3435 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3436 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3437 | n = scandir(path, &de_list, 0, alphasort); |
| 3438 | if (n < 0) { |
| 3439 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 3440 | err && *err ? *err : "", path, strerror(errno)); |
| 3441 | cfgerr++; |
| 3442 | } |
| 3443 | else { |
| 3444 | for (i = 0; i < n; i++) { |
| 3445 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 3446 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3447 | end = strrchr(de->d_name, '.'); |
| 3448 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 3449 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3450 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3451 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 3452 | if (stat(fp, &buf) != 0) { |
| 3453 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3454 | err && *err ? *err : "", fp, strerror(errno)); |
| 3455 | cfgerr++; |
| 3456 | goto ignore_entry; |
| 3457 | } |
| 3458 | if (!S_ISREG(buf.st_mode)) |
| 3459 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3460 | |
| 3461 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3462 | is_bundle = 0; |
| 3463 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 3464 | |
| 3465 | if (end) { |
| 3466 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 3467 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 3468 | is_bundle = 1; |
| 3469 | break; |
| 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | if (is_bundle) { |
| 3474 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 3475 | int dp_len; |
| 3476 | |
| 3477 | dp_len = end - de->d_name; |
| 3478 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 3479 | |
| 3480 | /* increment i and free de until we get to a non-bundle cert |
| 3481 | * Note here that we look at de_list[i + 1] before freeing de |
| 3482 | * this is important since ignore_entry will free de |
| 3483 | */ |
| 3484 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 3485 | free(de); |
| 3486 | i++; |
| 3487 | de = de_list[i]; |
| 3488 | } |
| 3489 | |
| 3490 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3491 | ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3492 | |
| 3493 | /* Successfully processed the bundle */ |
| 3494 | goto ignore_entry; |
| 3495 | } |
| 3496 | } |
| 3497 | |
| 3498 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3499 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3500 | ignore_entry: |
| 3501 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3502 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3503 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3504 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3505 | closedir(dir); |
| 3506 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3507 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3508 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3509 | cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3510 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3511 | return cfgerr; |
| 3512 | } |
| 3513 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3514 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3515 | * done once. Zero is returned if the operation fails. No error is returned |
| 3516 | * if the random is said as not implemented, because we expect that openssl |
| 3517 | * will use another method once needed. |
| 3518 | */ |
| 3519 | static int ssl_initialize_random() |
| 3520 | { |
| 3521 | unsigned char random; |
| 3522 | static int random_initialized = 0; |
| 3523 | |
| 3524 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3525 | random_initialized = 1; |
| 3526 | |
| 3527 | return random_initialized; |
| 3528 | } |
| 3529 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3530 | /* release ssl bind conf */ |
| 3531 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3532 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3533 | if (conf) { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 3534 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3535 | free(conf->npn_str); |
| 3536 | conf->npn_str = NULL; |
| 3537 | #endif |
| 3538 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 3539 | free(conf->alpn_str); |
| 3540 | conf->alpn_str = NULL; |
| 3541 | #endif |
| 3542 | free(conf->ca_file); |
| 3543 | conf->ca_file = NULL; |
| 3544 | free(conf->crl_file); |
| 3545 | conf->crl_file = NULL; |
| 3546 | free(conf->ciphers); |
| 3547 | conf->ciphers = NULL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3548 | free(conf->curves); |
| 3549 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3550 | free(conf->ecdhe); |
| 3551 | conf->ecdhe = NULL; |
| 3552 | } |
| 3553 | } |
| 3554 | |
| 3555 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 3556 | { |
| 3557 | char thisline[CRT_LINESIZE]; |
| 3558 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3559 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3560 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3561 | int linenum = 0; |
| 3562 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3563 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3564 | if ((f = fopen(file, "r")) == NULL) { |
| 3565 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3566 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3567 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3568 | |
| 3569 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3570 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3571 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3572 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3573 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3574 | char *crt_path; |
| 3575 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3576 | |
| 3577 | linenum++; |
| 3578 | end = line + strlen(line); |
| 3579 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 3580 | /* Check if we reached the limit and the last char is not \n. |
| 3581 | * Watch out for the last line without the terminating '\n'! |
| 3582 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3583 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 3584 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3585 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3586 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3587 | } |
| 3588 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3589 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3590 | newarg = 1; |
| 3591 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3592 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 3593 | /* end of string, end of loop */ |
| 3594 | *line = 0; |
| 3595 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3596 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3597 | newarg = 1; |
| 3598 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3599 | } else if (*line == '[') { |
| 3600 | if (ssl_b) { |
| 3601 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 3602 | cfgerr = 1; |
| 3603 | break; |
| 3604 | } |
| 3605 | if (!arg) { |
| 3606 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 3607 | cfgerr = 1; |
| 3608 | break; |
| 3609 | } |
| 3610 | ssl_b = arg; |
| 3611 | newarg = 1; |
| 3612 | *line = 0; |
| 3613 | } else if (*line == ']') { |
| 3614 | if (ssl_e) { |
| 3615 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3616 | cfgerr = 1; |
| 3617 | break; |
| 3618 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3619 | if (!ssl_b) { |
| 3620 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 3621 | cfgerr = 1; |
| 3622 | break; |
| 3623 | } |
| 3624 | ssl_e = arg; |
| 3625 | newarg = 1; |
| 3626 | *line = 0; |
| 3627 | } else if (newarg) { |
| 3628 | if (arg == MAX_CRT_ARGS) { |
| 3629 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 3630 | cfgerr = 1; |
| 3631 | break; |
| 3632 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3633 | newarg = 0; |
| 3634 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3635 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3636 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3637 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3638 | if (cfgerr) |
| 3639 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3640 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3641 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3642 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3643 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3644 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3645 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3646 | crt_path = args[0]; |
| 3647 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 3648 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 3649 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 3650 | crt_path, linenum, file); |
| 3651 | cfgerr = 1; |
| 3652 | break; |
| 3653 | } |
| 3654 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 3655 | crt_path = path; |
| 3656 | } |
| 3657 | |
| 3658 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 3659 | cur_arg = ssl_b ? ssl_b : 1; |
| 3660 | while (cur_arg < ssl_e) { |
| 3661 | newarg = 0; |
| 3662 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 3663 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 3664 | newarg = 1; |
| 3665 | cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
| 3666 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 3667 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 3668 | args[cur_arg], linenum, file); |
| 3669 | cfgerr = 1; |
| 3670 | } |
| 3671 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 3672 | break; |
| 3673 | } |
| 3674 | } |
| 3675 | if (!cfgerr && !newarg) { |
| 3676 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 3677 | args[cur_arg], linenum, file); |
| 3678 | cfgerr = 1; |
| 3679 | break; |
| 3680 | } |
| 3681 | } |
| 3682 | if (cfgerr) { |
| 3683 | ssl_sock_free_ssl_conf(ssl_conf); |
| 3684 | free(ssl_conf); |
| 3685 | ssl_conf = NULL; |
| 3686 | break; |
| 3687 | } |
| 3688 | |
| 3689 | if (stat(crt_path, &buf) == 0) { |
| 3690 | cfgerr = ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf, |
| 3691 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3692 | } else { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3693 | cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf, |
| 3694 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3695 | } |
| 3696 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3697 | if (cfgerr) { |
| 3698 | 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] | 3699 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3700 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3701 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3702 | fclose(f); |
| 3703 | return cfgerr; |
| 3704 | } |
| 3705 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3706 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3707 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3708 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3709 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3710 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3711 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3712 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3713 | SSL_OP_NO_SSLv2 | |
| 3714 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3715 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3716 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3717 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 3718 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3719 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3720 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3721 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3722 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3723 | SSL_MODE_RELEASE_BUFFERS | |
| 3724 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3725 | 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] | 3726 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3727 | int flags = MC_SSL_O_ALL; |
| 3728 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3729 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3730 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3731 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3732 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3733 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3734 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3735 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3736 | 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] | 3737 | else |
| 3738 | flags = conf_ssl_methods->flags; |
| 3739 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3740 | min = conf_ssl_methods->min; |
| 3741 | max = conf_ssl_methods->max; |
| 3742 | /* start with TLSv10 to remove SSLv3 per default */ |
| 3743 | if (!min && (!max || max >= CONF_TLSV10)) |
| 3744 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3745 | /* 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] | 3746 | if (min) |
| 3747 | flags |= (methodVersions[min].flag - 1); |
| 3748 | if (max) |
| 3749 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3750 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3751 | min = max = CONF_TLSV_NONE; |
| 3752 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3753 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3754 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3755 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3756 | if (min) { |
| 3757 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3758 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 3759 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3760 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3761 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3762 | hole = 0; |
| 3763 | } |
| 3764 | max = i; |
| 3765 | } |
| 3766 | else { |
| 3767 | min = max = i; |
| 3768 | } |
| 3769 | } |
| 3770 | else { |
| 3771 | if (min) |
| 3772 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3773 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3774 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3775 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3776 | 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] | 3777 | cfgerr += 1; |
| 3778 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3779 | /* save real min/max in bind_conf */ |
| 3780 | conf_ssl_methods->min = min; |
| 3781 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3782 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 3783 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3784 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
| 3785 | precautionary measure to avoid any suprise with older openssl version. */ |
| 3786 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3787 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3788 | else |
| 3789 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3790 | if (flags & methodVersions[i].flag) |
| 3791 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3792 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3793 | /* 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] | 3794 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3795 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3796 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3797 | |
| 3798 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3799 | options |= SSL_OP_NO_TICKET; |
| 3800 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3801 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 3802 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3803 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 3804 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3805 | if (global_ssl.async) |
| 3806 | mode |= SSL_MODE_ASYNC; |
| 3807 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3808 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3809 | if (global_ssl.life_time) |
| 3810 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3811 | |
| 3812 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3813 | #ifdef OPENSSL_IS_BORINGSSL |
| 3814 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3815 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3816 | #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 3817 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 3818 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3819 | #else |
| 3820 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3821 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 3822 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3823 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3824 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3825 | } |
| 3826 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3827 | |
| 3828 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 3829 | { |
| 3830 | if (first == block) { |
| 3831 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3832 | if (first->len > 0) |
| 3833 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 3834 | } |
| 3835 | } |
| 3836 | |
| 3837 | /* return first block from sh_ssl_sess */ |
| 3838 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 3839 | { |
| 3840 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 3841 | |
| 3842 | } |
| 3843 | |
| 3844 | /* store a session into the cache |
| 3845 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 3846 | * data: asn1 encoded session |
| 3847 | * data_len: asn1 encoded session length |
| 3848 | * Returns 1 id session was stored (else 0) |
| 3849 | */ |
| 3850 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 3851 | { |
| 3852 | struct shared_block *first; |
| 3853 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 3854 | |
| 3855 | first = shctx_row_reserve_hot(ssl_shctx, data_len + sizeof(struct sh_ssl_sess_hdr)); |
| 3856 | if (!first) { |
| 3857 | /* Could not retrieve enough free blocks to store that session */ |
| 3858 | return 0; |
| 3859 | } |
| 3860 | |
| 3861 | /* STORE the key in the first elem */ |
| 3862 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3863 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 3864 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3865 | |
| 3866 | /* it returns the already existing node |
| 3867 | or current node if none, never returns null */ |
| 3868 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 3869 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 3870 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 3871 | /* release the reserved row */ |
| 3872 | shctx_row_dec_hot(ssl_shctx, first); |
| 3873 | /* replace the previous session already in the tree */ |
| 3874 | sh_ssl_sess = oldsh_ssl_sess; |
| 3875 | /* ignore the previous session data, only use the header */ |
| 3876 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 3877 | shctx_row_inc_hot(ssl_shctx, first); |
| 3878 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3879 | } |
| 3880 | |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 3881 | if (shctx_row_data_append(ssl_shctx, first, data, data_len) < 0) { |
| 3882 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3883 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3887 | |
| 3888 | return 1; |
| 3889 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3890 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3891 | /* SSL callback used when a new session is created while connecting to a server */ |
| 3892 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 3893 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame^] | 3894 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3895 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3896 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3897 | s = objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3898 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3899 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 3900 | int len; |
| 3901 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3902 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3903 | len = i2d_SSL_SESSION(sess, NULL); |
| 3904 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 3905 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 3906 | } else { |
| 3907 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3908 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 3909 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 3910 | } |
| 3911 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 3912 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 3913 | &ptr); |
| 3914 | } |
| 3915 | } else { |
| 3916 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3917 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 3918 | } |
| 3919 | |
| 3920 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3921 | } |
| 3922 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3923 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3924 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3925 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3926 | { |
| 3927 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 3928 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 3929 | unsigned char *p; |
| 3930 | int data_len; |
| 3931 | unsigned int sid_length, sid_ctx_length; |
| 3932 | const unsigned char *sid_data; |
| 3933 | const unsigned char *sid_ctx_data; |
| 3934 | |
| 3935 | /* Session id is already stored in to key and session id is known |
| 3936 | * so we dont store it to keep size. |
| 3937 | */ |
| 3938 | |
| 3939 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 3940 | sid_ctx_data = SSL_SESSION_get0_id_context(sess, &sid_ctx_length); |
| 3941 | SSL_SESSION_set1_id(sess, sid_data, 0); |
| 3942 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, 0); |
| 3943 | |
| 3944 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 3945 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 3946 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 3947 | goto err; |
| 3948 | |
| 3949 | p = encsess; |
| 3950 | |
| 3951 | /* process ASN1 session encoding before the lock */ |
| 3952 | i2d_SSL_SESSION(sess, &p); |
| 3953 | |
| 3954 | memcpy(encid, sid_data, sid_length); |
| 3955 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 3956 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 3957 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3958 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3959 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3960 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3961 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3962 | err: |
| 3963 | /* reset original length values */ |
| 3964 | SSL_SESSION_set1_id(sess, sid_data, sid_length); |
| 3965 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, sid_ctx_length); |
| 3966 | |
| 3967 | return 0; /* do not increment session reference count */ |
| 3968 | } |
| 3969 | |
| 3970 | /* 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] | 3971 | 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] | 3972 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3973 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3974 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 3975 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3976 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3977 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3978 | |
| 3979 | global.shctx_lookups++; |
| 3980 | |
| 3981 | /* allow the session to be freed automatically by openssl */ |
| 3982 | *do_copy = 0; |
| 3983 | |
| 3984 | /* tree key is zeros padded sessionid */ |
| 3985 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3986 | memcpy(tmpkey, key, key_len); |
| 3987 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 3988 | key = tmpkey; |
| 3989 | } |
| 3990 | |
| 3991 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3992 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3993 | |
| 3994 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3995 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 3996 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3997 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3998 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3999 | global.shctx_misses++; |
| 4000 | return NULL; |
| 4001 | } |
| 4002 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4003 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 4004 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4005 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4006 | 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] | 4007 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4008 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4009 | |
| 4010 | /* decode ASN1 session */ |
| 4011 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4012 | 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] | 4013 | /* Reset session id and session id contenxt */ |
| 4014 | if (sess) { |
| 4015 | SSL_SESSION_set1_id(sess, key, key_len); |
| 4016 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4017 | } |
| 4018 | |
| 4019 | return sess; |
| 4020 | } |
| 4021 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4022 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4023 | /* 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] | 4024 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4025 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4026 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4027 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 4028 | unsigned int sid_length; |
| 4029 | const unsigned char *sid_data; |
| 4030 | (void)ctx; |
| 4031 | |
| 4032 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4033 | /* tree key is zeros padded sessionid */ |
| 4034 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4035 | memcpy(tmpkey, sid_data, sid_length); |
| 4036 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 4037 | sid_data = tmpkey; |
| 4038 | } |
| 4039 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4040 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4041 | |
| 4042 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4043 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 4044 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4045 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4046 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4047 | } |
| 4048 | |
| 4049 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4050 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | /* Set session cache mode to server and disable openssl internal cache. |
| 4054 | * Set shared cache callbacks on an ssl context. |
| 4055 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4056 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4057 | { |
| 4058 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4059 | |
| 4060 | if (!ssl_shctx) { |
| 4061 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4062 | return; |
| 4063 | } |
| 4064 | |
| 4065 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4066 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4067 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4068 | |
| 4069 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4070 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4071 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4072 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4073 | } |
| 4074 | |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4075 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
| 4076 | { |
| 4077 | struct proxy *curproxy = bind_conf->frontend; |
| 4078 | int cfgerr = 0; |
| 4079 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4080 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4081 | const char *conf_ciphers; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4082 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4083 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4084 | if (ssl_conf) { |
| 4085 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4086 | int i, min, max; |
| 4087 | int flags = MC_SSL_O_ALL; |
| 4088 | |
| 4089 | /* 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] | 4090 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4091 | 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] | 4092 | if (min) |
| 4093 | flags |= (methodVersions[min].flag - 1); |
| 4094 | if (max) |
| 4095 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4096 | min = max = CONF_TLSV_NONE; |
| 4097 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4098 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4099 | if (min) |
| 4100 | max = i; |
| 4101 | else |
| 4102 | min = max = i; |
| 4103 | } |
| 4104 | /* save real min/max */ |
| 4105 | conf_ssl_methods->min = min; |
| 4106 | conf_ssl_methods->max = max; |
| 4107 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4108 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4109 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4110 | cfgerr += 1; |
| 4111 | } |
| 4112 | } |
| 4113 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4114 | 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] | 4115 | case SSL_SOCK_VERIFY_NONE: |
| 4116 | verify = SSL_VERIFY_NONE; |
| 4117 | break; |
| 4118 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4119 | verify = SSL_VERIFY_PEER; |
| 4120 | break; |
| 4121 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4122 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4123 | break; |
| 4124 | } |
| 4125 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4126 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4127 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 4128 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 4129 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4130 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4131 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4132 | ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
| 4133 | 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] | 4134 | cfgerr++; |
| 4135 | } |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 4136 | if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) { |
| 4137 | /* set CA names for client cert request, function returns void */ |
| 4138 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file)); |
| 4139 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4140 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4141 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4142 | ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4143 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4144 | cfgerr++; |
| 4145 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4146 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4147 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4148 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4149 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4150 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4151 | ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4152 | 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] | 4153 | cfgerr++; |
| 4154 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4155 | else { |
| 4156 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4157 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4158 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4159 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4160 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4161 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4162 | #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] | 4163 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4164 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4165 | ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4166 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4167 | cfgerr++; |
| 4168 | } |
| 4169 | } |
| 4170 | #endif |
| 4171 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4172 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4173 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4174 | if (conf_ciphers && |
| 4175 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4176 | ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4177 | 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] | 4178 | cfgerr++; |
| 4179 | } |
| 4180 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4181 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4182 | /* If tune.ssl.default-dh-param has not been set, |
| 4183 | neither has ssl-default-dh-file and no static DH |
| 4184 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4185 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4186 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4187 | (ssl_dh_ptr_index == -1 || |
| 4188 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4189 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 4190 | const SSL_CIPHER * cipher = NULL; |
| 4191 | char cipher_description[128]; |
| 4192 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 4193 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 4194 | which is not ephemeral DH. */ |
| 4195 | const char dhe_description[] = " Kx=DH "; |
| 4196 | const char dhe_export_description[] = " Kx=DH("; |
| 4197 | int idx = 0; |
| 4198 | int dhe_found = 0; |
| 4199 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4200 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4201 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4202 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4203 | if (ssl) { |
| 4204 | ciphers = SSL_get_ciphers(ssl); |
| 4205 | |
| 4206 | if (ciphers) { |
| 4207 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 4208 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 4209 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 4210 | if (strstr(cipher_description, dhe_description) != NULL || |
| 4211 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 4212 | dhe_found = 1; |
| 4213 | break; |
| 4214 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 4215 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4216 | } |
| 4217 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4218 | SSL_free(ssl); |
| 4219 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4220 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4221 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4222 | if (dhe_found) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4223 | ha_warning("Setting tune.ssl.default-dh-param to 1024 by default, if your workload permits it you should set it to at least 2048. Please set a value >= 1024 to make this warning disappear.\n"); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4224 | } |
| 4225 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4226 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4227 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4228 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4229 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4230 | if (local_dh_1024 == NULL) { |
| 4231 | local_dh_1024 = ssl_get_dh_1024(); |
| 4232 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4233 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4234 | if (local_dh_2048 == NULL) { |
| 4235 | local_dh_2048 = ssl_get_dh_2048(); |
| 4236 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4237 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4238 | if (local_dh_4096 == NULL) { |
| 4239 | local_dh_4096 = ssl_get_dh_4096(); |
| 4240 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4241 | } |
| 4242 | } |
| 4243 | } |
| 4244 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4245 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4246 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4247 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4248 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4249 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4250 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4251 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4252 | ssl_conf_cur = NULL; |
| 4253 | if (ssl_conf && ssl_conf->npn_str) |
| 4254 | ssl_conf_cur = ssl_conf; |
| 4255 | else if (bind_conf->ssl_conf.npn_str) |
| 4256 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4257 | if (ssl_conf_cur) |
| 4258 | 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] | 4259 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4260 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4261 | ssl_conf_cur = NULL; |
| 4262 | if (ssl_conf && ssl_conf->alpn_str) |
| 4263 | ssl_conf_cur = ssl_conf; |
| 4264 | else if (bind_conf->ssl_conf.alpn_str) |
| 4265 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4266 | if (ssl_conf_cur) |
| 4267 | 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] | 4268 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4269 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 4270 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4271 | if (conf_curves) { |
| 4272 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4273 | ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4274 | curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4275 | cfgerr++; |
| 4276 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4277 | #if defined(SSL_CTX_set_ecdh_auto) |
| 4278 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
| 4279 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4280 | } |
| 4281 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4282 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4283 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4284 | int i; |
| 4285 | EC_KEY *ecdh; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4286 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4287 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4288 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4289 | NULL); |
| 4290 | |
| 4291 | if (ecdhe == NULL) { |
| 4292 | SSL_CTX_set_dh_auto(ctx, 1); |
| 4293 | return cfgerr; |
| 4294 | } |
| 4295 | #else |
| 4296 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4297 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4298 | ECDHE_DEFAULT_CURVE); |
| 4299 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4300 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4301 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4302 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4303 | ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4304 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4305 | cfgerr++; |
| 4306 | } |
| 4307 | else { |
| 4308 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4309 | EC_KEY_free(ecdh); |
| 4310 | } |
| 4311 | } |
| 4312 | #endif |
| 4313 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4314 | return cfgerr; |
| 4315 | } |
| 4316 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4317 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4318 | { |
| 4319 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4320 | size_t prefixlen, suffixlen; |
| 4321 | |
| 4322 | /* Trivial case */ |
| 4323 | if (strcmp(pattern, hostname) == 0) |
| 4324 | return 1; |
| 4325 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4326 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4327 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4328 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4329 | pattern_wildcard = NULL; |
| 4330 | pattern_left_label_end = pattern; |
| 4331 | while (*pattern_left_label_end != '.') { |
| 4332 | switch (*pattern_left_label_end) { |
| 4333 | case 0: |
| 4334 | /* End of label not found */ |
| 4335 | return 0; |
| 4336 | case '*': |
| 4337 | /* If there is more than one wildcards */ |
| 4338 | if (pattern_wildcard) |
| 4339 | return 0; |
| 4340 | pattern_wildcard = pattern_left_label_end; |
| 4341 | break; |
| 4342 | } |
| 4343 | pattern_left_label_end++; |
| 4344 | } |
| 4345 | |
| 4346 | /* If it's not trivial and there is no wildcard, it can't |
| 4347 | * match */ |
| 4348 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4349 | return 0; |
| 4350 | |
| 4351 | /* Make sure all labels match except the leftmost */ |
| 4352 | hostname_left_label_end = strchr(hostname, '.'); |
| 4353 | if (!hostname_left_label_end |
| 4354 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 4355 | return 0; |
| 4356 | |
| 4357 | /* Make sure the leftmost label of the hostname is long enough |
| 4358 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4359 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4360 | return 0; |
| 4361 | |
| 4362 | /* Finally compare the string on either side of the |
| 4363 | * wildcard */ |
| 4364 | prefixlen = pattern_wildcard - pattern; |
| 4365 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4366 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 4367 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4368 | return 0; |
| 4369 | |
| 4370 | return 1; |
| 4371 | } |
| 4372 | |
| 4373 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4374 | { |
| 4375 | SSL *ssl; |
| 4376 | struct connection *conn; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4377 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4378 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4379 | |
| 4380 | int depth; |
| 4381 | X509 *cert; |
| 4382 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4383 | int i; |
| 4384 | X509_NAME *cert_subject; |
| 4385 | char *str; |
| 4386 | |
| 4387 | if (ok == 0) |
| 4388 | return ok; |
| 4389 | |
| 4390 | ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame^] | 4391 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4392 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4393 | /* We're checking if the provided hostnames match the desired one. The |
| 4394 | * desired hostname comes from the SNI we presented if any, or if not |
| 4395 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4396 | * directive. If neither is set, we don't care about the name so the |
| 4397 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4398 | */ |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4399 | servername = SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4400 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4401 | if (!servername) { |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4402 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4403 | if (!servername) |
| 4404 | return ok; |
| 4405 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4406 | |
| 4407 | /* We only need to verify the CN on the actual server cert, |
| 4408 | * not the indirect CAs */ |
| 4409 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4410 | if (depth != 0) |
| 4411 | return ok; |
| 4412 | |
| 4413 | /* At this point, the cert is *not* OK unless we can find a |
| 4414 | * hostname match */ |
| 4415 | ok = 0; |
| 4416 | |
| 4417 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4418 | /* It seems like this might happen if verify peer isn't set */ |
| 4419 | if (!cert) |
| 4420 | return ok; |
| 4421 | |
| 4422 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4423 | if (alt_names) { |
| 4424 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4425 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4426 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4427 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 4428 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4429 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4430 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4431 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4432 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4433 | OPENSSL_free(str); |
| 4434 | } |
| 4435 | } |
| 4436 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4437 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | cert_subject = X509_get_subject_name(cert); |
| 4441 | i = -1; |
| 4442 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4443 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4444 | ASN1_STRING *value; |
| 4445 | value = X509_NAME_ENTRY_get_data(entry); |
| 4446 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4447 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4448 | OPENSSL_free(str); |
| 4449 | } |
| 4450 | } |
| 4451 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4452 | /* report the mismatch and indicate if SNI was used or not */ |
| 4453 | if (!ok && !conn->err_code) |
| 4454 | 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] | 4455 | return ok; |
| 4456 | } |
| 4457 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4458 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4459 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4460 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4461 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4462 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4463 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4464 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4465 | SSL_OP_NO_SSLv2 | |
| 4466 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4467 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4468 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4469 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4470 | SSL_MODE_RELEASE_BUFFERS | |
| 4471 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4472 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4473 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4474 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4475 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4476 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4477 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4478 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4479 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4480 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4481 | cfgerr++; |
| 4482 | } |
| 4483 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4484 | /* Automatic memory computations need to know we use SSL there */ |
| 4485 | global.ssl_used_backend = 1; |
| 4486 | |
| 4487 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4488 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4489 | if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4490 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 4491 | curproxy->id, srv->id, |
| 4492 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4493 | cfgerr++; |
| 4494 | return cfgerr; |
| 4495 | } |
| 4496 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4497 | if (srv->use_ssl) |
| 4498 | srv->xprt = &ssl_sock; |
| 4499 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 4500 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4501 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4502 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4503 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4504 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 4505 | proxy_type_str(curproxy), curproxy->id, |
| 4506 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4507 | cfgerr++; |
| 4508 | return cfgerr; |
| 4509 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4510 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4511 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4512 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 4513 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4514 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4515 | else |
| 4516 | flags = conf_ssl_methods->flags; |
| 4517 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4518 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 4519 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4520 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4521 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4522 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4523 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4524 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4525 | min = max = CONF_TLSV_NONE; |
| 4526 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4527 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4528 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4529 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4530 | if (min) { |
| 4531 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4532 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 4533 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4534 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4535 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4536 | hole = 0; |
| 4537 | } |
| 4538 | max = i; |
| 4539 | } |
| 4540 | else { |
| 4541 | min = max = i; |
| 4542 | } |
| 4543 | } |
| 4544 | else { |
| 4545 | if (min) |
| 4546 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4547 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4548 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4549 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4550 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4551 | cfgerr += 1; |
| 4552 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4553 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 4554 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4555 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
| 4556 | precautionary measure to avoid any suprise with older openssl version. */ |
| 4557 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4558 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4559 | else |
| 4560 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4561 | if (flags & methodVersions[i].flag) |
| 4562 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4563 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4564 | /* 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] | 4565 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4566 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4567 | #endif |
| 4568 | |
| 4569 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4570 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4571 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4572 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 4573 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4574 | if (global_ssl.async) |
| 4575 | mode |= SSL_MODE_ASYNC; |
| 4576 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4577 | SSL_CTX_set_mode(ctx, mode); |
| 4578 | srv->ssl_ctx.ctx = ctx; |
| 4579 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4580 | if (srv->ssl_ctx.client_crt) { |
| 4581 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4582 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4583 | proxy_type_str(curproxy), curproxy->id, |
| 4584 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4585 | cfgerr++; |
| 4586 | } |
| 4587 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4588 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4589 | proxy_type_str(curproxy), curproxy->id, |
| 4590 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4591 | cfgerr++; |
| 4592 | } |
| 4593 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4594 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4595 | proxy_type_str(curproxy), curproxy->id, |
| 4596 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4597 | cfgerr++; |
| 4598 | } |
| 4599 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4600 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4601 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4602 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4603 | switch (srv->ssl_ctx.verify) { |
| 4604 | case SSL_SOCK_VERIFY_NONE: |
| 4605 | verify = SSL_VERIFY_NONE; |
| 4606 | break; |
| 4607 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4608 | verify = SSL_VERIFY_PEER; |
| 4609 | break; |
| 4610 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4611 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4612 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4613 | (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] | 4614 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4615 | if (srv->ssl_ctx.ca_file) { |
| 4616 | /* load CAfile to verify */ |
| 4617 | if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4618 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n", |
| 4619 | curproxy->id, srv->id, |
| 4620 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4621 | cfgerr++; |
| 4622 | } |
| 4623 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4624 | else { |
| 4625 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4626 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled by default but no CA file specified. If you're running on a LAN where you're certain to trust the server's certificate, please set an explicit 'verify none' statement on the 'server' line, or use 'ssl-server-verify none' in the global section to disable server-side verifications by default.\n", |
| 4627 | curproxy->id, srv->id, |
| 4628 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4629 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4630 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 4631 | curproxy->id, srv->id, |
| 4632 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4633 | cfgerr++; |
| 4634 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4635 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4636 | if (srv->ssl_ctx.crl_file) { |
| 4637 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4638 | |
| 4639 | if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4640 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 4641 | curproxy->id, srv->id, |
| 4642 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4643 | cfgerr++; |
| 4644 | } |
| 4645 | else { |
| 4646 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4647 | } |
| 4648 | } |
| 4649 | #endif |
| 4650 | } |
| 4651 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4652 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 4653 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 4654 | SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4655 | if (srv->ssl_ctx.ciphers && |
| 4656 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4657 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4658 | curproxy->id, srv->id, |
| 4659 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4660 | cfgerr++; |
| 4661 | } |
| 4662 | |
| 4663 | return cfgerr; |
| 4664 | } |
| 4665 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4666 | /* 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] | 4667 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4668 | * encountered. |
| 4669 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4670 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4671 | { |
| 4672 | struct ebmb_node *node; |
| 4673 | struct sni_ctx *sni; |
| 4674 | int err = 0; |
| 4675 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4676 | /* Automatic memory computations need to know we use SSL there */ |
| 4677 | global.ssl_used_frontend = 1; |
| 4678 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4679 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4680 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4681 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4682 | err++; |
| 4683 | } |
| 4684 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4685 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4686 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4687 | /* It should not be necessary to call this function, but it's |
| 4688 | necessary first to check and move all initialisation related |
| 4689 | to initial_ctx in ssl_sock_initial_ctx. */ |
| 4690 | err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx); |
| 4691 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4692 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4693 | 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] | 4694 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4695 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4696 | while (node) { |
| 4697 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4698 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4699 | /* only initialize the CTX on its first occurrence and |
| 4700 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4701 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4702 | node = ebmb_next(node); |
| 4703 | } |
| 4704 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4705 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4706 | while (node) { |
| 4707 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4708 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4709 | /* only initialize the CTX on its first occurrence and |
| 4710 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4711 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4712 | node = ebmb_next(node); |
| 4713 | } |
| 4714 | return err; |
| 4715 | } |
| 4716 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4717 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4718 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4719 | * alerts are directly emitted since the rest of the stack does it below. |
| 4720 | */ |
| 4721 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4722 | { |
| 4723 | struct proxy *px = bind_conf->frontend; |
| 4724 | int alloc_ctx; |
| 4725 | int err; |
| 4726 | |
| 4727 | if (!bind_conf->is_ssl) { |
| 4728 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4729 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 4730 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4731 | } |
| 4732 | return 0; |
| 4733 | } |
| 4734 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4735 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4736 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 4737 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4738 | } |
| 4739 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4740 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 4741 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4742 | return -1; |
| 4743 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4744 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 4745 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4746 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
| 4747 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, |
| 4748 | sizeof(*sh_ssl_sess_tree), |
| 4749 | ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0); |
| 4750 | if (alloc_ctx < 0) { |
| 4751 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 4752 | ha_alert("Unable to initialize the lock for the shared SSL session cache. You can retry using the global statement 'tune.ssl.force-private-cache' but it could increase CPU usage due to renegotiations if nbproc > 1.\n"); |
| 4753 | else |
| 4754 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 4755 | return -1; |
| 4756 | } |
| 4757 | /* free block callback */ |
| 4758 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 4759 | /* init the root tree within the extra space */ |
| 4760 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 4761 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4762 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4763 | err = 0; |
| 4764 | /* initialize all certificate contexts */ |
| 4765 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 4766 | |
| 4767 | /* initialize CA variables if the certificates generation is enabled */ |
| 4768 | err += ssl_sock_load_ca(bind_conf); |
| 4769 | |
| 4770 | return -err; |
| 4771 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4772 | |
| 4773 | /* release ssl context allocated for servers. */ |
| 4774 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 4775 | { |
| 4776 | if (srv->ssl_ctx.ctx) |
| 4777 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 4778 | } |
| 4779 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4780 | /* 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] | 4781 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 4782 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4783 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4784 | { |
| 4785 | struct ebmb_node *node, *back; |
| 4786 | struct sni_ctx *sni; |
| 4787 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4788 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4789 | while (node) { |
| 4790 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4791 | back = ebmb_next(node); |
| 4792 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4793 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4794 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4795 | ssl_sock_free_ssl_conf(sni->conf); |
| 4796 | free(sni->conf); |
| 4797 | sni->conf = NULL; |
| 4798 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4799 | free(sni); |
| 4800 | node = back; |
| 4801 | } |
| 4802 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4803 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4804 | while (node) { |
| 4805 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4806 | back = ebmb_next(node); |
| 4807 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4808 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4809 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4810 | ssl_sock_free_ssl_conf(sni->conf); |
| 4811 | free(sni->conf); |
| 4812 | sni->conf = NULL; |
| 4813 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4814 | free(sni); |
| 4815 | node = back; |
| 4816 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4817 | SSL_CTX_free(bind_conf->initial_ctx); |
| 4818 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4819 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4820 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4821 | } |
| 4822 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4823 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 4824 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 4825 | { |
| 4826 | ssl_sock_free_ca(bind_conf); |
| 4827 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4828 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4829 | free(bind_conf->ca_sign_file); |
| 4830 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4831 | if (bind_conf->keys_ref) { |
| 4832 | free(bind_conf->keys_ref->filename); |
| 4833 | free(bind_conf->keys_ref->tlskeys); |
| 4834 | LIST_DEL(&bind_conf->keys_ref->list); |
| 4835 | free(bind_conf->keys_ref); |
| 4836 | } |
| 4837 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4838 | bind_conf->ca_sign_pass = NULL; |
| 4839 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4840 | } |
| 4841 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4842 | /* Load CA cert file and private key used to generate certificates */ |
| 4843 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4844 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4845 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4846 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4847 | FILE *fp; |
| 4848 | X509 *cacert = NULL; |
| 4849 | EVP_PKEY *capkey = NULL; |
| 4850 | int err = 0; |
| 4851 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 4852 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4853 | return err; |
| 4854 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4855 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4856 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4857 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4858 | HA_RWLOCK_INIT(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4859 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4860 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4861 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 4862 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4863 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4864 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4865 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 4866 | "no CA certificate File configured at [%s:%d].\n", |
| 4867 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4868 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4869 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4870 | |
| 4871 | /* read in the CA certificate */ |
| 4872 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4873 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4874 | 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] | 4875 | goto load_error; |
| 4876 | } |
| 4877 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4878 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4879 | 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] | 4880 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4881 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4882 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4883 | if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4884 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 4885 | 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] | 4886 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4887 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4888 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4889 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4890 | bind_conf->ca_sign_cert = cacert; |
| 4891 | bind_conf->ca_sign_pkey = capkey; |
| 4892 | return err; |
| 4893 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4894 | read_error: |
| 4895 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4896 | if (capkey) EVP_PKEY_free(capkey); |
| 4897 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4898 | load_error: |
| 4899 | bind_conf->generate_certs = 0; |
| 4900 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4901 | return err; |
| 4902 | } |
| 4903 | |
| 4904 | /* Release CA cert and private key used to generate certificated */ |
| 4905 | void |
| 4906 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 4907 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4908 | if (bind_conf->ca_sign_pkey) |
| 4909 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 4910 | if (bind_conf->ca_sign_cert) |
| 4911 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 4912 | bind_conf->ca_sign_pkey = NULL; |
| 4913 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4914 | } |
| 4915 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4916 | /* |
| 4917 | * This function is called if SSL * context is not yet allocated. The function |
| 4918 | * is designed to be called before any other data-layer operation and sets the |
| 4919 | * handshake flag on the connection. It is safe to call it multiple times. |
| 4920 | * It returns 0 on success and -1 in error case. |
| 4921 | */ |
| 4922 | static int ssl_sock_init(struct connection *conn) |
| 4923 | { |
| 4924 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4925 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4926 | return 0; |
| 4927 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4928 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4929 | return 0; |
| 4930 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4931 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 4932 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4933 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4934 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4935 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4936 | /* If it is in client mode initiate SSL session |
| 4937 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4938 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4939 | int may_retry = 1; |
| 4940 | |
| 4941 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4942 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4943 | conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4944 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4945 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4946 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4947 | goto retry_connect; |
| 4948 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4949 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4950 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4951 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4952 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4953 | /* set fd on SSL session context */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 4954 | if (!SSL_set_fd(conn->xprt_ctx, conn->handle.fd)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4955 | SSL_free(conn->xprt_ctx); |
| 4956 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4957 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4958 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4959 | goto retry_connect; |
| 4960 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4961 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4962 | return -1; |
| 4963 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4964 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4965 | /* set connection pointer */ |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame^] | 4966 | if (!SSL_set_ex_data(conn->xprt_ctx, ssl_app_data_index, conn)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4967 | SSL_free(conn->xprt_ctx); |
| 4968 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4969 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4970 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4971 | goto retry_connect; |
| 4972 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4973 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4974 | return -1; |
| 4975 | } |
| 4976 | |
| 4977 | SSL_set_connect_state(conn->xprt_ctx); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4978 | if (objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 4979 | const unsigned char *ptr = objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 4980 | SSL_SESSION *sess = d2i_SSL_SESSION(NULL, &ptr, objt_server(conn->target)->ssl_ctx.reused_sess[tid].size); |
| 4981 | if(sess && !SSL_set_session(conn->xprt_ctx, sess)) { |
| 4982 | SSL_SESSION_free(sess); |
| 4983 | free(objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 4984 | objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 4985 | } else if (sess) { |
| 4986 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4987 | } |
| 4988 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4989 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4990 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4991 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4992 | |
| 4993 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 4994 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4995 | return 0; |
| 4996 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4997 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4998 | int may_retry = 1; |
| 4999 | |
| 5000 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5001 | /* Alloc a new SSL session ctx */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5002 | 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] | 5003 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5004 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5005 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5006 | goto retry_accept; |
| 5007 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5008 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5009 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5010 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5011 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5012 | /* set fd on SSL session context */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5013 | if (!SSL_set_fd(conn->xprt_ctx, conn->handle.fd)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5014 | SSL_free(conn->xprt_ctx); |
| 5015 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5016 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5017 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5018 | goto retry_accept; |
| 5019 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5020 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5021 | return -1; |
| 5022 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5023 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5024 | /* set connection pointer */ |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame^] | 5025 | if (!SSL_set_ex_data(conn->xprt_ctx, ssl_app_data_index, conn)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5026 | SSL_free(conn->xprt_ctx); |
| 5027 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5028 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5029 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5030 | goto retry_accept; |
| 5031 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5032 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5033 | return -1; |
| 5034 | } |
| 5035 | |
| 5036 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5037 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5038 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5039 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5040 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L || defined(OPENSSL_IS_BORINGSSL) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5041 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 5042 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5043 | |
| 5044 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 5045 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5046 | return 0; |
| 5047 | } |
| 5048 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5049 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5050 | return -1; |
| 5051 | } |
| 5052 | |
| 5053 | |
| 5054 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5055 | * updates the FD status if it wants some polling before being called again. |
| 5056 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5057 | * otherwise it returns non-zero and removes itself from the connection's |
| 5058 | * flags (the bit is provided in <flag> by the caller). |
| 5059 | */ |
| 5060 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 5061 | { |
| 5062 | int ret; |
| 5063 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5064 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5065 | return 0; |
| 5066 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5067 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5068 | goto out_error; |
| 5069 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5070 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L |
| 5071 | /* |
| 5072 | * Check if we have early data. If we do, we have to read them |
| 5073 | * before SSL_do_handshake() is called, And there's no way to |
| 5074 | * detect early data, except to try to read them |
| 5075 | */ |
| 5076 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5077 | size_t read_data; |
| 5078 | |
| 5079 | ret = SSL_read_early_data(conn->xprt_ctx, &conn->tmp_early_data, |
| 5080 | 1, &read_data); |
| 5081 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5082 | goto check_error; |
| 5083 | if (ret == SSL_READ_EARLY_DATA_SUCCESS) { |
| 5084 | conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN); |
| 5085 | return 1; |
| 5086 | } else |
| 5087 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5088 | } |
| 5089 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5090 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5091 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5092 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5093 | * the reneg handshake. |
| 5094 | * Here we use SSL_peek as a workaround for reneg. |
| 5095 | */ |
| 5096 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5097 | char c; |
| 5098 | |
| 5099 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 5100 | if (ret <= 0) { |
| 5101 | /* handshake may have not been completed, let's find why */ |
| 5102 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5103 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5104 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5105 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 5106 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5107 | __conn_sock_want_send(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5108 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5109 | return 0; |
| 5110 | } |
| 5111 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5112 | /* handshake may have been completed but we have |
| 5113 | * no more data to read. |
| 5114 | */ |
| 5115 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5116 | ret = 1; |
| 5117 | goto reneg_ok; |
| 5118 | } |
| 5119 | /* SSL handshake needs to read, L4 connection is ready */ |
| 5120 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 5121 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5122 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5123 | __conn_sock_want_recv(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5124 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5125 | return 0; |
| 5126 | } |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5127 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5128 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5129 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5130 | return 0; |
| 5131 | } |
| 5132 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5133 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5134 | /* if errno is null, then connection was successfully established */ |
| 5135 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5136 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5137 | if (!conn->err_code) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5138 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 5139 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5140 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5141 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 5142 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5143 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 5144 | empty_handshake = state == TLS_ST_BEFORE; |
| 5145 | #else |
| 5146 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
| 5147 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5148 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5149 | if (!errno) { |
| 5150 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5151 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5152 | else |
| 5153 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5154 | } |
| 5155 | else { |
| 5156 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5157 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5158 | else |
| 5159 | conn->err_code = CO_ER_SSL_ABORT; |
| 5160 | } |
| 5161 | } |
| 5162 | else { |
| 5163 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5164 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5165 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5166 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5167 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5168 | #endif |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5169 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5170 | goto out_error; |
| 5171 | } |
| 5172 | else { |
| 5173 | /* Fail on all other handshake errors */ |
| 5174 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5175 | * buffer, causing an RST to be emitted upon close() on |
| 5176 | * TCP sockets. We first try to drain possibly pending |
| 5177 | * data to avoid this as much as possible. |
| 5178 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5179 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5180 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5181 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 5182 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5183 | goto out_error; |
| 5184 | } |
| 5185 | } |
| 5186 | /* read some data: consider handshake completed */ |
| 5187 | goto reneg_ok; |
| 5188 | } |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5189 | ret = SSL_do_handshake(conn->xprt_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5190 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5191 | if (ret != 1) { |
| 5192 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5193 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5194 | |
| 5195 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5196 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 5197 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5198 | __conn_sock_want_send(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5199 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5200 | return 0; |
| 5201 | } |
| 5202 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5203 | /* SSL handshake needs to read, L4 connection is ready */ |
| 5204 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 5205 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5206 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5207 | __conn_sock_want_recv(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5208 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5209 | return 0; |
| 5210 | } |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5211 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5212 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5213 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5214 | return 0; |
| 5215 | } |
| 5216 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5217 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5218 | /* if errno is null, then connection was successfully established */ |
| 5219 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5220 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5221 | if (!conn->err_code) { |
| 5222 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 5223 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5224 | #else |
| 5225 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 5226 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5227 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 5228 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5229 | #else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5230 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5231 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5232 | if (empty_handshake) { |
| 5233 | if (!errno) { |
| 5234 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5235 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5236 | else |
| 5237 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5238 | } |
| 5239 | else { |
| 5240 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5241 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5242 | else |
| 5243 | conn->err_code = CO_ER_SSL_ABORT; |
| 5244 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5245 | } |
| 5246 | else { |
| 5247 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5248 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5249 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5250 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5251 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5252 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5253 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5254 | goto out_error; |
| 5255 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5256 | else { |
| 5257 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5258 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5259 | * buffer, causing an RST to be emitted upon close() on |
| 5260 | * TCP sockets. We first try to drain possibly pending |
| 5261 | * data to avoid this as much as possible. |
| 5262 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5263 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5264 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5265 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 5266 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5267 | goto out_error; |
| 5268 | } |
| 5269 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5270 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5271 | else { |
| 5272 | /* |
| 5273 | * If the server refused the early data, we have to send a |
| 5274 | * 425 to the client, as we no longer have the data to sent |
| 5275 | * them again. |
| 5276 | */ |
| 5277 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
| 5278 | if (SSL_get_early_data_status(conn->xprt_ctx) == SSL_EARLY_DATA_REJECTED) { |
| 5279 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5280 | goto out_error; |
| 5281 | } |
| 5282 | } |
| 5283 | } |
| 5284 | #endif |
| 5285 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5286 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5287 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5288 | |
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 engine API doesn't support moving read/write |
| 5291 | * buffers. So we disable ASYNC mode right after |
| 5292 | * the handshake to avoid buffer oveflows. |
| 5293 | */ |
| 5294 | if (global_ssl.async) |
| 5295 | SSL_clear_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5296 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5297 | /* Handshake succeeded */ |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5298 | if (!SSL_session_reused(conn->xprt_ctx)) { |
| 5299 | if (objt_server(conn->target)) { |
| 5300 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5301 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5302 | global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5303 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5304 | else { |
| 5305 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5306 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5307 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5308 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5309 | } |
| 5310 | |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5311 | #ifdef OPENSSL_IS_BORINGSSL |
| 5312 | if ((conn->flags & CO_FL_EARLY_SSL_HS) && !SSL_in_early_data(conn->xprt_ctx)) |
| 5313 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5314 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5315 | /* The connection is now established at both layers, it's time to leave */ |
| 5316 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5317 | return 1; |
| 5318 | |
| 5319 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5320 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5321 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5322 | ERR_clear_error(); |
| 5323 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5324 | /* free resumed session if exists */ |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5325 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5326 | free(objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5327 | objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5328 | } |
| 5329 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5330 | /* Fail on all other handshake errors */ |
| 5331 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5332 | if (!conn->err_code) |
| 5333 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5334 | return 0; |
| 5335 | } |
| 5336 | |
| 5337 | /* 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] | 5338 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5339 | * buffer wraps, in which case a second call may be performed. The connection's |
| 5340 | * flags are updated with whatever special event is detected (error, read0, |
| 5341 | * empty). The caller is responsible for taking care of those events and |
| 5342 | * avoiding the call if inappropriate. The function does not call the |
| 5343 | * connection's polling update function, so the caller is responsible for this. |
| 5344 | */ |
| 5345 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 5346 | { |
| 5347 | int ret, done = 0; |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5348 | int try; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5349 | |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5350 | conn_refresh_polling_flags(conn); |
| 5351 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5352 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5353 | goto out_error; |
| 5354 | |
| 5355 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5356 | /* a handshake was requested */ |
| 5357 | return 0; |
| 5358 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5359 | /* let's realign the buffer to optimize I/O */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5360 | if (buffer_empty(buf)) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5361 | buf->p = buf->data; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5362 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5363 | |
| 5364 | /* read the largest possible block. For this, we perform only one call |
| 5365 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 5366 | * in which case we accept to do it once again. A new attempt is made on |
| 5367 | * EINTR too. |
| 5368 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 5369 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5370 | int need_out = 0; |
| 5371 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5372 | /* first check if we have some room after p+i */ |
| 5373 | try = buf->data + buf->size - (buf->p + buf->i); |
| 5374 | /* otherwise continue between data and p-o */ |
| 5375 | if (try <= 0) { |
| 5376 | try = buf->p - (buf->data + buf->o); |
| 5377 | if (try <= 0) |
| 5378 | break; |
| 5379 | } |
| 5380 | if (try > count) |
| 5381 | try = count; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5382 | if (((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_EARLY_SSL_HS) && |
| 5383 | conn->tmp_early_data != -1) { |
| 5384 | *bi_end(buf) = conn->tmp_early_data; |
| 5385 | done++; |
| 5386 | try--; |
| 5387 | count--; |
| 5388 | buf->i++; |
| 5389 | conn->tmp_early_data = -1; |
| 5390 | continue; |
| 5391 | } |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5392 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5393 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5394 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5395 | size_t read_length; |
| 5396 | |
| 5397 | ret = SSL_read_early_data(conn->xprt_ctx, |
| 5398 | bi_end(buf), try, &read_length); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5399 | if (ret == SSL_READ_EARLY_DATA_SUCCESS && |
| 5400 | read_length > 0) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5401 | conn->flags |= CO_FL_EARLY_DATA; |
| 5402 | if (ret == SSL_READ_EARLY_DATA_SUCCESS || |
| 5403 | ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5404 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5405 | /* |
| 5406 | * We're done reading the early data, |
| 5407 | * let's make the handshake |
| 5408 | */ |
| 5409 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5410 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5411 | need_out = 1; |
| 5412 | if (read_length == 0) |
| 5413 | break; |
| 5414 | } |
| 5415 | ret = read_length; |
| 5416 | } |
| 5417 | } else |
| 5418 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5419 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5420 | #ifdef OPENSSL_IS_BORINGSSL |
| 5421 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5422 | if (SSL_in_early_data(conn->xprt_ctx)) { |
| 5423 | if (ret > 0) |
| 5424 | conn->flags |= CO_FL_EARLY_DATA; |
| 5425 | } else { |
Emmanuel Hocdet | cebd796 | 2017-11-27 16:14:40 +0100 | [diff] [blame] | 5426 | conn->flags &= ~(CO_FL_EARLY_SSL_HS); |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5427 | } |
| 5428 | } |
| 5429 | #endif |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5430 | if (conn->flags & CO_FL_ERROR) { |
| 5431 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5432 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5433 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5434 | if (ret > 0) { |
| 5435 | buf->i += ret; |
| 5436 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5437 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5438 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5439 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5440 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5441 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5442 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5443 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5444 | __conn_sock_want_send(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5445 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5446 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5447 | if (global_ssl.async) |
| 5448 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5449 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5450 | break; |
| 5451 | } |
| 5452 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5453 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5454 | /* handshake is running, and it may need to re-enable read */ |
| 5455 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5456 | __conn_sock_want_recv(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5457 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5458 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5459 | if (global_ssl.async) |
| 5460 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5461 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5462 | break; |
| 5463 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5464 | /* we need to poll for retry a read later */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5465 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5466 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5467 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 5468 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5469 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 5470 | * stack before shutting down the connection for |
| 5471 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5472 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 5473 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5474 | /* otherwise it's a real error */ |
| 5475 | goto out_error; |
| 5476 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5477 | if (need_out) |
| 5478 | break; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5479 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5480 | leave: |
| 5481 | conn_cond_update_sock_polling(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5482 | return done; |
| 5483 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5484 | clear_ssl_error: |
| 5485 | /* Clear openssl global errors stack */ |
| 5486 | ssl_sock_dump_errors(conn); |
| 5487 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5488 | read0: |
| 5489 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5490 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5491 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5492 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5493 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5494 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5495 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5496 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5497 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5498 | } |
| 5499 | |
| 5500 | |
| 5501 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 5502 | * <flags> may contain some CO_SFL_* flags to hint the system about other |
| 5503 | * pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5504 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5505 | * a second call may be performed. The connection's flags are updated with |
| 5506 | * whatever special event is detected (error, empty). The caller is responsible |
| 5507 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5508 | * function does not call the connection's polling update function, so the caller |
| 5509 | * is responsible for this. |
| 5510 | */ |
| 5511 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 5512 | { |
| 5513 | int ret, try, done; |
| 5514 | |
| 5515 | done = 0; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5516 | conn_refresh_polling_flags(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5517 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5518 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5519 | goto out_error; |
| 5520 | |
| 5521 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5522 | /* a handshake was requested */ |
| 5523 | return 0; |
| 5524 | |
| 5525 | /* send the largest possible block. For this we perform only one call |
| 5526 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5527 | * in which case we accept to do it once again. |
| 5528 | */ |
| 5529 | while (buf->o) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5530 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5531 | size_t written_data; |
| 5532 | #endif |
| 5533 | |
Kevin Hester | cad8234 | 2013-05-30 15:12:41 -0700 | [diff] [blame] | 5534 | try = bo_contig_data(buf); |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5535 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 5536 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5537 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5538 | global_ssl.max_record && try > global_ssl.max_record) { |
| 5539 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5540 | } |
| 5541 | else { |
| 5542 | /* we need to keep the information about the fact that |
| 5543 | * we're not limiting the upcoming send(), because if it |
| 5544 | * fails, we'll have to retry with at least as many data. |
| 5545 | */ |
| 5546 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 5547 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5548 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5549 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5550 | if (!SSL_is_init_finished(conn->xprt_ctx)) { |
| 5551 | unsigned int max_early; |
| 5552 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5553 | if (objt_listener(conn->target)) |
| 5554 | max_early = SSL_get_max_early_data(conn->xprt_ctx); |
| 5555 | else { |
| 5556 | if (SSL_get0_session(conn->xprt_ctx)) |
| 5557 | max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(conn->xprt_ctx)); |
| 5558 | else |
| 5559 | max_early = 0; |
| 5560 | } |
| 5561 | |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 5562 | if (try + conn->sent_early_data > max_early) { |
| 5563 | try -= (try + conn->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5564 | if (try <= 0) { |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 5565 | if (!(conn->flags & CO_FL_EARLY_SSL_HS)) |
| 5566 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5567 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5568 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5569 | } |
| 5570 | ret = SSL_write_early_data(conn->xprt_ctx, bo_ptr(buf), try, &written_data); |
| 5571 | if (ret == 1) { |
| 5572 | ret = written_data; |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 5573 | conn->sent_early_data += ret; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5574 | if (objt_server(conn->target)) { |
| 5575 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5576 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA; |
| 5577 | } |
| 5578 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | } else |
| 5582 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5583 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5584 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5585 | if (conn->flags & CO_FL_ERROR) { |
| 5586 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5587 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5588 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5589 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5590 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
| 5591 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5592 | buf->o -= ret; |
| 5593 | done += ret; |
| 5594 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 5595 | if (likely(buffer_empty(buf))) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5596 | /* optimize data alignment in the buffer */ |
| 5597 | buf->p = buf->data; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5598 | } |
| 5599 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5600 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5601 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5602 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5603 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5604 | /* handshake is running, and it may need to re-enable write */ |
| 5605 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5606 | __conn_sock_want_send(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5607 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5608 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5609 | if (global_ssl.async) |
| 5610 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5611 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5612 | break; |
| 5613 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5614 | /* we need to poll to retry a write later */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5615 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5616 | break; |
| 5617 | } |
| 5618 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5619 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5620 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5621 | __conn_sock_want_recv(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5622 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5623 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5624 | if (global_ssl.async) |
| 5625 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5626 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5627 | break; |
| 5628 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5629 | goto out_error; |
| 5630 | } |
| 5631 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5632 | leave: |
| 5633 | conn_cond_update_sock_polling(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5634 | return done; |
| 5635 | |
| 5636 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5637 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5638 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5639 | ERR_clear_error(); |
| 5640 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5641 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5642 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5643 | } |
| 5644 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5645 | static void ssl_sock_close(struct connection *conn) { |
| 5646 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5647 | if (conn->xprt_ctx) { |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5648 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5649 | if (global_ssl.async) { |
| 5650 | OSSL_ASYNC_FD all_fd[32], afd; |
| 5651 | size_t num_all_fds = 0; |
| 5652 | int i; |
| 5653 | |
| 5654 | SSL_get_all_async_fds(conn->xprt_ctx, NULL, &num_all_fds); |
| 5655 | if (num_all_fds > 32) { |
| 5656 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 5657 | return; |
| 5658 | } |
| 5659 | |
| 5660 | SSL_get_all_async_fds(conn->xprt_ctx, all_fd, &num_all_fds); |
| 5661 | |
| 5662 | /* If an async job is pending, we must try to |
| 5663 | to catch the end using polling before calling |
| 5664 | SSL_free */ |
| 5665 | if (num_all_fds && SSL_waiting_for_async(conn->xprt_ctx)) { |
| 5666 | for (i=0 ; i < num_all_fds ; i++) { |
| 5667 | /* switch on an handler designed to |
| 5668 | * handle the SSL_free |
| 5669 | */ |
| 5670 | afd = all_fd[i]; |
| 5671 | fdtab[afd].iocb = ssl_async_fd_free; |
| 5672 | fdtab[afd].owner = conn->xprt_ctx; |
| 5673 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 5674 | /* To ensure that the fd cache won't be used |
| 5675 | * and we'll catch a real RD event. |
| 5676 | */ |
| 5677 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5678 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5679 | conn->xprt_ctx = NULL; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5680 | HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5681 | return; |
| 5682 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5683 | /* Else we can remove the fds from the fdtab |
| 5684 | * and call SSL_free. |
| 5685 | * note: we do a fd_remove and not a delete |
| 5686 | * because the fd is owned by the engine. |
| 5687 | * the engine is responsible to close |
| 5688 | */ |
| 5689 | for (i=0 ; i < num_all_fds ; i++) |
| 5690 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5691 | } |
| 5692 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5693 | SSL_free(conn->xprt_ctx); |
| 5694 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5695 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5696 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5697 | } |
| 5698 | |
| 5699 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 5700 | * any case, flags the connection as reusable if no handshake was in progress. |
| 5701 | */ |
| 5702 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 5703 | { |
| 5704 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5705 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5706 | if (!clean) |
| 5707 | /* don't sent notify on SSL_shutdown */ |
Willy Tarreau | e3cc3a3 | 2017-02-13 11:12:29 +0100 | [diff] [blame] | 5708 | SSL_set_quiet_shutdown(conn->xprt_ctx, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5709 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5710 | if (SSL_shutdown(conn->xprt_ctx) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5711 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5712 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5713 | ERR_clear_error(); |
| 5714 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5715 | } |
| 5716 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5717 | /* used for ppv2 pkey alog (can be used for logging) */ |
| 5718 | int ssl_sock_get_pkey_algo(struct connection *conn, struct chunk *out) |
| 5719 | { |
| 5720 | struct pkey_info *pkinfo; |
| 5721 | int bits = 0; |
| 5722 | int sig = TLSEXT_signature_anonymous; |
| 5723 | int len = -1; |
| 5724 | |
| 5725 | if (!ssl_sock_is_ssl(conn)) |
| 5726 | return 0; |
| 5727 | |
| 5728 | pkinfo = SSL_get_ex_data(conn->xprt_ctx, ssl_pkey_info_index); |
| 5729 | if (pkinfo) { |
| 5730 | sig = pkinfo->sig; |
| 5731 | bits = pkinfo->bits; |
| 5732 | } else { |
| 5733 | /* multicert and generated cert have no pkey info */ |
| 5734 | X509 *crt; |
| 5735 | EVP_PKEY *pkey; |
| 5736 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5737 | if (!crt) |
| 5738 | return 0; |
| 5739 | pkey = X509_get_pubkey(crt); |
| 5740 | if (pkey) { |
| 5741 | bits = EVP_PKEY_bits(pkey); |
| 5742 | switch(EVP_PKEY_base_id(pkey)) { |
| 5743 | case EVP_PKEY_RSA: |
| 5744 | sig = TLSEXT_signature_rsa; |
| 5745 | break; |
| 5746 | case EVP_PKEY_EC: |
| 5747 | sig = TLSEXT_signature_ecdsa; |
| 5748 | break; |
| 5749 | case EVP_PKEY_DSA: |
| 5750 | sig = TLSEXT_signature_dsa; |
| 5751 | break; |
| 5752 | } |
| 5753 | EVP_PKEY_free(pkey); |
| 5754 | } |
| 5755 | } |
| 5756 | |
| 5757 | switch(sig) { |
| 5758 | case TLSEXT_signature_rsa: |
| 5759 | len = chunk_printf(out, "RSA%d", bits); |
| 5760 | break; |
| 5761 | case TLSEXT_signature_ecdsa: |
| 5762 | len = chunk_printf(out, "EC%d", bits); |
| 5763 | break; |
| 5764 | case TLSEXT_signature_dsa: |
| 5765 | len = chunk_printf(out, "DSA%d", bits); |
| 5766 | break; |
| 5767 | default: |
| 5768 | return 0; |
| 5769 | } |
| 5770 | if (len < 0) |
| 5771 | return 0; |
| 5772 | return 1; |
| 5773 | } |
| 5774 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 5775 | /* used for ppv2 cert signature (can be used for logging) */ |
| 5776 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 5777 | { |
| 5778 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 5779 | X509 *crt; |
| 5780 | |
| 5781 | if (!ssl_sock_is_ssl(conn)) |
| 5782 | return NULL; |
| 5783 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5784 | if (!crt) |
| 5785 | return NULL; |
| 5786 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 5787 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 5788 | } |
| 5789 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 5790 | /* used for ppv2 authority */ |
| 5791 | const char *ssl_sock_get_sni(struct connection *conn) |
| 5792 | { |
| 5793 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5794 | if (!ssl_sock_is_ssl(conn)) |
| 5795 | return NULL; |
| 5796 | return SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 5797 | #else |
| 5798 | return 0; |
| 5799 | #endif |
| 5800 | } |
| 5801 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5802 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5803 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 5804 | { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5805 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5806 | return NULL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5807 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5808 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 5809 | } |
| 5810 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5811 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5812 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 5813 | { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5814 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5815 | return NULL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5816 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5817 | return SSL_get_version(conn->xprt_ctx); |
| 5818 | } |
| 5819 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5820 | /* Extract a serial from a cert, and copy it to a chunk. |
| 5821 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 5822 | * -1 if output is not large enough. |
| 5823 | */ |
| 5824 | static int |
| 5825 | ssl_sock_get_serial(X509 *crt, struct chunk *out) |
| 5826 | { |
| 5827 | ASN1_INTEGER *serial; |
| 5828 | |
| 5829 | serial = X509_get_serialNumber(crt); |
| 5830 | if (!serial) |
| 5831 | return 0; |
| 5832 | |
| 5833 | if (out->size < serial->length) |
| 5834 | return -1; |
| 5835 | |
| 5836 | memcpy(out->str, serial->data, serial->length); |
| 5837 | out->len = serial->length; |
| 5838 | return 1; |
| 5839 | } |
| 5840 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5841 | /* Extract a cert to der, and copy it to a chunk. |
| 5842 | * Returns 1 if cert is found and copied, 0 on der convertion failure and |
| 5843 | * -1 if output is not large enough. |
| 5844 | */ |
| 5845 | static int |
| 5846 | ssl_sock_crt2der(X509 *crt, struct chunk *out) |
| 5847 | { |
| 5848 | int len; |
| 5849 | unsigned char *p = (unsigned char *)out->str;; |
| 5850 | |
| 5851 | len =i2d_X509(crt, NULL); |
| 5852 | if (len <= 0) |
| 5853 | return 1; |
| 5854 | |
| 5855 | if (out->size < len) |
| 5856 | return -1; |
| 5857 | |
| 5858 | i2d_X509(crt,&p); |
| 5859 | out->len = len; |
| 5860 | return 1; |
| 5861 | } |
| 5862 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5863 | |
| 5864 | /* Copy Date in ASN1_UTCTIME format in struct chunk out. |
| 5865 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 5866 | * and -1 if output is not large enough. |
| 5867 | */ |
| 5868 | static int |
| 5869 | ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out) |
| 5870 | { |
| 5871 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 5872 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 5873 | |
| 5874 | if (gentm->length < 12) |
| 5875 | return 0; |
| 5876 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 5877 | return 0; |
| 5878 | if (out->size < gentm->length-2) |
| 5879 | return -1; |
| 5880 | |
| 5881 | memcpy(out->str, gentm->data+2, gentm->length-2); |
| 5882 | out->len = gentm->length-2; |
| 5883 | return 1; |
| 5884 | } |
| 5885 | else if (tm->type == V_ASN1_UTCTIME) { |
| 5886 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 5887 | |
| 5888 | if (utctm->length < 10) |
| 5889 | return 0; |
| 5890 | if (utctm->data[0] >= 0x35) |
| 5891 | return 0; |
| 5892 | if (out->size < utctm->length) |
| 5893 | return -1; |
| 5894 | |
| 5895 | memcpy(out->str, utctm->data, utctm->length); |
| 5896 | out->len = utctm->length; |
| 5897 | return 1; |
| 5898 | } |
| 5899 | |
| 5900 | return 0; |
| 5901 | } |
| 5902 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5903 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 5904 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 5905 | */ |
| 5906 | static int |
| 5907 | ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out) |
| 5908 | { |
| 5909 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5910 | ASN1_OBJECT *obj; |
| 5911 | ASN1_STRING *data; |
| 5912 | const unsigned char *data_ptr; |
| 5913 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5914 | int i, j, n; |
| 5915 | int cur = 0; |
| 5916 | const char *s; |
| 5917 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5918 | int name_count; |
| 5919 | |
| 5920 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5921 | |
| 5922 | out->len = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5923 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5924 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5925 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5926 | else |
| 5927 | j = i; |
| 5928 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5929 | ne = X509_NAME_get_entry(a, j); |
| 5930 | obj = X509_NAME_ENTRY_get_object(ne); |
| 5931 | data = X509_NAME_ENTRY_get_data(ne); |
| 5932 | data_ptr = ASN1_STRING_get0_data(data); |
| 5933 | data_len = ASN1_STRING_length(data); |
| 5934 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5935 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5936 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5937 | s = tmp; |
| 5938 | } |
| 5939 | |
| 5940 | if (chunk_strcasecmp(entry, s) != 0) |
| 5941 | continue; |
| 5942 | |
| 5943 | if (pos < 0) |
| 5944 | cur--; |
| 5945 | else |
| 5946 | cur++; |
| 5947 | |
| 5948 | if (cur != pos) |
| 5949 | continue; |
| 5950 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5951 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5952 | return -1; |
| 5953 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5954 | memcpy(out->str, data_ptr, data_len); |
| 5955 | out->len = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5956 | return 1; |
| 5957 | } |
| 5958 | |
| 5959 | return 0; |
| 5960 | |
| 5961 | } |
| 5962 | |
| 5963 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 5964 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 5965 | */ |
| 5966 | static int |
| 5967 | ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out) |
| 5968 | { |
| 5969 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5970 | ASN1_OBJECT *obj; |
| 5971 | ASN1_STRING *data; |
| 5972 | const unsigned char *data_ptr; |
| 5973 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5974 | int i, n, ln; |
| 5975 | int l = 0; |
| 5976 | const char *s; |
| 5977 | char *p; |
| 5978 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5979 | int name_count; |
| 5980 | |
| 5981 | |
| 5982 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5983 | |
| 5984 | out->len = 0; |
| 5985 | p = out->str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5986 | for (i = 0; i < name_count; i++) { |
| 5987 | ne = X509_NAME_get_entry(a, i); |
| 5988 | obj = X509_NAME_ENTRY_get_object(ne); |
| 5989 | data = X509_NAME_ENTRY_get_data(ne); |
| 5990 | data_ptr = ASN1_STRING_get0_data(data); |
| 5991 | data_len = ASN1_STRING_length(data); |
| 5992 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5993 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5994 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5995 | s = tmp; |
| 5996 | } |
| 5997 | ln = strlen(s); |
| 5998 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5999 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6000 | if (l > out->size) |
| 6001 | return -1; |
| 6002 | out->len = l; |
| 6003 | |
| 6004 | *(p++)='/'; |
| 6005 | memcpy(p, s, ln); |
| 6006 | p += ln; |
| 6007 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6008 | memcpy(p, data_ptr, data_len); |
| 6009 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6010 | } |
| 6011 | |
| 6012 | if (!out->len) |
| 6013 | return 0; |
| 6014 | |
| 6015 | return 1; |
| 6016 | } |
| 6017 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6018 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 6019 | * to disable SNI. |
| 6020 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6021 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 6022 | { |
| 6023 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6024 | char *prev_name; |
| 6025 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6026 | if (!ssl_sock_is_ssl(conn)) |
| 6027 | return; |
| 6028 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6029 | /* if the SNI changes, we must destroy the reusable context so that a |
| 6030 | * new connection will present a new SNI. As an optimization we could |
| 6031 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 6032 | * server. |
| 6033 | */ |
| 6034 | prev_name = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 6035 | if ((!prev_name && hostname) || |
| 6036 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
| 6037 | SSL_set_session(conn->xprt_ctx, NULL); |
| 6038 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6039 | SSL_set_tlsext_host_name(conn->xprt_ctx, hostname); |
| 6040 | #endif |
| 6041 | } |
| 6042 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6043 | /* Extract peer certificate's common name into the chunk dest |
| 6044 | * Returns |
| 6045 | * the len of the extracted common name |
| 6046 | * or 0 if no CN found in DN |
| 6047 | * or -1 on error case (i.e. no peer certificate) |
| 6048 | */ |
| 6049 | 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] | 6050 | { |
| 6051 | X509 *crt = NULL; |
| 6052 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6053 | const char find_cn[] = "CN"; |
| 6054 | const struct chunk find_cn_chunk = { |
| 6055 | .str = (char *)&find_cn, |
| 6056 | .len = sizeof(find_cn)-1 |
| 6057 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6058 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6059 | |
| 6060 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6061 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6062 | |
| 6063 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6064 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6065 | if (!crt) |
| 6066 | goto out; |
| 6067 | |
| 6068 | name = X509_get_subject_name(crt); |
| 6069 | if (!name) |
| 6070 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6071 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6072 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6073 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6074 | if (crt) |
| 6075 | X509_free(crt); |
| 6076 | |
| 6077 | return result; |
| 6078 | } |
| 6079 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6080 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6081 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6082 | { |
| 6083 | X509 *crt = NULL; |
| 6084 | |
| 6085 | if (!ssl_sock_is_ssl(conn)) |
| 6086 | return 0; |
| 6087 | |
| 6088 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6089 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6090 | if (!crt) |
| 6091 | return 0; |
| 6092 | |
| 6093 | X509_free(crt); |
| 6094 | return 1; |
| 6095 | } |
| 6096 | |
| 6097 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6098 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6099 | { |
| 6100 | if (!ssl_sock_is_ssl(conn)) |
| 6101 | return 0; |
| 6102 | |
| 6103 | return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
| 6104 | } |
| 6105 | |
| 6106 | /* returns result from SSL verify */ |
| 6107 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6108 | { |
| 6109 | if (!ssl_sock_is_ssl(conn)) |
| 6110 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 6111 | |
| 6112 | return (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
| 6113 | } |
| 6114 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6115 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6116 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6117 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6118 | * freed by the caller. NPN is also checked if available since older versions |
| 6119 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6120 | */ |
| 6121 | static int ssl_sock_get_alpn(const struct connection *conn, const char **str, int *len) |
| 6122 | { |
| 6123 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6124 | return 0; |
| 6125 | |
| 6126 | *str = NULL; |
| 6127 | |
| 6128 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 6129 | SSL_get0_alpn_selected(conn->xprt_ctx, (const unsigned char **)str, (unsigned *)len); |
| 6130 | if (*str) |
| 6131 | return 1; |
| 6132 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6133 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6134 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, (const unsigned char **)str, (unsigned *)len); |
| 6135 | if (*str) |
| 6136 | return 1; |
| 6137 | #endif |
| 6138 | return 0; |
| 6139 | } |
| 6140 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6141 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 6142 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6143 | static int |
| 6144 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6145 | { |
| 6146 | struct connection *conn; |
| 6147 | |
| 6148 | conn = objt_conn(smp->sess->origin); |
| 6149 | if (!conn || conn->xprt != &ssl_sock) |
| 6150 | return 0; |
| 6151 | |
| 6152 | smp->flags = 0; |
| 6153 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 6154 | smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) && |
| 6155 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6156 | |
| 6157 | return 1; |
| 6158 | } |
| 6159 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6160 | /* boolean, returns true if client cert was present */ |
| 6161 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6162 | 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] | 6163 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6164 | struct connection *conn; |
| 6165 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6166 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6167 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6168 | return 0; |
| 6169 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6170 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6171 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6172 | return 0; |
| 6173 | } |
| 6174 | |
| 6175 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6176 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6177 | 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] | 6178 | |
| 6179 | return 1; |
| 6180 | } |
| 6181 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6182 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 6183 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6184 | * should be use. |
| 6185 | */ |
| 6186 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6187 | 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] | 6188 | { |
| 6189 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 6190 | X509 *crt = NULL; |
| 6191 | int ret = 0; |
| 6192 | struct chunk *smp_trash; |
| 6193 | struct connection *conn; |
| 6194 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6195 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6196 | if (!conn || conn->xprt != &ssl_sock) |
| 6197 | return 0; |
| 6198 | |
| 6199 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 6200 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6201 | return 0; |
| 6202 | } |
| 6203 | |
| 6204 | if (cert_peer) |
| 6205 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6206 | else |
| 6207 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 6208 | |
| 6209 | if (!crt) |
| 6210 | goto out; |
| 6211 | |
| 6212 | smp_trash = get_trash_chunk(); |
| 6213 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 6214 | goto out; |
| 6215 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6216 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6217 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6218 | ret = 1; |
| 6219 | out: |
| 6220 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6221 | if (cert_peer && crt) |
| 6222 | X509_free(crt); |
| 6223 | return ret; |
| 6224 | } |
| 6225 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6226 | /* binary, returns serial of certificate in a binary chunk. |
| 6227 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6228 | * should be use. |
| 6229 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6230 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6231 | 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] | 6232 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6233 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6234 | X509 *crt = NULL; |
| 6235 | int ret = 0; |
| 6236 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6237 | struct connection *conn; |
| 6238 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6239 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6240 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6241 | return 0; |
| 6242 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6243 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6244 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6245 | return 0; |
| 6246 | } |
| 6247 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6248 | if (cert_peer) |
| 6249 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6250 | else |
| 6251 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 6252 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6253 | if (!crt) |
| 6254 | goto out; |
| 6255 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6256 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6257 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 6258 | goto out; |
| 6259 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6260 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6261 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6262 | ret = 1; |
| 6263 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6264 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6265 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6266 | X509_free(crt); |
| 6267 | return ret; |
| 6268 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6269 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6270 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 6271 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6272 | * should be use. |
| 6273 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6274 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6275 | 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] | 6276 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6277 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6278 | X509 *crt = NULL; |
| 6279 | const EVP_MD *digest; |
| 6280 | int ret = 0; |
| 6281 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6282 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6283 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6284 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6285 | if (!conn || conn->xprt != &ssl_sock) |
| 6286 | return 0; |
| 6287 | |
| 6288 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6289 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6290 | return 0; |
| 6291 | } |
| 6292 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6293 | if (cert_peer) |
| 6294 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6295 | else |
| 6296 | crt = SSL_get_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6297 | if (!crt) |
| 6298 | goto out; |
| 6299 | |
| 6300 | smp_trash = get_trash_chunk(); |
| 6301 | digest = EVP_sha1(); |
| 6302 | X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len); |
| 6303 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6304 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6305 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6306 | ret = 1; |
| 6307 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6308 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6309 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6310 | X509_free(crt); |
| 6311 | return ret; |
| 6312 | } |
| 6313 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6314 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 6315 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6316 | * should be use. |
| 6317 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6318 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6319 | 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] | 6320 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6321 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6322 | X509 *crt = NULL; |
| 6323 | int ret = 0; |
| 6324 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6325 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6326 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6327 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6328 | if (!conn || conn->xprt != &ssl_sock) |
| 6329 | return 0; |
| 6330 | |
| 6331 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6332 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6333 | return 0; |
| 6334 | } |
| 6335 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6336 | if (cert_peer) |
| 6337 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6338 | else |
| 6339 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6340 | if (!crt) |
| 6341 | goto out; |
| 6342 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6343 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6344 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 6345 | goto out; |
| 6346 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6347 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6348 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6349 | ret = 1; |
| 6350 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6351 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6352 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6353 | X509_free(crt); |
| 6354 | return ret; |
| 6355 | } |
| 6356 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6357 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 6358 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6359 | * should be use. |
| 6360 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6361 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6362 | 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] | 6363 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6364 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6365 | X509 *crt = NULL; |
| 6366 | X509_NAME *name; |
| 6367 | int ret = 0; |
| 6368 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6369 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6370 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6371 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6372 | if (!conn || conn->xprt != &ssl_sock) |
| 6373 | return 0; |
| 6374 | |
| 6375 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6376 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6377 | return 0; |
| 6378 | } |
| 6379 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6380 | if (cert_peer) |
| 6381 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6382 | else |
| 6383 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6384 | if (!crt) |
| 6385 | goto out; |
| 6386 | |
| 6387 | name = X509_get_issuer_name(crt); |
| 6388 | if (!name) |
| 6389 | goto out; |
| 6390 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6391 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6392 | if (args && args[0].type == ARGT_STR) { |
| 6393 | int pos = 1; |
| 6394 | |
| 6395 | if (args[1].type == ARGT_SINT) |
| 6396 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6397 | |
| 6398 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6399 | goto out; |
| 6400 | } |
| 6401 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6402 | goto out; |
| 6403 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6404 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6405 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6406 | ret = 1; |
| 6407 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6408 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6409 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6410 | X509_free(crt); |
| 6411 | return ret; |
| 6412 | } |
| 6413 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6414 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 6415 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6416 | * should be use. |
| 6417 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6418 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6419 | 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] | 6420 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6421 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6422 | X509 *crt = NULL; |
| 6423 | int ret = 0; |
| 6424 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6425 | struct connection *conn; |
| 6426 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6427 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6428 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6429 | return 0; |
| 6430 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6431 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6432 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6433 | return 0; |
| 6434 | } |
| 6435 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6436 | if (cert_peer) |
| 6437 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6438 | else |
| 6439 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6440 | if (!crt) |
| 6441 | goto out; |
| 6442 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6443 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6444 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 6445 | goto out; |
| 6446 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6447 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6448 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6449 | ret = 1; |
| 6450 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6451 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6452 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6453 | X509_free(crt); |
| 6454 | return ret; |
| 6455 | } |
| 6456 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6457 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 6458 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6459 | * should be use. |
| 6460 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6461 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6462 | 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] | 6463 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6464 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6465 | X509 *crt = NULL; |
| 6466 | X509_NAME *name; |
| 6467 | int ret = 0; |
| 6468 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6469 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6470 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6471 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6472 | if (!conn || conn->xprt != &ssl_sock) |
| 6473 | return 0; |
| 6474 | |
| 6475 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6476 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6477 | return 0; |
| 6478 | } |
| 6479 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6480 | if (cert_peer) |
| 6481 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6482 | else |
| 6483 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6484 | if (!crt) |
| 6485 | goto out; |
| 6486 | |
| 6487 | name = X509_get_subject_name(crt); |
| 6488 | if (!name) |
| 6489 | goto out; |
| 6490 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6491 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6492 | if (args && args[0].type == ARGT_STR) { |
| 6493 | int pos = 1; |
| 6494 | |
| 6495 | if (args[1].type == ARGT_SINT) |
| 6496 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6497 | |
| 6498 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6499 | goto out; |
| 6500 | } |
| 6501 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6502 | goto out; |
| 6503 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6504 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6505 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6506 | ret = 1; |
| 6507 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6508 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6509 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6510 | X509_free(crt); |
| 6511 | return ret; |
| 6512 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6513 | |
| 6514 | /* integer, returns true if current session use a client certificate */ |
| 6515 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6516 | 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] | 6517 | { |
| 6518 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6519 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6520 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6521 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6522 | if (!conn || conn->xprt != &ssl_sock) |
| 6523 | return 0; |
| 6524 | |
| 6525 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6526 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6527 | return 0; |
| 6528 | } |
| 6529 | |
| 6530 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6531 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6532 | if (crt) { |
| 6533 | X509_free(crt); |
| 6534 | } |
| 6535 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6536 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6537 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6538 | return 1; |
| 6539 | } |
| 6540 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6541 | /* integer, returns the certificate version |
| 6542 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6543 | * should be use. |
| 6544 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6545 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6546 | 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] | 6547 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6548 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6549 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6550 | struct connection *conn; |
| 6551 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +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 != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6554 | return 0; |
| 6555 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6556 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6557 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6558 | return 0; |
| 6559 | } |
| 6560 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6561 | if (cert_peer) |
| 6562 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6563 | else |
| 6564 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6565 | if (!crt) |
| 6566 | return 0; |
| 6567 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6568 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6569 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6570 | if (cert_peer) |
| 6571 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6572 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6573 | |
| 6574 | return 1; |
| 6575 | } |
| 6576 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6577 | /* string, returns the certificate's signature algorithm. |
| 6578 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6579 | * should be use. |
| 6580 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6581 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6582 | 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] | 6583 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6584 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6585 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6586 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6587 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6588 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6589 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6590 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6591 | if (!conn || conn->xprt != &ssl_sock) |
| 6592 | return 0; |
| 6593 | |
| 6594 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6595 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6596 | return 0; |
| 6597 | } |
| 6598 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6599 | if (cert_peer) |
| 6600 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6601 | else |
| 6602 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6603 | if (!crt) |
| 6604 | return 0; |
| 6605 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6606 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6607 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6608 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6609 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 6610 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6611 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6612 | if (cert_peer) |
| 6613 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6614 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 6615 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6616 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6617 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6618 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6619 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6620 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6621 | if (cert_peer) |
| 6622 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6623 | |
| 6624 | return 1; |
| 6625 | } |
| 6626 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6627 | /* string, returns the certificate's key algorithm. |
| 6628 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6629 | * should be use. |
| 6630 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6631 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6632 | 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] | 6633 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6634 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6635 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6636 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6637 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6638 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6639 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6640 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6641 | if (!conn || conn->xprt != &ssl_sock) |
| 6642 | return 0; |
| 6643 | |
| 6644 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6645 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6646 | return 0; |
| 6647 | } |
| 6648 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6649 | if (cert_peer) |
| 6650 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6651 | else |
| 6652 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6653 | if (!crt) |
| 6654 | return 0; |
| 6655 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6656 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 6657 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6658 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6659 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 6660 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6661 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6662 | if (cert_peer) |
| 6663 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6664 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 6665 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6666 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6667 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6668 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6669 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6670 | if (cert_peer) |
| 6671 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6672 | |
| 6673 | return 1; |
| 6674 | } |
| 6675 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6676 | /* boolean, returns true if front conn. transport layer is SSL. |
| 6677 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6678 | * char is 'b'. |
| 6679 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6680 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6681 | 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] | 6682 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6683 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6684 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6685 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6686 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6687 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6688 | return 1; |
| 6689 | } |
| 6690 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6691 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6692 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6693 | 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] | 6694 | { |
| 6695 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6696 | struct connection *conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6697 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6698 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6699 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6700 | conn->xprt_ctx && |
| 6701 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6702 | return 1; |
| 6703 | #else |
| 6704 | return 0; |
| 6705 | #endif |
| 6706 | } |
| 6707 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 6708 | /* boolean, returns true if client session has been resumed. |
| 6709 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6710 | * char is 'b'. |
| 6711 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6712 | static int |
| 6713 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6714 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 6715 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6716 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 6717 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6718 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6719 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6720 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6721 | conn->xprt_ctx && |
| 6722 | SSL_session_reused(conn->xprt_ctx); |
| 6723 | return 1; |
| 6724 | } |
| 6725 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6726 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 6727 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6728 | * char is 'b'. |
| 6729 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6730 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6731 | 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] | 6732 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6733 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6734 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6735 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6736 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6737 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6738 | return 0; |
| 6739 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6740 | smp->data.u.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
| 6741 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6742 | return 0; |
| 6743 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6744 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6745 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6746 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6747 | |
| 6748 | return 1; |
| 6749 | } |
| 6750 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6751 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 6752 | * is SSL. |
| 6753 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6754 | * char is 'b'. |
| 6755 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6756 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6757 | 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] | 6758 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6759 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6760 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6761 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6762 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6763 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6764 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6765 | return 0; |
| 6766 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 6767 | if (!SSL_get_cipher_bits(conn->xprt_ctx, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6768 | return 0; |
| 6769 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6770 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6771 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6772 | |
| 6773 | return 1; |
| 6774 | } |
| 6775 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6776 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 6777 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6778 | * char is 'b'. |
| 6779 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6780 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6781 | 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] | 6782 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6783 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6784 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6785 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6786 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6787 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6788 | return 0; |
| 6789 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6790 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
| 6791 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6792 | return 0; |
| 6793 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6794 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6795 | |
| 6796 | return 1; |
| 6797 | } |
| 6798 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6799 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6800 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6801 | 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] | 6802 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6803 | struct connection *conn; |
| 6804 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6805 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6806 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6807 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6808 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6809 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6810 | return 0; |
| 6811 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6812 | smp->data.u.str.str = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6813 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6814 | (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] | 6815 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6816 | if (!smp->data.u.str.str) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6817 | return 0; |
| 6818 | |
| 6819 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6820 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6821 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6822 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6823 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6824 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6825 | 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] | 6826 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6827 | struct connection *conn; |
| 6828 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6829 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6830 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6831 | |
Willy Tarreau | e26bf05 | 2015-05-12 10:30:12 +0200 | [diff] [blame] | 6832 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6833 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6834 | return 0; |
| 6835 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6836 | smp->data.u.str.str = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6837 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6838 | (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] | 6839 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6840 | if (!smp->data.u.str.str) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6841 | return 0; |
| 6842 | |
| 6843 | return 1; |
| 6844 | } |
| 6845 | #endif |
| 6846 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6847 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 6848 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6849 | * char is 'b'. |
| 6850 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6851 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6852 | 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] | 6853 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6854 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6855 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6856 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6857 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6858 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6859 | return 0; |
| 6860 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6861 | smp->data.u.str.str = (char *)SSL_get_version(conn->xprt_ctx); |
| 6862 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6863 | return 0; |
| 6864 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6865 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6866 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6867 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6868 | |
| 6869 | return 1; |
| 6870 | } |
| 6871 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6872 | /* 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] | 6873 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6874 | * char is 'b'. |
| 6875 | */ |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 6876 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6877 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6878 | 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] | 6879 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6880 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6881 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6882 | SSL_SESSION *ssl_sess; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6883 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6884 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6885 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6886 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6887 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6888 | return 0; |
| 6889 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6890 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 6891 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6892 | return 0; |
| 6893 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6894 | smp->data.u.str.str = (char *)SSL_SESSION_get_id(ssl_sess, (unsigned int *)&smp->data.u.str.len); |
| 6895 | if (!smp->data.u.str.str || !smp->data.u.str.len) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6896 | return 0; |
| 6897 | |
| 6898 | return 1; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6899 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 6900 | #endif |
| 6901 | |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6902 | |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 6903 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
| 6904 | static int |
| 6905 | smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6906 | { |
| 6907 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6908 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 6909 | SSL_SESSION *ssl_sess; |
| 6910 | struct chunk *data; |
| 6911 | |
| 6912 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6913 | return 0; |
| 6914 | |
| 6915 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 6916 | if (!ssl_sess) |
| 6917 | return 0; |
| 6918 | |
| 6919 | data = get_trash_chunk(); |
| 6920 | data->len = SSL_SESSION_get_master_key(ssl_sess, (unsigned char *)data->str, data->size); |
| 6921 | if (!data->len) |
| 6922 | return 0; |
| 6923 | |
| 6924 | smp->flags = 0; |
| 6925 | smp->data.type = SMP_T_BIN; |
| 6926 | smp->data.u.str = *data; |
| 6927 | |
| 6928 | return 1; |
| 6929 | } |
| 6930 | #endif |
| 6931 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 6932 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6933 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6934 | 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] | 6935 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6936 | struct connection *conn; |
| 6937 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6938 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6939 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6940 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6941 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6942 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6943 | return 0; |
| 6944 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6945 | smp->data.u.str.str = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 6946 | if (!smp->data.u.str.str) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 6947 | return 0; |
| 6948 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6949 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6950 | return 1; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6951 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 6952 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6953 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6954 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6955 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6956 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6957 | struct connection *conn; |
| 6958 | struct ssl_capture *capture; |
| 6959 | |
| 6960 | conn = objt_conn(smp->sess->origin); |
| 6961 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6962 | return 0; |
| 6963 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6964 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6965 | if (!capture) |
| 6966 | return 0; |
| 6967 | |
| 6968 | smp->flags = SMP_F_CONST; |
| 6969 | smp->data.type = SMP_T_BIN; |
| 6970 | smp->data.u.str.str = capture->ciphersuite; |
| 6971 | smp->data.u.str.len = capture->ciphersuite_len; |
| 6972 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6973 | } |
| 6974 | |
| 6975 | static int |
| 6976 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6977 | { |
| 6978 | struct chunk *data; |
| 6979 | |
| 6980 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 6981 | return 0; |
| 6982 | |
| 6983 | data = get_trash_chunk(); |
| 6984 | dump_binary(data, smp->data.u.str.str, smp->data.u.str.len); |
| 6985 | smp->data.type = SMP_T_BIN; |
| 6986 | smp->data.u.str = *data; |
| 6987 | return 1; |
| 6988 | } |
| 6989 | |
| 6990 | static int |
| 6991 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6992 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6993 | struct connection *conn; |
| 6994 | struct ssl_capture *capture; |
| 6995 | |
| 6996 | conn = objt_conn(smp->sess->origin); |
| 6997 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6998 | return 0; |
| 6999 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7000 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7001 | if (!capture) |
| 7002 | return 0; |
| 7003 | |
| 7004 | smp->data.type = SMP_T_SINT; |
| 7005 | smp->data.u.sint = capture->xxh64; |
| 7006 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7007 | } |
| 7008 | |
| 7009 | static int |
| 7010 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7011 | { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7012 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7013 | struct chunk *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7014 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7015 | |
| 7016 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 7017 | return 0; |
| 7018 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7019 | data = get_trash_chunk(); |
| 7020 | for (i = 0; i + 1 < smp->data.u.str.len; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7021 | const char *str; |
| 7022 | const SSL_CIPHER *cipher; |
| 7023 | const unsigned char *bin = (const unsigned char *)smp->data.u.str.str + i; |
| 7024 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 7025 | #if defined(OPENSSL_IS_BORINGSSL) |
| 7026 | cipher = SSL_get_cipher_by_value(id); |
| 7027 | #else |
| 7028 | struct connection *conn = objt_conn(smp->sess->origin); |
| 7029 | cipher = SSL_CIPHER_find(conn->xprt_ctx, bin); |
| 7030 | #endif |
| 7031 | str = SSL_CIPHER_get_name(cipher); |
| 7032 | if (!str || strcmp(str, "(NONE)") == 0) |
| 7033 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7034 | else |
| 7035 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 7036 | } |
| 7037 | smp->data.type = SMP_T_STR; |
| 7038 | smp->data.u.str = *data; |
| 7039 | return 1; |
| 7040 | #else |
| 7041 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 7042 | #endif |
| 7043 | } |
| 7044 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7045 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7046 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7047 | 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] | 7048 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7049 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7050 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7051 | int finished_len; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7052 | struct chunk *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7053 | |
| 7054 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7055 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7056 | return 0; |
| 7057 | |
| 7058 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 7059 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7060 | return 0; |
| 7061 | } |
| 7062 | |
| 7063 | finished_trash = get_trash_chunk(); |
| 7064 | if (!SSL_session_reused(conn->xprt_ctx)) |
| 7065 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 7066 | else |
| 7067 | finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 7068 | |
| 7069 | if (!finished_len) |
| 7070 | return 0; |
| 7071 | |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 7072 | finished_trash->len = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7073 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7074 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7075 | |
| 7076 | return 1; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7077 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7078 | #endif |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7079 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7080 | /* 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] | 7081 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7082 | 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] | 7083 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7084 | struct connection *conn; |
| 7085 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7086 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7087 | if (!conn || conn->xprt != &ssl_sock) |
| 7088 | return 0; |
| 7089 | |
| 7090 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7091 | smp->flags = SMP_F_MAY_CHANGE; |
| 7092 | return 0; |
| 7093 | } |
| 7094 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7095 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7096 | 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] | 7097 | smp->flags = 0; |
| 7098 | |
| 7099 | return 1; |
| 7100 | } |
| 7101 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7102 | /* 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] | 7103 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7104 | 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] | 7105 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7106 | struct connection *conn; |
| 7107 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7108 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7109 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7110 | return 0; |
| 7111 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7112 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7113 | smp->flags = SMP_F_MAY_CHANGE; |
| 7114 | return 0; |
| 7115 | } |
| 7116 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7117 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7118 | 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] | 7119 | smp->flags = 0; |
| 7120 | |
| 7121 | return 1; |
| 7122 | } |
| 7123 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7124 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7125 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7126 | 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] | 7127 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7128 | struct connection *conn; |
| 7129 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7130 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7131 | if (!conn || conn->xprt != &ssl_sock) |
| 7132 | return 0; |
| 7133 | |
| 7134 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7135 | smp->flags = SMP_F_MAY_CHANGE; |
| 7136 | return 0; |
| 7137 | } |
| 7138 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7139 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7140 | 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] | 7141 | smp->flags = 0; |
| 7142 | |
| 7143 | return 1; |
| 7144 | } |
| 7145 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7146 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7147 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7148 | 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] | 7149 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7150 | struct connection *conn; |
| 7151 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7152 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7153 | if (!conn || conn->xprt != &ssl_sock) |
| 7154 | return 0; |
| 7155 | |
| 7156 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7157 | smp->flags = SMP_F_MAY_CHANGE; |
| 7158 | return 0; |
| 7159 | } |
| 7160 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7161 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7162 | return 0; |
| 7163 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7164 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7165 | 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] | 7166 | smp->flags = 0; |
| 7167 | |
| 7168 | return 1; |
| 7169 | } |
| 7170 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7171 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7172 | 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] | 7173 | { |
| 7174 | if (!*args[cur_arg + 1]) { |
| 7175 | if (err) |
| 7176 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7177 | return ERR_ALERT | ERR_FATAL; |
| 7178 | } |
| 7179 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7180 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7181 | 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] | 7182 | else |
| 7183 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7184 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7185 | return 0; |
| 7186 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7187 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7188 | { |
| 7189 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7190 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7191 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7192 | /* parse the "ca-sign-file" bind keyword */ |
| 7193 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7194 | { |
| 7195 | if (!*args[cur_arg + 1]) { |
| 7196 | if (err) |
| 7197 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7198 | return ERR_ALERT | ERR_FATAL; |
| 7199 | } |
| 7200 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7201 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7202 | 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] | 7203 | else |
| 7204 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 7205 | |
| 7206 | return 0; |
| 7207 | } |
| 7208 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7209 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7210 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7211 | { |
| 7212 | if (!*args[cur_arg + 1]) { |
| 7213 | if (err) |
| 7214 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 7215 | return ERR_ALERT | ERR_FATAL; |
| 7216 | } |
| 7217 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 7218 | return 0; |
| 7219 | } |
| 7220 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7221 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7222 | 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] | 7223 | { |
| 7224 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7225 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7226 | return ERR_ALERT | ERR_FATAL; |
| 7227 | } |
| 7228 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7229 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7230 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7231 | return 0; |
| 7232 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7233 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7234 | { |
| 7235 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 7236 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7237 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7238 | 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] | 7239 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 7240 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 7241 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7242 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7243 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7244 | return ERR_ALERT | ERR_FATAL; |
| 7245 | } |
| 7246 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7247 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 7248 | 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] | 7249 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 7250 | return ERR_ALERT | ERR_FATAL; |
| 7251 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7252 | 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] | 7253 | if (ssl_sock_load_cert(path, conf, err) > 0) |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7254 | return ERR_ALERT | ERR_FATAL; |
| 7255 | |
| 7256 | return 0; |
| 7257 | } |
| 7258 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 7259 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7260 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7261 | |
| 7262 | return 0; |
| 7263 | } |
| 7264 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7265 | /* parse the "crt-list" bind keyword */ |
| 7266 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7267 | { |
| 7268 | if (!*args[cur_arg + 1]) { |
| 7269 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 7270 | return ERR_ALERT | ERR_FATAL; |
| 7271 | } |
| 7272 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7273 | 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] | 7274 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7275 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 7276 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7277 | |
| 7278 | return 0; |
| 7279 | } |
| 7280 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7281 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7282 | 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] | 7283 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7284 | #ifndef X509_V_FLAG_CRL_CHECK |
| 7285 | if (err) |
| 7286 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 7287 | return ERR_ALERT | ERR_FATAL; |
| 7288 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7289 | if (!*args[cur_arg + 1]) { |
| 7290 | if (err) |
| 7291 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 7292 | return ERR_ALERT | ERR_FATAL; |
| 7293 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7294 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7295 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7296 | 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] | 7297 | else |
| 7298 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7299 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7300 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7301 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7302 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7303 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7304 | { |
| 7305 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7306 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7307 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 7308 | /* parse the "curves" bind keyword keyword */ |
| 7309 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7310 | { |
| 7311 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 7312 | if (!*args[cur_arg + 1]) { |
| 7313 | if (err) |
| 7314 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 7315 | return ERR_ALERT | ERR_FATAL; |
| 7316 | } |
| 7317 | conf->curves = strdup(args[cur_arg + 1]); |
| 7318 | return 0; |
| 7319 | #else |
| 7320 | if (err) |
| 7321 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 7322 | return ERR_ALERT | ERR_FATAL; |
| 7323 | #endif |
| 7324 | } |
| 7325 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7326 | { |
| 7327 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 7328 | } |
| 7329 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7330 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7331 | 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] | 7332 | { |
| 7333 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 7334 | if (err) |
| 7335 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 7336 | return ERR_ALERT | ERR_FATAL; |
| 7337 | #elif defined(OPENSSL_NO_ECDH) |
| 7338 | if (err) |
| 7339 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 7340 | return ERR_ALERT | ERR_FATAL; |
| 7341 | #else |
| 7342 | if (!*args[cur_arg + 1]) { |
| 7343 | if (err) |
| 7344 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 7345 | return ERR_ALERT | ERR_FATAL; |
| 7346 | } |
| 7347 | |
| 7348 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7349 | |
| 7350 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7351 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7352 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7353 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7354 | { |
| 7355 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 7356 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7357 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7358 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7359 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7360 | { |
| 7361 | int code; |
| 7362 | char *p = args[cur_arg + 1]; |
| 7363 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 7364 | |
| 7365 | if (!*p) { |
| 7366 | if (err) |
| 7367 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 7368 | return ERR_ALERT | ERR_FATAL; |
| 7369 | } |
| 7370 | |
| 7371 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 7372 | ignerr = &conf->ca_ignerr; |
| 7373 | |
| 7374 | if (strcmp(p, "all") == 0) { |
| 7375 | *ignerr = ~0ULL; |
| 7376 | return 0; |
| 7377 | } |
| 7378 | |
| 7379 | while (p) { |
| 7380 | code = atoi(p); |
| 7381 | if ((code <= 0) || (code > 63)) { |
| 7382 | if (err) |
| 7383 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 7384 | args[cur_arg], code, args[cur_arg + 1]); |
| 7385 | return ERR_ALERT | ERR_FATAL; |
| 7386 | } |
| 7387 | *ignerr |= 1ULL << code; |
| 7388 | p = strchr(p, ','); |
| 7389 | if (p) |
| 7390 | p++; |
| 7391 | } |
| 7392 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7393 | return 0; |
| 7394 | } |
| 7395 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7396 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 7397 | 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] | 7398 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7399 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7400 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7401 | p = strchr(arg, '-'); |
| 7402 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7403 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7404 | p++; |
| 7405 | if (!strcmp(p, "sslv3")) |
| 7406 | v = CONF_SSLV3; |
| 7407 | else if (!strcmp(p, "tlsv10")) |
| 7408 | v = CONF_TLSV10; |
| 7409 | else if (!strcmp(p, "tlsv11")) |
| 7410 | v = CONF_TLSV11; |
| 7411 | else if (!strcmp(p, "tlsv12")) |
| 7412 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 7413 | else if (!strcmp(p, "tlsv13")) |
| 7414 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7415 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7416 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7417 | if (!strncmp(arg, "no-", 3)) |
| 7418 | methods->flags |= methodVersions[v].flag; |
| 7419 | else if (!strncmp(arg, "force-", 6)) |
| 7420 | methods->min = methods->max = v; |
| 7421 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7422 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7423 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7424 | fail: |
| 7425 | if (err) |
| 7426 | memprintf(err, "'%s' : option not implemented", arg); |
| 7427 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7428 | } |
| 7429 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7430 | 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] | 7431 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7432 | 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] | 7433 | } |
| 7434 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7435 | 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] | 7436 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7437 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 7438 | } |
| 7439 | |
| 7440 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 7441 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 7442 | { |
| 7443 | uint16_t i, v = 0; |
| 7444 | char *argv = args[cur_arg + 1]; |
| 7445 | if (!*argv) { |
| 7446 | if (err) |
| 7447 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
| 7448 | return ERR_ALERT | ERR_FATAL; |
| 7449 | } |
| 7450 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 7451 | if (!strcmp(argv, methodVersions[i].name)) |
| 7452 | v = i; |
| 7453 | if (!v) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7454 | if (err) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7455 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7456 | return ERR_ALERT | ERR_FATAL; |
| 7457 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7458 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 7459 | methods->min = v; |
| 7460 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 7461 | methods->max = v; |
| 7462 | else { |
| 7463 | if (err) |
| 7464 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
| 7465 | return ERR_ALERT | ERR_FATAL; |
| 7466 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7467 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7468 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7469 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 7470 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7471 | { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 7472 | #if (OPENSSL_VERSION_NUMBER < 0x10101000L) || !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7473 | ha_warning("crt-list: ssl-min-ver and ssl-max-ver are not supported with this Openssl version (skipped).\n"); |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 7474 | #endif |
| 7475 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 7476 | } |
| 7477 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7478 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7479 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7480 | 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] | 7481 | } |
| 7482 | |
| 7483 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7484 | { |
| 7485 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 7486 | } |
| 7487 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7488 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 7489 | 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] | 7490 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 7491 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7492 | return 0; |
| 7493 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7494 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7495 | /* parse the "allow-0rtt" bind keyword */ |
| 7496 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7497 | { |
| 7498 | conf->early_data = 1; |
| 7499 | return 0; |
| 7500 | } |
| 7501 | |
| 7502 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7503 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 7504 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7505 | return 0; |
| 7506 | } |
| 7507 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7508 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7509 | 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] | 7510 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7511 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7512 | char *p1, *p2; |
| 7513 | |
| 7514 | if (!*args[cur_arg + 1]) { |
| 7515 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 7516 | return ERR_ALERT | ERR_FATAL; |
| 7517 | } |
| 7518 | |
| 7519 | free(conf->npn_str); |
| 7520 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 7521 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 7522 | * so we reuse each comma to store the next <len> and need |
| 7523 | * one more for the end of the string. |
| 7524 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7525 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 7526 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7527 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 7528 | |
| 7529 | /* replace commas with the name length */ |
| 7530 | p1 = conf->npn_str; |
| 7531 | p2 = p1 + 1; |
| 7532 | while (1) { |
| 7533 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 7534 | if (!p2) |
| 7535 | p2 = p1 + 1 + strlen(p1 + 1); |
| 7536 | |
| 7537 | if (p2 - (p1 + 1) > 255) { |
| 7538 | *p2 = '\0'; |
| 7539 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 7540 | return ERR_ALERT | ERR_FATAL; |
| 7541 | } |
| 7542 | |
| 7543 | *p1 = p2 - (p1 + 1); |
| 7544 | p1 = p2; |
| 7545 | |
| 7546 | if (!*p2) |
| 7547 | break; |
| 7548 | |
| 7549 | *(p2++) = '\0'; |
| 7550 | } |
| 7551 | return 0; |
| 7552 | #else |
| 7553 | if (err) |
| 7554 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 7555 | return ERR_ALERT | ERR_FATAL; |
| 7556 | #endif |
| 7557 | } |
| 7558 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7559 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7560 | { |
| 7561 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 7562 | } |
| 7563 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7564 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7565 | 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] | 7566 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7567 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7568 | char *p1, *p2; |
| 7569 | |
| 7570 | if (!*args[cur_arg + 1]) { |
| 7571 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 7572 | return ERR_ALERT | ERR_FATAL; |
| 7573 | } |
| 7574 | |
| 7575 | free(conf->alpn_str); |
| 7576 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 7577 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 7578 | * so we reuse each comma to store the next <len> and need |
| 7579 | * one more for the end of the string. |
| 7580 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7581 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 7582 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7583 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 7584 | |
| 7585 | /* replace commas with the name length */ |
| 7586 | p1 = conf->alpn_str; |
| 7587 | p2 = p1 + 1; |
| 7588 | while (1) { |
| 7589 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 7590 | if (!p2) |
| 7591 | p2 = p1 + 1 + strlen(p1 + 1); |
| 7592 | |
| 7593 | if (p2 - (p1 + 1) > 255) { |
| 7594 | *p2 = '\0'; |
| 7595 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 7596 | return ERR_ALERT | ERR_FATAL; |
| 7597 | } |
| 7598 | |
| 7599 | *p1 = p2 - (p1 + 1); |
| 7600 | p1 = p2; |
| 7601 | |
| 7602 | if (!*p2) |
| 7603 | break; |
| 7604 | |
| 7605 | *(p2++) = '\0'; |
| 7606 | } |
| 7607 | return 0; |
| 7608 | #else |
| 7609 | if (err) |
| 7610 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 7611 | return ERR_ALERT | ERR_FATAL; |
| 7612 | #endif |
| 7613 | } |
| 7614 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7615 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7616 | { |
| 7617 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 7618 | } |
| 7619 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7620 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7621 | 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] | 7622 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 7623 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7624 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7625 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7626 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 7627 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 7628 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7629 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 7630 | if (!conf->ssl_conf.ssl_methods.min) |
| 7631 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 7632 | if (!conf->ssl_conf.ssl_methods.max) |
| 7633 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7634 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7635 | return 0; |
| 7636 | } |
| 7637 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 7638 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 7639 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7640 | { |
| 7641 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 7642 | return 0; |
| 7643 | } |
| 7644 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7645 | /* parse the "generate-certificates" bind keyword */ |
| 7646 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7647 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7648 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7649 | conf->generate_certs = 1; |
| 7650 | #else |
| 7651 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 7652 | err && *err ? *err : ""); |
| 7653 | #endif |
| 7654 | return 0; |
| 7655 | } |
| 7656 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 7657 | /* parse the "strict-sni" bind keyword */ |
| 7658 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7659 | { |
| 7660 | conf->strict_sni = 1; |
| 7661 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7662 | } |
| 7663 | |
| 7664 | /* parse the "tls-ticket-keys" bind keyword */ |
| 7665 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7666 | { |
| 7667 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7668 | FILE *f; |
| 7669 | int i = 0; |
| 7670 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7671 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7672 | |
| 7673 | if (!*args[cur_arg + 1]) { |
| 7674 | if (err) |
| 7675 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 7676 | return ERR_ALERT | ERR_FATAL; |
| 7677 | } |
| 7678 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7679 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
| 7680 | if(keys_ref) { |
| 7681 | conf->keys_ref = keys_ref; |
| 7682 | return 0; |
| 7683 | } |
| 7684 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 7685 | keys_ref = malloc(sizeof(*keys_ref)); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7686 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct tls_sess_key)); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7687 | |
| 7688 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 7689 | if (err) |
| 7690 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 7691 | return ERR_ALERT | ERR_FATAL; |
| 7692 | } |
| 7693 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7694 | keys_ref->filename = strdup(args[cur_arg + 1]); |
| 7695 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7696 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 7697 | int len = strlen(thisline); |
| 7698 | /* Strip newline characters from the end */ |
| 7699 | if(thisline[len - 1] == '\n') |
| 7700 | thisline[--len] = 0; |
| 7701 | |
| 7702 | if(thisline[len - 1] == '\r') |
| 7703 | thisline[--len] = 0; |
| 7704 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7705 | 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] | 7706 | if (err) |
| 7707 | 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] | 7708 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7709 | return ERR_ALERT | ERR_FATAL; |
| 7710 | } |
| 7711 | i++; |
| 7712 | } |
| 7713 | |
| 7714 | if (i < TLS_TICKETS_NO) { |
| 7715 | if (err) |
| 7716 | 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] | 7717 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7718 | return ERR_ALERT | ERR_FATAL; |
| 7719 | } |
| 7720 | |
| 7721 | fclose(f); |
| 7722 | |
| 7723 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 7724 | i -= 2; |
| 7725 | 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] | 7726 | keys_ref->unique_id = -1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7727 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7728 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7729 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7730 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 7731 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7732 | return 0; |
| 7733 | #else |
| 7734 | if (err) |
| 7735 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 7736 | return ERR_ALERT | ERR_FATAL; |
| 7737 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 7738 | } |
| 7739 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7740 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7741 | 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] | 7742 | { |
| 7743 | if (!*args[cur_arg + 1]) { |
| 7744 | if (err) |
| 7745 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 7746 | return ERR_ALERT | ERR_FATAL; |
| 7747 | } |
| 7748 | |
| 7749 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7750 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7751 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7752 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7753 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7754 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7755 | else { |
| 7756 | if (err) |
| 7757 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 7758 | args[cur_arg], args[cur_arg + 1]); |
| 7759 | return ERR_ALERT | ERR_FATAL; |
| 7760 | } |
| 7761 | |
| 7762 | return 0; |
| 7763 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7764 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7765 | { |
| 7766 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 7767 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7768 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 7769 | /* parse the "no-ca-names" bind keyword */ |
| 7770 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7771 | { |
| 7772 | conf->no_ca_names = 1; |
| 7773 | return 0; |
| 7774 | } |
| 7775 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7776 | { |
| 7777 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 7778 | } |
| 7779 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7780 | /************** "server" keywords ****************/ |
| 7781 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7782 | /* parse the "ca-file" server keyword */ |
| 7783 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7784 | { |
| 7785 | if (!*args[*cur_arg + 1]) { |
| 7786 | if (err) |
| 7787 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 7788 | return ERR_ALERT | ERR_FATAL; |
| 7789 | } |
| 7790 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7791 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7792 | 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] | 7793 | else |
| 7794 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 7795 | |
| 7796 | return 0; |
| 7797 | } |
| 7798 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 7799 | /* parse the "check-sni" server keyword */ |
| 7800 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7801 | { |
| 7802 | if (!*args[*cur_arg + 1]) { |
| 7803 | if (err) |
| 7804 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
| 7805 | return ERR_ALERT | ERR_FATAL; |
| 7806 | } |
| 7807 | |
| 7808 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 7809 | if (!newsrv->check.sni) { |
| 7810 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 7811 | return ERR_ALERT | ERR_FATAL; |
| 7812 | } |
| 7813 | return 0; |
| 7814 | |
| 7815 | } |
| 7816 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7817 | /* parse the "check-ssl" server keyword */ |
| 7818 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7819 | { |
| 7820 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7821 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 7822 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
| 7823 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7824 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 7825 | if (!newsrv->ssl_ctx.methods.min) |
| 7826 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 7827 | if (!newsrv->ssl_ctx.methods.max) |
| 7828 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 7829 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7830 | return 0; |
| 7831 | } |
| 7832 | |
| 7833 | /* parse the "ciphers" server keyword */ |
| 7834 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7835 | { |
| 7836 | if (!*args[*cur_arg + 1]) { |
| 7837 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 7838 | return ERR_ALERT | ERR_FATAL; |
| 7839 | } |
| 7840 | |
| 7841 | free(newsrv->ssl_ctx.ciphers); |
| 7842 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 7843 | return 0; |
| 7844 | } |
| 7845 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7846 | /* parse the "crl-file" server keyword */ |
| 7847 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7848 | { |
| 7849 | #ifndef X509_V_FLAG_CRL_CHECK |
| 7850 | if (err) |
| 7851 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 7852 | return ERR_ALERT | ERR_FATAL; |
| 7853 | #else |
| 7854 | if (!*args[*cur_arg + 1]) { |
| 7855 | if (err) |
| 7856 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 7857 | return ERR_ALERT | ERR_FATAL; |
| 7858 | } |
| 7859 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7860 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7861 | 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] | 7862 | else |
| 7863 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 7864 | |
| 7865 | return 0; |
| 7866 | #endif |
| 7867 | } |
| 7868 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 7869 | /* parse the "crt" server keyword */ |
| 7870 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7871 | { |
| 7872 | if (!*args[*cur_arg + 1]) { |
| 7873 | if (err) |
| 7874 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 7875 | return ERR_ALERT | ERR_FATAL; |
| 7876 | } |
| 7877 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7878 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 7879 | memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 7880 | else |
| 7881 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 7882 | |
| 7883 | return 0; |
| 7884 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7885 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 7886 | /* parse the "no-check-ssl" server keyword */ |
| 7887 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7888 | { |
| 7889 | newsrv->check.use_ssl = 0; |
| 7890 | free(newsrv->ssl_ctx.ciphers); |
| 7891 | newsrv->ssl_ctx.ciphers = NULL; |
| 7892 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 7893 | return 0; |
| 7894 | } |
| 7895 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 7896 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 7897 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7898 | { |
| 7899 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 7900 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 7901 | return 0; |
| 7902 | } |
| 7903 | |
| 7904 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 7905 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7906 | { |
| 7907 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 7908 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 7909 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 7910 | return 0; |
| 7911 | } |
| 7912 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 7913 | /* parse the "no-ssl" server keyword */ |
| 7914 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7915 | { |
| 7916 | newsrv->use_ssl = 0; |
| 7917 | free(newsrv->ssl_ctx.ciphers); |
| 7918 | newsrv->ssl_ctx.ciphers = NULL; |
| 7919 | return 0; |
| 7920 | } |
| 7921 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7922 | /* parse the "allow-0rtt" server keyword */ |
| 7923 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7924 | { |
| 7925 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 7926 | return 0; |
| 7927 | } |
| 7928 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 7929 | /* parse the "no-ssl-reuse" server keyword */ |
| 7930 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7931 | { |
| 7932 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 7933 | return 0; |
| 7934 | } |
| 7935 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7936 | /* parse the "no-tls-tickets" server keyword */ |
| 7937 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7938 | { |
| 7939 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 7940 | return 0; |
| 7941 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7942 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 7943 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7944 | { |
| 7945 | newsrv->pp_opts |= SRV_PP_V2; |
| 7946 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 7947 | return 0; |
| 7948 | } |
| 7949 | |
| 7950 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 7951 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7952 | { |
| 7953 | newsrv->pp_opts |= SRV_PP_V2; |
| 7954 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 7955 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 7956 | return 0; |
| 7957 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7958 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7959 | /* parse the "sni" server keyword */ |
| 7960 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7961 | { |
| 7962 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7963 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 7964 | return ERR_ALERT | ERR_FATAL; |
| 7965 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7966 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7967 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7968 | arg = args[*cur_arg + 1]; |
| 7969 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7970 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 7971 | return ERR_ALERT | ERR_FATAL; |
| 7972 | } |
| 7973 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7974 | free(newsrv->sni_expr); |
| 7975 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7976 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7977 | return 0; |
| 7978 | #endif |
| 7979 | } |
| 7980 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7981 | /* parse the "ssl" server keyword */ |
| 7982 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7983 | { |
| 7984 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7985 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 7986 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7987 | return 0; |
| 7988 | } |
| 7989 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 7990 | /* parse the "ssl-reuse" server keyword */ |
| 7991 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7992 | { |
| 7993 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 7994 | return 0; |
| 7995 | } |
| 7996 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 7997 | /* parse the "tls-tickets" server keyword */ |
| 7998 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7999 | { |
| 8000 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 8001 | return 0; |
| 8002 | } |
| 8003 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8004 | /* parse the "verify" server keyword */ |
| 8005 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8006 | { |
| 8007 | if (!*args[*cur_arg + 1]) { |
| 8008 | if (err) |
| 8009 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 8010 | return ERR_ALERT | ERR_FATAL; |
| 8011 | } |
| 8012 | |
| 8013 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8014 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8015 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8016 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8017 | else { |
| 8018 | if (err) |
| 8019 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 8020 | args[*cur_arg], args[*cur_arg + 1]); |
| 8021 | return ERR_ALERT | ERR_FATAL; |
| 8022 | } |
| 8023 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8024 | return 0; |
| 8025 | } |
| 8026 | |
| 8027 | /* parse the "verifyhost" server keyword */ |
| 8028 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8029 | { |
| 8030 | if (!*args[*cur_arg + 1]) { |
| 8031 | if (err) |
| 8032 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 8033 | return ERR_ALERT | ERR_FATAL; |
| 8034 | } |
| 8035 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 8036 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8037 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 8038 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8039 | return 0; |
| 8040 | } |
| 8041 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8042 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 8043 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 8044 | struct proxy *defpx, const char *file, int line, |
| 8045 | char **err) { |
| 8046 | int i = 1; |
| 8047 | |
| 8048 | if (*(args[i]) == 0) { |
| 8049 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8050 | return -1; |
| 8051 | } |
| 8052 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8053 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8054 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 8055 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 8056 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8057 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8058 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 8059 | i++; |
| 8060 | else { |
| 8061 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8062 | return -1; |
| 8063 | } |
| 8064 | } |
| 8065 | 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] | 8066 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8067 | return -1; |
| 8068 | } |
| 8069 | i++; |
| 8070 | } |
| 8071 | return 0; |
| 8072 | } |
| 8073 | |
| 8074 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 8075 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 8076 | struct proxy *defpx, const char *file, int line, |
| 8077 | char **err) { |
| 8078 | int i = 1; |
| 8079 | |
| 8080 | if (*(args[i]) == 0) { |
| 8081 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8082 | return -1; |
| 8083 | } |
| 8084 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8085 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8086 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8087 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8088 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 8089 | i++; |
| 8090 | else { |
| 8091 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8092 | return -1; |
| 8093 | } |
| 8094 | } |
| 8095 | 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] | 8096 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8097 | return -1; |
| 8098 | } |
| 8099 | i++; |
| 8100 | } |
| 8101 | return 0; |
| 8102 | } |
| 8103 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8104 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 8105 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8106 | */ |
| 8107 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 8108 | struct proxy *defpx, const char *file, int line, |
| 8109 | char **err) |
| 8110 | { |
| 8111 | char **target; |
| 8112 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8113 | 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] | 8114 | |
| 8115 | if (too_many_args(1, args, err, NULL)) |
| 8116 | return -1; |
| 8117 | |
| 8118 | if (*target) { |
| 8119 | memprintf(err, "'%s' already specified.", args[0]); |
| 8120 | return -1; |
| 8121 | } |
| 8122 | |
| 8123 | if (*(args[1]) == 0) { |
| 8124 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 8125 | return -1; |
| 8126 | } |
| 8127 | *target = strdup(args[1]); |
| 8128 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8129 | } |
| 8130 | |
| 8131 | /* parse the "ssl-mode-async" keyword in global section. |
| 8132 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8133 | */ |
| 8134 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 8135 | struct proxy *defpx, const char *file, int line, |
| 8136 | char **err) |
| 8137 | { |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 8138 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8139 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 8140 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8141 | return 0; |
| 8142 | #else |
| 8143 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 8144 | return -1; |
| 8145 | #endif |
| 8146 | } |
| 8147 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8148 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8149 | static int ssl_check_async_engine_count(void) { |
| 8150 | int err_code = 0; |
| 8151 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 8152 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8153 | ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n"); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8154 | err_code = ERR_ABORT; |
| 8155 | } |
| 8156 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8157 | } |
| 8158 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8159 | /* parse the "ssl-engine" keyword in global section. |
| 8160 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8161 | */ |
| 8162 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 8163 | struct proxy *defpx, const char *file, int line, |
| 8164 | char **err) |
| 8165 | { |
| 8166 | char *algo; |
| 8167 | int ret = -1; |
| 8168 | |
| 8169 | if (*(args[1]) == 0) { |
| 8170 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 8171 | return ret; |
| 8172 | } |
| 8173 | |
| 8174 | if (*(args[2]) == 0) { |
| 8175 | /* if no list of algorithms is given, it defaults to ALL */ |
| 8176 | algo = strdup("ALL"); |
| 8177 | goto add_engine; |
| 8178 | } |
| 8179 | |
| 8180 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 8181 | if (strcmp(args[2], "algo") != 0) { |
| 8182 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 8183 | return ret; |
| 8184 | } |
| 8185 | |
| 8186 | if (*(args[3]) == 0) { |
| 8187 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 8188 | return ret; |
| 8189 | } |
| 8190 | algo = strdup(args[3]); |
| 8191 | |
| 8192 | add_engine: |
| 8193 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 8194 | openssl_engines_initialized++; |
| 8195 | ret = 0; |
| 8196 | } |
| 8197 | free(algo); |
| 8198 | return ret; |
| 8199 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8200 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8201 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8202 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 8203 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 8204 | */ |
| 8205 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 8206 | struct proxy *defpx, const char *file, int line, |
| 8207 | char **err) |
| 8208 | { |
| 8209 | char **target; |
| 8210 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8211 | 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] | 8212 | |
| 8213 | if (too_many_args(1, args, err, NULL)) |
| 8214 | return -1; |
| 8215 | |
| 8216 | if (*(args[1]) == 0) { |
| 8217 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 8218 | return -1; |
| 8219 | } |
| 8220 | |
| 8221 | free(*target); |
| 8222 | *target = strdup(args[1]); |
| 8223 | return 0; |
| 8224 | } |
| 8225 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8226 | /* parse various global tune.ssl settings consisting in positive integers. |
| 8227 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8228 | */ |
| 8229 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 8230 | struct proxy *defpx, const char *file, int line, |
| 8231 | char **err) |
| 8232 | { |
| 8233 | int *target; |
| 8234 | |
| 8235 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 8236 | target = &global.tune.sslcachesize; |
| 8237 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8238 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8239 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8240 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 8241 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 8242 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8243 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 8244 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8245 | else { |
| 8246 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 8247 | return -1; |
| 8248 | } |
| 8249 | |
| 8250 | if (too_many_args(1, args, err, NULL)) |
| 8251 | return -1; |
| 8252 | |
| 8253 | if (*(args[1]) == 0) { |
| 8254 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 8255 | return -1; |
| 8256 | } |
| 8257 | |
| 8258 | *target = atoi(args[1]); |
| 8259 | if (*target < 0) { |
| 8260 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 8261 | return -1; |
| 8262 | } |
| 8263 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8264 | } |
| 8265 | |
| 8266 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 8267 | struct proxy *defpx, const char *file, int line, |
| 8268 | char **err) |
| 8269 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8270 | int ret; |
| 8271 | |
| 8272 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 8273 | if (ret != 0) |
| 8274 | return ret; |
| 8275 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8276 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8277 | memprintf(err, "'%s' is already configured.", args[0]); |
| 8278 | return -1; |
| 8279 | } |
| 8280 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8281 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 8282 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8283 | memprintf(err, "Out of memory error."); |
| 8284 | return -1; |
| 8285 | } |
| 8286 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8287 | } |
| 8288 | |
| 8289 | /* parse "ssl.force-private-cache". |
| 8290 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8291 | */ |
| 8292 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 8293 | struct proxy *defpx, const char *file, int line, |
| 8294 | char **err) |
| 8295 | { |
| 8296 | if (too_many_args(0, args, err, NULL)) |
| 8297 | return -1; |
| 8298 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8299 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8300 | return 0; |
| 8301 | } |
| 8302 | |
| 8303 | /* parse "ssl.lifetime". |
| 8304 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8305 | */ |
| 8306 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 8307 | struct proxy *defpx, const char *file, int line, |
| 8308 | char **err) |
| 8309 | { |
| 8310 | const char *res; |
| 8311 | |
| 8312 | if (too_many_args(1, args, err, NULL)) |
| 8313 | return -1; |
| 8314 | |
| 8315 | if (*(args[1]) == 0) { |
| 8316 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 8317 | return -1; |
| 8318 | } |
| 8319 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8320 | 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] | 8321 | if (res) { |
| 8322 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 8323 | return -1; |
| 8324 | } |
| 8325 | return 0; |
| 8326 | } |
| 8327 | |
| 8328 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 8329 | /* parse "ssl-dh-param-file". |
| 8330 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8331 | */ |
| 8332 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 8333 | struct proxy *defpx, const char *file, int line, |
| 8334 | char **err) |
| 8335 | { |
| 8336 | if (too_many_args(1, args, err, NULL)) |
| 8337 | return -1; |
| 8338 | |
| 8339 | if (*(args[1]) == 0) { |
| 8340 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 8341 | return -1; |
| 8342 | } |
| 8343 | |
| 8344 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 8345 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 8346 | return -1; |
| 8347 | } |
| 8348 | return 0; |
| 8349 | } |
| 8350 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8351 | /* parse "ssl.default-dh-param". |
| 8352 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8353 | */ |
| 8354 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 8355 | struct proxy *defpx, const char *file, int line, |
| 8356 | char **err) |
| 8357 | { |
| 8358 | if (too_many_args(1, args, err, NULL)) |
| 8359 | return -1; |
| 8360 | |
| 8361 | if (*(args[1]) == 0) { |
| 8362 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 8363 | return -1; |
| 8364 | } |
| 8365 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8366 | global_ssl.default_dh_param = atoi(args[1]); |
| 8367 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8368 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 8369 | return -1; |
| 8370 | } |
| 8371 | return 0; |
| 8372 | } |
| 8373 | #endif |
| 8374 | |
| 8375 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8376 | /* This function is used with TLS ticket keys management. It permits to browse |
| 8377 | * each reference. The variable <getnext> must contain the current node, |
| 8378 | * <end> point to the root node. |
| 8379 | */ |
| 8380 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8381 | static inline |
| 8382 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 8383 | { |
| 8384 | struct tls_keys_ref *ref = getnext; |
| 8385 | |
| 8386 | while (1) { |
| 8387 | |
| 8388 | /* Get next list entry. */ |
| 8389 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 8390 | |
| 8391 | /* If the entry is the last of the list, return NULL. */ |
| 8392 | if (&ref->list == end) |
| 8393 | return NULL; |
| 8394 | |
| 8395 | return ref; |
| 8396 | } |
| 8397 | } |
| 8398 | |
| 8399 | static inline |
| 8400 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 8401 | { |
| 8402 | int id; |
| 8403 | char *error; |
| 8404 | |
| 8405 | /* If the reference starts by a '#', this is numeric id. */ |
| 8406 | if (reference[0] == '#') { |
| 8407 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 8408 | id = strtol(reference + 1, &error, 10); |
| 8409 | if (*error != '\0') |
| 8410 | return NULL; |
| 8411 | |
| 8412 | /* Perform the unique id lookup. */ |
| 8413 | return tlskeys_ref_lookupid(id); |
| 8414 | } |
| 8415 | |
| 8416 | /* Perform the string lookup. */ |
| 8417 | return tlskeys_ref_lookup(reference); |
| 8418 | } |
| 8419 | #endif |
| 8420 | |
| 8421 | |
| 8422 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8423 | |
| 8424 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 8425 | |
| 8426 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 8427 | return cli_io_handler_tlskeys_files(appctx); |
| 8428 | } |
| 8429 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8430 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 8431 | * (next index to be dumped), and cli.p0 (next key reference). |
| 8432 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8433 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 8434 | |
| 8435 | struct stream_interface *si = appctx->owner; |
| 8436 | |
| 8437 | switch (appctx->st2) { |
| 8438 | case STAT_ST_INIT: |
| 8439 | /* Display the column headers. If the message cannot be sent, |
| 8440 | * quit the fucntion with returning 0. The function is called |
| 8441 | * later and restart at the state "STAT_ST_INIT". |
| 8442 | */ |
| 8443 | chunk_reset(&trash); |
| 8444 | |
| 8445 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 8446 | chunk_appendf(&trash, "# id secret\n"); |
| 8447 | else |
| 8448 | chunk_appendf(&trash, "# id (file)\n"); |
| 8449 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8450 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8451 | si_applet_cant_put(si); |
| 8452 | return 0; |
| 8453 | } |
| 8454 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8455 | /* Now, we start the browsing of the references lists. |
| 8456 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 8457 | * available field of this pointer is <list>. It is used with the function |
| 8458 | * tlskeys_list_get_next() for retruning the first available entry |
| 8459 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8460 | if (appctx->ctx.cli.p0 == NULL) { |
| 8461 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 8462 | 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] | 8463 | } |
| 8464 | |
| 8465 | appctx->st2 = STAT_ST_LIST; |
| 8466 | /* fall through */ |
| 8467 | |
| 8468 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8469 | while (appctx->ctx.cli.p0) { |
| 8470 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8471 | |
| 8472 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8473 | 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] | 8474 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8475 | |
| 8476 | if (appctx->ctx.cli.i1 == 0) |
| 8477 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 8478 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8479 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8480 | int head; |
| 8481 | |
| 8482 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 8483 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8484 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8485 | struct chunk *t2 = get_trash_chunk(); |
| 8486 | |
| 8487 | chunk_reset(t2); |
| 8488 | /* 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] | 8489 | 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] | 8490 | sizeof(struct tls_sess_key), t2->str, t2->size); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8491 | 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] | 8492 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8493 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8494 | /* let's try again later from this stream. We add ourselves into |
| 8495 | * this stream's users so that it can remove us upon termination. |
| 8496 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8497 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8498 | si_applet_cant_put(si); |
| 8499 | return 0; |
| 8500 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8501 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8502 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8503 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8504 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8505 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8506 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8507 | /* let's try again later from this stream. We add ourselves into |
| 8508 | * this stream's users so that it can remove us upon termination. |
| 8509 | */ |
| 8510 | si_applet_cant_put(si); |
| 8511 | return 0; |
| 8512 | } |
| 8513 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8514 | 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] | 8515 | break; |
| 8516 | |
| 8517 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8518 | 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] | 8519 | } |
| 8520 | |
| 8521 | appctx->st2 = STAT_ST_FIN; |
| 8522 | /* fall through */ |
| 8523 | |
| 8524 | default: |
| 8525 | appctx->st2 = STAT_ST_FIN; |
| 8526 | return 1; |
| 8527 | } |
| 8528 | return 0; |
| 8529 | } |
| 8530 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8531 | /* sets cli.i0 to non-zero if only file lists should be dumped */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8532 | static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8533 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8534 | /* no parameter, shows only file list */ |
| 8535 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8536 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8537 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 8538 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8539 | } |
| 8540 | |
| 8541 | if (args[2][0] == '*') { |
| 8542 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8543 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8544 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8545 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
| 8546 | if (!appctx->ctx.cli.p0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8547 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8548 | 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] | 8549 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8550 | return 1; |
| 8551 | } |
| 8552 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8553 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 8554 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8555 | } |
| 8556 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8557 | static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8558 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8559 | struct tls_keys_ref *ref; |
| 8560 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8561 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 8562 | if (!*args[3] || !*args[4]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8563 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8564 | 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] | 8565 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8566 | return 1; |
| 8567 | } |
| 8568 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8569 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 8570 | if (!ref) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8571 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8572 | 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] | 8573 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8574 | return 1; |
| 8575 | } |
| 8576 | |
| 8577 | trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size); |
| 8578 | if (trash.len != sizeof(struct tls_sess_key)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8579 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8580 | 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] | 8581 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8582 | return 1; |
| 8583 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8584 | ssl_sock_update_tlskey_ref(ref, &trash); |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8585 | appctx->ctx.cli.severity = LOG_INFO; |
Aurélien Nephtali | 6e8a41d | 2018-03-15 21:48:50 +0100 | [diff] [blame] | 8586 | appctx->ctx.cli.msg = "TLS ticket key updated!\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 8587 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8588 | return 1; |
| 8589 | |
| 8590 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 8591 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8592 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8593 | static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8594 | { |
| 8595 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 8596 | char *err = NULL; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 8597 | int i, j; |
| 8598 | |
| 8599 | if (!payload) |
| 8600 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8601 | |
| 8602 | /* Expect one parameter: the new response in base64 encoding */ |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 8603 | if (!*payload) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8604 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8605 | 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] | 8606 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8607 | return 1; |
| 8608 | } |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 8609 | |
| 8610 | /* remove \r and \n from the payload */ |
| 8611 | for (i = 0, j = 0; payload[i]; i++) { |
| 8612 | if (payload[i] == '\r' || payload[i] == '\n') |
| 8613 | continue; |
| 8614 | payload[j++] = payload[i]; |
| 8615 | } |
| 8616 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8617 | |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 8618 | trash.len = base64dec(payload, j, trash.str, trash.size); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8619 | if (trash.len < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8620 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8621 | 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] | 8622 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8623 | return 1; |
| 8624 | } |
| 8625 | |
| 8626 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
| 8627 | if (err) { |
| 8628 | memprintf(&err, "%s.\n", err); |
| 8629 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 8630 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8631 | } |
Aurélien Nephtali | 9a4da68 | 2018-04-16 19:02:42 +0200 | [diff] [blame] | 8632 | else { |
| 8633 | appctx->ctx.cli.severity = LOG_ERR; |
| 8634 | appctx->ctx.cli.msg = "Failed to update OCSP response.\n"; |
| 8635 | appctx->st0 = CLI_ST_PRINT; |
| 8636 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8637 | return 1; |
| 8638 | } |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8639 | appctx->ctx.cli.severity = LOG_INFO; |
Aurélien Nephtali | 6e8a41d | 2018-03-15 21:48:50 +0100 | [diff] [blame] | 8640 | appctx->ctx.cli.msg = "OCSP Response updated!\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 8641 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8642 | return 1; |
| 8643 | #else |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8644 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8645 | 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] | 8646 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8647 | return 1; |
| 8648 | #endif |
| 8649 | |
| 8650 | } |
| 8651 | |
| 8652 | /* register cli keywords */ |
| 8653 | static struct cli_kw_list cli_kws = {{ },{ |
| 8654 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8655 | { { "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] | 8656 | { { "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] | 8657 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 8658 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8659 | { { NULL }, NULL, NULL, NULL } |
| 8660 | }}; |
| 8661 | |
| 8662 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8663 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8664 | * Please take care of keeping this list alphabetically sorted. |
| 8665 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 8666 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8667 | { "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] | 8668 | { "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] | 8669 | { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 8670 | { "ssl_bc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV }, |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8671 | { "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] | 8672 | { "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] | 8673 | { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8674 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8675 | { "ssl_bc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8676 | #endif |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8677 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
| 8678 | { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 8679 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 8680 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 8681 | { "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] | 8682 | { "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] | 8683 | { "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] | 8684 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8685 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8686 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8687 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8688 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8689 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8690 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 8691 | { "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] | 8692 | { "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] | 8693 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 8694 | { "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] | 8695 | { "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] | 8696 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8697 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8698 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8699 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8700 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8701 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8702 | { "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] | 8703 | { "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] | 8704 | { "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] | 8705 | { "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] | 8706 | { "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] | 8707 | { "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] | 8708 | { "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] | 8709 | { "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] | 8710 | { "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] | 8711 | { "ssl_fc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 8712 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8713 | { "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] | 8714 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 8715 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8716 | { "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] | 8717 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8718 | { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8719 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 8720 | { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8721 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 8722 | { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8723 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8724 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8725 | #endif |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8726 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
| 8727 | { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 8728 | #endif |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8729 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8730 | { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8731 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8732 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8733 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 8734 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8735 | { "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] | 8736 | { NULL, NULL, 0, 0, 0 }, |
| 8737 | }}; |
| 8738 | |
| 8739 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8740 | * Please take care of keeping this list alphabetically sorted. |
| 8741 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 8742 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 8743 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 8744 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 8745 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8746 | }}; |
| 8747 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8748 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8749 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 8750 | * all code contributors. |
| 8751 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 8752 | * the config parser can report an appropriate error when a known keyword was |
| 8753 | * not enabled. |
| 8754 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8755 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8756 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8757 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 8758 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 8759 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 8760 | { "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] | 8761 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8762 | { "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] | 8763 | { "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] | 8764 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 8765 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 8766 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8767 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 8768 | { NULL, NULL, 0 }, |
| 8769 | }; |
| 8770 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 8771 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8772 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8773 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 8774 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 8775 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 8776 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 8777 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 8778 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 8779 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 8780 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 8781 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 8782 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 8783 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 8784 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 8785 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 8786 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 8787 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 8788 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 8789 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8790 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 8791 | { "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] | 8792 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 8793 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 8794 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 8795 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 8796 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8797 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 8798 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8799 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 8800 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8801 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 8802 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 8803 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 8804 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 8805 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8806 | { NULL, NULL, 0 }, |
| 8807 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8808 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8809 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8810 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 8811 | * all code contributors. |
| 8812 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 8813 | * the config parser can report an appropriate error when a known keyword was |
| 8814 | * not enabled. |
| 8815 | */ |
| 8816 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 8817 | { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8818 | { "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] | 8819 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8820 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 8821 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
| 8822 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 8823 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 8824 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 8825 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 8826 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 8827 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 8828 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 8829 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 8830 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 8831 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 8832 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 8833 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 8834 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 8835 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 8836 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 8837 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 8838 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 8839 | { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */ |
| 8840 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 8841 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 8842 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 8843 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 8844 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 8845 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 8846 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 8847 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 8848 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 8849 | { "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] | 8850 | { NULL, NULL, 0, 0 }, |
| 8851 | }}; |
| 8852 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8853 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8854 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 8855 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 8856 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8857 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 8858 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 8859 | #ifndef OPENSSL_NO_DH |
| 8860 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 8861 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8862 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8863 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8864 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8865 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8866 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 8867 | #ifndef OPENSSL_NO_DH |
| 8868 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 8869 | #endif |
| 8870 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 8871 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 8872 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 8873 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8874 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8875 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 8876 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8877 | { 0, NULL, NULL }, |
| 8878 | }}; |
| 8879 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 8880 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 8881 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8882 | .snd_buf = ssl_sock_from_buf, |
| 8883 | .rcv_buf = ssl_sock_to_buf, |
| 8884 | .rcv_pipe = NULL, |
| 8885 | .snd_pipe = NULL, |
| 8886 | .shutr = NULL, |
| 8887 | .shutw = ssl_sock_shutw, |
| 8888 | .close = ssl_sock_close, |
| 8889 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 8890 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 8891 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 8892 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 8893 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 8894 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 8895 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8896 | }; |
| 8897 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8898 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 8899 | struct session *sess, struct stream *s, int flags) |
| 8900 | { |
| 8901 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 8902 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8903 | |
| 8904 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 8905 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8906 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 8907 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8908 | if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) { |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 8909 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8910 | s->req.flags |= CF_READ_NULL; |
| 8911 | return ACT_RET_YIELD; |
| 8912 | } |
| 8913 | } |
| 8914 | return (ACT_RET_CONT); |
| 8915 | } |
| 8916 | |
| 8917 | 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) |
| 8918 | { |
| 8919 | rule->action_ptr = ssl_action_wait_for_hs; |
| 8920 | |
| 8921 | return ACT_RET_PRS_OK; |
| 8922 | } |
| 8923 | |
| 8924 | static struct action_kw_list http_req_actions = {ILH, { |
| 8925 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 8926 | { /* END */ } |
| 8927 | }}; |
| 8928 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 8929 | #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] | 8930 | |
| 8931 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8932 | { |
| 8933 | if (ptr) { |
| 8934 | chunk_destroy(ptr); |
| 8935 | free(ptr); |
| 8936 | } |
| 8937 | } |
| 8938 | |
| 8939 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8940 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8941 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8942 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8943 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 8944 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8945 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8946 | static void __ssl_sock_init(void) |
| 8947 | { |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8948 | char *ptr; |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 8949 | int i; |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8950 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8951 | STACK_OF(SSL_COMP)* cm; |
| 8952 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8953 | if (global_ssl.listen_default_ciphers) |
| 8954 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 8955 | if (global_ssl.connect_default_ciphers) |
| 8956 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 8957 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 8958 | xprt_register(XPRT_SSL, &ssl_sock); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8959 | SSL_library_init(); |
| 8960 | cm = SSL_COMP_get_compression_methods(); |
| 8961 | sk_SSL_COMP_zero(cm); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 8962 | #ifdef USE_THREAD |
| 8963 | ssl_locking_init(); |
| 8964 | #endif |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 8965 | #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] | 8966 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 8967 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame^] | 8968 | ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
Thierry FOURNIER | 16ff050 | 2018-06-17 21:33:01 +0200 | [diff] [blame] | 8969 | ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 8970 | ssl_pkey_info_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8971 | sample_register_fetches(&sample_fetch_keywords); |
| 8972 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8973 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8974 | srv_register_keywords(&srv_kws); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8975 | cfg_register_keywords(&cfg_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8976 | cli_register_kw(&cli_kws); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8977 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8978 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8979 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8980 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 8981 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8982 | hap_register_post_check(tlskeys_finalize_config); |
| 8983 | #endif |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 8984 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8985 | ptr = NULL; |
| 8986 | memprintf(&ptr, "Built with OpenSSL version : " |
| 8987 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 8988 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8989 | #else /* OPENSSL_IS_BORINGSSL */ |
| 8990 | OPENSSL_VERSION_TEXT |
| 8991 | "\nRunning on OpenSSL version : %s%s", |
| 8992 | SSLeay_version(SSLEAY_VERSION), |
| 8993 | ((OPENSSL_VERSION_NUMBER ^ SSLeay()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
| 8994 | #endif |
| 8995 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
| 8996 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 8997 | "no (library version too old)" |
| 8998 | #elif defined(OPENSSL_NO_TLSEXT) |
| 8999 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 9000 | #else |
| 9001 | "yes" |
| 9002 | #endif |
| 9003 | "", ptr); |
| 9004 | |
| 9005 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 9006 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 9007 | "yes" |
| 9008 | #else |
| 9009 | #ifdef OPENSSL_NO_TLSEXT |
| 9010 | "no (because of OPENSSL_NO_TLSEXT)" |
| 9011 | #else |
| 9012 | "no (version might be too old, 0.9.8f min needed)" |
| 9013 | #endif |
| 9014 | #endif |
| 9015 | "", ptr); |
| 9016 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 9017 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 9018 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 9019 | if (methodVersions[i].option) |
| 9020 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 9021 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9022 | hap_register_build_opts(ptr, 1); |
| 9023 | |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 9024 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 9025 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 9026 | |
| 9027 | #ifndef OPENSSL_NO_DH |
| 9028 | 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] | 9029 | hap_register_post_deinit(ssl_free_dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 9030 | #endif |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9031 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9032 | hap_register_post_deinit(ssl_free_engines); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9033 | #endif |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 9034 | /* Load SSL string for the verbose & debug mode. */ |
| 9035 | ERR_load_SSL_strings(); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9036 | |
| 9037 | http_req_keywords_register(&http_req_actions); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9038 | } |
| 9039 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9040 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9041 | void ssl_free_engines(void) { |
| 9042 | struct ssl_engine_list *wl, *wlb; |
| 9043 | /* free up engine list */ |
| 9044 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 9045 | ENGINE_finish(wl->e); |
| 9046 | ENGINE_free(wl->e); |
| 9047 | LIST_DEL(&wl->list); |
| 9048 | free(wl); |
| 9049 | } |
| 9050 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9051 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9052 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9053 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9054 | void ssl_free_dh(void) { |
| 9055 | if (local_dh_1024) { |
| 9056 | DH_free(local_dh_1024); |
| 9057 | local_dh_1024 = NULL; |
| 9058 | } |
| 9059 | if (local_dh_2048) { |
| 9060 | DH_free(local_dh_2048); |
| 9061 | local_dh_2048 = NULL; |
| 9062 | } |
| 9063 | if (local_dh_4096) { |
| 9064 | DH_free(local_dh_4096); |
| 9065 | local_dh_4096 = NULL; |
| 9066 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 9067 | if (global_dh) { |
| 9068 | DH_free(global_dh); |
| 9069 | global_dh = NULL; |
| 9070 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9071 | } |
| 9072 | #endif |
| 9073 | |
| 9074 | __attribute__((destructor)) |
| 9075 | static void __ssl_sock_deinit(void) |
| 9076 | { |
| 9077 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9078 | if (ssl_ctx_lru_tree) { |
| 9079 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 9080 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9081 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9082 | #endif |
| 9083 | |
| 9084 | ERR_remove_state(0); |
| 9085 | ERR_free_strings(); |
| 9086 | |
| 9087 | EVP_cleanup(); |
| 9088 | |
| 9089 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 9090 | CRYPTO_cleanup_all_ex_data(); |
| 9091 | #endif |
| 9092 | } |
| 9093 | |
| 9094 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9095 | /* |
| 9096 | * Local variables: |
| 9097 | * c-indent-level: 8 |
| 9098 | * c-basic-offset: 8 |
| 9099 | * End: |
| 9100 | */ |