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; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 263 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 264 | static int ssl_pkey_info_index = -1; |
| 265 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 266 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 267 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 268 | #endif |
| 269 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 270 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 271 | static unsigned int openssl_engines_initialized; |
| 272 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 273 | struct ssl_engine_list { |
| 274 | struct list list; |
| 275 | ENGINE *e; |
| 276 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 277 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 278 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 279 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 280 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 281 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 282 | static DH *local_dh_1024 = NULL; |
| 283 | static DH *local_dh_2048 = NULL; |
| 284 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 285 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 286 | #endif /* OPENSSL_NO_DH */ |
| 287 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 288 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 289 | /* X509V3 Extensions that will be added on generated certificates */ |
| 290 | #define X509V3_EXT_SIZE 5 |
| 291 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 292 | "basicConstraints", |
| 293 | "nsComment", |
| 294 | "subjectKeyIdentifier", |
| 295 | "authorityKeyIdentifier", |
| 296 | "keyUsage", |
| 297 | }; |
| 298 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 299 | "CA:FALSE", |
| 300 | "\"OpenSSL Generated Certificate\"", |
| 301 | "hash", |
| 302 | "keyid,issuer:always", |
| 303 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 304 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 305 | /* LRU cache to store generated certificate */ |
| 306 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 307 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 308 | static unsigned int ssl_ctx_serial; |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 309 | __decl_hathreads(static HA_RWLOCK_T ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 310 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 311 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 312 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 313 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 314 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 315 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 316 | /* The order here matters for picking a default context, |
| 317 | * keep the most common keytype at the bottom of the list |
| 318 | */ |
| 319 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 320 | "dsa", |
| 321 | "ecdsa", |
| 322 | "rsa" |
| 323 | }; |
| 324 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 325 | #else |
| 326 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 327 | #endif |
| 328 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 329 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 330 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 331 | |
| 332 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 333 | |
| 334 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 335 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 336 | |
| 337 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 338 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 339 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 340 | /* |
| 341 | * This function gives the detail of the SSL error. It is used only |
| 342 | * if the debug mode and the verbose mode are activated. It dump all |
| 343 | * the SSL error until the stack was empty. |
| 344 | */ |
| 345 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 346 | { |
| 347 | unsigned long ret; |
| 348 | |
| 349 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 350 | while(1) { |
| 351 | ret = ERR_get_error(); |
| 352 | if (ret == 0) |
| 353 | return; |
| 354 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 355 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 356 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 361 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 362 | /* |
| 363 | * struct alignment works here such that the key.key is the same as key_data |
| 364 | * Do not change the placement of key_data |
| 365 | */ |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 366 | struct certificate_ocsp { |
| 367 | struct ebmb_node key; |
| 368 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 369 | struct chunk response; |
| 370 | long expire; |
| 371 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 372 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 373 | struct ocsp_cbk_arg { |
| 374 | int is_single; |
| 375 | int single_kt; |
| 376 | union { |
| 377 | struct certificate_ocsp *s_ocsp; |
| 378 | /* |
| 379 | * m_ocsp will have multiple entries dependent on key type |
| 380 | * Entry 0 - DSA |
| 381 | * Entry 1 - ECDSA |
| 382 | * Entry 2 - RSA |
| 383 | */ |
| 384 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 385 | }; |
| 386 | }; |
| 387 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 388 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 389 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 390 | { |
| 391 | int err_code = ERR_ABORT; |
| 392 | ENGINE *engine; |
| 393 | struct ssl_engine_list *el; |
| 394 | |
| 395 | /* grab the structural reference to the engine */ |
| 396 | engine = ENGINE_by_id(engine_id); |
| 397 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 398 | 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] | 399 | goto fail_get; |
| 400 | } |
| 401 | |
| 402 | if (!ENGINE_init(engine)) { |
| 403 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 404 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 405 | goto fail_init; |
| 406 | } |
| 407 | |
| 408 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 409 | 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] | 410 | goto fail_set_method; |
| 411 | } |
| 412 | |
| 413 | el = calloc(1, sizeof(*el)); |
| 414 | el->e = engine; |
| 415 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 416 | nb_engines++; |
| 417 | if (global_ssl.async) |
| 418 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 419 | return 0; |
| 420 | |
| 421 | fail_set_method: |
| 422 | /* release the functional reference from ENGINE_init() */ |
| 423 | ENGINE_finish(engine); |
| 424 | |
| 425 | fail_init: |
| 426 | /* release the structural reference from ENGINE_by_id() */ |
| 427 | ENGINE_free(engine); |
| 428 | |
| 429 | fail_get: |
| 430 | return err_code; |
| 431 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 432 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 433 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 434 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 435 | /* |
| 436 | * openssl async fd handler |
| 437 | */ |
| 438 | static void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 439 | { |
| 440 | struct connection *conn = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 441 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 442 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 443 | * to poll this fd until it is requested |
| 444 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 445 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 446 | fd_cant_recv(fd); |
| 447 | |
| 448 | /* crypto engine is available, let's notify the associated |
| 449 | * connection that it can pursue its processing. |
| 450 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 451 | __conn_sock_want_recv(conn); |
| 452 | __conn_sock_want_send(conn); |
| 453 | conn_update_sock_polling(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 456 | /* |
| 457 | * openssl async delayed SSL_free handler |
| 458 | */ |
| 459 | static void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 460 | { |
| 461 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 462 | OSSL_ASYNC_FD all_fd[32]; |
| 463 | size_t num_all_fds = 0; |
| 464 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 465 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 466 | /* We suppose that the async job for a same SSL * |
| 467 | * are serialized. So if we are awake it is |
| 468 | * because the running job has just finished |
| 469 | * and we can remove all async fds safely |
| 470 | */ |
| 471 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 472 | if (num_all_fds > 32) { |
| 473 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 478 | for (i=0 ; i < num_all_fds ; i++) |
| 479 | fd_remove(all_fd[i]); |
| 480 | |
| 481 | /* 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] | 482 | SSL_free(ssl); |
| 483 | sslconns--; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 484 | HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 485 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 486 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 487 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 488 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 489 | */ |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 490 | static void inline ssl_async_process_fds(struct connection *conn, SSL *ssl) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 491 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 492 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 493 | OSSL_ASYNC_FD del_fd[32]; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 494 | size_t num_add_fds = 0; |
| 495 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 496 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 497 | |
| 498 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 499 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 500 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 501 | 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] | 502 | return; |
| 503 | } |
| 504 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 505 | 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] | 506 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 507 | /* We remove unused fds from the fdtab */ |
| 508 | for (i=0 ; i < num_del_fds ; i++) |
| 509 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 510 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 511 | /* We add new fds to the fdtab */ |
| 512 | for (i=0 ; i < num_add_fds ; i++) { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 513 | fd_insert(add_fd[i], conn, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 516 | num_add_fds = 0; |
| 517 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 518 | if (num_add_fds > 32) { |
| 519 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 520 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 521 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 522 | |
| 523 | /* We activate the polling for all known async fds */ |
| 524 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 525 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 526 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 527 | /* To ensure that the fd cache won't be used |
| 528 | * We'll prefer to catch a real RD event |
| 529 | * because handling an EAGAIN on this fd will |
| 530 | * result in a context switch and also |
| 531 | * some engines uses a fd in blocking mode. |
| 532 | */ |
| 533 | fd_cant_recv(add_fd[i]); |
| 534 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 535 | |
| 536 | /* We must also prevent the conn_handler |
| 537 | * to be called until a read event was |
| 538 | * polled on an async fd |
| 539 | */ |
| 540 | __conn_sock_stop_both(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 541 | } |
| 542 | #endif |
| 543 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 544 | /* |
| 545 | * This function returns the number of seconds elapsed |
| 546 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 547 | * date presented un ASN1_GENERALIZEDTIME. |
| 548 | * |
| 549 | * In parsing error case, it returns -1. |
| 550 | */ |
| 551 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 552 | { |
| 553 | long epoch; |
| 554 | char *p, *end; |
| 555 | const unsigned short month_offset[12] = { |
| 556 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 557 | }; |
| 558 | int year, month; |
| 559 | |
| 560 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 561 | |
| 562 | p = (char *)d->data; |
| 563 | end = p + d->length; |
| 564 | |
| 565 | if (end - p < 4) return -1; |
| 566 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 567 | p += 4; |
| 568 | if (end - p < 2) return -1; |
| 569 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 570 | if (month < 1 || month > 12) return -1; |
| 571 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 572 | We consider leap years and the current month (<marsh or not) */ |
| 573 | epoch = ( ((year - 1970) * 365) |
| 574 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 575 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 576 | + month_offset[month-1] |
| 577 | ) * 24 * 60 * 60; |
| 578 | p += 2; |
| 579 | if (end - p < 2) return -1; |
| 580 | /* Add the number of seconds of completed days of current month */ |
| 581 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 582 | p += 2; |
| 583 | if (end - p < 2) return -1; |
| 584 | /* Add the completed hours of the current day */ |
| 585 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 586 | p += 2; |
| 587 | if (end - p < 2) return -1; |
| 588 | /* Add the completed minutes of the current hour */ |
| 589 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 590 | p += 2; |
| 591 | if (p == end) return -1; |
| 592 | /* Test if there is available seconds */ |
| 593 | if (p[0] < '0' || p[0] > '9') |
| 594 | goto nosec; |
| 595 | if (end - p < 2) return -1; |
| 596 | /* Add the seconds of the current minute */ |
| 597 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 598 | p += 2; |
| 599 | if (p == end) return -1; |
| 600 | /* Ignore seconds float part if present */ |
| 601 | if (p[0] == '.') { |
| 602 | do { |
| 603 | if (++p == end) return -1; |
| 604 | } while (p[0] >= '0' && p[0] <= '9'); |
| 605 | } |
| 606 | |
| 607 | nosec: |
| 608 | if (p[0] == 'Z') { |
| 609 | if (end - p != 1) return -1; |
| 610 | return epoch; |
| 611 | } |
| 612 | else if (p[0] == '+') { |
| 613 | if (end - p != 5) return -1; |
| 614 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 615 | 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] | 616 | } |
| 617 | else if (p[0] == '-') { |
| 618 | if (end - p != 5) return -1; |
| 619 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 620 | 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] | 621 | } |
| 622 | |
| 623 | return -1; |
| 624 | } |
| 625 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 626 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 627 | |
| 628 | /* This function starts to check if the OCSP response (in DER format) contained |
| 629 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 630 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 631 | * contained in the OCSP Response and exits on error if no match. |
| 632 | * If it's a valid OCSP Response: |
| 633 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 634 | * pointed by 'ocsp'. |
| 635 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 636 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 637 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 638 | * already present in the container, it will be overwritten. |
| 639 | * |
| 640 | * Note: OCSP response containing more than one OCSP Single response is not |
| 641 | * considered valid. |
| 642 | * |
| 643 | * Returns 0 on success, 1 in error case. |
| 644 | */ |
| 645 | static int ssl_sock_load_ocsp_response(struct chunk *ocsp_response, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 646 | { |
| 647 | OCSP_RESPONSE *resp; |
| 648 | OCSP_BASICRESP *bs = NULL; |
| 649 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 650 | OCSP_CERTID *id; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 651 | unsigned char *p = (unsigned char *)ocsp_response->str; |
| 652 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 653 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 654 | int reason; |
| 655 | int ret = 1; |
| 656 | |
| 657 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, ocsp_response->len); |
| 658 | if (!resp) { |
| 659 | memprintf(err, "Unable to parse OCSP response"); |
| 660 | goto out; |
| 661 | } |
| 662 | |
| 663 | rc = OCSP_response_status(resp); |
| 664 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 665 | memprintf(err, "OCSP response status not successful"); |
| 666 | goto out; |
| 667 | } |
| 668 | |
| 669 | bs = OCSP_response_get1_basic(resp); |
| 670 | if (!bs) { |
| 671 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 672 | goto out; |
| 673 | } |
| 674 | |
| 675 | count_sr = OCSP_resp_count(bs); |
| 676 | if (count_sr > 1) { |
| 677 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 678 | goto out; |
| 679 | } |
| 680 | |
| 681 | sr = OCSP_resp_get0(bs, 0); |
| 682 | if (!sr) { |
| 683 | memprintf(err, "Failed to get OCSP single response"); |
| 684 | goto out; |
| 685 | } |
| 686 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 687 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 688 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 689 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 690 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 691 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 692 | goto out; |
| 693 | } |
| 694 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 695 | if (!nextupd) { |
| 696 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 697 | goto out; |
| 698 | } |
| 699 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 700 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 701 | if (!rc) { |
| 702 | memprintf(err, "OCSP single response: no longer valid."); |
| 703 | goto out; |
| 704 | } |
| 705 | |
| 706 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 707 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 708 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 709 | goto out; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | if (!ocsp) { |
| 714 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 715 | unsigned char *p; |
| 716 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 717 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 718 | if (!rc) { |
| 719 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 720 | goto out; |
| 721 | } |
| 722 | |
| 723 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 724 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 725 | goto out; |
| 726 | } |
| 727 | |
| 728 | p = key; |
| 729 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 730 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 731 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 732 | if (!ocsp) { |
| 733 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 734 | goto out; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /* According to comments on "chunk_dup", the |
| 739 | previous chunk buffer will be freed */ |
| 740 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 741 | memprintf(err, "OCSP response: Memory allocation error"); |
| 742 | goto out; |
| 743 | } |
| 744 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 745 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 746 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 747 | ret = 0; |
| 748 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 749 | ERR_clear_error(); |
| 750 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 751 | if (bs) |
| 752 | OCSP_BASICRESP_free(bs); |
| 753 | |
| 754 | if (resp) |
| 755 | OCSP_RESPONSE_free(resp); |
| 756 | |
| 757 | return ret; |
| 758 | } |
| 759 | /* |
| 760 | * External function use to update the OCSP response in the OCSP response's |
| 761 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 762 | * to update in DER format. |
| 763 | * |
| 764 | * Returns 0 on success, 1 in error case. |
| 765 | */ |
| 766 | int ssl_sock_update_ocsp_response(struct chunk *ocsp_response, char **err) |
| 767 | { |
| 768 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * This function load the OCSP Resonse in DER format contained in file at |
| 773 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 774 | * |
| 775 | * Returns 0 on success, 1 in error case. |
| 776 | */ |
| 777 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 778 | { |
| 779 | int fd = -1; |
| 780 | int r = 0; |
| 781 | int ret = 1; |
| 782 | |
| 783 | fd = open(ocsp_path, O_RDONLY); |
| 784 | if (fd == -1) { |
| 785 | memprintf(err, "Error opening OCSP response file"); |
| 786 | goto end; |
| 787 | } |
| 788 | |
| 789 | trash.len = 0; |
| 790 | while (trash.len < trash.size) { |
| 791 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 792 | if (r < 0) { |
| 793 | if (errno == EINTR) |
| 794 | continue; |
| 795 | |
| 796 | memprintf(err, "Error reading OCSP response from file"); |
| 797 | goto end; |
| 798 | } |
| 799 | else if (r == 0) { |
| 800 | break; |
| 801 | } |
| 802 | trash.len += r; |
| 803 | } |
| 804 | |
| 805 | close(fd); |
| 806 | fd = -1; |
| 807 | |
| 808 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 809 | end: |
| 810 | if (fd != -1) |
| 811 | close(fd); |
| 812 | |
| 813 | return ret; |
| 814 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 815 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 816 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 817 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 818 | 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) |
| 819 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 820 | struct tls_keys_ref *ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 821 | struct tls_sess_key *keys; |
| 822 | struct connection *conn; |
| 823 | int head; |
| 824 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 825 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 826 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 827 | conn = SSL_get_app_data(s); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 828 | ref = objt_listener(conn->target)->bind_conf->keys_ref; |
| 829 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 830 | |
| 831 | keys = ref->tlskeys; |
| 832 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 833 | |
| 834 | if (enc) { |
| 835 | memcpy(key_name, keys[head].name, 16); |
| 836 | |
| 837 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 838 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 839 | |
| 840 | 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] | 841 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 842 | |
| 843 | HMAC_Init_ex(hctx, keys[head].hmac_key, 16, HASH_FUNCT(), NULL); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 844 | ret = 1; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 845 | } else { |
| 846 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 847 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 848 | goto found; |
| 849 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 850 | ret = 0; |
| 851 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 852 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 853 | found: |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 854 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].hmac_key, 16, HASH_FUNCT(), NULL); |
| 855 | 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] | 856 | goto end; |
| 857 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 858 | /* 2 for key renewal, 1 if current key is still valid */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 859 | ret = i ? 2 : 1; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 860 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 861 | end: |
| 862 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 863 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 867 | { |
| 868 | struct tls_keys_ref *ref; |
| 869 | |
| 870 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 871 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 872 | return ref; |
| 873 | return NULL; |
| 874 | } |
| 875 | |
| 876 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 877 | { |
| 878 | struct tls_keys_ref *ref; |
| 879 | |
| 880 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 881 | if (ref->unique_id == unique_id) |
| 882 | return ref; |
| 883 | return NULL; |
| 884 | } |
| 885 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 886 | void ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, struct chunk *tlskey) |
| 887 | { |
| 888 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 889 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), tlskey->str, tlskey->len); |
| 890 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 891 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 892 | } |
| 893 | |
| 894 | int ssl_sock_update_tlskey(char *filename, struct chunk *tlskey, char **err) |
| 895 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 896 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 897 | |
| 898 | if(!ref) { |
| 899 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 900 | return 1; |
| 901 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 902 | ssl_sock_update_tlskey_ref(ref, tlskey); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 903 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 904 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 905 | |
| 906 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 907 | * automatic ids. It's called just after the basic checks. It returns |
| 908 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 909 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 910 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 911 | { |
| 912 | int i = 0; |
| 913 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 914 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 915 | |
| 916 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 917 | if (ref->unique_id == -1) { |
| 918 | /* Look for the first free id. */ |
| 919 | while (1) { |
| 920 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 921 | if (ref2->unique_id == i) { |
| 922 | i++; |
| 923 | break; |
| 924 | } |
| 925 | } |
| 926 | if (&ref2->list == &tlskeys_reference) |
| 927 | break; |
| 928 | } |
| 929 | |
| 930 | /* Uses the unique id and increment it for the next entry. */ |
| 931 | ref->unique_id = i; |
| 932 | i++; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | /* This sort the reference list by id. */ |
| 937 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 938 | LIST_DEL(&ref->list); |
| 939 | list_for_each_entry(ref3, &tkr, list) { |
| 940 | if (ref->unique_id < ref3->unique_id) { |
| 941 | LIST_ADDQ(&ref3->list, &ref->list); |
| 942 | break; |
| 943 | } |
| 944 | } |
| 945 | if (&ref3->list == &tkr) |
| 946 | LIST_ADDQ(&tkr, &ref->list); |
| 947 | } |
| 948 | |
| 949 | /* swap root */ |
| 950 | LIST_ADD(&tkr, &tlskeys_reference); |
| 951 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 952 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 953 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 954 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 955 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 956 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 957 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 958 | { |
| 959 | switch (evp_keytype) { |
| 960 | case EVP_PKEY_RSA: |
| 961 | return 2; |
| 962 | case EVP_PKEY_DSA: |
| 963 | return 0; |
| 964 | case EVP_PKEY_EC: |
| 965 | return 1; |
| 966 | } |
| 967 | |
| 968 | return -1; |
| 969 | } |
| 970 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 971 | /* |
| 972 | * Callback used to set OCSP status extension content in server hello. |
| 973 | */ |
| 974 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 975 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 976 | struct certificate_ocsp *ocsp; |
| 977 | struct ocsp_cbk_arg *ocsp_arg; |
| 978 | char *ssl_buf; |
| 979 | EVP_PKEY *ssl_pkey; |
| 980 | int key_type; |
| 981 | int index; |
| 982 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 983 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 984 | |
| 985 | ssl_pkey = SSL_get_privatekey(ssl); |
| 986 | if (!ssl_pkey) |
| 987 | return SSL_TLSEXT_ERR_NOACK; |
| 988 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 989 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 990 | |
| 991 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 992 | ocsp = ocsp_arg->s_ocsp; |
| 993 | else { |
| 994 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 995 | * the certificate type |
| 996 | */ |
| 997 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 998 | |
| 999 | if (index < 0) |
| 1000 | return SSL_TLSEXT_ERR_NOACK; |
| 1001 | |
| 1002 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1003 | |
| 1004 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1005 | |
| 1006 | if (!ocsp || |
| 1007 | !ocsp->response.str || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1008 | !ocsp->response.len || |
| 1009 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1010 | return SSL_TLSEXT_ERR_NOACK; |
| 1011 | |
| 1012 | ssl_buf = OPENSSL_malloc(ocsp->response.len); |
| 1013 | if (!ssl_buf) |
| 1014 | return SSL_TLSEXT_ERR_NOACK; |
| 1015 | |
| 1016 | memcpy(ssl_buf, ocsp->response.str, ocsp->response.len); |
| 1017 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.len); |
| 1018 | |
| 1019 | return SSL_TLSEXT_ERR_OK; |
| 1020 | } |
| 1021 | |
| 1022 | /* |
| 1023 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 1024 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 1025 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 1026 | * It should be present in the certificate's extra chain builded from file |
| 1027 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 1028 | * named 'cert_path' suffixed using '.issuer'. |
| 1029 | * |
| 1030 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 1031 | * response. If file is empty or content is not a valid OCSP response, |
| 1032 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 1033 | * is displayed). |
| 1034 | * |
| 1035 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
| 1036 | * succesfully enabled, or -1 in other error case. |
| 1037 | */ |
| 1038 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 1039 | { |
| 1040 | |
| 1041 | BIO *in = NULL; |
| 1042 | X509 *x, *xi = NULL, *issuer = NULL; |
| 1043 | STACK_OF(X509) *chain = NULL; |
| 1044 | OCSP_CERTID *cid = NULL; |
| 1045 | SSL *ssl; |
| 1046 | char ocsp_path[MAXPATHLEN+1]; |
| 1047 | int i, ret = -1; |
| 1048 | struct stat st; |
| 1049 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1050 | char *warn = NULL; |
| 1051 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1052 | pem_password_cb *passwd_cb; |
| 1053 | void *passwd_cb_userdata; |
| 1054 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1055 | |
| 1056 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1057 | |
| 1058 | if (stat(ocsp_path, &st)) |
| 1059 | return 1; |
| 1060 | |
| 1061 | ssl = SSL_new(ctx); |
| 1062 | if (!ssl) |
| 1063 | goto out; |
| 1064 | |
| 1065 | x = SSL_get_certificate(ssl); |
| 1066 | if (!x) |
| 1067 | goto out; |
| 1068 | |
| 1069 | /* Try to lookup for issuer in certificate extra chain */ |
| 1070 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 1071 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 1072 | #else |
| 1073 | chain = ctx->extra_certs; |
| 1074 | #endif |
| 1075 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1076 | issuer = sk_X509_value(chain, i); |
| 1077 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 1078 | break; |
| 1079 | else |
| 1080 | issuer = NULL; |
| 1081 | } |
| 1082 | |
| 1083 | /* If not found try to load issuer from a suffixed file */ |
| 1084 | if (!issuer) { |
| 1085 | char issuer_path[MAXPATHLEN+1]; |
| 1086 | |
| 1087 | in = BIO_new(BIO_s_file()); |
| 1088 | if (!in) |
| 1089 | goto out; |
| 1090 | |
| 1091 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 1092 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 1093 | goto out; |
| 1094 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1095 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 1096 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 1097 | |
| 1098 | 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] | 1099 | if (!xi) |
| 1100 | goto out; |
| 1101 | |
| 1102 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 1103 | goto out; |
| 1104 | |
| 1105 | issuer = xi; |
| 1106 | } |
| 1107 | |
| 1108 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1109 | if (!cid) |
| 1110 | goto out; |
| 1111 | |
| 1112 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1113 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1114 | goto out; |
| 1115 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1116 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1117 | if (!ocsp) |
| 1118 | goto out; |
| 1119 | |
| 1120 | p = ocsp->key_data; |
| 1121 | i2d_OCSP_CERTID(cid, &p); |
| 1122 | |
| 1123 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1124 | if (iocsp == ocsp) |
| 1125 | ocsp = NULL; |
| 1126 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1127 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1128 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1129 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1130 | #endif |
| 1131 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1132 | |
| 1133 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1134 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1135 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1136 | |
| 1137 | cb_arg->is_single = 1; |
| 1138 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1139 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1140 | pkey = X509_get_pubkey(x); |
| 1141 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1142 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1143 | |
| 1144 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1145 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1146 | } else { |
| 1147 | /* |
| 1148 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1149 | * Update that cb_arg with the new cert's staple |
| 1150 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1151 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1152 | struct certificate_ocsp *tmp_ocsp; |
| 1153 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1154 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1155 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1156 | |
| 1157 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1158 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1159 | #else |
| 1160 | cb_arg = ctx->tlsext_status_arg; |
| 1161 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1162 | |
| 1163 | /* |
| 1164 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1165 | * the order of operations below matter, take care when changing it |
| 1166 | */ |
| 1167 | tmp_ocsp = cb_arg->s_ocsp; |
| 1168 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1169 | cb_arg->s_ocsp = NULL; |
| 1170 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1171 | cb_arg->is_single = 0; |
| 1172 | cb_arg->single_kt = 0; |
| 1173 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1174 | pkey = X509_get_pubkey(x); |
| 1175 | key_type = EVP_PKEY_base_id(pkey); |
| 1176 | EVP_PKEY_free(pkey); |
| 1177 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1178 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1179 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1180 | cb_arg->m_ocsp[index] = iocsp; |
| 1181 | |
| 1182 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1183 | |
| 1184 | ret = 0; |
| 1185 | |
| 1186 | warn = NULL; |
| 1187 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 1188 | 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] | 1189 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | out: |
| 1193 | if (ssl) |
| 1194 | SSL_free(ssl); |
| 1195 | |
| 1196 | if (in) |
| 1197 | BIO_free(in); |
| 1198 | |
| 1199 | if (xi) |
| 1200 | X509_free(xi); |
| 1201 | |
| 1202 | if (cid) |
| 1203 | OCSP_CERTID_free(cid); |
| 1204 | |
| 1205 | if (ocsp) |
| 1206 | free(ocsp); |
| 1207 | |
| 1208 | if (warn) |
| 1209 | free(warn); |
| 1210 | |
| 1211 | |
| 1212 | return ret; |
| 1213 | } |
| 1214 | |
| 1215 | #endif |
| 1216 | |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1217 | #ifdef OPENSSL_IS_BORINGSSL |
| 1218 | static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path) |
| 1219 | { |
| 1220 | char ocsp_path[MAXPATHLEN+1]; |
| 1221 | struct stat st; |
| 1222 | int fd = -1, r = 0; |
| 1223 | |
| 1224 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1225 | if (stat(ocsp_path, &st)) |
| 1226 | return 0; |
| 1227 | |
| 1228 | fd = open(ocsp_path, O_RDONLY); |
| 1229 | if (fd == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1230 | ha_warning("Error opening OCSP response file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1231 | return -1; |
| 1232 | } |
| 1233 | |
| 1234 | trash.len = 0; |
| 1235 | while (trash.len < trash.size) { |
| 1236 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 1237 | if (r < 0) { |
| 1238 | if (errno == EINTR) |
| 1239 | continue; |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1240 | ha_warning("Error reading OCSP response from file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1241 | close(fd); |
| 1242 | return -1; |
| 1243 | } |
| 1244 | else if (r == 0) { |
| 1245 | break; |
| 1246 | } |
| 1247 | trash.len += r; |
| 1248 | } |
| 1249 | close(fd); |
| 1250 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)trash.str, trash.len); |
| 1251 | } |
| 1252 | #endif |
| 1253 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 1254 | #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] | 1255 | |
| 1256 | #define CT_EXTENSION_TYPE 18 |
| 1257 | |
| 1258 | static int sctl_ex_index = -1; |
| 1259 | |
| 1260 | /* |
| 1261 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1262 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1263 | * is performed. |
| 1264 | */ |
| 1265 | static int ssl_sock_parse_sctl(struct chunk *sctl) |
| 1266 | { |
| 1267 | int ret = 1; |
| 1268 | int len, pos, sct_len; |
| 1269 | unsigned char *data; |
| 1270 | |
| 1271 | if (sctl->len < 2) |
| 1272 | goto out; |
| 1273 | |
| 1274 | data = (unsigned char *)sctl->str; |
| 1275 | len = (data[0] << 8) | data[1]; |
| 1276 | |
| 1277 | if (len + 2 != sctl->len) |
| 1278 | goto out; |
| 1279 | |
| 1280 | data = data + 2; |
| 1281 | pos = 0; |
| 1282 | while (pos < len) { |
| 1283 | if (len - pos < 2) |
| 1284 | goto out; |
| 1285 | |
| 1286 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1287 | if (pos + sct_len + 2 > len) |
| 1288 | goto out; |
| 1289 | |
| 1290 | pos += sct_len + 2; |
| 1291 | } |
| 1292 | |
| 1293 | ret = 0; |
| 1294 | |
| 1295 | out: |
| 1296 | return ret; |
| 1297 | } |
| 1298 | |
| 1299 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, struct chunk **sctl) |
| 1300 | { |
| 1301 | int fd = -1; |
| 1302 | int r = 0; |
| 1303 | int ret = 1; |
| 1304 | |
| 1305 | *sctl = NULL; |
| 1306 | |
| 1307 | fd = open(sctl_path, O_RDONLY); |
| 1308 | if (fd == -1) |
| 1309 | goto end; |
| 1310 | |
| 1311 | trash.len = 0; |
| 1312 | while (trash.len < trash.size) { |
| 1313 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 1314 | if (r < 0) { |
| 1315 | if (errno == EINTR) |
| 1316 | continue; |
| 1317 | |
| 1318 | goto end; |
| 1319 | } |
| 1320 | else if (r == 0) { |
| 1321 | break; |
| 1322 | } |
| 1323 | trash.len += r; |
| 1324 | } |
| 1325 | |
| 1326 | ret = ssl_sock_parse_sctl(&trash); |
| 1327 | if (ret) |
| 1328 | goto end; |
| 1329 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1330 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1331 | if (!chunk_dup(*sctl, &trash)) { |
| 1332 | free(*sctl); |
| 1333 | *sctl = NULL; |
| 1334 | goto end; |
| 1335 | } |
| 1336 | |
| 1337 | end: |
| 1338 | if (fd != -1) |
| 1339 | close(fd); |
| 1340 | |
| 1341 | return ret; |
| 1342 | } |
| 1343 | |
| 1344 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1345 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1346 | struct chunk *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1347 | |
| 1348 | *out = (unsigned char *)sctl->str; |
| 1349 | *outlen = sctl->len; |
| 1350 | |
| 1351 | return 1; |
| 1352 | } |
| 1353 | |
| 1354 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1355 | { |
| 1356 | return 1; |
| 1357 | } |
| 1358 | |
| 1359 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 1360 | { |
| 1361 | char sctl_path[MAXPATHLEN+1]; |
| 1362 | int ret = -1; |
| 1363 | struct stat st; |
| 1364 | struct chunk *sctl = NULL; |
| 1365 | |
| 1366 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 1367 | |
| 1368 | if (stat(sctl_path, &st)) |
| 1369 | return 1; |
| 1370 | |
| 1371 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 1372 | goto out; |
| 1373 | |
| 1374 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 1375 | free(sctl); |
| 1376 | goto out; |
| 1377 | } |
| 1378 | |
| 1379 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1380 | |
| 1381 | ret = 0; |
| 1382 | |
| 1383 | out: |
| 1384 | return ret; |
| 1385 | } |
| 1386 | |
| 1387 | #endif |
| 1388 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1389 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1390 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1391 | struct connection *conn = SSL_get_app_data(ssl); |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1392 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1393 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1394 | |
| 1395 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1396 | /* Disable renegotiation (CVE-2009-3555) */ |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 1397 | 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] | 1398 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1399 | conn->err_code = CO_ER_SSL_RENEG; |
| 1400 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1401 | } |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1402 | |
| 1403 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 1404 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 1405 | /* Long certificate chains optimz |
| 1406 | If write and read bios are differents, we |
| 1407 | consider that the buffering was activated, |
| 1408 | so we rise the output buffer size from 4k |
| 1409 | to 16k */ |
| 1410 | write_bio = SSL_get_wbio(ssl); |
| 1411 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1412 | BIO_set_write_buffer_size(write_bio, 16384); |
| 1413 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 1414 | } |
| 1415 | } |
| 1416 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1417 | } |
| 1418 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1419 | /* Callback is called for each certificate of the chain during a verify |
| 1420 | ok is set to 1 if preverify detect no error on current certificate. |
| 1421 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1422 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1423 | { |
| 1424 | SSL *ssl; |
| 1425 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1426 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1427 | |
| 1428 | ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1429 | conn = SSL_get_app_data(ssl); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1430 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1431 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1432 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1433 | if (ok) /* no errors */ |
| 1434 | return ok; |
| 1435 | |
| 1436 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1437 | err = X509_STORE_CTX_get_error(x_store); |
| 1438 | |
| 1439 | /* check if CA error needs to be ignored */ |
| 1440 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1441 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 1442 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1443 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1444 | } |
| 1445 | |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1446 | 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] | 1447 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1448 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1449 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1450 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1451 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1452 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1456 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 1457 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1458 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1459 | /* check if certificate error needs to be ignored */ |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1460 | 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] | 1461 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1462 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1463 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1464 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1465 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1466 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1467 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1468 | } |
| 1469 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1470 | static inline |
| 1471 | 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] | 1472 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1473 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1474 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1475 | unsigned char *msg; |
| 1476 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1477 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1478 | |
| 1479 | /* This function is called for "from client" and "to server" |
| 1480 | * connections. The combination of write_p == 0 and content_type == 22 |
| 1481 | * is only avalaible during "from client" connection. |
| 1482 | */ |
| 1483 | |
| 1484 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1485 | * otherwise it is set to 1. |
| 1486 | */ |
| 1487 | if (write_p != 0) |
| 1488 | return; |
| 1489 | |
| 1490 | /* content_type contains the type of message received or sent |
| 1491 | * according with the SSL/TLS protocol spec. This message is |
| 1492 | * encoded with one byte. The value 256 (two bytes) is used |
| 1493 | * for designing the SSL/TLS record layer. According with the |
| 1494 | * rfc6101, the expected message (other than 256) are: |
| 1495 | * - change_cipher_spec(20) |
| 1496 | * - alert(21) |
| 1497 | * - handshake(22) |
| 1498 | * - application_data(23) |
| 1499 | * - (255) |
| 1500 | * We are interessed by the handshake and specially the client |
| 1501 | * hello. |
| 1502 | */ |
| 1503 | if (content_type != 22) |
| 1504 | return; |
| 1505 | |
| 1506 | /* The message length is at least 4 bytes, containing the |
| 1507 | * message type and the message length. |
| 1508 | */ |
| 1509 | if (len < 4) |
| 1510 | return; |
| 1511 | |
| 1512 | /* First byte of the handshake message id the type of |
| 1513 | * message. The konwn types are: |
| 1514 | * - hello_request(0) |
| 1515 | * - client_hello(1) |
| 1516 | * - server_hello(2) |
| 1517 | * - certificate(11) |
| 1518 | * - server_key_exchange (12) |
| 1519 | * - certificate_request(13) |
| 1520 | * - server_hello_done(14) |
| 1521 | * We are interested by the client hello. |
| 1522 | */ |
| 1523 | msg = (unsigned char *)buf; |
| 1524 | if (msg[0] != 1) |
| 1525 | return; |
| 1526 | |
| 1527 | /* Next three bytes are the length of the message. The total length |
| 1528 | * must be this decoded length + 4. If the length given as argument |
| 1529 | * is not the same, we abort the protocol dissector. |
| 1530 | */ |
| 1531 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1532 | if (len < rec_len + 4) |
| 1533 | return; |
| 1534 | msg += 4; |
| 1535 | end = msg + rec_len; |
| 1536 | if (end < msg) |
| 1537 | return; |
| 1538 | |
| 1539 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1540 | * for minor, the random, composed by 4 bytes for the unix time and |
| 1541 | * 28 bytes for unix payload, and them 1 byte for the session id. So |
| 1542 | * we jump 1 + 1 + 4 + 28 + 1 bytes. |
| 1543 | */ |
| 1544 | msg += 1 + 1 + 4 + 28 + 1; |
| 1545 | if (msg > end) |
| 1546 | return; |
| 1547 | |
| 1548 | /* Next two bytes are the ciphersuite length. */ |
| 1549 | if (msg + 2 > end) |
| 1550 | return; |
| 1551 | rec_len = (msg[0] << 8) + msg[1]; |
| 1552 | msg += 2; |
| 1553 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1554 | return; |
| 1555 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1556 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1557 | if (!capture) |
| 1558 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1559 | /* Compute the xxh64 of the ciphersuite. */ |
| 1560 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1561 | |
| 1562 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1563 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1564 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1565 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1566 | |
| 1567 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1568 | } |
| 1569 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1570 | /* Callback is called for ssl protocol analyse */ |
| 1571 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1572 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1573 | #ifdef TLS1_RT_HEARTBEAT |
| 1574 | /* test heartbeat received (write_p is set to 0 |
| 1575 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1576 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1577 | struct connection *conn = SSL_get_app_data(ssl); |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1578 | const unsigned char *p = buf; |
| 1579 | unsigned int payload; |
| 1580 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1581 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1582 | |
| 1583 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1584 | if (*p != TLS1_HB_REQUEST) |
| 1585 | return; |
| 1586 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1587 | 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] | 1588 | goto kill_it; |
| 1589 | |
| 1590 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1591 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1592 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1593 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1594 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1595 | * advertised payload is larger than the advertised packet |
| 1596 | * length, so we have garbage in the buffer between the |
| 1597 | * payload and the end of the buffer (p+len). We can't know |
| 1598 | * if the SSL stack is patched, and we don't know if we can |
| 1599 | * safely wipe out the area between p+3+len and payload. |
| 1600 | * So instead, we prevent the response from being sent by |
| 1601 | * setting the max_send_fragment to 0 and we report an SSL |
| 1602 | * error, which will kill this connection. It will be reported |
| 1603 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1604 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1605 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1606 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1607 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1608 | return; |
| 1609 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1610 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1611 | if (global_ssl.capture_cipherlist > 0) |
| 1612 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1613 | } |
| 1614 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1615 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1616 | /* This callback is used so that the server advertises the list of |
| 1617 | * negociable protocols for NPN. |
| 1618 | */ |
| 1619 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1620 | unsigned int *len, void *arg) |
| 1621 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1622 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1623 | |
| 1624 | *data = (const unsigned char *)conf->npn_str; |
| 1625 | *len = conf->npn_len; |
| 1626 | return SSL_TLSEXT_ERR_OK; |
| 1627 | } |
| 1628 | #endif |
| 1629 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1630 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1631 | /* This callback is used so that the server advertises the list of |
| 1632 | * negociable protocols for ALPN. |
| 1633 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1634 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1635 | unsigned char *outlen, |
| 1636 | const unsigned char *server, |
| 1637 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1638 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1639 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1640 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1641 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1642 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1643 | return SSL_TLSEXT_ERR_NOACK; |
| 1644 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1645 | return SSL_TLSEXT_ERR_OK; |
| 1646 | } |
| 1647 | #endif |
| 1648 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1649 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1650 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1651 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1652 | /* Create a X509 certificate with the specified servername and serial. This |
| 1653 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1654 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1655 | 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] | 1656 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1657 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1658 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1659 | SSL_CTX *ssl_ctx = NULL; |
| 1660 | X509 *newcrt = NULL; |
| 1661 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1662 | SSL *tmp_ssl = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1663 | X509_NAME *name; |
| 1664 | const EVP_MD *digest; |
| 1665 | X509V3_CTX ctx; |
| 1666 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1667 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1668 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1669 | /* Get the private key of the default certificate and use it */ |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1670 | #if (OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined LIBRESSL_VERSION_NUMBER) |
| 1671 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1672 | #else |
| 1673 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1674 | if (tmp_ssl) |
| 1675 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1676 | #endif |
| 1677 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1678 | goto mkcert_error; |
| 1679 | |
| 1680 | /* Create the certificate */ |
| 1681 | if (!(newcrt = X509_new())) |
| 1682 | goto mkcert_error; |
| 1683 | |
| 1684 | /* Set version number for the certificate (X509v3) and the serial |
| 1685 | * number */ |
| 1686 | if (X509_set_version(newcrt, 2L) != 1) |
| 1687 | goto mkcert_error; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1688 | 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] | 1689 | |
| 1690 | /* Set duration for the certificate */ |
| 1691 | if (!X509_gmtime_adj(X509_get_notBefore(newcrt), (long)-60*60*24) || |
| 1692 | !X509_gmtime_adj(X509_get_notAfter(newcrt),(long)60*60*24*365)) |
| 1693 | goto mkcert_error; |
| 1694 | |
| 1695 | /* set public key in the certificate */ |
| 1696 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1697 | goto mkcert_error; |
| 1698 | |
| 1699 | /* Set issuer name from the CA */ |
| 1700 | if (!(name = X509_get_subject_name(cacert))) |
| 1701 | goto mkcert_error; |
| 1702 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1703 | goto mkcert_error; |
| 1704 | |
| 1705 | /* Set the subject name using the same, but the CN */ |
| 1706 | name = X509_NAME_dup(name); |
| 1707 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1708 | (const unsigned char *)servername, |
| 1709 | -1, -1, 0) != 1) { |
| 1710 | X509_NAME_free(name); |
| 1711 | goto mkcert_error; |
| 1712 | } |
| 1713 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1714 | X509_NAME_free(name); |
| 1715 | goto mkcert_error; |
| 1716 | } |
| 1717 | X509_NAME_free(name); |
| 1718 | |
| 1719 | /* Add x509v3 extensions as specified */ |
| 1720 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1721 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1722 | X509_EXTENSION *ext; |
| 1723 | |
| 1724 | if (!(ext = X509V3_EXT_conf(NULL, &ctx, x509v3_ext_names[i], x509v3_ext_values[i]))) |
| 1725 | goto mkcert_error; |
| 1726 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1727 | X509_EXTENSION_free(ext); |
| 1728 | goto mkcert_error; |
| 1729 | } |
| 1730 | X509_EXTENSION_free(ext); |
| 1731 | } |
| 1732 | |
| 1733 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1734 | |
| 1735 | key_type = EVP_PKEY_base_id(capkey); |
| 1736 | |
| 1737 | if (key_type == EVP_PKEY_DSA) |
| 1738 | digest = EVP_sha1(); |
| 1739 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1740 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1741 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1742 | digest = EVP_sha256(); |
| 1743 | else { |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1744 | #if (OPENSSL_VERSION_NUMBER >= 0x1000000fL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1745 | int nid; |
| 1746 | |
| 1747 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1748 | goto mkcert_error; |
| 1749 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1750 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1751 | #else |
| 1752 | goto mkcert_error; |
| 1753 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1754 | } |
| 1755 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1756 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1757 | goto mkcert_error; |
| 1758 | |
| 1759 | /* Create and set the new SSL_CTX */ |
| 1760 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1761 | goto mkcert_error; |
| 1762 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1763 | goto mkcert_error; |
| 1764 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1765 | goto mkcert_error; |
| 1766 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1767 | goto mkcert_error; |
| 1768 | |
| 1769 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1770 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1771 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1772 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1773 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1774 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1775 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1776 | 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] | 1777 | EC_KEY *ecc; |
| 1778 | int nid; |
| 1779 | |
| 1780 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1781 | goto end; |
| 1782 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1783 | goto end; |
| 1784 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1785 | EC_KEY_free(ecc); |
| 1786 | } |
| 1787 | #endif |
| 1788 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1789 | return ssl_ctx; |
| 1790 | |
| 1791 | mkcert_error: |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1792 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1793 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1794 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1795 | return NULL; |
| 1796 | } |
| 1797 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1798 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1799 | 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] | 1800 | { |
| 1801 | struct bind_conf *bind_conf = objt_listener(conn->target)->bind_conf; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1802 | |
| 1803 | return ssl_sock_do_create_cert(servername, bind_conf, conn->xprt_ctx); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1804 | } |
| 1805 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1806 | /* 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] | 1807 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1808 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1809 | 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] | 1810 | { |
| 1811 | struct lru64 *lru = NULL; |
| 1812 | |
| 1813 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1814 | HA_RWLOCK_RDLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1815 | 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] | 1816 | if (lru && lru->domain) { |
| 1817 | if (ssl) |
| 1818 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1819 | HA_RWLOCK_RDUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1820 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1821 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1822 | HA_RWLOCK_RDUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1823 | } |
| 1824 | return NULL; |
| 1825 | } |
| 1826 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1827 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 1828 | * function is not thread-safe, it should only be used to check if a certificate |
| 1829 | * exists in the lru cache (with no warranty it will not be removed by another |
| 1830 | * thread). It is kept for backward compatibility. */ |
| 1831 | SSL_CTX * |
| 1832 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 1833 | { |
| 1834 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 1835 | } |
| 1836 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1837 | /* Set a certificate int the LRU cache used to store generated |
| 1838 | * certificate. Return 0 on success, otherwise -1 */ |
| 1839 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1840 | 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] | 1841 | { |
| 1842 | struct lru64 *lru = NULL; |
| 1843 | |
| 1844 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1845 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1846 | 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] | 1847 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1848 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1849 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1850 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1851 | if (lru->domain && lru->data) |
| 1852 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1853 | 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] | 1854 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1855 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1856 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1857 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1858 | } |
| 1859 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1860 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1861 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1862 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1863 | { |
| 1864 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 1865 | } |
| 1866 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1867 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 1868 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 1869 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1870 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1871 | 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] | 1872 | { |
| 1873 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1874 | SSL_CTX *ssl_ctx = NULL; |
| 1875 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1876 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1877 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1878 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1879 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1880 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1881 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1882 | if (lru && lru->domain) |
| 1883 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1884 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1885 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1886 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1887 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1888 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1889 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1890 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1891 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1892 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1893 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1894 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 1895 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 1896 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1897 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1898 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1899 | return 0; |
| 1900 | } |
| 1901 | static int |
| 1902 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 1903 | { |
| 1904 | unsigned int key; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1905 | struct connection *conn = SSL_get_app_data(ssl); |
| 1906 | |
| 1907 | conn_get_to_addr(conn); |
| 1908 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
| 1909 | 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] | 1910 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1911 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1912 | } |
| 1913 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1914 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1915 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1916 | |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1917 | |
| 1918 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 1919 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 1920 | #endif |
| 1921 | |
| 1922 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 1923 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
| 1924 | #define SSL_renegotiate_pending(arg) 0 |
| 1925 | #endif |
| 1926 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 1927 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1928 | #endif |
| 1929 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 1930 | #define SSL_OP_NO_TICKET 0 |
| 1931 | #endif |
| 1932 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 1933 | #define SSL_OP_NO_COMPRESSION 0 |
| 1934 | #endif |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 1935 | #ifdef OPENSSL_NO_SSL3 /* SSLv3 support removed */ |
| 1936 | #undef SSL_OP_NO_SSLv3 |
| 1937 | #define SSL_OP_NO_SSLv3 0 |
| 1938 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1939 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 1940 | #define SSL_OP_NO_TLSv1_1 0 |
| 1941 | #endif |
| 1942 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 1943 | #define SSL_OP_NO_TLSv1_2 0 |
| 1944 | #endif |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 1945 | #ifndef SSL_OP_NO_TLSv1_3 /* needs OpenSSL >= 1.1.1 */ |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1946 | #define SSL_OP_NO_TLSv1_3 0 |
| 1947 | #endif |
| 1948 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 1949 | #define SSL_OP_SINGLE_DH_USE 0 |
| 1950 | #endif |
| 1951 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 1952 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1953 | #endif |
| 1954 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 1955 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 1956 | #endif |
| 1957 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 1958 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 1959 | #endif |
| 1960 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 1961 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1962 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 1963 | |
| 1964 | 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] | 1965 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 1966 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1967 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1968 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 1969 | #endif |
| 1970 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1971 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 1972 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1973 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 1974 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1975 | 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] | 1976 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1977 | 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] | 1978 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 1979 | #endif |
| 1980 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1981 | 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] | 1982 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1983 | 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] | 1984 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 1985 | #endif |
| 1986 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1987 | /* TLS 1.2 is the last supported version in this context. */ |
| 1988 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 1989 | /* Unusable in this context. */ |
| 1990 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 1991 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 1992 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 1993 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 1994 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1995 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1996 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 1997 | |
| 1998 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 1999 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2000 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2001 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2002 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2003 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2004 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2005 | } |
| 2006 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2007 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2008 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2009 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2010 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2011 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2012 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2013 | } |
| 2014 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2015 | 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] | 2016 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2017 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2018 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2019 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2020 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2021 | } |
| 2022 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2023 | 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] | 2024 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2025 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2026 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2027 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2028 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2029 | } |
| 2030 | 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] | 2031 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2032 | 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] | 2033 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2034 | #endif |
| 2035 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2036 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2037 | #if SSL_OP_NO_TLSv1_3 |
| 2038 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2039 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2040 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2041 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2042 | #endif |
| 2043 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2044 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2045 | |
| 2046 | static struct { |
| 2047 | int option; |
| 2048 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2049 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 2050 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2051 | const char *name; |
| 2052 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2053 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2054 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2055 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2056 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2057 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2058 | {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] | 2059 | }; |
| 2060 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2061 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2062 | { |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 2063 | struct pkey_info *pkinfo; |
| 2064 | |
| 2065 | pkinfo = SSL_CTX_get_ex_data(ctx, ssl_pkey_info_index); |
| 2066 | if (pkinfo) |
| 2067 | SSL_set_ex_data(ssl, ssl_pkey_info_index, pkinfo); |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2068 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2069 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2070 | SSL_set_SSL_CTX(ssl, ctx); |
| 2071 | } |
| 2072 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2073 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2074 | |
| 2075 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2076 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2077 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2078 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2079 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2080 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2081 | return SSL_TLSEXT_ERR_OK; |
| 2082 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2083 | } |
| 2084 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2085 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2086 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2087 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2088 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2089 | #else |
| 2090 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2091 | { |
| 2092 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2093 | struct connection *conn; |
| 2094 | struct bind_conf *s; |
| 2095 | const uint8_t *extension_data; |
| 2096 | size_t extension_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2097 | int has_rsa = 0, has_ecdsa = 0, has_ecdsa_sig = 0; |
| 2098 | |
| 2099 | char *wildp = NULL; |
| 2100 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2101 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2102 | 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] | 2103 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2104 | int i; |
| 2105 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2106 | conn = SSL_get_app_data(ssl); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2107 | s = objt_listener(conn->target)->bind_conf; |
| 2108 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2109 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2110 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2111 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2112 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2113 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2114 | #else |
| 2115 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2116 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2117 | /* |
| 2118 | * The server_name extension was given too much extensibility when it |
| 2119 | * was written, so parsing the normal case is a bit complex. |
| 2120 | */ |
| 2121 | size_t len; |
| 2122 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2123 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2124 | /* Extract the length of the supplied list of names. */ |
| 2125 | len = (*extension_data++) << 8; |
| 2126 | len |= *extension_data++; |
| 2127 | if (len + 2 != extension_len) |
| 2128 | goto abort; |
| 2129 | /* |
| 2130 | * The list in practice only has a single element, so we only consider |
| 2131 | * the first one. |
| 2132 | */ |
| 2133 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2134 | goto abort; |
| 2135 | extension_len = len - 1; |
| 2136 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2137 | if (extension_len <= 2) |
| 2138 | goto abort; |
| 2139 | len = (*extension_data++) << 8; |
| 2140 | len |= *extension_data++; |
| 2141 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2142 | || memchr(extension_data, 0, len) != NULL) |
| 2143 | goto abort; |
| 2144 | servername = extension_data; |
| 2145 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2146 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2147 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2148 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2149 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2150 | } |
| 2151 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2152 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2153 | if (!s->strict_sni) { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2154 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2155 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2156 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2157 | goto abort; |
| 2158 | } |
| 2159 | |
| 2160 | /* extract/check clientHello informations */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2161 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2162 | 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] | 2163 | #else |
| 2164 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2165 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2166 | uint8_t sign; |
| 2167 | size_t len; |
| 2168 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2169 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2170 | len = (*extension_data++) << 8; |
| 2171 | len |= *extension_data++; |
| 2172 | if (len + 2 != extension_len) |
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 | if (len % 2 != 0) |
| 2175 | goto abort; |
| 2176 | for (; len > 0; len -= 2) { |
| 2177 | extension_data++; /* hash */ |
| 2178 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2179 | switch (sign) { |
| 2180 | case TLSEXT_signature_rsa: |
| 2181 | has_rsa = 1; |
| 2182 | break; |
| 2183 | case TLSEXT_signature_ecdsa: |
| 2184 | has_ecdsa_sig = 1; |
| 2185 | break; |
| 2186 | default: |
| 2187 | continue; |
| 2188 | } |
| 2189 | if (has_ecdsa_sig && has_rsa) |
| 2190 | break; |
| 2191 | } |
| 2192 | } else { |
| 2193 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLS 1.2) */ |
| 2194 | has_rsa = 1; |
| 2195 | } |
| 2196 | 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] | 2197 | const SSL_CIPHER *cipher; |
| 2198 | size_t len; |
| 2199 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2200 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2201 | len = ctx->cipher_suites_len; |
| 2202 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2203 | #else |
| 2204 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2205 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2206 | if (len % 2 != 0) |
| 2207 | goto abort; |
| 2208 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2209 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2210 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2211 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2212 | #else |
| 2213 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2214 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2215 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2216 | has_ecdsa = 1; |
| 2217 | break; |
| 2218 | } |
| 2219 | } |
| 2220 | } |
| 2221 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2222 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2223 | trash.str[i] = tolower(servername[i]); |
| 2224 | if (!wildp && (trash.str[i] == '.')) |
| 2225 | wildp = &trash.str[i]; |
| 2226 | } |
| 2227 | trash.str[i] = 0; |
| 2228 | |
| 2229 | /* lookup in full qualified names */ |
| 2230 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 2231 | |
| 2232 | /* lookup a not neg filter */ |
| 2233 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2234 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2235 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2236 | case TLSEXT_signature_ecdsa: |
| 2237 | if (has_ecdsa) { |
| 2238 | node_ecdsa = n; |
| 2239 | goto find_one; |
| 2240 | } |
| 2241 | break; |
| 2242 | case TLSEXT_signature_rsa: |
| 2243 | if (has_rsa && !node_rsa) { |
| 2244 | node_rsa = n; |
| 2245 | if (!has_ecdsa) |
| 2246 | goto find_one; |
| 2247 | } |
| 2248 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2249 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2250 | if (!node_anonymous) |
| 2251 | node_anonymous = n; |
| 2252 | break; |
| 2253 | } |
| 2254 | } |
| 2255 | } |
| 2256 | if (wildp) { |
| 2257 | /* lookup in wildcards names */ |
| 2258 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2259 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2260 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2261 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2262 | case TLSEXT_signature_ecdsa: |
| 2263 | if (has_ecdsa) { |
| 2264 | node_ecdsa = n; |
| 2265 | goto find_one; |
| 2266 | } |
| 2267 | break; |
| 2268 | case TLSEXT_signature_rsa: |
| 2269 | if (has_rsa && !node_rsa) { |
| 2270 | node_rsa = n; |
| 2271 | if (!has_ecdsa) |
| 2272 | goto find_one; |
| 2273 | } |
| 2274 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2275 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2276 | if (!node_anonymous) |
| 2277 | node_anonymous = n; |
| 2278 | break; |
| 2279 | } |
| 2280 | } |
| 2281 | } |
| 2282 | } |
| 2283 | find_one: |
| 2284 | /* select by key_signature priority order */ |
| 2285 | node = node_ecdsa ? node_ecdsa : (node_rsa ? node_rsa : node_anonymous); |
| 2286 | |
| 2287 | if (node) { |
| 2288 | /* switch ctx */ |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 2289 | 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] | 2290 | 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] | 2291 | if (conf) { |
| 2292 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2293 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2294 | if (conf->early_data) |
| 2295 | allow_early = 1; |
| 2296 | } |
| 2297 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2298 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2299 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2300 | if (s->generate_certs && ssl_sock_generate_certificate(trash.str, s, ssl)) { |
| 2301 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2302 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2303 | } |
| 2304 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2305 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2306 | /* no certificate match, is the default_ctx */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2307 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2308 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2309 | allow_early: |
| 2310 | #ifdef OPENSSL_IS_BORINGSSL |
| 2311 | if (allow_early) |
| 2312 | SSL_set_early_data_enabled(ssl, 1); |
| 2313 | #else |
| 2314 | if (!allow_early) |
| 2315 | SSL_set_max_early_data(ssl, 0); |
| 2316 | #endif |
| 2317 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2318 | abort: |
| 2319 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2320 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2321 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2322 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2323 | #else |
| 2324 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2325 | return 0; |
| 2326 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2330 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2331 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2332 | * warning when no match is found, which implies the default (first) cert |
| 2333 | * will keep being used. |
| 2334 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2335 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2336 | { |
| 2337 | const char *servername; |
| 2338 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2339 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2340 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2341 | int i; |
| 2342 | (void)al; /* shut gcc stupid warning */ |
| 2343 | |
| 2344 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2345 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2346 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2347 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2348 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2349 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2350 | if (s->strict_sni) |
| 2351 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2352 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2353 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2354 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2355 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2356 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2357 | if (!servername[i]) |
| 2358 | break; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2359 | trash.str[i] = tolower(servername[i]); |
| 2360 | if (!wildp && (trash.str[i] == '.')) |
| 2361 | wildp = &trash.str[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2362 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2363 | trash.str[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2364 | |
| 2365 | /* lookup in full qualified names */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2366 | node = ebst_lookup(&s->sni_ctx, trash.str); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2367 | |
| 2368 | /* lookup a not neg filter */ |
| 2369 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2370 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2371 | node = n; |
| 2372 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2373 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2374 | } |
| 2375 | if (!node && wildp) { |
| 2376 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2377 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2378 | } |
| 2379 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2380 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2381 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2382 | /* switch ctx done in ssl_sock_generate_certificate */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2383 | return SSL_TLSEXT_ERR_OK; |
| 2384 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2385 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2386 | if (s->strict_sni) |
| 2387 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2388 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2389 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2393 | 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] | 2394 | return SSL_TLSEXT_ERR_OK; |
| 2395 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2396 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2397 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2398 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2399 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2400 | |
| 2401 | static DH * ssl_get_dh_1024(void) |
| 2402 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2403 | static unsigned char dh1024_p[]={ |
| 2404 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2405 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2406 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2407 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2408 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2409 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2410 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2411 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2412 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2413 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2414 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2415 | }; |
| 2416 | static unsigned char dh1024_g[]={ |
| 2417 | 0x02, |
| 2418 | }; |
| 2419 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2420 | BIGNUM *p; |
| 2421 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2422 | DH *dh = DH_new(); |
| 2423 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2424 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2425 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2426 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2427 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2428 | DH_free(dh); |
| 2429 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2430 | } else { |
| 2431 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2432 | } |
| 2433 | } |
| 2434 | return dh; |
| 2435 | } |
| 2436 | |
| 2437 | static DH *ssl_get_dh_2048(void) |
| 2438 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2439 | static unsigned char dh2048_p[]={ |
| 2440 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2441 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2442 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2443 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2444 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2445 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2446 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2447 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2448 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2449 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2450 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2451 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2452 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2453 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2454 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2455 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2456 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2457 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2458 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2459 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2460 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2461 | 0xB7,0x1F,0x77,0xF3, |
| 2462 | }; |
| 2463 | static unsigned char dh2048_g[]={ |
| 2464 | 0x02, |
| 2465 | }; |
| 2466 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2467 | BIGNUM *p; |
| 2468 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2469 | DH *dh = DH_new(); |
| 2470 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2471 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2472 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2473 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2474 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2475 | DH_free(dh); |
| 2476 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2477 | } else { |
| 2478 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2479 | } |
| 2480 | } |
| 2481 | return dh; |
| 2482 | } |
| 2483 | |
| 2484 | static DH *ssl_get_dh_4096(void) |
| 2485 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2486 | static unsigned char dh4096_p[]={ |
| 2487 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2488 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2489 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2490 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2491 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2492 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2493 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2494 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2495 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2496 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2497 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2498 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2499 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2500 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2501 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2502 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2503 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2504 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2505 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2506 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2507 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2508 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2509 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2510 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2511 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2512 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2513 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2514 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2515 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2516 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2517 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2518 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2519 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2520 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2521 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2522 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2523 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2524 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2525 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2526 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2527 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2528 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2529 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2530 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2531 | static unsigned char dh4096_g[]={ |
| 2532 | 0x02, |
| 2533 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2534 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2535 | BIGNUM *p; |
| 2536 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2537 | DH *dh = DH_new(); |
| 2538 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2539 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2540 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2541 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2542 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2543 | DH_free(dh); |
| 2544 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2545 | } else { |
| 2546 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2547 | } |
| 2548 | } |
| 2549 | return dh; |
| 2550 | } |
| 2551 | |
| 2552 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2553 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2554 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2555 | { |
| 2556 | DH *dh = NULL; |
| 2557 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2558 | int type; |
| 2559 | |
| 2560 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2561 | |
| 2562 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2563 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2564 | */ |
| 2565 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2566 | keylen = EVP_PKEY_bits(pkey); |
| 2567 | } |
| 2568 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2569 | if (keylen > global_ssl.default_dh_param) { |
| 2570 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2571 | } |
| 2572 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2573 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2574 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2575 | } |
| 2576 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2577 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2578 | } |
| 2579 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2580 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | return dh; |
| 2584 | } |
| 2585 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2586 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2587 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2588 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2589 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2590 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2591 | if (in == NULL) |
| 2592 | goto end; |
| 2593 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2594 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2595 | goto end; |
| 2596 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2597 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2598 | |
| 2599 | end: |
| 2600 | if (in) |
| 2601 | BIO_free(in); |
| 2602 | |
| 2603 | return dh; |
| 2604 | } |
| 2605 | |
| 2606 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2607 | { |
| 2608 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2609 | |
| 2610 | if (global_dh) { |
| 2611 | return 0; |
| 2612 | } |
| 2613 | |
| 2614 | return -1; |
| 2615 | } |
| 2616 | |
| 2617 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 2618 | if an error occured, and 0 if parameter not found. */ |
| 2619 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 2620 | { |
| 2621 | int ret = -1; |
| 2622 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 2623 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2624 | if (dh) { |
| 2625 | ret = 1; |
| 2626 | SSL_CTX_set_tmp_dh(ctx, dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2627 | |
| 2628 | if (ssl_dh_ptr_index >= 0) { |
| 2629 | /* store a pointer to the DH params to avoid complaining about |
| 2630 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2631 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2632 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2633 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2634 | else if (global_dh) { |
| 2635 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 2636 | ret = 0; /* DH params not found */ |
| 2637 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2638 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2639 | /* Clear openssl global errors stack */ |
| 2640 | ERR_clear_error(); |
| 2641 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2642 | if (global_ssl.default_dh_param <= 1024) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2643 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 2644 | if (local_dh_1024 == NULL) |
| 2645 | local_dh_1024 = ssl_get_dh_1024(); |
| 2646 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2647 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2648 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2649 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2650 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2651 | } |
| 2652 | else { |
| 2653 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2654 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2655 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2656 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2657 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2658 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2659 | end: |
| 2660 | if (dh) |
| 2661 | DH_free(dh); |
| 2662 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2663 | return ret; |
| 2664 | } |
| 2665 | #endif |
| 2666 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2667 | 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] | 2668 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2669 | { |
| 2670 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2671 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2672 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2673 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2674 | if (*name == '!') { |
| 2675 | neg = 1; |
| 2676 | name++; |
| 2677 | } |
| 2678 | if (*name == '*') { |
| 2679 | wild = 1; |
| 2680 | name++; |
| 2681 | } |
| 2682 | /* !* filter is a nop */ |
| 2683 | if (neg && wild) |
| 2684 | return order; |
| 2685 | if (*name) { |
| 2686 | int j, len; |
| 2687 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2688 | for (j = 0; j < len && j < trash.size; j++) |
| 2689 | trash.str[j] = tolower(name[j]); |
| 2690 | if (j >= trash.size) |
| 2691 | return order; |
| 2692 | trash.str[j] = 0; |
| 2693 | |
| 2694 | /* Check for duplicates. */ |
| 2695 | if (wild) |
| 2696 | node = ebst_lookup(&s->sni_w_ctx, trash.str); |
| 2697 | else |
| 2698 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 2699 | for (; node; node = ebmb_next_dup(node)) { |
| 2700 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2701 | if (sc->ctx == ctx && sc->conf == conf && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2702 | return order; |
| 2703 | } |
| 2704 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2705 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2706 | if (!sc) |
| 2707 | return order; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2708 | memcpy(sc->name.key, trash.str, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2709 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2710 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2711 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2712 | sc->order = order++; |
| 2713 | sc->neg = neg; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 2714 | if (kinfo.sig != TLSEXT_signature_anonymous) |
| 2715 | SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2716 | if (wild) |
| 2717 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 2718 | else |
| 2719 | ebst_insert(&s->sni_ctx, &sc->name); |
| 2720 | } |
| 2721 | return order; |
| 2722 | } |
| 2723 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2724 | |
| 2725 | /* The following code is used for loading multiple crt files into |
| 2726 | * SSL_CTX's based on CN/SAN |
| 2727 | */ |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 2728 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(LIBRESSL_VERSION_NUMBER) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2729 | /* This is used to preload the certifcate, private key |
| 2730 | * and Cert Chain of a file passed in via the crt |
| 2731 | * argument |
| 2732 | * |
| 2733 | * This way, we do not have to read the file multiple times |
| 2734 | */ |
| 2735 | struct cert_key_and_chain { |
| 2736 | X509 *cert; |
| 2737 | EVP_PKEY *key; |
| 2738 | unsigned int num_chain_certs; |
| 2739 | /* This is an array of X509 pointers */ |
| 2740 | X509 **chain_certs; |
| 2741 | }; |
| 2742 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2743 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 2744 | |
| 2745 | struct key_combo_ctx { |
| 2746 | SSL_CTX *ctx; |
| 2747 | int order; |
| 2748 | }; |
| 2749 | |
| 2750 | /* Map used for processing multiple keypairs for a single purpose |
| 2751 | * |
| 2752 | * This maps CN/SNI name to certificate type |
| 2753 | */ |
| 2754 | struct sni_keytype { |
| 2755 | int keytypes; /* BITMASK for keytypes */ |
| 2756 | struct ebmb_node name; /* node holding the servername value */ |
| 2757 | }; |
| 2758 | |
| 2759 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2760 | /* Frees the contents of a cert_key_and_chain |
| 2761 | */ |
| 2762 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2763 | { |
| 2764 | int i; |
| 2765 | |
| 2766 | if (!ckch) |
| 2767 | return; |
| 2768 | |
| 2769 | /* Free the certificate and set pointer to NULL */ |
| 2770 | if (ckch->cert) |
| 2771 | X509_free(ckch->cert); |
| 2772 | ckch->cert = NULL; |
| 2773 | |
| 2774 | /* Free the key and set pointer to NULL */ |
| 2775 | if (ckch->key) |
| 2776 | EVP_PKEY_free(ckch->key); |
| 2777 | ckch->key = NULL; |
| 2778 | |
| 2779 | /* Free each certificate in the chain */ |
| 2780 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2781 | if (ckch->chain_certs[i]) |
| 2782 | X509_free(ckch->chain_certs[i]); |
| 2783 | } |
| 2784 | |
| 2785 | /* Free the chain obj itself and set to NULL */ |
| 2786 | if (ckch->num_chain_certs > 0) { |
| 2787 | free(ckch->chain_certs); |
| 2788 | ckch->num_chain_certs = 0; |
| 2789 | ckch->chain_certs = NULL; |
| 2790 | } |
| 2791 | |
| 2792 | } |
| 2793 | |
| 2794 | /* checks if a key and cert exists in the ckch |
| 2795 | */ |
| 2796 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 2797 | { |
| 2798 | return (ckch->cert != NULL && ckch->key != NULL); |
| 2799 | } |
| 2800 | |
| 2801 | |
| 2802 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 2803 | * This allows us to carry the contents of the file without having to |
| 2804 | * read the file multiple times. |
| 2805 | * |
| 2806 | * returns: |
| 2807 | * 0 on Success |
| 2808 | * 1 on SSL Failure |
| 2809 | * 2 on file not found |
| 2810 | */ |
| 2811 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 2812 | { |
| 2813 | |
| 2814 | BIO *in; |
| 2815 | X509 *ca = NULL; |
| 2816 | int ret = 1; |
| 2817 | |
| 2818 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2819 | |
| 2820 | in = BIO_new(BIO_s_file()); |
| 2821 | if (in == NULL) |
| 2822 | goto end; |
| 2823 | |
| 2824 | if (BIO_read_filename(in, path) <= 0) |
| 2825 | goto end; |
| 2826 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2827 | /* Read Private Key */ |
| 2828 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 2829 | if (ckch->key == NULL) { |
| 2830 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 2831 | err && *err ? *err : "", path); |
| 2832 | goto end; |
| 2833 | } |
| 2834 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2835 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 2836 | if (BIO_reset(in) == -1) { |
| 2837 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 2838 | err && *err ? *err : "", path); |
| 2839 | goto end; |
| 2840 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2841 | |
| 2842 | /* Read Certificate */ |
| 2843 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 2844 | if (ckch->cert == NULL) { |
| 2845 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 2846 | err && *err ? *err : "", path); |
| 2847 | goto end; |
| 2848 | } |
| 2849 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2850 | /* Read Certificate Chain */ |
| 2851 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 2852 | /* Grow the chain certs */ |
| 2853 | ckch->num_chain_certs++; |
| 2854 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 2855 | |
| 2856 | /* use - 1 here since we just incremented it above */ |
| 2857 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 2858 | } |
| 2859 | ret = ERR_get_error(); |
| 2860 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 2861 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 2862 | err && *err ? *err : "", path); |
| 2863 | ret = 1; |
| 2864 | goto end; |
| 2865 | } |
| 2866 | |
| 2867 | ret = 0; |
| 2868 | |
| 2869 | end: |
| 2870 | |
| 2871 | ERR_clear_error(); |
| 2872 | if (in) |
| 2873 | BIO_free(in); |
| 2874 | |
| 2875 | /* Something went wrong in one of the reads */ |
| 2876 | if (ret != 0) |
| 2877 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2878 | |
| 2879 | return ret; |
| 2880 | } |
| 2881 | |
| 2882 | /* Loads the info in ckch into ctx |
| 2883 | * Currently, this does not process any information about ocsp, dhparams or |
| 2884 | * sctl |
| 2885 | * Returns |
| 2886 | * 0 on success |
| 2887 | * 1 on failure |
| 2888 | */ |
| 2889 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2890 | { |
| 2891 | int i = 0; |
| 2892 | |
| 2893 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2894 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2895 | err && *err ? *err : "", path); |
| 2896 | return 1; |
| 2897 | } |
| 2898 | |
| 2899 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 2900 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 2901 | err && *err ? *err : "", path); |
| 2902 | return 1; |
| 2903 | } |
| 2904 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2905 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 2906 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2907 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2908 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 2909 | err && *err ? *err : "", (i+1), path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2910 | return 1; |
| 2911 | } |
| 2912 | } |
| 2913 | |
| 2914 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2915 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2916 | err && *err ? *err : "", path); |
| 2917 | return 1; |
| 2918 | } |
| 2919 | |
| 2920 | return 0; |
| 2921 | } |
| 2922 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2923 | |
| 2924 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 2925 | { |
| 2926 | struct sni_keytype *s_kt = NULL; |
| 2927 | struct ebmb_node *node; |
| 2928 | int i; |
| 2929 | |
| 2930 | for (i = 0; i < trash.size; i++) { |
| 2931 | if (!str[i]) |
| 2932 | break; |
| 2933 | trash.str[i] = tolower(str[i]); |
| 2934 | } |
| 2935 | trash.str[i] = 0; |
| 2936 | node = ebst_lookup(sni_keytypes, trash.str); |
| 2937 | if (!node) { |
| 2938 | /* CN not found in tree */ |
| 2939 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 2940 | /* Using memcpy here instead of strncpy. |
| 2941 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 2942 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 2943 | */ |
| 2944 | memcpy(s_kt->name.key, trash.str, i+1); |
| 2945 | s_kt->keytypes = 0; |
| 2946 | ebst_insert(sni_keytypes, &s_kt->name); |
| 2947 | } else { |
| 2948 | /* CN found in tree */ |
| 2949 | s_kt = container_of(node, struct sni_keytype, name); |
| 2950 | } |
| 2951 | |
| 2952 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 2953 | s_kt->keytypes |= 1<<key_index; |
| 2954 | |
| 2955 | } |
| 2956 | |
| 2957 | |
| 2958 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 2959 | * If any are found, group these files into a set of SSL_CTX* |
| 2960 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 2961 | * |
| 2962 | * This will allow the user to explictly group multiple cert/keys for a single purpose |
| 2963 | * |
| 2964 | * Returns |
| 2965 | * 0 on success |
| 2966 | * 1 on failure |
| 2967 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2968 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2969 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2970 | { |
| 2971 | char fp[MAXPATHLEN+1] = {0}; |
| 2972 | int n = 0; |
| 2973 | int i = 0; |
| 2974 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 2975 | struct eb_root sni_keytypes_map = { {0} }; |
| 2976 | struct ebmb_node *node; |
| 2977 | struct ebmb_node *next; |
| 2978 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 2979 | * of keytypes |
| 2980 | */ |
| 2981 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 2982 | int rv = 0; |
| 2983 | X509_NAME *xname = NULL; |
| 2984 | char *str = NULL; |
| 2985 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2986 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 2987 | #endif |
| 2988 | |
| 2989 | /* Load all possible certs and keys */ |
| 2990 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2991 | struct stat buf; |
| 2992 | |
| 2993 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2994 | if (stat(fp, &buf) == 0) { |
| 2995 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
| 2996 | rv = 1; |
| 2997 | goto end; |
| 2998 | } |
| 2999 | } |
| 3000 | } |
| 3001 | |
| 3002 | /* Process each ckch and update keytypes for each CN/SAN |
| 3003 | * for example, if CN/SAN www.a.com is associated with |
| 3004 | * certs with keytype 0 and 2, then at the end of the loop, |
| 3005 | * www.a.com will have: |
| 3006 | * keyindex = 0 | 1 | 4 = 5 |
| 3007 | */ |
| 3008 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3009 | |
| 3010 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 3011 | continue; |
| 3012 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3013 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 3014 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3015 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 3016 | } else { |
| 3017 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 3018 | * so the line that contains logic is marked via comments |
| 3019 | */ |
| 3020 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 3021 | i = -1; |
| 3022 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3023 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3024 | ASN1_STRING *value; |
| 3025 | value = X509_NAME_ENTRY_get_data(entry); |
| 3026 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3027 | /* Important line is here */ |
| 3028 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3029 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3030 | OPENSSL_free(str); |
| 3031 | str = NULL; |
| 3032 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3033 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3034 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3035 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3036 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3037 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 3038 | if (names) { |
| 3039 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3040 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3041 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3042 | if (name->type == GEN_DNS) { |
| 3043 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 3044 | /* Important line is here */ |
| 3045 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3046 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3047 | OPENSSL_free(str); |
| 3048 | str = NULL; |
| 3049 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3050 | } |
| 3051 | } |
| 3052 | } |
| 3053 | } |
| 3054 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 3055 | } |
| 3056 | |
| 3057 | /* If no files found, return error */ |
| 3058 | if (eb_is_empty(&sni_keytypes_map)) { |
| 3059 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3060 | err && *err ? *err : "", path); |
| 3061 | rv = 1; |
| 3062 | goto end; |
| 3063 | } |
| 3064 | |
| 3065 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 3066 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 3067 | * and add each CTX to the SNI tree |
| 3068 | * |
| 3069 | * Some math here: |
| 3070 | * There are 2^n - 1 possibile combinations, each unique |
| 3071 | * combination is denoted by the key in the map. Each key |
| 3072 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 3073 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 3074 | * entry in the array to correspond to the unique combo (key) |
| 3075 | * associated with i. This unique key combo (i) will be associated |
| 3076 | * with combos[i-1] |
| 3077 | */ |
| 3078 | |
| 3079 | node = ebmb_first(&sni_keytypes_map); |
| 3080 | while (node) { |
| 3081 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3082 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3083 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3084 | |
| 3085 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 3086 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 3087 | cur_ctx = key_combos[i-1].ctx; |
| 3088 | |
| 3089 | if (cur_ctx == NULL) { |
| 3090 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3091 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3092 | if (cur_ctx == NULL) { |
| 3093 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3094 | err && *err ? *err : ""); |
| 3095 | rv = 1; |
| 3096 | goto end; |
| 3097 | } |
| 3098 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3099 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3100 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3101 | if (i & (1<<n)) { |
| 3102 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3103 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3104 | 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] | 3105 | SSL_CTX_free(cur_ctx); |
| 3106 | rv = 1; |
| 3107 | goto end; |
| 3108 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3109 | |
| 3110 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 3111 | /* Load OCSP Info into context */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3112 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3113 | if (err) |
| 3114 | 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] | 3115 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3116 | SSL_CTX_free(cur_ctx); |
| 3117 | rv = 1; |
| 3118 | goto end; |
| 3119 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3120 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3121 | ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3122 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | /* Load DH params into the ctx to support DHE keys */ |
| 3127 | #ifndef OPENSSL_NO_DH |
| 3128 | if (ssl_dh_ptr_index >= 0) |
| 3129 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 3130 | |
| 3131 | rv = ssl_sock_load_dh_params(cur_ctx, NULL); |
| 3132 | if (rv < 0) { |
| 3133 | if (err) |
| 3134 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3135 | *err ? *err : "", path); |
| 3136 | rv = 1; |
| 3137 | goto end; |
| 3138 | } |
| 3139 | #endif |
| 3140 | |
| 3141 | /* Update key_combos */ |
| 3142 | key_combos[i-1].ctx = cur_ctx; |
| 3143 | } |
| 3144 | |
| 3145 | /* Update SNI Tree */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3146 | 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] | 3147 | kinfo, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3148 | node = ebmb_next(node); |
| 3149 | } |
| 3150 | |
| 3151 | |
| 3152 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 3153 | if (!bind_conf->default_ctx) { |
| 3154 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 3155 | if (key_combos[i].ctx) { |
| 3156 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3157 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3158 | break; |
| 3159 | } |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | end: |
| 3164 | |
| 3165 | if (names) |
| 3166 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 3167 | |
| 3168 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 3169 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 3170 | |
| 3171 | node = ebmb_first(&sni_keytypes_map); |
| 3172 | while (node) { |
| 3173 | next = ebmb_next(node); |
| 3174 | ebmb_delete(node); |
| 3175 | node = next; |
| 3176 | } |
| 3177 | |
| 3178 | return rv; |
| 3179 | } |
| 3180 | #else |
| 3181 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3182 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3183 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3184 | { |
| 3185 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3186 | err && *err ? *err : "", path, strerror(errno)); |
| 3187 | return 1; |
| 3188 | } |
| 3189 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3190 | #endif /* #if OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
| 3191 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3192 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 3193 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 3194 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3195 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, |
| 3196 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3197 | { |
| 3198 | BIO *in; |
| 3199 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3200 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3201 | int ret = -1; |
| 3202 | int order = 0; |
| 3203 | X509_NAME *xname; |
| 3204 | char *str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3205 | pem_password_cb *passwd_cb; |
| 3206 | void *passwd_cb_userdata; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3207 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3208 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3209 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3210 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3211 | STACK_OF(GENERAL_NAME) *names; |
| 3212 | #endif |
| 3213 | |
| 3214 | in = BIO_new(BIO_s_file()); |
| 3215 | if (in == NULL) |
| 3216 | goto end; |
| 3217 | |
| 3218 | if (BIO_read_filename(in, file) <= 0) |
| 3219 | goto end; |
| 3220 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3221 | |
| 3222 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 3223 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 3224 | |
| 3225 | 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] | 3226 | if (x == NULL) |
| 3227 | goto end; |
| 3228 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3229 | pkey = X509_get_pubkey(x); |
| 3230 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3231 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3232 | switch(EVP_PKEY_base_id(pkey)) { |
| 3233 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3234 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3235 | break; |
| 3236 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3237 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3238 | break; |
| 3239 | case EVP_PKEY_DSA: |
| 3240 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3241 | break; |
| 3242 | } |
| 3243 | EVP_PKEY_free(pkey); |
| 3244 | } |
| 3245 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3246 | if (fcount) { |
| 3247 | while (fcount--) |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3248 | 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] | 3249 | } |
| 3250 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3251 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3252 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 3253 | if (names) { |
| 3254 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3255 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3256 | if (name->type == GEN_DNS) { |
| 3257 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3258 | 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] | 3259 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3260 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3261 | } |
| 3262 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3263 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
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 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3266 | xname = X509_get_subject_name(x); |
| 3267 | i = -1; |
| 3268 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3269 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3270 | ASN1_STRING *value; |
| 3271 | |
| 3272 | value = X509_NAME_ENTRY_get_data(entry); |
| 3273 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3274 | 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] | 3275 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3276 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3277 | } |
| 3278 | } |
| 3279 | |
| 3280 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 3281 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 3282 | goto end; |
| 3283 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3284 | #ifdef SSL_CTX_clear_extra_chain_certs |
| 3285 | SSL_CTX_clear_extra_chain_certs(ctx); |
| 3286 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3287 | if (ctx->extra_certs != NULL) { |
| 3288 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 3289 | ctx->extra_certs = NULL; |
| 3290 | } |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3291 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3292 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3293 | 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] | 3294 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3295 | X509_free(ca); |
| 3296 | goto end; |
| 3297 | } |
| 3298 | } |
| 3299 | |
| 3300 | err = ERR_get_error(); |
| 3301 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3302 | /* we successfully reached the last cert in the file */ |
| 3303 | ret = 1; |
| 3304 | } |
| 3305 | ERR_clear_error(); |
| 3306 | |
| 3307 | end: |
| 3308 | if (x) |
| 3309 | X509_free(x); |
| 3310 | |
| 3311 | if (in) |
| 3312 | BIO_free(in); |
| 3313 | |
| 3314 | return ret; |
| 3315 | } |
| 3316 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3317 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3318 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3319 | { |
| 3320 | int ret; |
| 3321 | SSL_CTX *ctx; |
| 3322 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3323 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3324 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3325 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3326 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3327 | return 1; |
| 3328 | } |
| 3329 | |
| 3330 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3331 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 3332 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3333 | SSL_CTX_free(ctx); |
| 3334 | return 1; |
| 3335 | } |
| 3336 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3337 | 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] | 3338 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3339 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 3340 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3341 | if (ret < 0) /* serious error, must do that ourselves */ |
| 3342 | SSL_CTX_free(ctx); |
| 3343 | return 1; |
| 3344 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 3345 | |
| 3346 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3347 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3348 | err && *err ? *err : "", path); |
| 3349 | return 1; |
| 3350 | } |
| 3351 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3352 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3353 | * the tree, so it will be discovered and cleaned in time. |
| 3354 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3355 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3356 | /* store a NULL pointer to indicate we have not yet loaded |
| 3357 | a custom DH param file */ |
| 3358 | if (ssl_dh_ptr_index >= 0) { |
| 3359 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3360 | } |
| 3361 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3362 | ret = ssl_sock_load_dh_params(ctx, path); |
| 3363 | if (ret < 0) { |
| 3364 | if (err) |
| 3365 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3366 | *err ? *err : "", path); |
| 3367 | return 1; |
| 3368 | } |
| 3369 | #endif |
| 3370 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 3371 | #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] | 3372 | ret = ssl_sock_load_ocsp(ctx, path); |
| 3373 | if (ret < 0) { |
| 3374 | if (err) |
| 3375 | 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", |
| 3376 | *err ? *err : "", path); |
| 3377 | return 1; |
| 3378 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3379 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3380 | ssl_sock_set_ocsp_response_from_file(ctx, path); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3381 | #endif |
| 3382 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 3383 | #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] | 3384 | if (sctl_ex_index >= 0) { |
| 3385 | ret = ssl_sock_load_sctl(ctx, path); |
| 3386 | if (ret < 0) { |
| 3387 | if (err) |
| 3388 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 3389 | *err ? *err : "", path); |
| 3390 | return 1; |
| 3391 | } |
| 3392 | } |
| 3393 | #endif |
| 3394 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3395 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3396 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3397 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3398 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3399 | return 1; |
| 3400 | } |
| 3401 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3402 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3403 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3404 | bind_conf->default_ssl_conf = ssl_conf; |
| 3405 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3406 | |
| 3407 | return 0; |
| 3408 | } |
| 3409 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3410 | 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] | 3411 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3412 | struct dirent **de_list; |
| 3413 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3414 | DIR *dir; |
| 3415 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 3416 | char *end; |
| 3417 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3418 | int cfgerr = 0; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3419 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3420 | int is_bundle; |
| 3421 | int j; |
| 3422 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3423 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3424 | if (stat(path, &buf) == 0) { |
| 3425 | dir = opendir(path); |
| 3426 | if (!dir) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3427 | 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] | 3428 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3429 | /* strip trailing slashes, including first one */ |
| 3430 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 3431 | *end = 0; |
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 | n = scandir(path, &de_list, 0, alphasort); |
| 3434 | if (n < 0) { |
| 3435 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 3436 | err && *err ? *err : "", path, strerror(errno)); |
| 3437 | cfgerr++; |
| 3438 | } |
| 3439 | else { |
| 3440 | for (i = 0; i < n; i++) { |
| 3441 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 3442 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3443 | end = strrchr(de->d_name, '.'); |
| 3444 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 3445 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3446 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3447 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 3448 | if (stat(fp, &buf) != 0) { |
| 3449 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3450 | err && *err ? *err : "", fp, strerror(errno)); |
| 3451 | cfgerr++; |
| 3452 | goto ignore_entry; |
| 3453 | } |
| 3454 | if (!S_ISREG(buf.st_mode)) |
| 3455 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3456 | |
| 3457 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3458 | is_bundle = 0; |
| 3459 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 3460 | |
| 3461 | if (end) { |
| 3462 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 3463 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 3464 | is_bundle = 1; |
| 3465 | break; |
| 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | if (is_bundle) { |
| 3470 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 3471 | int dp_len; |
| 3472 | |
| 3473 | dp_len = end - de->d_name; |
| 3474 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 3475 | |
| 3476 | /* increment i and free de until we get to a non-bundle cert |
| 3477 | * Note here that we look at de_list[i + 1] before freeing de |
| 3478 | * this is important since ignore_entry will free de |
| 3479 | */ |
| 3480 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 3481 | free(de); |
| 3482 | i++; |
| 3483 | de = de_list[i]; |
| 3484 | } |
| 3485 | |
| 3486 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3487 | ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3488 | |
| 3489 | /* Successfully processed the bundle */ |
| 3490 | goto ignore_entry; |
| 3491 | } |
| 3492 | } |
| 3493 | |
| 3494 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3495 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3496 | ignore_entry: |
| 3497 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3498 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3499 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3500 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3501 | closedir(dir); |
| 3502 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3503 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3504 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3505 | cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3506 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3507 | return cfgerr; |
| 3508 | } |
| 3509 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3510 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3511 | * done once. Zero is returned if the operation fails. No error is returned |
| 3512 | * if the random is said as not implemented, because we expect that openssl |
| 3513 | * will use another method once needed. |
| 3514 | */ |
| 3515 | static int ssl_initialize_random() |
| 3516 | { |
| 3517 | unsigned char random; |
| 3518 | static int random_initialized = 0; |
| 3519 | |
| 3520 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3521 | random_initialized = 1; |
| 3522 | |
| 3523 | return random_initialized; |
| 3524 | } |
| 3525 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3526 | /* release ssl bind conf */ |
| 3527 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3528 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3529 | if (conf) { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 3530 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3531 | free(conf->npn_str); |
| 3532 | conf->npn_str = NULL; |
| 3533 | #endif |
| 3534 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 3535 | free(conf->alpn_str); |
| 3536 | conf->alpn_str = NULL; |
| 3537 | #endif |
| 3538 | free(conf->ca_file); |
| 3539 | conf->ca_file = NULL; |
| 3540 | free(conf->crl_file); |
| 3541 | conf->crl_file = NULL; |
| 3542 | free(conf->ciphers); |
| 3543 | conf->ciphers = NULL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3544 | free(conf->curves); |
| 3545 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3546 | free(conf->ecdhe); |
| 3547 | conf->ecdhe = NULL; |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 3552 | { |
| 3553 | char thisline[CRT_LINESIZE]; |
| 3554 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3555 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3556 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3557 | int linenum = 0; |
| 3558 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3559 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3560 | if ((f = fopen(file, "r")) == NULL) { |
| 3561 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3562 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3563 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3564 | |
| 3565 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3566 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3567 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3568 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3569 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3570 | char *crt_path; |
| 3571 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3572 | |
| 3573 | linenum++; |
| 3574 | end = line + strlen(line); |
| 3575 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 3576 | /* Check if we reached the limit and the last char is not \n. |
| 3577 | * Watch out for the last line without the terminating '\n'! |
| 3578 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3579 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 3580 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3581 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3582 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3583 | } |
| 3584 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3585 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3586 | newarg = 1; |
| 3587 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3588 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 3589 | /* end of string, end of loop */ |
| 3590 | *line = 0; |
| 3591 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3592 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3593 | newarg = 1; |
| 3594 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3595 | } else if (*line == '[') { |
| 3596 | if (ssl_b) { |
| 3597 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 3598 | cfgerr = 1; |
| 3599 | break; |
| 3600 | } |
| 3601 | if (!arg) { |
| 3602 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 3603 | cfgerr = 1; |
| 3604 | break; |
| 3605 | } |
| 3606 | ssl_b = arg; |
| 3607 | newarg = 1; |
| 3608 | *line = 0; |
| 3609 | } else if (*line == ']') { |
| 3610 | if (ssl_e) { |
| 3611 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3612 | cfgerr = 1; |
| 3613 | break; |
| 3614 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3615 | if (!ssl_b) { |
| 3616 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 3617 | cfgerr = 1; |
| 3618 | break; |
| 3619 | } |
| 3620 | ssl_e = arg; |
| 3621 | newarg = 1; |
| 3622 | *line = 0; |
| 3623 | } else if (newarg) { |
| 3624 | if (arg == MAX_CRT_ARGS) { |
| 3625 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 3626 | cfgerr = 1; |
| 3627 | break; |
| 3628 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3629 | newarg = 0; |
| 3630 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3631 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3632 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3633 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3634 | if (cfgerr) |
| 3635 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3636 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3637 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3638 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3639 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3640 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3641 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3642 | crt_path = args[0]; |
| 3643 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 3644 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 3645 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 3646 | crt_path, linenum, file); |
| 3647 | cfgerr = 1; |
| 3648 | break; |
| 3649 | } |
| 3650 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 3651 | crt_path = path; |
| 3652 | } |
| 3653 | |
| 3654 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 3655 | cur_arg = ssl_b ? ssl_b : 1; |
| 3656 | while (cur_arg < ssl_e) { |
| 3657 | newarg = 0; |
| 3658 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 3659 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 3660 | newarg = 1; |
| 3661 | cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
| 3662 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 3663 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 3664 | args[cur_arg], linenum, file); |
| 3665 | cfgerr = 1; |
| 3666 | } |
| 3667 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 3668 | break; |
| 3669 | } |
| 3670 | } |
| 3671 | if (!cfgerr && !newarg) { |
| 3672 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 3673 | args[cur_arg], linenum, file); |
| 3674 | cfgerr = 1; |
| 3675 | break; |
| 3676 | } |
| 3677 | } |
| 3678 | if (cfgerr) { |
| 3679 | ssl_sock_free_ssl_conf(ssl_conf); |
| 3680 | free(ssl_conf); |
| 3681 | ssl_conf = NULL; |
| 3682 | break; |
| 3683 | } |
| 3684 | |
| 3685 | if (stat(crt_path, &buf) == 0) { |
| 3686 | cfgerr = ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf, |
| 3687 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3688 | } else { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3689 | cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf, |
| 3690 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3691 | } |
| 3692 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3693 | if (cfgerr) { |
| 3694 | 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] | 3695 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3696 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3697 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3698 | fclose(f); |
| 3699 | return cfgerr; |
| 3700 | } |
| 3701 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3702 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3703 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3704 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3705 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3706 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3707 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3708 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3709 | SSL_OP_NO_SSLv2 | |
| 3710 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3711 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3712 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3713 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
| 3714 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3715 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3716 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3717 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3718 | SSL_MODE_RELEASE_BUFFERS | |
| 3719 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3720 | 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] | 3721 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3722 | int flags = MC_SSL_O_ALL; |
| 3723 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3724 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3725 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3726 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3727 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3728 | 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] | 3729 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3730 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3731 | 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] | 3732 | else |
| 3733 | flags = conf_ssl_methods->flags; |
| 3734 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3735 | min = conf_ssl_methods->min; |
| 3736 | max = conf_ssl_methods->max; |
| 3737 | /* start with TLSv10 to remove SSLv3 per default */ |
| 3738 | if (!min && (!max || max >= CONF_TLSV10)) |
| 3739 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3740 | /* 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] | 3741 | if (min) |
| 3742 | flags |= (methodVersions[min].flag - 1); |
| 3743 | if (max) |
| 3744 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3745 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3746 | min = max = CONF_TLSV_NONE; |
| 3747 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3748 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3749 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3750 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3751 | if (min) { |
| 3752 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3753 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 3754 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3755 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3756 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3757 | hole = 0; |
| 3758 | } |
| 3759 | max = i; |
| 3760 | } |
| 3761 | else { |
| 3762 | min = max = i; |
| 3763 | } |
| 3764 | } |
| 3765 | else { |
| 3766 | if (min) |
| 3767 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3768 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3769 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3770 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3771 | 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] | 3772 | cfgerr += 1; |
| 3773 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3774 | /* save real min/max in bind_conf */ |
| 3775 | conf_ssl_methods->min = min; |
| 3776 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3777 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 3778 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3779 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
| 3780 | precautionary measure to avoid any suprise with older openssl version. */ |
| 3781 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3782 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3783 | else |
| 3784 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3785 | if (flags & methodVersions[i].flag) |
| 3786 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3787 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3788 | /* 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] | 3789 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3790 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3791 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3792 | |
| 3793 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3794 | options |= SSL_OP_NO_TICKET; |
| 3795 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3796 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 3797 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3798 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 3799 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3800 | if (global_ssl.async) |
| 3801 | mode |= SSL_MODE_ASYNC; |
| 3802 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3803 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3804 | if (global_ssl.life_time) |
| 3805 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3806 | |
| 3807 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3808 | #ifdef OPENSSL_IS_BORINGSSL |
| 3809 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3810 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3811 | #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 3812 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 3813 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3814 | #else |
| 3815 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3816 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 3817 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3818 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3819 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3820 | } |
| 3821 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3822 | |
| 3823 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 3824 | { |
| 3825 | if (first == block) { |
| 3826 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3827 | if (first->len > 0) |
| 3828 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 3829 | } |
| 3830 | } |
| 3831 | |
| 3832 | /* return first block from sh_ssl_sess */ |
| 3833 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 3834 | { |
| 3835 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 3836 | |
| 3837 | } |
| 3838 | |
| 3839 | /* store a session into the cache |
| 3840 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 3841 | * data: asn1 encoded session |
| 3842 | * data_len: asn1 encoded session length |
| 3843 | * Returns 1 id session was stored (else 0) |
| 3844 | */ |
| 3845 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 3846 | { |
| 3847 | struct shared_block *first; |
| 3848 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 3849 | |
| 3850 | first = shctx_row_reserve_hot(ssl_shctx, data_len + sizeof(struct sh_ssl_sess_hdr)); |
| 3851 | if (!first) { |
| 3852 | /* Could not retrieve enough free blocks to store that session */ |
| 3853 | return 0; |
| 3854 | } |
| 3855 | |
| 3856 | /* STORE the key in the first elem */ |
| 3857 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3858 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 3859 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3860 | |
| 3861 | /* it returns the already existing node |
| 3862 | or current node if none, never returns null */ |
| 3863 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 3864 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 3865 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 3866 | /* release the reserved row */ |
| 3867 | shctx_row_dec_hot(ssl_shctx, first); |
| 3868 | /* replace the previous session already in the tree */ |
| 3869 | sh_ssl_sess = oldsh_ssl_sess; |
| 3870 | /* ignore the previous session data, only use the header */ |
| 3871 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 3872 | shctx_row_inc_hot(ssl_shctx, first); |
| 3873 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3874 | } |
| 3875 | |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 3876 | if (shctx_row_data_append(ssl_shctx, first, data, data_len) < 0) { |
| 3877 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3878 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 3879 | } |
| 3880 | |
| 3881 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3882 | |
| 3883 | return 1; |
| 3884 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3885 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3886 | /* SSL callback used when a new session is created while connecting to a server */ |
| 3887 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 3888 | { |
| 3889 | struct connection *conn = SSL_get_app_data(ssl); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3890 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3891 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3892 | s = objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3893 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3894 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 3895 | int len; |
| 3896 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3897 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3898 | len = i2d_SSL_SESSION(sess, NULL); |
| 3899 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 3900 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 3901 | } else { |
| 3902 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3903 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 3904 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 3905 | } |
| 3906 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 3907 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 3908 | &ptr); |
| 3909 | } |
| 3910 | } else { |
| 3911 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3912 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 3913 | } |
| 3914 | |
| 3915 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3916 | } |
| 3917 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3918 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3919 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3920 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3921 | { |
| 3922 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 3923 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 3924 | unsigned char *p; |
| 3925 | int data_len; |
| 3926 | unsigned int sid_length, sid_ctx_length; |
| 3927 | const unsigned char *sid_data; |
| 3928 | const unsigned char *sid_ctx_data; |
| 3929 | |
| 3930 | /* Session id is already stored in to key and session id is known |
| 3931 | * so we dont store it to keep size. |
| 3932 | */ |
| 3933 | |
| 3934 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 3935 | sid_ctx_data = SSL_SESSION_get0_id_context(sess, &sid_ctx_length); |
| 3936 | SSL_SESSION_set1_id(sess, sid_data, 0); |
| 3937 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, 0); |
| 3938 | |
| 3939 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 3940 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 3941 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 3942 | goto err; |
| 3943 | |
| 3944 | p = encsess; |
| 3945 | |
| 3946 | /* process ASN1 session encoding before the lock */ |
| 3947 | i2d_SSL_SESSION(sess, &p); |
| 3948 | |
| 3949 | memcpy(encid, sid_data, sid_length); |
| 3950 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 3951 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 3952 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3953 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3954 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3955 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3956 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3957 | err: |
| 3958 | /* reset original length values */ |
| 3959 | SSL_SESSION_set1_id(sess, sid_data, sid_length); |
| 3960 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, sid_ctx_length); |
| 3961 | |
| 3962 | return 0; /* do not increment session reference count */ |
| 3963 | } |
| 3964 | |
| 3965 | /* 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] | 3966 | 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] | 3967 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3968 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3969 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 3970 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3971 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3972 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3973 | |
| 3974 | global.shctx_lookups++; |
| 3975 | |
| 3976 | /* allow the session to be freed automatically by openssl */ |
| 3977 | *do_copy = 0; |
| 3978 | |
| 3979 | /* tree key is zeros padded sessionid */ |
| 3980 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3981 | memcpy(tmpkey, key, key_len); |
| 3982 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 3983 | key = tmpkey; |
| 3984 | } |
| 3985 | |
| 3986 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3987 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3988 | |
| 3989 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3990 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 3991 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3992 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3993 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3994 | global.shctx_misses++; |
| 3995 | return NULL; |
| 3996 | } |
| 3997 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3998 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 3999 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4000 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4001 | 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] | 4002 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4003 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4004 | |
| 4005 | /* decode ASN1 session */ |
| 4006 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4007 | 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] | 4008 | /* Reset session id and session id contenxt */ |
| 4009 | if (sess) { |
| 4010 | SSL_SESSION_set1_id(sess, key, key_len); |
| 4011 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4012 | } |
| 4013 | |
| 4014 | return sess; |
| 4015 | } |
| 4016 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4017 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4018 | /* 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] | 4019 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4020 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4021 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4022 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 4023 | unsigned int sid_length; |
| 4024 | const unsigned char *sid_data; |
| 4025 | (void)ctx; |
| 4026 | |
| 4027 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4028 | /* tree key is zeros padded sessionid */ |
| 4029 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4030 | memcpy(tmpkey, sid_data, sid_length); |
| 4031 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 4032 | sid_data = tmpkey; |
| 4033 | } |
| 4034 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4035 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4036 | |
| 4037 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4038 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 4039 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4040 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4041 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4045 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4046 | } |
| 4047 | |
| 4048 | /* Set session cache mode to server and disable openssl internal cache. |
| 4049 | * Set shared cache callbacks on an ssl context. |
| 4050 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4051 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4052 | { |
| 4053 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4054 | |
| 4055 | if (!ssl_shctx) { |
| 4056 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4057 | return; |
| 4058 | } |
| 4059 | |
| 4060 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4061 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4062 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4063 | |
| 4064 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4065 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4066 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4067 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4068 | } |
| 4069 | |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4070 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
| 4071 | { |
| 4072 | struct proxy *curproxy = bind_conf->frontend; |
| 4073 | int cfgerr = 0; |
| 4074 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4075 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4076 | const char *conf_ciphers; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4077 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4078 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4079 | if (ssl_conf) { |
| 4080 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4081 | int i, min, max; |
| 4082 | int flags = MC_SSL_O_ALL; |
| 4083 | |
| 4084 | /* 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] | 4085 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4086 | 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] | 4087 | if (min) |
| 4088 | flags |= (methodVersions[min].flag - 1); |
| 4089 | if (max) |
| 4090 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4091 | min = max = CONF_TLSV_NONE; |
| 4092 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4093 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4094 | if (min) |
| 4095 | max = i; |
| 4096 | else |
| 4097 | min = max = i; |
| 4098 | } |
| 4099 | /* save real min/max */ |
| 4100 | conf_ssl_methods->min = min; |
| 4101 | conf_ssl_methods->max = max; |
| 4102 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4103 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4104 | 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] | 4105 | cfgerr += 1; |
| 4106 | } |
| 4107 | } |
| 4108 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4109 | 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] | 4110 | case SSL_SOCK_VERIFY_NONE: |
| 4111 | verify = SSL_VERIFY_NONE; |
| 4112 | break; |
| 4113 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4114 | verify = SSL_VERIFY_PEER; |
| 4115 | break; |
| 4116 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4117 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4118 | break; |
| 4119 | } |
| 4120 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4121 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4122 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 4123 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 4124 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4125 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4126 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4127 | ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
| 4128 | 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] | 4129 | cfgerr++; |
| 4130 | } |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 4131 | if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) { |
| 4132 | /* set CA names for client cert request, function returns void */ |
| 4133 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file)); |
| 4134 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4135 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4136 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4137 | ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4138 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4139 | cfgerr++; |
| 4140 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4141 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4142 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4143 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4144 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4145 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4146 | ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4147 | 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] | 4148 | cfgerr++; |
| 4149 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4150 | else { |
| 4151 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4152 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4153 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4154 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4155 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4156 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4157 | #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] | 4158 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4159 | 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] | 4160 | ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4161 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4162 | cfgerr++; |
| 4163 | } |
| 4164 | } |
| 4165 | #endif |
| 4166 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4167 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4168 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4169 | if (conf_ciphers && |
| 4170 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4171 | ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4172 | 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] | 4173 | cfgerr++; |
| 4174 | } |
| 4175 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4176 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4177 | /* If tune.ssl.default-dh-param has not been set, |
| 4178 | neither has ssl-default-dh-file and no static DH |
| 4179 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4180 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4181 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4182 | (ssl_dh_ptr_index == -1 || |
| 4183 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4184 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 4185 | const SSL_CIPHER * cipher = NULL; |
| 4186 | char cipher_description[128]; |
| 4187 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 4188 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 4189 | which is not ephemeral DH. */ |
| 4190 | const char dhe_description[] = " Kx=DH "; |
| 4191 | const char dhe_export_description[] = " Kx=DH("; |
| 4192 | int idx = 0; |
| 4193 | int dhe_found = 0; |
| 4194 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4195 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4196 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4197 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4198 | if (ssl) { |
| 4199 | ciphers = SSL_get_ciphers(ssl); |
| 4200 | |
| 4201 | if (ciphers) { |
| 4202 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 4203 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 4204 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 4205 | if (strstr(cipher_description, dhe_description) != NULL || |
| 4206 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 4207 | dhe_found = 1; |
| 4208 | break; |
| 4209 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 4210 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4211 | } |
| 4212 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4213 | SSL_free(ssl); |
| 4214 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4215 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4216 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4217 | if (dhe_found) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4218 | 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] | 4219 | } |
| 4220 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4221 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4222 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4223 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4224 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4225 | if (local_dh_1024 == NULL) { |
| 4226 | local_dh_1024 = ssl_get_dh_1024(); |
| 4227 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4228 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4229 | if (local_dh_2048 == NULL) { |
| 4230 | local_dh_2048 = ssl_get_dh_2048(); |
| 4231 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4232 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4233 | if (local_dh_4096 == NULL) { |
| 4234 | local_dh_4096 = ssl_get_dh_4096(); |
| 4235 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4236 | } |
| 4237 | } |
| 4238 | } |
| 4239 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4240 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4241 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4242 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4243 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4244 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4245 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4246 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4247 | ssl_conf_cur = NULL; |
| 4248 | if (ssl_conf && ssl_conf->npn_str) |
| 4249 | ssl_conf_cur = ssl_conf; |
| 4250 | else if (bind_conf->ssl_conf.npn_str) |
| 4251 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4252 | if (ssl_conf_cur) |
| 4253 | 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] | 4254 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4255 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4256 | ssl_conf_cur = NULL; |
| 4257 | if (ssl_conf && ssl_conf->alpn_str) |
| 4258 | ssl_conf_cur = ssl_conf; |
| 4259 | else if (bind_conf->ssl_conf.alpn_str) |
| 4260 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4261 | if (ssl_conf_cur) |
| 4262 | 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] | 4263 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4264 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 4265 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4266 | if (conf_curves) { |
| 4267 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4268 | ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4269 | 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] | 4270 | cfgerr++; |
| 4271 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4272 | #if defined(SSL_CTX_set_ecdh_auto) |
| 4273 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
| 4274 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4275 | } |
| 4276 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4277 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4278 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4279 | int i; |
| 4280 | EC_KEY *ecdh; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4281 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4282 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4283 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4284 | NULL); |
| 4285 | |
| 4286 | if (ecdhe == NULL) { |
| 4287 | SSL_CTX_set_dh_auto(ctx, 1); |
| 4288 | return cfgerr; |
| 4289 | } |
| 4290 | #else |
| 4291 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4292 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4293 | ECDHE_DEFAULT_CURVE); |
| 4294 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4295 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4296 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4297 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4298 | ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4299 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4300 | cfgerr++; |
| 4301 | } |
| 4302 | else { |
| 4303 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4304 | EC_KEY_free(ecdh); |
| 4305 | } |
| 4306 | } |
| 4307 | #endif |
| 4308 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4309 | return cfgerr; |
| 4310 | } |
| 4311 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4312 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4313 | { |
| 4314 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4315 | size_t prefixlen, suffixlen; |
| 4316 | |
| 4317 | /* Trivial case */ |
| 4318 | if (strcmp(pattern, hostname) == 0) |
| 4319 | return 1; |
| 4320 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4321 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4322 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4323 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4324 | pattern_wildcard = NULL; |
| 4325 | pattern_left_label_end = pattern; |
| 4326 | while (*pattern_left_label_end != '.') { |
| 4327 | switch (*pattern_left_label_end) { |
| 4328 | case 0: |
| 4329 | /* End of label not found */ |
| 4330 | return 0; |
| 4331 | case '*': |
| 4332 | /* If there is more than one wildcards */ |
| 4333 | if (pattern_wildcard) |
| 4334 | return 0; |
| 4335 | pattern_wildcard = pattern_left_label_end; |
| 4336 | break; |
| 4337 | } |
| 4338 | pattern_left_label_end++; |
| 4339 | } |
| 4340 | |
| 4341 | /* If it's not trivial and there is no wildcard, it can't |
| 4342 | * match */ |
| 4343 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4344 | return 0; |
| 4345 | |
| 4346 | /* Make sure all labels match except the leftmost */ |
| 4347 | hostname_left_label_end = strchr(hostname, '.'); |
| 4348 | if (!hostname_left_label_end |
| 4349 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 4350 | return 0; |
| 4351 | |
| 4352 | /* Make sure the leftmost label of the hostname is long enough |
| 4353 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4354 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4355 | return 0; |
| 4356 | |
| 4357 | /* Finally compare the string on either side of the |
| 4358 | * wildcard */ |
| 4359 | prefixlen = pattern_wildcard - pattern; |
| 4360 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4361 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 4362 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4363 | return 0; |
| 4364 | |
| 4365 | return 1; |
| 4366 | } |
| 4367 | |
| 4368 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4369 | { |
| 4370 | SSL *ssl; |
| 4371 | struct connection *conn; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4372 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4373 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4374 | |
| 4375 | int depth; |
| 4376 | X509 *cert; |
| 4377 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4378 | int i; |
| 4379 | X509_NAME *cert_subject; |
| 4380 | char *str; |
| 4381 | |
| 4382 | if (ok == 0) |
| 4383 | return ok; |
| 4384 | |
| 4385 | ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4386 | conn = SSL_get_app_data(ssl); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4387 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4388 | /* We're checking if the provided hostnames match the desired one. The |
| 4389 | * desired hostname comes from the SNI we presented if any, or if not |
| 4390 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4391 | * directive. If neither is set, we don't care about the name so the |
| 4392 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4393 | */ |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4394 | servername = SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4395 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4396 | if (!servername) { |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4397 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4398 | if (!servername) |
| 4399 | return ok; |
| 4400 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4401 | |
| 4402 | /* We only need to verify the CN on the actual server cert, |
| 4403 | * not the indirect CAs */ |
| 4404 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4405 | if (depth != 0) |
| 4406 | return ok; |
| 4407 | |
| 4408 | /* At this point, the cert is *not* OK unless we can find a |
| 4409 | * hostname match */ |
| 4410 | ok = 0; |
| 4411 | |
| 4412 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4413 | /* It seems like this might happen if verify peer isn't set */ |
| 4414 | if (!cert) |
| 4415 | return ok; |
| 4416 | |
| 4417 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4418 | if (alt_names) { |
| 4419 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4420 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4421 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4422 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 4423 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4424 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4425 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4426 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4427 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4428 | OPENSSL_free(str); |
| 4429 | } |
| 4430 | } |
| 4431 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4432 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | cert_subject = X509_get_subject_name(cert); |
| 4436 | i = -1; |
| 4437 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4438 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4439 | ASN1_STRING *value; |
| 4440 | value = X509_NAME_ENTRY_get_data(entry); |
| 4441 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4442 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4443 | OPENSSL_free(str); |
| 4444 | } |
| 4445 | } |
| 4446 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4447 | /* report the mismatch and indicate if SNI was used or not */ |
| 4448 | if (!ok && !conn->err_code) |
| 4449 | 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] | 4450 | return ok; |
| 4451 | } |
| 4452 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4453 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4454 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4455 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4456 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4457 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4458 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4459 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4460 | SSL_OP_NO_SSLv2 | |
| 4461 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4462 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4463 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4464 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4465 | SSL_MODE_RELEASE_BUFFERS | |
| 4466 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4467 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4468 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4469 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4470 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4471 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4472 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4473 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4474 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4475 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4476 | cfgerr++; |
| 4477 | } |
| 4478 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4479 | /* Automatic memory computations need to know we use SSL there */ |
| 4480 | global.ssl_used_backend = 1; |
| 4481 | |
| 4482 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4483 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4484 | 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] | 4485 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 4486 | curproxy->id, srv->id, |
| 4487 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4488 | cfgerr++; |
| 4489 | return cfgerr; |
| 4490 | } |
| 4491 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4492 | if (srv->use_ssl) |
| 4493 | srv->xprt = &ssl_sock; |
| 4494 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 4495 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4496 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4497 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4498 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4499 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 4500 | proxy_type_str(curproxy), curproxy->id, |
| 4501 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4502 | cfgerr++; |
| 4503 | return cfgerr; |
| 4504 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4505 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4506 | 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] | 4507 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 4508 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4509 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4510 | else |
| 4511 | flags = conf_ssl_methods->flags; |
| 4512 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4513 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 4514 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4515 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4516 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4517 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4518 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4519 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4520 | min = max = CONF_TLSV_NONE; |
| 4521 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4522 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4523 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4524 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4525 | if (min) { |
| 4526 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4527 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 4528 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4529 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4530 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4531 | hole = 0; |
| 4532 | } |
| 4533 | max = i; |
| 4534 | } |
| 4535 | else { |
| 4536 | min = max = i; |
| 4537 | } |
| 4538 | } |
| 4539 | else { |
| 4540 | if (min) |
| 4541 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4542 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4543 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4544 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4545 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4546 | cfgerr += 1; |
| 4547 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4548 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 4549 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4550 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
| 4551 | precautionary measure to avoid any suprise with older openssl version. */ |
| 4552 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4553 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4554 | else |
| 4555 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4556 | if (flags & methodVersions[i].flag) |
| 4557 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4558 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4559 | /* 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] | 4560 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4561 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4562 | #endif |
| 4563 | |
| 4564 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4565 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4566 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4567 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 4568 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4569 | if (global_ssl.async) |
| 4570 | mode |= SSL_MODE_ASYNC; |
| 4571 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4572 | SSL_CTX_set_mode(ctx, mode); |
| 4573 | srv->ssl_ctx.ctx = ctx; |
| 4574 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4575 | if (srv->ssl_ctx.client_crt) { |
| 4576 | 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] | 4577 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4578 | proxy_type_str(curproxy), curproxy->id, |
| 4579 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4580 | cfgerr++; |
| 4581 | } |
| 4582 | 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] | 4583 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4584 | proxy_type_str(curproxy), curproxy->id, |
| 4585 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4586 | cfgerr++; |
| 4587 | } |
| 4588 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4589 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4590 | proxy_type_str(curproxy), curproxy->id, |
| 4591 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4592 | cfgerr++; |
| 4593 | } |
| 4594 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4595 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4596 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4597 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4598 | switch (srv->ssl_ctx.verify) { |
| 4599 | case SSL_SOCK_VERIFY_NONE: |
| 4600 | verify = SSL_VERIFY_NONE; |
| 4601 | break; |
| 4602 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4603 | verify = SSL_VERIFY_PEER; |
| 4604 | break; |
| 4605 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4606 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4607 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4608 | (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] | 4609 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4610 | if (srv->ssl_ctx.ca_file) { |
| 4611 | /* load CAfile to verify */ |
| 4612 | 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] | 4613 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n", |
| 4614 | curproxy->id, srv->id, |
| 4615 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4616 | cfgerr++; |
| 4617 | } |
| 4618 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4619 | else { |
| 4620 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4621 | 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", |
| 4622 | curproxy->id, srv->id, |
| 4623 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4624 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4625 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 4626 | curproxy->id, srv->id, |
| 4627 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4628 | cfgerr++; |
| 4629 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4630 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4631 | if (srv->ssl_ctx.crl_file) { |
| 4632 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4633 | |
| 4634 | 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] | 4635 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 4636 | curproxy->id, srv->id, |
| 4637 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4638 | cfgerr++; |
| 4639 | } |
| 4640 | else { |
| 4641 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4642 | } |
| 4643 | } |
| 4644 | #endif |
| 4645 | } |
| 4646 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4647 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 4648 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 4649 | 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] | 4650 | if (srv->ssl_ctx.ciphers && |
| 4651 | !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] | 4652 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4653 | curproxy->id, srv->id, |
| 4654 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4655 | cfgerr++; |
| 4656 | } |
| 4657 | |
| 4658 | return cfgerr; |
| 4659 | } |
| 4660 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4661 | /* 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] | 4662 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4663 | * encountered. |
| 4664 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4665 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4666 | { |
| 4667 | struct ebmb_node *node; |
| 4668 | struct sni_ctx *sni; |
| 4669 | int err = 0; |
| 4670 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4671 | /* Automatic memory computations need to know we use SSL there */ |
| 4672 | global.ssl_used_frontend = 1; |
| 4673 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4674 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4675 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4676 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4677 | err++; |
| 4678 | } |
| 4679 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4680 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4681 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4682 | /* It should not be necessary to call this function, but it's |
| 4683 | necessary first to check and move all initialisation related |
| 4684 | to initial_ctx in ssl_sock_initial_ctx. */ |
| 4685 | err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx); |
| 4686 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4687 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4688 | 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] | 4689 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4690 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4691 | while (node) { |
| 4692 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4693 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4694 | /* only initialize the CTX on its first occurrence and |
| 4695 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4696 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4697 | node = ebmb_next(node); |
| 4698 | } |
| 4699 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4700 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4701 | while (node) { |
| 4702 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4703 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4704 | /* only initialize the CTX on its first occurrence and |
| 4705 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4706 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4707 | node = ebmb_next(node); |
| 4708 | } |
| 4709 | return err; |
| 4710 | } |
| 4711 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4712 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4713 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4714 | * alerts are directly emitted since the rest of the stack does it below. |
| 4715 | */ |
| 4716 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4717 | { |
| 4718 | struct proxy *px = bind_conf->frontend; |
| 4719 | int alloc_ctx; |
| 4720 | int err; |
| 4721 | |
| 4722 | if (!bind_conf->is_ssl) { |
| 4723 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4724 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 4725 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4726 | } |
| 4727 | return 0; |
| 4728 | } |
| 4729 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4730 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4731 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 4732 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4733 | } |
| 4734 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4735 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 4736 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4737 | return -1; |
| 4738 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4739 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 4740 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4741 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
| 4742 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, |
| 4743 | sizeof(*sh_ssl_sess_tree), |
| 4744 | ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0); |
| 4745 | if (alloc_ctx < 0) { |
| 4746 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 4747 | 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"); |
| 4748 | else |
| 4749 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 4750 | return -1; |
| 4751 | } |
| 4752 | /* free block callback */ |
| 4753 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 4754 | /* init the root tree within the extra space */ |
| 4755 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 4756 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4757 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4758 | err = 0; |
| 4759 | /* initialize all certificate contexts */ |
| 4760 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 4761 | |
| 4762 | /* initialize CA variables if the certificates generation is enabled */ |
| 4763 | err += ssl_sock_load_ca(bind_conf); |
| 4764 | |
| 4765 | return -err; |
| 4766 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4767 | |
| 4768 | /* release ssl context allocated for servers. */ |
| 4769 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 4770 | { |
| 4771 | if (srv->ssl_ctx.ctx) |
| 4772 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 4773 | } |
| 4774 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4775 | /* 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] | 4776 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 4777 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4778 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4779 | { |
| 4780 | struct ebmb_node *node, *back; |
| 4781 | struct sni_ctx *sni; |
| 4782 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4783 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4784 | while (node) { |
| 4785 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4786 | back = ebmb_next(node); |
| 4787 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4788 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4789 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4790 | ssl_sock_free_ssl_conf(sni->conf); |
| 4791 | free(sni->conf); |
| 4792 | sni->conf = NULL; |
| 4793 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4794 | free(sni); |
| 4795 | node = back; |
| 4796 | } |
| 4797 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4798 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4799 | while (node) { |
| 4800 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4801 | back = ebmb_next(node); |
| 4802 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4803 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4804 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4805 | ssl_sock_free_ssl_conf(sni->conf); |
| 4806 | free(sni->conf); |
| 4807 | sni->conf = NULL; |
| 4808 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4809 | free(sni); |
| 4810 | node = back; |
| 4811 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4812 | SSL_CTX_free(bind_conf->initial_ctx); |
| 4813 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4814 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4815 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4816 | } |
| 4817 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4818 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 4819 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 4820 | { |
| 4821 | ssl_sock_free_ca(bind_conf); |
| 4822 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4823 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4824 | free(bind_conf->ca_sign_file); |
| 4825 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4826 | if (bind_conf->keys_ref) { |
| 4827 | free(bind_conf->keys_ref->filename); |
| 4828 | free(bind_conf->keys_ref->tlskeys); |
| 4829 | LIST_DEL(&bind_conf->keys_ref->list); |
| 4830 | free(bind_conf->keys_ref); |
| 4831 | } |
| 4832 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4833 | bind_conf->ca_sign_pass = NULL; |
| 4834 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4835 | } |
| 4836 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4837 | /* Load CA cert file and private key used to generate certificates */ |
| 4838 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4839 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4840 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4841 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4842 | FILE *fp; |
| 4843 | X509 *cacert = NULL; |
| 4844 | EVP_PKEY *capkey = NULL; |
| 4845 | int err = 0; |
| 4846 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 4847 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4848 | return err; |
| 4849 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4850 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4851 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4852 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4853 | HA_RWLOCK_INIT(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4854 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4855 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4856 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 4857 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4858 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4859 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4860 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 4861 | "no CA certificate File configured at [%s:%d].\n", |
| 4862 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4863 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4864 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4865 | |
| 4866 | /* read in the CA certificate */ |
| 4867 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4868 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4869 | 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] | 4870 | goto load_error; |
| 4871 | } |
| 4872 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
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 | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4875 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4876 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4877 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4878 | 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] | 4879 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 4880 | 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] | 4881 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4882 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4883 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4884 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4885 | bind_conf->ca_sign_cert = cacert; |
| 4886 | bind_conf->ca_sign_pkey = capkey; |
| 4887 | return err; |
| 4888 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4889 | read_error: |
| 4890 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4891 | if (capkey) EVP_PKEY_free(capkey); |
| 4892 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4893 | load_error: |
| 4894 | bind_conf->generate_certs = 0; |
| 4895 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4896 | return err; |
| 4897 | } |
| 4898 | |
| 4899 | /* Release CA cert and private key used to generate certificated */ |
| 4900 | void |
| 4901 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 4902 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4903 | if (bind_conf->ca_sign_pkey) |
| 4904 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 4905 | if (bind_conf->ca_sign_cert) |
| 4906 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 4907 | bind_conf->ca_sign_pkey = NULL; |
| 4908 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4909 | } |
| 4910 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4911 | /* |
| 4912 | * This function is called if SSL * context is not yet allocated. The function |
| 4913 | * is designed to be called before any other data-layer operation and sets the |
| 4914 | * handshake flag on the connection. It is safe to call it multiple times. |
| 4915 | * It returns 0 on success and -1 in error case. |
| 4916 | */ |
| 4917 | static int ssl_sock_init(struct connection *conn) |
| 4918 | { |
| 4919 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4920 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4921 | return 0; |
| 4922 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4923 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4924 | return 0; |
| 4925 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4926 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 4927 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4928 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4929 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4930 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4931 | /* If it is in client mode initiate SSL session |
| 4932 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4933 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4934 | int may_retry = 1; |
| 4935 | |
| 4936 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4937 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4938 | conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4939 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4940 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4941 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4942 | goto retry_connect; |
| 4943 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4944 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4945 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4946 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4947 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4948 | /* set fd on SSL session context */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 4949 | if (!SSL_set_fd(conn->xprt_ctx, conn->handle.fd)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4950 | SSL_free(conn->xprt_ctx); |
| 4951 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4952 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4953 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4954 | goto retry_connect; |
| 4955 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4956 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4957 | return -1; |
| 4958 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4959 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4960 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4961 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 4962 | SSL_free(conn->xprt_ctx); |
| 4963 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4964 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4965 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4966 | goto retry_connect; |
| 4967 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4968 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4969 | return -1; |
| 4970 | } |
| 4971 | |
| 4972 | SSL_set_connect_state(conn->xprt_ctx); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4973 | if (objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 4974 | const unsigned char *ptr = objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 4975 | SSL_SESSION *sess = d2i_SSL_SESSION(NULL, &ptr, objt_server(conn->target)->ssl_ctx.reused_sess[tid].size); |
| 4976 | if(sess && !SSL_set_session(conn->xprt_ctx, sess)) { |
| 4977 | SSL_SESSION_free(sess); |
| 4978 | free(objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 4979 | objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 4980 | } else if (sess) { |
| 4981 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4982 | } |
| 4983 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4984 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4985 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4986 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4987 | |
| 4988 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 4989 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4990 | return 0; |
| 4991 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4992 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4993 | int may_retry = 1; |
| 4994 | |
| 4995 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4996 | /* Alloc a new SSL session ctx */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4997 | 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] | 4998 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4999 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5000 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5001 | goto retry_accept; |
| 5002 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5003 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5004 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5005 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5006 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5007 | /* set fd on SSL session context */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5008 | if (!SSL_set_fd(conn->xprt_ctx, conn->handle.fd)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5009 | SSL_free(conn->xprt_ctx); |
| 5010 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5011 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5012 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5013 | goto retry_accept; |
| 5014 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5015 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5016 | return -1; |
| 5017 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5018 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5019 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5020 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 5021 | SSL_free(conn->xprt_ctx); |
| 5022 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5023 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5024 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5025 | goto retry_accept; |
| 5026 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5027 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5028 | return -1; |
| 5029 | } |
| 5030 | |
| 5031 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5032 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5033 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5034 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5035 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L || defined(OPENSSL_IS_BORINGSSL) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5036 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 5037 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5038 | |
| 5039 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 5040 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5041 | return 0; |
| 5042 | } |
| 5043 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5044 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5045 | return -1; |
| 5046 | } |
| 5047 | |
| 5048 | |
| 5049 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5050 | * updates the FD status if it wants some polling before being called again. |
| 5051 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5052 | * otherwise it returns non-zero and removes itself from the connection's |
| 5053 | * flags (the bit is provided in <flag> by the caller). |
| 5054 | */ |
| 5055 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 5056 | { |
| 5057 | int ret; |
| 5058 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5059 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5060 | return 0; |
| 5061 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5062 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5063 | goto out_error; |
| 5064 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5065 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L |
| 5066 | /* |
| 5067 | * Check if we have early data. If we do, we have to read them |
| 5068 | * before SSL_do_handshake() is called, And there's no way to |
| 5069 | * detect early data, except to try to read them |
| 5070 | */ |
| 5071 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5072 | size_t read_data; |
| 5073 | |
| 5074 | ret = SSL_read_early_data(conn->xprt_ctx, &conn->tmp_early_data, |
| 5075 | 1, &read_data); |
| 5076 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5077 | goto check_error; |
| 5078 | if (ret == SSL_READ_EARLY_DATA_SUCCESS) { |
| 5079 | conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN); |
| 5080 | return 1; |
| 5081 | } else |
| 5082 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5083 | } |
| 5084 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5085 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5086 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5087 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5088 | * the reneg handshake. |
| 5089 | * Here we use SSL_peek as a workaround for reneg. |
| 5090 | */ |
| 5091 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5092 | char c; |
| 5093 | |
| 5094 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 5095 | if (ret <= 0) { |
| 5096 | /* handshake may have not been completed, let's find why */ |
| 5097 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5098 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5099 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5100 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 5101 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5102 | __conn_sock_want_send(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5103 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5104 | return 0; |
| 5105 | } |
| 5106 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5107 | /* handshake may have been completed but we have |
| 5108 | * no more data to read. |
| 5109 | */ |
| 5110 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5111 | ret = 1; |
| 5112 | goto reneg_ok; |
| 5113 | } |
| 5114 | /* SSL handshake needs to read, L4 connection is ready */ |
| 5115 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 5116 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5117 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5118 | __conn_sock_want_recv(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5119 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5120 | return 0; |
| 5121 | } |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5122 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5123 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5124 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5125 | return 0; |
| 5126 | } |
| 5127 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5128 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5129 | /* if errno is null, then connection was successfully established */ |
| 5130 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5131 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5132 | if (!conn->err_code) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5133 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 5134 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5135 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5136 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 5137 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5138 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 5139 | empty_handshake = state == TLS_ST_BEFORE; |
| 5140 | #else |
| 5141 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
| 5142 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5143 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5144 | if (!errno) { |
| 5145 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5146 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5147 | else |
| 5148 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5149 | } |
| 5150 | else { |
| 5151 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5152 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5153 | else |
| 5154 | conn->err_code = CO_ER_SSL_ABORT; |
| 5155 | } |
| 5156 | } |
| 5157 | else { |
| 5158 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5159 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5160 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5161 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5162 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5163 | #endif |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5164 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5165 | goto out_error; |
| 5166 | } |
| 5167 | else { |
| 5168 | /* Fail on all other handshake errors */ |
| 5169 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5170 | * buffer, causing an RST to be emitted upon close() on |
| 5171 | * TCP sockets. We first try to drain possibly pending |
| 5172 | * data to avoid this as much as possible. |
| 5173 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5174 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5175 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5176 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 5177 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5178 | goto out_error; |
| 5179 | } |
| 5180 | } |
| 5181 | /* read some data: consider handshake completed */ |
| 5182 | goto reneg_ok; |
| 5183 | } |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5184 | ret = SSL_do_handshake(conn->xprt_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5185 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5186 | if (ret != 1) { |
| 5187 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5188 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5189 | |
| 5190 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5191 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 5192 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5193 | __conn_sock_want_send(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5194 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5195 | return 0; |
| 5196 | } |
| 5197 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5198 | /* SSL handshake needs to read, L4 connection is ready */ |
| 5199 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 5200 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5201 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5202 | __conn_sock_want_recv(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5203 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5204 | return 0; |
| 5205 | } |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5206 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5207 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5208 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5209 | return 0; |
| 5210 | } |
| 5211 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5212 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5213 | /* if errno is null, then connection was successfully established */ |
| 5214 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5215 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5216 | if (!conn->err_code) { |
| 5217 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 5218 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5219 | #else |
| 5220 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 5221 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5222 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 5223 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5224 | #else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5225 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5226 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5227 | if (empty_handshake) { |
| 5228 | if (!errno) { |
| 5229 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5230 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5231 | else |
| 5232 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5233 | } |
| 5234 | else { |
| 5235 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5236 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5237 | else |
| 5238 | conn->err_code = CO_ER_SSL_ABORT; |
| 5239 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5240 | } |
| 5241 | else { |
| 5242 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5243 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5244 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5245 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5246 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5247 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5248 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5249 | goto out_error; |
| 5250 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5251 | else { |
| 5252 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5253 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5254 | * buffer, causing an RST to be emitted upon close() on |
| 5255 | * TCP sockets. We first try to drain possibly pending |
| 5256 | * data to avoid this as much as possible. |
| 5257 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5258 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5259 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5260 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 5261 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5262 | goto out_error; |
| 5263 | } |
| 5264 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5265 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5266 | else { |
| 5267 | /* |
| 5268 | * If the server refused the early data, we have to send a |
| 5269 | * 425 to the client, as we no longer have the data to sent |
| 5270 | * them again. |
| 5271 | */ |
| 5272 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
| 5273 | if (SSL_get_early_data_status(conn->xprt_ctx) == SSL_EARLY_DATA_REJECTED) { |
| 5274 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5275 | goto out_error; |
| 5276 | } |
| 5277 | } |
| 5278 | } |
| 5279 | #endif |
| 5280 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5281 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5282 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5283 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5284 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5285 | /* ASYNC engine API doesn't support moving read/write |
| 5286 | * buffers. So we disable ASYNC mode right after |
| 5287 | * the handshake to avoid buffer oveflows. |
| 5288 | */ |
| 5289 | if (global_ssl.async) |
| 5290 | SSL_clear_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5291 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5292 | /* Handshake succeeded */ |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5293 | if (!SSL_session_reused(conn->xprt_ctx)) { |
| 5294 | if (objt_server(conn->target)) { |
| 5295 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5296 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5297 | 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] | 5298 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5299 | else { |
| 5300 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5301 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5302 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5303 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5304 | } |
| 5305 | |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5306 | #ifdef OPENSSL_IS_BORINGSSL |
| 5307 | if ((conn->flags & CO_FL_EARLY_SSL_HS) && !SSL_in_early_data(conn->xprt_ctx)) |
| 5308 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5309 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5310 | /* The connection is now established at both layers, it's time to leave */ |
| 5311 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5312 | return 1; |
| 5313 | |
| 5314 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5315 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5316 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5317 | ERR_clear_error(); |
| 5318 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5319 | /* free resumed session if exists */ |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5320 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5321 | free(objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5322 | objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5323 | } |
| 5324 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5325 | /* Fail on all other handshake errors */ |
| 5326 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5327 | if (!conn->err_code) |
| 5328 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5329 | return 0; |
| 5330 | } |
| 5331 | |
| 5332 | /* 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] | 5333 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5334 | * buffer wraps, in which case a second call may be performed. The connection's |
| 5335 | * flags are updated with whatever special event is detected (error, read0, |
| 5336 | * empty). The caller is responsible for taking care of those events and |
| 5337 | * avoiding the call if inappropriate. The function does not call the |
| 5338 | * connection's polling update function, so the caller is responsible for this. |
| 5339 | */ |
| 5340 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 5341 | { |
| 5342 | int ret, done = 0; |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5343 | int try; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5344 | |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5345 | conn_refresh_polling_flags(conn); |
| 5346 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5347 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5348 | goto out_error; |
| 5349 | |
| 5350 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5351 | /* a handshake was requested */ |
| 5352 | return 0; |
| 5353 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5354 | /* let's realign the buffer to optimize I/O */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5355 | if (buffer_empty(buf)) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5356 | buf->p = buf->data; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5357 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5358 | |
| 5359 | /* read the largest possible block. For this, we perform only one call |
| 5360 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 5361 | * in which case we accept to do it once again. A new attempt is made on |
| 5362 | * EINTR too. |
| 5363 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 5364 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5365 | int need_out = 0; |
| 5366 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5367 | /* first check if we have some room after p+i */ |
| 5368 | try = buf->data + buf->size - (buf->p + buf->i); |
| 5369 | /* otherwise continue between data and p-o */ |
| 5370 | if (try <= 0) { |
| 5371 | try = buf->p - (buf->data + buf->o); |
| 5372 | if (try <= 0) |
| 5373 | break; |
| 5374 | } |
| 5375 | if (try > count) |
| 5376 | try = count; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5377 | if (((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_EARLY_SSL_HS) && |
| 5378 | conn->tmp_early_data != -1) { |
| 5379 | *bi_end(buf) = conn->tmp_early_data; |
| 5380 | done++; |
| 5381 | try--; |
| 5382 | count--; |
| 5383 | buf->i++; |
| 5384 | conn->tmp_early_data = -1; |
| 5385 | continue; |
| 5386 | } |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5387 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5388 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5389 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5390 | size_t read_length; |
| 5391 | |
| 5392 | ret = SSL_read_early_data(conn->xprt_ctx, |
| 5393 | bi_end(buf), try, &read_length); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5394 | if (ret == SSL_READ_EARLY_DATA_SUCCESS && |
| 5395 | read_length > 0) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5396 | conn->flags |= CO_FL_EARLY_DATA; |
| 5397 | if (ret == SSL_READ_EARLY_DATA_SUCCESS || |
| 5398 | ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5399 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5400 | /* |
| 5401 | * We're done reading the early data, |
| 5402 | * let's make the handshake |
| 5403 | */ |
| 5404 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5405 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5406 | need_out = 1; |
| 5407 | if (read_length == 0) |
| 5408 | break; |
| 5409 | } |
| 5410 | ret = read_length; |
| 5411 | } |
| 5412 | } else |
| 5413 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5414 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5415 | #ifdef OPENSSL_IS_BORINGSSL |
| 5416 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5417 | if (SSL_in_early_data(conn->xprt_ctx)) { |
| 5418 | if (ret > 0) |
| 5419 | conn->flags |= CO_FL_EARLY_DATA; |
| 5420 | } else { |
Emmanuel Hocdet | cebd796 | 2017-11-27 16:14:40 +0100 | [diff] [blame] | 5421 | conn->flags &= ~(CO_FL_EARLY_SSL_HS); |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5422 | } |
| 5423 | } |
| 5424 | #endif |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5425 | if (conn->flags & CO_FL_ERROR) { |
| 5426 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5427 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5428 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5429 | if (ret > 0) { |
| 5430 | buf->i += ret; |
| 5431 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5432 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5433 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5434 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5435 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5436 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5437 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5438 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5439 | __conn_sock_want_send(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5440 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5441 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5442 | if (global_ssl.async) |
| 5443 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5444 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5445 | break; |
| 5446 | } |
| 5447 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5448 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5449 | /* handshake is running, and it may need to re-enable read */ |
| 5450 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5451 | __conn_sock_want_recv(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5452 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5453 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5454 | if (global_ssl.async) |
| 5455 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5456 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5457 | break; |
| 5458 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5459 | /* we need to poll for retry a read later */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5460 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5461 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5462 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 5463 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5464 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 5465 | * stack before shutting down the connection for |
| 5466 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5467 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 5468 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5469 | /* otherwise it's a real error */ |
| 5470 | goto out_error; |
| 5471 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5472 | if (need_out) |
| 5473 | break; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5474 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5475 | leave: |
| 5476 | conn_cond_update_sock_polling(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5477 | return done; |
| 5478 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5479 | clear_ssl_error: |
| 5480 | /* Clear openssl global errors stack */ |
| 5481 | ssl_sock_dump_errors(conn); |
| 5482 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5483 | read0: |
| 5484 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5485 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5486 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5487 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5488 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5489 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5490 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5491 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5492 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | |
| 5496 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 5497 | * <flags> may contain some CO_SFL_* flags to hint the system about other |
| 5498 | * pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5499 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5500 | * a second call may be performed. The connection's flags are updated with |
| 5501 | * whatever special event is detected (error, empty). The caller is responsible |
| 5502 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5503 | * function does not call the connection's polling update function, so the caller |
| 5504 | * is responsible for this. |
| 5505 | */ |
| 5506 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 5507 | { |
| 5508 | int ret, try, done; |
| 5509 | |
| 5510 | done = 0; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5511 | conn_refresh_polling_flags(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5512 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5513 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5514 | goto out_error; |
| 5515 | |
| 5516 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5517 | /* a handshake was requested */ |
| 5518 | return 0; |
| 5519 | |
| 5520 | /* send the largest possible block. For this we perform only one call |
| 5521 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5522 | * in which case we accept to do it once again. |
| 5523 | */ |
| 5524 | while (buf->o) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5525 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5526 | size_t written_data; |
| 5527 | #endif |
| 5528 | |
Kevin Hester | cad8234 | 2013-05-30 15:12:41 -0700 | [diff] [blame] | 5529 | try = bo_contig_data(buf); |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5530 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 5531 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5532 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5533 | global_ssl.max_record && try > global_ssl.max_record) { |
| 5534 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5535 | } |
| 5536 | else { |
| 5537 | /* we need to keep the information about the fact that |
| 5538 | * we're not limiting the upcoming send(), because if it |
| 5539 | * fails, we'll have to retry with at least as many data. |
| 5540 | */ |
| 5541 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 5542 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5543 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5544 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5545 | if (!SSL_is_init_finished(conn->xprt_ctx)) { |
| 5546 | unsigned int max_early; |
| 5547 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5548 | if (objt_listener(conn->target)) |
| 5549 | max_early = SSL_get_max_early_data(conn->xprt_ctx); |
| 5550 | else { |
| 5551 | if (SSL_get0_session(conn->xprt_ctx)) |
| 5552 | max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(conn->xprt_ctx)); |
| 5553 | else |
| 5554 | max_early = 0; |
| 5555 | } |
| 5556 | |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 5557 | if (try + conn->sent_early_data > max_early) { |
| 5558 | try -= (try + conn->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5559 | if (try <= 0) { |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 5560 | if (!(conn->flags & CO_FL_EARLY_SSL_HS)) |
| 5561 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5562 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5563 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5564 | } |
| 5565 | ret = SSL_write_early_data(conn->xprt_ctx, bo_ptr(buf), try, &written_data); |
| 5566 | if (ret == 1) { |
| 5567 | ret = written_data; |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 5568 | conn->sent_early_data += ret; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5569 | if (objt_server(conn->target)) { |
| 5570 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5571 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA; |
| 5572 | } |
| 5573 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5574 | } |
| 5575 | |
| 5576 | } else |
| 5577 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5578 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5579 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5580 | if (conn->flags & CO_FL_ERROR) { |
| 5581 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5582 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5583 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5584 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5585 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
| 5586 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5587 | buf->o -= ret; |
| 5588 | done += ret; |
| 5589 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 5590 | if (likely(buffer_empty(buf))) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5591 | /* optimize data alignment in the buffer */ |
| 5592 | buf->p = buf->data; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5593 | } |
| 5594 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5595 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5596 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5597 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5598 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5599 | /* handshake is running, and it may need to re-enable write */ |
| 5600 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5601 | __conn_sock_want_send(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5602 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5603 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5604 | if (global_ssl.async) |
| 5605 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5606 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5607 | break; |
| 5608 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5609 | /* we need to poll to retry a write later */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5610 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5611 | break; |
| 5612 | } |
| 5613 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5614 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5615 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5616 | __conn_sock_want_recv(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5617 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5618 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5619 | if (global_ssl.async) |
| 5620 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5621 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5622 | break; |
| 5623 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5624 | goto out_error; |
| 5625 | } |
| 5626 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5627 | leave: |
| 5628 | conn_cond_update_sock_polling(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5629 | return done; |
| 5630 | |
| 5631 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5632 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5633 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5634 | ERR_clear_error(); |
| 5635 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5636 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5637 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5638 | } |
| 5639 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5640 | static void ssl_sock_close(struct connection *conn) { |
| 5641 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5642 | if (conn->xprt_ctx) { |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5643 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5644 | if (global_ssl.async) { |
| 5645 | OSSL_ASYNC_FD all_fd[32], afd; |
| 5646 | size_t num_all_fds = 0; |
| 5647 | int i; |
| 5648 | |
| 5649 | SSL_get_all_async_fds(conn->xprt_ctx, NULL, &num_all_fds); |
| 5650 | if (num_all_fds > 32) { |
| 5651 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 5652 | return; |
| 5653 | } |
| 5654 | |
| 5655 | SSL_get_all_async_fds(conn->xprt_ctx, all_fd, &num_all_fds); |
| 5656 | |
| 5657 | /* If an async job is pending, we must try to |
| 5658 | to catch the end using polling before calling |
| 5659 | SSL_free */ |
| 5660 | if (num_all_fds && SSL_waiting_for_async(conn->xprt_ctx)) { |
| 5661 | for (i=0 ; i < num_all_fds ; i++) { |
| 5662 | /* switch on an handler designed to |
| 5663 | * handle the SSL_free |
| 5664 | */ |
| 5665 | afd = all_fd[i]; |
| 5666 | fdtab[afd].iocb = ssl_async_fd_free; |
| 5667 | fdtab[afd].owner = conn->xprt_ctx; |
| 5668 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 5669 | /* To ensure that the fd cache won't be used |
| 5670 | * and we'll catch a real RD event. |
| 5671 | */ |
| 5672 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5673 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5674 | conn->xprt_ctx = NULL; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5675 | HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5676 | return; |
| 5677 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5678 | /* Else we can remove the fds from the fdtab |
| 5679 | * and call SSL_free. |
| 5680 | * note: we do a fd_remove and not a delete |
| 5681 | * because the fd is owned by the engine. |
| 5682 | * the engine is responsible to close |
| 5683 | */ |
| 5684 | for (i=0 ; i < num_all_fds ; i++) |
| 5685 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5686 | } |
| 5687 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5688 | SSL_free(conn->xprt_ctx); |
| 5689 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5690 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5691 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5692 | } |
| 5693 | |
| 5694 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 5695 | * any case, flags the connection as reusable if no handshake was in progress. |
| 5696 | */ |
| 5697 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 5698 | { |
| 5699 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5700 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5701 | if (!clean) |
| 5702 | /* don't sent notify on SSL_shutdown */ |
Willy Tarreau | e3cc3a3 | 2017-02-13 11:12:29 +0100 | [diff] [blame] | 5703 | SSL_set_quiet_shutdown(conn->xprt_ctx, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5704 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5705 | if (SSL_shutdown(conn->xprt_ctx) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5706 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5707 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5708 | ERR_clear_error(); |
| 5709 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5710 | } |
| 5711 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5712 | /* used for ppv2 pkey alog (can be used for logging) */ |
| 5713 | int ssl_sock_get_pkey_algo(struct connection *conn, struct chunk *out) |
| 5714 | { |
| 5715 | struct pkey_info *pkinfo; |
| 5716 | int bits = 0; |
| 5717 | int sig = TLSEXT_signature_anonymous; |
| 5718 | int len = -1; |
| 5719 | |
| 5720 | if (!ssl_sock_is_ssl(conn)) |
| 5721 | return 0; |
| 5722 | |
| 5723 | pkinfo = SSL_get_ex_data(conn->xprt_ctx, ssl_pkey_info_index); |
| 5724 | if (pkinfo) { |
| 5725 | sig = pkinfo->sig; |
| 5726 | bits = pkinfo->bits; |
| 5727 | } else { |
| 5728 | /* multicert and generated cert have no pkey info */ |
| 5729 | X509 *crt; |
| 5730 | EVP_PKEY *pkey; |
| 5731 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5732 | if (!crt) |
| 5733 | return 0; |
| 5734 | pkey = X509_get_pubkey(crt); |
| 5735 | if (pkey) { |
| 5736 | bits = EVP_PKEY_bits(pkey); |
| 5737 | switch(EVP_PKEY_base_id(pkey)) { |
| 5738 | case EVP_PKEY_RSA: |
| 5739 | sig = TLSEXT_signature_rsa; |
| 5740 | break; |
| 5741 | case EVP_PKEY_EC: |
| 5742 | sig = TLSEXT_signature_ecdsa; |
| 5743 | break; |
| 5744 | case EVP_PKEY_DSA: |
| 5745 | sig = TLSEXT_signature_dsa; |
| 5746 | break; |
| 5747 | } |
| 5748 | EVP_PKEY_free(pkey); |
| 5749 | } |
| 5750 | } |
| 5751 | |
| 5752 | switch(sig) { |
| 5753 | case TLSEXT_signature_rsa: |
| 5754 | len = chunk_printf(out, "RSA%d", bits); |
| 5755 | break; |
| 5756 | case TLSEXT_signature_ecdsa: |
| 5757 | len = chunk_printf(out, "EC%d", bits); |
| 5758 | break; |
| 5759 | case TLSEXT_signature_dsa: |
| 5760 | len = chunk_printf(out, "DSA%d", bits); |
| 5761 | break; |
| 5762 | default: |
| 5763 | return 0; |
| 5764 | } |
| 5765 | if (len < 0) |
| 5766 | return 0; |
| 5767 | return 1; |
| 5768 | } |
| 5769 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 5770 | /* used for ppv2 cert signature (can be used for logging) */ |
| 5771 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 5772 | { |
| 5773 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 5774 | X509 *crt; |
| 5775 | |
| 5776 | if (!ssl_sock_is_ssl(conn)) |
| 5777 | return NULL; |
| 5778 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5779 | if (!crt) |
| 5780 | return NULL; |
| 5781 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 5782 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 5783 | } |
| 5784 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 5785 | /* used for ppv2 authority */ |
| 5786 | const char *ssl_sock_get_sni(struct connection *conn) |
| 5787 | { |
| 5788 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5789 | if (!ssl_sock_is_ssl(conn)) |
| 5790 | return NULL; |
| 5791 | return SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 5792 | #else |
| 5793 | return 0; |
| 5794 | #endif |
| 5795 | } |
| 5796 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5797 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5798 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 5799 | { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5800 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5801 | return NULL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5802 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5803 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 5804 | } |
| 5805 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5806 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5807 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 5808 | { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5809 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5810 | return NULL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5811 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5812 | return SSL_get_version(conn->xprt_ctx); |
| 5813 | } |
| 5814 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5815 | /* Extract a serial from a cert, and copy it to a chunk. |
| 5816 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 5817 | * -1 if output is not large enough. |
| 5818 | */ |
| 5819 | static int |
| 5820 | ssl_sock_get_serial(X509 *crt, struct chunk *out) |
| 5821 | { |
| 5822 | ASN1_INTEGER *serial; |
| 5823 | |
| 5824 | serial = X509_get_serialNumber(crt); |
| 5825 | if (!serial) |
| 5826 | return 0; |
| 5827 | |
| 5828 | if (out->size < serial->length) |
| 5829 | return -1; |
| 5830 | |
| 5831 | memcpy(out->str, serial->data, serial->length); |
| 5832 | out->len = serial->length; |
| 5833 | return 1; |
| 5834 | } |
| 5835 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5836 | /* Extract a cert to der, and copy it to a chunk. |
| 5837 | * Returns 1 if cert is found and copied, 0 on der convertion failure and |
| 5838 | * -1 if output is not large enough. |
| 5839 | */ |
| 5840 | static int |
| 5841 | ssl_sock_crt2der(X509 *crt, struct chunk *out) |
| 5842 | { |
| 5843 | int len; |
| 5844 | unsigned char *p = (unsigned char *)out->str;; |
| 5845 | |
| 5846 | len =i2d_X509(crt, NULL); |
| 5847 | if (len <= 0) |
| 5848 | return 1; |
| 5849 | |
| 5850 | if (out->size < len) |
| 5851 | return -1; |
| 5852 | |
| 5853 | i2d_X509(crt,&p); |
| 5854 | out->len = len; |
| 5855 | return 1; |
| 5856 | } |
| 5857 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5858 | |
| 5859 | /* Copy Date in ASN1_UTCTIME format in struct chunk out. |
| 5860 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 5861 | * and -1 if output is not large enough. |
| 5862 | */ |
| 5863 | static int |
| 5864 | ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out) |
| 5865 | { |
| 5866 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 5867 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 5868 | |
| 5869 | if (gentm->length < 12) |
| 5870 | return 0; |
| 5871 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 5872 | return 0; |
| 5873 | if (out->size < gentm->length-2) |
| 5874 | return -1; |
| 5875 | |
| 5876 | memcpy(out->str, gentm->data+2, gentm->length-2); |
| 5877 | out->len = gentm->length-2; |
| 5878 | return 1; |
| 5879 | } |
| 5880 | else if (tm->type == V_ASN1_UTCTIME) { |
| 5881 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 5882 | |
| 5883 | if (utctm->length < 10) |
| 5884 | return 0; |
| 5885 | if (utctm->data[0] >= 0x35) |
| 5886 | return 0; |
| 5887 | if (out->size < utctm->length) |
| 5888 | return -1; |
| 5889 | |
| 5890 | memcpy(out->str, utctm->data, utctm->length); |
| 5891 | out->len = utctm->length; |
| 5892 | return 1; |
| 5893 | } |
| 5894 | |
| 5895 | return 0; |
| 5896 | } |
| 5897 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5898 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 5899 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 5900 | */ |
| 5901 | static int |
| 5902 | ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out) |
| 5903 | { |
| 5904 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5905 | ASN1_OBJECT *obj; |
| 5906 | ASN1_STRING *data; |
| 5907 | const unsigned char *data_ptr; |
| 5908 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5909 | int i, j, n; |
| 5910 | int cur = 0; |
| 5911 | const char *s; |
| 5912 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5913 | int name_count; |
| 5914 | |
| 5915 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5916 | |
| 5917 | out->len = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5918 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5919 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5920 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5921 | else |
| 5922 | j = i; |
| 5923 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5924 | ne = X509_NAME_get_entry(a, j); |
| 5925 | obj = X509_NAME_ENTRY_get_object(ne); |
| 5926 | data = X509_NAME_ENTRY_get_data(ne); |
| 5927 | data_ptr = ASN1_STRING_get0_data(data); |
| 5928 | data_len = ASN1_STRING_length(data); |
| 5929 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5930 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5931 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5932 | s = tmp; |
| 5933 | } |
| 5934 | |
| 5935 | if (chunk_strcasecmp(entry, s) != 0) |
| 5936 | continue; |
| 5937 | |
| 5938 | if (pos < 0) |
| 5939 | cur--; |
| 5940 | else |
| 5941 | cur++; |
| 5942 | |
| 5943 | if (cur != pos) |
| 5944 | continue; |
| 5945 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5946 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5947 | return -1; |
| 5948 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5949 | memcpy(out->str, data_ptr, data_len); |
| 5950 | out->len = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5951 | return 1; |
| 5952 | } |
| 5953 | |
| 5954 | return 0; |
| 5955 | |
| 5956 | } |
| 5957 | |
| 5958 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 5959 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 5960 | */ |
| 5961 | static int |
| 5962 | ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out) |
| 5963 | { |
| 5964 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5965 | ASN1_OBJECT *obj; |
| 5966 | ASN1_STRING *data; |
| 5967 | const unsigned char *data_ptr; |
| 5968 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5969 | int i, n, ln; |
| 5970 | int l = 0; |
| 5971 | const char *s; |
| 5972 | char *p; |
| 5973 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5974 | int name_count; |
| 5975 | |
| 5976 | |
| 5977 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5978 | |
| 5979 | out->len = 0; |
| 5980 | p = out->str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5981 | for (i = 0; i < name_count; i++) { |
| 5982 | ne = X509_NAME_get_entry(a, i); |
| 5983 | obj = X509_NAME_ENTRY_get_object(ne); |
| 5984 | data = X509_NAME_ENTRY_get_data(ne); |
| 5985 | data_ptr = ASN1_STRING_get0_data(data); |
| 5986 | data_len = ASN1_STRING_length(data); |
| 5987 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5988 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5989 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5990 | s = tmp; |
| 5991 | } |
| 5992 | ln = strlen(s); |
| 5993 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5994 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5995 | if (l > out->size) |
| 5996 | return -1; |
| 5997 | out->len = l; |
| 5998 | |
| 5999 | *(p++)='/'; |
| 6000 | memcpy(p, s, ln); |
| 6001 | p += ln; |
| 6002 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6003 | memcpy(p, data_ptr, data_len); |
| 6004 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6005 | } |
| 6006 | |
| 6007 | if (!out->len) |
| 6008 | return 0; |
| 6009 | |
| 6010 | return 1; |
| 6011 | } |
| 6012 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6013 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 6014 | * to disable SNI. |
| 6015 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6016 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 6017 | { |
| 6018 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6019 | char *prev_name; |
| 6020 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6021 | if (!ssl_sock_is_ssl(conn)) |
| 6022 | return; |
| 6023 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6024 | /* if the SNI changes, we must destroy the reusable context so that a |
| 6025 | * new connection will present a new SNI. As an optimization we could |
| 6026 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 6027 | * server. |
| 6028 | */ |
| 6029 | prev_name = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 6030 | if ((!prev_name && hostname) || |
| 6031 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
| 6032 | SSL_set_session(conn->xprt_ctx, NULL); |
| 6033 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6034 | SSL_set_tlsext_host_name(conn->xprt_ctx, hostname); |
| 6035 | #endif |
| 6036 | } |
| 6037 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6038 | /* Extract peer certificate's common name into the chunk dest |
| 6039 | * Returns |
| 6040 | * the len of the extracted common name |
| 6041 | * or 0 if no CN found in DN |
| 6042 | * or -1 on error case (i.e. no peer certificate) |
| 6043 | */ |
| 6044 | 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] | 6045 | { |
| 6046 | X509 *crt = NULL; |
| 6047 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6048 | const char find_cn[] = "CN"; |
| 6049 | const struct chunk find_cn_chunk = { |
| 6050 | .str = (char *)&find_cn, |
| 6051 | .len = sizeof(find_cn)-1 |
| 6052 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6053 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6054 | |
| 6055 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6056 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6057 | |
| 6058 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6059 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6060 | if (!crt) |
| 6061 | goto out; |
| 6062 | |
| 6063 | name = X509_get_subject_name(crt); |
| 6064 | if (!name) |
| 6065 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6066 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6067 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6068 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6069 | if (crt) |
| 6070 | X509_free(crt); |
| 6071 | |
| 6072 | return result; |
| 6073 | } |
| 6074 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6075 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6076 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6077 | { |
| 6078 | X509 *crt = NULL; |
| 6079 | |
| 6080 | if (!ssl_sock_is_ssl(conn)) |
| 6081 | return 0; |
| 6082 | |
| 6083 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6084 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6085 | if (!crt) |
| 6086 | return 0; |
| 6087 | |
| 6088 | X509_free(crt); |
| 6089 | return 1; |
| 6090 | } |
| 6091 | |
| 6092 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6093 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6094 | { |
| 6095 | if (!ssl_sock_is_ssl(conn)) |
| 6096 | return 0; |
| 6097 | |
| 6098 | return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
| 6099 | } |
| 6100 | |
| 6101 | /* returns result from SSL verify */ |
| 6102 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6103 | { |
| 6104 | if (!ssl_sock_is_ssl(conn)) |
| 6105 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 6106 | |
| 6107 | return (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
| 6108 | } |
| 6109 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6110 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6111 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6112 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6113 | * freed by the caller. NPN is also checked if available since older versions |
| 6114 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6115 | */ |
| 6116 | static int ssl_sock_get_alpn(const struct connection *conn, const char **str, int *len) |
| 6117 | { |
| 6118 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6119 | return 0; |
| 6120 | |
| 6121 | *str = NULL; |
| 6122 | |
| 6123 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 6124 | SSL_get0_alpn_selected(conn->xprt_ctx, (const unsigned char **)str, (unsigned *)len); |
| 6125 | if (*str) |
| 6126 | return 1; |
| 6127 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6128 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6129 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, (const unsigned char **)str, (unsigned *)len); |
| 6130 | if (*str) |
| 6131 | return 1; |
| 6132 | #endif |
| 6133 | return 0; |
| 6134 | } |
| 6135 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6136 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 6137 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6138 | static int |
| 6139 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6140 | { |
| 6141 | struct connection *conn; |
| 6142 | |
| 6143 | conn = objt_conn(smp->sess->origin); |
| 6144 | if (!conn || conn->xprt != &ssl_sock) |
| 6145 | return 0; |
| 6146 | |
| 6147 | smp->flags = 0; |
| 6148 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 6149 | smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) && |
| 6150 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6151 | |
| 6152 | return 1; |
| 6153 | } |
| 6154 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6155 | /* boolean, returns true if client cert was present */ |
| 6156 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6157 | 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] | 6158 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6159 | struct connection *conn; |
| 6160 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6161 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6162 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6163 | return 0; |
| 6164 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6165 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6166 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6167 | return 0; |
| 6168 | } |
| 6169 | |
| 6170 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6171 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6172 | 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] | 6173 | |
| 6174 | return 1; |
| 6175 | } |
| 6176 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6177 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 6178 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6179 | * should be use. |
| 6180 | */ |
| 6181 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6182 | 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] | 6183 | { |
| 6184 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 6185 | X509 *crt = NULL; |
| 6186 | int ret = 0; |
| 6187 | struct chunk *smp_trash; |
| 6188 | struct connection *conn; |
| 6189 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6190 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6191 | if (!conn || conn->xprt != &ssl_sock) |
| 6192 | return 0; |
| 6193 | |
| 6194 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 6195 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6196 | return 0; |
| 6197 | } |
| 6198 | |
| 6199 | if (cert_peer) |
| 6200 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6201 | else |
| 6202 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 6203 | |
| 6204 | if (!crt) |
| 6205 | goto out; |
| 6206 | |
| 6207 | smp_trash = get_trash_chunk(); |
| 6208 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 6209 | goto out; |
| 6210 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6211 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6212 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6213 | ret = 1; |
| 6214 | out: |
| 6215 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6216 | if (cert_peer && crt) |
| 6217 | X509_free(crt); |
| 6218 | return ret; |
| 6219 | } |
| 6220 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6221 | /* binary, returns serial of certificate in a binary chunk. |
| 6222 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6223 | * should be use. |
| 6224 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6225 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6226 | 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] | 6227 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6228 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6229 | X509 *crt = NULL; |
| 6230 | int ret = 0; |
| 6231 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6232 | struct connection *conn; |
| 6233 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6234 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6235 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6236 | return 0; |
| 6237 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6238 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6239 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6240 | return 0; |
| 6241 | } |
| 6242 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6243 | if (cert_peer) |
| 6244 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6245 | else |
| 6246 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 6247 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6248 | if (!crt) |
| 6249 | goto out; |
| 6250 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6251 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6252 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 6253 | goto out; |
| 6254 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6255 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6256 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6257 | ret = 1; |
| 6258 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6259 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6260 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6261 | X509_free(crt); |
| 6262 | return ret; |
| 6263 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6264 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6265 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 6266 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6267 | * should be use. |
| 6268 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6269 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6270 | 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] | 6271 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6272 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6273 | X509 *crt = NULL; |
| 6274 | const EVP_MD *digest; |
| 6275 | int ret = 0; |
| 6276 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6277 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6278 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6279 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6280 | if (!conn || conn->xprt != &ssl_sock) |
| 6281 | return 0; |
| 6282 | |
| 6283 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6284 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6285 | return 0; |
| 6286 | } |
| 6287 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6288 | if (cert_peer) |
| 6289 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6290 | else |
| 6291 | crt = SSL_get_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6292 | if (!crt) |
| 6293 | goto out; |
| 6294 | |
| 6295 | smp_trash = get_trash_chunk(); |
| 6296 | digest = EVP_sha1(); |
| 6297 | X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len); |
| 6298 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6299 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6300 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6301 | ret = 1; |
| 6302 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6303 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6304 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6305 | X509_free(crt); |
| 6306 | return ret; |
| 6307 | } |
| 6308 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6309 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 6310 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6311 | * should be use. |
| 6312 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6313 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6314 | 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] | 6315 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6316 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6317 | X509 *crt = NULL; |
| 6318 | int ret = 0; |
| 6319 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6320 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6321 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6322 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6323 | if (!conn || conn->xprt != &ssl_sock) |
| 6324 | return 0; |
| 6325 | |
| 6326 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6327 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6328 | return 0; |
| 6329 | } |
| 6330 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6331 | if (cert_peer) |
| 6332 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6333 | else |
| 6334 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6335 | if (!crt) |
| 6336 | goto out; |
| 6337 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6338 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6339 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 6340 | goto out; |
| 6341 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6342 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6343 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6344 | ret = 1; |
| 6345 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6346 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6347 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6348 | X509_free(crt); |
| 6349 | return ret; |
| 6350 | } |
| 6351 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6352 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 6353 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6354 | * should be use. |
| 6355 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6356 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6357 | 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] | 6358 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6359 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6360 | X509 *crt = NULL; |
| 6361 | X509_NAME *name; |
| 6362 | int ret = 0; |
| 6363 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6364 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6365 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6366 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6367 | if (!conn || conn->xprt != &ssl_sock) |
| 6368 | return 0; |
| 6369 | |
| 6370 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6371 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6372 | return 0; |
| 6373 | } |
| 6374 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6375 | if (cert_peer) |
| 6376 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6377 | else |
| 6378 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6379 | if (!crt) |
| 6380 | goto out; |
| 6381 | |
| 6382 | name = X509_get_issuer_name(crt); |
| 6383 | if (!name) |
| 6384 | goto out; |
| 6385 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6386 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6387 | if (args && args[0].type == ARGT_STR) { |
| 6388 | int pos = 1; |
| 6389 | |
| 6390 | if (args[1].type == ARGT_SINT) |
| 6391 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6392 | |
| 6393 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6394 | goto out; |
| 6395 | } |
| 6396 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6397 | goto out; |
| 6398 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6399 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6400 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6401 | ret = 1; |
| 6402 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6403 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6404 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6405 | X509_free(crt); |
| 6406 | return ret; |
| 6407 | } |
| 6408 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6409 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 6410 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6411 | * should be use. |
| 6412 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6413 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6414 | 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] | 6415 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6416 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6417 | X509 *crt = NULL; |
| 6418 | int ret = 0; |
| 6419 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6420 | struct connection *conn; |
| 6421 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6422 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6423 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6424 | return 0; |
| 6425 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6426 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6427 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6428 | return 0; |
| 6429 | } |
| 6430 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6431 | if (cert_peer) |
| 6432 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6433 | else |
| 6434 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6435 | if (!crt) |
| 6436 | goto out; |
| 6437 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6438 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6439 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 6440 | goto out; |
| 6441 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6442 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6443 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6444 | ret = 1; |
| 6445 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6446 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6447 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6448 | X509_free(crt); |
| 6449 | return ret; |
| 6450 | } |
| 6451 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6452 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 6453 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6454 | * should be use. |
| 6455 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6456 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6457 | 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] | 6458 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6459 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6460 | X509 *crt = NULL; |
| 6461 | X509_NAME *name; |
| 6462 | int ret = 0; |
| 6463 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6464 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6465 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6466 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6467 | if (!conn || conn->xprt != &ssl_sock) |
| 6468 | return 0; |
| 6469 | |
| 6470 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6471 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6472 | return 0; |
| 6473 | } |
| 6474 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6475 | if (cert_peer) |
| 6476 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6477 | else |
| 6478 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6479 | if (!crt) |
| 6480 | goto out; |
| 6481 | |
| 6482 | name = X509_get_subject_name(crt); |
| 6483 | if (!name) |
| 6484 | goto out; |
| 6485 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6486 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6487 | if (args && args[0].type == ARGT_STR) { |
| 6488 | int pos = 1; |
| 6489 | |
| 6490 | if (args[1].type == ARGT_SINT) |
| 6491 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6492 | |
| 6493 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6494 | goto out; |
| 6495 | } |
| 6496 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6497 | goto out; |
| 6498 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6499 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6500 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6501 | ret = 1; |
| 6502 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6503 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6504 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6505 | X509_free(crt); |
| 6506 | return ret; |
| 6507 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6508 | |
| 6509 | /* integer, returns true if current session use a client certificate */ |
| 6510 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6511 | 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] | 6512 | { |
| 6513 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6514 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6515 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6516 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6517 | if (!conn || conn->xprt != &ssl_sock) |
| 6518 | return 0; |
| 6519 | |
| 6520 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6521 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6522 | return 0; |
| 6523 | } |
| 6524 | |
| 6525 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6526 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6527 | if (crt) { |
| 6528 | X509_free(crt); |
| 6529 | } |
| 6530 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6531 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6532 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6533 | return 1; |
| 6534 | } |
| 6535 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6536 | /* integer, returns the certificate version |
| 6537 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6538 | * should be use. |
| 6539 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6540 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6541 | 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] | 6542 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6543 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6544 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6545 | struct connection *conn; |
| 6546 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6547 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6548 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6549 | return 0; |
| 6550 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6551 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6552 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6553 | return 0; |
| 6554 | } |
| 6555 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6556 | if (cert_peer) |
| 6557 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6558 | else |
| 6559 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6560 | if (!crt) |
| 6561 | return 0; |
| 6562 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6563 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6564 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6565 | if (cert_peer) |
| 6566 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6567 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6568 | |
| 6569 | return 1; |
| 6570 | } |
| 6571 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6572 | /* string, returns the certificate's signature algorithm. |
| 6573 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6574 | * should be use. |
| 6575 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6576 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6577 | 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] | 6578 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6579 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6580 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6581 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6582 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6583 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6584 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6585 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6586 | if (!conn || conn->xprt != &ssl_sock) |
| 6587 | return 0; |
| 6588 | |
| 6589 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6590 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6591 | return 0; |
| 6592 | } |
| 6593 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6594 | if (cert_peer) |
| 6595 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6596 | else |
| 6597 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6598 | if (!crt) |
| 6599 | return 0; |
| 6600 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6601 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6602 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6603 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6604 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 6605 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6606 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6607 | if (cert_peer) |
| 6608 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6609 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 6610 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6611 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6612 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6613 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6614 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6615 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6616 | if (cert_peer) |
| 6617 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6618 | |
| 6619 | return 1; |
| 6620 | } |
| 6621 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6622 | /* string, returns the certificate's key algorithm. |
| 6623 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6624 | * should be use. |
| 6625 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6626 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6627 | 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] | 6628 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6629 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6630 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6631 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6632 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6633 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6634 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6635 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6636 | if (!conn || conn->xprt != &ssl_sock) |
| 6637 | return 0; |
| 6638 | |
| 6639 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6640 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6641 | return 0; |
| 6642 | } |
| 6643 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6644 | if (cert_peer) |
| 6645 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6646 | else |
| 6647 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6648 | if (!crt) |
| 6649 | return 0; |
| 6650 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6651 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 6652 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6653 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6654 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 6655 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6656 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6657 | if (cert_peer) |
| 6658 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6659 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 6660 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6661 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6662 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6663 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6664 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6665 | if (cert_peer) |
| 6666 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6667 | |
| 6668 | return 1; |
| 6669 | } |
| 6670 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6671 | /* boolean, returns true if front conn. transport layer is SSL. |
| 6672 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6673 | * char is 'b'. |
| 6674 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6675 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6676 | 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] | 6677 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6678 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6679 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6680 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6681 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6682 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6683 | return 1; |
| 6684 | } |
| 6685 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6686 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6687 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6688 | 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] | 6689 | { |
| 6690 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6691 | struct connection *conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6692 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6693 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6694 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6695 | conn->xprt_ctx && |
| 6696 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6697 | return 1; |
| 6698 | #else |
| 6699 | return 0; |
| 6700 | #endif |
| 6701 | } |
| 6702 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 6703 | /* boolean, returns true if client session has been resumed. |
| 6704 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6705 | * char is 'b'. |
| 6706 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6707 | static int |
| 6708 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6709 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 6710 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6711 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 6712 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6713 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6714 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6715 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6716 | conn->xprt_ctx && |
| 6717 | SSL_session_reused(conn->xprt_ctx); |
| 6718 | return 1; |
| 6719 | } |
| 6720 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6721 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 6722 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6723 | * char is 'b'. |
| 6724 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6725 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6726 | 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] | 6727 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6728 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6729 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6730 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6731 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6732 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6733 | return 0; |
| 6734 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6735 | smp->data.u.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
| 6736 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6737 | return 0; |
| 6738 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6739 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6740 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6741 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6742 | |
| 6743 | return 1; |
| 6744 | } |
| 6745 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6746 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 6747 | * is SSL. |
| 6748 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6749 | * char is 'b'. |
| 6750 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6751 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6752 | 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] | 6753 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6754 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6755 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6756 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6757 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6758 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6759 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6760 | return 0; |
| 6761 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 6762 | if (!SSL_get_cipher_bits(conn->xprt_ctx, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6763 | return 0; |
| 6764 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6765 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6766 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6767 | |
| 6768 | return 1; |
| 6769 | } |
| 6770 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6771 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 6772 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6773 | * char is 'b'. |
| 6774 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6775 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6776 | 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] | 6777 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6778 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6779 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6780 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6781 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6782 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6783 | return 0; |
| 6784 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6785 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
| 6786 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6787 | return 0; |
| 6788 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6789 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6790 | |
| 6791 | return 1; |
| 6792 | } |
| 6793 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6794 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6795 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6796 | 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] | 6797 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6798 | struct connection *conn; |
| 6799 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6800 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6801 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6802 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6803 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6804 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6805 | return 0; |
| 6806 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6807 | smp->data.u.str.str = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6808 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6809 | (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] | 6810 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6811 | if (!smp->data.u.str.str) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6812 | return 0; |
| 6813 | |
| 6814 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6815 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6816 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6817 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6818 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6819 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6820 | 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] | 6821 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6822 | struct connection *conn; |
| 6823 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6824 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6825 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6826 | |
Willy Tarreau | e26bf05 | 2015-05-12 10:30:12 +0200 | [diff] [blame] | 6827 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6828 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6829 | return 0; |
| 6830 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6831 | smp->data.u.str.str = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6832 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6833 | (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] | 6834 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6835 | if (!smp->data.u.str.str) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6836 | return 0; |
| 6837 | |
| 6838 | return 1; |
| 6839 | } |
| 6840 | #endif |
| 6841 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6842 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 6843 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6844 | * char is 'b'. |
| 6845 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6846 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6847 | 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] | 6848 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6849 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6850 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6851 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6852 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6853 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6854 | return 0; |
| 6855 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6856 | smp->data.u.str.str = (char *)SSL_get_version(conn->xprt_ctx); |
| 6857 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6858 | return 0; |
| 6859 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6860 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6861 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6862 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6863 | |
| 6864 | return 1; |
| 6865 | } |
| 6866 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6867 | /* 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] | 6868 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6869 | * char is 'b'. |
| 6870 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6871 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6872 | 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] | 6873 | { |
| 6874 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6875 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6876 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6877 | SSL_SESSION *ssl_sess; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6878 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6879 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6880 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6881 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6882 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6883 | return 0; |
| 6884 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6885 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 6886 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6887 | return 0; |
| 6888 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6889 | smp->data.u.str.str = (char *)SSL_SESSION_get_id(ssl_sess, (unsigned int *)&smp->data.u.str.len); |
| 6890 | if (!smp->data.u.str.str || !smp->data.u.str.len) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6891 | return 0; |
| 6892 | |
| 6893 | return 1; |
| 6894 | #else |
| 6895 | return 0; |
| 6896 | #endif |
| 6897 | } |
| 6898 | |
| 6899 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6900 | 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] | 6901 | { |
| 6902 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6903 | struct connection *conn; |
| 6904 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6905 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6906 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6907 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6908 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6909 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6910 | return 0; |
| 6911 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6912 | smp->data.u.str.str = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 6913 | if (!smp->data.u.str.str) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 6914 | return 0; |
| 6915 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6916 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6917 | return 1; |
| 6918 | #else |
| 6919 | return 0; |
| 6920 | #endif |
| 6921 | } |
| 6922 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6923 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6924 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6925 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6926 | struct connection *conn; |
| 6927 | struct ssl_capture *capture; |
| 6928 | |
| 6929 | conn = objt_conn(smp->sess->origin); |
| 6930 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6931 | return 0; |
| 6932 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6933 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6934 | if (!capture) |
| 6935 | return 0; |
| 6936 | |
| 6937 | smp->flags = SMP_F_CONST; |
| 6938 | smp->data.type = SMP_T_BIN; |
| 6939 | smp->data.u.str.str = capture->ciphersuite; |
| 6940 | smp->data.u.str.len = capture->ciphersuite_len; |
| 6941 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6942 | } |
| 6943 | |
| 6944 | static int |
| 6945 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6946 | { |
| 6947 | struct chunk *data; |
| 6948 | |
| 6949 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 6950 | return 0; |
| 6951 | |
| 6952 | data = get_trash_chunk(); |
| 6953 | dump_binary(data, smp->data.u.str.str, smp->data.u.str.len); |
| 6954 | smp->data.type = SMP_T_BIN; |
| 6955 | smp->data.u.str = *data; |
| 6956 | return 1; |
| 6957 | } |
| 6958 | |
| 6959 | static int |
| 6960 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6961 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6962 | struct connection *conn; |
| 6963 | struct ssl_capture *capture; |
| 6964 | |
| 6965 | conn = objt_conn(smp->sess->origin); |
| 6966 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6967 | return 0; |
| 6968 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6969 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6970 | if (!capture) |
| 6971 | return 0; |
| 6972 | |
| 6973 | smp->data.type = SMP_T_SINT; |
| 6974 | smp->data.u.sint = capture->xxh64; |
| 6975 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6976 | } |
| 6977 | |
| 6978 | static int |
| 6979 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6980 | { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 6981 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6982 | struct chunk *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6983 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6984 | |
| 6985 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 6986 | return 0; |
| 6987 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6988 | data = get_trash_chunk(); |
| 6989 | for (i = 0; i + 1 < smp->data.u.str.len; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 6990 | const char *str; |
| 6991 | const SSL_CIPHER *cipher; |
| 6992 | const unsigned char *bin = (const unsigned char *)smp->data.u.str.str + i; |
| 6993 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 6994 | #if defined(OPENSSL_IS_BORINGSSL) |
| 6995 | cipher = SSL_get_cipher_by_value(id); |
| 6996 | #else |
| 6997 | struct connection *conn = objt_conn(smp->sess->origin); |
| 6998 | cipher = SSL_CIPHER_find(conn->xprt_ctx, bin); |
| 6999 | #endif |
| 7000 | str = SSL_CIPHER_get_name(cipher); |
| 7001 | if (!str || strcmp(str, "(NONE)") == 0) |
| 7002 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7003 | else |
| 7004 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 7005 | } |
| 7006 | smp->data.type = SMP_T_STR; |
| 7007 | smp->data.u.str = *data; |
| 7008 | return 1; |
| 7009 | #else |
| 7010 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 7011 | #endif |
| 7012 | } |
| 7013 | |
| 7014 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7015 | 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] | 7016 | { |
| 7017 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7018 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7019 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7020 | int finished_len; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7021 | struct chunk *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7022 | |
| 7023 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7024 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7025 | return 0; |
| 7026 | |
| 7027 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 7028 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7029 | return 0; |
| 7030 | } |
| 7031 | |
| 7032 | finished_trash = get_trash_chunk(); |
| 7033 | if (!SSL_session_reused(conn->xprt_ctx)) |
| 7034 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 7035 | else |
| 7036 | finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 7037 | |
| 7038 | if (!finished_len) |
| 7039 | return 0; |
| 7040 | |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 7041 | finished_trash->len = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7042 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7043 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7044 | |
| 7045 | return 1; |
| 7046 | #else |
| 7047 | return 0; |
| 7048 | #endif |
| 7049 | } |
| 7050 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7051 | /* 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] | 7052 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7053 | 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] | 7054 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7055 | struct connection *conn; |
| 7056 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7057 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7058 | if (!conn || conn->xprt != &ssl_sock) |
| 7059 | return 0; |
| 7060 | |
| 7061 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7062 | smp->flags = SMP_F_MAY_CHANGE; |
| 7063 | return 0; |
| 7064 | } |
| 7065 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7066 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7067 | 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] | 7068 | smp->flags = 0; |
| 7069 | |
| 7070 | return 1; |
| 7071 | } |
| 7072 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7073 | /* 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] | 7074 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7075 | 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] | 7076 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7077 | struct connection *conn; |
| 7078 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7079 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7080 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7081 | return 0; |
| 7082 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7083 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7084 | smp->flags = SMP_F_MAY_CHANGE; |
| 7085 | return 0; |
| 7086 | } |
| 7087 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7088 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7089 | 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] | 7090 | smp->flags = 0; |
| 7091 | |
| 7092 | return 1; |
| 7093 | } |
| 7094 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7095 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7096 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7097 | 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] | 7098 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7099 | struct connection *conn; |
| 7100 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7101 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7102 | if (!conn || conn->xprt != &ssl_sock) |
| 7103 | return 0; |
| 7104 | |
| 7105 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7106 | smp->flags = SMP_F_MAY_CHANGE; |
| 7107 | return 0; |
| 7108 | } |
| 7109 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7110 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7111 | 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] | 7112 | smp->flags = 0; |
| 7113 | |
| 7114 | return 1; |
| 7115 | } |
| 7116 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7117 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7118 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7119 | 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] | 7120 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7121 | struct connection *conn; |
| 7122 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7123 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7124 | if (!conn || conn->xprt != &ssl_sock) |
| 7125 | return 0; |
| 7126 | |
| 7127 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7128 | smp->flags = SMP_F_MAY_CHANGE; |
| 7129 | return 0; |
| 7130 | } |
| 7131 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7132 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7133 | return 0; |
| 7134 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7135 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7136 | 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] | 7137 | smp->flags = 0; |
| 7138 | |
| 7139 | return 1; |
| 7140 | } |
| 7141 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7142 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7143 | 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] | 7144 | { |
| 7145 | if (!*args[cur_arg + 1]) { |
| 7146 | if (err) |
| 7147 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7148 | return ERR_ALERT | ERR_FATAL; |
| 7149 | } |
| 7150 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7151 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7152 | 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] | 7153 | else |
| 7154 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7155 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7156 | return 0; |
| 7157 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7158 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7159 | { |
| 7160 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7161 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7162 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7163 | /* parse the "ca-sign-file" bind keyword */ |
| 7164 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7165 | { |
| 7166 | if (!*args[cur_arg + 1]) { |
| 7167 | if (err) |
| 7168 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7169 | return ERR_ALERT | ERR_FATAL; |
| 7170 | } |
| 7171 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7172 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7173 | 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] | 7174 | else |
| 7175 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 7176 | |
| 7177 | return 0; |
| 7178 | } |
| 7179 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7180 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7181 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7182 | { |
| 7183 | if (!*args[cur_arg + 1]) { |
| 7184 | if (err) |
| 7185 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 7186 | return ERR_ALERT | ERR_FATAL; |
| 7187 | } |
| 7188 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 7189 | return 0; |
| 7190 | } |
| 7191 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7192 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7193 | 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] | 7194 | { |
| 7195 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7196 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7197 | return ERR_ALERT | ERR_FATAL; |
| 7198 | } |
| 7199 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7200 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7201 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7202 | return 0; |
| 7203 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7204 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7205 | { |
| 7206 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 7207 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7208 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7209 | 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] | 7210 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 7211 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 7212 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7213 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7214 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7215 | return ERR_ALERT | ERR_FATAL; |
| 7216 | } |
| 7217 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7218 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 7219 | 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] | 7220 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 7221 | return ERR_ALERT | ERR_FATAL; |
| 7222 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7223 | 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] | 7224 | if (ssl_sock_load_cert(path, conf, err) > 0) |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7225 | return ERR_ALERT | ERR_FATAL; |
| 7226 | |
| 7227 | return 0; |
| 7228 | } |
| 7229 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 7230 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7231 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7232 | |
| 7233 | return 0; |
| 7234 | } |
| 7235 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7236 | /* parse the "crt-list" bind keyword */ |
| 7237 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7238 | { |
| 7239 | if (!*args[cur_arg + 1]) { |
| 7240 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 7241 | return ERR_ALERT | ERR_FATAL; |
| 7242 | } |
| 7243 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7244 | 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] | 7245 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7246 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 7247 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7248 | |
| 7249 | return 0; |
| 7250 | } |
| 7251 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7252 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7253 | 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] | 7254 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7255 | #ifndef X509_V_FLAG_CRL_CHECK |
| 7256 | if (err) |
| 7257 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 7258 | return ERR_ALERT | ERR_FATAL; |
| 7259 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7260 | if (!*args[cur_arg + 1]) { |
| 7261 | if (err) |
| 7262 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 7263 | return ERR_ALERT | ERR_FATAL; |
| 7264 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7265 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7266 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7267 | 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] | 7268 | else |
| 7269 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7270 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7271 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7272 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7273 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7274 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7275 | { |
| 7276 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7277 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7278 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 7279 | /* parse the "curves" bind keyword keyword */ |
| 7280 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7281 | { |
| 7282 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 7283 | if (!*args[cur_arg + 1]) { |
| 7284 | if (err) |
| 7285 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 7286 | return ERR_ALERT | ERR_FATAL; |
| 7287 | } |
| 7288 | conf->curves = strdup(args[cur_arg + 1]); |
| 7289 | return 0; |
| 7290 | #else |
| 7291 | if (err) |
| 7292 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 7293 | return ERR_ALERT | ERR_FATAL; |
| 7294 | #endif |
| 7295 | } |
| 7296 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7297 | { |
| 7298 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 7299 | } |
| 7300 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7301 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7302 | 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] | 7303 | { |
| 7304 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 7305 | if (err) |
| 7306 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 7307 | return ERR_ALERT | ERR_FATAL; |
| 7308 | #elif defined(OPENSSL_NO_ECDH) |
| 7309 | if (err) |
| 7310 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 7311 | return ERR_ALERT | ERR_FATAL; |
| 7312 | #else |
| 7313 | if (!*args[cur_arg + 1]) { |
| 7314 | if (err) |
| 7315 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 7316 | return ERR_ALERT | ERR_FATAL; |
| 7317 | } |
| 7318 | |
| 7319 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7320 | |
| 7321 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7322 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7323 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7324 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7325 | { |
| 7326 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 7327 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7328 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7329 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7330 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7331 | { |
| 7332 | int code; |
| 7333 | char *p = args[cur_arg + 1]; |
| 7334 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 7335 | |
| 7336 | if (!*p) { |
| 7337 | if (err) |
| 7338 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 7339 | return ERR_ALERT | ERR_FATAL; |
| 7340 | } |
| 7341 | |
| 7342 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 7343 | ignerr = &conf->ca_ignerr; |
| 7344 | |
| 7345 | if (strcmp(p, "all") == 0) { |
| 7346 | *ignerr = ~0ULL; |
| 7347 | return 0; |
| 7348 | } |
| 7349 | |
| 7350 | while (p) { |
| 7351 | code = atoi(p); |
| 7352 | if ((code <= 0) || (code > 63)) { |
| 7353 | if (err) |
| 7354 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 7355 | args[cur_arg], code, args[cur_arg + 1]); |
| 7356 | return ERR_ALERT | ERR_FATAL; |
| 7357 | } |
| 7358 | *ignerr |= 1ULL << code; |
| 7359 | p = strchr(p, ','); |
| 7360 | if (p) |
| 7361 | p++; |
| 7362 | } |
| 7363 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7364 | return 0; |
| 7365 | } |
| 7366 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7367 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 7368 | 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] | 7369 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7370 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7371 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7372 | p = strchr(arg, '-'); |
| 7373 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7374 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7375 | p++; |
| 7376 | if (!strcmp(p, "sslv3")) |
| 7377 | v = CONF_SSLV3; |
| 7378 | else if (!strcmp(p, "tlsv10")) |
| 7379 | v = CONF_TLSV10; |
| 7380 | else if (!strcmp(p, "tlsv11")) |
| 7381 | v = CONF_TLSV11; |
| 7382 | else if (!strcmp(p, "tlsv12")) |
| 7383 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 7384 | else if (!strcmp(p, "tlsv13")) |
| 7385 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7386 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7387 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7388 | if (!strncmp(arg, "no-", 3)) |
| 7389 | methods->flags |= methodVersions[v].flag; |
| 7390 | else if (!strncmp(arg, "force-", 6)) |
| 7391 | methods->min = methods->max = v; |
| 7392 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7393 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7394 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7395 | fail: |
| 7396 | if (err) |
| 7397 | memprintf(err, "'%s' : option not implemented", arg); |
| 7398 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7399 | } |
| 7400 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7401 | 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] | 7402 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7403 | 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] | 7404 | } |
| 7405 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7406 | 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] | 7407 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7408 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 7409 | } |
| 7410 | |
| 7411 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 7412 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 7413 | { |
| 7414 | uint16_t i, v = 0; |
| 7415 | char *argv = args[cur_arg + 1]; |
| 7416 | if (!*argv) { |
| 7417 | if (err) |
| 7418 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
| 7419 | return ERR_ALERT | ERR_FATAL; |
| 7420 | } |
| 7421 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 7422 | if (!strcmp(argv, methodVersions[i].name)) |
| 7423 | v = i; |
| 7424 | if (!v) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7425 | if (err) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7426 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7427 | return ERR_ALERT | ERR_FATAL; |
| 7428 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7429 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 7430 | methods->min = v; |
| 7431 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 7432 | methods->max = v; |
| 7433 | else { |
| 7434 | if (err) |
| 7435 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
| 7436 | return ERR_ALERT | ERR_FATAL; |
| 7437 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7438 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7439 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7440 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 7441 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7442 | { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 7443 | #if (OPENSSL_VERSION_NUMBER < 0x10101000L) || !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7444 | 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] | 7445 | #endif |
| 7446 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 7447 | } |
| 7448 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7449 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7450 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7451 | 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] | 7452 | } |
| 7453 | |
| 7454 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7455 | { |
| 7456 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 7457 | } |
| 7458 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7459 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 7460 | 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] | 7461 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 7462 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7463 | return 0; |
| 7464 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7465 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7466 | /* parse the "allow-0rtt" bind keyword */ |
| 7467 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7468 | { |
| 7469 | conf->early_data = 1; |
| 7470 | return 0; |
| 7471 | } |
| 7472 | |
| 7473 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7474 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 7475 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7476 | return 0; |
| 7477 | } |
| 7478 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7479 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7480 | 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] | 7481 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7482 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7483 | char *p1, *p2; |
| 7484 | |
| 7485 | if (!*args[cur_arg + 1]) { |
| 7486 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 7487 | return ERR_ALERT | ERR_FATAL; |
| 7488 | } |
| 7489 | |
| 7490 | free(conf->npn_str); |
| 7491 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 7492 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 7493 | * so we reuse each comma to store the next <len> and need |
| 7494 | * one more for the end of the string. |
| 7495 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7496 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 7497 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7498 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 7499 | |
| 7500 | /* replace commas with the name length */ |
| 7501 | p1 = conf->npn_str; |
| 7502 | p2 = p1 + 1; |
| 7503 | while (1) { |
| 7504 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 7505 | if (!p2) |
| 7506 | p2 = p1 + 1 + strlen(p1 + 1); |
| 7507 | |
| 7508 | if (p2 - (p1 + 1) > 255) { |
| 7509 | *p2 = '\0'; |
| 7510 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 7511 | return ERR_ALERT | ERR_FATAL; |
| 7512 | } |
| 7513 | |
| 7514 | *p1 = p2 - (p1 + 1); |
| 7515 | p1 = p2; |
| 7516 | |
| 7517 | if (!*p2) |
| 7518 | break; |
| 7519 | |
| 7520 | *(p2++) = '\0'; |
| 7521 | } |
| 7522 | return 0; |
| 7523 | #else |
| 7524 | if (err) |
| 7525 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 7526 | return ERR_ALERT | ERR_FATAL; |
| 7527 | #endif |
| 7528 | } |
| 7529 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7530 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7531 | { |
| 7532 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 7533 | } |
| 7534 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7535 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7536 | 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] | 7537 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7538 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7539 | char *p1, *p2; |
| 7540 | |
| 7541 | if (!*args[cur_arg + 1]) { |
| 7542 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 7543 | return ERR_ALERT | ERR_FATAL; |
| 7544 | } |
| 7545 | |
| 7546 | free(conf->alpn_str); |
| 7547 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 7548 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 7549 | * so we reuse each comma to store the next <len> and need |
| 7550 | * one more for the end of the string. |
| 7551 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7552 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 7553 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7554 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 7555 | |
| 7556 | /* replace commas with the name length */ |
| 7557 | p1 = conf->alpn_str; |
| 7558 | p2 = p1 + 1; |
| 7559 | while (1) { |
| 7560 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 7561 | if (!p2) |
| 7562 | p2 = p1 + 1 + strlen(p1 + 1); |
| 7563 | |
| 7564 | if (p2 - (p1 + 1) > 255) { |
| 7565 | *p2 = '\0'; |
| 7566 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 7567 | return ERR_ALERT | ERR_FATAL; |
| 7568 | } |
| 7569 | |
| 7570 | *p1 = p2 - (p1 + 1); |
| 7571 | p1 = p2; |
| 7572 | |
| 7573 | if (!*p2) |
| 7574 | break; |
| 7575 | |
| 7576 | *(p2++) = '\0'; |
| 7577 | } |
| 7578 | return 0; |
| 7579 | #else |
| 7580 | if (err) |
| 7581 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 7582 | return ERR_ALERT | ERR_FATAL; |
| 7583 | #endif |
| 7584 | } |
| 7585 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7586 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7587 | { |
| 7588 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 7589 | } |
| 7590 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7591 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7592 | 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] | 7593 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 7594 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7595 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7596 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7597 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 7598 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 7599 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7600 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 7601 | if (!conf->ssl_conf.ssl_methods.min) |
| 7602 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 7603 | if (!conf->ssl_conf.ssl_methods.max) |
| 7604 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7605 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7606 | return 0; |
| 7607 | } |
| 7608 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 7609 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 7610 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7611 | { |
| 7612 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 7613 | return 0; |
| 7614 | } |
| 7615 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7616 | /* parse the "generate-certificates" bind keyword */ |
| 7617 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7618 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7619 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7620 | conf->generate_certs = 1; |
| 7621 | #else |
| 7622 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 7623 | err && *err ? *err : ""); |
| 7624 | #endif |
| 7625 | return 0; |
| 7626 | } |
| 7627 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 7628 | /* parse the "strict-sni" bind keyword */ |
| 7629 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7630 | { |
| 7631 | conf->strict_sni = 1; |
| 7632 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7633 | } |
| 7634 | |
| 7635 | /* parse the "tls-ticket-keys" bind keyword */ |
| 7636 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7637 | { |
| 7638 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7639 | FILE *f; |
| 7640 | int i = 0; |
| 7641 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7642 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7643 | |
| 7644 | if (!*args[cur_arg + 1]) { |
| 7645 | if (err) |
| 7646 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 7647 | return ERR_ALERT | ERR_FATAL; |
| 7648 | } |
| 7649 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7650 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
| 7651 | if(keys_ref) { |
| 7652 | conf->keys_ref = keys_ref; |
| 7653 | return 0; |
| 7654 | } |
| 7655 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 7656 | keys_ref = malloc(sizeof(*keys_ref)); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7657 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct tls_sess_key)); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7658 | |
| 7659 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 7660 | if (err) |
| 7661 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 7662 | return ERR_ALERT | ERR_FATAL; |
| 7663 | } |
| 7664 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7665 | keys_ref->filename = strdup(args[cur_arg + 1]); |
| 7666 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7667 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 7668 | int len = strlen(thisline); |
| 7669 | /* Strip newline characters from the end */ |
| 7670 | if(thisline[len - 1] == '\n') |
| 7671 | thisline[--len] = 0; |
| 7672 | |
| 7673 | if(thisline[len - 1] == '\r') |
| 7674 | thisline[--len] = 0; |
| 7675 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7676 | 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] | 7677 | if (err) |
| 7678 | 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] | 7679 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7680 | return ERR_ALERT | ERR_FATAL; |
| 7681 | } |
| 7682 | i++; |
| 7683 | } |
| 7684 | |
| 7685 | if (i < TLS_TICKETS_NO) { |
| 7686 | if (err) |
| 7687 | 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] | 7688 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7689 | return ERR_ALERT | ERR_FATAL; |
| 7690 | } |
| 7691 | |
| 7692 | fclose(f); |
| 7693 | |
| 7694 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 7695 | i -= 2; |
| 7696 | 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] | 7697 | keys_ref->unique_id = -1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7698 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7699 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7700 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7701 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 7702 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7703 | return 0; |
| 7704 | #else |
| 7705 | if (err) |
| 7706 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 7707 | return ERR_ALERT | ERR_FATAL; |
| 7708 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 7709 | } |
| 7710 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7711 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7712 | 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] | 7713 | { |
| 7714 | if (!*args[cur_arg + 1]) { |
| 7715 | if (err) |
| 7716 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 7717 | return ERR_ALERT | ERR_FATAL; |
| 7718 | } |
| 7719 | |
| 7720 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7721 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7722 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7723 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7724 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7725 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7726 | else { |
| 7727 | if (err) |
| 7728 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 7729 | args[cur_arg], args[cur_arg + 1]); |
| 7730 | return ERR_ALERT | ERR_FATAL; |
| 7731 | } |
| 7732 | |
| 7733 | return 0; |
| 7734 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7735 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7736 | { |
| 7737 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 7738 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7739 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 7740 | /* parse the "no-ca-names" bind keyword */ |
| 7741 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7742 | { |
| 7743 | conf->no_ca_names = 1; |
| 7744 | return 0; |
| 7745 | } |
| 7746 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7747 | { |
| 7748 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 7749 | } |
| 7750 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7751 | /************** "server" keywords ****************/ |
| 7752 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7753 | /* parse the "ca-file" server keyword */ |
| 7754 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7755 | { |
| 7756 | if (!*args[*cur_arg + 1]) { |
| 7757 | if (err) |
| 7758 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 7759 | return ERR_ALERT | ERR_FATAL; |
| 7760 | } |
| 7761 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7762 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7763 | 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] | 7764 | else |
| 7765 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 7766 | |
| 7767 | return 0; |
| 7768 | } |
| 7769 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 7770 | /* parse the "check-sni" server keyword */ |
| 7771 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7772 | { |
| 7773 | if (!*args[*cur_arg + 1]) { |
| 7774 | if (err) |
| 7775 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
| 7776 | return ERR_ALERT | ERR_FATAL; |
| 7777 | } |
| 7778 | |
| 7779 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 7780 | if (!newsrv->check.sni) { |
| 7781 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 7782 | return ERR_ALERT | ERR_FATAL; |
| 7783 | } |
| 7784 | return 0; |
| 7785 | |
| 7786 | } |
| 7787 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7788 | /* parse the "check-ssl" server keyword */ |
| 7789 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7790 | { |
| 7791 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7792 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 7793 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
| 7794 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7795 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 7796 | if (!newsrv->ssl_ctx.methods.min) |
| 7797 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 7798 | if (!newsrv->ssl_ctx.methods.max) |
| 7799 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 7800 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7801 | return 0; |
| 7802 | } |
| 7803 | |
| 7804 | /* parse the "ciphers" server keyword */ |
| 7805 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7806 | { |
| 7807 | if (!*args[*cur_arg + 1]) { |
| 7808 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 7809 | return ERR_ALERT | ERR_FATAL; |
| 7810 | } |
| 7811 | |
| 7812 | free(newsrv->ssl_ctx.ciphers); |
| 7813 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 7814 | return 0; |
| 7815 | } |
| 7816 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7817 | /* parse the "crl-file" server keyword */ |
| 7818 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7819 | { |
| 7820 | #ifndef X509_V_FLAG_CRL_CHECK |
| 7821 | if (err) |
| 7822 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 7823 | return ERR_ALERT | ERR_FATAL; |
| 7824 | #else |
| 7825 | if (!*args[*cur_arg + 1]) { |
| 7826 | if (err) |
| 7827 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 7828 | return ERR_ALERT | ERR_FATAL; |
| 7829 | } |
| 7830 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7831 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7832 | 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] | 7833 | else |
| 7834 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 7835 | |
| 7836 | return 0; |
| 7837 | #endif |
| 7838 | } |
| 7839 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 7840 | /* parse the "crt" server keyword */ |
| 7841 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7842 | { |
| 7843 | if (!*args[*cur_arg + 1]) { |
| 7844 | if (err) |
| 7845 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 7846 | return ERR_ALERT | ERR_FATAL; |
| 7847 | } |
| 7848 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7849 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 7850 | 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] | 7851 | else |
| 7852 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 7853 | |
| 7854 | return 0; |
| 7855 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7856 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 7857 | /* parse the "no-check-ssl" server keyword */ |
| 7858 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7859 | { |
| 7860 | newsrv->check.use_ssl = 0; |
| 7861 | free(newsrv->ssl_ctx.ciphers); |
| 7862 | newsrv->ssl_ctx.ciphers = NULL; |
| 7863 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 7864 | return 0; |
| 7865 | } |
| 7866 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 7867 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 7868 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7869 | { |
| 7870 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 7871 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 7872 | return 0; |
| 7873 | } |
| 7874 | |
| 7875 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 7876 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7877 | { |
| 7878 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 7879 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 7880 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 7881 | return 0; |
| 7882 | } |
| 7883 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 7884 | /* parse the "no-ssl" server keyword */ |
| 7885 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7886 | { |
| 7887 | newsrv->use_ssl = 0; |
| 7888 | free(newsrv->ssl_ctx.ciphers); |
| 7889 | newsrv->ssl_ctx.ciphers = NULL; |
| 7890 | return 0; |
| 7891 | } |
| 7892 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7893 | /* parse the "allow-0rtt" server keyword */ |
| 7894 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7895 | { |
| 7896 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 7897 | return 0; |
| 7898 | } |
| 7899 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 7900 | /* parse the "no-ssl-reuse" server keyword */ |
| 7901 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7902 | { |
| 7903 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 7904 | return 0; |
| 7905 | } |
| 7906 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7907 | /* parse the "no-tls-tickets" server keyword */ |
| 7908 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7909 | { |
| 7910 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 7911 | return 0; |
| 7912 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7913 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 7914 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7915 | { |
| 7916 | newsrv->pp_opts |= SRV_PP_V2; |
| 7917 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 7918 | return 0; |
| 7919 | } |
| 7920 | |
| 7921 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 7922 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7923 | { |
| 7924 | newsrv->pp_opts |= SRV_PP_V2; |
| 7925 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 7926 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 7927 | return 0; |
| 7928 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7929 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7930 | /* parse the "sni" server keyword */ |
| 7931 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7932 | { |
| 7933 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7934 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 7935 | return ERR_ALERT | ERR_FATAL; |
| 7936 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7937 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7938 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7939 | arg = args[*cur_arg + 1]; |
| 7940 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7941 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 7942 | return ERR_ALERT | ERR_FATAL; |
| 7943 | } |
| 7944 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7945 | free(newsrv->sni_expr); |
| 7946 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7947 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7948 | return 0; |
| 7949 | #endif |
| 7950 | } |
| 7951 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7952 | /* parse the "ssl" server keyword */ |
| 7953 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7954 | { |
| 7955 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7956 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 7957 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7958 | return 0; |
| 7959 | } |
| 7960 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 7961 | /* parse the "ssl-reuse" server keyword */ |
| 7962 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7963 | { |
| 7964 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 7965 | return 0; |
| 7966 | } |
| 7967 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 7968 | /* parse the "tls-tickets" server keyword */ |
| 7969 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7970 | { |
| 7971 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 7972 | return 0; |
| 7973 | } |
| 7974 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7975 | /* parse the "verify" server keyword */ |
| 7976 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7977 | { |
| 7978 | if (!*args[*cur_arg + 1]) { |
| 7979 | if (err) |
| 7980 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 7981 | return ERR_ALERT | ERR_FATAL; |
| 7982 | } |
| 7983 | |
| 7984 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7985 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7986 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7987 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7988 | else { |
| 7989 | if (err) |
| 7990 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 7991 | args[*cur_arg], args[*cur_arg + 1]); |
| 7992 | return ERR_ALERT | ERR_FATAL; |
| 7993 | } |
| 7994 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 7995 | return 0; |
| 7996 | } |
| 7997 | |
| 7998 | /* parse the "verifyhost" server keyword */ |
| 7999 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8000 | { |
| 8001 | if (!*args[*cur_arg + 1]) { |
| 8002 | if (err) |
| 8003 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 8004 | return ERR_ALERT | ERR_FATAL; |
| 8005 | } |
| 8006 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 8007 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8008 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 8009 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8010 | return 0; |
| 8011 | } |
| 8012 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8013 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 8014 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 8015 | struct proxy *defpx, const char *file, int line, |
| 8016 | char **err) { |
| 8017 | int i = 1; |
| 8018 | |
| 8019 | if (*(args[i]) == 0) { |
| 8020 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8021 | return -1; |
| 8022 | } |
| 8023 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8024 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8025 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 8026 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 8027 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8028 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8029 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 8030 | i++; |
| 8031 | else { |
| 8032 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8033 | return -1; |
| 8034 | } |
| 8035 | } |
| 8036 | 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] | 8037 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8038 | return -1; |
| 8039 | } |
| 8040 | i++; |
| 8041 | } |
| 8042 | return 0; |
| 8043 | } |
| 8044 | |
| 8045 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 8046 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 8047 | struct proxy *defpx, const char *file, int line, |
| 8048 | char **err) { |
| 8049 | int i = 1; |
| 8050 | |
| 8051 | if (*(args[i]) == 0) { |
| 8052 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8053 | return -1; |
| 8054 | } |
| 8055 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8056 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8057 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8058 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8059 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 8060 | i++; |
| 8061 | else { |
| 8062 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8063 | return -1; |
| 8064 | } |
| 8065 | } |
| 8066 | 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] | 8067 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8068 | return -1; |
| 8069 | } |
| 8070 | i++; |
| 8071 | } |
| 8072 | return 0; |
| 8073 | } |
| 8074 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8075 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 8076 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8077 | */ |
| 8078 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 8079 | struct proxy *defpx, const char *file, int line, |
| 8080 | char **err) |
| 8081 | { |
| 8082 | char **target; |
| 8083 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8084 | 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] | 8085 | |
| 8086 | if (too_many_args(1, args, err, NULL)) |
| 8087 | return -1; |
| 8088 | |
| 8089 | if (*target) { |
| 8090 | memprintf(err, "'%s' already specified.", args[0]); |
| 8091 | return -1; |
| 8092 | } |
| 8093 | |
| 8094 | if (*(args[1]) == 0) { |
| 8095 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 8096 | return -1; |
| 8097 | } |
| 8098 | *target = strdup(args[1]); |
| 8099 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8100 | } |
| 8101 | |
| 8102 | /* parse the "ssl-mode-async" keyword in global section. |
| 8103 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8104 | */ |
| 8105 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 8106 | struct proxy *defpx, const char *file, int line, |
| 8107 | char **err) |
| 8108 | { |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 8109 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8110 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 8111 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8112 | return 0; |
| 8113 | #else |
| 8114 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 8115 | return -1; |
| 8116 | #endif |
| 8117 | } |
| 8118 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8119 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8120 | static int ssl_check_async_engine_count(void) { |
| 8121 | int err_code = 0; |
| 8122 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 8123 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8124 | 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] | 8125 | err_code = ERR_ABORT; |
| 8126 | } |
| 8127 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8128 | } |
| 8129 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8130 | /* parse the "ssl-engine" keyword in global section. |
| 8131 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8132 | */ |
| 8133 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 8134 | struct proxy *defpx, const char *file, int line, |
| 8135 | char **err) |
| 8136 | { |
| 8137 | char *algo; |
| 8138 | int ret = -1; |
| 8139 | |
| 8140 | if (*(args[1]) == 0) { |
| 8141 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 8142 | return ret; |
| 8143 | } |
| 8144 | |
| 8145 | if (*(args[2]) == 0) { |
| 8146 | /* if no list of algorithms is given, it defaults to ALL */ |
| 8147 | algo = strdup("ALL"); |
| 8148 | goto add_engine; |
| 8149 | } |
| 8150 | |
| 8151 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 8152 | if (strcmp(args[2], "algo") != 0) { |
| 8153 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 8154 | return ret; |
| 8155 | } |
| 8156 | |
| 8157 | if (*(args[3]) == 0) { |
| 8158 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 8159 | return ret; |
| 8160 | } |
| 8161 | algo = strdup(args[3]); |
| 8162 | |
| 8163 | add_engine: |
| 8164 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 8165 | openssl_engines_initialized++; |
| 8166 | ret = 0; |
| 8167 | } |
| 8168 | free(algo); |
| 8169 | return ret; |
| 8170 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8171 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8172 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8173 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 8174 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 8175 | */ |
| 8176 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 8177 | struct proxy *defpx, const char *file, int line, |
| 8178 | char **err) |
| 8179 | { |
| 8180 | char **target; |
| 8181 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8182 | 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] | 8183 | |
| 8184 | if (too_many_args(1, args, err, NULL)) |
| 8185 | return -1; |
| 8186 | |
| 8187 | if (*(args[1]) == 0) { |
| 8188 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 8189 | return -1; |
| 8190 | } |
| 8191 | |
| 8192 | free(*target); |
| 8193 | *target = strdup(args[1]); |
| 8194 | return 0; |
| 8195 | } |
| 8196 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8197 | /* parse various global tune.ssl settings consisting in positive integers. |
| 8198 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8199 | */ |
| 8200 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 8201 | struct proxy *defpx, const char *file, int line, |
| 8202 | char **err) |
| 8203 | { |
| 8204 | int *target; |
| 8205 | |
| 8206 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 8207 | target = &global.tune.sslcachesize; |
| 8208 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8209 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8210 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8211 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 8212 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 8213 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8214 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 8215 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8216 | else { |
| 8217 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 8218 | return -1; |
| 8219 | } |
| 8220 | |
| 8221 | if (too_many_args(1, args, err, NULL)) |
| 8222 | return -1; |
| 8223 | |
| 8224 | if (*(args[1]) == 0) { |
| 8225 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 8226 | return -1; |
| 8227 | } |
| 8228 | |
| 8229 | *target = atoi(args[1]); |
| 8230 | if (*target < 0) { |
| 8231 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 8232 | return -1; |
| 8233 | } |
| 8234 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8235 | } |
| 8236 | |
| 8237 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 8238 | struct proxy *defpx, const char *file, int line, |
| 8239 | char **err) |
| 8240 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8241 | int ret; |
| 8242 | |
| 8243 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 8244 | if (ret != 0) |
| 8245 | return ret; |
| 8246 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8247 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8248 | memprintf(err, "'%s' is already configured.", args[0]); |
| 8249 | return -1; |
| 8250 | } |
| 8251 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8252 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 8253 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8254 | memprintf(err, "Out of memory error."); |
| 8255 | return -1; |
| 8256 | } |
| 8257 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8258 | } |
| 8259 | |
| 8260 | /* parse "ssl.force-private-cache". |
| 8261 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8262 | */ |
| 8263 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 8264 | struct proxy *defpx, const char *file, int line, |
| 8265 | char **err) |
| 8266 | { |
| 8267 | if (too_many_args(0, args, err, NULL)) |
| 8268 | return -1; |
| 8269 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8270 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8271 | return 0; |
| 8272 | } |
| 8273 | |
| 8274 | /* parse "ssl.lifetime". |
| 8275 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8276 | */ |
| 8277 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 8278 | struct proxy *defpx, const char *file, int line, |
| 8279 | char **err) |
| 8280 | { |
| 8281 | const char *res; |
| 8282 | |
| 8283 | if (too_many_args(1, args, err, NULL)) |
| 8284 | return -1; |
| 8285 | |
| 8286 | if (*(args[1]) == 0) { |
| 8287 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 8288 | return -1; |
| 8289 | } |
| 8290 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8291 | 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] | 8292 | if (res) { |
| 8293 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 8294 | return -1; |
| 8295 | } |
| 8296 | return 0; |
| 8297 | } |
| 8298 | |
| 8299 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 8300 | /* parse "ssl-dh-param-file". |
| 8301 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8302 | */ |
| 8303 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 8304 | struct proxy *defpx, const char *file, int line, |
| 8305 | char **err) |
| 8306 | { |
| 8307 | if (too_many_args(1, args, err, NULL)) |
| 8308 | return -1; |
| 8309 | |
| 8310 | if (*(args[1]) == 0) { |
| 8311 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 8312 | return -1; |
| 8313 | } |
| 8314 | |
| 8315 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 8316 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 8317 | return -1; |
| 8318 | } |
| 8319 | return 0; |
| 8320 | } |
| 8321 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8322 | /* parse "ssl.default-dh-param". |
| 8323 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8324 | */ |
| 8325 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 8326 | struct proxy *defpx, const char *file, int line, |
| 8327 | char **err) |
| 8328 | { |
| 8329 | if (too_many_args(1, args, err, NULL)) |
| 8330 | return -1; |
| 8331 | |
| 8332 | if (*(args[1]) == 0) { |
| 8333 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 8334 | return -1; |
| 8335 | } |
| 8336 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8337 | global_ssl.default_dh_param = atoi(args[1]); |
| 8338 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8339 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 8340 | return -1; |
| 8341 | } |
| 8342 | return 0; |
| 8343 | } |
| 8344 | #endif |
| 8345 | |
| 8346 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8347 | /* This function is used with TLS ticket keys management. It permits to browse |
| 8348 | * each reference. The variable <getnext> must contain the current node, |
| 8349 | * <end> point to the root node. |
| 8350 | */ |
| 8351 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8352 | static inline |
| 8353 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 8354 | { |
| 8355 | struct tls_keys_ref *ref = getnext; |
| 8356 | |
| 8357 | while (1) { |
| 8358 | |
| 8359 | /* Get next list entry. */ |
| 8360 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 8361 | |
| 8362 | /* If the entry is the last of the list, return NULL. */ |
| 8363 | if (&ref->list == end) |
| 8364 | return NULL; |
| 8365 | |
| 8366 | return ref; |
| 8367 | } |
| 8368 | } |
| 8369 | |
| 8370 | static inline |
| 8371 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 8372 | { |
| 8373 | int id; |
| 8374 | char *error; |
| 8375 | |
| 8376 | /* If the reference starts by a '#', this is numeric id. */ |
| 8377 | if (reference[0] == '#') { |
| 8378 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 8379 | id = strtol(reference + 1, &error, 10); |
| 8380 | if (*error != '\0') |
| 8381 | return NULL; |
| 8382 | |
| 8383 | /* Perform the unique id lookup. */ |
| 8384 | return tlskeys_ref_lookupid(id); |
| 8385 | } |
| 8386 | |
| 8387 | /* Perform the string lookup. */ |
| 8388 | return tlskeys_ref_lookup(reference); |
| 8389 | } |
| 8390 | #endif |
| 8391 | |
| 8392 | |
| 8393 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8394 | |
| 8395 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 8396 | |
| 8397 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 8398 | return cli_io_handler_tlskeys_files(appctx); |
| 8399 | } |
| 8400 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8401 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 8402 | * (next index to be dumped), and cli.p0 (next key reference). |
| 8403 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8404 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 8405 | |
| 8406 | struct stream_interface *si = appctx->owner; |
| 8407 | |
| 8408 | switch (appctx->st2) { |
| 8409 | case STAT_ST_INIT: |
| 8410 | /* Display the column headers. If the message cannot be sent, |
| 8411 | * quit the fucntion with returning 0. The function is called |
| 8412 | * later and restart at the state "STAT_ST_INIT". |
| 8413 | */ |
| 8414 | chunk_reset(&trash); |
| 8415 | |
| 8416 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 8417 | chunk_appendf(&trash, "# id secret\n"); |
| 8418 | else |
| 8419 | chunk_appendf(&trash, "# id (file)\n"); |
| 8420 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8421 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8422 | si_applet_cant_put(si); |
| 8423 | return 0; |
| 8424 | } |
| 8425 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8426 | /* Now, we start the browsing of the references lists. |
| 8427 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 8428 | * available field of this pointer is <list>. It is used with the function |
| 8429 | * tlskeys_list_get_next() for retruning the first available entry |
| 8430 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8431 | if (appctx->ctx.cli.p0 == NULL) { |
| 8432 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 8433 | 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] | 8434 | } |
| 8435 | |
| 8436 | appctx->st2 = STAT_ST_LIST; |
| 8437 | /* fall through */ |
| 8438 | |
| 8439 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8440 | while (appctx->ctx.cli.p0) { |
| 8441 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8442 | |
| 8443 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8444 | 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] | 8445 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8446 | |
| 8447 | if (appctx->ctx.cli.i1 == 0) |
| 8448 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 8449 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8450 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8451 | int head; |
| 8452 | |
| 8453 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 8454 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8455 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8456 | struct chunk *t2 = get_trash_chunk(); |
| 8457 | |
| 8458 | chunk_reset(t2); |
| 8459 | /* 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] | 8460 | 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] | 8461 | sizeof(struct tls_sess_key), t2->str, t2->size); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8462 | 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] | 8463 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8464 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8465 | /* let's try again later from this stream. We add ourselves into |
| 8466 | * this stream's users so that it can remove us upon termination. |
| 8467 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8468 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8469 | si_applet_cant_put(si); |
| 8470 | return 0; |
| 8471 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8472 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8473 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8474 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8475 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8476 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8477 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8478 | /* let's try again later from this stream. We add ourselves into |
| 8479 | * this stream's users so that it can remove us upon termination. |
| 8480 | */ |
| 8481 | si_applet_cant_put(si); |
| 8482 | return 0; |
| 8483 | } |
| 8484 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8485 | 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] | 8486 | break; |
| 8487 | |
| 8488 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8489 | 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] | 8490 | } |
| 8491 | |
| 8492 | appctx->st2 = STAT_ST_FIN; |
| 8493 | /* fall through */ |
| 8494 | |
| 8495 | default: |
| 8496 | appctx->st2 = STAT_ST_FIN; |
| 8497 | return 1; |
| 8498 | } |
| 8499 | return 0; |
| 8500 | } |
| 8501 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8502 | /* sets cli.i0 to non-zero if only file lists should be dumped */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8503 | static int cli_parse_show_tlskeys(char **args, struct appctx *appctx, void *private) |
| 8504 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8505 | /* no parameter, shows only file list */ |
| 8506 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8507 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8508 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 8509 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8510 | } |
| 8511 | |
| 8512 | if (args[2][0] == '*') { |
| 8513 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8514 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8515 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8516 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
| 8517 | if (!appctx->ctx.cli.p0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8518 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8519 | 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] | 8520 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8521 | return 1; |
| 8522 | } |
| 8523 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8524 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 8525 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8526 | } |
| 8527 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8528 | static int cli_parse_set_tlskeys(char **args, struct appctx *appctx, void *private) |
| 8529 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8530 | struct tls_keys_ref *ref; |
| 8531 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8532 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 8533 | if (!*args[3] || !*args[4]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8534 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8535 | 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] | 8536 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8537 | return 1; |
| 8538 | } |
| 8539 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8540 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 8541 | if (!ref) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8542 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8543 | 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] | 8544 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8545 | return 1; |
| 8546 | } |
| 8547 | |
| 8548 | trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size); |
| 8549 | if (trash.len != sizeof(struct tls_sess_key)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8550 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8551 | 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] | 8552 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8553 | return 1; |
| 8554 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8555 | ssl_sock_update_tlskey_ref(ref, &trash); |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8556 | appctx->ctx.cli.severity = LOG_INFO; |
Aurélien Nephtali | 6e8a41d | 2018-03-15 21:48:50 +0100 | [diff] [blame] | 8557 | appctx->ctx.cli.msg = "TLS ticket key updated!\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 8558 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8559 | return 1; |
| 8560 | |
| 8561 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 8562 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8563 | |
| 8564 | static int cli_parse_set_ocspresponse(char **args, struct appctx *appctx, void *private) |
| 8565 | { |
| 8566 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 8567 | char *err = NULL; |
| 8568 | |
| 8569 | /* Expect one parameter: the new response in base64 encoding */ |
| 8570 | if (!*args[3]) { |
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 ocsp-response' expects response in base64 encoding.\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[3], strlen(args[3]), trash.str, trash.size); |
| 8578 | if (trash.len < 0) { |
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 ocsp-response' received invalid base64 encoded response.\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 | } |
| 8584 | |
| 8585 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
| 8586 | if (err) { |
| 8587 | memprintf(&err, "%s.\n", err); |
| 8588 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 8589 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8590 | } |
Aurélien Nephtali | 9a4da68 | 2018-04-16 19:02:42 +0200 | [diff] [blame] | 8591 | else { |
| 8592 | appctx->ctx.cli.severity = LOG_ERR; |
| 8593 | appctx->ctx.cli.msg = "Failed to update OCSP response.\n"; |
| 8594 | appctx->st0 = CLI_ST_PRINT; |
| 8595 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8596 | return 1; |
| 8597 | } |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8598 | appctx->ctx.cli.severity = LOG_INFO; |
Aurélien Nephtali | 6e8a41d | 2018-03-15 21:48:50 +0100 | [diff] [blame] | 8599 | appctx->ctx.cli.msg = "OCSP Response updated!\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 8600 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8601 | return 1; |
| 8602 | #else |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8603 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8604 | 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] | 8605 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8606 | return 1; |
| 8607 | #endif |
| 8608 | |
| 8609 | } |
| 8610 | |
| 8611 | /* register cli keywords */ |
| 8612 | static struct cli_kw_list cli_kws = {{ },{ |
| 8613 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8614 | { { "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] | 8615 | { { "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] | 8616 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 8617 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8618 | { { NULL }, NULL, NULL, NULL } |
| 8619 | }}; |
| 8620 | |
| 8621 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8622 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8623 | * Please take care of keeping this list alphabetically sorted. |
| 8624 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 8625 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8626 | { "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] | 8627 | { "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] | 8628 | { "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] | 8629 | { "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] | 8630 | { "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] | 8631 | { "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] | 8632 | { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV }, |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8633 | { "ssl_bc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 8634 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 8635 | { "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] | 8636 | { "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] | 8637 | { "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] | 8638 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8639 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8640 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8641 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8642 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8643 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8644 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 8645 | { "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] | 8646 | { "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] | 8647 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 8648 | { "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] | 8649 | { "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] | 8650 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8651 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8652 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8653 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8654 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8655 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8656 | { "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] | 8657 | { "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] | 8658 | { "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] | 8659 | { "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] | 8660 | { "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] | 8661 | { "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] | 8662 | { "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] | 8663 | { "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] | 8664 | { "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] | 8665 | { "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] | 8666 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8667 | { "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] | 8668 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 8669 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8670 | { "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] | 8671 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8672 | { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 8673 | { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 8674 | { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8675 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 8676 | { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8677 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8678 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 8679 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8680 | { "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] | 8681 | { NULL, NULL, 0, 0, 0 }, |
| 8682 | }}; |
| 8683 | |
| 8684 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8685 | * Please take care of keeping this list alphabetically sorted. |
| 8686 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 8687 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 8688 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 8689 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 8690 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8691 | }}; |
| 8692 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8693 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8694 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 8695 | * all code contributors. |
| 8696 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 8697 | * the config parser can report an appropriate error when a known keyword was |
| 8698 | * not enabled. |
| 8699 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8700 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8701 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8702 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 8703 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 8704 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 8705 | { "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] | 8706 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8707 | { "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] | 8708 | { "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] | 8709 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 8710 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 8711 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8712 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 8713 | { NULL, NULL, 0 }, |
| 8714 | }; |
| 8715 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 8716 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8717 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8718 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 8719 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 8720 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 8721 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 8722 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 8723 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 8724 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 8725 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 8726 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 8727 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 8728 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 8729 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 8730 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 8731 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 8732 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 8733 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 8734 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8735 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 8736 | { "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] | 8737 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 8738 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 8739 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 8740 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 8741 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8742 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 8743 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8744 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 8745 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8746 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 8747 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 8748 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 8749 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 8750 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8751 | { NULL, NULL, 0 }, |
| 8752 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8753 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8754 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8755 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 8756 | * all code contributors. |
| 8757 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 8758 | * the config parser can report an appropriate error when a known keyword was |
| 8759 | * not enabled. |
| 8760 | */ |
| 8761 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 8762 | { "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] | 8763 | { "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] | 8764 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8765 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 8766 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
| 8767 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 8768 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 8769 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 8770 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 8771 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 8772 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 8773 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 8774 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 8775 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 8776 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 8777 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 8778 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 8779 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 8780 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 8781 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 8782 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 8783 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 8784 | { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */ |
| 8785 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 8786 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 8787 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 8788 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 8789 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 8790 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 8791 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 8792 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 8793 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 8794 | { "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] | 8795 | { NULL, NULL, 0, 0 }, |
| 8796 | }}; |
| 8797 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8798 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8799 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 8800 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 8801 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8802 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 8803 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 8804 | #ifndef OPENSSL_NO_DH |
| 8805 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 8806 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8807 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8808 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8809 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8810 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8811 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 8812 | #ifndef OPENSSL_NO_DH |
| 8813 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 8814 | #endif |
| 8815 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 8816 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 8817 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 8818 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8819 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8820 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 8821 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8822 | { 0, NULL, NULL }, |
| 8823 | }}; |
| 8824 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 8825 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 8826 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8827 | .snd_buf = ssl_sock_from_buf, |
| 8828 | .rcv_buf = ssl_sock_to_buf, |
| 8829 | .rcv_pipe = NULL, |
| 8830 | .snd_pipe = NULL, |
| 8831 | .shutr = NULL, |
| 8832 | .shutw = ssl_sock_shutw, |
| 8833 | .close = ssl_sock_close, |
| 8834 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 8835 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 8836 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 8837 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 8838 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 8839 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 8840 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8841 | }; |
| 8842 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8843 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 8844 | struct session *sess, struct stream *s, int flags) |
| 8845 | { |
| 8846 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 8847 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8848 | |
| 8849 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 8850 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8851 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 8852 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8853 | 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] | 8854 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8855 | s->req.flags |= CF_READ_NULL; |
| 8856 | return ACT_RET_YIELD; |
| 8857 | } |
| 8858 | } |
| 8859 | return (ACT_RET_CONT); |
| 8860 | } |
| 8861 | |
| 8862 | 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) |
| 8863 | { |
| 8864 | rule->action_ptr = ssl_action_wait_for_hs; |
| 8865 | |
| 8866 | return ACT_RET_PRS_OK; |
| 8867 | } |
| 8868 | |
| 8869 | static struct action_kw_list http_req_actions = {ILH, { |
| 8870 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 8871 | { /* END */ } |
| 8872 | }}; |
| 8873 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 8874 | #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] | 8875 | |
| 8876 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8877 | { |
| 8878 | if (ptr) { |
| 8879 | chunk_destroy(ptr); |
| 8880 | free(ptr); |
| 8881 | } |
| 8882 | } |
| 8883 | |
| 8884 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8885 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8886 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8887 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8888 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 8889 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8890 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8891 | static void __ssl_sock_init(void) |
| 8892 | { |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8893 | char *ptr; |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 8894 | int i; |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8895 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8896 | STACK_OF(SSL_COMP)* cm; |
| 8897 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8898 | if (global_ssl.listen_default_ciphers) |
| 8899 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 8900 | if (global_ssl.connect_default_ciphers) |
| 8901 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 8902 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 8903 | xprt_register(XPRT_SSL, &ssl_sock); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8904 | SSL_library_init(); |
| 8905 | cm = SSL_COMP_get_compression_methods(); |
| 8906 | sk_SSL_COMP_zero(cm); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 8907 | #ifdef USE_THREAD |
| 8908 | ssl_locking_init(); |
| 8909 | #endif |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 8910 | #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] | 8911 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 8912 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8913 | ssl_capture_ptr_index = SSL_CTX_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] | 8914 | 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] | 8915 | sample_register_fetches(&sample_fetch_keywords); |
| 8916 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8917 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8918 | srv_register_keywords(&srv_kws); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8919 | cfg_register_keywords(&cfg_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8920 | cli_register_kw(&cli_kws); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8921 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8922 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8923 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8924 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 8925 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8926 | hap_register_post_check(tlskeys_finalize_config); |
| 8927 | #endif |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 8928 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8929 | ptr = NULL; |
| 8930 | memprintf(&ptr, "Built with OpenSSL version : " |
| 8931 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 8932 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8933 | #else /* OPENSSL_IS_BORINGSSL */ |
| 8934 | OPENSSL_VERSION_TEXT |
| 8935 | "\nRunning on OpenSSL version : %s%s", |
| 8936 | SSLeay_version(SSLEAY_VERSION), |
| 8937 | ((OPENSSL_VERSION_NUMBER ^ SSLeay()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
| 8938 | #endif |
| 8939 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
| 8940 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 8941 | "no (library version too old)" |
| 8942 | #elif defined(OPENSSL_NO_TLSEXT) |
| 8943 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 8944 | #else |
| 8945 | "yes" |
| 8946 | #endif |
| 8947 | "", ptr); |
| 8948 | |
| 8949 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 8950 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 8951 | "yes" |
| 8952 | #else |
| 8953 | #ifdef OPENSSL_NO_TLSEXT |
| 8954 | "no (because of OPENSSL_NO_TLSEXT)" |
| 8955 | #else |
| 8956 | "no (version might be too old, 0.9.8f min needed)" |
| 8957 | #endif |
| 8958 | #endif |
| 8959 | "", ptr); |
| 8960 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 8961 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 8962 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 8963 | if (methodVersions[i].option) |
| 8964 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 8965 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8966 | hap_register_build_opts(ptr, 1); |
| 8967 | |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 8968 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 8969 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 8970 | |
| 8971 | #ifndef OPENSSL_NO_DH |
| 8972 | 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] | 8973 | hap_register_post_deinit(ssl_free_dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 8974 | #endif |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8975 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8976 | hap_register_post_deinit(ssl_free_engines); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8977 | #endif |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 8978 | /* Load SSL string for the verbose & debug mode. */ |
| 8979 | ERR_load_SSL_strings(); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8980 | |
| 8981 | http_req_keywords_register(&http_req_actions); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8982 | } |
| 8983 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8984 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8985 | void ssl_free_engines(void) { |
| 8986 | struct ssl_engine_list *wl, *wlb; |
| 8987 | /* free up engine list */ |
| 8988 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 8989 | ENGINE_finish(wl->e); |
| 8990 | ENGINE_free(wl->e); |
| 8991 | LIST_DEL(&wl->list); |
| 8992 | free(wl); |
| 8993 | } |
| 8994 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8995 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8996 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8997 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8998 | void ssl_free_dh(void) { |
| 8999 | if (local_dh_1024) { |
| 9000 | DH_free(local_dh_1024); |
| 9001 | local_dh_1024 = NULL; |
| 9002 | } |
| 9003 | if (local_dh_2048) { |
| 9004 | DH_free(local_dh_2048); |
| 9005 | local_dh_2048 = NULL; |
| 9006 | } |
| 9007 | if (local_dh_4096) { |
| 9008 | DH_free(local_dh_4096); |
| 9009 | local_dh_4096 = NULL; |
| 9010 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 9011 | if (global_dh) { |
| 9012 | DH_free(global_dh); |
| 9013 | global_dh = NULL; |
| 9014 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9015 | } |
| 9016 | #endif |
| 9017 | |
| 9018 | __attribute__((destructor)) |
| 9019 | static void __ssl_sock_deinit(void) |
| 9020 | { |
| 9021 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9022 | if (ssl_ctx_lru_tree) { |
| 9023 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 9024 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9025 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9026 | #endif |
| 9027 | |
| 9028 | ERR_remove_state(0); |
| 9029 | ERR_free_strings(); |
| 9030 | |
| 9031 | EVP_cleanup(); |
| 9032 | |
| 9033 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 9034 | CRYPTO_cleanup_all_ex_data(); |
| 9035 | #endif |
| 9036 | } |
| 9037 | |
| 9038 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9039 | /* |
| 9040 | * Local variables: |
| 9041 | * c-indent-level: 8 |
| 9042 | * c-basic-offset: 8 |
| 9043 | * End: |
| 9044 | */ |