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> |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 67 | #include <common/chunk.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 68 | #include <common/compat.h> |
| 69 | #include <common/config.h> |
| 70 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 71 | #include <common/errors.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 72 | #include <common/standard.h> |
| 73 | #include <common/ticks.h> |
| 74 | #include <common/time.h> |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 75 | #include <common/cfgparse.h> |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 76 | #include <common/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 77 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 78 | #include <ebsttree.h> |
| 79 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 80 | #include <types/applet.h> |
| 81 | #include <types/cli.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 82 | #include <types/global.h> |
| 83 | #include <types/ssl_sock.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 84 | #include <types/stats.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 85 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 86 | #include <proto/acl.h> |
| 87 | #include <proto/arg.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 88 | #include <proto/channel.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 89 | #include <proto/connection.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 90 | #include <proto/cli.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 91 | #include <proto/fd.h> |
| 92 | #include <proto/freq_ctr.h> |
| 93 | #include <proto/frontend.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 94 | #include <proto/listener.h> |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 95 | #include <proto/openssl-compat.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 96 | #include <proto/pattern.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 97 | #include <proto/proto_tcp.h> |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 98 | #include <proto/proto_http.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 99 | #include <proto/server.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 100 | #include <proto/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 101 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 102 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 103 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 104 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 105 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 106 | #include <proto/task.h> |
| 107 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 108 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 109 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 110 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 111 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 112 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 113 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 114 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 115 | |
| 116 | /* Verify errors macros */ |
| 117 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 118 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 119 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 120 | |
| 121 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 122 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 123 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 124 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 125 | /* Supported hash function for TLS tickets */ |
| 126 | #ifdef OPENSSL_NO_SHA256 |
| 127 | #define HASH_FUNCT EVP_sha1 |
| 128 | #else |
| 129 | #define HASH_FUNCT EVP_sha256 |
| 130 | #endif /* OPENSSL_NO_SHA256 */ |
| 131 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 132 | /* ssl_methods flags for ssl options */ |
| 133 | #define MC_SSL_O_ALL 0x0000 |
| 134 | #define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */ |
| 135 | #define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */ |
| 136 | #define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */ |
| 137 | #define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 138 | #define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 139 | |
| 140 | /* ssl_methods versions */ |
| 141 | enum { |
| 142 | CONF_TLSV_NONE = 0, |
| 143 | CONF_TLSV_MIN = 1, |
| 144 | CONF_SSLV3 = 1, |
| 145 | CONF_TLSV10 = 2, |
| 146 | CONF_TLSV11 = 3, |
| 147 | CONF_TLSV12 = 4, |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 148 | CONF_TLSV13 = 5, |
| 149 | CONF_TLSV_MAX = 5, |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 150 | }; |
| 151 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 152 | /* server and bind verify method, it uses a global value as default */ |
| 153 | enum { |
| 154 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 155 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 156 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 157 | SSL_SOCK_VERIFY_NONE = 3, |
| 158 | }; |
| 159 | |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 160 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 161 | int sslconns = 0; |
| 162 | int totalsslconns = 0; |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 163 | static struct xprt_ops ssl_sock; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 164 | int nb_engines = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 165 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 166 | static struct { |
| 167 | char *crt_base; /* base directory path for certificates */ |
| 168 | char *ca_base; /* base directory path for CAs and CRLs */ |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 169 | int async; /* whether we use ssl async mode */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 170 | |
| 171 | char *listen_default_ciphers; |
| 172 | char *connect_default_ciphers; |
| 173 | int listen_default_ssloptions; |
| 174 | int connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 175 | struct tls_version_filter listen_default_sslmethods; |
| 176 | struct tls_version_filter connect_default_sslmethods; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 177 | |
| 178 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 179 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 180 | unsigned int max_record; /* SSL max record size */ |
| 181 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 182 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 183 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 184 | } global_ssl = { |
| 185 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 186 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 187 | #endif |
| 188 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 189 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 190 | #endif |
| 191 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 192 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 193 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 194 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 195 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 196 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 197 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 198 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 199 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 200 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 201 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 202 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 203 | #endif |
| 204 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 205 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 206 | .capture_cipherlist = 0, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 207 | }; |
| 208 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 209 | #ifdef USE_THREAD |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 210 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 211 | static HA_RWLOCK_T *ssl_rwlocks; |
| 212 | |
| 213 | |
| 214 | unsigned long ssl_id_function(void) |
| 215 | { |
| 216 | return (unsigned long)tid; |
| 217 | } |
| 218 | |
| 219 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 220 | { |
| 221 | if (mode & CRYPTO_LOCK) { |
| 222 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 223 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 224 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 225 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 226 | } |
| 227 | else { |
| 228 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 229 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 230 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 231 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | static int ssl_locking_init(void) |
| 236 | { |
| 237 | int i; |
| 238 | |
| 239 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 240 | if (!ssl_rwlocks) |
| 241 | return -1; |
| 242 | |
| 243 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 244 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 245 | |
| 246 | CRYPTO_set_id_callback(ssl_id_function); |
| 247 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 248 | |
| 249 | return 0; |
| 250 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 251 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 252 | #endif |
| 253 | |
| 254 | |
| 255 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 256 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 257 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 258 | unsigned long long int xxh64; |
| 259 | unsigned char ciphersuite_len; |
| 260 | char ciphersuite[0]; |
| 261 | }; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 262 | struct pool_head *pool_head_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 263 | static int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 264 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 265 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 266 | static int ssl_pkey_info_index = -1; |
| 267 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 268 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 269 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 270 | #endif |
| 271 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 272 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 273 | static unsigned int openssl_engines_initialized; |
| 274 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 275 | struct ssl_engine_list { |
| 276 | struct list list; |
| 277 | ENGINE *e; |
| 278 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 279 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 280 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 281 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 282 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 283 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 284 | static DH *local_dh_1024 = NULL; |
| 285 | static DH *local_dh_2048 = NULL; |
| 286 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 287 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 288 | #endif /* OPENSSL_NO_DH */ |
| 289 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 290 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 291 | /* X509V3 Extensions that will be added on generated certificates */ |
| 292 | #define X509V3_EXT_SIZE 5 |
| 293 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 294 | "basicConstraints", |
| 295 | "nsComment", |
| 296 | "subjectKeyIdentifier", |
| 297 | "authorityKeyIdentifier", |
| 298 | "keyUsage", |
| 299 | }; |
| 300 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 301 | "CA:FALSE", |
| 302 | "\"OpenSSL Generated Certificate\"", |
| 303 | "hash", |
| 304 | "keyid,issuer:always", |
| 305 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 306 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 307 | /* LRU cache to store generated certificate */ |
| 308 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 309 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 310 | static unsigned int ssl_ctx_serial; |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 311 | __decl_hathreads(static HA_RWLOCK_T ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 312 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 313 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 314 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 315 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 316 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 317 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 318 | /* The order here matters for picking a default context, |
| 319 | * keep the most common keytype at the bottom of the list |
| 320 | */ |
| 321 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 322 | "dsa", |
| 323 | "ecdsa", |
| 324 | "rsa" |
| 325 | }; |
| 326 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 327 | #else |
| 328 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 329 | #endif |
| 330 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 331 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 332 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 333 | |
| 334 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 335 | |
| 336 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 337 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 338 | |
| 339 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 340 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 341 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 342 | /* |
| 343 | * This function gives the detail of the SSL error. It is used only |
| 344 | * if the debug mode and the verbose mode are activated. It dump all |
| 345 | * the SSL error until the stack was empty. |
| 346 | */ |
| 347 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 348 | { |
| 349 | unsigned long ret; |
| 350 | |
| 351 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 352 | while(1) { |
| 353 | ret = ERR_get_error(); |
| 354 | if (ret == 0) |
| 355 | return; |
| 356 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 357 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 358 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 363 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 364 | /* |
| 365 | * struct alignment works here such that the key.key is the same as key_data |
| 366 | * Do not change the placement of key_data |
| 367 | */ |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 368 | struct certificate_ocsp { |
| 369 | struct ebmb_node key; |
| 370 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 371 | struct buffer response; |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 372 | long expire; |
| 373 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 374 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 375 | struct ocsp_cbk_arg { |
| 376 | int is_single; |
| 377 | int single_kt; |
| 378 | union { |
| 379 | struct certificate_ocsp *s_ocsp; |
| 380 | /* |
| 381 | * m_ocsp will have multiple entries dependent on key type |
| 382 | * Entry 0 - DSA |
| 383 | * Entry 1 - ECDSA |
| 384 | * Entry 2 - RSA |
| 385 | */ |
| 386 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 387 | }; |
| 388 | }; |
| 389 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 390 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 391 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 392 | { |
| 393 | int err_code = ERR_ABORT; |
| 394 | ENGINE *engine; |
| 395 | struct ssl_engine_list *el; |
| 396 | |
| 397 | /* grab the structural reference to the engine */ |
| 398 | engine = ENGINE_by_id(engine_id); |
| 399 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 400 | 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] | 401 | goto fail_get; |
| 402 | } |
| 403 | |
| 404 | if (!ENGINE_init(engine)) { |
| 405 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 406 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 407 | goto fail_init; |
| 408 | } |
| 409 | |
| 410 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 411 | 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] | 412 | goto fail_set_method; |
| 413 | } |
| 414 | |
| 415 | el = calloc(1, sizeof(*el)); |
| 416 | el->e = engine; |
| 417 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 418 | nb_engines++; |
| 419 | if (global_ssl.async) |
| 420 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 421 | return 0; |
| 422 | |
| 423 | fail_set_method: |
| 424 | /* release the functional reference from ENGINE_init() */ |
| 425 | ENGINE_finish(engine); |
| 426 | |
| 427 | fail_init: |
| 428 | /* release the structural reference from ENGINE_by_id() */ |
| 429 | ENGINE_free(engine); |
| 430 | |
| 431 | fail_get: |
| 432 | return err_code; |
| 433 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 434 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 435 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 436 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 437 | /* |
| 438 | * openssl async fd handler |
| 439 | */ |
| 440 | static void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 441 | { |
| 442 | struct connection *conn = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 443 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 444 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 445 | * to poll this fd until it is requested |
| 446 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 447 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 448 | fd_cant_recv(fd); |
| 449 | |
| 450 | /* crypto engine is available, let's notify the associated |
| 451 | * connection that it can pursue its processing. |
| 452 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 453 | __conn_sock_want_recv(conn); |
| 454 | __conn_sock_want_send(conn); |
| 455 | conn_update_sock_polling(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 458 | /* |
| 459 | * openssl async delayed SSL_free handler |
| 460 | */ |
| 461 | static void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 462 | { |
| 463 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 464 | OSSL_ASYNC_FD all_fd[32]; |
| 465 | size_t num_all_fds = 0; |
| 466 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 467 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 468 | /* We suppose that the async job for a same SSL * |
| 469 | * are serialized. So if we are awake it is |
| 470 | * because the running job has just finished |
| 471 | * and we can remove all async fds safely |
| 472 | */ |
| 473 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 474 | if (num_all_fds > 32) { |
| 475 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 480 | for (i=0 ; i < num_all_fds ; i++) |
| 481 | fd_remove(all_fd[i]); |
| 482 | |
| 483 | /* 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] | 484 | SSL_free(ssl); |
| 485 | sslconns--; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 486 | HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 487 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 488 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 489 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 490 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 491 | */ |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 492 | static void inline ssl_async_process_fds(struct connection *conn, SSL *ssl) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 493 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 494 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 495 | OSSL_ASYNC_FD del_fd[32]; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 496 | size_t num_add_fds = 0; |
| 497 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 498 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 499 | |
| 500 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 501 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 502 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 503 | 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] | 504 | return; |
| 505 | } |
| 506 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 507 | 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] | 508 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 509 | /* We remove unused fds from the fdtab */ |
| 510 | for (i=0 ; i < num_del_fds ; i++) |
| 511 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 512 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 513 | /* We add new fds to the fdtab */ |
| 514 | for (i=0 ; i < num_add_fds ; i++) { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 515 | fd_insert(add_fd[i], conn, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 518 | num_add_fds = 0; |
| 519 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 520 | if (num_add_fds > 32) { |
| 521 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 522 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 523 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 524 | |
| 525 | /* We activate the polling for all known async fds */ |
| 526 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 527 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 528 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 529 | /* To ensure that the fd cache won't be used |
| 530 | * We'll prefer to catch a real RD event |
| 531 | * because handling an EAGAIN on this fd will |
| 532 | * result in a context switch and also |
| 533 | * some engines uses a fd in blocking mode. |
| 534 | */ |
| 535 | fd_cant_recv(add_fd[i]); |
| 536 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 537 | |
| 538 | /* We must also prevent the conn_handler |
| 539 | * to be called until a read event was |
| 540 | * polled on an async fd |
| 541 | */ |
| 542 | __conn_sock_stop_both(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 543 | } |
| 544 | #endif |
| 545 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 546 | /* |
| 547 | * This function returns the number of seconds elapsed |
| 548 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 549 | * date presented un ASN1_GENERALIZEDTIME. |
| 550 | * |
| 551 | * In parsing error case, it returns -1. |
| 552 | */ |
| 553 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 554 | { |
| 555 | long epoch; |
| 556 | char *p, *end; |
| 557 | const unsigned short month_offset[12] = { |
| 558 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 559 | }; |
| 560 | int year, month; |
| 561 | |
| 562 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 563 | |
| 564 | p = (char *)d->data; |
| 565 | end = p + d->length; |
| 566 | |
| 567 | if (end - p < 4) return -1; |
| 568 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 569 | p += 4; |
| 570 | if (end - p < 2) return -1; |
| 571 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 572 | if (month < 1 || month > 12) return -1; |
| 573 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 574 | We consider leap years and the current month (<marsh or not) */ |
| 575 | epoch = ( ((year - 1970) * 365) |
| 576 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 577 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 578 | + month_offset[month-1] |
| 579 | ) * 24 * 60 * 60; |
| 580 | p += 2; |
| 581 | if (end - p < 2) return -1; |
| 582 | /* Add the number of seconds of completed days of current month */ |
| 583 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 584 | p += 2; |
| 585 | if (end - p < 2) return -1; |
| 586 | /* Add the completed hours of the current day */ |
| 587 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 588 | p += 2; |
| 589 | if (end - p < 2) return -1; |
| 590 | /* Add the completed minutes of the current hour */ |
| 591 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 592 | p += 2; |
| 593 | if (p == end) return -1; |
| 594 | /* Test if there is available seconds */ |
| 595 | if (p[0] < '0' || p[0] > '9') |
| 596 | goto nosec; |
| 597 | if (end - p < 2) return -1; |
| 598 | /* Add the seconds of the current minute */ |
| 599 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 600 | p += 2; |
| 601 | if (p == end) return -1; |
| 602 | /* Ignore seconds float part if present */ |
| 603 | if (p[0] == '.') { |
| 604 | do { |
| 605 | if (++p == end) return -1; |
| 606 | } while (p[0] >= '0' && p[0] <= '9'); |
| 607 | } |
| 608 | |
| 609 | nosec: |
| 610 | if (p[0] == 'Z') { |
| 611 | if (end - p != 1) return -1; |
| 612 | return epoch; |
| 613 | } |
| 614 | else if (p[0] == '+') { |
| 615 | if (end - p != 5) return -1; |
| 616 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 617 | 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] | 618 | } |
| 619 | else if (p[0] == '-') { |
| 620 | if (end - p != 5) return -1; |
| 621 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 622 | 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] | 623 | } |
| 624 | |
| 625 | return -1; |
| 626 | } |
| 627 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 628 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 629 | |
| 630 | /* This function starts to check if the OCSP response (in DER format) contained |
| 631 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 632 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 633 | * contained in the OCSP Response and exits on error if no match. |
| 634 | * If it's a valid OCSP Response: |
| 635 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 636 | * pointed by 'ocsp'. |
| 637 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 638 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 639 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 640 | * already present in the container, it will be overwritten. |
| 641 | * |
| 642 | * Note: OCSP response containing more than one OCSP Single response is not |
| 643 | * considered valid. |
| 644 | * |
| 645 | * Returns 0 on success, 1 in error case. |
| 646 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 647 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 648 | struct certificate_ocsp *ocsp, |
| 649 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 650 | { |
| 651 | OCSP_RESPONSE *resp; |
| 652 | OCSP_BASICRESP *bs = NULL; |
| 653 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 654 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 655 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 656 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 657 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 658 | int reason; |
| 659 | int ret = 1; |
| 660 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 661 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 662 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 663 | if (!resp) { |
| 664 | memprintf(err, "Unable to parse OCSP response"); |
| 665 | goto out; |
| 666 | } |
| 667 | |
| 668 | rc = OCSP_response_status(resp); |
| 669 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 670 | memprintf(err, "OCSP response status not successful"); |
| 671 | goto out; |
| 672 | } |
| 673 | |
| 674 | bs = OCSP_response_get1_basic(resp); |
| 675 | if (!bs) { |
| 676 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 677 | goto out; |
| 678 | } |
| 679 | |
| 680 | count_sr = OCSP_resp_count(bs); |
| 681 | if (count_sr > 1) { |
| 682 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 683 | goto out; |
| 684 | } |
| 685 | |
| 686 | sr = OCSP_resp_get0(bs, 0); |
| 687 | if (!sr) { |
| 688 | memprintf(err, "Failed to get OCSP single response"); |
| 689 | goto out; |
| 690 | } |
| 691 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 692 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 693 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 694 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 695 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 696 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 697 | goto out; |
| 698 | } |
| 699 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 700 | if (!nextupd) { |
| 701 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 702 | goto out; |
| 703 | } |
| 704 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 705 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 706 | if (!rc) { |
| 707 | memprintf(err, "OCSP single response: no longer valid."); |
| 708 | goto out; |
| 709 | } |
| 710 | |
| 711 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 712 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 713 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 714 | goto out; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | if (!ocsp) { |
| 719 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 720 | unsigned char *p; |
| 721 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 722 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 723 | if (!rc) { |
| 724 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 725 | goto out; |
| 726 | } |
| 727 | |
| 728 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 729 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 730 | goto out; |
| 731 | } |
| 732 | |
| 733 | p = key; |
| 734 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 735 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 736 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 737 | if (!ocsp) { |
| 738 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 739 | goto out; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | /* According to comments on "chunk_dup", the |
| 744 | previous chunk buffer will be freed */ |
| 745 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 746 | memprintf(err, "OCSP response: Memory allocation error"); |
| 747 | goto out; |
| 748 | } |
| 749 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 750 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 751 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 752 | ret = 0; |
| 753 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 754 | ERR_clear_error(); |
| 755 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 756 | if (bs) |
| 757 | OCSP_BASICRESP_free(bs); |
| 758 | |
| 759 | if (resp) |
| 760 | OCSP_RESPONSE_free(resp); |
| 761 | |
| 762 | return ret; |
| 763 | } |
| 764 | /* |
| 765 | * External function use to update the OCSP response in the OCSP response's |
| 766 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 767 | * to update in DER format. |
| 768 | * |
| 769 | * Returns 0 on success, 1 in error case. |
| 770 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 771 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 772 | { |
| 773 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * This function load the OCSP Resonse in DER format contained in file at |
| 778 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 779 | * |
| 780 | * Returns 0 on success, 1 in error case. |
| 781 | */ |
| 782 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 783 | { |
| 784 | int fd = -1; |
| 785 | int r = 0; |
| 786 | int ret = 1; |
| 787 | |
| 788 | fd = open(ocsp_path, O_RDONLY); |
| 789 | if (fd == -1) { |
| 790 | memprintf(err, "Error opening OCSP response file"); |
| 791 | goto end; |
| 792 | } |
| 793 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 794 | trash.data = 0; |
| 795 | while (trash.data < trash.size) { |
| 796 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 797 | if (r < 0) { |
| 798 | if (errno == EINTR) |
| 799 | continue; |
| 800 | |
| 801 | memprintf(err, "Error reading OCSP response from file"); |
| 802 | goto end; |
| 803 | } |
| 804 | else if (r == 0) { |
| 805 | break; |
| 806 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 807 | trash.data += r; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | close(fd); |
| 811 | fd = -1; |
| 812 | |
| 813 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 814 | end: |
| 815 | if (fd != -1) |
| 816 | close(fd); |
| 817 | |
| 818 | return ret; |
| 819 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 820 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 821 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 822 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 823 | 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) |
| 824 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 825 | struct tls_keys_ref *ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 826 | struct tls_sess_key *keys; |
| 827 | struct connection *conn; |
| 828 | int head; |
| 829 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 830 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 831 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 832 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 833 | ref = objt_listener(conn->target)->bind_conf->keys_ref; |
| 834 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 835 | |
| 836 | keys = ref->tlskeys; |
| 837 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 838 | |
| 839 | if (enc) { |
| 840 | memcpy(key_name, keys[head].name, 16); |
| 841 | |
| 842 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 843 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 844 | |
| 845 | 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] | 846 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 847 | |
| 848 | HMAC_Init_ex(hctx, keys[head].hmac_key, 16, HASH_FUNCT(), NULL); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 849 | ret = 1; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 850 | } else { |
| 851 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 852 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 853 | goto found; |
| 854 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 855 | ret = 0; |
| 856 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 857 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 858 | found: |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 859 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].hmac_key, 16, HASH_FUNCT(), NULL); |
| 860 | 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] | 861 | goto end; |
| 862 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 863 | /* 2 for key renewal, 1 if current key is still valid */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 864 | ret = i ? 2 : 1; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 865 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 866 | end: |
| 867 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 868 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 872 | { |
| 873 | struct tls_keys_ref *ref; |
| 874 | |
| 875 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 876 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 877 | return ref; |
| 878 | return NULL; |
| 879 | } |
| 880 | |
| 881 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 882 | { |
| 883 | struct tls_keys_ref *ref; |
| 884 | |
| 885 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 886 | if (ref->unique_id == unique_id) |
| 887 | return ref; |
| 888 | return NULL; |
| 889 | } |
| 890 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 891 | void ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
| 892 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 893 | { |
| 894 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 895 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 896 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 897 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 898 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 899 | } |
| 900 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 901 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 902 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 903 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 904 | |
| 905 | if(!ref) { |
| 906 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 907 | return 1; |
| 908 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 909 | ssl_sock_update_tlskey_ref(ref, tlskey); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 910 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 911 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 912 | |
| 913 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 914 | * automatic ids. It's called just after the basic checks. It returns |
| 915 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 916 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 917 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 918 | { |
| 919 | int i = 0; |
| 920 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 921 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 922 | |
| 923 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 924 | if (ref->unique_id == -1) { |
| 925 | /* Look for the first free id. */ |
| 926 | while (1) { |
| 927 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 928 | if (ref2->unique_id == i) { |
| 929 | i++; |
| 930 | break; |
| 931 | } |
| 932 | } |
| 933 | if (&ref2->list == &tlskeys_reference) |
| 934 | break; |
| 935 | } |
| 936 | |
| 937 | /* Uses the unique id and increment it for the next entry. */ |
| 938 | ref->unique_id = i; |
| 939 | i++; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | /* This sort the reference list by id. */ |
| 944 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 945 | LIST_DEL(&ref->list); |
| 946 | list_for_each_entry(ref3, &tkr, list) { |
| 947 | if (ref->unique_id < ref3->unique_id) { |
| 948 | LIST_ADDQ(&ref3->list, &ref->list); |
| 949 | break; |
| 950 | } |
| 951 | } |
| 952 | if (&ref3->list == &tkr) |
| 953 | LIST_ADDQ(&tkr, &ref->list); |
| 954 | } |
| 955 | |
| 956 | /* swap root */ |
| 957 | LIST_ADD(&tkr, &tlskeys_reference); |
| 958 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 959 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 960 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 961 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 962 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 963 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 964 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 965 | { |
| 966 | switch (evp_keytype) { |
| 967 | case EVP_PKEY_RSA: |
| 968 | return 2; |
| 969 | case EVP_PKEY_DSA: |
| 970 | return 0; |
| 971 | case EVP_PKEY_EC: |
| 972 | return 1; |
| 973 | } |
| 974 | |
| 975 | return -1; |
| 976 | } |
| 977 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 978 | /* |
| 979 | * Callback used to set OCSP status extension content in server hello. |
| 980 | */ |
| 981 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 982 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 983 | struct certificate_ocsp *ocsp; |
| 984 | struct ocsp_cbk_arg *ocsp_arg; |
| 985 | char *ssl_buf; |
| 986 | EVP_PKEY *ssl_pkey; |
| 987 | int key_type; |
| 988 | int index; |
| 989 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 990 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 991 | |
| 992 | ssl_pkey = SSL_get_privatekey(ssl); |
| 993 | if (!ssl_pkey) |
| 994 | return SSL_TLSEXT_ERR_NOACK; |
| 995 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 996 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 997 | |
| 998 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 999 | ocsp = ocsp_arg->s_ocsp; |
| 1000 | else { |
| 1001 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1002 | * the certificate type |
| 1003 | */ |
| 1004 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1005 | |
| 1006 | if (index < 0) |
| 1007 | return SSL_TLSEXT_ERR_NOACK; |
| 1008 | |
| 1009 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1010 | |
| 1011 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1012 | |
| 1013 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1014 | !ocsp->response.area || |
| 1015 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1016 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1017 | return SSL_TLSEXT_ERR_NOACK; |
| 1018 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1019 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1020 | if (!ssl_buf) |
| 1021 | return SSL_TLSEXT_ERR_NOACK; |
| 1022 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1023 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1024 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1025 | |
| 1026 | return SSL_TLSEXT_ERR_OK; |
| 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 1031 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 1032 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 1033 | * It should be present in the certificate's extra chain builded from file |
| 1034 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 1035 | * named 'cert_path' suffixed using '.issuer'. |
| 1036 | * |
| 1037 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 1038 | * response. If file is empty or content is not a valid OCSP response, |
| 1039 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 1040 | * is displayed). |
| 1041 | * |
| 1042 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
| 1043 | * succesfully enabled, or -1 in other error case. |
| 1044 | */ |
| 1045 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 1046 | { |
| 1047 | |
| 1048 | BIO *in = NULL; |
| 1049 | X509 *x, *xi = NULL, *issuer = NULL; |
| 1050 | STACK_OF(X509) *chain = NULL; |
| 1051 | OCSP_CERTID *cid = NULL; |
| 1052 | SSL *ssl; |
| 1053 | char ocsp_path[MAXPATHLEN+1]; |
| 1054 | int i, ret = -1; |
| 1055 | struct stat st; |
| 1056 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1057 | char *warn = NULL; |
| 1058 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1059 | pem_password_cb *passwd_cb; |
| 1060 | void *passwd_cb_userdata; |
| 1061 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1062 | |
| 1063 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1064 | |
| 1065 | if (stat(ocsp_path, &st)) |
| 1066 | return 1; |
| 1067 | |
| 1068 | ssl = SSL_new(ctx); |
| 1069 | if (!ssl) |
| 1070 | goto out; |
| 1071 | |
| 1072 | x = SSL_get_certificate(ssl); |
| 1073 | if (!x) |
| 1074 | goto out; |
| 1075 | |
| 1076 | /* Try to lookup for issuer in certificate extra chain */ |
| 1077 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 1078 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 1079 | #else |
| 1080 | chain = ctx->extra_certs; |
| 1081 | #endif |
| 1082 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1083 | issuer = sk_X509_value(chain, i); |
| 1084 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 1085 | break; |
| 1086 | else |
| 1087 | issuer = NULL; |
| 1088 | } |
| 1089 | |
| 1090 | /* If not found try to load issuer from a suffixed file */ |
| 1091 | if (!issuer) { |
| 1092 | char issuer_path[MAXPATHLEN+1]; |
| 1093 | |
| 1094 | in = BIO_new(BIO_s_file()); |
| 1095 | if (!in) |
| 1096 | goto out; |
| 1097 | |
| 1098 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 1099 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 1100 | goto out; |
| 1101 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1102 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 1103 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 1104 | |
| 1105 | 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] | 1106 | if (!xi) |
| 1107 | goto out; |
| 1108 | |
| 1109 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 1110 | goto out; |
| 1111 | |
| 1112 | issuer = xi; |
| 1113 | } |
| 1114 | |
| 1115 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1116 | if (!cid) |
| 1117 | goto out; |
| 1118 | |
| 1119 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1120 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1121 | goto out; |
| 1122 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1123 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1124 | if (!ocsp) |
| 1125 | goto out; |
| 1126 | |
| 1127 | p = ocsp->key_data; |
| 1128 | i2d_OCSP_CERTID(cid, &p); |
| 1129 | |
| 1130 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1131 | if (iocsp == ocsp) |
| 1132 | ocsp = NULL; |
| 1133 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1134 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1135 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1136 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1137 | #endif |
| 1138 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1139 | |
| 1140 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1141 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1142 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1143 | |
| 1144 | cb_arg->is_single = 1; |
| 1145 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1146 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1147 | pkey = X509_get_pubkey(x); |
| 1148 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1149 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1150 | |
| 1151 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1152 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1153 | } else { |
| 1154 | /* |
| 1155 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1156 | * Update that cb_arg with the new cert's staple |
| 1157 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1158 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1159 | struct certificate_ocsp *tmp_ocsp; |
| 1160 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1161 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1162 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1163 | |
| 1164 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1165 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1166 | #else |
| 1167 | cb_arg = ctx->tlsext_status_arg; |
| 1168 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1169 | |
| 1170 | /* |
| 1171 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1172 | * the order of operations below matter, take care when changing it |
| 1173 | */ |
| 1174 | tmp_ocsp = cb_arg->s_ocsp; |
| 1175 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1176 | cb_arg->s_ocsp = NULL; |
| 1177 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1178 | cb_arg->is_single = 0; |
| 1179 | cb_arg->single_kt = 0; |
| 1180 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1181 | pkey = X509_get_pubkey(x); |
| 1182 | key_type = EVP_PKEY_base_id(pkey); |
| 1183 | EVP_PKEY_free(pkey); |
| 1184 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1185 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1186 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1187 | cb_arg->m_ocsp[index] = iocsp; |
| 1188 | |
| 1189 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1190 | |
| 1191 | ret = 0; |
| 1192 | |
| 1193 | warn = NULL; |
| 1194 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 1195 | 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] | 1196 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | out: |
| 1200 | if (ssl) |
| 1201 | SSL_free(ssl); |
| 1202 | |
| 1203 | if (in) |
| 1204 | BIO_free(in); |
| 1205 | |
| 1206 | if (xi) |
| 1207 | X509_free(xi); |
| 1208 | |
| 1209 | if (cid) |
| 1210 | OCSP_CERTID_free(cid); |
| 1211 | |
| 1212 | if (ocsp) |
| 1213 | free(ocsp); |
| 1214 | |
| 1215 | if (warn) |
| 1216 | free(warn); |
| 1217 | |
| 1218 | |
| 1219 | return ret; |
| 1220 | } |
| 1221 | |
| 1222 | #endif |
| 1223 | |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1224 | #ifdef OPENSSL_IS_BORINGSSL |
| 1225 | static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path) |
| 1226 | { |
| 1227 | char ocsp_path[MAXPATHLEN+1]; |
| 1228 | struct stat st; |
| 1229 | int fd = -1, r = 0; |
| 1230 | |
| 1231 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1232 | if (stat(ocsp_path, &st)) |
| 1233 | return 0; |
| 1234 | |
| 1235 | fd = open(ocsp_path, O_RDONLY); |
| 1236 | if (fd == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1237 | ha_warning("Error opening OCSP response file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1238 | return -1; |
| 1239 | } |
| 1240 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1241 | trash.data = 0; |
| 1242 | while (trash.data < trash.size) { |
| 1243 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1244 | if (r < 0) { |
| 1245 | if (errno == EINTR) |
| 1246 | continue; |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1247 | ha_warning("Error reading OCSP response from file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1248 | close(fd); |
| 1249 | return -1; |
| 1250 | } |
| 1251 | else if (r == 0) { |
| 1252 | break; |
| 1253 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1254 | trash.data += r; |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1255 | } |
| 1256 | close(fd); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1257 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *) trash.area, |
| 1258 | trash.data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1259 | } |
| 1260 | #endif |
| 1261 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 1262 | #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] | 1263 | |
| 1264 | #define CT_EXTENSION_TYPE 18 |
| 1265 | |
| 1266 | static int sctl_ex_index = -1; |
| 1267 | |
| 1268 | /* |
| 1269 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1270 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1271 | * is performed. |
| 1272 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1273 | static int ssl_sock_parse_sctl(struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1274 | { |
| 1275 | int ret = 1; |
| 1276 | int len, pos, sct_len; |
| 1277 | unsigned char *data; |
| 1278 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1279 | if (sctl->data < 2) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1280 | goto out; |
| 1281 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1282 | data = (unsigned char *) sctl->area; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1283 | len = (data[0] << 8) | data[1]; |
| 1284 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1285 | if (len + 2 != sctl->data) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1286 | goto out; |
| 1287 | |
| 1288 | data = data + 2; |
| 1289 | pos = 0; |
| 1290 | while (pos < len) { |
| 1291 | if (len - pos < 2) |
| 1292 | goto out; |
| 1293 | |
| 1294 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1295 | if (pos + sct_len + 2 > len) |
| 1296 | goto out; |
| 1297 | |
| 1298 | pos += sct_len + 2; |
| 1299 | } |
| 1300 | |
| 1301 | ret = 0; |
| 1302 | |
| 1303 | out: |
| 1304 | return ret; |
| 1305 | } |
| 1306 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1307 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, |
| 1308 | struct buffer **sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1309 | { |
| 1310 | int fd = -1; |
| 1311 | int r = 0; |
| 1312 | int ret = 1; |
| 1313 | |
| 1314 | *sctl = NULL; |
| 1315 | |
| 1316 | fd = open(sctl_path, O_RDONLY); |
| 1317 | if (fd == -1) |
| 1318 | goto end; |
| 1319 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1320 | trash.data = 0; |
| 1321 | while (trash.data < trash.size) { |
| 1322 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1323 | if (r < 0) { |
| 1324 | if (errno == EINTR) |
| 1325 | continue; |
| 1326 | |
| 1327 | goto end; |
| 1328 | } |
| 1329 | else if (r == 0) { |
| 1330 | break; |
| 1331 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1332 | trash.data += r; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | ret = ssl_sock_parse_sctl(&trash); |
| 1336 | if (ret) |
| 1337 | goto end; |
| 1338 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1339 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1340 | if (!chunk_dup(*sctl, &trash)) { |
| 1341 | free(*sctl); |
| 1342 | *sctl = NULL; |
| 1343 | goto end; |
| 1344 | } |
| 1345 | |
| 1346 | end: |
| 1347 | if (fd != -1) |
| 1348 | close(fd); |
| 1349 | |
| 1350 | return ret; |
| 1351 | } |
| 1352 | |
| 1353 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1354 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1355 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1356 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1357 | *out = (unsigned char *) sctl->area; |
| 1358 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1359 | |
| 1360 | return 1; |
| 1361 | } |
| 1362 | |
| 1363 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1364 | { |
| 1365 | return 1; |
| 1366 | } |
| 1367 | |
| 1368 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 1369 | { |
| 1370 | char sctl_path[MAXPATHLEN+1]; |
| 1371 | int ret = -1; |
| 1372 | struct stat st; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1373 | struct buffer *sctl = NULL; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1374 | |
| 1375 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 1376 | |
| 1377 | if (stat(sctl_path, &st)) |
| 1378 | return 1; |
| 1379 | |
| 1380 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 1381 | goto out; |
| 1382 | |
| 1383 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 1384 | free(sctl); |
| 1385 | goto out; |
| 1386 | } |
| 1387 | |
| 1388 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1389 | |
| 1390 | ret = 0; |
| 1391 | |
| 1392 | out: |
| 1393 | return ret; |
| 1394 | } |
| 1395 | |
| 1396 | #endif |
| 1397 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1398 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1399 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1400 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1401 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1402 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1403 | |
| 1404 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1405 | /* Disable renegotiation (CVE-2009-3555) */ |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 1406 | 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] | 1407 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1408 | conn->err_code = CO_ER_SSL_RENEG; |
| 1409 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1410 | } |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1411 | |
| 1412 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 1413 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 1414 | /* Long certificate chains optimz |
| 1415 | If write and read bios are differents, we |
| 1416 | consider that the buffering was activated, |
| 1417 | so we rise the output buffer size from 4k |
| 1418 | to 16k */ |
| 1419 | write_bio = SSL_get_wbio(ssl); |
| 1420 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1421 | BIO_set_write_buffer_size(write_bio, 16384); |
| 1422 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 1423 | } |
| 1424 | } |
| 1425 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1426 | } |
| 1427 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1428 | /* Callback is called for each certificate of the chain during a verify |
| 1429 | ok is set to 1 if preverify detect no error on current certificate. |
| 1430 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1431 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1432 | { |
| 1433 | SSL *ssl; |
| 1434 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1435 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1436 | |
| 1437 | ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1438 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1439 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1440 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1441 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1442 | if (ok) /* no errors */ |
| 1443 | return ok; |
| 1444 | |
| 1445 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1446 | err = X509_STORE_CTX_get_error(x_store); |
| 1447 | |
| 1448 | /* check if CA error needs to be ignored */ |
| 1449 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1450 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 1451 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1452 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1453 | } |
| 1454 | |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1455 | 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] | 1456 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1457 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1458 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1459 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1460 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1461 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1462 | return 0; |
| 1463 | } |
| 1464 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1465 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 1466 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1467 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1468 | /* check if certificate error needs to be ignored */ |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1469 | 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] | 1470 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1471 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1472 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1473 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1474 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1475 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1476 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1477 | } |
| 1478 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1479 | static inline |
| 1480 | 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] | 1481 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1482 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1483 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1484 | unsigned char *msg; |
| 1485 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1486 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1487 | |
| 1488 | /* This function is called for "from client" and "to server" |
| 1489 | * connections. The combination of write_p == 0 and content_type == 22 |
| 1490 | * is only avalaible during "from client" connection. |
| 1491 | */ |
| 1492 | |
| 1493 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1494 | * otherwise it is set to 1. |
| 1495 | */ |
| 1496 | if (write_p != 0) |
| 1497 | return; |
| 1498 | |
| 1499 | /* content_type contains the type of message received or sent |
| 1500 | * according with the SSL/TLS protocol spec. This message is |
| 1501 | * encoded with one byte. The value 256 (two bytes) is used |
| 1502 | * for designing the SSL/TLS record layer. According with the |
| 1503 | * rfc6101, the expected message (other than 256) are: |
| 1504 | * - change_cipher_spec(20) |
| 1505 | * - alert(21) |
| 1506 | * - handshake(22) |
| 1507 | * - application_data(23) |
| 1508 | * - (255) |
| 1509 | * We are interessed by the handshake and specially the client |
| 1510 | * hello. |
| 1511 | */ |
| 1512 | if (content_type != 22) |
| 1513 | return; |
| 1514 | |
| 1515 | /* The message length is at least 4 bytes, containing the |
| 1516 | * message type and the message length. |
| 1517 | */ |
| 1518 | if (len < 4) |
| 1519 | return; |
| 1520 | |
| 1521 | /* First byte of the handshake message id the type of |
| 1522 | * message. The konwn types are: |
| 1523 | * - hello_request(0) |
| 1524 | * - client_hello(1) |
| 1525 | * - server_hello(2) |
| 1526 | * - certificate(11) |
| 1527 | * - server_key_exchange (12) |
| 1528 | * - certificate_request(13) |
| 1529 | * - server_hello_done(14) |
| 1530 | * We are interested by the client hello. |
| 1531 | */ |
| 1532 | msg = (unsigned char *)buf; |
| 1533 | if (msg[0] != 1) |
| 1534 | return; |
| 1535 | |
| 1536 | /* Next three bytes are the length of the message. The total length |
| 1537 | * must be this decoded length + 4. If the length given as argument |
| 1538 | * is not the same, we abort the protocol dissector. |
| 1539 | */ |
| 1540 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1541 | if (len < rec_len + 4) |
| 1542 | return; |
| 1543 | msg += 4; |
| 1544 | end = msg + rec_len; |
| 1545 | if (end < msg) |
| 1546 | return; |
| 1547 | |
| 1548 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1549 | * for minor, the random, composed by 4 bytes for the unix time and |
| 1550 | * 28 bytes for unix payload, and them 1 byte for the session id. So |
| 1551 | * we jump 1 + 1 + 4 + 28 + 1 bytes. |
| 1552 | */ |
| 1553 | msg += 1 + 1 + 4 + 28 + 1; |
| 1554 | if (msg > end) |
| 1555 | return; |
| 1556 | |
| 1557 | /* Next two bytes are the ciphersuite length. */ |
| 1558 | if (msg + 2 > end) |
| 1559 | return; |
| 1560 | rec_len = (msg[0] << 8) + msg[1]; |
| 1561 | msg += 2; |
| 1562 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1563 | return; |
| 1564 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1565 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1566 | if (!capture) |
| 1567 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1568 | /* Compute the xxh64 of the ciphersuite. */ |
| 1569 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1570 | |
| 1571 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1572 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1573 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1574 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1575 | |
| 1576 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1577 | } |
| 1578 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1579 | /* Callback is called for ssl protocol analyse */ |
| 1580 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1581 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1582 | #ifdef TLS1_RT_HEARTBEAT |
| 1583 | /* test heartbeat received (write_p is set to 0 |
| 1584 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1585 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1586 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1587 | const unsigned char *p = buf; |
| 1588 | unsigned int payload; |
| 1589 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1590 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1591 | |
| 1592 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1593 | if (*p != TLS1_HB_REQUEST) |
| 1594 | return; |
| 1595 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1596 | 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] | 1597 | goto kill_it; |
| 1598 | |
| 1599 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1600 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1601 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1602 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1603 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1604 | * advertised payload is larger than the advertised packet |
| 1605 | * length, so we have garbage in the buffer between the |
| 1606 | * payload and the end of the buffer (p+len). We can't know |
| 1607 | * if the SSL stack is patched, and we don't know if we can |
| 1608 | * safely wipe out the area between p+3+len and payload. |
| 1609 | * So instead, we prevent the response from being sent by |
| 1610 | * setting the max_send_fragment to 0 and we report an SSL |
| 1611 | * error, which will kill this connection. It will be reported |
| 1612 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1613 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1614 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1615 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1616 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1617 | return; |
| 1618 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1619 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1620 | if (global_ssl.capture_cipherlist > 0) |
| 1621 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1622 | } |
| 1623 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1624 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1625 | /* This callback is used so that the server advertises the list of |
| 1626 | * negociable protocols for NPN. |
| 1627 | */ |
| 1628 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1629 | unsigned int *len, void *arg) |
| 1630 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1631 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1632 | |
| 1633 | *data = (const unsigned char *)conf->npn_str; |
| 1634 | *len = conf->npn_len; |
| 1635 | return SSL_TLSEXT_ERR_OK; |
| 1636 | } |
| 1637 | #endif |
| 1638 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1639 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1640 | /* This callback is used so that the server advertises the list of |
| 1641 | * negociable protocols for ALPN. |
| 1642 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1643 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1644 | unsigned char *outlen, |
| 1645 | const unsigned char *server, |
| 1646 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1647 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1648 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1649 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1650 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1651 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1652 | return SSL_TLSEXT_ERR_NOACK; |
| 1653 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1654 | return SSL_TLSEXT_ERR_OK; |
| 1655 | } |
| 1656 | #endif |
| 1657 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1658 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1659 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1660 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1661 | /* Create a X509 certificate with the specified servername and serial. This |
| 1662 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1663 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1664 | 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] | 1665 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1666 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1667 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1668 | SSL_CTX *ssl_ctx = NULL; |
| 1669 | X509 *newcrt = NULL; |
| 1670 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1671 | SSL *tmp_ssl = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1672 | X509_NAME *name; |
| 1673 | const EVP_MD *digest; |
| 1674 | X509V3_CTX ctx; |
| 1675 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1676 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1677 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1678 | /* Get the private key of the default certificate and use it */ |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1679 | #if (OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined LIBRESSL_VERSION_NUMBER) |
| 1680 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1681 | #else |
| 1682 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1683 | if (tmp_ssl) |
| 1684 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1685 | #endif |
| 1686 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1687 | goto mkcert_error; |
| 1688 | |
| 1689 | /* Create the certificate */ |
| 1690 | if (!(newcrt = X509_new())) |
| 1691 | goto mkcert_error; |
| 1692 | |
| 1693 | /* Set version number for the certificate (X509v3) and the serial |
| 1694 | * number */ |
| 1695 | if (X509_set_version(newcrt, 2L) != 1) |
| 1696 | goto mkcert_error; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1697 | 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] | 1698 | |
| 1699 | /* Set duration for the certificate */ |
| 1700 | if (!X509_gmtime_adj(X509_get_notBefore(newcrt), (long)-60*60*24) || |
| 1701 | !X509_gmtime_adj(X509_get_notAfter(newcrt),(long)60*60*24*365)) |
| 1702 | goto mkcert_error; |
| 1703 | |
| 1704 | /* set public key in the certificate */ |
| 1705 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1706 | goto mkcert_error; |
| 1707 | |
| 1708 | /* Set issuer name from the CA */ |
| 1709 | if (!(name = X509_get_subject_name(cacert))) |
| 1710 | goto mkcert_error; |
| 1711 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1712 | goto mkcert_error; |
| 1713 | |
| 1714 | /* Set the subject name using the same, but the CN */ |
| 1715 | name = X509_NAME_dup(name); |
| 1716 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1717 | (const unsigned char *)servername, |
| 1718 | -1, -1, 0) != 1) { |
| 1719 | X509_NAME_free(name); |
| 1720 | goto mkcert_error; |
| 1721 | } |
| 1722 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1723 | X509_NAME_free(name); |
| 1724 | goto mkcert_error; |
| 1725 | } |
| 1726 | X509_NAME_free(name); |
| 1727 | |
| 1728 | /* Add x509v3 extensions as specified */ |
| 1729 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1730 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1731 | X509_EXTENSION *ext; |
| 1732 | |
| 1733 | if (!(ext = X509V3_EXT_conf(NULL, &ctx, x509v3_ext_names[i], x509v3_ext_values[i]))) |
| 1734 | goto mkcert_error; |
| 1735 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1736 | X509_EXTENSION_free(ext); |
| 1737 | goto mkcert_error; |
| 1738 | } |
| 1739 | X509_EXTENSION_free(ext); |
| 1740 | } |
| 1741 | |
| 1742 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1743 | |
| 1744 | key_type = EVP_PKEY_base_id(capkey); |
| 1745 | |
| 1746 | if (key_type == EVP_PKEY_DSA) |
| 1747 | digest = EVP_sha1(); |
| 1748 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1749 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1750 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1751 | digest = EVP_sha256(); |
| 1752 | else { |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1753 | #if (OPENSSL_VERSION_NUMBER >= 0x1000000fL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1754 | int nid; |
| 1755 | |
| 1756 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1757 | goto mkcert_error; |
| 1758 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1759 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1760 | #else |
| 1761 | goto mkcert_error; |
| 1762 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1763 | } |
| 1764 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1765 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1766 | goto mkcert_error; |
| 1767 | |
| 1768 | /* Create and set the new SSL_CTX */ |
| 1769 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1770 | goto mkcert_error; |
| 1771 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1772 | goto mkcert_error; |
| 1773 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1774 | goto mkcert_error; |
| 1775 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1776 | goto mkcert_error; |
| 1777 | |
| 1778 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1779 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1780 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1781 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1782 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1783 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1784 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1785 | 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] | 1786 | EC_KEY *ecc; |
| 1787 | int nid; |
| 1788 | |
| 1789 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1790 | goto end; |
| 1791 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1792 | goto end; |
| 1793 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1794 | EC_KEY_free(ecc); |
| 1795 | } |
| 1796 | #endif |
| 1797 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1798 | return ssl_ctx; |
| 1799 | |
| 1800 | mkcert_error: |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1801 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1802 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1803 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1804 | return NULL; |
| 1805 | } |
| 1806 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1807 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1808 | 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] | 1809 | { |
| 1810 | struct bind_conf *bind_conf = objt_listener(conn->target)->bind_conf; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1811 | |
| 1812 | return ssl_sock_do_create_cert(servername, bind_conf, conn->xprt_ctx); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1813 | } |
| 1814 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1815 | /* 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] | 1816 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1817 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1818 | 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] | 1819 | { |
| 1820 | struct lru64 *lru = NULL; |
| 1821 | |
| 1822 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1823 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1824 | 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] | 1825 | if (lru && lru->domain) { |
| 1826 | if (ssl) |
| 1827 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1828 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1829 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1830 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1831 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1832 | } |
| 1833 | return NULL; |
| 1834 | } |
| 1835 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1836 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 1837 | * function is not thread-safe, it should only be used to check if a certificate |
| 1838 | * exists in the lru cache (with no warranty it will not be removed by another |
| 1839 | * thread). It is kept for backward compatibility. */ |
| 1840 | SSL_CTX * |
| 1841 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 1842 | { |
| 1843 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 1844 | } |
| 1845 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1846 | /* Set a certificate int the LRU cache used to store generated |
| 1847 | * certificate. Return 0 on success, otherwise -1 */ |
| 1848 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1849 | 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] | 1850 | { |
| 1851 | struct lru64 *lru = NULL; |
| 1852 | |
| 1853 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1854 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1855 | 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] | 1856 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1857 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1858 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1859 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1860 | if (lru->domain && lru->data) |
| 1861 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1862 | 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] | 1863 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1864 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1865 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1866 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1867 | } |
| 1868 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1869 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1870 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1871 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1872 | { |
| 1873 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 1874 | } |
| 1875 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1876 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 1877 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 1878 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1879 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1880 | 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] | 1881 | { |
| 1882 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1883 | SSL_CTX *ssl_ctx = NULL; |
| 1884 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1885 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1886 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1887 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1888 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1889 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1890 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1891 | if (lru && lru->domain) |
| 1892 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1893 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1894 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1895 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1896 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1897 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1898 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1899 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1900 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1901 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1902 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1903 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 1904 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 1905 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1906 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1907 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1908 | return 0; |
| 1909 | } |
| 1910 | static int |
| 1911 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 1912 | { |
| 1913 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1914 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1915 | |
| 1916 | conn_get_to_addr(conn); |
| 1917 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
| 1918 | 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] | 1919 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1920 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1921 | } |
| 1922 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1923 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1924 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1925 | |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1926 | |
| 1927 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 1928 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 1929 | #endif |
| 1930 | |
| 1931 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 1932 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
| 1933 | #define SSL_renegotiate_pending(arg) 0 |
| 1934 | #endif |
| 1935 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 1936 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1937 | #endif |
| 1938 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 1939 | #define SSL_OP_NO_TICKET 0 |
| 1940 | #endif |
| 1941 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 1942 | #define SSL_OP_NO_COMPRESSION 0 |
| 1943 | #endif |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 1944 | #ifdef OPENSSL_NO_SSL3 /* SSLv3 support removed */ |
| 1945 | #undef SSL_OP_NO_SSLv3 |
| 1946 | #define SSL_OP_NO_SSLv3 0 |
| 1947 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1948 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 1949 | #define SSL_OP_NO_TLSv1_1 0 |
| 1950 | #endif |
| 1951 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 1952 | #define SSL_OP_NO_TLSv1_2 0 |
| 1953 | #endif |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 1954 | #ifndef SSL_OP_NO_TLSv1_3 /* needs OpenSSL >= 1.1.1 */ |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1955 | #define SSL_OP_NO_TLSv1_3 0 |
| 1956 | #endif |
| 1957 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 1958 | #define SSL_OP_SINGLE_DH_USE 0 |
| 1959 | #endif |
| 1960 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 1961 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1962 | #endif |
| 1963 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 1964 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 1965 | #endif |
| 1966 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 1967 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 1968 | #endif |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 1969 | #ifndef SSL_OP_PRIORITIZE_CHACHA /* needs OpenSSL >= 1.1.1 */ |
| 1970 | #define SSL_OP_PRIORITIZE_CHACHA 0 |
| 1971 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1972 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 1973 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1974 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 1975 | |
| 1976 | 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] | 1977 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 1978 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1979 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1980 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 1981 | #endif |
| 1982 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1983 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 1984 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 1985 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 1986 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1987 | 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] | 1988 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1989 | 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] | 1990 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 1991 | #endif |
| 1992 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1993 | 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] | 1994 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 1995 | 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] | 1996 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 1997 | #endif |
| 1998 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 1999 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2000 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2001 | /* Unusable in this context. */ |
| 2002 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2003 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2004 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2005 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2006 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2007 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2008 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 2009 | |
| 2010 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2011 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2012 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2013 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2014 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2015 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2016 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2017 | } |
| 2018 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2019 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2020 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2021 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2022 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2023 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2024 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2025 | } |
| 2026 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2027 | 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] | 2028 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2029 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2030 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2031 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2032 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2033 | } |
| 2034 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2035 | 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] | 2036 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2037 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2038 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2039 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2040 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2041 | } |
| 2042 | 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] | 2043 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2044 | 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] | 2045 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2046 | #endif |
| 2047 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2048 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2049 | #if SSL_OP_NO_TLSv1_3 |
| 2050 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2051 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2052 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2053 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2054 | #endif |
| 2055 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2056 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2057 | |
| 2058 | static struct { |
| 2059 | int option; |
| 2060 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2061 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 2062 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2063 | const char *name; |
| 2064 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2065 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2066 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2067 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2068 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2069 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2070 | {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] | 2071 | }; |
| 2072 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2073 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2074 | { |
| 2075 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2076 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2077 | SSL_set_SSL_CTX(ssl, ctx); |
| 2078 | } |
| 2079 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2080 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2081 | |
| 2082 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2083 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2084 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2085 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2086 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2087 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2088 | return SSL_TLSEXT_ERR_OK; |
| 2089 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2090 | } |
| 2091 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2092 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2093 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2094 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2095 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2096 | #else |
| 2097 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2098 | { |
| 2099 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2100 | struct connection *conn; |
| 2101 | struct bind_conf *s; |
| 2102 | const uint8_t *extension_data; |
| 2103 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2104 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2105 | |
| 2106 | char *wildp = NULL; |
| 2107 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2108 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2109 | 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] | 2110 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2111 | int i; |
| 2112 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2113 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2114 | s = objt_listener(conn->target)->bind_conf; |
| 2115 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2116 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2117 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2118 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2119 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2120 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2121 | #else |
| 2122 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2123 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2124 | /* |
| 2125 | * The server_name extension was given too much extensibility when it |
| 2126 | * was written, so parsing the normal case is a bit complex. |
| 2127 | */ |
| 2128 | size_t len; |
| 2129 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2130 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2131 | /* Extract the length of the supplied list of names. */ |
| 2132 | len = (*extension_data++) << 8; |
| 2133 | len |= *extension_data++; |
| 2134 | if (len + 2 != extension_len) |
| 2135 | goto abort; |
| 2136 | /* |
| 2137 | * The list in practice only has a single element, so we only consider |
| 2138 | * the first one. |
| 2139 | */ |
| 2140 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2141 | goto abort; |
| 2142 | extension_len = len - 1; |
| 2143 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2144 | if (extension_len <= 2) |
| 2145 | goto abort; |
| 2146 | len = (*extension_data++) << 8; |
| 2147 | len |= *extension_data++; |
| 2148 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2149 | || memchr(extension_data, 0, len) != NULL) |
| 2150 | goto abort; |
| 2151 | servername = extension_data; |
| 2152 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2153 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2154 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2155 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2156 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2157 | } |
| 2158 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2159 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2160 | if (!s->strict_sni) { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2161 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2162 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2163 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2164 | goto abort; |
| 2165 | } |
| 2166 | |
| 2167 | /* extract/check clientHello informations */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2168 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2169 | 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] | 2170 | #else |
| 2171 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2172 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2173 | uint8_t sign; |
| 2174 | size_t len; |
| 2175 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2176 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2177 | len = (*extension_data++) << 8; |
| 2178 | len |= *extension_data++; |
| 2179 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2180 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2181 | if (len % 2 != 0) |
| 2182 | goto abort; |
| 2183 | for (; len > 0; len -= 2) { |
| 2184 | extension_data++; /* hash */ |
| 2185 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2186 | switch (sign) { |
| 2187 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2188 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2189 | break; |
| 2190 | case TLSEXT_signature_ecdsa: |
| 2191 | has_ecdsa_sig = 1; |
| 2192 | break; |
| 2193 | default: |
| 2194 | continue; |
| 2195 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2196 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2197 | break; |
| 2198 | } |
| 2199 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2200 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2201 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2202 | } |
| 2203 | 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] | 2204 | const SSL_CIPHER *cipher; |
| 2205 | size_t len; |
| 2206 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2207 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2208 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2209 | len = ctx->cipher_suites_len; |
| 2210 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2211 | #else |
| 2212 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2213 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2214 | if (len % 2 != 0) |
| 2215 | goto abort; |
| 2216 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2217 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2218 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2219 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2220 | #else |
| 2221 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2222 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2223 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2224 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2225 | break; |
| 2226 | } |
| 2227 | } |
| 2228 | } |
| 2229 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2230 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2231 | trash.area[i] = tolower(servername[i]); |
| 2232 | if (!wildp && (trash.area[i] == '.')) |
| 2233 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2234 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2235 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2236 | |
| 2237 | /* lookup in full qualified names */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2238 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2239 | |
| 2240 | /* lookup a not neg filter */ |
| 2241 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2242 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2243 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2244 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2245 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2246 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2247 | break; |
| 2248 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2249 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2250 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2251 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2252 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2253 | if (!node_anonymous) |
| 2254 | node_anonymous = n; |
| 2255 | break; |
| 2256 | } |
| 2257 | } |
| 2258 | } |
| 2259 | if (wildp) { |
| 2260 | /* lookup in wildcards names */ |
| 2261 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2262 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2263 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2264 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2265 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2266 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2267 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2268 | break; |
| 2269 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2270 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2271 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2272 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2273 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2274 | if (!node_anonymous) |
| 2275 | node_anonymous = n; |
| 2276 | break; |
| 2277 | } |
| 2278 | } |
| 2279 | } |
| 2280 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2281 | /* select by key_signature priority order */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2282 | node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa |
| 2283 | : ((has_rsa_sig && node_rsa) ? node_rsa |
| 2284 | : (node_anonymous ? node_anonymous |
| 2285 | : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */ |
| 2286 | : node_rsa /* no rsa signature case (far far away) */ |
| 2287 | ))); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2288 | if (node) { |
| 2289 | /* switch ctx */ |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 2290 | 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] | 2291 | 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] | 2292 | if (conf) { |
| 2293 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2294 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2295 | if (conf->early_data) |
| 2296 | allow_early = 1; |
| 2297 | } |
| 2298 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2299 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2300 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2301 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2302 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2303 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2304 | } |
| 2305 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2306 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2307 | /* no certificate match, is the default_ctx */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2308 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2309 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2310 | allow_early: |
| 2311 | #ifdef OPENSSL_IS_BORINGSSL |
| 2312 | if (allow_early) |
| 2313 | SSL_set_early_data_enabled(ssl, 1); |
| 2314 | #else |
| 2315 | if (!allow_early) |
| 2316 | SSL_set_max_early_data(ssl, 0); |
| 2317 | #endif |
| 2318 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2319 | abort: |
| 2320 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2321 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2322 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2323 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2324 | #else |
| 2325 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2326 | return 0; |
| 2327 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2331 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2332 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2333 | * warning when no match is found, which implies the default (first) cert |
| 2334 | * will keep being used. |
| 2335 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2336 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2337 | { |
| 2338 | const char *servername; |
| 2339 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2340 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2341 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2342 | int i; |
| 2343 | (void)al; /* shut gcc stupid warning */ |
| 2344 | |
| 2345 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2346 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2347 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2348 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2349 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2350 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2351 | if (s->strict_sni) |
| 2352 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2353 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2354 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2355 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2356 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2357 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2358 | if (!servername[i]) |
| 2359 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2360 | trash.area[i] = tolower(servername[i]); |
| 2361 | if (!wildp && (trash.area[i] == '.')) |
| 2362 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2363 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2364 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2365 | |
| 2366 | /* lookup in full qualified names */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2367 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2368 | |
| 2369 | /* lookup a not neg filter */ |
| 2370 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2371 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2372 | node = n; |
| 2373 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2374 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2375 | } |
| 2376 | if (!node && wildp) { |
| 2377 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2378 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2379 | } |
| 2380 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2381 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2382 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2383 | /* switch ctx done in ssl_sock_generate_certificate */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2384 | return SSL_TLSEXT_ERR_OK; |
| 2385 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2386 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2387 | if (s->strict_sni) |
| 2388 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2389 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2390 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2391 | } |
| 2392 | |
| 2393 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2394 | 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] | 2395 | return SSL_TLSEXT_ERR_OK; |
| 2396 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2397 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2398 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2399 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2400 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2401 | |
| 2402 | static DH * ssl_get_dh_1024(void) |
| 2403 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2404 | static unsigned char dh1024_p[]={ |
| 2405 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2406 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2407 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2408 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2409 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2410 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2411 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2412 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2413 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2414 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2415 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2416 | }; |
| 2417 | static unsigned char dh1024_g[]={ |
| 2418 | 0x02, |
| 2419 | }; |
| 2420 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2421 | BIGNUM *p; |
| 2422 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2423 | DH *dh = DH_new(); |
| 2424 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2425 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2426 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2427 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2428 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2429 | DH_free(dh); |
| 2430 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2431 | } else { |
| 2432 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2433 | } |
| 2434 | } |
| 2435 | return dh; |
| 2436 | } |
| 2437 | |
| 2438 | static DH *ssl_get_dh_2048(void) |
| 2439 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2440 | static unsigned char dh2048_p[]={ |
| 2441 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2442 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2443 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2444 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2445 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2446 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2447 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2448 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2449 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2450 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2451 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2452 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2453 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2454 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2455 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2456 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2457 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2458 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2459 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2460 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2461 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2462 | 0xB7,0x1F,0x77,0xF3, |
| 2463 | }; |
| 2464 | static unsigned char dh2048_g[]={ |
| 2465 | 0x02, |
| 2466 | }; |
| 2467 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2468 | BIGNUM *p; |
| 2469 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2470 | DH *dh = DH_new(); |
| 2471 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2472 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2473 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2474 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2475 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2476 | DH_free(dh); |
| 2477 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2478 | } else { |
| 2479 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2480 | } |
| 2481 | } |
| 2482 | return dh; |
| 2483 | } |
| 2484 | |
| 2485 | static DH *ssl_get_dh_4096(void) |
| 2486 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2487 | static unsigned char dh4096_p[]={ |
| 2488 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2489 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2490 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2491 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2492 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2493 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2494 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2495 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2496 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2497 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2498 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2499 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2500 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2501 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2502 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2503 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2504 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2505 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2506 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2507 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2508 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2509 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2510 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2511 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2512 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2513 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2514 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2515 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2516 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2517 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2518 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2519 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2520 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2521 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2522 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2523 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2524 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2525 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2526 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2527 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2528 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2529 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2530 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2531 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2532 | static unsigned char dh4096_g[]={ |
| 2533 | 0x02, |
| 2534 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2535 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2536 | BIGNUM *p; |
| 2537 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2538 | DH *dh = DH_new(); |
| 2539 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2540 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2541 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2542 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2543 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2544 | DH_free(dh); |
| 2545 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2546 | } else { |
| 2547 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2548 | } |
| 2549 | } |
| 2550 | return dh; |
| 2551 | } |
| 2552 | |
| 2553 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2554 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2555 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2556 | { |
| 2557 | DH *dh = NULL; |
| 2558 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2559 | int type; |
| 2560 | |
| 2561 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2562 | |
| 2563 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2564 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2565 | */ |
| 2566 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2567 | keylen = EVP_PKEY_bits(pkey); |
| 2568 | } |
| 2569 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2570 | if (keylen > global_ssl.default_dh_param) { |
| 2571 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2572 | } |
| 2573 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2574 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2575 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2576 | } |
| 2577 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2578 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2579 | } |
| 2580 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2581 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | return dh; |
| 2585 | } |
| 2586 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2587 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2588 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2589 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2590 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2591 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2592 | if (in == NULL) |
| 2593 | goto end; |
| 2594 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2595 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2596 | goto end; |
| 2597 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2598 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2599 | |
| 2600 | end: |
| 2601 | if (in) |
| 2602 | BIO_free(in); |
| 2603 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2604 | ERR_clear_error(); |
| 2605 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2606 | return dh; |
| 2607 | } |
| 2608 | |
| 2609 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2610 | { |
| 2611 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2612 | |
| 2613 | if (global_dh) { |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
| 2617 | return -1; |
| 2618 | } |
| 2619 | |
| 2620 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 2621 | if an error occured, and 0 if parameter not found. */ |
| 2622 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 2623 | { |
| 2624 | int ret = -1; |
| 2625 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 2626 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2627 | if (dh) { |
| 2628 | ret = 1; |
| 2629 | SSL_CTX_set_tmp_dh(ctx, dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2630 | |
| 2631 | if (ssl_dh_ptr_index >= 0) { |
| 2632 | /* store a pointer to the DH params to avoid complaining about |
| 2633 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2634 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2635 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2636 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2637 | else if (global_dh) { |
| 2638 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 2639 | ret = 0; /* DH params not found */ |
| 2640 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2641 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2642 | /* Clear openssl global errors stack */ |
| 2643 | ERR_clear_error(); |
| 2644 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2645 | if (global_ssl.default_dh_param <= 1024) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2646 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 2647 | if (local_dh_1024 == NULL) |
| 2648 | local_dh_1024 = ssl_get_dh_1024(); |
| 2649 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2650 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2651 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2652 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2653 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2654 | } |
| 2655 | else { |
| 2656 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2657 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2658 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2659 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2660 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2661 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2662 | end: |
| 2663 | if (dh) |
| 2664 | DH_free(dh); |
| 2665 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2666 | return ret; |
| 2667 | } |
| 2668 | #endif |
| 2669 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2670 | 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] | 2671 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2672 | { |
| 2673 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2674 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2675 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2676 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2677 | if (*name == '!') { |
| 2678 | neg = 1; |
| 2679 | name++; |
| 2680 | } |
| 2681 | if (*name == '*') { |
| 2682 | wild = 1; |
| 2683 | name++; |
| 2684 | } |
| 2685 | /* !* filter is a nop */ |
| 2686 | if (neg && wild) |
| 2687 | return order; |
| 2688 | if (*name) { |
| 2689 | int j, len; |
| 2690 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2691 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2692 | trash.area[j] = tolower(name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2693 | if (j >= trash.size) |
| 2694 | return order; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2695 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2696 | |
| 2697 | /* Check for duplicates. */ |
| 2698 | if (wild) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2699 | node = ebst_lookup(&s->sni_w_ctx, trash.area); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2700 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2701 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2702 | for (; node; node = ebmb_next_dup(node)) { |
| 2703 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2704 | if (sc->ctx == ctx && sc->conf == conf && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2705 | return order; |
| 2706 | } |
| 2707 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2708 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2709 | if (!sc) |
| 2710 | return order; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2711 | memcpy(sc->name.key, trash.area, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2712 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2713 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2714 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2715 | sc->order = order++; |
| 2716 | sc->neg = neg; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 2717 | if (kinfo.sig != TLSEXT_signature_anonymous) |
| 2718 | SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2719 | if (wild) |
| 2720 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 2721 | else |
| 2722 | ebst_insert(&s->sni_ctx, &sc->name); |
| 2723 | } |
| 2724 | return order; |
| 2725 | } |
| 2726 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2727 | |
| 2728 | /* The following code is used for loading multiple crt files into |
| 2729 | * SSL_CTX's based on CN/SAN |
| 2730 | */ |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 2731 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(LIBRESSL_VERSION_NUMBER) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2732 | /* This is used to preload the certifcate, private key |
| 2733 | * and Cert Chain of a file passed in via the crt |
| 2734 | * argument |
| 2735 | * |
| 2736 | * This way, we do not have to read the file multiple times |
| 2737 | */ |
| 2738 | struct cert_key_and_chain { |
| 2739 | X509 *cert; |
| 2740 | EVP_PKEY *key; |
| 2741 | unsigned int num_chain_certs; |
| 2742 | /* This is an array of X509 pointers */ |
| 2743 | X509 **chain_certs; |
| 2744 | }; |
| 2745 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2746 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 2747 | |
| 2748 | struct key_combo_ctx { |
| 2749 | SSL_CTX *ctx; |
| 2750 | int order; |
| 2751 | }; |
| 2752 | |
| 2753 | /* Map used for processing multiple keypairs for a single purpose |
| 2754 | * |
| 2755 | * This maps CN/SNI name to certificate type |
| 2756 | */ |
| 2757 | struct sni_keytype { |
| 2758 | int keytypes; /* BITMASK for keytypes */ |
| 2759 | struct ebmb_node name; /* node holding the servername value */ |
| 2760 | }; |
| 2761 | |
| 2762 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2763 | /* Frees the contents of a cert_key_and_chain |
| 2764 | */ |
| 2765 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2766 | { |
| 2767 | int i; |
| 2768 | |
| 2769 | if (!ckch) |
| 2770 | return; |
| 2771 | |
| 2772 | /* Free the certificate and set pointer to NULL */ |
| 2773 | if (ckch->cert) |
| 2774 | X509_free(ckch->cert); |
| 2775 | ckch->cert = NULL; |
| 2776 | |
| 2777 | /* Free the key and set pointer to NULL */ |
| 2778 | if (ckch->key) |
| 2779 | EVP_PKEY_free(ckch->key); |
| 2780 | ckch->key = NULL; |
| 2781 | |
| 2782 | /* Free each certificate in the chain */ |
| 2783 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2784 | if (ckch->chain_certs[i]) |
| 2785 | X509_free(ckch->chain_certs[i]); |
| 2786 | } |
| 2787 | |
| 2788 | /* Free the chain obj itself and set to NULL */ |
| 2789 | if (ckch->num_chain_certs > 0) { |
| 2790 | free(ckch->chain_certs); |
| 2791 | ckch->num_chain_certs = 0; |
| 2792 | ckch->chain_certs = NULL; |
| 2793 | } |
| 2794 | |
| 2795 | } |
| 2796 | |
| 2797 | /* checks if a key and cert exists in the ckch |
| 2798 | */ |
| 2799 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 2800 | { |
| 2801 | return (ckch->cert != NULL && ckch->key != NULL); |
| 2802 | } |
| 2803 | |
| 2804 | |
| 2805 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 2806 | * This allows us to carry the contents of the file without having to |
| 2807 | * read the file multiple times. |
| 2808 | * |
| 2809 | * returns: |
| 2810 | * 0 on Success |
| 2811 | * 1 on SSL Failure |
| 2812 | * 2 on file not found |
| 2813 | */ |
| 2814 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 2815 | { |
| 2816 | |
| 2817 | BIO *in; |
| 2818 | X509 *ca = NULL; |
| 2819 | int ret = 1; |
| 2820 | |
| 2821 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2822 | |
| 2823 | in = BIO_new(BIO_s_file()); |
| 2824 | if (in == NULL) |
| 2825 | goto end; |
| 2826 | |
| 2827 | if (BIO_read_filename(in, path) <= 0) |
| 2828 | goto end; |
| 2829 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2830 | /* Read Private Key */ |
| 2831 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 2832 | if (ckch->key == NULL) { |
| 2833 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 2834 | err && *err ? *err : "", path); |
| 2835 | goto end; |
| 2836 | } |
| 2837 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2838 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 2839 | if (BIO_reset(in) == -1) { |
| 2840 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 2841 | err && *err ? *err : "", path); |
| 2842 | goto end; |
| 2843 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2844 | |
| 2845 | /* Read Certificate */ |
| 2846 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 2847 | if (ckch->cert == NULL) { |
| 2848 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 2849 | err && *err ? *err : "", path); |
| 2850 | goto end; |
| 2851 | } |
| 2852 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2853 | /* Read Certificate Chain */ |
| 2854 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 2855 | /* Grow the chain certs */ |
| 2856 | ckch->num_chain_certs++; |
| 2857 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 2858 | |
| 2859 | /* use - 1 here since we just incremented it above */ |
| 2860 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 2861 | } |
| 2862 | ret = ERR_get_error(); |
| 2863 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 2864 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 2865 | err && *err ? *err : "", path); |
| 2866 | ret = 1; |
| 2867 | goto end; |
| 2868 | } |
| 2869 | |
| 2870 | ret = 0; |
| 2871 | |
| 2872 | end: |
| 2873 | |
| 2874 | ERR_clear_error(); |
| 2875 | if (in) |
| 2876 | BIO_free(in); |
| 2877 | |
| 2878 | /* Something went wrong in one of the reads */ |
| 2879 | if (ret != 0) |
| 2880 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2881 | |
| 2882 | return ret; |
| 2883 | } |
| 2884 | |
| 2885 | /* Loads the info in ckch into ctx |
| 2886 | * Currently, this does not process any information about ocsp, dhparams or |
| 2887 | * sctl |
| 2888 | * Returns |
| 2889 | * 0 on success |
| 2890 | * 1 on failure |
| 2891 | */ |
| 2892 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2893 | { |
| 2894 | int i = 0; |
| 2895 | |
| 2896 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2897 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2898 | err && *err ? *err : "", path); |
| 2899 | return 1; |
| 2900 | } |
| 2901 | |
| 2902 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 2903 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 2904 | err && *err ? *err : "", path); |
| 2905 | return 1; |
| 2906 | } |
| 2907 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2908 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 2909 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2910 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2911 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 2912 | err && *err ? *err : "", (i+1), path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2913 | return 1; |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2918 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2919 | err && *err ? *err : "", path); |
| 2920 | return 1; |
| 2921 | } |
| 2922 | |
| 2923 | return 0; |
| 2924 | } |
| 2925 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2926 | |
| 2927 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 2928 | { |
| 2929 | struct sni_keytype *s_kt = NULL; |
| 2930 | struct ebmb_node *node; |
| 2931 | int i; |
| 2932 | |
| 2933 | for (i = 0; i < trash.size; i++) { |
| 2934 | if (!str[i]) |
| 2935 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2936 | trash.area[i] = tolower(str[i]); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2937 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2938 | trash.area[i] = 0; |
| 2939 | node = ebst_lookup(sni_keytypes, trash.area); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2940 | if (!node) { |
| 2941 | /* CN not found in tree */ |
| 2942 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 2943 | /* Using memcpy here instead of strncpy. |
| 2944 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 2945 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 2946 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2947 | memcpy(s_kt->name.key, trash.area, i+1); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2948 | s_kt->keytypes = 0; |
| 2949 | ebst_insert(sni_keytypes, &s_kt->name); |
| 2950 | } else { |
| 2951 | /* CN found in tree */ |
| 2952 | s_kt = container_of(node, struct sni_keytype, name); |
| 2953 | } |
| 2954 | |
| 2955 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 2956 | s_kt->keytypes |= 1<<key_index; |
| 2957 | |
| 2958 | } |
| 2959 | |
| 2960 | |
| 2961 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 2962 | * If any are found, group these files into a set of SSL_CTX* |
| 2963 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 2964 | * |
| 2965 | * This will allow the user to explictly group multiple cert/keys for a single purpose |
| 2966 | * |
| 2967 | * Returns |
| 2968 | * 0 on success |
| 2969 | * 1 on failure |
| 2970 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2971 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2972 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2973 | { |
| 2974 | char fp[MAXPATHLEN+1] = {0}; |
| 2975 | int n = 0; |
| 2976 | int i = 0; |
| 2977 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 2978 | struct eb_root sni_keytypes_map = { {0} }; |
| 2979 | struct ebmb_node *node; |
| 2980 | struct ebmb_node *next; |
| 2981 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 2982 | * of keytypes |
| 2983 | */ |
| 2984 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 2985 | int rv = 0; |
| 2986 | X509_NAME *xname = NULL; |
| 2987 | char *str = NULL; |
| 2988 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2989 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 2990 | #endif |
| 2991 | |
| 2992 | /* Load all possible certs and keys */ |
| 2993 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2994 | struct stat buf; |
| 2995 | |
| 2996 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2997 | if (stat(fp, &buf) == 0) { |
| 2998 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
| 2999 | rv = 1; |
| 3000 | goto end; |
| 3001 | } |
| 3002 | } |
| 3003 | } |
| 3004 | |
| 3005 | /* Process each ckch and update keytypes for each CN/SAN |
| 3006 | * for example, if CN/SAN www.a.com is associated with |
| 3007 | * certs with keytype 0 and 2, then at the end of the loop, |
| 3008 | * www.a.com will have: |
| 3009 | * keyindex = 0 | 1 | 4 = 5 |
| 3010 | */ |
| 3011 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3012 | |
| 3013 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 3014 | continue; |
| 3015 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3016 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 3017 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3018 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 3019 | } else { |
| 3020 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 3021 | * so the line that contains logic is marked via comments |
| 3022 | */ |
| 3023 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 3024 | i = -1; |
| 3025 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3026 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3027 | ASN1_STRING *value; |
| 3028 | value = X509_NAME_ENTRY_get_data(entry); |
| 3029 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3030 | /* Important line is here */ |
| 3031 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3032 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3033 | OPENSSL_free(str); |
| 3034 | str = NULL; |
| 3035 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3036 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3037 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3038 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3039 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3040 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 3041 | if (names) { |
| 3042 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3043 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3044 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3045 | if (name->type == GEN_DNS) { |
| 3046 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 3047 | /* Important line is here */ |
| 3048 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3049 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3050 | OPENSSL_free(str); |
| 3051 | str = NULL; |
| 3052 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3053 | } |
| 3054 | } |
| 3055 | } |
| 3056 | } |
| 3057 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 3058 | } |
| 3059 | |
| 3060 | /* If no files found, return error */ |
| 3061 | if (eb_is_empty(&sni_keytypes_map)) { |
| 3062 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3063 | err && *err ? *err : "", path); |
| 3064 | rv = 1; |
| 3065 | goto end; |
| 3066 | } |
| 3067 | |
| 3068 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 3069 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 3070 | * and add each CTX to the SNI tree |
| 3071 | * |
| 3072 | * Some math here: |
| 3073 | * There are 2^n - 1 possibile combinations, each unique |
| 3074 | * combination is denoted by the key in the map. Each key |
| 3075 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 3076 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 3077 | * entry in the array to correspond to the unique combo (key) |
| 3078 | * associated with i. This unique key combo (i) will be associated |
| 3079 | * with combos[i-1] |
| 3080 | */ |
| 3081 | |
| 3082 | node = ebmb_first(&sni_keytypes_map); |
| 3083 | while (node) { |
| 3084 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3085 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3086 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3087 | |
| 3088 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 3089 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 3090 | cur_ctx = key_combos[i-1].ctx; |
| 3091 | |
| 3092 | if (cur_ctx == NULL) { |
| 3093 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3094 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3095 | if (cur_ctx == NULL) { |
| 3096 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3097 | err && *err ? *err : ""); |
| 3098 | rv = 1; |
| 3099 | goto end; |
| 3100 | } |
| 3101 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3102 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3103 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3104 | if (i & (1<<n)) { |
| 3105 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3106 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3107 | 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] | 3108 | SSL_CTX_free(cur_ctx); |
| 3109 | rv = 1; |
| 3110 | goto end; |
| 3111 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3112 | |
| 3113 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 3114 | /* Load OCSP Info into context */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3115 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3116 | if (err) |
| 3117 | 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] | 3118 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3119 | SSL_CTX_free(cur_ctx); |
| 3120 | rv = 1; |
| 3121 | goto end; |
| 3122 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3123 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3124 | ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3125 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3126 | } |
| 3127 | } |
| 3128 | |
| 3129 | /* Load DH params into the ctx to support DHE keys */ |
| 3130 | #ifndef OPENSSL_NO_DH |
| 3131 | if (ssl_dh_ptr_index >= 0) |
| 3132 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 3133 | |
| 3134 | rv = ssl_sock_load_dh_params(cur_ctx, NULL); |
| 3135 | if (rv < 0) { |
| 3136 | if (err) |
| 3137 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3138 | *err ? *err : "", path); |
| 3139 | rv = 1; |
| 3140 | goto end; |
| 3141 | } |
| 3142 | #endif |
| 3143 | |
| 3144 | /* Update key_combos */ |
| 3145 | key_combos[i-1].ctx = cur_ctx; |
| 3146 | } |
| 3147 | |
| 3148 | /* Update SNI Tree */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3149 | 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] | 3150 | kinfo, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3151 | node = ebmb_next(node); |
| 3152 | } |
| 3153 | |
| 3154 | |
| 3155 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 3156 | if (!bind_conf->default_ctx) { |
| 3157 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 3158 | if (key_combos[i].ctx) { |
| 3159 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3160 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3161 | break; |
| 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | end: |
| 3167 | |
| 3168 | if (names) |
| 3169 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 3170 | |
| 3171 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 3172 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 3173 | |
| 3174 | node = ebmb_first(&sni_keytypes_map); |
| 3175 | while (node) { |
| 3176 | next = ebmb_next(node); |
| 3177 | ebmb_delete(node); |
| 3178 | node = next; |
| 3179 | } |
| 3180 | |
| 3181 | return rv; |
| 3182 | } |
| 3183 | #else |
| 3184 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3185 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3186 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3187 | { |
| 3188 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3189 | err && *err ? *err : "", path, strerror(errno)); |
| 3190 | return 1; |
| 3191 | } |
| 3192 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3193 | #endif /* #if OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
| 3194 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3195 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 3196 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 3197 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3198 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, |
| 3199 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3200 | { |
| 3201 | BIO *in; |
| 3202 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3203 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3204 | int ret = -1; |
| 3205 | int order = 0; |
| 3206 | X509_NAME *xname; |
| 3207 | char *str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3208 | pem_password_cb *passwd_cb; |
| 3209 | void *passwd_cb_userdata; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3210 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3211 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3212 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3213 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3214 | STACK_OF(GENERAL_NAME) *names; |
| 3215 | #endif |
| 3216 | |
| 3217 | in = BIO_new(BIO_s_file()); |
| 3218 | if (in == NULL) |
| 3219 | goto end; |
| 3220 | |
| 3221 | if (BIO_read_filename(in, file) <= 0) |
| 3222 | goto end; |
| 3223 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3224 | |
| 3225 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 3226 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 3227 | |
| 3228 | 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] | 3229 | if (x == NULL) |
| 3230 | goto end; |
| 3231 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3232 | pkey = X509_get_pubkey(x); |
| 3233 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3234 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3235 | switch(EVP_PKEY_base_id(pkey)) { |
| 3236 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3237 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3238 | break; |
| 3239 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3240 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3241 | break; |
| 3242 | case EVP_PKEY_DSA: |
| 3243 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3244 | break; |
| 3245 | } |
| 3246 | EVP_PKEY_free(pkey); |
| 3247 | } |
| 3248 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3249 | if (fcount) { |
| 3250 | while (fcount--) |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3251 | 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] | 3252 | } |
| 3253 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3254 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3255 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 3256 | if (names) { |
| 3257 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3258 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3259 | if (name->type == GEN_DNS) { |
| 3260 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3261 | 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] | 3262 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3263 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3264 | } |
| 3265 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3266 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3267 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3268 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3269 | xname = X509_get_subject_name(x); |
| 3270 | i = -1; |
| 3271 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3272 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3273 | ASN1_STRING *value; |
| 3274 | |
| 3275 | value = X509_NAME_ENTRY_get_data(entry); |
| 3276 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3277 | 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] | 3278 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3279 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3280 | } |
| 3281 | } |
| 3282 | |
| 3283 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 3284 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 3285 | goto end; |
| 3286 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3287 | #ifdef SSL_CTX_clear_extra_chain_certs |
| 3288 | SSL_CTX_clear_extra_chain_certs(ctx); |
| 3289 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3290 | if (ctx->extra_certs != NULL) { |
| 3291 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 3292 | ctx->extra_certs = NULL; |
| 3293 | } |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3294 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3295 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3296 | 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] | 3297 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3298 | X509_free(ca); |
| 3299 | goto end; |
| 3300 | } |
| 3301 | } |
| 3302 | |
| 3303 | err = ERR_get_error(); |
| 3304 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3305 | /* we successfully reached the last cert in the file */ |
| 3306 | ret = 1; |
| 3307 | } |
| 3308 | ERR_clear_error(); |
| 3309 | |
| 3310 | end: |
| 3311 | if (x) |
| 3312 | X509_free(x); |
| 3313 | |
| 3314 | if (in) |
| 3315 | BIO_free(in); |
| 3316 | |
| 3317 | return ret; |
| 3318 | } |
| 3319 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3320 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3321 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3322 | { |
| 3323 | int ret; |
| 3324 | SSL_CTX *ctx; |
| 3325 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3326 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3327 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3328 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3329 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3330 | return 1; |
| 3331 | } |
| 3332 | |
| 3333 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3334 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 3335 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3336 | SSL_CTX_free(ctx); |
| 3337 | return 1; |
| 3338 | } |
| 3339 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3340 | 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] | 3341 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3342 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 3343 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3344 | if (ret < 0) /* serious error, must do that ourselves */ |
| 3345 | SSL_CTX_free(ctx); |
| 3346 | return 1; |
| 3347 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 3348 | |
| 3349 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3350 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3351 | err && *err ? *err : "", path); |
| 3352 | return 1; |
| 3353 | } |
| 3354 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3355 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3356 | * the tree, so it will be discovered and cleaned in time. |
| 3357 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3358 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3359 | /* store a NULL pointer to indicate we have not yet loaded |
| 3360 | a custom DH param file */ |
| 3361 | if (ssl_dh_ptr_index >= 0) { |
| 3362 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3363 | } |
| 3364 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3365 | ret = ssl_sock_load_dh_params(ctx, path); |
| 3366 | if (ret < 0) { |
| 3367 | if (err) |
| 3368 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3369 | *err ? *err : "", path); |
| 3370 | return 1; |
| 3371 | } |
| 3372 | #endif |
| 3373 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 3374 | #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] | 3375 | ret = ssl_sock_load_ocsp(ctx, path); |
| 3376 | if (ret < 0) { |
| 3377 | if (err) |
| 3378 | 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", |
| 3379 | *err ? *err : "", path); |
| 3380 | return 1; |
| 3381 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3382 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3383 | ssl_sock_set_ocsp_response_from_file(ctx, path); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3384 | #endif |
| 3385 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 3386 | #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] | 3387 | if (sctl_ex_index >= 0) { |
| 3388 | ret = ssl_sock_load_sctl(ctx, path); |
| 3389 | if (ret < 0) { |
| 3390 | if (err) |
| 3391 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 3392 | *err ? *err : "", path); |
| 3393 | return 1; |
| 3394 | } |
| 3395 | } |
| 3396 | #endif |
| 3397 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3398 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3399 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3400 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3401 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3402 | return 1; |
| 3403 | } |
| 3404 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3405 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3406 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3407 | bind_conf->default_ssl_conf = ssl_conf; |
| 3408 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3409 | |
| 3410 | return 0; |
| 3411 | } |
| 3412 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3413 | 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] | 3414 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3415 | struct dirent **de_list; |
| 3416 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3417 | DIR *dir; |
| 3418 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 3419 | char *end; |
| 3420 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3421 | int cfgerr = 0; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3422 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3423 | int is_bundle; |
| 3424 | int j; |
| 3425 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3426 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3427 | if (stat(path, &buf) == 0) { |
| 3428 | dir = opendir(path); |
| 3429 | if (!dir) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3430 | 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] | 3431 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3432 | /* strip trailing slashes, including first one */ |
| 3433 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 3434 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3435 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3436 | n = scandir(path, &de_list, 0, alphasort); |
| 3437 | if (n < 0) { |
| 3438 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 3439 | err && *err ? *err : "", path, strerror(errno)); |
| 3440 | cfgerr++; |
| 3441 | } |
| 3442 | else { |
| 3443 | for (i = 0; i < n; i++) { |
| 3444 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 3445 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3446 | end = strrchr(de->d_name, '.'); |
| 3447 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 3448 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3449 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3450 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 3451 | if (stat(fp, &buf) != 0) { |
| 3452 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3453 | err && *err ? *err : "", fp, strerror(errno)); |
| 3454 | cfgerr++; |
| 3455 | goto ignore_entry; |
| 3456 | } |
| 3457 | if (!S_ISREG(buf.st_mode)) |
| 3458 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3459 | |
| 3460 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3461 | is_bundle = 0; |
| 3462 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 3463 | |
| 3464 | if (end) { |
| 3465 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 3466 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 3467 | is_bundle = 1; |
| 3468 | break; |
| 3469 | } |
| 3470 | } |
| 3471 | |
| 3472 | if (is_bundle) { |
| 3473 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 3474 | int dp_len; |
| 3475 | |
| 3476 | dp_len = end - de->d_name; |
| 3477 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 3478 | |
| 3479 | /* increment i and free de until we get to a non-bundle cert |
| 3480 | * Note here that we look at de_list[i + 1] before freeing de |
| 3481 | * this is important since ignore_entry will free de |
| 3482 | */ |
| 3483 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 3484 | free(de); |
| 3485 | i++; |
| 3486 | de = de_list[i]; |
| 3487 | } |
| 3488 | |
| 3489 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
Emeric Brun | eb155b6 | 2018-08-16 15:11:12 +0200 | [diff] [blame] | 3490 | cfgerr += ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3491 | |
| 3492 | /* Successfully processed the bundle */ |
| 3493 | goto ignore_entry; |
| 3494 | } |
| 3495 | } |
| 3496 | |
| 3497 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3498 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3499 | ignore_entry: |
| 3500 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3501 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3502 | free(de_list); |
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 | closedir(dir); |
| 3505 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3506 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3507 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3508 | cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3509 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3510 | return cfgerr; |
| 3511 | } |
| 3512 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3513 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3514 | * done once. Zero is returned if the operation fails. No error is returned |
| 3515 | * if the random is said as not implemented, because we expect that openssl |
| 3516 | * will use another method once needed. |
| 3517 | */ |
| 3518 | static int ssl_initialize_random() |
| 3519 | { |
| 3520 | unsigned char random; |
| 3521 | static int random_initialized = 0; |
| 3522 | |
| 3523 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3524 | random_initialized = 1; |
| 3525 | |
| 3526 | return random_initialized; |
| 3527 | } |
| 3528 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3529 | /* release ssl bind conf */ |
| 3530 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3531 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3532 | if (conf) { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 3533 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3534 | free(conf->npn_str); |
| 3535 | conf->npn_str = NULL; |
| 3536 | #endif |
| 3537 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 3538 | free(conf->alpn_str); |
| 3539 | conf->alpn_str = NULL; |
| 3540 | #endif |
| 3541 | free(conf->ca_file); |
| 3542 | conf->ca_file = NULL; |
| 3543 | free(conf->crl_file); |
| 3544 | conf->crl_file = NULL; |
| 3545 | free(conf->ciphers); |
| 3546 | conf->ciphers = NULL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3547 | free(conf->curves); |
| 3548 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3549 | free(conf->ecdhe); |
| 3550 | conf->ecdhe = NULL; |
| 3551 | } |
| 3552 | } |
| 3553 | |
| 3554 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 3555 | { |
| 3556 | char thisline[CRT_LINESIZE]; |
| 3557 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3558 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3559 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3560 | int linenum = 0; |
| 3561 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3562 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3563 | if ((f = fopen(file, "r")) == NULL) { |
| 3564 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3565 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3566 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3567 | |
| 3568 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3569 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3570 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3571 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3572 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3573 | char *crt_path; |
| 3574 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3575 | |
| 3576 | linenum++; |
| 3577 | end = line + strlen(line); |
| 3578 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 3579 | /* Check if we reached the limit and the last char is not \n. |
| 3580 | * Watch out for the last line without the terminating '\n'! |
| 3581 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3582 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 3583 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3584 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3585 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3586 | } |
| 3587 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3588 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3589 | newarg = 1; |
| 3590 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3591 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 3592 | /* end of string, end of loop */ |
| 3593 | *line = 0; |
| 3594 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3595 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3596 | newarg = 1; |
| 3597 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3598 | } else if (*line == '[') { |
| 3599 | if (ssl_b) { |
| 3600 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 3601 | cfgerr = 1; |
| 3602 | break; |
| 3603 | } |
| 3604 | if (!arg) { |
| 3605 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 3606 | cfgerr = 1; |
| 3607 | break; |
| 3608 | } |
| 3609 | ssl_b = arg; |
| 3610 | newarg = 1; |
| 3611 | *line = 0; |
| 3612 | } else if (*line == ']') { |
| 3613 | if (ssl_e) { |
| 3614 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3615 | cfgerr = 1; |
| 3616 | break; |
| 3617 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3618 | if (!ssl_b) { |
| 3619 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 3620 | cfgerr = 1; |
| 3621 | break; |
| 3622 | } |
| 3623 | ssl_e = arg; |
| 3624 | newarg = 1; |
| 3625 | *line = 0; |
| 3626 | } else if (newarg) { |
| 3627 | if (arg == MAX_CRT_ARGS) { |
| 3628 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 3629 | cfgerr = 1; |
| 3630 | break; |
| 3631 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3632 | newarg = 0; |
| 3633 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3634 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3635 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3636 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3637 | if (cfgerr) |
| 3638 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3639 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3640 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3641 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3642 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3643 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3644 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3645 | crt_path = args[0]; |
| 3646 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 3647 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 3648 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 3649 | crt_path, linenum, file); |
| 3650 | cfgerr = 1; |
| 3651 | break; |
| 3652 | } |
| 3653 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 3654 | crt_path = path; |
| 3655 | } |
| 3656 | |
| 3657 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 3658 | cur_arg = ssl_b ? ssl_b : 1; |
| 3659 | while (cur_arg < ssl_e) { |
| 3660 | newarg = 0; |
| 3661 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 3662 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 3663 | newarg = 1; |
| 3664 | cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
| 3665 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 3666 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 3667 | args[cur_arg], linenum, file); |
| 3668 | cfgerr = 1; |
| 3669 | } |
| 3670 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 3671 | break; |
| 3672 | } |
| 3673 | } |
| 3674 | if (!cfgerr && !newarg) { |
| 3675 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 3676 | args[cur_arg], linenum, file); |
| 3677 | cfgerr = 1; |
| 3678 | break; |
| 3679 | } |
| 3680 | } |
| 3681 | if (cfgerr) { |
| 3682 | ssl_sock_free_ssl_conf(ssl_conf); |
| 3683 | free(ssl_conf); |
| 3684 | ssl_conf = NULL; |
| 3685 | break; |
| 3686 | } |
| 3687 | |
| 3688 | if (stat(crt_path, &buf) == 0) { |
| 3689 | cfgerr = ssl_sock_load_cert_file(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 | } else { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3692 | cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf, |
| 3693 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3694 | } |
| 3695 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3696 | if (cfgerr) { |
| 3697 | 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] | 3698 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3699 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3700 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3701 | fclose(f); |
| 3702 | return cfgerr; |
| 3703 | } |
| 3704 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3705 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3706 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3707 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3708 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3709 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3710 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3711 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3712 | SSL_OP_NO_SSLv2 | |
| 3713 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3714 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3715 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3716 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 3717 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3718 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3719 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3720 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3721 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3722 | SSL_MODE_RELEASE_BUFFERS | |
| 3723 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3724 | 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] | 3725 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3726 | int flags = MC_SSL_O_ALL; |
| 3727 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3728 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3729 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3730 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3731 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3732 | 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] | 3733 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3734 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3735 | 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] | 3736 | else |
| 3737 | flags = conf_ssl_methods->flags; |
| 3738 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3739 | min = conf_ssl_methods->min; |
| 3740 | max = conf_ssl_methods->max; |
| 3741 | /* start with TLSv10 to remove SSLv3 per default */ |
| 3742 | if (!min && (!max || max >= CONF_TLSV10)) |
| 3743 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3744 | /* 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] | 3745 | if (min) |
| 3746 | flags |= (methodVersions[min].flag - 1); |
| 3747 | if (max) |
| 3748 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3749 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3750 | min = max = CONF_TLSV_NONE; |
| 3751 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3752 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3753 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3754 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3755 | if (min) { |
| 3756 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3757 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 3758 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3759 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3760 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3761 | hole = 0; |
| 3762 | } |
| 3763 | max = i; |
| 3764 | } |
| 3765 | else { |
| 3766 | min = max = i; |
| 3767 | } |
| 3768 | } |
| 3769 | else { |
| 3770 | if (min) |
| 3771 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3772 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3773 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3774 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3775 | 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] | 3776 | cfgerr += 1; |
| 3777 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3778 | /* save real min/max in bind_conf */ |
| 3779 | conf_ssl_methods->min = min; |
| 3780 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3781 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 3782 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3783 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
| 3784 | precautionary measure to avoid any suprise with older openssl version. */ |
| 3785 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3786 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3787 | else |
| 3788 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3789 | if (flags & methodVersions[i].flag) |
| 3790 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3791 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3792 | /* 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] | 3793 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3794 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3795 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3796 | |
| 3797 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3798 | options |= SSL_OP_NO_TICKET; |
| 3799 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3800 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 3801 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3802 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 3803 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3804 | if (global_ssl.async) |
| 3805 | mode |= SSL_MODE_ASYNC; |
| 3806 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3807 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3808 | if (global_ssl.life_time) |
| 3809 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3810 | |
| 3811 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3812 | #ifdef OPENSSL_IS_BORINGSSL |
| 3813 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3814 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3815 | #elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 3816 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 3817 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3818 | #else |
| 3819 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3820 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 3821 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3822 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3823 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3824 | } |
| 3825 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3826 | |
| 3827 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 3828 | { |
| 3829 | if (first == block) { |
| 3830 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3831 | if (first->len > 0) |
| 3832 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 3833 | } |
| 3834 | } |
| 3835 | |
| 3836 | /* return first block from sh_ssl_sess */ |
| 3837 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 3838 | { |
| 3839 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 3840 | |
| 3841 | } |
| 3842 | |
| 3843 | /* store a session into the cache |
| 3844 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 3845 | * data: asn1 encoded session |
| 3846 | * data_len: asn1 encoded session length |
| 3847 | * Returns 1 id session was stored (else 0) |
| 3848 | */ |
| 3849 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 3850 | { |
| 3851 | struct shared_block *first; |
| 3852 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 3853 | |
| 3854 | first = shctx_row_reserve_hot(ssl_shctx, data_len + sizeof(struct sh_ssl_sess_hdr)); |
| 3855 | if (!first) { |
| 3856 | /* Could not retrieve enough free blocks to store that session */ |
| 3857 | return 0; |
| 3858 | } |
| 3859 | |
| 3860 | /* STORE the key in the first elem */ |
| 3861 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3862 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 3863 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3864 | |
| 3865 | /* it returns the already existing node |
| 3866 | or current node if none, never returns null */ |
| 3867 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 3868 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 3869 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 3870 | /* release the reserved row */ |
| 3871 | shctx_row_dec_hot(ssl_shctx, first); |
| 3872 | /* replace the previous session already in the tree */ |
| 3873 | sh_ssl_sess = oldsh_ssl_sess; |
| 3874 | /* ignore the previous session data, only use the header */ |
| 3875 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 3876 | shctx_row_inc_hot(ssl_shctx, first); |
| 3877 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3878 | } |
| 3879 | |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 3880 | if (shctx_row_data_append(ssl_shctx, first, data, data_len) < 0) { |
| 3881 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3882 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 3883 | } |
| 3884 | |
| 3885 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3886 | |
| 3887 | return 1; |
| 3888 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3889 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3890 | /* SSL callback used when a new session is created while connecting to a server */ |
| 3891 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 3892 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 3893 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3894 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3895 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3896 | s = objt_server(conn->target); |
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 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 3899 | int len; |
| 3900 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3901 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3902 | len = i2d_SSL_SESSION(sess, NULL); |
| 3903 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 3904 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 3905 | } else { |
| 3906 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3907 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 3908 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 3909 | } |
| 3910 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 3911 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 3912 | &ptr); |
| 3913 | } |
| 3914 | } else { |
| 3915 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3916 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 3917 | } |
| 3918 | |
| 3919 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3920 | } |
| 3921 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3922 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3923 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3924 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3925 | { |
| 3926 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 3927 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 3928 | unsigned char *p; |
| 3929 | int data_len; |
| 3930 | unsigned int sid_length, sid_ctx_length; |
| 3931 | const unsigned char *sid_data; |
| 3932 | const unsigned char *sid_ctx_data; |
| 3933 | |
| 3934 | /* Session id is already stored in to key and session id is known |
| 3935 | * so we dont store it to keep size. |
| 3936 | */ |
| 3937 | |
| 3938 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 3939 | sid_ctx_data = SSL_SESSION_get0_id_context(sess, &sid_ctx_length); |
| 3940 | SSL_SESSION_set1_id(sess, sid_data, 0); |
| 3941 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, 0); |
| 3942 | |
| 3943 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 3944 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 3945 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 3946 | goto err; |
| 3947 | |
| 3948 | p = encsess; |
| 3949 | |
| 3950 | /* process ASN1 session encoding before the lock */ |
| 3951 | i2d_SSL_SESSION(sess, &p); |
| 3952 | |
| 3953 | memcpy(encid, sid_data, sid_length); |
| 3954 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 3955 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 3956 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3957 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3958 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3959 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3960 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3961 | err: |
| 3962 | /* reset original length values */ |
| 3963 | SSL_SESSION_set1_id(sess, sid_data, sid_length); |
| 3964 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, sid_ctx_length); |
| 3965 | |
| 3966 | return 0; /* do not increment session reference count */ |
| 3967 | } |
| 3968 | |
| 3969 | /* 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] | 3970 | 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] | 3971 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3972 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3973 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 3974 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3975 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3976 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3977 | |
| 3978 | global.shctx_lookups++; |
| 3979 | |
| 3980 | /* allow the session to be freed automatically by openssl */ |
| 3981 | *do_copy = 0; |
| 3982 | |
| 3983 | /* tree key is zeros padded sessionid */ |
| 3984 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3985 | memcpy(tmpkey, key, key_len); |
| 3986 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 3987 | key = tmpkey; |
| 3988 | } |
| 3989 | |
| 3990 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3991 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3992 | |
| 3993 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3994 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 3995 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3996 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3997 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3998 | global.shctx_misses++; |
| 3999 | return NULL; |
| 4000 | } |
| 4001 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4002 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 4003 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4004 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4005 | 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] | 4006 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4007 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4008 | |
| 4009 | /* decode ASN1 session */ |
| 4010 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4011 | 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] | 4012 | /* Reset session id and session id contenxt */ |
| 4013 | if (sess) { |
| 4014 | SSL_SESSION_set1_id(sess, key, key_len); |
| 4015 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4016 | } |
| 4017 | |
| 4018 | return sess; |
| 4019 | } |
| 4020 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4021 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4022 | /* 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] | 4023 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4024 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4025 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4026 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 4027 | unsigned int sid_length; |
| 4028 | const unsigned char *sid_data; |
| 4029 | (void)ctx; |
| 4030 | |
| 4031 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4032 | /* tree key is zeros padded sessionid */ |
| 4033 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4034 | memcpy(tmpkey, sid_data, sid_length); |
| 4035 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 4036 | sid_data = tmpkey; |
| 4037 | } |
| 4038 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4039 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4040 | |
| 4041 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4042 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 4043 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4044 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4045 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4046 | } |
| 4047 | |
| 4048 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4049 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4050 | } |
| 4051 | |
| 4052 | /* Set session cache mode to server and disable openssl internal cache. |
| 4053 | * Set shared cache callbacks on an ssl context. |
| 4054 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4055 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4056 | { |
| 4057 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4058 | |
| 4059 | if (!ssl_shctx) { |
| 4060 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4061 | return; |
| 4062 | } |
| 4063 | |
| 4064 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4065 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4066 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4067 | |
| 4068 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4069 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4070 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4071 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4072 | } |
| 4073 | |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4074 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
| 4075 | { |
| 4076 | struct proxy *curproxy = bind_conf->frontend; |
| 4077 | int cfgerr = 0; |
| 4078 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4079 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4080 | const char *conf_ciphers; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4081 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4082 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4083 | if (ssl_conf) { |
| 4084 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4085 | int i, min, max; |
| 4086 | int flags = MC_SSL_O_ALL; |
| 4087 | |
| 4088 | /* 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] | 4089 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4090 | 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] | 4091 | if (min) |
| 4092 | flags |= (methodVersions[min].flag - 1); |
| 4093 | if (max) |
| 4094 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4095 | min = max = CONF_TLSV_NONE; |
| 4096 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4097 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4098 | if (min) |
| 4099 | max = i; |
| 4100 | else |
| 4101 | min = max = i; |
| 4102 | } |
| 4103 | /* save real min/max */ |
| 4104 | conf_ssl_methods->min = min; |
| 4105 | conf_ssl_methods->max = max; |
| 4106 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4107 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4108 | 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] | 4109 | cfgerr += 1; |
| 4110 | } |
| 4111 | } |
| 4112 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4113 | 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] | 4114 | case SSL_SOCK_VERIFY_NONE: |
| 4115 | verify = SSL_VERIFY_NONE; |
| 4116 | break; |
| 4117 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4118 | verify = SSL_VERIFY_PEER; |
| 4119 | break; |
| 4120 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4121 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4122 | break; |
| 4123 | } |
| 4124 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4125 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4126 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 4127 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 4128 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4129 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4130 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4131 | ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
| 4132 | 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] | 4133 | cfgerr++; |
| 4134 | } |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 4135 | if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) { |
| 4136 | /* set CA names for client cert request, function returns void */ |
| 4137 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file)); |
| 4138 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4139 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4140 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4141 | ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4142 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4143 | cfgerr++; |
| 4144 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4145 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4146 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4147 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4148 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4149 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4150 | ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4151 | 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] | 4152 | cfgerr++; |
| 4153 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4154 | else { |
| 4155 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4156 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4157 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4158 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4159 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4160 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4161 | #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] | 4162 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4163 | 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] | 4164 | ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4165 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4166 | cfgerr++; |
| 4167 | } |
| 4168 | } |
| 4169 | #endif |
| 4170 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4171 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4172 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4173 | if (conf_ciphers && |
| 4174 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4175 | ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4176 | 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] | 4177 | cfgerr++; |
| 4178 | } |
| 4179 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4180 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4181 | /* If tune.ssl.default-dh-param has not been set, |
| 4182 | neither has ssl-default-dh-file and no static DH |
| 4183 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4184 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4185 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4186 | (ssl_dh_ptr_index == -1 || |
| 4187 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4188 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 4189 | const SSL_CIPHER * cipher = NULL; |
| 4190 | char cipher_description[128]; |
| 4191 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 4192 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 4193 | which is not ephemeral DH. */ |
| 4194 | const char dhe_description[] = " Kx=DH "; |
| 4195 | const char dhe_export_description[] = " Kx=DH("; |
| 4196 | int idx = 0; |
| 4197 | int dhe_found = 0; |
| 4198 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4199 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4200 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4201 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4202 | if (ssl) { |
| 4203 | ciphers = SSL_get_ciphers(ssl); |
| 4204 | |
| 4205 | if (ciphers) { |
| 4206 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 4207 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 4208 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 4209 | if (strstr(cipher_description, dhe_description) != NULL || |
| 4210 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 4211 | dhe_found = 1; |
| 4212 | break; |
| 4213 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 4214 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4215 | } |
| 4216 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4217 | SSL_free(ssl); |
| 4218 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4219 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4220 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4221 | if (dhe_found) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4222 | 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] | 4223 | } |
| 4224 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4225 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4226 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4227 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4228 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4229 | if (local_dh_1024 == NULL) { |
| 4230 | local_dh_1024 = ssl_get_dh_1024(); |
| 4231 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4232 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4233 | if (local_dh_2048 == NULL) { |
| 4234 | local_dh_2048 = ssl_get_dh_2048(); |
| 4235 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4236 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4237 | if (local_dh_4096 == NULL) { |
| 4238 | local_dh_4096 = ssl_get_dh_4096(); |
| 4239 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4240 | } |
| 4241 | } |
| 4242 | } |
| 4243 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4244 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4245 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4246 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4247 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4248 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4249 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4250 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4251 | ssl_conf_cur = NULL; |
| 4252 | if (ssl_conf && ssl_conf->npn_str) |
| 4253 | ssl_conf_cur = ssl_conf; |
| 4254 | else if (bind_conf->ssl_conf.npn_str) |
| 4255 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4256 | if (ssl_conf_cur) |
| 4257 | 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] | 4258 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4259 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4260 | ssl_conf_cur = NULL; |
| 4261 | if (ssl_conf && ssl_conf->alpn_str) |
| 4262 | ssl_conf_cur = ssl_conf; |
| 4263 | else if (bind_conf->ssl_conf.alpn_str) |
| 4264 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4265 | if (ssl_conf_cur) |
| 4266 | 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] | 4267 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4268 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 4269 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4270 | if (conf_curves) { |
| 4271 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4272 | ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4273 | 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] | 4274 | cfgerr++; |
| 4275 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4276 | #if defined(SSL_CTX_set_ecdh_auto) |
| 4277 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
| 4278 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4279 | } |
| 4280 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4281 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4282 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4283 | int i; |
| 4284 | EC_KEY *ecdh; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4285 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4286 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4287 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4288 | NULL); |
| 4289 | |
| 4290 | if (ecdhe == NULL) { |
| 4291 | SSL_CTX_set_dh_auto(ctx, 1); |
| 4292 | return cfgerr; |
| 4293 | } |
| 4294 | #else |
| 4295 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4296 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4297 | ECDHE_DEFAULT_CURVE); |
| 4298 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4299 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4300 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4301 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4302 | ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4303 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4304 | cfgerr++; |
| 4305 | } |
| 4306 | else { |
| 4307 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4308 | EC_KEY_free(ecdh); |
| 4309 | } |
| 4310 | } |
| 4311 | #endif |
| 4312 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4313 | return cfgerr; |
| 4314 | } |
| 4315 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4316 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4317 | { |
| 4318 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4319 | size_t prefixlen, suffixlen; |
| 4320 | |
| 4321 | /* Trivial case */ |
| 4322 | if (strcmp(pattern, hostname) == 0) |
| 4323 | return 1; |
| 4324 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4325 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4326 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4327 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4328 | pattern_wildcard = NULL; |
| 4329 | pattern_left_label_end = pattern; |
| 4330 | while (*pattern_left_label_end != '.') { |
| 4331 | switch (*pattern_left_label_end) { |
| 4332 | case 0: |
| 4333 | /* End of label not found */ |
| 4334 | return 0; |
| 4335 | case '*': |
| 4336 | /* If there is more than one wildcards */ |
| 4337 | if (pattern_wildcard) |
| 4338 | return 0; |
| 4339 | pattern_wildcard = pattern_left_label_end; |
| 4340 | break; |
| 4341 | } |
| 4342 | pattern_left_label_end++; |
| 4343 | } |
| 4344 | |
| 4345 | /* If it's not trivial and there is no wildcard, it can't |
| 4346 | * match */ |
| 4347 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4348 | return 0; |
| 4349 | |
| 4350 | /* Make sure all labels match except the leftmost */ |
| 4351 | hostname_left_label_end = strchr(hostname, '.'); |
| 4352 | if (!hostname_left_label_end |
| 4353 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 4354 | return 0; |
| 4355 | |
| 4356 | /* Make sure the leftmost label of the hostname is long enough |
| 4357 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4358 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4359 | return 0; |
| 4360 | |
| 4361 | /* Finally compare the string on either side of the |
| 4362 | * wildcard */ |
| 4363 | prefixlen = pattern_wildcard - pattern; |
| 4364 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4365 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 4366 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4367 | return 0; |
| 4368 | |
| 4369 | return 1; |
| 4370 | } |
| 4371 | |
| 4372 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4373 | { |
| 4374 | SSL *ssl; |
| 4375 | struct connection *conn; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4376 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4377 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4378 | |
| 4379 | int depth; |
| 4380 | X509 *cert; |
| 4381 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4382 | int i; |
| 4383 | X509_NAME *cert_subject; |
| 4384 | char *str; |
| 4385 | |
| 4386 | if (ok == 0) |
| 4387 | return ok; |
| 4388 | |
| 4389 | ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4390 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4391 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4392 | /* We're checking if the provided hostnames match the desired one. The |
| 4393 | * desired hostname comes from the SNI we presented if any, or if not |
| 4394 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4395 | * directive. If neither is set, we don't care about the name so the |
| 4396 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4397 | */ |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4398 | servername = SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4399 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4400 | if (!servername) { |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4401 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4402 | if (!servername) |
| 4403 | return ok; |
| 4404 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4405 | |
| 4406 | /* We only need to verify the CN on the actual server cert, |
| 4407 | * not the indirect CAs */ |
| 4408 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4409 | if (depth != 0) |
| 4410 | return ok; |
| 4411 | |
| 4412 | /* At this point, the cert is *not* OK unless we can find a |
| 4413 | * hostname match */ |
| 4414 | ok = 0; |
| 4415 | |
| 4416 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4417 | /* It seems like this might happen if verify peer isn't set */ |
| 4418 | if (!cert) |
| 4419 | return ok; |
| 4420 | |
| 4421 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4422 | if (alt_names) { |
| 4423 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4424 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4425 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4426 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 4427 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4428 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4429 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4430 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4431 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4432 | OPENSSL_free(str); |
| 4433 | } |
| 4434 | } |
| 4435 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4436 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | cert_subject = X509_get_subject_name(cert); |
| 4440 | i = -1; |
| 4441 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4442 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4443 | ASN1_STRING *value; |
| 4444 | value = X509_NAME_ENTRY_get_data(entry); |
| 4445 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4446 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4447 | OPENSSL_free(str); |
| 4448 | } |
| 4449 | } |
| 4450 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4451 | /* report the mismatch and indicate if SNI was used or not */ |
| 4452 | if (!ok && !conn->err_code) |
| 4453 | 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] | 4454 | return ok; |
| 4455 | } |
| 4456 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4457 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4458 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4459 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4460 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4461 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4462 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4463 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4464 | SSL_OP_NO_SSLv2 | |
| 4465 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4466 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4467 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4468 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4469 | SSL_MODE_RELEASE_BUFFERS | |
| 4470 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4471 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4472 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4473 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4474 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4475 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4476 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4477 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4478 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4479 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4480 | cfgerr++; |
| 4481 | } |
| 4482 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4483 | /* Automatic memory computations need to know we use SSL there */ |
| 4484 | global.ssl_used_backend = 1; |
| 4485 | |
| 4486 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4487 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4488 | 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] | 4489 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 4490 | curproxy->id, srv->id, |
| 4491 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4492 | cfgerr++; |
| 4493 | return cfgerr; |
| 4494 | } |
| 4495 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4496 | if (srv->use_ssl) |
| 4497 | srv->xprt = &ssl_sock; |
| 4498 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 4499 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4500 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4501 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4502 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4503 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 4504 | proxy_type_str(curproxy), curproxy->id, |
| 4505 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4506 | cfgerr++; |
| 4507 | return cfgerr; |
| 4508 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4509 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4510 | 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] | 4511 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 4512 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4513 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4514 | else |
| 4515 | flags = conf_ssl_methods->flags; |
| 4516 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4517 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 4518 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4519 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4520 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4521 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4522 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4523 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4524 | min = max = CONF_TLSV_NONE; |
| 4525 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4526 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4527 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4528 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4529 | if (min) { |
| 4530 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4531 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 4532 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4533 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4534 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4535 | hole = 0; |
| 4536 | } |
| 4537 | max = i; |
| 4538 | } |
| 4539 | else { |
| 4540 | min = max = i; |
| 4541 | } |
| 4542 | } |
| 4543 | else { |
| 4544 | if (min) |
| 4545 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4546 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4547 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4548 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4549 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4550 | cfgerr += 1; |
| 4551 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4552 | |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 4553 | #if (OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4554 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
| 4555 | precautionary measure to avoid any suprise with older openssl version. */ |
| 4556 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4557 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4558 | else |
| 4559 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4560 | if (flags & methodVersions[i].flag) |
| 4561 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4562 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4563 | /* 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] | 4564 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4565 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4566 | #endif |
| 4567 | |
| 4568 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4569 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4570 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4571 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 4572 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4573 | if (global_ssl.async) |
| 4574 | mode |= SSL_MODE_ASYNC; |
| 4575 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4576 | SSL_CTX_set_mode(ctx, mode); |
| 4577 | srv->ssl_ctx.ctx = ctx; |
| 4578 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4579 | if (srv->ssl_ctx.client_crt) { |
| 4580 | 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] | 4581 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4582 | proxy_type_str(curproxy), curproxy->id, |
| 4583 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4584 | cfgerr++; |
| 4585 | } |
| 4586 | 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] | 4587 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4588 | proxy_type_str(curproxy), curproxy->id, |
| 4589 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4590 | cfgerr++; |
| 4591 | } |
| 4592 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4593 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4594 | proxy_type_str(curproxy), curproxy->id, |
| 4595 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4596 | cfgerr++; |
| 4597 | } |
| 4598 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4599 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4600 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4601 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4602 | switch (srv->ssl_ctx.verify) { |
| 4603 | case SSL_SOCK_VERIFY_NONE: |
| 4604 | verify = SSL_VERIFY_NONE; |
| 4605 | break; |
| 4606 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4607 | verify = SSL_VERIFY_PEER; |
| 4608 | break; |
| 4609 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4610 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4611 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4612 | (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] | 4613 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4614 | if (srv->ssl_ctx.ca_file) { |
| 4615 | /* load CAfile to verify */ |
| 4616 | 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] | 4617 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n", |
| 4618 | curproxy->id, srv->id, |
| 4619 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4620 | cfgerr++; |
| 4621 | } |
| 4622 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4623 | else { |
| 4624 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4625 | 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", |
| 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 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4629 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 4630 | curproxy->id, srv->id, |
| 4631 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4632 | cfgerr++; |
| 4633 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4634 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4635 | if (srv->ssl_ctx.crl_file) { |
| 4636 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4637 | |
| 4638 | 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] | 4639 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 4640 | curproxy->id, srv->id, |
| 4641 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4642 | cfgerr++; |
| 4643 | } |
| 4644 | else { |
| 4645 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4646 | } |
| 4647 | } |
| 4648 | #endif |
| 4649 | } |
| 4650 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4651 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 4652 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 4653 | 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] | 4654 | if (srv->ssl_ctx.ciphers && |
| 4655 | !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] | 4656 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4657 | curproxy->id, srv->id, |
| 4658 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4659 | cfgerr++; |
| 4660 | } |
| 4661 | |
| 4662 | return cfgerr; |
| 4663 | } |
| 4664 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4665 | /* 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] | 4666 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4667 | * encountered. |
| 4668 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4669 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4670 | { |
| 4671 | struct ebmb_node *node; |
| 4672 | struct sni_ctx *sni; |
| 4673 | int err = 0; |
| 4674 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4675 | /* Automatic memory computations need to know we use SSL there */ |
| 4676 | global.ssl_used_frontend = 1; |
| 4677 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4678 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4679 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4680 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4681 | err++; |
| 4682 | } |
| 4683 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4684 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4685 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4686 | /* It should not be necessary to call this function, but it's |
| 4687 | necessary first to check and move all initialisation related |
| 4688 | to initial_ctx in ssl_sock_initial_ctx. */ |
| 4689 | err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx); |
| 4690 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4691 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4692 | 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] | 4693 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4694 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4695 | while (node) { |
| 4696 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4697 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4698 | /* only initialize the CTX on its first occurrence and |
| 4699 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4700 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4701 | node = ebmb_next(node); |
| 4702 | } |
| 4703 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4704 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4705 | while (node) { |
| 4706 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4707 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4708 | /* only initialize the CTX on its first occurrence and |
| 4709 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4710 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4711 | node = ebmb_next(node); |
| 4712 | } |
| 4713 | return err; |
| 4714 | } |
| 4715 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4716 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4717 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4718 | * alerts are directly emitted since the rest of the stack does it below. |
| 4719 | */ |
| 4720 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4721 | { |
| 4722 | struct proxy *px = bind_conf->frontend; |
| 4723 | int alloc_ctx; |
| 4724 | int err; |
| 4725 | |
| 4726 | if (!bind_conf->is_ssl) { |
| 4727 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4728 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 4729 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4730 | } |
| 4731 | return 0; |
| 4732 | } |
| 4733 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4734 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4735 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (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 | } |
| 4738 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4739 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 4740 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4741 | return -1; |
| 4742 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4743 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 4744 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4745 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
| 4746 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, |
| 4747 | sizeof(*sh_ssl_sess_tree), |
| 4748 | ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0); |
| 4749 | if (alloc_ctx < 0) { |
| 4750 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 4751 | 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"); |
| 4752 | else |
| 4753 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 4754 | return -1; |
| 4755 | } |
| 4756 | /* free block callback */ |
| 4757 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 4758 | /* init the root tree within the extra space */ |
| 4759 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 4760 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4761 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4762 | err = 0; |
| 4763 | /* initialize all certificate contexts */ |
| 4764 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 4765 | |
| 4766 | /* initialize CA variables if the certificates generation is enabled */ |
| 4767 | err += ssl_sock_load_ca(bind_conf); |
| 4768 | |
| 4769 | return -err; |
| 4770 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4771 | |
| 4772 | /* release ssl context allocated for servers. */ |
| 4773 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 4774 | { |
| 4775 | if (srv->ssl_ctx.ctx) |
| 4776 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 4777 | } |
| 4778 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4779 | /* 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] | 4780 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 4781 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4782 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4783 | { |
| 4784 | struct ebmb_node *node, *back; |
| 4785 | struct sni_ctx *sni; |
| 4786 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4787 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4788 | while (node) { |
| 4789 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4790 | back = ebmb_next(node); |
| 4791 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4792 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4793 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4794 | ssl_sock_free_ssl_conf(sni->conf); |
| 4795 | free(sni->conf); |
| 4796 | sni->conf = NULL; |
| 4797 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4798 | free(sni); |
| 4799 | node = back; |
| 4800 | } |
| 4801 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4802 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4803 | while (node) { |
| 4804 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4805 | back = ebmb_next(node); |
| 4806 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4807 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4808 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4809 | ssl_sock_free_ssl_conf(sni->conf); |
| 4810 | free(sni->conf); |
| 4811 | sni->conf = NULL; |
| 4812 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4813 | free(sni); |
| 4814 | node = back; |
| 4815 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4816 | SSL_CTX_free(bind_conf->initial_ctx); |
| 4817 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4818 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4819 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4820 | } |
| 4821 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4822 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 4823 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 4824 | { |
| 4825 | ssl_sock_free_ca(bind_conf); |
| 4826 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4827 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4828 | free(bind_conf->ca_sign_file); |
| 4829 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 4830 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4831 | free(bind_conf->keys_ref->filename); |
| 4832 | free(bind_conf->keys_ref->tlskeys); |
| 4833 | LIST_DEL(&bind_conf->keys_ref->list); |
| 4834 | free(bind_conf->keys_ref); |
| 4835 | } |
| 4836 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4837 | bind_conf->ca_sign_pass = NULL; |
| 4838 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4839 | } |
| 4840 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4841 | /* Load CA cert file and private key used to generate certificates */ |
| 4842 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4843 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4844 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4845 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4846 | FILE *fp; |
| 4847 | X509 *cacert = NULL; |
| 4848 | EVP_PKEY *capkey = NULL; |
| 4849 | int err = 0; |
| 4850 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 4851 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4852 | return err; |
| 4853 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4854 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4855 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4856 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4857 | HA_RWLOCK_INIT(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4858 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4859 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4860 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 4861 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4862 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4863 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4864 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 4865 | "no CA certificate File configured at [%s:%d].\n", |
| 4866 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4867 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4868 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4869 | |
| 4870 | /* read in the CA certificate */ |
| 4871 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4872 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4873 | 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] | 4874 | goto load_error; |
| 4875 | } |
| 4876 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4877 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4878 | 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] | 4879 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4880 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4881 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4882 | 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] | 4883 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 4884 | 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] | 4885 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4886 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4887 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4888 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4889 | bind_conf->ca_sign_cert = cacert; |
| 4890 | bind_conf->ca_sign_pkey = capkey; |
| 4891 | return err; |
| 4892 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4893 | read_error: |
| 4894 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4895 | if (capkey) EVP_PKEY_free(capkey); |
| 4896 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4897 | load_error: |
| 4898 | bind_conf->generate_certs = 0; |
| 4899 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4900 | return err; |
| 4901 | } |
| 4902 | |
| 4903 | /* Release CA cert and private key used to generate certificated */ |
| 4904 | void |
| 4905 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 4906 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4907 | if (bind_conf->ca_sign_pkey) |
| 4908 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 4909 | if (bind_conf->ca_sign_cert) |
| 4910 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 4911 | bind_conf->ca_sign_pkey = NULL; |
| 4912 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4913 | } |
| 4914 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4915 | /* |
| 4916 | * This function is called if SSL * context is not yet allocated. The function |
| 4917 | * is designed to be called before any other data-layer operation and sets the |
| 4918 | * handshake flag on the connection. It is safe to call it multiple times. |
| 4919 | * It returns 0 on success and -1 in error case. |
| 4920 | */ |
| 4921 | static int ssl_sock_init(struct connection *conn) |
| 4922 | { |
| 4923 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4924 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4925 | return 0; |
| 4926 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4927 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4928 | return 0; |
| 4929 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4930 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 4931 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4932 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4933 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4934 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4935 | /* If it is in client mode initiate SSL session |
| 4936 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4937 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4938 | int may_retry = 1; |
| 4939 | |
| 4940 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4941 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4942 | conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4943 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4944 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4945 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4946 | goto retry_connect; |
| 4947 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4948 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4949 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4950 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4951 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4952 | /* set fd on SSL session context */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 4953 | if (!SSL_set_fd(conn->xprt_ctx, conn->handle.fd)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4954 | SSL_free(conn->xprt_ctx); |
| 4955 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4956 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4957 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4958 | goto retry_connect; |
| 4959 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4960 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4961 | return -1; |
| 4962 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4963 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4964 | /* set connection pointer */ |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4965 | if (!SSL_set_ex_data(conn->xprt_ctx, ssl_app_data_index, conn)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4966 | SSL_free(conn->xprt_ctx); |
| 4967 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4968 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4969 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4970 | goto retry_connect; |
| 4971 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4972 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4973 | return -1; |
| 4974 | } |
| 4975 | |
| 4976 | SSL_set_connect_state(conn->xprt_ctx); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4977 | if (objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 4978 | const unsigned char *ptr = objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 4979 | SSL_SESSION *sess = d2i_SSL_SESSION(NULL, &ptr, objt_server(conn->target)->ssl_ctx.reused_sess[tid].size); |
| 4980 | if(sess && !SSL_set_session(conn->xprt_ctx, sess)) { |
| 4981 | SSL_SESSION_free(sess); |
| 4982 | free(objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 4983 | objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 4984 | } else if (sess) { |
| 4985 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4986 | } |
| 4987 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4988 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4989 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4990 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4991 | |
| 4992 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 4993 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4994 | return 0; |
| 4995 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4996 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4997 | int may_retry = 1; |
| 4998 | |
| 4999 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5000 | /* Alloc a new SSL session ctx */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5001 | 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] | 5002 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5003 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5004 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5005 | goto retry_accept; |
| 5006 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5007 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5008 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5009 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5010 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5011 | /* set fd on SSL session context */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5012 | if (!SSL_set_fd(conn->xprt_ctx, conn->handle.fd)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5013 | SSL_free(conn->xprt_ctx); |
| 5014 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5015 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5016 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5017 | goto retry_accept; |
| 5018 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5019 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5020 | return -1; |
| 5021 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5022 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5023 | /* set connection pointer */ |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 5024 | if (!SSL_set_ex_data(conn->xprt_ctx, ssl_app_data_index, conn)) { |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5025 | SSL_free(conn->xprt_ctx); |
| 5026 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5027 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5028 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5029 | goto retry_accept; |
| 5030 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5031 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5032 | return -1; |
| 5033 | } |
| 5034 | |
| 5035 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5036 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5037 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5038 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5039 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L || defined(OPENSSL_IS_BORINGSSL) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5040 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 5041 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5042 | |
| 5043 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 5044 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5045 | return 0; |
| 5046 | } |
| 5047 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5048 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5049 | return -1; |
| 5050 | } |
| 5051 | |
| 5052 | |
| 5053 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5054 | * updates the FD status if it wants some polling before being called again. |
| 5055 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5056 | * otherwise it returns non-zero and removes itself from the connection's |
| 5057 | * flags (the bit is provided in <flag> by the caller). |
| 5058 | */ |
| 5059 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 5060 | { |
| 5061 | int ret; |
| 5062 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5063 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5064 | return 0; |
| 5065 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5066 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5067 | goto out_error; |
| 5068 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5069 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L |
| 5070 | /* |
| 5071 | * Check if we have early data. If we do, we have to read them |
| 5072 | * before SSL_do_handshake() is called, And there's no way to |
| 5073 | * detect early data, except to try to read them |
| 5074 | */ |
| 5075 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5076 | size_t read_data; |
| 5077 | |
| 5078 | ret = SSL_read_early_data(conn->xprt_ctx, &conn->tmp_early_data, |
| 5079 | 1, &read_data); |
| 5080 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5081 | goto check_error; |
| 5082 | if (ret == SSL_READ_EARLY_DATA_SUCCESS) { |
| 5083 | conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN); |
| 5084 | return 1; |
| 5085 | } else |
| 5086 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5087 | } |
| 5088 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5089 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5090 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5091 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5092 | * the reneg handshake. |
| 5093 | * Here we use SSL_peek as a workaround for reneg. |
| 5094 | */ |
| 5095 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5096 | char c; |
| 5097 | |
| 5098 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 5099 | if (ret <= 0) { |
| 5100 | /* handshake may have not been completed, let's find why */ |
| 5101 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5102 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5103 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5104 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 5105 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5106 | __conn_sock_want_send(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5107 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5108 | return 0; |
| 5109 | } |
| 5110 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5111 | /* handshake may have been completed but we have |
| 5112 | * no more data to read. |
| 5113 | */ |
| 5114 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5115 | ret = 1; |
| 5116 | goto reneg_ok; |
| 5117 | } |
| 5118 | /* SSL handshake needs to read, L4 connection is ready */ |
| 5119 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 5120 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5121 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5122 | __conn_sock_want_recv(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5123 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5124 | return 0; |
| 5125 | } |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5126 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5127 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5128 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5129 | return 0; |
| 5130 | } |
| 5131 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5132 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5133 | /* if errno is null, then connection was successfully established */ |
| 5134 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5135 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5136 | if (!conn->err_code) { |
Emeric Brun | 77e8919 | 2018-08-16 11:36:40 +0200 | [diff] [blame] | 5137 | #ifdef OPENSSL_IS_BORINGSSL /* BoringSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5138 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5139 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5140 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 5141 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5142 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 5143 | empty_handshake = state == TLS_ST_BEFORE; |
| 5144 | #else |
| 5145 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
| 5146 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5147 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5148 | if (!errno) { |
| 5149 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5150 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5151 | else |
| 5152 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5153 | } |
| 5154 | else { |
| 5155 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5156 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5157 | else |
| 5158 | conn->err_code = CO_ER_SSL_ABORT; |
| 5159 | } |
| 5160 | } |
| 5161 | else { |
| 5162 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5163 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5164 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5165 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5166 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5167 | #endif |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5168 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5169 | goto out_error; |
| 5170 | } |
| 5171 | else { |
| 5172 | /* Fail on all other handshake errors */ |
| 5173 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5174 | * buffer, causing an RST to be emitted upon close() on |
| 5175 | * TCP sockets. We first try to drain possibly pending |
| 5176 | * data to avoid this as much as possible. |
| 5177 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5178 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5179 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5180 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 5181 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5182 | goto out_error; |
| 5183 | } |
| 5184 | } |
| 5185 | /* read some data: consider handshake completed */ |
| 5186 | goto reneg_ok; |
| 5187 | } |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5188 | ret = SSL_do_handshake(conn->xprt_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5189 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5190 | if (ret != 1) { |
| 5191 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5192 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5193 | |
| 5194 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5195 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 5196 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5197 | __conn_sock_want_send(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5198 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5199 | return 0; |
| 5200 | } |
| 5201 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5202 | /* SSL handshake needs to read, L4 connection is ready */ |
| 5203 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 5204 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5205 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5206 | __conn_sock_want_recv(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5207 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5208 | return 0; |
| 5209 | } |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5210 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5211 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5212 | ssl_async_process_fds(conn, conn->xprt_ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5213 | return 0; |
| 5214 | } |
| 5215 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5216 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5217 | /* if errno is null, then connection was successfully established */ |
| 5218 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5219 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5220 | if (!conn->err_code) { |
Emeric Brun | 77e8919 | 2018-08-16 11:36:40 +0200 | [diff] [blame] | 5221 | #ifdef OPENSSL_IS_BORINGSSL /* BoringSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5222 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5223 | #else |
| 5224 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 5225 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5226 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 5227 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5228 | #else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5229 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5230 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5231 | if (empty_handshake) { |
| 5232 | if (!errno) { |
| 5233 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5234 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5235 | else |
| 5236 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5237 | } |
| 5238 | else { |
| 5239 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5240 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5241 | else |
| 5242 | conn->err_code = CO_ER_SSL_ABORT; |
| 5243 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5244 | } |
| 5245 | else { |
| 5246 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 5247 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5248 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5249 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5250 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5251 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5252 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5253 | goto out_error; |
| 5254 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5255 | else { |
| 5256 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5257 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5258 | * buffer, causing an RST to be emitted upon close() on |
| 5259 | * TCP sockets. We first try to drain possibly pending |
| 5260 | * data to avoid this as much as possible. |
| 5261 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5262 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5263 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5264 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 5265 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5266 | goto out_error; |
| 5267 | } |
| 5268 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5269 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5270 | else { |
| 5271 | /* |
| 5272 | * If the server refused the early data, we have to send a |
| 5273 | * 425 to the client, as we no longer have the data to sent |
| 5274 | * them again. |
| 5275 | */ |
| 5276 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
| 5277 | if (SSL_get_early_data_status(conn->xprt_ctx) == SSL_EARLY_DATA_REJECTED) { |
| 5278 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5279 | goto out_error; |
| 5280 | } |
| 5281 | } |
| 5282 | } |
| 5283 | #endif |
| 5284 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5285 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5286 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5287 | |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5288 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5289 | /* ASYNC engine API doesn't support moving read/write |
| 5290 | * buffers. So we disable ASYNC mode right after |
| 5291 | * the handshake to avoid buffer oveflows. |
| 5292 | */ |
| 5293 | if (global_ssl.async) |
| 5294 | SSL_clear_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5295 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5296 | /* Handshake succeeded */ |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5297 | if (!SSL_session_reused(conn->xprt_ctx)) { |
| 5298 | if (objt_server(conn->target)) { |
| 5299 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5300 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5301 | 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] | 5302 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5303 | else { |
| 5304 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5305 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5306 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5307 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5308 | } |
| 5309 | |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5310 | #ifdef OPENSSL_IS_BORINGSSL |
| 5311 | if ((conn->flags & CO_FL_EARLY_SSL_HS) && !SSL_in_early_data(conn->xprt_ctx)) |
| 5312 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5313 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5314 | /* The connection is now established at both layers, it's time to leave */ |
| 5315 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5316 | return 1; |
| 5317 | |
| 5318 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5319 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5320 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5321 | ERR_clear_error(); |
| 5322 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5323 | /* free resumed session if exists */ |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5324 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5325 | free(objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5326 | objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5327 | } |
| 5328 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5329 | /* Fail on all other handshake errors */ |
| 5330 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5331 | if (!conn->err_code) |
| 5332 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5333 | return 0; |
| 5334 | } |
| 5335 | |
| 5336 | /* 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] | 5337 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5338 | * buffer wraps, in which case a second call may be performed. The connection's |
| 5339 | * flags are updated with whatever special event is detected (error, read0, |
| 5340 | * empty). The caller is responsible for taking care of those events and |
| 5341 | * avoiding the call if inappropriate. The function does not call the |
| 5342 | * connection's polling update function, so the caller is responsible for this. |
| 5343 | */ |
Willy Tarreau | 7f3225f | 2018-06-19 06:15:17 +0200 | [diff] [blame] | 5344 | static size_t ssl_sock_to_buf(struct connection *conn, struct buffer *buf, size_t count, int flags) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5345 | { |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 5346 | ssize_t ret; |
| 5347 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5348 | |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5349 | conn_refresh_polling_flags(conn); |
| 5350 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5351 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5352 | goto out_error; |
| 5353 | |
| 5354 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5355 | /* a handshake was requested */ |
| 5356 | return 0; |
| 5357 | |
Willy Tarreau | 0c7ed5d | 2018-07-10 09:53:31 +0200 | [diff] [blame] | 5358 | b_realign_if_empty(buf); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5359 | |
| 5360 | /* read the largest possible block. For this, we perform only one call |
| 5361 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 5362 | * in which case we accept to do it once again. A new attempt is made on |
| 5363 | * EINTR too. |
| 5364 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 5365 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5366 | int need_out = 0; |
| 5367 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5368 | try = b_contig_space(buf); |
| 5369 | if (!try) |
| 5370 | break; |
| 5371 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5372 | if (try > count) |
| 5373 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5374 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5375 | if (((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_EARLY_SSL_HS) && |
| 5376 | conn->tmp_early_data != -1) { |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 5377 | *b_tail(buf) = conn->tmp_early_data; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5378 | done++; |
| 5379 | try--; |
| 5380 | count--; |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5381 | b_add(buf, 1); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5382 | conn->tmp_early_data = -1; |
| 5383 | continue; |
| 5384 | } |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5385 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5386 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5387 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5388 | size_t read_length; |
| 5389 | |
| 5390 | ret = SSL_read_early_data(conn->xprt_ctx, |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 5391 | b_tail(buf), try, &read_length); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5392 | if (ret == SSL_READ_EARLY_DATA_SUCCESS && |
| 5393 | read_length > 0) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5394 | conn->flags |= CO_FL_EARLY_DATA; |
| 5395 | if (ret == SSL_READ_EARLY_DATA_SUCCESS || |
| 5396 | ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5397 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5398 | /* |
| 5399 | * We're done reading the early data, |
| 5400 | * let's make the handshake |
| 5401 | */ |
| 5402 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5403 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5404 | need_out = 1; |
| 5405 | if (read_length == 0) |
| 5406 | break; |
| 5407 | } |
| 5408 | ret = read_length; |
| 5409 | } |
| 5410 | } else |
| 5411 | #endif |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 5412 | ret = SSL_read(conn->xprt_ctx, b_tail(buf), try); |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5413 | #ifdef OPENSSL_IS_BORINGSSL |
| 5414 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5415 | if (SSL_in_early_data(conn->xprt_ctx)) { |
| 5416 | if (ret > 0) |
| 5417 | conn->flags |= CO_FL_EARLY_DATA; |
| 5418 | } else { |
Emmanuel Hocdet | cebd796 | 2017-11-27 16:14:40 +0100 | [diff] [blame] | 5419 | conn->flags &= ~(CO_FL_EARLY_SSL_HS); |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5420 | } |
| 5421 | } |
| 5422 | #endif |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5423 | if (conn->flags & CO_FL_ERROR) { |
| 5424 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5425 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5426 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5427 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5428 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5429 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5430 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5431 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5432 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5433 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5434 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5435 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5436 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5437 | __conn_sock_want_send(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5438 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5439 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5440 | if (global_ssl.async) |
| 5441 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5442 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5443 | break; |
| 5444 | } |
| 5445 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5446 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5447 | /* handshake is running, and it may need to re-enable read */ |
| 5448 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5449 | __conn_sock_want_recv(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5450 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5451 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5452 | if (global_ssl.async) |
| 5453 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5454 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5455 | break; |
| 5456 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5457 | /* we need to poll for retry a read later */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5458 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5459 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5460 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 5461 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5462 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 5463 | * stack before shutting down the connection for |
| 5464 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5465 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 5466 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5467 | /* otherwise it's a real error */ |
| 5468 | goto out_error; |
| 5469 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5470 | if (need_out) |
| 5471 | break; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5472 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5473 | leave: |
| 5474 | conn_cond_update_sock_polling(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5475 | return done; |
| 5476 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5477 | clear_ssl_error: |
| 5478 | /* Clear openssl global errors stack */ |
| 5479 | ssl_sock_dump_errors(conn); |
| 5480 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5481 | read0: |
| 5482 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5483 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5484 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5485 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5486 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5487 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5488 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5489 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5490 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5494 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 5495 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 5496 | * other pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5497 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5498 | * a second call may be performed. The connection's flags are updated with |
| 5499 | * whatever special event is detected (error, empty). The caller is responsible |
| 5500 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5501 | * function does not call the connection's polling update function, so the caller |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5502 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 5503 | * caller to take care of this. It's up to the caller to update the buffer's |
| 5504 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5505 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5506 | static size_t ssl_sock_from_buf(struct connection *conn, const struct buffer *buf, size_t count, int flags) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5507 | { |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5508 | ssize_t ret; |
| 5509 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5510 | |
| 5511 | done = 0; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5512 | conn_refresh_polling_flags(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5513 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5514 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5515 | goto out_error; |
| 5516 | |
| 5517 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5518 | /* a handshake was requested */ |
| 5519 | return 0; |
| 5520 | |
| 5521 | /* send the largest possible block. For this we perform only one call |
| 5522 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5523 | * in which case we accept to do it once again. |
| 5524 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5525 | while (count) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5526 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5527 | size_t written_data; |
| 5528 | #endif |
| 5529 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5530 | try = b_contig_data(buf, done); |
| 5531 | if (try > count) |
| 5532 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5533 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 5534 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5535 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5536 | global_ssl.max_record && try > global_ssl.max_record) { |
| 5537 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5538 | } |
| 5539 | else { |
| 5540 | /* we need to keep the information about the fact that |
| 5541 | * we're not limiting the upcoming send(), because if it |
| 5542 | * fails, we'll have to retry with at least as many data. |
| 5543 | */ |
| 5544 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 5545 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5546 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5547 | #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5548 | if (!SSL_is_init_finished(conn->xprt_ctx)) { |
| 5549 | unsigned int max_early; |
| 5550 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5551 | if (objt_listener(conn->target)) |
| 5552 | max_early = SSL_get_max_early_data(conn->xprt_ctx); |
| 5553 | else { |
| 5554 | if (SSL_get0_session(conn->xprt_ctx)) |
| 5555 | max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(conn->xprt_ctx)); |
| 5556 | else |
| 5557 | max_early = 0; |
| 5558 | } |
| 5559 | |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 5560 | if (try + conn->sent_early_data > max_early) { |
| 5561 | try -= (try + conn->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5562 | if (try <= 0) { |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 5563 | if (!(conn->flags & CO_FL_EARLY_SSL_HS)) |
| 5564 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5565 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5566 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5567 | } |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5568 | ret = SSL_write_early_data(conn->xprt_ctx, b_peek(buf, done), try, &written_data); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5569 | if (ret == 1) { |
| 5570 | ret = written_data; |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 5571 | conn->sent_early_data += ret; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5572 | if (objt_server(conn->target)) { |
| 5573 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5574 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA; |
| 5575 | } |
| 5576 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | } else |
| 5580 | #endif |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5581 | ret = SSL_write(conn->xprt_ctx, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5582 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5583 | if (conn->flags & CO_FL_ERROR) { |
| 5584 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5585 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5586 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5587 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5588 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5589 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5590 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5591 | } |
| 5592 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5593 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5594 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5595 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5596 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 5597 | /* handshake is running, and it may need to re-enable write */ |
| 5598 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5599 | __conn_sock_want_send(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5600 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5601 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5602 | if (global_ssl.async) |
| 5603 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5604 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5605 | break; |
| 5606 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5607 | /* we need to poll to retry a write later */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5608 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5609 | break; |
| 5610 | } |
| 5611 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5612 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5613 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5614 | __conn_sock_want_recv(conn); |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5615 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5616 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5617 | if (global_ssl.async) |
| 5618 | SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC); |
| 5619 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5620 | break; |
| 5621 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5622 | goto out_error; |
| 5623 | } |
| 5624 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5625 | leave: |
| 5626 | conn_cond_update_sock_polling(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5627 | return done; |
| 5628 | |
| 5629 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5630 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5631 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5632 | ERR_clear_error(); |
| 5633 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5634 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5635 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5636 | } |
| 5637 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5638 | static void ssl_sock_close(struct connection *conn) { |
| 5639 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5640 | if (conn->xprt_ctx) { |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 5641 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5642 | if (global_ssl.async) { |
| 5643 | OSSL_ASYNC_FD all_fd[32], afd; |
| 5644 | size_t num_all_fds = 0; |
| 5645 | int i; |
| 5646 | |
| 5647 | SSL_get_all_async_fds(conn->xprt_ctx, NULL, &num_all_fds); |
| 5648 | if (num_all_fds > 32) { |
| 5649 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 5650 | return; |
| 5651 | } |
| 5652 | |
| 5653 | SSL_get_all_async_fds(conn->xprt_ctx, all_fd, &num_all_fds); |
| 5654 | |
| 5655 | /* If an async job is pending, we must try to |
| 5656 | to catch the end using polling before calling |
| 5657 | SSL_free */ |
| 5658 | if (num_all_fds && SSL_waiting_for_async(conn->xprt_ctx)) { |
| 5659 | for (i=0 ; i < num_all_fds ; i++) { |
| 5660 | /* switch on an handler designed to |
| 5661 | * handle the SSL_free |
| 5662 | */ |
| 5663 | afd = all_fd[i]; |
| 5664 | fdtab[afd].iocb = ssl_async_fd_free; |
| 5665 | fdtab[afd].owner = conn->xprt_ctx; |
| 5666 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 5667 | /* To ensure that the fd cache won't be used |
| 5668 | * and we'll catch a real RD event. |
| 5669 | */ |
| 5670 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5671 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5672 | conn->xprt_ctx = NULL; |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5673 | HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5674 | return; |
| 5675 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5676 | /* Else we can remove the fds from the fdtab |
| 5677 | * and call SSL_free. |
| 5678 | * note: we do a fd_remove and not a delete |
| 5679 | * because the fd is owned by the engine. |
| 5680 | * the engine is responsible to close |
| 5681 | */ |
| 5682 | for (i=0 ; i < num_all_fds ; i++) |
| 5683 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5684 | } |
| 5685 | #endif |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5686 | SSL_free(conn->xprt_ctx); |
| 5687 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5688 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5689 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 5693 | * any case, flags the connection as reusable if no handshake was in progress. |
| 5694 | */ |
| 5695 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 5696 | { |
| 5697 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5698 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5699 | if (!clean) |
| 5700 | /* don't sent notify on SSL_shutdown */ |
Willy Tarreau | e3cc3a3 | 2017-02-13 11:12:29 +0100 | [diff] [blame] | 5701 | SSL_set_quiet_shutdown(conn->xprt_ctx, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5702 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5703 | if (SSL_shutdown(conn->xprt_ctx) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5704 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5705 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5706 | ERR_clear_error(); |
| 5707 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5708 | } |
| 5709 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5710 | /* used for ppv2 pkey alog (can be used for logging) */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5711 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5712 | { |
| 5713 | struct pkey_info *pkinfo; |
| 5714 | int bits = 0; |
| 5715 | int sig = TLSEXT_signature_anonymous; |
| 5716 | int len = -1; |
| 5717 | |
| 5718 | if (!ssl_sock_is_ssl(conn)) |
| 5719 | return 0; |
| 5720 | |
Emmanuel Hocdet | 3448c49 | 2018-06-18 12:44:19 +0200 | [diff] [blame] | 5721 | pkinfo = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(conn->xprt_ctx), ssl_pkey_info_index); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5722 | if (pkinfo) { |
| 5723 | sig = pkinfo->sig; |
| 5724 | bits = pkinfo->bits; |
| 5725 | } else { |
| 5726 | /* multicert and generated cert have no pkey info */ |
| 5727 | X509 *crt; |
| 5728 | EVP_PKEY *pkey; |
| 5729 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5730 | if (!crt) |
| 5731 | return 0; |
| 5732 | pkey = X509_get_pubkey(crt); |
| 5733 | if (pkey) { |
| 5734 | bits = EVP_PKEY_bits(pkey); |
| 5735 | switch(EVP_PKEY_base_id(pkey)) { |
| 5736 | case EVP_PKEY_RSA: |
| 5737 | sig = TLSEXT_signature_rsa; |
| 5738 | break; |
| 5739 | case EVP_PKEY_EC: |
| 5740 | sig = TLSEXT_signature_ecdsa; |
| 5741 | break; |
| 5742 | case EVP_PKEY_DSA: |
| 5743 | sig = TLSEXT_signature_dsa; |
| 5744 | break; |
| 5745 | } |
| 5746 | EVP_PKEY_free(pkey); |
| 5747 | } |
| 5748 | } |
| 5749 | |
| 5750 | switch(sig) { |
| 5751 | case TLSEXT_signature_rsa: |
| 5752 | len = chunk_printf(out, "RSA%d", bits); |
| 5753 | break; |
| 5754 | case TLSEXT_signature_ecdsa: |
| 5755 | len = chunk_printf(out, "EC%d", bits); |
| 5756 | break; |
| 5757 | case TLSEXT_signature_dsa: |
| 5758 | len = chunk_printf(out, "DSA%d", bits); |
| 5759 | break; |
| 5760 | default: |
| 5761 | return 0; |
| 5762 | } |
| 5763 | if (len < 0) |
| 5764 | return 0; |
| 5765 | return 1; |
| 5766 | } |
| 5767 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 5768 | /* used for ppv2 cert signature (can be used for logging) */ |
| 5769 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 5770 | { |
| 5771 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 5772 | X509 *crt; |
| 5773 | |
| 5774 | if (!ssl_sock_is_ssl(conn)) |
| 5775 | return NULL; |
| 5776 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 5777 | if (!crt) |
| 5778 | return NULL; |
| 5779 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 5780 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 5781 | } |
| 5782 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 5783 | /* used for ppv2 authority */ |
| 5784 | const char *ssl_sock_get_sni(struct connection *conn) |
| 5785 | { |
| 5786 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5787 | if (!ssl_sock_is_ssl(conn)) |
| 5788 | return NULL; |
| 5789 | return SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 5790 | #else |
| 5791 | return 0; |
| 5792 | #endif |
| 5793 | } |
| 5794 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5795 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5796 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 5797 | { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5798 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5799 | return NULL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5800 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5801 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 5802 | } |
| 5803 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5804 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5805 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 5806 | { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5807 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5808 | return NULL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5809 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5810 | return SSL_get_version(conn->xprt_ctx); |
| 5811 | } |
| 5812 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5813 | /* Extract a serial from a cert, and copy it to a chunk. |
| 5814 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 5815 | * -1 if output is not large enough. |
| 5816 | */ |
| 5817 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5818 | ssl_sock_get_serial(X509 *crt, struct buffer *out) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5819 | { |
| 5820 | ASN1_INTEGER *serial; |
| 5821 | |
| 5822 | serial = X509_get_serialNumber(crt); |
| 5823 | if (!serial) |
| 5824 | return 0; |
| 5825 | |
| 5826 | if (out->size < serial->length) |
| 5827 | return -1; |
| 5828 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5829 | memcpy(out->area, serial->data, serial->length); |
| 5830 | out->data = serial->length; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 5831 | return 1; |
| 5832 | } |
| 5833 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5834 | /* Extract a cert to der, and copy it to a chunk. |
| 5835 | * Returns 1 if cert is found and copied, 0 on der convertion failure and |
| 5836 | * -1 if output is not large enough. |
| 5837 | */ |
| 5838 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5839 | ssl_sock_crt2der(X509 *crt, struct buffer *out) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5840 | { |
| 5841 | int len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5842 | unsigned char *p = (unsigned char *) out->area;; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5843 | |
| 5844 | len =i2d_X509(crt, NULL); |
| 5845 | if (len <= 0) |
| 5846 | return 1; |
| 5847 | |
| 5848 | if (out->size < len) |
| 5849 | return -1; |
| 5850 | |
| 5851 | i2d_X509(crt,&p); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5852 | out->data = len; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 5853 | return 1; |
| 5854 | } |
| 5855 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5856 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5857 | /* Copy Date in ASN1_UTCTIME format in struct buffer out. |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5858 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 5859 | * and -1 if output is not large enough. |
| 5860 | */ |
| 5861 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5862 | ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5863 | { |
| 5864 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 5865 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 5866 | |
| 5867 | if (gentm->length < 12) |
| 5868 | return 0; |
| 5869 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 5870 | return 0; |
| 5871 | if (out->size < gentm->length-2) |
| 5872 | return -1; |
| 5873 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5874 | memcpy(out->area, gentm->data+2, gentm->length-2); |
| 5875 | out->data = gentm->length-2; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5876 | return 1; |
| 5877 | } |
| 5878 | else if (tm->type == V_ASN1_UTCTIME) { |
| 5879 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 5880 | |
| 5881 | if (utctm->length < 10) |
| 5882 | return 0; |
| 5883 | if (utctm->data[0] >= 0x35) |
| 5884 | return 0; |
| 5885 | if (out->size < utctm->length) |
| 5886 | return -1; |
| 5887 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5888 | memcpy(out->area, utctm->data, utctm->length); |
| 5889 | out->data = utctm->length; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5890 | return 1; |
| 5891 | } |
| 5892 | |
| 5893 | return 0; |
| 5894 | } |
| 5895 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5896 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 5897 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 5898 | */ |
| 5899 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5900 | ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos, |
| 5901 | struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5902 | { |
| 5903 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5904 | ASN1_OBJECT *obj; |
| 5905 | ASN1_STRING *data; |
| 5906 | const unsigned char *data_ptr; |
| 5907 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5908 | int i, j, n; |
| 5909 | int cur = 0; |
| 5910 | const char *s; |
| 5911 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5912 | int name_count; |
| 5913 | |
| 5914 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5915 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5916 | out->data = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5917 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5918 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5919 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5920 | else |
| 5921 | j = i; |
| 5922 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5923 | ne = X509_NAME_get_entry(a, j); |
| 5924 | obj = X509_NAME_ENTRY_get_object(ne); |
| 5925 | data = X509_NAME_ENTRY_get_data(ne); |
| 5926 | data_ptr = ASN1_STRING_get0_data(data); |
| 5927 | data_len = ASN1_STRING_length(data); |
| 5928 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5929 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5930 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5931 | s = tmp; |
| 5932 | } |
| 5933 | |
| 5934 | if (chunk_strcasecmp(entry, s) != 0) |
| 5935 | continue; |
| 5936 | |
| 5937 | if (pos < 0) |
| 5938 | cur--; |
| 5939 | else |
| 5940 | cur++; |
| 5941 | |
| 5942 | if (cur != pos) |
| 5943 | continue; |
| 5944 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5945 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5946 | return -1; |
| 5947 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5948 | memcpy(out->area, data_ptr, data_len); |
| 5949 | out->data = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5950 | return 1; |
| 5951 | } |
| 5952 | |
| 5953 | return 0; |
| 5954 | |
| 5955 | } |
| 5956 | |
| 5957 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 5958 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 5959 | */ |
| 5960 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5961 | ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5962 | { |
| 5963 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5964 | ASN1_OBJECT *obj; |
| 5965 | ASN1_STRING *data; |
| 5966 | const unsigned char *data_ptr; |
| 5967 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5968 | int i, n, ln; |
| 5969 | int l = 0; |
| 5970 | const char *s; |
| 5971 | char *p; |
| 5972 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5973 | int name_count; |
| 5974 | |
| 5975 | |
| 5976 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5977 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5978 | out->data = 0; |
| 5979 | p = out->area; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5980 | for (i = 0; i < name_count; i++) { |
| 5981 | ne = X509_NAME_get_entry(a, i); |
| 5982 | obj = X509_NAME_ENTRY_get_object(ne); |
| 5983 | data = X509_NAME_ENTRY_get_data(ne); |
| 5984 | data_ptr = ASN1_STRING_get0_data(data); |
| 5985 | data_len = ASN1_STRING_length(data); |
| 5986 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5987 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5988 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5989 | s = tmp; |
| 5990 | } |
| 5991 | ln = strlen(s); |
| 5992 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5993 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5994 | if (l > out->size) |
| 5995 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5996 | out->data = l; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5997 | |
| 5998 | *(p++)='/'; |
| 5999 | memcpy(p, s, ln); |
| 6000 | p += ln; |
| 6001 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6002 | memcpy(p, data_ptr, data_len); |
| 6003 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6004 | } |
| 6005 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6006 | if (!out->data) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6007 | return 0; |
| 6008 | |
| 6009 | return 1; |
| 6010 | } |
| 6011 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6012 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 6013 | * to disable SNI. |
| 6014 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6015 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 6016 | { |
| 6017 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6018 | char *prev_name; |
| 6019 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6020 | if (!ssl_sock_is_ssl(conn)) |
| 6021 | return; |
| 6022 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6023 | /* if the SNI changes, we must destroy the reusable context so that a |
| 6024 | * new connection will present a new SNI. As an optimization we could |
| 6025 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 6026 | * server. |
| 6027 | */ |
| 6028 | prev_name = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 6029 | if ((!prev_name && hostname) || |
| 6030 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
| 6031 | SSL_set_session(conn->xprt_ctx, NULL); |
| 6032 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6033 | SSL_set_tlsext_host_name(conn->xprt_ctx, hostname); |
| 6034 | #endif |
| 6035 | } |
| 6036 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6037 | /* Extract peer certificate's common name into the chunk dest |
| 6038 | * Returns |
| 6039 | * the len of the extracted common name |
| 6040 | * or 0 if no CN found in DN |
| 6041 | * or -1 on error case (i.e. no peer certificate) |
| 6042 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6043 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 6044 | struct buffer *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"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6049 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6050 | .area = (char *)&find_cn, |
| 6051 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 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; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6187 | struct buffer *smp_trash; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 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; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6231 | struct buffer *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; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6276 | struct buffer *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(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6297 | X509_digest(crt, digest, (unsigned char *) smp_trash->area, |
| 6298 | (unsigned int *)&smp_trash->data); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6299 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6300 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6301 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6302 | ret = 1; |
| 6303 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6304 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6305 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6306 | X509_free(crt); |
| 6307 | return ret; |
| 6308 | } |
| 6309 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6310 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 6311 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6312 | * should be use. |
| 6313 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6314 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6315 | 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] | 6316 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6317 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6318 | X509 *crt = NULL; |
| 6319 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6320 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6321 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6322 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6323 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6324 | if (!conn || conn->xprt != &ssl_sock) |
| 6325 | return 0; |
| 6326 | |
| 6327 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6328 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6329 | return 0; |
| 6330 | } |
| 6331 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6332 | if (cert_peer) |
| 6333 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6334 | else |
| 6335 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6336 | if (!crt) |
| 6337 | goto out; |
| 6338 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6339 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6340 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 6341 | goto out; |
| 6342 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6343 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6344 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6345 | ret = 1; |
| 6346 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6347 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6348 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6349 | X509_free(crt); |
| 6350 | return ret; |
| 6351 | } |
| 6352 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6353 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 6354 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6355 | * should be use. |
| 6356 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6357 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6358 | 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] | 6359 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6360 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6361 | X509 *crt = NULL; |
| 6362 | X509_NAME *name; |
| 6363 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6364 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6365 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6366 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6367 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6368 | if (!conn || conn->xprt != &ssl_sock) |
| 6369 | return 0; |
| 6370 | |
| 6371 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6372 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6373 | return 0; |
| 6374 | } |
| 6375 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6376 | if (cert_peer) |
| 6377 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6378 | else |
| 6379 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6380 | if (!crt) |
| 6381 | goto out; |
| 6382 | |
| 6383 | name = X509_get_issuer_name(crt); |
| 6384 | if (!name) |
| 6385 | goto out; |
| 6386 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6387 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6388 | if (args && args[0].type == ARGT_STR) { |
| 6389 | int pos = 1; |
| 6390 | |
| 6391 | if (args[1].type == ARGT_SINT) |
| 6392 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6393 | |
| 6394 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6395 | goto out; |
| 6396 | } |
| 6397 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6398 | goto out; |
| 6399 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6400 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6401 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6402 | ret = 1; |
| 6403 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6404 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6405 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6406 | X509_free(crt); |
| 6407 | return ret; |
| 6408 | } |
| 6409 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6410 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 6411 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6412 | * should be use. |
| 6413 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6414 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6415 | 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] | 6416 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6417 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6418 | X509 *crt = NULL; |
| 6419 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6420 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6421 | struct connection *conn; |
| 6422 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6423 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6424 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6425 | return 0; |
| 6426 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6427 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6428 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6429 | return 0; |
| 6430 | } |
| 6431 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6432 | if (cert_peer) |
| 6433 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6434 | else |
| 6435 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6436 | if (!crt) |
| 6437 | goto out; |
| 6438 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6439 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6440 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 6441 | goto out; |
| 6442 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6443 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6444 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6445 | ret = 1; |
| 6446 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6447 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6448 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6449 | X509_free(crt); |
| 6450 | return ret; |
| 6451 | } |
| 6452 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6453 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 6454 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6455 | * should be use. |
| 6456 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6457 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6458 | 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] | 6459 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6460 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6461 | X509 *crt = NULL; |
| 6462 | X509_NAME *name; |
| 6463 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6464 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6465 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6466 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6467 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6468 | if (!conn || conn->xprt != &ssl_sock) |
| 6469 | return 0; |
| 6470 | |
| 6471 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6472 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6473 | return 0; |
| 6474 | } |
| 6475 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6476 | if (cert_peer) |
| 6477 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6478 | else |
| 6479 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6480 | if (!crt) |
| 6481 | goto out; |
| 6482 | |
| 6483 | name = X509_get_subject_name(crt); |
| 6484 | if (!name) |
| 6485 | goto out; |
| 6486 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6487 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6488 | if (args && args[0].type == ARGT_STR) { |
| 6489 | int pos = 1; |
| 6490 | |
| 6491 | if (args[1].type == ARGT_SINT) |
| 6492 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6493 | |
| 6494 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6495 | goto out; |
| 6496 | } |
| 6497 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6498 | goto out; |
| 6499 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6500 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6501 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6502 | ret = 1; |
| 6503 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6504 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6505 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6506 | X509_free(crt); |
| 6507 | return ret; |
| 6508 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6509 | |
| 6510 | /* integer, returns true if current session use a client certificate */ |
| 6511 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6512 | 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] | 6513 | { |
| 6514 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6515 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6516 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6517 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6518 | if (!conn || conn->xprt != &ssl_sock) |
| 6519 | return 0; |
| 6520 | |
| 6521 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6522 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6523 | return 0; |
| 6524 | } |
| 6525 | |
| 6526 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6527 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6528 | if (crt) { |
| 6529 | X509_free(crt); |
| 6530 | } |
| 6531 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6532 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6533 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6534 | return 1; |
| 6535 | } |
| 6536 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6537 | /* integer, returns the certificate version |
| 6538 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6539 | * should be use. |
| 6540 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6541 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6542 | 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] | 6543 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6544 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6545 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6546 | struct connection *conn; |
| 6547 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6548 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6549 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6550 | return 0; |
| 6551 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6552 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6553 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6554 | return 0; |
| 6555 | } |
| 6556 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6557 | if (cert_peer) |
| 6558 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6559 | else |
| 6560 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6561 | if (!crt) |
| 6562 | return 0; |
| 6563 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6564 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6565 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6566 | if (cert_peer) |
| 6567 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6568 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6569 | |
| 6570 | return 1; |
| 6571 | } |
| 6572 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6573 | /* string, returns the certificate's signature algorithm. |
| 6574 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6575 | * should be use. |
| 6576 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6577 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6578 | 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] | 6579 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6580 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6581 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6582 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6583 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6584 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6585 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6586 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6587 | if (!conn || conn->xprt != &ssl_sock) |
| 6588 | return 0; |
| 6589 | |
| 6590 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6591 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6592 | return 0; |
| 6593 | } |
| 6594 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6595 | if (cert_peer) |
| 6596 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6597 | else |
| 6598 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6599 | if (!crt) |
| 6600 | return 0; |
| 6601 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6602 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6603 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6604 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6605 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 6606 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6607 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6608 | if (cert_peer) |
| 6609 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6610 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 6611 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6612 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6613 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6614 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6615 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6616 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6617 | if (cert_peer) |
| 6618 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6619 | |
| 6620 | return 1; |
| 6621 | } |
| 6622 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6623 | /* string, returns the certificate's key algorithm. |
| 6624 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6625 | * should be use. |
| 6626 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6627 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6628 | 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] | 6629 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6630 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6631 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6632 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6633 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6634 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6635 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6636 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6637 | if (!conn || conn->xprt != &ssl_sock) |
| 6638 | return 0; |
| 6639 | |
| 6640 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6641 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6642 | return 0; |
| 6643 | } |
| 6644 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6645 | if (cert_peer) |
| 6646 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 6647 | else |
| 6648 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6649 | if (!crt) |
| 6650 | return 0; |
| 6651 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6652 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 6653 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6654 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6655 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 6656 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6657 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6658 | if (cert_peer) |
| 6659 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6660 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 6661 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6662 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6663 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6664 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6665 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6666 | if (cert_peer) |
| 6667 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6668 | |
| 6669 | return 1; |
| 6670 | } |
| 6671 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6672 | /* boolean, returns true if front conn. transport layer is SSL. |
| 6673 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6674 | * char is 'b'. |
| 6675 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6676 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6677 | 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] | 6678 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6679 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6680 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6681 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6682 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6683 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6684 | return 1; |
| 6685 | } |
| 6686 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 6687 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6688 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6689 | 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] | 6690 | { |
| 6691 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6692 | struct connection *conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6693 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6694 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6695 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6696 | conn->xprt_ctx && |
| 6697 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6698 | return 1; |
| 6699 | #else |
| 6700 | return 0; |
| 6701 | #endif |
| 6702 | } |
| 6703 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 6704 | /* boolean, returns true if client session has been resumed. |
| 6705 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6706 | * char is 'b'. |
| 6707 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6708 | static int |
| 6709 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6710 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 6711 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6712 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 6713 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6714 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6715 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6716 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 6717 | conn->xprt_ctx && |
| 6718 | SSL_session_reused(conn->xprt_ctx); |
| 6719 | return 1; |
| 6720 | } |
| 6721 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6722 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 6723 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6724 | * char is 'b'. |
| 6725 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6726 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6727 | 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] | 6728 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6729 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6730 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6731 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6732 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6733 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6734 | return 0; |
| 6735 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6736 | smp->data.u.str.area = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
| 6737 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6738 | return 0; |
| 6739 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6740 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6741 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6742 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6743 | |
| 6744 | return 1; |
| 6745 | } |
| 6746 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6747 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 6748 | * is SSL. |
| 6749 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6750 | * char is 'b'. |
| 6751 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6752 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6753 | 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] | 6754 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6755 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6756 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6757 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6758 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6759 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6760 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6761 | return 0; |
| 6762 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 6763 | if (!SSL_get_cipher_bits(conn->xprt_ctx, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6764 | return 0; |
| 6765 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6766 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6767 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6768 | |
| 6769 | return 1; |
| 6770 | } |
| 6771 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6772 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 6773 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6774 | * char is 'b'. |
| 6775 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6776 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6777 | 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] | 6778 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6779 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6780 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6781 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6782 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6783 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6784 | return 0; |
| 6785 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6786 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
| 6787 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6788 | return 0; |
| 6789 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6790 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6791 | |
| 6792 | return 1; |
| 6793 | } |
| 6794 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6795 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6796 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6797 | 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] | 6798 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6799 | struct connection *conn; |
| 6800 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6801 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6802 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6803 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6804 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6805 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6806 | return 0; |
| 6807 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6808 | smp->data.u.str.area = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6809 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6810 | (const unsigned char **)&smp->data.u.str.area, |
| 6811 | (unsigned *)&smp->data.u.str.data); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6812 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6813 | if (!smp->data.u.str.area) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6814 | return 0; |
| 6815 | |
| 6816 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6817 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6818 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6819 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6820 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6821 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6822 | 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] | 6823 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6824 | struct connection *conn; |
| 6825 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6826 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6827 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6828 | |
Willy Tarreau | e26bf05 | 2015-05-12 10:30:12 +0200 | [diff] [blame] | 6829 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6830 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6831 | return 0; |
| 6832 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6833 | smp->data.u.str.area = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6834 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6835 | (const unsigned char **)&smp->data.u.str.area, |
| 6836 | (unsigned *)&smp->data.u.str.data); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6837 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6838 | if (!smp->data.u.str.area) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6839 | return 0; |
| 6840 | |
| 6841 | return 1; |
| 6842 | } |
| 6843 | #endif |
| 6844 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6845 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 6846 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6847 | * char is 'b'. |
| 6848 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 6849 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6850 | 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] | 6851 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6852 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6853 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6854 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6855 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6856 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6857 | return 0; |
| 6858 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6859 | smp->data.u.str.area = (char *)SSL_get_version(conn->xprt_ctx); |
| 6860 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6861 | return 0; |
| 6862 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6863 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6864 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6865 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6866 | |
| 6867 | return 1; |
| 6868 | } |
| 6869 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6870 | /* 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] | 6871 | * This function is also usable on backend conn if the fetch keyword 5th |
| 6872 | * char is 'b'. |
| 6873 | */ |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 6874 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 6875 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6876 | 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] | 6877 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 6878 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6879 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 6880 | SSL_SESSION *ssl_sess; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6881 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6882 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6883 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6884 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6885 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6886 | return 0; |
| 6887 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6888 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 6889 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6890 | return 0; |
| 6891 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6892 | smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, |
| 6893 | (unsigned int *)&smp->data.u.str.data); |
| 6894 | if (!smp->data.u.str.area || !smp->data.u.str.data) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6895 | return 0; |
| 6896 | |
| 6897 | return 1; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6898 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 6899 | #endif |
| 6900 | |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6901 | |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 6902 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
| 6903 | static int |
| 6904 | smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6905 | { |
| 6906 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 6907 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 6908 | SSL_SESSION *ssl_sess; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6909 | struct buffer *data; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 6910 | |
| 6911 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6912 | return 0; |
| 6913 | |
| 6914 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 6915 | if (!ssl_sess) |
| 6916 | return 0; |
| 6917 | |
| 6918 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6919 | data->data = SSL_SESSION_get_master_key(ssl_sess, |
| 6920 | (unsigned char *) data->area, |
| 6921 | data->size); |
| 6922 | if (!data->data) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 6923 | return 0; |
| 6924 | |
| 6925 | smp->flags = 0; |
| 6926 | smp->data.type = SMP_T_BIN; |
| 6927 | smp->data.u.str = *data; |
| 6928 | |
| 6929 | return 1; |
| 6930 | } |
| 6931 | #endif |
| 6932 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 6933 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 6934 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6935 | 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] | 6936 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6937 | struct connection *conn; |
| 6938 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6939 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6940 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6941 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6942 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6943 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6944 | return 0; |
| 6945 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6946 | smp->data.u.str.area = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 6947 | if (!smp->data.u.str.area) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 6948 | return 0; |
| 6949 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6950 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6951 | return 1; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6952 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 6953 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6954 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 6955 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6956 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6957 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6958 | struct connection *conn; |
| 6959 | struct ssl_capture *capture; |
| 6960 | |
| 6961 | conn = objt_conn(smp->sess->origin); |
| 6962 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6963 | return 0; |
| 6964 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6965 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6966 | if (!capture) |
| 6967 | return 0; |
| 6968 | |
| 6969 | smp->flags = SMP_F_CONST; |
| 6970 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6971 | smp->data.u.str.area = capture->ciphersuite; |
| 6972 | smp->data.u.str.data = capture->ciphersuite_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6973 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6974 | } |
| 6975 | |
| 6976 | static int |
| 6977 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6978 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6979 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6980 | |
| 6981 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 6982 | return 0; |
| 6983 | |
| 6984 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6985 | dump_binary(data, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6986 | smp->data.type = SMP_T_BIN; |
| 6987 | smp->data.u.str = *data; |
| 6988 | return 1; |
| 6989 | } |
| 6990 | |
| 6991 | static int |
| 6992 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6993 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6994 | struct connection *conn; |
| 6995 | struct ssl_capture *capture; |
| 6996 | |
| 6997 | conn = objt_conn(smp->sess->origin); |
| 6998 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 6999 | return 0; |
| 7000 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7001 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7002 | if (!capture) |
| 7003 | return 0; |
| 7004 | |
| 7005 | smp->data.type = SMP_T_SINT; |
| 7006 | smp->data.u.sint = capture->xxh64; |
| 7007 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7008 | } |
| 7009 | |
| 7010 | static int |
| 7011 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7012 | { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7013 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7014 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7015 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7016 | |
| 7017 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 7018 | return 0; |
| 7019 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7020 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7021 | for (i = 0; i + 1 < smp->data.u.str.data; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7022 | const char *str; |
| 7023 | const SSL_CIPHER *cipher; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7024 | const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i; |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7025 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 7026 | #if defined(OPENSSL_IS_BORINGSSL) |
| 7027 | cipher = SSL_get_cipher_by_value(id); |
| 7028 | #else |
| 7029 | struct connection *conn = objt_conn(smp->sess->origin); |
| 7030 | cipher = SSL_CIPHER_find(conn->xprt_ctx, bin); |
| 7031 | #endif |
| 7032 | str = SSL_CIPHER_get_name(cipher); |
| 7033 | if (!str || strcmp(str, "(NONE)") == 0) |
| 7034 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7035 | else |
| 7036 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 7037 | } |
| 7038 | smp->data.type = SMP_T_STR; |
| 7039 | smp->data.u.str = *data; |
| 7040 | return 1; |
| 7041 | #else |
| 7042 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 7043 | #endif |
| 7044 | } |
| 7045 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7046 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7047 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7048 | 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] | 7049 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7050 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7051 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7052 | int finished_len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7053 | struct buffer *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7054 | |
| 7055 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7056 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7057 | return 0; |
| 7058 | |
| 7059 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 7060 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7061 | return 0; |
| 7062 | } |
| 7063 | |
| 7064 | finished_trash = get_trash_chunk(); |
| 7065 | if (!SSL_session_reused(conn->xprt_ctx)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7066 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, |
| 7067 | finished_trash->area, |
| 7068 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7069 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7070 | finished_len = SSL_get_finished(conn->xprt_ctx, |
| 7071 | finished_trash->area, |
| 7072 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7073 | |
| 7074 | if (!finished_len) |
| 7075 | return 0; |
| 7076 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7077 | finished_trash->data = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7078 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7079 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7080 | |
| 7081 | return 1; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7082 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7083 | #endif |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7084 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7085 | /* 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] | 7086 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7087 | 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] | 7088 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7089 | struct connection *conn; |
| 7090 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7091 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7092 | if (!conn || conn->xprt != &ssl_sock) |
| 7093 | return 0; |
| 7094 | |
| 7095 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7096 | smp->flags = SMP_F_MAY_CHANGE; |
| 7097 | return 0; |
| 7098 | } |
| 7099 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7100 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7101 | 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] | 7102 | smp->flags = 0; |
| 7103 | |
| 7104 | return 1; |
| 7105 | } |
| 7106 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7107 | /* 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] | 7108 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7109 | 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] | 7110 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7111 | struct connection *conn; |
| 7112 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7113 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7114 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7115 | return 0; |
| 7116 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7117 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7118 | smp->flags = SMP_F_MAY_CHANGE; |
| 7119 | return 0; |
| 7120 | } |
| 7121 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7122 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7123 | 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] | 7124 | smp->flags = 0; |
| 7125 | |
| 7126 | return 1; |
| 7127 | } |
| 7128 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7129 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7130 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7131 | 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] | 7132 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7133 | struct connection *conn; |
| 7134 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7135 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7136 | if (!conn || conn->xprt != &ssl_sock) |
| 7137 | return 0; |
| 7138 | |
| 7139 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7140 | smp->flags = SMP_F_MAY_CHANGE; |
| 7141 | return 0; |
| 7142 | } |
| 7143 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7144 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7145 | 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] | 7146 | smp->flags = 0; |
| 7147 | |
| 7148 | return 1; |
| 7149 | } |
| 7150 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7151 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7152 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7153 | 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] | 7154 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7155 | struct connection *conn; |
| 7156 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7157 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7158 | if (!conn || conn->xprt != &ssl_sock) |
| 7159 | return 0; |
| 7160 | |
| 7161 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7162 | smp->flags = SMP_F_MAY_CHANGE; |
| 7163 | return 0; |
| 7164 | } |
| 7165 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7166 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7167 | return 0; |
| 7168 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7169 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7170 | 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] | 7171 | smp->flags = 0; |
| 7172 | |
| 7173 | return 1; |
| 7174 | } |
| 7175 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7176 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7177 | 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] | 7178 | { |
| 7179 | if (!*args[cur_arg + 1]) { |
| 7180 | if (err) |
| 7181 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7182 | return ERR_ALERT | ERR_FATAL; |
| 7183 | } |
| 7184 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7185 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7186 | 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] | 7187 | else |
| 7188 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7189 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7190 | return 0; |
| 7191 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7192 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7193 | { |
| 7194 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7195 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7196 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7197 | /* parse the "ca-sign-file" bind keyword */ |
| 7198 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7199 | { |
| 7200 | if (!*args[cur_arg + 1]) { |
| 7201 | if (err) |
| 7202 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7203 | return ERR_ALERT | ERR_FATAL; |
| 7204 | } |
| 7205 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7206 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7207 | 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] | 7208 | else |
| 7209 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 7210 | |
| 7211 | return 0; |
| 7212 | } |
| 7213 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7214 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7215 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7216 | { |
| 7217 | if (!*args[cur_arg + 1]) { |
| 7218 | if (err) |
| 7219 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 7220 | return ERR_ALERT | ERR_FATAL; |
| 7221 | } |
| 7222 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 7223 | return 0; |
| 7224 | } |
| 7225 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7226 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7227 | 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] | 7228 | { |
| 7229 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7230 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7231 | return ERR_ALERT | ERR_FATAL; |
| 7232 | } |
| 7233 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7234 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7235 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7236 | return 0; |
| 7237 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7238 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7239 | { |
| 7240 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 7241 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7242 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7243 | 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] | 7244 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 7245 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 7246 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7247 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7248 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7249 | return ERR_ALERT | ERR_FATAL; |
| 7250 | } |
| 7251 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7252 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 7253 | 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] | 7254 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 7255 | return ERR_ALERT | ERR_FATAL; |
| 7256 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7257 | 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] | 7258 | if (ssl_sock_load_cert(path, conf, err) > 0) |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7259 | return ERR_ALERT | ERR_FATAL; |
| 7260 | |
| 7261 | return 0; |
| 7262 | } |
| 7263 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 7264 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7265 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7266 | |
| 7267 | return 0; |
| 7268 | } |
| 7269 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7270 | /* parse the "crt-list" bind keyword */ |
| 7271 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7272 | { |
| 7273 | if (!*args[cur_arg + 1]) { |
| 7274 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 7275 | return ERR_ALERT | ERR_FATAL; |
| 7276 | } |
| 7277 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7278 | 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] | 7279 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7280 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 7281 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7282 | |
| 7283 | return 0; |
| 7284 | } |
| 7285 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7286 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7287 | 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] | 7288 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7289 | #ifndef X509_V_FLAG_CRL_CHECK |
| 7290 | if (err) |
| 7291 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 7292 | return ERR_ALERT | ERR_FATAL; |
| 7293 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7294 | if (!*args[cur_arg + 1]) { |
| 7295 | if (err) |
| 7296 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 7297 | return ERR_ALERT | ERR_FATAL; |
| 7298 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7299 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7300 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7301 | 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] | 7302 | else |
| 7303 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7304 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7305 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7306 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7307 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7308 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7309 | { |
| 7310 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7311 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7312 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 7313 | /* parse the "curves" bind keyword keyword */ |
| 7314 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7315 | { |
| 7316 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 7317 | if (!*args[cur_arg + 1]) { |
| 7318 | if (err) |
| 7319 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 7320 | return ERR_ALERT | ERR_FATAL; |
| 7321 | } |
| 7322 | conf->curves = strdup(args[cur_arg + 1]); |
| 7323 | return 0; |
| 7324 | #else |
| 7325 | if (err) |
| 7326 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 7327 | return ERR_ALERT | ERR_FATAL; |
| 7328 | #endif |
| 7329 | } |
| 7330 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7331 | { |
| 7332 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 7333 | } |
| 7334 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7335 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7336 | 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] | 7337 | { |
| 7338 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 7339 | if (err) |
| 7340 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 7341 | return ERR_ALERT | ERR_FATAL; |
| 7342 | #elif defined(OPENSSL_NO_ECDH) |
| 7343 | if (err) |
| 7344 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 7345 | return ERR_ALERT | ERR_FATAL; |
| 7346 | #else |
| 7347 | if (!*args[cur_arg + 1]) { |
| 7348 | if (err) |
| 7349 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 7350 | return ERR_ALERT | ERR_FATAL; |
| 7351 | } |
| 7352 | |
| 7353 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7354 | |
| 7355 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7356 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7357 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7358 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7359 | { |
| 7360 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 7361 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7362 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7363 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7364 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7365 | { |
| 7366 | int code; |
| 7367 | char *p = args[cur_arg + 1]; |
| 7368 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 7369 | |
| 7370 | if (!*p) { |
| 7371 | if (err) |
| 7372 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 7373 | return ERR_ALERT | ERR_FATAL; |
| 7374 | } |
| 7375 | |
| 7376 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 7377 | ignerr = &conf->ca_ignerr; |
| 7378 | |
| 7379 | if (strcmp(p, "all") == 0) { |
| 7380 | *ignerr = ~0ULL; |
| 7381 | return 0; |
| 7382 | } |
| 7383 | |
| 7384 | while (p) { |
| 7385 | code = atoi(p); |
| 7386 | if ((code <= 0) || (code > 63)) { |
| 7387 | if (err) |
| 7388 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 7389 | args[cur_arg], code, args[cur_arg + 1]); |
| 7390 | return ERR_ALERT | ERR_FATAL; |
| 7391 | } |
| 7392 | *ignerr |= 1ULL << code; |
| 7393 | p = strchr(p, ','); |
| 7394 | if (p) |
| 7395 | p++; |
| 7396 | } |
| 7397 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7398 | return 0; |
| 7399 | } |
| 7400 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7401 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 7402 | 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] | 7403 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7404 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7405 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7406 | p = strchr(arg, '-'); |
| 7407 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7408 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7409 | p++; |
| 7410 | if (!strcmp(p, "sslv3")) |
| 7411 | v = CONF_SSLV3; |
| 7412 | else if (!strcmp(p, "tlsv10")) |
| 7413 | v = CONF_TLSV10; |
| 7414 | else if (!strcmp(p, "tlsv11")) |
| 7415 | v = CONF_TLSV11; |
| 7416 | else if (!strcmp(p, "tlsv12")) |
| 7417 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 7418 | else if (!strcmp(p, "tlsv13")) |
| 7419 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7420 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7421 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7422 | if (!strncmp(arg, "no-", 3)) |
| 7423 | methods->flags |= methodVersions[v].flag; |
| 7424 | else if (!strncmp(arg, "force-", 6)) |
| 7425 | methods->min = methods->max = v; |
| 7426 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7427 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7428 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7429 | fail: |
| 7430 | if (err) |
| 7431 | memprintf(err, "'%s' : option not implemented", arg); |
| 7432 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7433 | } |
| 7434 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7435 | 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] | 7436 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7437 | 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] | 7438 | } |
| 7439 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7440 | 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] | 7441 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7442 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 7443 | } |
| 7444 | |
| 7445 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 7446 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 7447 | { |
| 7448 | uint16_t i, v = 0; |
| 7449 | char *argv = args[cur_arg + 1]; |
| 7450 | if (!*argv) { |
| 7451 | if (err) |
| 7452 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
| 7453 | return ERR_ALERT | ERR_FATAL; |
| 7454 | } |
| 7455 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 7456 | if (!strcmp(argv, methodVersions[i].name)) |
| 7457 | v = i; |
| 7458 | if (!v) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7459 | if (err) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7460 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7461 | return ERR_ALERT | ERR_FATAL; |
| 7462 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7463 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 7464 | methods->min = v; |
| 7465 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 7466 | methods->max = v; |
| 7467 | else { |
| 7468 | if (err) |
| 7469 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
| 7470 | return ERR_ALERT | ERR_FATAL; |
| 7471 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7472 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7473 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7474 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 7475 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7476 | { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 7477 | #if (OPENSSL_VERSION_NUMBER < 0x10101000L) || !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7478 | 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] | 7479 | #endif |
| 7480 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 7481 | } |
| 7482 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7483 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7484 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7485 | 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] | 7486 | } |
| 7487 | |
| 7488 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7489 | { |
| 7490 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 7491 | } |
| 7492 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7493 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 7494 | 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] | 7495 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 7496 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7497 | return 0; |
| 7498 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7499 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7500 | /* parse the "allow-0rtt" bind keyword */ |
| 7501 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7502 | { |
| 7503 | conf->early_data = 1; |
| 7504 | return 0; |
| 7505 | } |
| 7506 | |
| 7507 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7508 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 7509 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7510 | return 0; |
| 7511 | } |
| 7512 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7513 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7514 | 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] | 7515 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7516 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7517 | char *p1, *p2; |
| 7518 | |
| 7519 | if (!*args[cur_arg + 1]) { |
| 7520 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 7521 | return ERR_ALERT | ERR_FATAL; |
| 7522 | } |
| 7523 | |
| 7524 | free(conf->npn_str); |
| 7525 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 7526 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 7527 | * so we reuse each comma to store the next <len> and need |
| 7528 | * one more for the end of the string. |
| 7529 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7530 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 7531 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7532 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 7533 | |
| 7534 | /* replace commas with the name length */ |
| 7535 | p1 = conf->npn_str; |
| 7536 | p2 = p1 + 1; |
| 7537 | while (1) { |
| 7538 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 7539 | if (!p2) |
| 7540 | p2 = p1 + 1 + strlen(p1 + 1); |
| 7541 | |
| 7542 | if (p2 - (p1 + 1) > 255) { |
| 7543 | *p2 = '\0'; |
| 7544 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 7545 | return ERR_ALERT | ERR_FATAL; |
| 7546 | } |
| 7547 | |
| 7548 | *p1 = p2 - (p1 + 1); |
| 7549 | p1 = p2; |
| 7550 | |
| 7551 | if (!*p2) |
| 7552 | break; |
| 7553 | |
| 7554 | *(p2++) = '\0'; |
| 7555 | } |
| 7556 | return 0; |
| 7557 | #else |
| 7558 | if (err) |
| 7559 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 7560 | return ERR_ALERT | ERR_FATAL; |
| 7561 | #endif |
| 7562 | } |
| 7563 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7564 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7565 | { |
| 7566 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 7567 | } |
| 7568 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7569 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7570 | 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] | 7571 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7572 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7573 | char *p1, *p2; |
| 7574 | |
| 7575 | if (!*args[cur_arg + 1]) { |
| 7576 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 7577 | return ERR_ALERT | ERR_FATAL; |
| 7578 | } |
| 7579 | |
| 7580 | free(conf->alpn_str); |
| 7581 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 7582 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 7583 | * so we reuse each comma to store the next <len> and need |
| 7584 | * one more for the end of the string. |
| 7585 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7586 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 7587 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7588 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 7589 | |
| 7590 | /* replace commas with the name length */ |
| 7591 | p1 = conf->alpn_str; |
| 7592 | p2 = p1 + 1; |
| 7593 | while (1) { |
| 7594 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 7595 | if (!p2) |
| 7596 | p2 = p1 + 1 + strlen(p1 + 1); |
| 7597 | |
| 7598 | if (p2 - (p1 + 1) > 255) { |
| 7599 | *p2 = '\0'; |
| 7600 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 7601 | return ERR_ALERT | ERR_FATAL; |
| 7602 | } |
| 7603 | |
| 7604 | *p1 = p2 - (p1 + 1); |
| 7605 | p1 = p2; |
| 7606 | |
| 7607 | if (!*p2) |
| 7608 | break; |
| 7609 | |
| 7610 | *(p2++) = '\0'; |
| 7611 | } |
| 7612 | return 0; |
| 7613 | #else |
| 7614 | if (err) |
| 7615 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 7616 | return ERR_ALERT | ERR_FATAL; |
| 7617 | #endif |
| 7618 | } |
| 7619 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7620 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7621 | { |
| 7622 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 7623 | } |
| 7624 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7625 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7626 | 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] | 7627 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 7628 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7629 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7630 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7631 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 7632 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 7633 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7634 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 7635 | if (!conf->ssl_conf.ssl_methods.min) |
| 7636 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 7637 | if (!conf->ssl_conf.ssl_methods.max) |
| 7638 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7639 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7640 | return 0; |
| 7641 | } |
| 7642 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 7643 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 7644 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7645 | { |
| 7646 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 7647 | return 0; |
| 7648 | } |
| 7649 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7650 | /* parse the "generate-certificates" bind keyword */ |
| 7651 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7652 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7653 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7654 | conf->generate_certs = 1; |
| 7655 | #else |
| 7656 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 7657 | err && *err ? *err : ""); |
| 7658 | #endif |
| 7659 | return 0; |
| 7660 | } |
| 7661 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 7662 | /* parse the "strict-sni" bind keyword */ |
| 7663 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7664 | { |
| 7665 | conf->strict_sni = 1; |
| 7666 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7667 | } |
| 7668 | |
| 7669 | /* parse the "tls-ticket-keys" bind keyword */ |
| 7670 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7671 | { |
| 7672 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7673 | FILE *f; |
| 7674 | int i = 0; |
| 7675 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7676 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7677 | |
| 7678 | if (!*args[cur_arg + 1]) { |
| 7679 | if (err) |
| 7680 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 7681 | return ERR_ALERT | ERR_FATAL; |
| 7682 | } |
| 7683 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7684 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 7685 | if (keys_ref) { |
| 7686 | keys_ref->refcount++; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7687 | conf->keys_ref = keys_ref; |
| 7688 | return 0; |
| 7689 | } |
| 7690 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 7691 | keys_ref = malloc(sizeof(*keys_ref)); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7692 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct tls_sess_key)); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7693 | |
| 7694 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 7695 | if (err) |
| 7696 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 7697 | return ERR_ALERT | ERR_FATAL; |
| 7698 | } |
| 7699 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7700 | keys_ref->filename = strdup(args[cur_arg + 1]); |
| 7701 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7702 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 7703 | int len = strlen(thisline); |
| 7704 | /* Strip newline characters from the end */ |
| 7705 | if(thisline[len - 1] == '\n') |
| 7706 | thisline[--len] = 0; |
| 7707 | |
| 7708 | if(thisline[len - 1] == '\r') |
| 7709 | thisline[--len] = 0; |
| 7710 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7711 | 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] | 7712 | if (err) |
| 7713 | 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] | 7714 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7715 | return ERR_ALERT | ERR_FATAL; |
| 7716 | } |
| 7717 | i++; |
| 7718 | } |
| 7719 | |
| 7720 | if (i < TLS_TICKETS_NO) { |
| 7721 | if (err) |
| 7722 | 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] | 7723 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7724 | return ERR_ALERT | ERR_FATAL; |
| 7725 | } |
| 7726 | |
| 7727 | fclose(f); |
| 7728 | |
| 7729 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 7730 | i -= 2; |
| 7731 | 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] | 7732 | keys_ref->unique_id = -1; |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 7733 | keys_ref->refcount = 1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7734 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 7735 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7736 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 7737 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 7738 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7739 | return 0; |
| 7740 | #else |
| 7741 | if (err) |
| 7742 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 7743 | return ERR_ALERT | ERR_FATAL; |
| 7744 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 7745 | } |
| 7746 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7747 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7748 | 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] | 7749 | { |
| 7750 | if (!*args[cur_arg + 1]) { |
| 7751 | if (err) |
| 7752 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 7753 | return ERR_ALERT | ERR_FATAL; |
| 7754 | } |
| 7755 | |
| 7756 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7757 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7758 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7759 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7760 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 7761 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7762 | else { |
| 7763 | if (err) |
| 7764 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 7765 | args[cur_arg], args[cur_arg + 1]); |
| 7766 | return ERR_ALERT | ERR_FATAL; |
| 7767 | } |
| 7768 | |
| 7769 | return 0; |
| 7770 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7771 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7772 | { |
| 7773 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 7774 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7775 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 7776 | /* parse the "no-ca-names" bind keyword */ |
| 7777 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7778 | { |
| 7779 | conf->no_ca_names = 1; |
| 7780 | return 0; |
| 7781 | } |
| 7782 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7783 | { |
| 7784 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 7785 | } |
| 7786 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7787 | /************** "server" keywords ****************/ |
| 7788 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7789 | /* parse the "ca-file" server keyword */ |
| 7790 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7791 | { |
| 7792 | if (!*args[*cur_arg + 1]) { |
| 7793 | if (err) |
| 7794 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 7795 | return ERR_ALERT | ERR_FATAL; |
| 7796 | } |
| 7797 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7798 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7799 | 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] | 7800 | else |
| 7801 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 7802 | |
| 7803 | return 0; |
| 7804 | } |
| 7805 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 7806 | /* parse the "check-sni" server keyword */ |
| 7807 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7808 | { |
| 7809 | if (!*args[*cur_arg + 1]) { |
| 7810 | if (err) |
| 7811 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
| 7812 | return ERR_ALERT | ERR_FATAL; |
| 7813 | } |
| 7814 | |
| 7815 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 7816 | if (!newsrv->check.sni) { |
| 7817 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 7818 | return ERR_ALERT | ERR_FATAL; |
| 7819 | } |
| 7820 | return 0; |
| 7821 | |
| 7822 | } |
| 7823 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7824 | /* parse the "check-ssl" server keyword */ |
| 7825 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7826 | { |
| 7827 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7828 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 7829 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
| 7830 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7831 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 7832 | if (!newsrv->ssl_ctx.methods.min) |
| 7833 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 7834 | if (!newsrv->ssl_ctx.methods.max) |
| 7835 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 7836 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7837 | return 0; |
| 7838 | } |
| 7839 | |
| 7840 | /* parse the "ciphers" server keyword */ |
| 7841 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7842 | { |
| 7843 | if (!*args[*cur_arg + 1]) { |
| 7844 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 7845 | return ERR_ALERT | ERR_FATAL; |
| 7846 | } |
| 7847 | |
| 7848 | free(newsrv->ssl_ctx.ciphers); |
| 7849 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 7850 | return 0; |
| 7851 | } |
| 7852 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7853 | /* parse the "crl-file" server keyword */ |
| 7854 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7855 | { |
| 7856 | #ifndef X509_V_FLAG_CRL_CHECK |
| 7857 | if (err) |
| 7858 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 7859 | return ERR_ALERT | ERR_FATAL; |
| 7860 | #else |
| 7861 | if (!*args[*cur_arg + 1]) { |
| 7862 | if (err) |
| 7863 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 7864 | return ERR_ALERT | ERR_FATAL; |
| 7865 | } |
| 7866 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7867 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7868 | 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] | 7869 | else |
| 7870 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 7871 | |
| 7872 | return 0; |
| 7873 | #endif |
| 7874 | } |
| 7875 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 7876 | /* parse the "crt" server keyword */ |
| 7877 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7878 | { |
| 7879 | if (!*args[*cur_arg + 1]) { |
| 7880 | if (err) |
| 7881 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 7882 | return ERR_ALERT | ERR_FATAL; |
| 7883 | } |
| 7884 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7885 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 7886 | 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] | 7887 | else |
| 7888 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 7889 | |
| 7890 | return 0; |
| 7891 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7892 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 7893 | /* parse the "no-check-ssl" server keyword */ |
| 7894 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7895 | { |
| 7896 | newsrv->check.use_ssl = 0; |
| 7897 | free(newsrv->ssl_ctx.ciphers); |
| 7898 | newsrv->ssl_ctx.ciphers = NULL; |
| 7899 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 7900 | return 0; |
| 7901 | } |
| 7902 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 7903 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 7904 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7905 | { |
| 7906 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 7907 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 7908 | return 0; |
| 7909 | } |
| 7910 | |
| 7911 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 7912 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7913 | { |
| 7914 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 7915 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 7916 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 7917 | return 0; |
| 7918 | } |
| 7919 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 7920 | /* parse the "no-ssl" server keyword */ |
| 7921 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7922 | { |
| 7923 | newsrv->use_ssl = 0; |
| 7924 | free(newsrv->ssl_ctx.ciphers); |
| 7925 | newsrv->ssl_ctx.ciphers = NULL; |
| 7926 | return 0; |
| 7927 | } |
| 7928 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7929 | /* parse the "allow-0rtt" server keyword */ |
| 7930 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7931 | { |
| 7932 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 7933 | return 0; |
| 7934 | } |
| 7935 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 7936 | /* parse the "no-ssl-reuse" server keyword */ |
| 7937 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7938 | { |
| 7939 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 7940 | return 0; |
| 7941 | } |
| 7942 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7943 | /* parse the "no-tls-tickets" server keyword */ |
| 7944 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7945 | { |
| 7946 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 7947 | return 0; |
| 7948 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7949 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 7950 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7951 | { |
| 7952 | newsrv->pp_opts |= SRV_PP_V2; |
| 7953 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 7954 | return 0; |
| 7955 | } |
| 7956 | |
| 7957 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 7958 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7959 | { |
| 7960 | newsrv->pp_opts |= SRV_PP_V2; |
| 7961 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 7962 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 7963 | return 0; |
| 7964 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7965 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7966 | /* parse the "sni" server keyword */ |
| 7967 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7968 | { |
| 7969 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7970 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 7971 | return ERR_ALERT | ERR_FATAL; |
| 7972 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7973 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7974 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7975 | arg = args[*cur_arg + 1]; |
| 7976 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7977 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 7978 | return ERR_ALERT | ERR_FATAL; |
| 7979 | } |
| 7980 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 7981 | free(newsrv->sni_expr); |
| 7982 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7983 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7984 | return 0; |
| 7985 | #endif |
| 7986 | } |
| 7987 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7988 | /* parse the "ssl" server keyword */ |
| 7989 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7990 | { |
| 7991 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7992 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 7993 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7994 | return 0; |
| 7995 | } |
| 7996 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 7997 | /* parse the "ssl-reuse" server keyword */ |
| 7998 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7999 | { |
| 8000 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 8001 | return 0; |
| 8002 | } |
| 8003 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 8004 | /* parse the "tls-tickets" server keyword */ |
| 8005 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8006 | { |
| 8007 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 8008 | return 0; |
| 8009 | } |
| 8010 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8011 | /* parse the "verify" server keyword */ |
| 8012 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8013 | { |
| 8014 | if (!*args[*cur_arg + 1]) { |
| 8015 | if (err) |
| 8016 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 8017 | return ERR_ALERT | ERR_FATAL; |
| 8018 | } |
| 8019 | |
| 8020 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8021 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8022 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8023 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8024 | else { |
| 8025 | if (err) |
| 8026 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 8027 | args[*cur_arg], args[*cur_arg + 1]); |
| 8028 | return ERR_ALERT | ERR_FATAL; |
| 8029 | } |
| 8030 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8031 | return 0; |
| 8032 | } |
| 8033 | |
| 8034 | /* parse the "verifyhost" server keyword */ |
| 8035 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8036 | { |
| 8037 | if (!*args[*cur_arg + 1]) { |
| 8038 | if (err) |
| 8039 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 8040 | return ERR_ALERT | ERR_FATAL; |
| 8041 | } |
| 8042 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 8043 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8044 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 8045 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8046 | return 0; |
| 8047 | } |
| 8048 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8049 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 8050 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 8051 | struct proxy *defpx, const char *file, int line, |
| 8052 | char **err) { |
| 8053 | int i = 1; |
| 8054 | |
| 8055 | if (*(args[i]) == 0) { |
| 8056 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8057 | return -1; |
| 8058 | } |
| 8059 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8060 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8061 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 8062 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 8063 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8064 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8065 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 8066 | i++; |
| 8067 | else { |
| 8068 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8069 | return -1; |
| 8070 | } |
| 8071 | } |
| 8072 | 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] | 8073 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8074 | return -1; |
| 8075 | } |
| 8076 | i++; |
| 8077 | } |
| 8078 | return 0; |
| 8079 | } |
| 8080 | |
| 8081 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 8082 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 8083 | struct proxy *defpx, const char *file, int line, |
| 8084 | char **err) { |
| 8085 | int i = 1; |
| 8086 | |
| 8087 | if (*(args[i]) == 0) { |
| 8088 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8089 | return -1; |
| 8090 | } |
| 8091 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8092 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8093 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8094 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8095 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 8096 | i++; |
| 8097 | else { |
| 8098 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8099 | return -1; |
| 8100 | } |
| 8101 | } |
| 8102 | 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] | 8103 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8104 | return -1; |
| 8105 | } |
| 8106 | i++; |
| 8107 | } |
| 8108 | return 0; |
| 8109 | } |
| 8110 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8111 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 8112 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8113 | */ |
| 8114 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 8115 | struct proxy *defpx, const char *file, int line, |
| 8116 | char **err) |
| 8117 | { |
| 8118 | char **target; |
| 8119 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8120 | 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] | 8121 | |
| 8122 | if (too_many_args(1, args, err, NULL)) |
| 8123 | return -1; |
| 8124 | |
| 8125 | if (*target) { |
| 8126 | memprintf(err, "'%s' already specified.", args[0]); |
| 8127 | return -1; |
| 8128 | } |
| 8129 | |
| 8130 | if (*(args[1]) == 0) { |
| 8131 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 8132 | return -1; |
| 8133 | } |
| 8134 | *target = strdup(args[1]); |
| 8135 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8136 | } |
| 8137 | |
| 8138 | /* parse the "ssl-mode-async" keyword in global section. |
| 8139 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8140 | */ |
| 8141 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 8142 | struct proxy *defpx, const char *file, int line, |
| 8143 | char **err) |
| 8144 | { |
Emmanuel Hocdet | e966e4e | 2017-10-24 18:11:48 +0200 | [diff] [blame] | 8145 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8146 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 8147 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8148 | return 0; |
| 8149 | #else |
| 8150 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 8151 | return -1; |
| 8152 | #endif |
| 8153 | } |
| 8154 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8155 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8156 | static int ssl_check_async_engine_count(void) { |
| 8157 | int err_code = 0; |
| 8158 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 8159 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8160 | 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] | 8161 | err_code = ERR_ABORT; |
| 8162 | } |
| 8163 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8164 | } |
| 8165 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8166 | /* parse the "ssl-engine" keyword in global section. |
| 8167 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8168 | */ |
| 8169 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 8170 | struct proxy *defpx, const char *file, int line, |
| 8171 | char **err) |
| 8172 | { |
| 8173 | char *algo; |
| 8174 | int ret = -1; |
| 8175 | |
| 8176 | if (*(args[1]) == 0) { |
| 8177 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 8178 | return ret; |
| 8179 | } |
| 8180 | |
| 8181 | if (*(args[2]) == 0) { |
| 8182 | /* if no list of algorithms is given, it defaults to ALL */ |
| 8183 | algo = strdup("ALL"); |
| 8184 | goto add_engine; |
| 8185 | } |
| 8186 | |
| 8187 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 8188 | if (strcmp(args[2], "algo") != 0) { |
| 8189 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 8190 | return ret; |
| 8191 | } |
| 8192 | |
| 8193 | if (*(args[3]) == 0) { |
| 8194 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 8195 | return ret; |
| 8196 | } |
| 8197 | algo = strdup(args[3]); |
| 8198 | |
| 8199 | add_engine: |
| 8200 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 8201 | openssl_engines_initialized++; |
| 8202 | ret = 0; |
| 8203 | } |
| 8204 | free(algo); |
| 8205 | return ret; |
| 8206 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8207 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8208 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8209 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 8210 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 8211 | */ |
| 8212 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 8213 | struct proxy *defpx, const char *file, int line, |
| 8214 | char **err) |
| 8215 | { |
| 8216 | char **target; |
| 8217 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8218 | 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] | 8219 | |
| 8220 | if (too_many_args(1, args, err, NULL)) |
| 8221 | return -1; |
| 8222 | |
| 8223 | if (*(args[1]) == 0) { |
| 8224 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 8225 | return -1; |
| 8226 | } |
| 8227 | |
| 8228 | free(*target); |
| 8229 | *target = strdup(args[1]); |
| 8230 | return 0; |
| 8231 | } |
| 8232 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8233 | /* parse various global tune.ssl settings consisting in positive integers. |
| 8234 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8235 | */ |
| 8236 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 8237 | struct proxy *defpx, const char *file, int line, |
| 8238 | char **err) |
| 8239 | { |
| 8240 | int *target; |
| 8241 | |
| 8242 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 8243 | target = &global.tune.sslcachesize; |
| 8244 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8245 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8246 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8247 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 8248 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 8249 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8250 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 8251 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8252 | else { |
| 8253 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 8254 | return -1; |
| 8255 | } |
| 8256 | |
| 8257 | if (too_many_args(1, args, err, NULL)) |
| 8258 | return -1; |
| 8259 | |
| 8260 | if (*(args[1]) == 0) { |
| 8261 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 8262 | return -1; |
| 8263 | } |
| 8264 | |
| 8265 | *target = atoi(args[1]); |
| 8266 | if (*target < 0) { |
| 8267 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 8268 | return -1; |
| 8269 | } |
| 8270 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8271 | } |
| 8272 | |
| 8273 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 8274 | struct proxy *defpx, const char *file, int line, |
| 8275 | char **err) |
| 8276 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8277 | int ret; |
| 8278 | |
| 8279 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 8280 | if (ret != 0) |
| 8281 | return ret; |
| 8282 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8283 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8284 | memprintf(err, "'%s' is already configured.", args[0]); |
| 8285 | return -1; |
| 8286 | } |
| 8287 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8288 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 8289 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8290 | memprintf(err, "Out of memory error."); |
| 8291 | return -1; |
| 8292 | } |
| 8293 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8294 | } |
| 8295 | |
| 8296 | /* parse "ssl.force-private-cache". |
| 8297 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8298 | */ |
| 8299 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 8300 | struct proxy *defpx, const char *file, int line, |
| 8301 | char **err) |
| 8302 | { |
| 8303 | if (too_many_args(0, args, err, NULL)) |
| 8304 | return -1; |
| 8305 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8306 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8307 | return 0; |
| 8308 | } |
| 8309 | |
| 8310 | /* parse "ssl.lifetime". |
| 8311 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8312 | */ |
| 8313 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 8314 | struct proxy *defpx, const char *file, int line, |
| 8315 | char **err) |
| 8316 | { |
| 8317 | const char *res; |
| 8318 | |
| 8319 | if (too_many_args(1, args, err, NULL)) |
| 8320 | return -1; |
| 8321 | |
| 8322 | if (*(args[1]) == 0) { |
| 8323 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 8324 | return -1; |
| 8325 | } |
| 8326 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8327 | 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] | 8328 | if (res) { |
| 8329 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 8330 | return -1; |
| 8331 | } |
| 8332 | return 0; |
| 8333 | } |
| 8334 | |
| 8335 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 8336 | /* parse "ssl-dh-param-file". |
| 8337 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8338 | */ |
| 8339 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 8340 | struct proxy *defpx, const char *file, int line, |
| 8341 | char **err) |
| 8342 | { |
| 8343 | if (too_many_args(1, args, err, NULL)) |
| 8344 | return -1; |
| 8345 | |
| 8346 | if (*(args[1]) == 0) { |
| 8347 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 8348 | return -1; |
| 8349 | } |
| 8350 | |
| 8351 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 8352 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 8353 | return -1; |
| 8354 | } |
| 8355 | return 0; |
| 8356 | } |
| 8357 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8358 | /* parse "ssl.default-dh-param". |
| 8359 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8360 | */ |
| 8361 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 8362 | struct proxy *defpx, const char *file, int line, |
| 8363 | char **err) |
| 8364 | { |
| 8365 | if (too_many_args(1, args, err, NULL)) |
| 8366 | return -1; |
| 8367 | |
| 8368 | if (*(args[1]) == 0) { |
| 8369 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 8370 | return -1; |
| 8371 | } |
| 8372 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8373 | global_ssl.default_dh_param = atoi(args[1]); |
| 8374 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8375 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 8376 | return -1; |
| 8377 | } |
| 8378 | return 0; |
| 8379 | } |
| 8380 | #endif |
| 8381 | |
| 8382 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8383 | /* This function is used with TLS ticket keys management. It permits to browse |
| 8384 | * each reference. The variable <getnext> must contain the current node, |
| 8385 | * <end> point to the root node. |
| 8386 | */ |
| 8387 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8388 | static inline |
| 8389 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 8390 | { |
| 8391 | struct tls_keys_ref *ref = getnext; |
| 8392 | |
| 8393 | while (1) { |
| 8394 | |
| 8395 | /* Get next list entry. */ |
| 8396 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 8397 | |
| 8398 | /* If the entry is the last of the list, return NULL. */ |
| 8399 | if (&ref->list == end) |
| 8400 | return NULL; |
| 8401 | |
| 8402 | return ref; |
| 8403 | } |
| 8404 | } |
| 8405 | |
| 8406 | static inline |
| 8407 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 8408 | { |
| 8409 | int id; |
| 8410 | char *error; |
| 8411 | |
| 8412 | /* If the reference starts by a '#', this is numeric id. */ |
| 8413 | if (reference[0] == '#') { |
| 8414 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 8415 | id = strtol(reference + 1, &error, 10); |
| 8416 | if (*error != '\0') |
| 8417 | return NULL; |
| 8418 | |
| 8419 | /* Perform the unique id lookup. */ |
| 8420 | return tlskeys_ref_lookupid(id); |
| 8421 | } |
| 8422 | |
| 8423 | /* Perform the string lookup. */ |
| 8424 | return tlskeys_ref_lookup(reference); |
| 8425 | } |
| 8426 | #endif |
| 8427 | |
| 8428 | |
| 8429 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8430 | |
| 8431 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 8432 | |
| 8433 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 8434 | return cli_io_handler_tlskeys_files(appctx); |
| 8435 | } |
| 8436 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8437 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 8438 | * (next index to be dumped), and cli.p0 (next key reference). |
| 8439 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8440 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 8441 | |
| 8442 | struct stream_interface *si = appctx->owner; |
| 8443 | |
| 8444 | switch (appctx->st2) { |
| 8445 | case STAT_ST_INIT: |
| 8446 | /* Display the column headers. If the message cannot be sent, |
| 8447 | * quit the fucntion with returning 0. The function is called |
| 8448 | * later and restart at the state "STAT_ST_INIT". |
| 8449 | */ |
| 8450 | chunk_reset(&trash); |
| 8451 | |
| 8452 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 8453 | chunk_appendf(&trash, "# id secret\n"); |
| 8454 | else |
| 8455 | chunk_appendf(&trash, "# id (file)\n"); |
| 8456 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8457 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8458 | si_applet_cant_put(si); |
| 8459 | return 0; |
| 8460 | } |
| 8461 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8462 | /* Now, we start the browsing of the references lists. |
| 8463 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 8464 | * available field of this pointer is <list>. It is used with the function |
| 8465 | * tlskeys_list_get_next() for retruning the first available entry |
| 8466 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8467 | if (appctx->ctx.cli.p0 == NULL) { |
| 8468 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 8469 | 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] | 8470 | } |
| 8471 | |
| 8472 | appctx->st2 = STAT_ST_LIST; |
| 8473 | /* fall through */ |
| 8474 | |
| 8475 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8476 | while (appctx->ctx.cli.p0) { |
| 8477 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8478 | |
| 8479 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8480 | 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] | 8481 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8482 | |
| 8483 | if (appctx->ctx.cli.i1 == 0) |
| 8484 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 8485 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8486 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8487 | int head; |
| 8488 | |
| 8489 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 8490 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8491 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8492 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8493 | |
| 8494 | chunk_reset(t2); |
| 8495 | /* should never fail here because we dump only a key in the t2 buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8496 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 8497 | sizeof(struct tls_sess_key), |
| 8498 | t2->area, t2->size); |
| 8499 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 8500 | t2->area); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8501 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8502 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8503 | /* let's try again later from this stream. We add ourselves into |
| 8504 | * this stream's users so that it can remove us upon termination. |
| 8505 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8506 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8507 | si_applet_cant_put(si); |
| 8508 | return 0; |
| 8509 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8510 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8511 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8512 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8513 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8514 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 8515 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8516 | /* let's try again later from this stream. We add ourselves into |
| 8517 | * this stream's users so that it can remove us upon termination. |
| 8518 | */ |
| 8519 | si_applet_cant_put(si); |
| 8520 | return 0; |
| 8521 | } |
| 8522 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8523 | 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] | 8524 | break; |
| 8525 | |
| 8526 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8527 | 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] | 8528 | } |
| 8529 | |
| 8530 | appctx->st2 = STAT_ST_FIN; |
| 8531 | /* fall through */ |
| 8532 | |
| 8533 | default: |
| 8534 | appctx->st2 = STAT_ST_FIN; |
| 8535 | return 1; |
| 8536 | } |
| 8537 | return 0; |
| 8538 | } |
| 8539 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8540 | /* sets cli.i0 to non-zero if only file lists should be dumped */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8541 | static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8542 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8543 | /* no parameter, shows only file list */ |
| 8544 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8545 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8546 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 8547 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8548 | } |
| 8549 | |
| 8550 | if (args[2][0] == '*') { |
| 8551 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8552 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8553 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8554 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
| 8555 | if (!appctx->ctx.cli.p0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8556 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8557 | 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] | 8558 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8559 | return 1; |
| 8560 | } |
| 8561 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8562 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 8563 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8564 | } |
| 8565 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8566 | static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8567 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8568 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 8569 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8570 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8571 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 8572 | if (!*args[3] || !*args[4]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8573 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8574 | 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] | 8575 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8576 | return 1; |
| 8577 | } |
| 8578 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 8579 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 8580 | if (!ref) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8581 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8582 | 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] | 8583 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8584 | return 1; |
| 8585 | } |
| 8586 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 8587 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
| 8588 | if (ret != sizeof(struct tls_sess_key)) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8589 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8590 | 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] | 8591 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8592 | return 1; |
| 8593 | } |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 8594 | trash.data = ret; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8595 | ssl_sock_update_tlskey_ref(ref, &trash); |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8596 | appctx->ctx.cli.severity = LOG_INFO; |
Aurélien Nephtali | 6e8a41d | 2018-03-15 21:48:50 +0100 | [diff] [blame] | 8597 | appctx->ctx.cli.msg = "TLS ticket key updated!\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 8598 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8599 | return 1; |
| 8600 | |
| 8601 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 8602 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8603 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8604 | static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8605 | { |
| 8606 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 8607 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 8608 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 8609 | |
| 8610 | if (!payload) |
| 8611 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8612 | |
| 8613 | /* Expect one parameter: the new response in base64 encoding */ |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 8614 | if (!*payload) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8615 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8616 | 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] | 8617 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8618 | return 1; |
| 8619 | } |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 8620 | |
| 8621 | /* remove \r and \n from the payload */ |
| 8622 | for (i = 0, j = 0; payload[i]; i++) { |
| 8623 | if (payload[i] == '\r' || payload[i] == '\n') |
| 8624 | continue; |
| 8625 | payload[j++] = payload[i]; |
| 8626 | } |
| 8627 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8628 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 8629 | ret = base64dec(payload, j, trash.area, trash.size); |
| 8630 | if (ret < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8631 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8632 | 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] | 8633 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8634 | return 1; |
| 8635 | } |
| 8636 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 8637 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8638 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
| 8639 | if (err) { |
| 8640 | memprintf(&err, "%s.\n", err); |
| 8641 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 8642 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8643 | } |
Aurélien Nephtali | 9a4da68 | 2018-04-16 19:02:42 +0200 | [diff] [blame] | 8644 | else { |
| 8645 | appctx->ctx.cli.severity = LOG_ERR; |
| 8646 | appctx->ctx.cli.msg = "Failed to update OCSP response.\n"; |
| 8647 | appctx->st0 = CLI_ST_PRINT; |
| 8648 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8649 | return 1; |
| 8650 | } |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8651 | appctx->ctx.cli.severity = LOG_INFO; |
Aurélien Nephtali | 6e8a41d | 2018-03-15 21:48:50 +0100 | [diff] [blame] | 8652 | appctx->ctx.cli.msg = "OCSP Response updated!\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 8653 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8654 | return 1; |
| 8655 | #else |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 8656 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8657 | 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] | 8658 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8659 | return 1; |
| 8660 | #endif |
| 8661 | |
| 8662 | } |
| 8663 | |
| 8664 | /* register cli keywords */ |
| 8665 | static struct cli_kw_list cli_kws = {{ },{ |
| 8666 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8667 | { { "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] | 8668 | { { "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] | 8669 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 8670 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8671 | { { NULL }, NULL, NULL, NULL } |
| 8672 | }}; |
| 8673 | |
| 8674 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8675 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8676 | * Please take care of keeping this list alphabetically sorted. |
| 8677 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 8678 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8679 | { "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] | 8680 | { "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] | 8681 | { "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] | 8682 | { "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] | 8683 | { "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] | 8684 | { "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] | 8685 | { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8686 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 8687 | { "ssl_bc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8688 | #endif |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8689 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
| 8690 | { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 8691 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 8692 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 8693 | { "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] | 8694 | { "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] | 8695 | { "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] | 8696 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8697 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8698 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8699 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8700 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8701 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8702 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 8703 | { "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] | 8704 | { "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] | 8705 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 8706 | { "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] | 8707 | { "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] | 8708 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8709 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8710 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8711 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8712 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8713 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8714 | { "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] | 8715 | { "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] | 8716 | { "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] | 8717 | { "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] | 8718 | { "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] | 8719 | { "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] | 8720 | { "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] | 8721 | { "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] | 8722 | { "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] | 8723 | { "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] | 8724 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8725 | { "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] | 8726 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 8727 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8728 | { "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] | 8729 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8730 | { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8731 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 8732 | { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8733 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 8734 | { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8735 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8736 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8737 | #endif |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8738 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
| 8739 | { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 8740 | #endif |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8741 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8742 | { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8743 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8744 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8745 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 8746 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 8747 | { "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] | 8748 | { NULL, NULL, 0, 0, 0 }, |
| 8749 | }}; |
| 8750 | |
| 8751 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8752 | * Please take care of keeping this list alphabetically sorted. |
| 8753 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 8754 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 8755 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 8756 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 8757 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8758 | }}; |
| 8759 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8760 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8761 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 8762 | * all code contributors. |
| 8763 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 8764 | * the config parser can report an appropriate error when a known keyword was |
| 8765 | * not enabled. |
| 8766 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8767 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8768 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8769 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 8770 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 8771 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 8772 | { "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] | 8773 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8774 | { "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] | 8775 | { "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] | 8776 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 8777 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 8778 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8779 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 8780 | { NULL, NULL, 0 }, |
| 8781 | }; |
| 8782 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 8783 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8784 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8785 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 8786 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 8787 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 8788 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 8789 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 8790 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 8791 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 8792 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 8793 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 8794 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 8795 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 8796 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 8797 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 8798 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 8799 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 8800 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 8801 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8802 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 8803 | { "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] | 8804 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 8805 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 8806 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 8807 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 8808 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8809 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 8810 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8811 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 8812 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8813 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 8814 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 8815 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 8816 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 8817 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8818 | { NULL, NULL, 0 }, |
| 8819 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8820 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8821 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8822 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 8823 | * all code contributors. |
| 8824 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 8825 | * the config parser can report an appropriate error when a known keyword was |
| 8826 | * not enabled. |
| 8827 | */ |
| 8828 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 8829 | { "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] | 8830 | { "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] | 8831 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8832 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 8833 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
| 8834 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 8835 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 8836 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 8837 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 8838 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 8839 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 8840 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 8841 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 8842 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 8843 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 8844 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 8845 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 8846 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 8847 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 8848 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 8849 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 8850 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 8851 | { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */ |
| 8852 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 8853 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 8854 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 8855 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 8856 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 8857 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 8858 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 8859 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 8860 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 8861 | { "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] | 8862 | { NULL, NULL, 0, 0 }, |
| 8863 | }}; |
| 8864 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8865 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8866 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 8867 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 8868 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8869 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 8870 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 8871 | #ifndef OPENSSL_NO_DH |
| 8872 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 8873 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8874 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8875 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8876 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8877 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8878 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 8879 | #ifndef OPENSSL_NO_DH |
| 8880 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 8881 | #endif |
| 8882 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 8883 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 8884 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 8885 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8886 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8887 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 8888 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8889 | { 0, NULL, NULL }, |
| 8890 | }}; |
| 8891 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 8892 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 8893 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8894 | .snd_buf = ssl_sock_from_buf, |
| 8895 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 8896 | .subscribe = conn_subscribe, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8897 | .rcv_pipe = NULL, |
| 8898 | .snd_pipe = NULL, |
| 8899 | .shutr = NULL, |
| 8900 | .shutw = ssl_sock_shutw, |
| 8901 | .close = ssl_sock_close, |
| 8902 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 8903 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 8904 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 8905 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 8906 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 8907 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 8908 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8909 | }; |
| 8910 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8911 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 8912 | struct session *sess, struct stream *s, int flags) |
| 8913 | { |
| 8914 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 8915 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8916 | |
| 8917 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 8918 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8919 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 8920 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8921 | 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] | 8922 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8923 | s->req.flags |= CF_READ_NULL; |
| 8924 | return ACT_RET_YIELD; |
| 8925 | } |
| 8926 | } |
| 8927 | return (ACT_RET_CONT); |
| 8928 | } |
| 8929 | |
| 8930 | 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) |
| 8931 | { |
| 8932 | rule->action_ptr = ssl_action_wait_for_hs; |
| 8933 | |
| 8934 | return ACT_RET_PRS_OK; |
| 8935 | } |
| 8936 | |
| 8937 | static struct action_kw_list http_req_actions = {ILH, { |
| 8938 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 8939 | { /* END */ } |
| 8940 | }}; |
| 8941 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 8942 | #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] | 8943 | |
| 8944 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8945 | { |
| 8946 | if (ptr) { |
| 8947 | chunk_destroy(ptr); |
| 8948 | free(ptr); |
| 8949 | } |
| 8950 | } |
| 8951 | |
| 8952 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8953 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8954 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8955 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8956 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 8957 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8958 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8959 | static void __ssl_sock_init(void) |
| 8960 | { |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8961 | char *ptr; |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 8962 | int i; |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8963 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8964 | STACK_OF(SSL_COMP)* cm; |
| 8965 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8966 | if (global_ssl.listen_default_ciphers) |
| 8967 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 8968 | if (global_ssl.connect_default_ciphers) |
| 8969 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 8970 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 8971 | xprt_register(XPRT_SSL, &ssl_sock); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8972 | SSL_library_init(); |
| 8973 | cm = SSL_COMP_get_compression_methods(); |
| 8974 | sk_SSL_COMP_zero(cm); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 8975 | #ifdef USE_THREAD |
| 8976 | ssl_locking_init(); |
| 8977 | #endif |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 8978 | #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] | 8979 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 8980 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 8981 | ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
Thierry FOURNIER | 16ff050 | 2018-06-17 21:33:01 +0200 | [diff] [blame] | 8982 | ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 8983 | 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] | 8984 | sample_register_fetches(&sample_fetch_keywords); |
| 8985 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8986 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8987 | srv_register_keywords(&srv_kws); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8988 | cfg_register_keywords(&cfg_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8989 | cli_register_kw(&cli_kws); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8990 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8991 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8992 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8993 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 8994 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8995 | hap_register_post_check(tlskeys_finalize_config); |
| 8996 | #endif |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 8997 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8998 | ptr = NULL; |
| 8999 | memprintf(&ptr, "Built with OpenSSL version : " |
| 9000 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 9001 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9002 | #else /* OPENSSL_IS_BORINGSSL */ |
| 9003 | OPENSSL_VERSION_TEXT |
| 9004 | "\nRunning on OpenSSL version : %s%s", |
| 9005 | SSLeay_version(SSLEAY_VERSION), |
| 9006 | ((OPENSSL_VERSION_NUMBER ^ SSLeay()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
| 9007 | #endif |
| 9008 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
| 9009 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 9010 | "no (library version too old)" |
| 9011 | #elif defined(OPENSSL_NO_TLSEXT) |
| 9012 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 9013 | #else |
| 9014 | "yes" |
| 9015 | #endif |
| 9016 | "", ptr); |
| 9017 | |
| 9018 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 9019 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 9020 | "yes" |
| 9021 | #else |
| 9022 | #ifdef OPENSSL_NO_TLSEXT |
| 9023 | "no (because of OPENSSL_NO_TLSEXT)" |
| 9024 | #else |
| 9025 | "no (version might be too old, 0.9.8f min needed)" |
| 9026 | #endif |
| 9027 | #endif |
| 9028 | "", ptr); |
| 9029 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 9030 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 9031 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 9032 | if (methodVersions[i].option) |
| 9033 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 9034 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9035 | hap_register_build_opts(ptr, 1); |
| 9036 | |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 9037 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 9038 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 9039 | |
| 9040 | #ifndef OPENSSL_NO_DH |
| 9041 | 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] | 9042 | hap_register_post_deinit(ssl_free_dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 9043 | #endif |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9044 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9045 | hap_register_post_deinit(ssl_free_engines); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9046 | #endif |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 9047 | /* Load SSL string for the verbose & debug mode. */ |
| 9048 | ERR_load_SSL_strings(); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9049 | |
| 9050 | http_req_keywords_register(&http_req_actions); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9051 | } |
| 9052 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9053 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9054 | void ssl_free_engines(void) { |
| 9055 | struct ssl_engine_list *wl, *wlb; |
| 9056 | /* free up engine list */ |
| 9057 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 9058 | ENGINE_finish(wl->e); |
| 9059 | ENGINE_free(wl->e); |
| 9060 | LIST_DEL(&wl->list); |
| 9061 | free(wl); |
| 9062 | } |
| 9063 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9064 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9065 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9066 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9067 | void ssl_free_dh(void) { |
| 9068 | if (local_dh_1024) { |
| 9069 | DH_free(local_dh_1024); |
| 9070 | local_dh_1024 = NULL; |
| 9071 | } |
| 9072 | if (local_dh_2048) { |
| 9073 | DH_free(local_dh_2048); |
| 9074 | local_dh_2048 = NULL; |
| 9075 | } |
| 9076 | if (local_dh_4096) { |
| 9077 | DH_free(local_dh_4096); |
| 9078 | local_dh_4096 = NULL; |
| 9079 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 9080 | if (global_dh) { |
| 9081 | DH_free(global_dh); |
| 9082 | global_dh = NULL; |
| 9083 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9084 | } |
| 9085 | #endif |
| 9086 | |
| 9087 | __attribute__((destructor)) |
| 9088 | static void __ssl_sock_deinit(void) |
| 9089 | { |
| 9090 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9091 | if (ssl_ctx_lru_tree) { |
| 9092 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 9093 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9094 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9095 | #endif |
| 9096 | |
| 9097 | ERR_remove_state(0); |
| 9098 | ERR_free_strings(); |
| 9099 | |
| 9100 | EVP_cleanup(); |
| 9101 | |
| 9102 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 9103 | CRYPTO_cleanup_all_ex_data(); |
| 9104 | #endif |
| 9105 | } |
| 9106 | |
| 9107 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9108 | /* |
| 9109 | * Local variables: |
| 9110 | * c-indent-level: 8 |
| 9111 | * c-basic-offset: 8 |
| 9112 | * End: |
| 9113 | */ |