yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 1 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2 | /* |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3 | * SSL/TLS transport layer over SOCK_STREAM sockets |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
Willy Tarreau | 69845df | 2012-09-10 09:43:09 +0200 | [diff] [blame] | 12 | * Acknowledgement: |
| 13 | * We'd like to specially thank the Stud project authors for a very clean |
| 14 | * and well documented code which helped us understand how the OpenSSL API |
| 15 | * ought to be used in non-blocking mode. This is one difficult part which |
| 16 | * is not easy to get from the OpenSSL doc, and reading the Stud code made |
| 17 | * it much more obvious than the examples in the OpenSSL package. Keep up |
| 18 | * the good works, guys ! |
| 19 | * |
| 20 | * Stud is an extremely efficient and scalable SSL/TLS proxy which combines |
| 21 | * particularly well with haproxy. For more info about this project, visit : |
| 22 | * https://github.com/bumptech/stud |
| 23 | * |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #define _GNU_SOURCE |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 27 | #include <ctype.h> |
| 28 | #include <dirent.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <unistd.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 35 | |
| 36 | #include <sys/socket.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 39 | #include <netdb.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 40 | #include <netinet/tcp.h> |
| 41 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 42 | #include <openssl/crypto.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 43 | #include <openssl/ssl.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 44 | #include <openssl/x509.h> |
| 45 | #include <openssl/x509v3.h> |
| 46 | #include <openssl/x509.h> |
| 47 | #include <openssl/err.h> |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 48 | #include <openssl/rand.h> |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 49 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 50 | #include <openssl/ocsp.h> |
| 51 | #endif |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 52 | #ifndef OPENSSL_NO_DH |
| 53 | #include <openssl/dh.h> |
| 54 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 55 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 56 | #include <import/lru.h> |
| 57 | #include <import/xxhash.h> |
| 58 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 59 | #include <common/buffer.h> |
| 60 | #include <common/compat.h> |
| 61 | #include <common/config.h> |
| 62 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 63 | #include <common/errors.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 64 | #include <common/standard.h> |
| 65 | #include <common/ticks.h> |
| 66 | #include <common/time.h> |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 67 | #include <common/cfgparse.h> |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 68 | #include <common/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 69 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 70 | #include <ebsttree.h> |
| 71 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 72 | #include <types/applet.h> |
| 73 | #include <types/cli.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 74 | #include <types/global.h> |
| 75 | #include <types/ssl_sock.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 76 | #include <types/stats.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 77 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 78 | #include <proto/acl.h> |
| 79 | #include <proto/arg.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 80 | #include <proto/channel.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 81 | #include <proto/connection.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 82 | #include <proto/cli.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 83 | #include <proto/fd.h> |
| 84 | #include <proto/freq_ctr.h> |
| 85 | #include <proto/frontend.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 86 | #include <proto/listener.h> |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 87 | #include <proto/openssl-compat.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 88 | #include <proto/pattern.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 89 | #include <proto/proto_tcp.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 90 | #include <proto/server.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 91 | #include <proto/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 92 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 93 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 94 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 95 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 96 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 97 | #include <proto/task.h> |
| 98 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 99 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 100 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 101 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 102 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 103 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 104 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 105 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 106 | |
| 107 | /* Verify errors macros */ |
| 108 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 109 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 110 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 111 | |
| 112 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 113 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 114 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 115 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 116 | /* Supported hash function for TLS tickets */ |
| 117 | #ifdef OPENSSL_NO_SHA256 |
| 118 | #define HASH_FUNCT EVP_sha1 |
| 119 | #else |
| 120 | #define HASH_FUNCT EVP_sha256 |
| 121 | #endif /* OPENSSL_NO_SHA256 */ |
| 122 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 123 | /* server and bind verify method, it uses a global value as default */ |
| 124 | enum { |
| 125 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 126 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 127 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 128 | SSL_SOCK_VERIFY_NONE = 3, |
| 129 | }; |
| 130 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 131 | int sslconns = 0; |
| 132 | int totalsslconns = 0; |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 133 | static struct xprt_ops ssl_sock; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 134 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 135 | static struct { |
| 136 | char *crt_base; /* base directory path for certificates */ |
| 137 | char *ca_base; /* base directory path for CAs and CRLs */ |
| 138 | |
| 139 | char *listen_default_ciphers; |
| 140 | char *connect_default_ciphers; |
| 141 | int listen_default_ssloptions; |
| 142 | int connect_default_ssloptions; |
| 143 | |
| 144 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 145 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 146 | unsigned int max_record; /* SSL max record size */ |
| 147 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 148 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 149 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 150 | } global_ssl = { |
| 151 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 152 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 153 | #endif |
| 154 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 155 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 156 | #endif |
| 157 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 158 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 159 | |
| 160 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 161 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 162 | #endif |
| 163 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 164 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 165 | .capture_cipherlist = 0, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 166 | }; |
| 167 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 168 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 169 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 170 | unsigned long long int xxh64; |
| 171 | unsigned char ciphersuite_len; |
| 172 | char ciphersuite[0]; |
| 173 | }; |
| 174 | struct pool_head *pool2_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 175 | static int ssl_capture_ptr_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 176 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 177 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 178 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 179 | #endif |
| 180 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 181 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 182 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 183 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 184 | static DH *local_dh_1024 = NULL; |
| 185 | static DH *local_dh_2048 = NULL; |
| 186 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 187 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 188 | #endif /* OPENSSL_NO_DH */ |
| 189 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 190 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 191 | /* X509V3 Extensions that will be added on generated certificates */ |
| 192 | #define X509V3_EXT_SIZE 5 |
| 193 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 194 | "basicConstraints", |
| 195 | "nsComment", |
| 196 | "subjectKeyIdentifier", |
| 197 | "authorityKeyIdentifier", |
| 198 | "keyUsage", |
| 199 | }; |
| 200 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 201 | "CA:FALSE", |
| 202 | "\"OpenSSL Generated Certificate\"", |
| 203 | "hash", |
| 204 | "keyid,issuer:always", |
| 205 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 206 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 207 | /* LRU cache to store generated certificate */ |
| 208 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 209 | static unsigned int ssl_ctx_lru_seed = 0; |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 210 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 211 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 212 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 213 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 214 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 215 | /* The order here matters for picking a default context, |
| 216 | * keep the most common keytype at the bottom of the list |
| 217 | */ |
| 218 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 219 | "dsa", |
| 220 | "ecdsa", |
| 221 | "rsa" |
| 222 | }; |
| 223 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 224 | #else |
| 225 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 226 | #endif |
| 227 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 228 | /* |
| 229 | * This function gives the detail of the SSL error. It is used only |
| 230 | * if the debug mode and the verbose mode are activated. It dump all |
| 231 | * the SSL error until the stack was empty. |
| 232 | */ |
| 233 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 234 | { |
| 235 | unsigned long ret; |
| 236 | |
| 237 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 238 | while(1) { |
| 239 | ret = ERR_get_error(); |
| 240 | if (ret == 0) |
| 241 | return; |
| 242 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
| 243 | (unsigned short)conn->t.sock.fd, ret, |
| 244 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 249 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 250 | /* |
| 251 | * struct alignment works here such that the key.key is the same as key_data |
| 252 | * Do not change the placement of key_data |
| 253 | */ |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 254 | struct certificate_ocsp { |
| 255 | struct ebmb_node key; |
| 256 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 257 | struct chunk response; |
| 258 | long expire; |
| 259 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 260 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 261 | struct ocsp_cbk_arg { |
| 262 | int is_single; |
| 263 | int single_kt; |
| 264 | union { |
| 265 | struct certificate_ocsp *s_ocsp; |
| 266 | /* |
| 267 | * m_ocsp will have multiple entries dependent on key type |
| 268 | * Entry 0 - DSA |
| 269 | * Entry 1 - ECDSA |
| 270 | * Entry 2 - RSA |
| 271 | */ |
| 272 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 273 | }; |
| 274 | }; |
| 275 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 276 | /* |
| 277 | * This function returns the number of seconds elapsed |
| 278 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 279 | * date presented un ASN1_GENERALIZEDTIME. |
| 280 | * |
| 281 | * In parsing error case, it returns -1. |
| 282 | */ |
| 283 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 284 | { |
| 285 | long epoch; |
| 286 | char *p, *end; |
| 287 | const unsigned short month_offset[12] = { |
| 288 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 289 | }; |
| 290 | int year, month; |
| 291 | |
| 292 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 293 | |
| 294 | p = (char *)d->data; |
| 295 | end = p + d->length; |
| 296 | |
| 297 | if (end - p < 4) return -1; |
| 298 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 299 | p += 4; |
| 300 | if (end - p < 2) return -1; |
| 301 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 302 | if (month < 1 || month > 12) return -1; |
| 303 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 304 | We consider leap years and the current month (<marsh or not) */ |
| 305 | epoch = ( ((year - 1970) * 365) |
| 306 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 307 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 308 | + month_offset[month-1] |
| 309 | ) * 24 * 60 * 60; |
| 310 | p += 2; |
| 311 | if (end - p < 2) return -1; |
| 312 | /* Add the number of seconds of completed days of current month */ |
| 313 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 314 | p += 2; |
| 315 | if (end - p < 2) return -1; |
| 316 | /* Add the completed hours of the current day */ |
| 317 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 318 | p += 2; |
| 319 | if (end - p < 2) return -1; |
| 320 | /* Add the completed minutes of the current hour */ |
| 321 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 322 | p += 2; |
| 323 | if (p == end) return -1; |
| 324 | /* Test if there is available seconds */ |
| 325 | if (p[0] < '0' || p[0] > '9') |
| 326 | goto nosec; |
| 327 | if (end - p < 2) return -1; |
| 328 | /* Add the seconds of the current minute */ |
| 329 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 330 | p += 2; |
| 331 | if (p == end) return -1; |
| 332 | /* Ignore seconds float part if present */ |
| 333 | if (p[0] == '.') { |
| 334 | do { |
| 335 | if (++p == end) return -1; |
| 336 | } while (p[0] >= '0' && p[0] <= '9'); |
| 337 | } |
| 338 | |
| 339 | nosec: |
| 340 | if (p[0] == 'Z') { |
| 341 | if (end - p != 1) return -1; |
| 342 | return epoch; |
| 343 | } |
| 344 | else if (p[0] == '+') { |
| 345 | if (end - p != 5) return -1; |
| 346 | /* Apply timezone offset */ |
| 347 | return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 348 | } |
| 349 | else if (p[0] == '-') { |
| 350 | if (end - p != 5) return -1; |
| 351 | /* Apply timezone offset */ |
| 352 | return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 353 | } |
| 354 | |
| 355 | return -1; |
| 356 | } |
| 357 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 358 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 359 | |
| 360 | /* This function starts to check if the OCSP response (in DER format) contained |
| 361 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 362 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 363 | * contained in the OCSP Response and exits on error if no match. |
| 364 | * If it's a valid OCSP Response: |
| 365 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 366 | * pointed by 'ocsp'. |
| 367 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 368 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 369 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 370 | * already present in the container, it will be overwritten. |
| 371 | * |
| 372 | * Note: OCSP response containing more than one OCSP Single response is not |
| 373 | * considered valid. |
| 374 | * |
| 375 | * Returns 0 on success, 1 in error case. |
| 376 | */ |
| 377 | static int ssl_sock_load_ocsp_response(struct chunk *ocsp_response, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 378 | { |
| 379 | OCSP_RESPONSE *resp; |
| 380 | OCSP_BASICRESP *bs = NULL; |
| 381 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 382 | OCSP_CERTID *id; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 383 | unsigned char *p = (unsigned char *)ocsp_response->str; |
| 384 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 385 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 386 | int reason; |
| 387 | int ret = 1; |
| 388 | |
| 389 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, ocsp_response->len); |
| 390 | if (!resp) { |
| 391 | memprintf(err, "Unable to parse OCSP response"); |
| 392 | goto out; |
| 393 | } |
| 394 | |
| 395 | rc = OCSP_response_status(resp); |
| 396 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 397 | memprintf(err, "OCSP response status not successful"); |
| 398 | goto out; |
| 399 | } |
| 400 | |
| 401 | bs = OCSP_response_get1_basic(resp); |
| 402 | if (!bs) { |
| 403 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 404 | goto out; |
| 405 | } |
| 406 | |
| 407 | count_sr = OCSP_resp_count(bs); |
| 408 | if (count_sr > 1) { |
| 409 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 410 | goto out; |
| 411 | } |
| 412 | |
| 413 | sr = OCSP_resp_get0(bs, 0); |
| 414 | if (!sr) { |
| 415 | memprintf(err, "Failed to get OCSP single response"); |
| 416 | goto out; |
| 417 | } |
| 418 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 419 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 420 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 421 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
| 422 | if (rc != V_OCSP_CERTSTATUS_GOOD) { |
| 423 | memprintf(err, "OCSP single response: certificate status not good"); |
| 424 | goto out; |
| 425 | } |
| 426 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 427 | if (!nextupd) { |
| 428 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 429 | goto out; |
| 430 | } |
| 431 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 432 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 433 | if (!rc) { |
| 434 | memprintf(err, "OCSP single response: no longer valid."); |
| 435 | goto out; |
| 436 | } |
| 437 | |
| 438 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 439 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 440 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 441 | goto out; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if (!ocsp) { |
| 446 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 447 | unsigned char *p; |
| 448 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 449 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 450 | if (!rc) { |
| 451 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 452 | goto out; |
| 453 | } |
| 454 | |
| 455 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 456 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 457 | goto out; |
| 458 | } |
| 459 | |
| 460 | p = key; |
| 461 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 462 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 463 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 464 | if (!ocsp) { |
| 465 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 466 | goto out; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /* According to comments on "chunk_dup", the |
| 471 | previous chunk buffer will be freed */ |
| 472 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 473 | memprintf(err, "OCSP response: Memory allocation error"); |
| 474 | goto out; |
| 475 | } |
| 476 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 477 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 478 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 479 | ret = 0; |
| 480 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 481 | ERR_clear_error(); |
| 482 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 483 | if (bs) |
| 484 | OCSP_BASICRESP_free(bs); |
| 485 | |
| 486 | if (resp) |
| 487 | OCSP_RESPONSE_free(resp); |
| 488 | |
| 489 | return ret; |
| 490 | } |
| 491 | /* |
| 492 | * External function use to update the OCSP response in the OCSP response's |
| 493 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 494 | * to update in DER format. |
| 495 | * |
| 496 | * Returns 0 on success, 1 in error case. |
| 497 | */ |
| 498 | int ssl_sock_update_ocsp_response(struct chunk *ocsp_response, char **err) |
| 499 | { |
| 500 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * This function load the OCSP Resonse in DER format contained in file at |
| 505 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 506 | * |
| 507 | * Returns 0 on success, 1 in error case. |
| 508 | */ |
| 509 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 510 | { |
| 511 | int fd = -1; |
| 512 | int r = 0; |
| 513 | int ret = 1; |
| 514 | |
| 515 | fd = open(ocsp_path, O_RDONLY); |
| 516 | if (fd == -1) { |
| 517 | memprintf(err, "Error opening OCSP response file"); |
| 518 | goto end; |
| 519 | } |
| 520 | |
| 521 | trash.len = 0; |
| 522 | while (trash.len < trash.size) { |
| 523 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 524 | if (r < 0) { |
| 525 | if (errno == EINTR) |
| 526 | continue; |
| 527 | |
| 528 | memprintf(err, "Error reading OCSP response from file"); |
| 529 | goto end; |
| 530 | } |
| 531 | else if (r == 0) { |
| 532 | break; |
| 533 | } |
| 534 | trash.len += r; |
| 535 | } |
| 536 | |
| 537 | close(fd); |
| 538 | fd = -1; |
| 539 | |
| 540 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 541 | end: |
| 542 | if (fd != -1) |
| 543 | close(fd); |
| 544 | |
| 545 | return ret; |
| 546 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 547 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 548 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 549 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 550 | 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) |
| 551 | { |
| 552 | struct tls_sess_key *keys; |
| 553 | struct connection *conn; |
| 554 | int head; |
| 555 | int i; |
| 556 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 557 | conn = SSL_get_app_data(s); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 558 | keys = objt_listener(conn->target)->bind_conf->keys_ref->tlskeys; |
| 559 | head = objt_listener(conn->target)->bind_conf->keys_ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 560 | |
| 561 | if (enc) { |
| 562 | memcpy(key_name, keys[head].name, 16); |
| 563 | |
| 564 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
| 565 | return -1; |
| 566 | |
| 567 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].aes_key, iv)) |
| 568 | return -1; |
| 569 | |
| 570 | HMAC_Init_ex(hctx, keys[head].hmac_key, 16, HASH_FUNCT(), NULL); |
| 571 | |
| 572 | return 1; |
| 573 | } else { |
| 574 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 575 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 576 | goto found; |
| 577 | } |
| 578 | return 0; |
| 579 | |
| 580 | found: |
| 581 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].hmac_key, 16, HASH_FUNCT(), NULL); |
| 582 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].aes_key, iv)) |
| 583 | return -1; |
| 584 | /* 2 for key renewal, 1 if current key is still valid */ |
| 585 | return i ? 2 : 1; |
| 586 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 590 | { |
| 591 | struct tls_keys_ref *ref; |
| 592 | |
| 593 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 594 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 595 | return ref; |
| 596 | return NULL; |
| 597 | } |
| 598 | |
| 599 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 600 | { |
| 601 | struct tls_keys_ref *ref; |
| 602 | |
| 603 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 604 | if (ref->unique_id == unique_id) |
| 605 | return ref; |
| 606 | return NULL; |
| 607 | } |
| 608 | |
| 609 | int ssl_sock_update_tlskey(char *filename, struct chunk *tlskey, char **err) { |
| 610 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 611 | |
| 612 | if(!ref) { |
| 613 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 614 | return 1; |
| 615 | } |
| 616 | |
Pradeep Jindal | cc79b00 | 2015-08-20 18:25:17 +0530 | [diff] [blame] | 617 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), tlskey->str, tlskey->len); |
| 618 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 619 | |
| 620 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 621 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 622 | |
| 623 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 624 | * automatic ids. It's called just after the basic checks. It returns |
| 625 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 626 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 627 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 628 | { |
| 629 | int i = 0; |
| 630 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 631 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 632 | |
| 633 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 634 | if (ref->unique_id == -1) { |
| 635 | /* Look for the first free id. */ |
| 636 | while (1) { |
| 637 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 638 | if (ref2->unique_id == i) { |
| 639 | i++; |
| 640 | break; |
| 641 | } |
| 642 | } |
| 643 | if (&ref2->list == &tlskeys_reference) |
| 644 | break; |
| 645 | } |
| 646 | |
| 647 | /* Uses the unique id and increment it for the next entry. */ |
| 648 | ref->unique_id = i; |
| 649 | i++; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /* This sort the reference list by id. */ |
| 654 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 655 | LIST_DEL(&ref->list); |
| 656 | list_for_each_entry(ref3, &tkr, list) { |
| 657 | if (ref->unique_id < ref3->unique_id) { |
| 658 | LIST_ADDQ(&ref3->list, &ref->list); |
| 659 | break; |
| 660 | } |
| 661 | } |
| 662 | if (&ref3->list == &tkr) |
| 663 | LIST_ADDQ(&tkr, &ref->list); |
| 664 | } |
| 665 | |
| 666 | /* swap root */ |
| 667 | LIST_ADD(&tkr, &tlskeys_reference); |
| 668 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 669 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 670 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 671 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 672 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 673 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 674 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 675 | { |
| 676 | switch (evp_keytype) { |
| 677 | case EVP_PKEY_RSA: |
| 678 | return 2; |
| 679 | case EVP_PKEY_DSA: |
| 680 | return 0; |
| 681 | case EVP_PKEY_EC: |
| 682 | return 1; |
| 683 | } |
| 684 | |
| 685 | return -1; |
| 686 | } |
| 687 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 688 | /* |
| 689 | * Callback used to set OCSP status extension content in server hello. |
| 690 | */ |
| 691 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 692 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 693 | struct certificate_ocsp *ocsp; |
| 694 | struct ocsp_cbk_arg *ocsp_arg; |
| 695 | char *ssl_buf; |
| 696 | EVP_PKEY *ssl_pkey; |
| 697 | int key_type; |
| 698 | int index; |
| 699 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 700 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 701 | |
| 702 | ssl_pkey = SSL_get_privatekey(ssl); |
| 703 | if (!ssl_pkey) |
| 704 | return SSL_TLSEXT_ERR_NOACK; |
| 705 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 706 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 707 | |
| 708 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 709 | ocsp = ocsp_arg->s_ocsp; |
| 710 | else { |
| 711 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 712 | * the certificate type |
| 713 | */ |
| 714 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 715 | |
| 716 | if (index < 0) |
| 717 | return SSL_TLSEXT_ERR_NOACK; |
| 718 | |
| 719 | ocsp = ocsp_arg->m_ocsp[index]; |
| 720 | |
| 721 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 722 | |
| 723 | if (!ocsp || |
| 724 | !ocsp->response.str || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 725 | !ocsp->response.len || |
| 726 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 727 | return SSL_TLSEXT_ERR_NOACK; |
| 728 | |
| 729 | ssl_buf = OPENSSL_malloc(ocsp->response.len); |
| 730 | if (!ssl_buf) |
| 731 | return SSL_TLSEXT_ERR_NOACK; |
| 732 | |
| 733 | memcpy(ssl_buf, ocsp->response.str, ocsp->response.len); |
| 734 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.len); |
| 735 | |
| 736 | return SSL_TLSEXT_ERR_OK; |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 741 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 742 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 743 | * It should be present in the certificate's extra chain builded from file |
| 744 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 745 | * named 'cert_path' suffixed using '.issuer'. |
| 746 | * |
| 747 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 748 | * response. If file is empty or content is not a valid OCSP response, |
| 749 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 750 | * is displayed). |
| 751 | * |
| 752 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
| 753 | * succesfully enabled, or -1 in other error case. |
| 754 | */ |
| 755 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 756 | { |
| 757 | |
| 758 | BIO *in = NULL; |
| 759 | X509 *x, *xi = NULL, *issuer = NULL; |
| 760 | STACK_OF(X509) *chain = NULL; |
| 761 | OCSP_CERTID *cid = NULL; |
| 762 | SSL *ssl; |
| 763 | char ocsp_path[MAXPATHLEN+1]; |
| 764 | int i, ret = -1; |
| 765 | struct stat st; |
| 766 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 767 | char *warn = NULL; |
| 768 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 769 | pem_password_cb *passwd_cb; |
| 770 | void *passwd_cb_userdata; |
| 771 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 772 | |
| 773 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 774 | |
| 775 | if (stat(ocsp_path, &st)) |
| 776 | return 1; |
| 777 | |
| 778 | ssl = SSL_new(ctx); |
| 779 | if (!ssl) |
| 780 | goto out; |
| 781 | |
| 782 | x = SSL_get_certificate(ssl); |
| 783 | if (!x) |
| 784 | goto out; |
| 785 | |
| 786 | /* Try to lookup for issuer in certificate extra chain */ |
| 787 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 788 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 789 | #else |
| 790 | chain = ctx->extra_certs; |
| 791 | #endif |
| 792 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 793 | issuer = sk_X509_value(chain, i); |
| 794 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 795 | break; |
| 796 | else |
| 797 | issuer = NULL; |
| 798 | } |
| 799 | |
| 800 | /* If not found try to load issuer from a suffixed file */ |
| 801 | if (!issuer) { |
| 802 | char issuer_path[MAXPATHLEN+1]; |
| 803 | |
| 804 | in = BIO_new(BIO_s_file()); |
| 805 | if (!in) |
| 806 | goto out; |
| 807 | |
| 808 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 809 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 810 | goto out; |
| 811 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 812 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 813 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 814 | |
| 815 | 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] | 816 | if (!xi) |
| 817 | goto out; |
| 818 | |
| 819 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 820 | goto out; |
| 821 | |
| 822 | issuer = xi; |
| 823 | } |
| 824 | |
| 825 | cid = OCSP_cert_to_id(0, x, issuer); |
| 826 | if (!cid) |
| 827 | goto out; |
| 828 | |
| 829 | i = i2d_OCSP_CERTID(cid, NULL); |
| 830 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 831 | goto out; |
| 832 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 833 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 834 | if (!ocsp) |
| 835 | goto out; |
| 836 | |
| 837 | p = ocsp->key_data; |
| 838 | i2d_OCSP_CERTID(cid, &p); |
| 839 | |
| 840 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 841 | if (iocsp == ocsp) |
| 842 | ocsp = NULL; |
| 843 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 844 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 845 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 846 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 847 | #endif |
| 848 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 849 | |
| 850 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 851 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 852 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 853 | |
| 854 | cb_arg->is_single = 1; |
| 855 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 856 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 857 | pkey = X509_get_pubkey(x); |
| 858 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 859 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 860 | |
| 861 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 862 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 863 | } else { |
| 864 | /* |
| 865 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 866 | * Update that cb_arg with the new cert's staple |
| 867 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 868 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 869 | struct certificate_ocsp *tmp_ocsp; |
| 870 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 871 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 872 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 873 | |
| 874 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 875 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 876 | #else |
| 877 | cb_arg = ctx->tlsext_status_arg; |
| 878 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 879 | |
| 880 | /* |
| 881 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 882 | * the order of operations below matter, take care when changing it |
| 883 | */ |
| 884 | tmp_ocsp = cb_arg->s_ocsp; |
| 885 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 886 | cb_arg->s_ocsp = NULL; |
| 887 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 888 | cb_arg->is_single = 0; |
| 889 | cb_arg->single_kt = 0; |
| 890 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 891 | pkey = X509_get_pubkey(x); |
| 892 | key_type = EVP_PKEY_base_id(pkey); |
| 893 | EVP_PKEY_free(pkey); |
| 894 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 895 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 896 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 897 | cb_arg->m_ocsp[index] = iocsp; |
| 898 | |
| 899 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 900 | |
| 901 | ret = 0; |
| 902 | |
| 903 | warn = NULL; |
| 904 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 905 | memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure"); |
| 906 | Warning("%s.\n", warn); |
| 907 | } |
| 908 | |
| 909 | out: |
| 910 | if (ssl) |
| 911 | SSL_free(ssl); |
| 912 | |
| 913 | if (in) |
| 914 | BIO_free(in); |
| 915 | |
| 916 | if (xi) |
| 917 | X509_free(xi); |
| 918 | |
| 919 | if (cid) |
| 920 | OCSP_CERTID_free(cid); |
| 921 | |
| 922 | if (ocsp) |
| 923 | free(ocsp); |
| 924 | |
| 925 | if (warn) |
| 926 | free(warn); |
| 927 | |
| 928 | |
| 929 | return ret; |
| 930 | } |
| 931 | |
| 932 | #endif |
| 933 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 934 | #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] | 935 | |
| 936 | #define CT_EXTENSION_TYPE 18 |
| 937 | |
| 938 | static int sctl_ex_index = -1; |
| 939 | |
| 940 | /* |
| 941 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 942 | * makes only basic test if the data seems like SCTL. No signature validation |
| 943 | * is performed. |
| 944 | */ |
| 945 | static int ssl_sock_parse_sctl(struct chunk *sctl) |
| 946 | { |
| 947 | int ret = 1; |
| 948 | int len, pos, sct_len; |
| 949 | unsigned char *data; |
| 950 | |
| 951 | if (sctl->len < 2) |
| 952 | goto out; |
| 953 | |
| 954 | data = (unsigned char *)sctl->str; |
| 955 | len = (data[0] << 8) | data[1]; |
| 956 | |
| 957 | if (len + 2 != sctl->len) |
| 958 | goto out; |
| 959 | |
| 960 | data = data + 2; |
| 961 | pos = 0; |
| 962 | while (pos < len) { |
| 963 | if (len - pos < 2) |
| 964 | goto out; |
| 965 | |
| 966 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 967 | if (pos + sct_len + 2 > len) |
| 968 | goto out; |
| 969 | |
| 970 | pos += sct_len + 2; |
| 971 | } |
| 972 | |
| 973 | ret = 0; |
| 974 | |
| 975 | out: |
| 976 | return ret; |
| 977 | } |
| 978 | |
| 979 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, struct chunk **sctl) |
| 980 | { |
| 981 | int fd = -1; |
| 982 | int r = 0; |
| 983 | int ret = 1; |
| 984 | |
| 985 | *sctl = NULL; |
| 986 | |
| 987 | fd = open(sctl_path, O_RDONLY); |
| 988 | if (fd == -1) |
| 989 | goto end; |
| 990 | |
| 991 | trash.len = 0; |
| 992 | while (trash.len < trash.size) { |
| 993 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 994 | if (r < 0) { |
| 995 | if (errno == EINTR) |
| 996 | continue; |
| 997 | |
| 998 | goto end; |
| 999 | } |
| 1000 | else if (r == 0) { |
| 1001 | break; |
| 1002 | } |
| 1003 | trash.len += r; |
| 1004 | } |
| 1005 | |
| 1006 | ret = ssl_sock_parse_sctl(&trash); |
| 1007 | if (ret) |
| 1008 | goto end; |
| 1009 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1010 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1011 | if (!chunk_dup(*sctl, &trash)) { |
| 1012 | free(*sctl); |
| 1013 | *sctl = NULL; |
| 1014 | goto end; |
| 1015 | } |
| 1016 | |
| 1017 | end: |
| 1018 | if (fd != -1) |
| 1019 | close(fd); |
| 1020 | |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
| 1024 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1025 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1026 | struct chunk *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1027 | |
| 1028 | *out = (unsigned char *)sctl->str; |
| 1029 | *outlen = sctl->len; |
| 1030 | |
| 1031 | return 1; |
| 1032 | } |
| 1033 | |
| 1034 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1035 | { |
| 1036 | return 1; |
| 1037 | } |
| 1038 | |
| 1039 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 1040 | { |
| 1041 | char sctl_path[MAXPATHLEN+1]; |
| 1042 | int ret = -1; |
| 1043 | struct stat st; |
| 1044 | struct chunk *sctl = NULL; |
| 1045 | |
| 1046 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 1047 | |
| 1048 | if (stat(sctl_path, &st)) |
| 1049 | return 1; |
| 1050 | |
| 1051 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 1052 | goto out; |
| 1053 | |
| 1054 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 1055 | free(sctl); |
| 1056 | goto out; |
| 1057 | } |
| 1058 | |
| 1059 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1060 | |
| 1061 | ret = 0; |
| 1062 | |
| 1063 | out: |
| 1064 | return ret; |
| 1065 | } |
| 1066 | |
| 1067 | #endif |
| 1068 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1069 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1070 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1071 | struct connection *conn = SSL_get_app_data(ssl); |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1072 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1073 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1074 | |
| 1075 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1076 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1077 | if (conn->flags & CO_FL_CONNECTED) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1078 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1079 | conn->err_code = CO_ER_SSL_RENEG; |
| 1080 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1081 | } |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1082 | |
| 1083 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 1084 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 1085 | /* Long certificate chains optimz |
| 1086 | If write and read bios are differents, we |
| 1087 | consider that the buffering was activated, |
| 1088 | so we rise the output buffer size from 4k |
| 1089 | to 16k */ |
| 1090 | write_bio = SSL_get_wbio(ssl); |
| 1091 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1092 | BIO_set_write_buffer_size(write_bio, 16384); |
| 1093 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 1094 | } |
| 1095 | } |
| 1096 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1097 | } |
| 1098 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1099 | /* Callback is called for each certificate of the chain during a verify |
| 1100 | ok is set to 1 if preverify detect no error on current certificate. |
| 1101 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1102 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1103 | { |
| 1104 | SSL *ssl; |
| 1105 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1106 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1107 | |
| 1108 | ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1109 | conn = SSL_get_app_data(ssl); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1110 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1111 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1112 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1113 | if (ok) /* no errors */ |
| 1114 | return ok; |
| 1115 | |
| 1116 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1117 | err = X509_STORE_CTX_get_error(x_store); |
| 1118 | |
| 1119 | /* check if CA error needs to be ignored */ |
| 1120 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1121 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 1122 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1123 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1124 | } |
| 1125 | |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1126 | 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] | 1127 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1128 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1129 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1130 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1131 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1132 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1133 | return 0; |
| 1134 | } |
| 1135 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1136 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 1137 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1138 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1139 | /* check if certificate error needs to be ignored */ |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1140 | 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] | 1141 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1142 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1143 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1144 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1145 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1146 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1147 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1150 | static inline |
| 1151 | 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] | 1152 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1153 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1154 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1155 | unsigned char *msg; |
| 1156 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1157 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1158 | |
| 1159 | /* This function is called for "from client" and "to server" |
| 1160 | * connections. The combination of write_p == 0 and content_type == 22 |
| 1161 | * is only avalaible during "from client" connection. |
| 1162 | */ |
| 1163 | |
| 1164 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1165 | * otherwise it is set to 1. |
| 1166 | */ |
| 1167 | if (write_p != 0) |
| 1168 | return; |
| 1169 | |
| 1170 | /* content_type contains the type of message received or sent |
| 1171 | * according with the SSL/TLS protocol spec. This message is |
| 1172 | * encoded with one byte. The value 256 (two bytes) is used |
| 1173 | * for designing the SSL/TLS record layer. According with the |
| 1174 | * rfc6101, the expected message (other than 256) are: |
| 1175 | * - change_cipher_spec(20) |
| 1176 | * - alert(21) |
| 1177 | * - handshake(22) |
| 1178 | * - application_data(23) |
| 1179 | * - (255) |
| 1180 | * We are interessed by the handshake and specially the client |
| 1181 | * hello. |
| 1182 | */ |
| 1183 | if (content_type != 22) |
| 1184 | return; |
| 1185 | |
| 1186 | /* The message length is at least 4 bytes, containing the |
| 1187 | * message type and the message length. |
| 1188 | */ |
| 1189 | if (len < 4) |
| 1190 | return; |
| 1191 | |
| 1192 | /* First byte of the handshake message id the type of |
| 1193 | * message. The konwn types are: |
| 1194 | * - hello_request(0) |
| 1195 | * - client_hello(1) |
| 1196 | * - server_hello(2) |
| 1197 | * - certificate(11) |
| 1198 | * - server_key_exchange (12) |
| 1199 | * - certificate_request(13) |
| 1200 | * - server_hello_done(14) |
| 1201 | * We are interested by the client hello. |
| 1202 | */ |
| 1203 | msg = (unsigned char *)buf; |
| 1204 | if (msg[0] != 1) |
| 1205 | return; |
| 1206 | |
| 1207 | /* Next three bytes are the length of the message. The total length |
| 1208 | * must be this decoded length + 4. If the length given as argument |
| 1209 | * is not the same, we abort the protocol dissector. |
| 1210 | */ |
| 1211 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1212 | if (len < rec_len + 4) |
| 1213 | return; |
| 1214 | msg += 4; |
| 1215 | end = msg + rec_len; |
| 1216 | if (end < msg) |
| 1217 | return; |
| 1218 | |
| 1219 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1220 | * for minor, the random, composed by 4 bytes for the unix time and |
| 1221 | * 28 bytes for unix payload, and them 1 byte for the session id. So |
| 1222 | * we jump 1 + 1 + 4 + 28 + 1 bytes. |
| 1223 | */ |
| 1224 | msg += 1 + 1 + 4 + 28 + 1; |
| 1225 | if (msg > end) |
| 1226 | return; |
| 1227 | |
| 1228 | /* Next two bytes are the ciphersuite length. */ |
| 1229 | if (msg + 2 > end) |
| 1230 | return; |
| 1231 | rec_len = (msg[0] << 8) + msg[1]; |
| 1232 | msg += 2; |
| 1233 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1234 | return; |
| 1235 | |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1236 | capture = pool_alloc_dirty(pool2_ssl_capture); |
| 1237 | if (!capture) |
| 1238 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1239 | /* Compute the xxh64 of the ciphersuite. */ |
| 1240 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1241 | |
| 1242 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1243 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1244 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1245 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1246 | |
| 1247 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1248 | } |
| 1249 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1250 | /* Callback is called for ssl protocol analyse */ |
| 1251 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1252 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1253 | #ifdef TLS1_RT_HEARTBEAT |
| 1254 | /* test heartbeat received (write_p is set to 0 |
| 1255 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1256 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1257 | struct connection *conn = SSL_get_app_data(ssl); |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1258 | const unsigned char *p = buf; |
| 1259 | unsigned int payload; |
| 1260 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1261 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1262 | |
| 1263 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1264 | if (*p != TLS1_HB_REQUEST) |
| 1265 | return; |
| 1266 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1267 | 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] | 1268 | goto kill_it; |
| 1269 | |
| 1270 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1271 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1272 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1273 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1274 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1275 | * advertised payload is larger than the advertised packet |
| 1276 | * length, so we have garbage in the buffer between the |
| 1277 | * payload and the end of the buffer (p+len). We can't know |
| 1278 | * if the SSL stack is patched, and we don't know if we can |
| 1279 | * safely wipe out the area between p+3+len and payload. |
| 1280 | * So instead, we prevent the response from being sent by |
| 1281 | * setting the max_send_fragment to 0 and we report an SSL |
| 1282 | * error, which will kill this connection. It will be reported |
| 1283 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1284 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1285 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1286 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1287 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1288 | return; |
| 1289 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1290 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1291 | if (global_ssl.capture_cipherlist > 0) |
| 1292 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1295 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 1296 | /* This callback is used so that the server advertises the list of |
| 1297 | * negociable protocols for NPN. |
| 1298 | */ |
| 1299 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1300 | unsigned int *len, void *arg) |
| 1301 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1302 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1303 | |
| 1304 | *data = (const unsigned char *)conf->npn_str; |
| 1305 | *len = conf->npn_len; |
| 1306 | return SSL_TLSEXT_ERR_OK; |
| 1307 | } |
| 1308 | #endif |
| 1309 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1310 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1311 | /* This callback is used so that the server advertises the list of |
| 1312 | * negociable protocols for ALPN. |
| 1313 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1314 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1315 | unsigned char *outlen, |
| 1316 | const unsigned char *server, |
| 1317 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1318 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1319 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1320 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1321 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1322 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1323 | return SSL_TLSEXT_ERR_NOACK; |
| 1324 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1325 | return SSL_TLSEXT_ERR_OK; |
| 1326 | } |
| 1327 | #endif |
| 1328 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1329 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1330 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1331 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1332 | /* Create a X509 certificate with the specified servername and serial. This |
| 1333 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1334 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1335 | 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] | 1336 | { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1337 | static unsigned int serial = 0; |
| 1338 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1339 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1340 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1341 | SSL_CTX *ssl_ctx = NULL; |
| 1342 | X509 *newcrt = NULL; |
| 1343 | EVP_PKEY *pkey = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1344 | X509_NAME *name; |
| 1345 | const EVP_MD *digest; |
| 1346 | X509V3_CTX ctx; |
| 1347 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1348 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1349 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1350 | /* Get the private key of the defautl certificate and use it */ |
| 1351 | if (!(pkey = SSL_get_privatekey(ssl))) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1352 | goto mkcert_error; |
| 1353 | |
| 1354 | /* Create the certificate */ |
| 1355 | if (!(newcrt = X509_new())) |
| 1356 | goto mkcert_error; |
| 1357 | |
| 1358 | /* Set version number for the certificate (X509v3) and the serial |
| 1359 | * number */ |
| 1360 | if (X509_set_version(newcrt, 2L) != 1) |
| 1361 | goto mkcert_error; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1362 | if (!serial) |
| 1363 | serial = now_ms; |
| 1364 | ASN1_INTEGER_set(X509_get_serialNumber(newcrt), serial++); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1365 | |
| 1366 | /* Set duration for the certificate */ |
| 1367 | if (!X509_gmtime_adj(X509_get_notBefore(newcrt), (long)-60*60*24) || |
| 1368 | !X509_gmtime_adj(X509_get_notAfter(newcrt),(long)60*60*24*365)) |
| 1369 | goto mkcert_error; |
| 1370 | |
| 1371 | /* set public key in the certificate */ |
| 1372 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1373 | goto mkcert_error; |
| 1374 | |
| 1375 | /* Set issuer name from the CA */ |
| 1376 | if (!(name = X509_get_subject_name(cacert))) |
| 1377 | goto mkcert_error; |
| 1378 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1379 | goto mkcert_error; |
| 1380 | |
| 1381 | /* Set the subject name using the same, but the CN */ |
| 1382 | name = X509_NAME_dup(name); |
| 1383 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1384 | (const unsigned char *)servername, |
| 1385 | -1, -1, 0) != 1) { |
| 1386 | X509_NAME_free(name); |
| 1387 | goto mkcert_error; |
| 1388 | } |
| 1389 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1390 | X509_NAME_free(name); |
| 1391 | goto mkcert_error; |
| 1392 | } |
| 1393 | X509_NAME_free(name); |
| 1394 | |
| 1395 | /* Add x509v3 extensions as specified */ |
| 1396 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1397 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1398 | X509_EXTENSION *ext; |
| 1399 | |
| 1400 | if (!(ext = X509V3_EXT_conf(NULL, &ctx, x509v3_ext_names[i], x509v3_ext_values[i]))) |
| 1401 | goto mkcert_error; |
| 1402 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1403 | X509_EXTENSION_free(ext); |
| 1404 | goto mkcert_error; |
| 1405 | } |
| 1406 | X509_EXTENSION_free(ext); |
| 1407 | } |
| 1408 | |
| 1409 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1410 | |
| 1411 | key_type = EVP_PKEY_base_id(capkey); |
| 1412 | |
| 1413 | if (key_type == EVP_PKEY_DSA) |
| 1414 | digest = EVP_sha1(); |
| 1415 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1416 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1417 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1418 | digest = EVP_sha256(); |
| 1419 | else { |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1420 | #if (OPENSSL_VERSION_NUMBER >= 0x1000000fL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1421 | int nid; |
| 1422 | |
| 1423 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1424 | goto mkcert_error; |
| 1425 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1426 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1427 | #else |
| 1428 | goto mkcert_error; |
| 1429 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1430 | } |
| 1431 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1432 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1433 | goto mkcert_error; |
| 1434 | |
| 1435 | /* Create and set the new SSL_CTX */ |
| 1436 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1437 | goto mkcert_error; |
| 1438 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1439 | goto mkcert_error; |
| 1440 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1441 | goto mkcert_error; |
| 1442 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1443 | goto mkcert_error; |
| 1444 | |
| 1445 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1446 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1447 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1448 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1449 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1450 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1451 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1452 | 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] | 1453 | EC_KEY *ecc; |
| 1454 | int nid; |
| 1455 | |
| 1456 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1457 | goto end; |
| 1458 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1459 | goto end; |
| 1460 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1461 | EC_KEY_free(ecc); |
| 1462 | } |
| 1463 | #endif |
| 1464 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1465 | return ssl_ctx; |
| 1466 | |
| 1467 | mkcert_error: |
| 1468 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1469 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1470 | return NULL; |
| 1471 | } |
| 1472 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1473 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1474 | 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] | 1475 | { |
| 1476 | struct bind_conf *bind_conf = objt_listener(conn->target)->bind_conf; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1477 | |
| 1478 | return ssl_sock_do_create_cert(servername, bind_conf, conn->xprt_ctx); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1479 | } |
| 1480 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1481 | /* Do a lookup for a certificate in the LRU cache used to store generated |
| 1482 | * certificates. */ |
| 1483 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1484 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1485 | { |
| 1486 | struct lru64 *lru = NULL; |
| 1487 | |
| 1488 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1489 | lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1490 | if (lru && lru->domain) |
| 1491 | return (SSL_CTX *)lru->data; |
| 1492 | } |
| 1493 | return NULL; |
| 1494 | } |
| 1495 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1496 | /* Set a certificate int the LRU cache used to store generated |
| 1497 | * certificate. Return 0 on success, otherwise -1 */ |
| 1498 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1499 | 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] | 1500 | { |
| 1501 | struct lru64 *lru = NULL; |
| 1502 | |
| 1503 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1504 | lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1505 | if (!lru) |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1506 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1507 | if (lru->domain && lru->data) |
| 1508 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1509 | lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1510 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1511 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1512 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1513 | } |
| 1514 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1515 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1516 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1517 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1518 | { |
| 1519 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 1520 | } |
| 1521 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1522 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 1523 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 1524 | */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1525 | static SSL_CTX * |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1526 | 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] | 1527 | { |
| 1528 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1529 | SSL_CTX *ssl_ctx = NULL; |
| 1530 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1531 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1532 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1533 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1534 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1535 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1536 | if (lru && lru->domain) |
| 1537 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1538 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1539 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1540 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1541 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1542 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1543 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1544 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1545 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1546 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 1547 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 1548 | SSL_CTX_free(ssl_ctx); |
| 1549 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1550 | return ssl_ctx; |
| 1551 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1552 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1553 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 1554 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 1555 | { |
| 1556 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 1557 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 1558 | SSL_set_SSL_CTX(ssl, ctx); |
| 1559 | } |
| 1560 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1561 | #ifdef OPENSSL_IS_BORINGSSL |
| 1562 | |
| 1563 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 1564 | { |
| 1565 | (void)al; /* shut gcc stupid warning */ |
| 1566 | (void)priv; |
| 1567 | |
| 1568 | if (!SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name)) |
| 1569 | return SSL_TLSEXT_ERR_NOACK; |
| 1570 | return SSL_TLSEXT_ERR_OK; |
| 1571 | } |
| 1572 | |
| 1573 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 1574 | { |
| 1575 | struct connection *conn; |
| 1576 | struct bind_conf *s; |
| 1577 | const uint8_t *extension_data; |
| 1578 | size_t extension_len; |
| 1579 | CBS extension, cipher_suites, server_name_list, host_name, sig_algs; |
| 1580 | const SSL_CIPHER *cipher; |
| 1581 | uint16_t cipher_suite; |
| 1582 | uint8_t name_type, hash, sign; |
| 1583 | int has_rsa = 0, has_ecdsa = 0, has_ecdsa_sig = 0; |
| 1584 | |
| 1585 | char *wildp = NULL; |
| 1586 | const uint8_t *servername; |
| 1587 | struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL; |
| 1588 | int i; |
| 1589 | |
| 1590 | conn = SSL_get_app_data(ctx->ssl); |
| 1591 | s = objt_listener(conn->target)->bind_conf; |
| 1592 | |
| 1593 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 1594 | &extension_data, &extension_len)) { |
| 1595 | CBS_init(&extension, extension_data, extension_len); |
| 1596 | |
| 1597 | if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) |
| 1598 | || !CBS_get_u8(&server_name_list, &name_type) |
| 1599 | /* Although the server_name extension was intended to be extensible to |
| 1600 | * new name types and multiple names, OpenSSL 1.0.x had a bug which meant |
| 1601 | * different name types will cause an error. Further, RFC 4366 originally |
| 1602 | * defined syntax inextensibly. RFC 6066 corrected this mistake, but |
| 1603 | * adding new name types is no longer feasible. |
| 1604 | * |
| 1605 | * Act as if the extensibility does not exist to simplify parsing. */ |
| 1606 | || !CBS_get_u16_length_prefixed(&server_name_list, &host_name) |
| 1607 | || CBS_len(&server_name_list) != 0 |
| 1608 | || CBS_len(&extension) != 0 |
| 1609 | || name_type != TLSEXT_NAMETYPE_host_name |
| 1610 | || CBS_len(&host_name) == 0 |
| 1611 | || CBS_len(&host_name) > TLSEXT_MAXLEN_host_name |
| 1612 | || CBS_contains_zero_byte(&host_name)) { |
| 1613 | goto abort; |
| 1614 | } |
| 1615 | } else { |
| 1616 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 1617 | if (!s->strict_sni) { |
| 1618 | ssl_sock_switchctx_set(ctx->ssl, s->default_ctx); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1619 | return 1; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 1620 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1621 | goto abort; |
| 1622 | } |
| 1623 | |
| 1624 | /* extract/check clientHello informations */ |
| 1625 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 1626 | CBS_init(&extension, extension_data, extension_len); |
| 1627 | |
| 1628 | if (!CBS_get_u16_length_prefixed(&extension, &sig_algs) |
| 1629 | || CBS_len(&sig_algs) == 0 |
| 1630 | || CBS_len(&extension) != 0) { |
| 1631 | goto abort; |
| 1632 | } |
| 1633 | if (CBS_len(&sig_algs) % 2 != 0) { |
| 1634 | goto abort; |
| 1635 | } |
| 1636 | while (CBS_len(&sig_algs) != 0) { |
| 1637 | if (!CBS_get_u8(&sig_algs, &hash) |
| 1638 | || !CBS_get_u8(&sig_algs, &sign)) { |
| 1639 | goto abort; |
| 1640 | } |
| 1641 | switch (sign) { |
| 1642 | case TLSEXT_signature_rsa: |
| 1643 | has_rsa = 1; |
| 1644 | break; |
| 1645 | case TLSEXT_signature_ecdsa: |
| 1646 | has_ecdsa_sig = 1; |
| 1647 | break; |
| 1648 | default: |
| 1649 | continue; |
| 1650 | } |
| 1651 | if (has_ecdsa_sig && has_rsa) |
| 1652 | break; |
| 1653 | } |
| 1654 | } else { |
| 1655 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLS 1.2) */ |
| 1656 | has_rsa = 1; |
| 1657 | } |
| 1658 | if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */ |
| 1659 | CBS_init(&cipher_suites, ctx->cipher_suites, ctx->cipher_suites_len); |
| 1660 | |
| 1661 | while (CBS_len(&cipher_suites) != 0) { |
| 1662 | if (!CBS_get_u16(&cipher_suites, &cipher_suite)) { |
| 1663 | goto abort; |
| 1664 | } |
| 1665 | cipher = SSL_get_cipher_by_value(cipher_suite); |
| 1666 | if (cipher && SSL_CIPHER_is_ECDSA(cipher)) { |
| 1667 | has_ecdsa = 1; |
| 1668 | break; |
| 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | servername = CBS_data(&host_name); |
| 1674 | for (i = 0; i < trash.size && i < CBS_len(&host_name); i++) { |
| 1675 | trash.str[i] = tolower(servername[i]); |
| 1676 | if (!wildp && (trash.str[i] == '.')) |
| 1677 | wildp = &trash.str[i]; |
| 1678 | } |
| 1679 | trash.str[i] = 0; |
| 1680 | |
| 1681 | /* lookup in full qualified names */ |
| 1682 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 1683 | |
| 1684 | /* lookup a not neg filter */ |
| 1685 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 1686 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 1687 | switch(container_of(n, struct sni_ctx, name)->key_sig) { |
| 1688 | case TLSEXT_signature_ecdsa: |
| 1689 | if (has_ecdsa) { |
| 1690 | node_ecdsa = n; |
| 1691 | goto find_one; |
| 1692 | } |
| 1693 | break; |
| 1694 | case TLSEXT_signature_rsa: |
| 1695 | if (has_rsa && !node_rsa) { |
| 1696 | node_rsa = n; |
| 1697 | if (!has_ecdsa) |
| 1698 | goto find_one; |
| 1699 | } |
| 1700 | break; |
| 1701 | default: /* TLSEXT_signature_anonymous */ |
| 1702 | if (!node_anonymous) |
| 1703 | node_anonymous = n; |
| 1704 | break; |
| 1705 | } |
| 1706 | } |
| 1707 | } |
| 1708 | if (wildp) { |
| 1709 | /* lookup in wildcards names */ |
| 1710 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 1711 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 1712 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 1713 | switch(container_of(n, struct sni_ctx, name)->key_sig) { |
| 1714 | case TLSEXT_signature_ecdsa: |
| 1715 | if (has_ecdsa) { |
| 1716 | node_ecdsa = n; |
| 1717 | goto find_one; |
| 1718 | } |
| 1719 | break; |
| 1720 | case TLSEXT_signature_rsa: |
| 1721 | if (has_rsa && !node_rsa) { |
| 1722 | node_rsa = n; |
| 1723 | if (!has_ecdsa) |
| 1724 | goto find_one; |
| 1725 | } |
| 1726 | break; |
| 1727 | default: /* TLSEXT_signature_anonymous */ |
| 1728 | if (!node_anonymous) |
| 1729 | node_anonymous = n; |
| 1730 | break; |
| 1731 | } |
| 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | find_one: |
| 1736 | /* select by key_signature priority order */ |
| 1737 | node = node_ecdsa ? node_ecdsa : (node_rsa ? node_rsa : node_anonymous); |
| 1738 | |
| 1739 | if (node) { |
| 1740 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 1741 | ssl_sock_switchctx_set(ctx->ssl, container_of(node, struct sni_ctx, name)->ctx); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1742 | return 1; |
| 1743 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 1744 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1745 | /* no certificate match, is the default_ctx */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 1746 | ssl_sock_switchctx_set(ctx->ssl, s->default_ctx); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1747 | return 1; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 1748 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1749 | abort: |
| 1750 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 1751 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 1752 | return -1; |
| 1753 | } |
| 1754 | |
| 1755 | #else /* OPENSSL_IS_BORINGSSL */ |
| 1756 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1757 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 1758 | * warning when no match is found, which implies the default (first) cert |
| 1759 | * will keep being used. |
| 1760 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1761 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1762 | { |
| 1763 | const char *servername; |
| 1764 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1765 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1766 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1767 | int i; |
| 1768 | (void)al; /* shut gcc stupid warning */ |
| 1769 | |
| 1770 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1771 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1772 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1773 | if (s->generate_certs) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1774 | struct connection *conn = SSL_get_app_data(ssl); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1775 | unsigned int key; |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1776 | SSL_CTX *ctx; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1777 | |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1778 | conn_get_to_addr(conn); |
| 1779 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1780 | key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to)); |
| 1781 | ctx = ssl_sock_get_generated_cert(key, s); |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1782 | if (ctx) { |
| 1783 | /* switch ctx */ |
| 1784 | SSL_set_SSL_CTX(ssl, ctx); |
| 1785 | return SSL_TLSEXT_ERR_OK; |
| 1786 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1787 | } |
| 1788 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1789 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 1790 | if (s->strict_sni) |
| 1791 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 1792 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 1793 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1794 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1795 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1796 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1797 | if (!servername[i]) |
| 1798 | break; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1799 | trash.str[i] = tolower(servername[i]); |
| 1800 | if (!wildp && (trash.str[i] == '.')) |
| 1801 | wildp = &trash.str[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1802 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1803 | trash.str[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1804 | |
| 1805 | /* lookup in full qualified names */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1806 | node = ebst_lookup(&s->sni_ctx, trash.str); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1807 | |
| 1808 | /* lookup a not neg filter */ |
| 1809 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 1810 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 1811 | node = n; |
| 1812 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1813 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1814 | } |
| 1815 | if (!node && wildp) { |
| 1816 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1817 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1818 | } |
| 1819 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1820 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1821 | SSL_CTX *ctx; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1822 | if (s->generate_certs && |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1823 | (ctx = ssl_sock_generate_certificate(servername, s, ssl))) { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1824 | /* switch ctx */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1825 | return SSL_TLSEXT_ERR_OK; |
| 1826 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1827 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 1828 | if (s->strict_sni) |
| 1829 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 1830 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 1831 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 1835 | 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] | 1836 | return SSL_TLSEXT_ERR_OK; |
| 1837 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1838 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1839 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 1840 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1841 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1842 | |
| 1843 | static DH * ssl_get_dh_1024(void) |
| 1844 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1845 | static unsigned char dh1024_p[]={ |
| 1846 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 1847 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 1848 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 1849 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 1850 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 1851 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 1852 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 1853 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 1854 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 1855 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 1856 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 1857 | }; |
| 1858 | static unsigned char dh1024_g[]={ |
| 1859 | 0x02, |
| 1860 | }; |
| 1861 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1862 | BIGNUM *p; |
| 1863 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1864 | DH *dh = DH_new(); |
| 1865 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1866 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 1867 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1868 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1869 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1870 | DH_free(dh); |
| 1871 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1872 | } else { |
| 1873 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | return dh; |
| 1877 | } |
| 1878 | |
| 1879 | static DH *ssl_get_dh_2048(void) |
| 1880 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1881 | static unsigned char dh2048_p[]={ |
| 1882 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 1883 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 1884 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 1885 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 1886 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 1887 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 1888 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 1889 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 1890 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 1891 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 1892 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 1893 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 1894 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 1895 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 1896 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 1897 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 1898 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 1899 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 1900 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 1901 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 1902 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 1903 | 0xB7,0x1F,0x77,0xF3, |
| 1904 | }; |
| 1905 | static unsigned char dh2048_g[]={ |
| 1906 | 0x02, |
| 1907 | }; |
| 1908 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1909 | BIGNUM *p; |
| 1910 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1911 | DH *dh = DH_new(); |
| 1912 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1913 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 1914 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1915 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1916 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1917 | DH_free(dh); |
| 1918 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1919 | } else { |
| 1920 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | return dh; |
| 1924 | } |
| 1925 | |
| 1926 | static DH *ssl_get_dh_4096(void) |
| 1927 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1928 | static unsigned char dh4096_p[]={ |
| 1929 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 1930 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 1931 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 1932 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 1933 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 1934 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 1935 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 1936 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 1937 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 1938 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 1939 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 1940 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 1941 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 1942 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 1943 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 1944 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 1945 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 1946 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 1947 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 1948 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 1949 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 1950 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 1951 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 1952 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 1953 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 1954 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 1955 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 1956 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 1957 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 1958 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 1959 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 1960 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 1961 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 1962 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 1963 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 1964 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 1965 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 1966 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 1967 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 1968 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 1969 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 1970 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 1971 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1972 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1973 | static unsigned char dh4096_g[]={ |
| 1974 | 0x02, |
| 1975 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1976 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1977 | BIGNUM *p; |
| 1978 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1979 | DH *dh = DH_new(); |
| 1980 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1981 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 1982 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1983 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1984 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1985 | DH_free(dh); |
| 1986 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1987 | } else { |
| 1988 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1989 | } |
| 1990 | } |
| 1991 | return dh; |
| 1992 | } |
| 1993 | |
| 1994 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 1995 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1996 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 1997 | { |
| 1998 | DH *dh = NULL; |
| 1999 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2000 | int type; |
| 2001 | |
| 2002 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2003 | |
| 2004 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2005 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2006 | */ |
| 2007 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2008 | keylen = EVP_PKEY_bits(pkey); |
| 2009 | } |
| 2010 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2011 | if (keylen > global_ssl.default_dh_param) { |
| 2012 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2013 | } |
| 2014 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2015 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2016 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2017 | } |
| 2018 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2019 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2020 | } |
| 2021 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2022 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | return dh; |
| 2026 | } |
| 2027 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2028 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2029 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2030 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2031 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2032 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2033 | if (in == NULL) |
| 2034 | goto end; |
| 2035 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2036 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2037 | goto end; |
| 2038 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2039 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2040 | |
| 2041 | end: |
| 2042 | if (in) |
| 2043 | BIO_free(in); |
| 2044 | |
| 2045 | return dh; |
| 2046 | } |
| 2047 | |
| 2048 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2049 | { |
| 2050 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2051 | |
| 2052 | if (global_dh) { |
| 2053 | return 0; |
| 2054 | } |
| 2055 | |
| 2056 | return -1; |
| 2057 | } |
| 2058 | |
| 2059 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 2060 | if an error occured, and 0 if parameter not found. */ |
| 2061 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 2062 | { |
| 2063 | int ret = -1; |
| 2064 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 2065 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2066 | if (dh) { |
| 2067 | ret = 1; |
| 2068 | SSL_CTX_set_tmp_dh(ctx, dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2069 | |
| 2070 | if (ssl_dh_ptr_index >= 0) { |
| 2071 | /* store a pointer to the DH params to avoid complaining about |
| 2072 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2073 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2074 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2075 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2076 | else if (global_dh) { |
| 2077 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 2078 | ret = 0; /* DH params not found */ |
| 2079 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2080 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2081 | /* Clear openssl global errors stack */ |
| 2082 | ERR_clear_error(); |
| 2083 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2084 | if (global_ssl.default_dh_param <= 1024) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2085 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 2086 | if (local_dh_1024 == NULL) |
| 2087 | local_dh_1024 = ssl_get_dh_1024(); |
| 2088 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2089 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2090 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2091 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2092 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2093 | } |
| 2094 | else { |
| 2095 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2096 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2097 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2098 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2099 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2100 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2101 | end: |
| 2102 | if (dh) |
| 2103 | DH_free(dh); |
| 2104 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2105 | return ret; |
| 2106 | } |
| 2107 | #endif |
| 2108 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2109 | static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, struct ssl_bind_conf *conf, |
| 2110 | uint8_t key_sig, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2111 | { |
| 2112 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2113 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2114 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2115 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2116 | if (*name == '!') { |
| 2117 | neg = 1; |
| 2118 | name++; |
| 2119 | } |
| 2120 | if (*name == '*') { |
| 2121 | wild = 1; |
| 2122 | name++; |
| 2123 | } |
| 2124 | /* !* filter is a nop */ |
| 2125 | if (neg && wild) |
| 2126 | return order; |
| 2127 | if (*name) { |
| 2128 | int j, len; |
| 2129 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2130 | for (j = 0; j < len && j < trash.size; j++) |
| 2131 | trash.str[j] = tolower(name[j]); |
| 2132 | if (j >= trash.size) |
| 2133 | return order; |
| 2134 | trash.str[j] = 0; |
| 2135 | |
| 2136 | /* Check for duplicates. */ |
| 2137 | if (wild) |
| 2138 | node = ebst_lookup(&s->sni_w_ctx, trash.str); |
| 2139 | else |
| 2140 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 2141 | for (; node; node = ebmb_next_dup(node)) { |
| 2142 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2143 | if (sc->ctx == ctx && sc->conf == conf && |
| 2144 | sc->key_sig == key_sig && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2145 | return order; |
| 2146 | } |
| 2147 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2148 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2149 | if (!sc) |
| 2150 | return order; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2151 | memcpy(sc->name.key, trash.str, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2152 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2153 | sc->conf = conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2154 | sc->key_sig = key_sig; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2155 | sc->order = order++; |
| 2156 | sc->neg = neg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2157 | if (wild) |
| 2158 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 2159 | else |
| 2160 | ebst_insert(&s->sni_ctx, &sc->name); |
| 2161 | } |
| 2162 | return order; |
| 2163 | } |
| 2164 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2165 | |
| 2166 | /* The following code is used for loading multiple crt files into |
| 2167 | * SSL_CTX's based on CN/SAN |
| 2168 | */ |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 2169 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(LIBRESSL_VERSION_NUMBER) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2170 | /* This is used to preload the certifcate, private key |
| 2171 | * and Cert Chain of a file passed in via the crt |
| 2172 | * argument |
| 2173 | * |
| 2174 | * This way, we do not have to read the file multiple times |
| 2175 | */ |
| 2176 | struct cert_key_and_chain { |
| 2177 | X509 *cert; |
| 2178 | EVP_PKEY *key; |
| 2179 | unsigned int num_chain_certs; |
| 2180 | /* This is an array of X509 pointers */ |
| 2181 | X509 **chain_certs; |
| 2182 | }; |
| 2183 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2184 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 2185 | |
| 2186 | struct key_combo_ctx { |
| 2187 | SSL_CTX *ctx; |
| 2188 | int order; |
| 2189 | }; |
| 2190 | |
| 2191 | /* Map used for processing multiple keypairs for a single purpose |
| 2192 | * |
| 2193 | * This maps CN/SNI name to certificate type |
| 2194 | */ |
| 2195 | struct sni_keytype { |
| 2196 | int keytypes; /* BITMASK for keytypes */ |
| 2197 | struct ebmb_node name; /* node holding the servername value */ |
| 2198 | }; |
| 2199 | |
| 2200 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2201 | /* Frees the contents of a cert_key_and_chain |
| 2202 | */ |
| 2203 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2204 | { |
| 2205 | int i; |
| 2206 | |
| 2207 | if (!ckch) |
| 2208 | return; |
| 2209 | |
| 2210 | /* Free the certificate and set pointer to NULL */ |
| 2211 | if (ckch->cert) |
| 2212 | X509_free(ckch->cert); |
| 2213 | ckch->cert = NULL; |
| 2214 | |
| 2215 | /* Free the key and set pointer to NULL */ |
| 2216 | if (ckch->key) |
| 2217 | EVP_PKEY_free(ckch->key); |
| 2218 | ckch->key = NULL; |
| 2219 | |
| 2220 | /* Free each certificate in the chain */ |
| 2221 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2222 | if (ckch->chain_certs[i]) |
| 2223 | X509_free(ckch->chain_certs[i]); |
| 2224 | } |
| 2225 | |
| 2226 | /* Free the chain obj itself and set to NULL */ |
| 2227 | if (ckch->num_chain_certs > 0) { |
| 2228 | free(ckch->chain_certs); |
| 2229 | ckch->num_chain_certs = 0; |
| 2230 | ckch->chain_certs = NULL; |
| 2231 | } |
| 2232 | |
| 2233 | } |
| 2234 | |
| 2235 | /* checks if a key and cert exists in the ckch |
| 2236 | */ |
| 2237 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 2238 | { |
| 2239 | return (ckch->cert != NULL && ckch->key != NULL); |
| 2240 | } |
| 2241 | |
| 2242 | |
| 2243 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 2244 | * This allows us to carry the contents of the file without having to |
| 2245 | * read the file multiple times. |
| 2246 | * |
| 2247 | * returns: |
| 2248 | * 0 on Success |
| 2249 | * 1 on SSL Failure |
| 2250 | * 2 on file not found |
| 2251 | */ |
| 2252 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 2253 | { |
| 2254 | |
| 2255 | BIO *in; |
| 2256 | X509 *ca = NULL; |
| 2257 | int ret = 1; |
| 2258 | |
| 2259 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2260 | |
| 2261 | in = BIO_new(BIO_s_file()); |
| 2262 | if (in == NULL) |
| 2263 | goto end; |
| 2264 | |
| 2265 | if (BIO_read_filename(in, path) <= 0) |
| 2266 | goto end; |
| 2267 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2268 | /* Read Private Key */ |
| 2269 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 2270 | if (ckch->key == NULL) { |
| 2271 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 2272 | err && *err ? *err : "", path); |
| 2273 | goto end; |
| 2274 | } |
| 2275 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2276 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 2277 | if (BIO_reset(in) == -1) { |
| 2278 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 2279 | err && *err ? *err : "", path); |
| 2280 | goto end; |
| 2281 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2282 | |
| 2283 | /* Read Certificate */ |
| 2284 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 2285 | if (ckch->cert == NULL) { |
| 2286 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 2287 | err && *err ? *err : "", path); |
| 2288 | goto end; |
| 2289 | } |
| 2290 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2291 | /* Read Certificate Chain */ |
| 2292 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 2293 | /* Grow the chain certs */ |
| 2294 | ckch->num_chain_certs++; |
| 2295 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 2296 | |
| 2297 | /* use - 1 here since we just incremented it above */ |
| 2298 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 2299 | } |
| 2300 | ret = ERR_get_error(); |
| 2301 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 2302 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 2303 | err && *err ? *err : "", path); |
| 2304 | ret = 1; |
| 2305 | goto end; |
| 2306 | } |
| 2307 | |
| 2308 | ret = 0; |
| 2309 | |
| 2310 | end: |
| 2311 | |
| 2312 | ERR_clear_error(); |
| 2313 | if (in) |
| 2314 | BIO_free(in); |
| 2315 | |
| 2316 | /* Something went wrong in one of the reads */ |
| 2317 | if (ret != 0) |
| 2318 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2319 | |
| 2320 | return ret; |
| 2321 | } |
| 2322 | |
| 2323 | /* Loads the info in ckch into ctx |
| 2324 | * Currently, this does not process any information about ocsp, dhparams or |
| 2325 | * sctl |
| 2326 | * Returns |
| 2327 | * 0 on success |
| 2328 | * 1 on failure |
| 2329 | */ |
| 2330 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2331 | { |
| 2332 | int i = 0; |
| 2333 | |
| 2334 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2335 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2336 | err && *err ? *err : "", path); |
| 2337 | return 1; |
| 2338 | } |
| 2339 | |
| 2340 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 2341 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 2342 | err && *err ? *err : "", path); |
| 2343 | return 1; |
| 2344 | } |
| 2345 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2346 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 2347 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2348 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2349 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 2350 | err && *err ? *err : "", (i+1), path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2351 | return 1; |
| 2352 | } |
| 2353 | } |
| 2354 | |
| 2355 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2356 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2357 | err && *err ? *err : "", path); |
| 2358 | return 1; |
| 2359 | } |
| 2360 | |
| 2361 | return 0; |
| 2362 | } |
| 2363 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2364 | |
| 2365 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 2366 | { |
| 2367 | struct sni_keytype *s_kt = NULL; |
| 2368 | struct ebmb_node *node; |
| 2369 | int i; |
| 2370 | |
| 2371 | for (i = 0; i < trash.size; i++) { |
| 2372 | if (!str[i]) |
| 2373 | break; |
| 2374 | trash.str[i] = tolower(str[i]); |
| 2375 | } |
| 2376 | trash.str[i] = 0; |
| 2377 | node = ebst_lookup(sni_keytypes, trash.str); |
| 2378 | if (!node) { |
| 2379 | /* CN not found in tree */ |
| 2380 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 2381 | /* Using memcpy here instead of strncpy. |
| 2382 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 2383 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 2384 | */ |
| 2385 | memcpy(s_kt->name.key, trash.str, i+1); |
| 2386 | s_kt->keytypes = 0; |
| 2387 | ebst_insert(sni_keytypes, &s_kt->name); |
| 2388 | } else { |
| 2389 | /* CN found in tree */ |
| 2390 | s_kt = container_of(node, struct sni_keytype, name); |
| 2391 | } |
| 2392 | |
| 2393 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 2394 | s_kt->keytypes |= 1<<key_index; |
| 2395 | |
| 2396 | } |
| 2397 | |
| 2398 | |
| 2399 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 2400 | * If any are found, group these files into a set of SSL_CTX* |
| 2401 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 2402 | * |
| 2403 | * This will allow the user to explictly group multiple cert/keys for a single purpose |
| 2404 | * |
| 2405 | * Returns |
| 2406 | * 0 on success |
| 2407 | * 1 on failure |
| 2408 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2409 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2410 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2411 | { |
| 2412 | char fp[MAXPATHLEN+1] = {0}; |
| 2413 | int n = 0; |
| 2414 | int i = 0; |
| 2415 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 2416 | struct eb_root sni_keytypes_map = { {0} }; |
| 2417 | struct ebmb_node *node; |
| 2418 | struct ebmb_node *next; |
| 2419 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 2420 | * of keytypes |
| 2421 | */ |
| 2422 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 2423 | int rv = 0; |
| 2424 | X509_NAME *xname = NULL; |
| 2425 | char *str = NULL; |
| 2426 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2427 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 2428 | #endif |
| 2429 | |
| 2430 | /* Load all possible certs and keys */ |
| 2431 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2432 | struct stat buf; |
| 2433 | |
| 2434 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2435 | if (stat(fp, &buf) == 0) { |
| 2436 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
| 2437 | rv = 1; |
| 2438 | goto end; |
| 2439 | } |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | /* Process each ckch and update keytypes for each CN/SAN |
| 2444 | * for example, if CN/SAN www.a.com is associated with |
| 2445 | * certs with keytype 0 and 2, then at the end of the loop, |
| 2446 | * www.a.com will have: |
| 2447 | * keyindex = 0 | 1 | 4 = 5 |
| 2448 | */ |
| 2449 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2450 | |
| 2451 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 2452 | continue; |
| 2453 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2454 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 2455 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2456 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 2457 | } else { |
| 2458 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 2459 | * so the line that contains logic is marked via comments |
| 2460 | */ |
| 2461 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 2462 | i = -1; |
| 2463 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 2464 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2465 | ASN1_STRING *value; |
| 2466 | value = X509_NAME_ENTRY_get_data(entry); |
| 2467 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2468 | /* Important line is here */ |
| 2469 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2470 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2471 | OPENSSL_free(str); |
| 2472 | str = NULL; |
| 2473 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2474 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2475 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2476 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2477 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2478 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 2479 | if (names) { |
| 2480 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 2481 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2482 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2483 | if (name->type == GEN_DNS) { |
| 2484 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 2485 | /* Important line is here */ |
| 2486 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2487 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2488 | OPENSSL_free(str); |
| 2489 | str = NULL; |
| 2490 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2491 | } |
| 2492 | } |
| 2493 | } |
| 2494 | } |
| 2495 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2496 | } |
| 2497 | |
| 2498 | /* If no files found, return error */ |
| 2499 | if (eb_is_empty(&sni_keytypes_map)) { |
| 2500 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 2501 | err && *err ? *err : "", path); |
| 2502 | rv = 1; |
| 2503 | goto end; |
| 2504 | } |
| 2505 | |
| 2506 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 2507 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 2508 | * and add each CTX to the SNI tree |
| 2509 | * |
| 2510 | * Some math here: |
| 2511 | * There are 2^n - 1 possibile combinations, each unique |
| 2512 | * combination is denoted by the key in the map. Each key |
| 2513 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 2514 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 2515 | * entry in the array to correspond to the unique combo (key) |
| 2516 | * associated with i. This unique key combo (i) will be associated |
| 2517 | * with combos[i-1] |
| 2518 | */ |
| 2519 | |
| 2520 | node = ebmb_first(&sni_keytypes_map); |
| 2521 | while (node) { |
| 2522 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2523 | char cur_file[MAXPATHLEN+1]; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2524 | |
| 2525 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 2526 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 2527 | cur_ctx = key_combos[i-1].ctx; |
| 2528 | |
| 2529 | if (cur_ctx == NULL) { |
| 2530 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2531 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2532 | if (cur_ctx == NULL) { |
| 2533 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 2534 | err && *err ? *err : ""); |
| 2535 | rv = 1; |
| 2536 | goto end; |
| 2537 | } |
| 2538 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2539 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2540 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2541 | if (i & (1<<n)) { |
| 2542 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2543 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2544 | 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] | 2545 | SSL_CTX_free(cur_ctx); |
| 2546 | rv = 1; |
| 2547 | goto end; |
| 2548 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2549 | |
| 2550 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 2551 | /* Load OCSP Info into context */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2552 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2553 | if (err) |
| 2554 | 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] | 2555 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2556 | SSL_CTX_free(cur_ctx); |
| 2557 | rv = 1; |
| 2558 | goto end; |
| 2559 | } |
| 2560 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | /* Load DH params into the ctx to support DHE keys */ |
| 2565 | #ifndef OPENSSL_NO_DH |
| 2566 | if (ssl_dh_ptr_index >= 0) |
| 2567 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 2568 | |
| 2569 | rv = ssl_sock_load_dh_params(cur_ctx, NULL); |
| 2570 | if (rv < 0) { |
| 2571 | if (err) |
| 2572 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 2573 | *err ? *err : "", path); |
| 2574 | rv = 1; |
| 2575 | goto end; |
| 2576 | } |
| 2577 | #endif |
| 2578 | |
| 2579 | /* Update key_combos */ |
| 2580 | key_combos[i-1].ctx = cur_ctx; |
| 2581 | } |
| 2582 | |
| 2583 | /* Update SNI Tree */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2584 | key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf, |
| 2585 | TLSEXT_signature_anonymous, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2586 | node = ebmb_next(node); |
| 2587 | } |
| 2588 | |
| 2589 | |
| 2590 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 2591 | if (!bind_conf->default_ctx) { |
| 2592 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 2593 | if (key_combos[i].ctx) { |
| 2594 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2595 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2596 | break; |
| 2597 | } |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | end: |
| 2602 | |
| 2603 | if (names) |
| 2604 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 2605 | |
| 2606 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 2607 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 2608 | |
| 2609 | node = ebmb_first(&sni_keytypes_map); |
| 2610 | while (node) { |
| 2611 | next = ebmb_next(node); |
| 2612 | ebmb_delete(node); |
| 2613 | node = next; |
| 2614 | } |
| 2615 | |
| 2616 | return rv; |
| 2617 | } |
| 2618 | #else |
| 2619 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2620 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2621 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2622 | { |
| 2623 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 2624 | err && *err ? *err : "", path, strerror(errno)); |
| 2625 | return 1; |
| 2626 | } |
| 2627 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2628 | #endif /* #if OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
| 2629 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2630 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 2631 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 2632 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2633 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, |
| 2634 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2635 | { |
| 2636 | BIO *in; |
| 2637 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2638 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2639 | int ret = -1; |
| 2640 | int order = 0; |
| 2641 | X509_NAME *xname; |
| 2642 | char *str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2643 | pem_password_cb *passwd_cb; |
| 2644 | void *passwd_cb_userdata; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2645 | EVP_PKEY *pkey; |
| 2646 | uint8_t key_sig = TLSEXT_signature_anonymous; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2647 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2648 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2649 | STACK_OF(GENERAL_NAME) *names; |
| 2650 | #endif |
| 2651 | |
| 2652 | in = BIO_new(BIO_s_file()); |
| 2653 | if (in == NULL) |
| 2654 | goto end; |
| 2655 | |
| 2656 | if (BIO_read_filename(in, file) <= 0) |
| 2657 | goto end; |
| 2658 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2659 | |
| 2660 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 2661 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 2662 | |
| 2663 | 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] | 2664 | if (x == NULL) |
| 2665 | goto end; |
| 2666 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2667 | pkey = X509_get_pubkey(x); |
| 2668 | if (pkey) { |
| 2669 | switch(EVP_PKEY_base_id(pkey)) { |
| 2670 | case EVP_PKEY_RSA: |
| 2671 | key_sig = TLSEXT_signature_rsa; |
| 2672 | break; |
| 2673 | case EVP_PKEY_EC: |
| 2674 | key_sig = TLSEXT_signature_ecdsa; |
| 2675 | break; |
| 2676 | } |
| 2677 | EVP_PKEY_free(pkey); |
| 2678 | } |
| 2679 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2680 | if (fcount) { |
| 2681 | while (fcount--) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2682 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, key_sig, sni_filter[fcount], order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2683 | } |
| 2684 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2685 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2686 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 2687 | if (names) { |
| 2688 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 2689 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 2690 | if (name->type == GEN_DNS) { |
| 2691 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2692 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, key_sig, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2693 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2694 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2695 | } |
| 2696 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2697 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2698 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2699 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2700 | xname = X509_get_subject_name(x); |
| 2701 | i = -1; |
| 2702 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 2703 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2704 | ASN1_STRING *value; |
| 2705 | |
| 2706 | value = X509_NAME_ENTRY_get_data(entry); |
| 2707 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2708 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, key_sig, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2709 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2710 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 2715 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 2716 | goto end; |
| 2717 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2718 | #ifdef SSL_CTX_clear_extra_chain_certs |
| 2719 | SSL_CTX_clear_extra_chain_certs(ctx); |
| 2720 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2721 | if (ctx->extra_certs != NULL) { |
| 2722 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 2723 | ctx->extra_certs = NULL; |
| 2724 | } |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2725 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2726 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2727 | 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] | 2728 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 2729 | X509_free(ca); |
| 2730 | goto end; |
| 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | err = ERR_get_error(); |
| 2735 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 2736 | /* we successfully reached the last cert in the file */ |
| 2737 | ret = 1; |
| 2738 | } |
| 2739 | ERR_clear_error(); |
| 2740 | |
| 2741 | end: |
| 2742 | if (x) |
| 2743 | X509_free(x); |
| 2744 | |
| 2745 | if (in) |
| 2746 | BIO_free(in); |
| 2747 | |
| 2748 | return ret; |
| 2749 | } |
| 2750 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2751 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2752 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2753 | { |
| 2754 | int ret; |
| 2755 | SSL_CTX *ctx; |
| 2756 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2757 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2758 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2759 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 2760 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2761 | return 1; |
| 2762 | } |
| 2763 | |
| 2764 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2765 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 2766 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2767 | SSL_CTX_free(ctx); |
| 2768 | return 1; |
| 2769 | } |
| 2770 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2771 | 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] | 2772 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2773 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 2774 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2775 | if (ret < 0) /* serious error, must do that ourselves */ |
| 2776 | SSL_CTX_free(ctx); |
| 2777 | return 1; |
| 2778 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 2779 | |
| 2780 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2781 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2782 | err && *err ? *err : "", path); |
| 2783 | return 1; |
| 2784 | } |
| 2785 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2786 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 2787 | * the tree, so it will be discovered and cleaned in time. |
| 2788 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2789 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2790 | /* store a NULL pointer to indicate we have not yet loaded |
| 2791 | a custom DH param file */ |
| 2792 | if (ssl_dh_ptr_index >= 0) { |
| 2793 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 2794 | } |
| 2795 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2796 | ret = ssl_sock_load_dh_params(ctx, path); |
| 2797 | if (ret < 0) { |
| 2798 | if (err) |
| 2799 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 2800 | *err ? *err : "", path); |
| 2801 | return 1; |
| 2802 | } |
| 2803 | #endif |
| 2804 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 2805 | #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] | 2806 | ret = ssl_sock_load_ocsp(ctx, path); |
| 2807 | if (ret < 0) { |
| 2808 | if (err) |
| 2809 | 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", |
| 2810 | *err ? *err : "", path); |
| 2811 | return 1; |
| 2812 | } |
| 2813 | #endif |
| 2814 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 2815 | #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] | 2816 | if (sctl_ex_index >= 0) { |
| 2817 | ret = ssl_sock_load_sctl(ctx, path); |
| 2818 | if (ret < 0) { |
| 2819 | if (err) |
| 2820 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 2821 | *err ? *err : "", path); |
| 2822 | return 1; |
| 2823 | } |
| 2824 | } |
| 2825 | #endif |
| 2826 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2827 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2828 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2829 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 2830 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2831 | return 1; |
| 2832 | } |
| 2833 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2834 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2835 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2836 | bind_conf->default_ssl_conf = ssl_conf; |
| 2837 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2838 | |
| 2839 | return 0; |
| 2840 | } |
| 2841 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 2842 | 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] | 2843 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 2844 | struct dirent **de_list; |
| 2845 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2846 | DIR *dir; |
| 2847 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 2848 | char *end; |
| 2849 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2850 | int cfgerr = 0; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 2851 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 2852 | int is_bundle; |
| 2853 | int j; |
| 2854 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2855 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2856 | if (stat(path, &buf) == 0) { |
| 2857 | dir = opendir(path); |
| 2858 | if (!dir) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2859 | 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] | 2860 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2861 | /* strip trailing slashes, including first one */ |
| 2862 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 2863 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2864 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2865 | n = scandir(path, &de_list, 0, alphasort); |
| 2866 | if (n < 0) { |
| 2867 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 2868 | err && *err ? *err : "", path, strerror(errno)); |
| 2869 | cfgerr++; |
| 2870 | } |
| 2871 | else { |
| 2872 | for (i = 0; i < n; i++) { |
| 2873 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 2874 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2875 | end = strrchr(de->d_name, '.'); |
| 2876 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 2877 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 2878 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2879 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 2880 | if (stat(fp, &buf) != 0) { |
| 2881 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 2882 | err && *err ? *err : "", fp, strerror(errno)); |
| 2883 | cfgerr++; |
| 2884 | goto ignore_entry; |
| 2885 | } |
| 2886 | if (!S_ISREG(buf.st_mode)) |
| 2887 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 2888 | |
| 2889 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 2890 | is_bundle = 0; |
| 2891 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 2892 | |
| 2893 | if (end) { |
| 2894 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 2895 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 2896 | is_bundle = 1; |
| 2897 | break; |
| 2898 | } |
| 2899 | } |
| 2900 | |
| 2901 | if (is_bundle) { |
| 2902 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 2903 | int dp_len; |
| 2904 | |
| 2905 | dp_len = end - de->d_name; |
| 2906 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 2907 | |
| 2908 | /* increment i and free de until we get to a non-bundle cert |
| 2909 | * Note here that we look at de_list[i + 1] before freeing de |
| 2910 | * this is important since ignore_entry will free de |
| 2911 | */ |
| 2912 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 2913 | free(de); |
| 2914 | i++; |
| 2915 | de = de_list[i]; |
| 2916 | } |
| 2917 | |
| 2918 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2919 | ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 2920 | |
| 2921 | /* Successfully processed the bundle */ |
| 2922 | goto ignore_entry; |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2927 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2928 | ignore_entry: |
| 2929 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 2930 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2931 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2932 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2933 | closedir(dir); |
| 2934 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2935 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2936 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2937 | cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2938 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2939 | return cfgerr; |
| 2940 | } |
| 2941 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 2942 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 2943 | * done once. Zero is returned if the operation fails. No error is returned |
| 2944 | * if the random is said as not implemented, because we expect that openssl |
| 2945 | * will use another method once needed. |
| 2946 | */ |
| 2947 | static int ssl_initialize_random() |
| 2948 | { |
| 2949 | unsigned char random; |
| 2950 | static int random_initialized = 0; |
| 2951 | |
| 2952 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 2953 | random_initialized = 1; |
| 2954 | |
| 2955 | return random_initialized; |
| 2956 | } |
| 2957 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2958 | /* release ssl bind conf */ |
| 2959 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2960 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2961 | if (conf) { |
| 2962 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 2963 | free(conf->npn_str); |
| 2964 | conf->npn_str = NULL; |
| 2965 | #endif |
| 2966 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 2967 | free(conf->alpn_str); |
| 2968 | conf->alpn_str = NULL; |
| 2969 | #endif |
| 2970 | free(conf->ca_file); |
| 2971 | conf->ca_file = NULL; |
| 2972 | free(conf->crl_file); |
| 2973 | conf->crl_file = NULL; |
| 2974 | free(conf->ciphers); |
| 2975 | conf->ciphers = NULL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 2976 | free(conf->curves); |
| 2977 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2978 | free(conf->ecdhe); |
| 2979 | conf->ecdhe = NULL; |
| 2980 | } |
| 2981 | } |
| 2982 | |
| 2983 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 2984 | { |
| 2985 | char thisline[CRT_LINESIZE]; |
| 2986 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2987 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 2988 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2989 | int linenum = 0; |
| 2990 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2991 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2992 | if ((f = fopen(file, "r")) == NULL) { |
| 2993 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2994 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2995 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2996 | |
| 2997 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2998 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2999 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3000 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3001 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3002 | char *crt_path; |
| 3003 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3004 | |
| 3005 | linenum++; |
| 3006 | end = line + strlen(line); |
| 3007 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 3008 | /* Check if we reached the limit and the last char is not \n. |
| 3009 | * Watch out for the last line without the terminating '\n'! |
| 3010 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3011 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 3012 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3013 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3014 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3015 | } |
| 3016 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3017 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3018 | newarg = 1; |
| 3019 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3020 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 3021 | /* end of string, end of loop */ |
| 3022 | *line = 0; |
| 3023 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3024 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3025 | newarg = 1; |
| 3026 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3027 | } else if (*line == '[') { |
| 3028 | if (ssl_b) { |
| 3029 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 3030 | cfgerr = 1; |
| 3031 | break; |
| 3032 | } |
| 3033 | if (!arg) { |
| 3034 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 3035 | cfgerr = 1; |
| 3036 | break; |
| 3037 | } |
| 3038 | ssl_b = arg; |
| 3039 | newarg = 1; |
| 3040 | *line = 0; |
| 3041 | } else if (*line == ']') { |
| 3042 | if (ssl_e) { |
| 3043 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3044 | cfgerr = 1; |
| 3045 | break; |
| 3046 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3047 | if (!ssl_b) { |
| 3048 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 3049 | cfgerr = 1; |
| 3050 | break; |
| 3051 | } |
| 3052 | ssl_e = arg; |
| 3053 | newarg = 1; |
| 3054 | *line = 0; |
| 3055 | } else if (newarg) { |
| 3056 | if (arg == MAX_CRT_ARGS) { |
| 3057 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 3058 | cfgerr = 1; |
| 3059 | break; |
| 3060 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3061 | newarg = 0; |
| 3062 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3063 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3064 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3065 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3066 | if (cfgerr) |
| 3067 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3068 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3069 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3070 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3071 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3072 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3073 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3074 | crt_path = args[0]; |
| 3075 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 3076 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 3077 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 3078 | crt_path, linenum, file); |
| 3079 | cfgerr = 1; |
| 3080 | break; |
| 3081 | } |
| 3082 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 3083 | crt_path = path; |
| 3084 | } |
| 3085 | |
| 3086 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 3087 | cur_arg = ssl_b ? ssl_b : 1; |
| 3088 | while (cur_arg < ssl_e) { |
| 3089 | newarg = 0; |
| 3090 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 3091 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 3092 | newarg = 1; |
| 3093 | cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
| 3094 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 3095 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 3096 | args[cur_arg], linenum, file); |
| 3097 | cfgerr = 1; |
| 3098 | } |
| 3099 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 3100 | break; |
| 3101 | } |
| 3102 | } |
| 3103 | if (!cfgerr && !newarg) { |
| 3104 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 3105 | args[cur_arg], linenum, file); |
| 3106 | cfgerr = 1; |
| 3107 | break; |
| 3108 | } |
| 3109 | } |
| 3110 | if (cfgerr) { |
| 3111 | ssl_sock_free_ssl_conf(ssl_conf); |
| 3112 | free(ssl_conf); |
| 3113 | ssl_conf = NULL; |
| 3114 | break; |
| 3115 | } |
| 3116 | |
| 3117 | if (stat(crt_path, &buf) == 0) { |
| 3118 | cfgerr = ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf, |
| 3119 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3120 | } else { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3121 | cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf, |
| 3122 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3123 | } |
| 3124 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3125 | if (cfgerr) { |
| 3126 | 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] | 3127 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3128 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3129 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3130 | fclose(f); |
| 3131 | return cfgerr; |
| 3132 | } |
| 3133 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3134 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 3135 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 3136 | #endif |
| 3137 | |
| 3138 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 3139 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
Willy Tarreau | 7d588ee | 2012-11-26 18:47:31 +0100 | [diff] [blame] | 3140 | #define SSL_renegotiate_pending(arg) 0 |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3141 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3142 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 3143 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 3144 | #endif |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3145 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 3146 | #define SSL_OP_NO_TICKET 0 |
| 3147 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3148 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 3149 | #define SSL_OP_NO_COMPRESSION 0 |
| 3150 | #endif |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 3151 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 3152 | #define SSL_OP_NO_TLSv1_1 0 |
| 3153 | #endif |
| 3154 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 3155 | #define SSL_OP_NO_TLSv1_2 0 |
| 3156 | #endif |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3157 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 3158 | #define SSL_OP_SINGLE_DH_USE 0 |
| 3159 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3160 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 3161 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 3162 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3163 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 3164 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 3165 | #endif |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3166 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 3167 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 3168 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3169 | |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3170 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3171 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3172 | static SSL_CTX * |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3173 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3174 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3175 | SSL_CTX *ctx = NULL; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 3176 | long ssloptions = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3177 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3178 | SSL_OP_NO_SSLv2 | |
| 3179 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3180 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3181 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3182 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
| 3183 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 3184 | long sslmode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3185 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3186 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3187 | SSL_MODE_RELEASE_BUFFERS | |
| 3188 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 3189 | int conf_ssl_options = bind_conf->ssl_options; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3190 | |
| 3191 | #if SSL_OP_NO_TLSv1_2 |
| 3192 | if (!ctx && conf_ssl_options & BC_SSL_O_USE_TLSV12) |
| 3193 | ctx = SSL_CTX_new(TLSv1_2_server_method()); |
| 3194 | #endif |
| 3195 | #if SSL_OP_NO_TLSv1_1 |
| 3196 | if (!ctx && conf_ssl_options & BC_SSL_O_USE_TLSV11) |
| 3197 | ctx = SSL_CTX_new(TLSv1_1_server_method()); |
| 3198 | #endif |
| 3199 | if (!ctx && conf_ssl_options & BC_SSL_O_USE_TLSV10) |
| 3200 | ctx = SSL_CTX_new(TLSv1_server_method()); |
| 3201 | #ifndef OPENSSL_NO_SSL3 |
| 3202 | if (!ctx && conf_ssl_options & BC_SSL_O_USE_SSLV3) |
| 3203 | ctx = SSL_CTX_new(SSLv3_server_method()); |
| 3204 | #endif |
| 3205 | if (!ctx) { |
| 3206 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 3207 | if (conf_ssl_options & BC_SSL_O_NO_SSLV3) |
| 3208 | ssloptions |= SSL_OP_NO_SSLv3; |
| 3209 | if (conf_ssl_options & BC_SSL_O_NO_TLSV10) |
| 3210 | ssloptions |= SSL_OP_NO_TLSv1; |
| 3211 | if (conf_ssl_options & BC_SSL_O_NO_TLSV11) |
| 3212 | ssloptions |= SSL_OP_NO_TLSv1_1; |
| 3213 | if (conf_ssl_options & BC_SSL_O_NO_TLSV12) |
| 3214 | ssloptions |= SSL_OP_NO_TLSv1_2; |
| 3215 | } |
| 3216 | if (conf_ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3217 | ssloptions |= SSL_OP_NO_TICKET; |
| 3218 | SSL_CTX_set_options(ctx, ssloptions); |
| 3219 | SSL_CTX_set_mode(ctx, sslmode); |
| 3220 | if (global_ssl.life_time) |
| 3221 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3222 | |
| 3223 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3224 | #ifdef OPENSSL_IS_BORINGSSL |
| 3225 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3226 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 3227 | #else |
| 3228 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 3229 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
| 3230 | #endif |
| 3231 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3232 | return ctx; |
| 3233 | } |
| 3234 | |
| 3235 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
| 3236 | { |
| 3237 | struct proxy *curproxy = bind_conf->frontend; |
| 3238 | int cfgerr = 0; |
| 3239 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3240 | struct ssl_bind_conf *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3241 | const char *conf_ciphers; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3242 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3243 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3244 | 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] | 3245 | case SSL_SOCK_VERIFY_NONE: |
| 3246 | verify = SSL_VERIFY_NONE; |
| 3247 | break; |
| 3248 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 3249 | verify = SSL_VERIFY_PEER; |
| 3250 | break; |
| 3251 | case SSL_SOCK_VERIFY_REQUIRED: |
| 3252 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 3253 | break; |
| 3254 | } |
| 3255 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 3256 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3257 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 3258 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 3259 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3260 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3261 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3262 | Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3263 | 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] | 3264 | cfgerr++; |
| 3265 | } |
| 3266 | /* set CA names fo client cert request, function returns void */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3267 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file)); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3268 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3269 | else { |
| 3270 | Alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 3271 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3272 | cfgerr++; |
| 3273 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 3274 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3275 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3276 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 3277 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3278 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3279 | Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3280 | 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] | 3281 | cfgerr++; |
| 3282 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 3283 | else { |
| 3284 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 3285 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3286 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 3287 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3288 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3289 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 3290 | #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] | 3291 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 3292 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
| 3293 | Alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 3294 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3295 | cfgerr++; |
| 3296 | } |
| 3297 | } |
| 3298 | #endif |
| 3299 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3300 | shared_context_set_cache(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3301 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 3302 | if (conf_ciphers && |
| 3303 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3304 | Alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3305 | 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] | 3306 | cfgerr++; |
| 3307 | } |
| 3308 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 3309 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3310 | /* If tune.ssl.default-dh-param has not been set, |
| 3311 | neither has ssl-default-dh-file and no static DH |
| 3312 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3313 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3314 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3315 | (ssl_dh_ptr_index == -1 || |
| 3316 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 3317 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 3318 | const SSL_CIPHER * cipher = NULL; |
| 3319 | char cipher_description[128]; |
| 3320 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 3321 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 3322 | which is not ephemeral DH. */ |
| 3323 | const char dhe_description[] = " Kx=DH "; |
| 3324 | const char dhe_export_description[] = " Kx=DH("; |
| 3325 | int idx = 0; |
| 3326 | int dhe_found = 0; |
| 3327 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 3328 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3329 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3330 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3331 | if (ssl) { |
| 3332 | ciphers = SSL_get_ciphers(ssl); |
| 3333 | |
| 3334 | if (ciphers) { |
| 3335 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 3336 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 3337 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 3338 | if (strstr(cipher_description, dhe_description) != NULL || |
| 3339 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 3340 | dhe_found = 1; |
| 3341 | break; |
| 3342 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 3343 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3344 | } |
| 3345 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3346 | SSL_free(ssl); |
| 3347 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 3348 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3349 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 3350 | if (dhe_found) { |
| 3351 | 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] | 3352 | } |
| 3353 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3354 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3355 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3356 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3357 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3358 | if (local_dh_1024 == NULL) { |
| 3359 | local_dh_1024 = ssl_get_dh_1024(); |
| 3360 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3361 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3362 | if (local_dh_2048 == NULL) { |
| 3363 | local_dh_2048 = ssl_get_dh_2048(); |
| 3364 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3365 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3366 | if (local_dh_4096 == NULL) { |
| 3367 | local_dh_4096 = ssl_get_dh_4096(); |
| 3368 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3369 | } |
| 3370 | } |
| 3371 | } |
| 3372 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3373 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3374 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 3375 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3376 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 3377 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3378 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3379 | #ifdef OPENSSL_NPN_NEGOTIATED |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3380 | ssl_conf_cur = NULL; |
| 3381 | if (ssl_conf && ssl_conf->npn_str) |
| 3382 | ssl_conf_cur = ssl_conf; |
| 3383 | else if (bind_conf->ssl_conf.npn_str) |
| 3384 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 3385 | if (ssl_conf_cur) |
| 3386 | 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] | 3387 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 3388 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3389 | ssl_conf_cur = NULL; |
| 3390 | if (ssl_conf && ssl_conf->alpn_str) |
| 3391 | ssl_conf_cur = ssl_conf; |
| 3392 | else if (bind_conf->ssl_conf.alpn_str) |
| 3393 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 3394 | if (ssl_conf_cur) |
| 3395 | 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] | 3396 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3397 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3398 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 3399 | if (conf_curves) { |
| 3400 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
| 3401 | Alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 3402 | curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3403 | cfgerr++; |
| 3404 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 3405 | #if defined(SSL_CTX_set_ecdh_auto) |
| 3406 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
| 3407 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3408 | } |
| 3409 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3410 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3411 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3412 | int i; |
| 3413 | EC_KEY *ecdh; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3414 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 3415 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3416 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3417 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3418 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
| 3419 | Alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3420 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3421 | cfgerr++; |
| 3422 | } |
| 3423 | else { |
| 3424 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 3425 | EC_KEY_free(ecdh); |
| 3426 | } |
| 3427 | } |
| 3428 | #endif |
| 3429 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3430 | return cfgerr; |
| 3431 | } |
| 3432 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3433 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 3434 | { |
| 3435 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 3436 | size_t prefixlen, suffixlen; |
| 3437 | |
| 3438 | /* Trivial case */ |
| 3439 | if (strcmp(pattern, hostname) == 0) |
| 3440 | return 1; |
| 3441 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3442 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 3443 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 3444 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 3445 | pattern_wildcard = NULL; |
| 3446 | pattern_left_label_end = pattern; |
| 3447 | while (*pattern_left_label_end != '.') { |
| 3448 | switch (*pattern_left_label_end) { |
| 3449 | case 0: |
| 3450 | /* End of label not found */ |
| 3451 | return 0; |
| 3452 | case '*': |
| 3453 | /* If there is more than one wildcards */ |
| 3454 | if (pattern_wildcard) |
| 3455 | return 0; |
| 3456 | pattern_wildcard = pattern_left_label_end; |
| 3457 | break; |
| 3458 | } |
| 3459 | pattern_left_label_end++; |
| 3460 | } |
| 3461 | |
| 3462 | /* If it's not trivial and there is no wildcard, it can't |
| 3463 | * match */ |
| 3464 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3465 | return 0; |
| 3466 | |
| 3467 | /* Make sure all labels match except the leftmost */ |
| 3468 | hostname_left_label_end = strchr(hostname, '.'); |
| 3469 | if (!hostname_left_label_end |
| 3470 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 3471 | return 0; |
| 3472 | |
| 3473 | /* Make sure the leftmost label of the hostname is long enough |
| 3474 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 3475 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3476 | return 0; |
| 3477 | |
| 3478 | /* Finally compare the string on either side of the |
| 3479 | * wildcard */ |
| 3480 | prefixlen = pattern_wildcard - pattern; |
| 3481 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 3482 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 3483 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3484 | return 0; |
| 3485 | |
| 3486 | return 1; |
| 3487 | } |
| 3488 | |
| 3489 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 3490 | { |
| 3491 | SSL *ssl; |
| 3492 | struct connection *conn; |
| 3493 | char *servername; |
| 3494 | |
| 3495 | int depth; |
| 3496 | X509 *cert; |
| 3497 | STACK_OF(GENERAL_NAME) *alt_names; |
| 3498 | int i; |
| 3499 | X509_NAME *cert_subject; |
| 3500 | char *str; |
| 3501 | |
| 3502 | if (ok == 0) |
| 3503 | return ok; |
| 3504 | |
| 3505 | ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3506 | conn = SSL_get_app_data(ssl); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3507 | |
| 3508 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
| 3509 | |
| 3510 | /* We only need to verify the CN on the actual server cert, |
| 3511 | * not the indirect CAs */ |
| 3512 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 3513 | if (depth != 0) |
| 3514 | return ok; |
| 3515 | |
| 3516 | /* At this point, the cert is *not* OK unless we can find a |
| 3517 | * hostname match */ |
| 3518 | ok = 0; |
| 3519 | |
| 3520 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 3521 | /* It seems like this might happen if verify peer isn't set */ |
| 3522 | if (!cert) |
| 3523 | return ok; |
| 3524 | |
| 3525 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 3526 | if (alt_names) { |
| 3527 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 3528 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 3529 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 3530 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 3531 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 3532 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3533 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 3534 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3535 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 3536 | OPENSSL_free(str); |
| 3537 | } |
| 3538 | } |
| 3539 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 3540 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | cert_subject = X509_get_subject_name(cert); |
| 3544 | i = -1; |
| 3545 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 3546 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3547 | ASN1_STRING *value; |
| 3548 | value = X509_NAME_ENTRY_get_data(entry); |
| 3549 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3550 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 3551 | OPENSSL_free(str); |
| 3552 | } |
| 3553 | } |
| 3554 | |
| 3555 | return ok; |
| 3556 | } |
| 3557 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3558 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3559 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3560 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3561 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3562 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 3563 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3564 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3565 | SSL_OP_NO_SSLv2 | |
| 3566 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 3567 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3568 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3569 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3570 | SSL_MODE_RELEASE_BUFFERS | |
| 3571 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3572 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3573 | SSL_CTX *ctx = NULL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3574 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3575 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 3576 | if (!ssl_initialize_random()) { |
| 3577 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 3578 | cfgerr++; |
| 3579 | } |
| 3580 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 3581 | /* Automatic memory computations need to know we use SSL there */ |
| 3582 | global.ssl_used_backend = 1; |
| 3583 | |
| 3584 | /* Initiate SSL context for current server */ |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3585 | srv->ssl_ctx.reused_sess = NULL; |
| 3586 | if (srv->use_ssl) |
| 3587 | srv->xprt = &ssl_sock; |
| 3588 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 3589 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3590 | |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3591 | #if SSL_OP_NO_TLSv1_2 |
| 3592 | if (!ctx && srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV12) |
| 3593 | ctx = SSL_CTX_new(TLSv1_2_client_method()); |
| 3594 | #endif |
| 3595 | #if SSL_OP_NO_TLSv1_1 |
| 3596 | if (!ctx && srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV11) |
| 3597 | ctx = SSL_CTX_new(TLSv1_1_client_method()); |
| 3598 | #endif |
| 3599 | if (!ctx && srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV10) |
| 3600 | ctx = SSL_CTX_new(TLSv1_client_method()); |
| 3601 | #ifndef OPENSSL_NO_SSL3 |
| 3602 | if (!ctx && srv->ssl_ctx.options & SRV_SSL_O_USE_SSLV3) |
| 3603 | ctx = SSL_CTX_new(SSLv3_client_method()); |
| 3604 | #endif |
| 3605 | if (!ctx) { |
| 3606 | ctx = SSL_CTX_new(SSLv23_client_method()); |
| 3607 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_SSLV3) |
| 3608 | options |= SSL_OP_NO_SSLv3; |
| 3609 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV10) |
| 3610 | options |= SSL_OP_NO_TLSv1; |
| 3611 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV11) |
| 3612 | options |= SSL_OP_NO_TLSv1_1; |
| 3613 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV12) |
| 3614 | options |= SSL_OP_NO_TLSv1_2; |
| 3615 | } |
| 3616 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 3617 | options |= SSL_OP_NO_TICKET; |
| 3618 | if (!ctx) { |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3619 | Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 3620 | proxy_type_str(curproxy), curproxy->id, |
| 3621 | srv->id); |
| 3622 | cfgerr++; |
| 3623 | return cfgerr; |
| 3624 | } |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3625 | SSL_CTX_set_options(ctx, options); |
| 3626 | SSL_CTX_set_mode(ctx, mode); |
| 3627 | srv->ssl_ctx.ctx = ctx; |
| 3628 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 3629 | if (srv->ssl_ctx.client_crt) { |
| 3630 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
| 3631 | Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 3632 | proxy_type_str(curproxy), curproxy->id, |
| 3633 | srv->id, srv->ssl_ctx.client_crt); |
| 3634 | cfgerr++; |
| 3635 | } |
| 3636 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
| 3637 | Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 3638 | proxy_type_str(curproxy), curproxy->id, |
| 3639 | srv->id, srv->ssl_ctx.client_crt); |
| 3640 | cfgerr++; |
| 3641 | } |
| 3642 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
| 3643 | Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3644 | proxy_type_str(curproxy), curproxy->id, |
| 3645 | srv->id, srv->ssl_ctx.client_crt); |
| 3646 | cfgerr++; |
| 3647 | } |
| 3648 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3649 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3650 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 3651 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3652 | switch (srv->ssl_ctx.verify) { |
| 3653 | case SSL_SOCK_VERIFY_NONE: |
| 3654 | verify = SSL_VERIFY_NONE; |
| 3655 | break; |
| 3656 | case SSL_SOCK_VERIFY_REQUIRED: |
| 3657 | verify = SSL_VERIFY_PEER; |
| 3658 | break; |
| 3659 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3660 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3661 | verify, |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3662 | srv->ssl_ctx.verify_host ? ssl_sock_srv_verifycbk : NULL); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3663 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3664 | if (srv->ssl_ctx.ca_file) { |
| 3665 | /* load CAfile to verify */ |
| 3666 | if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) { |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 3667 | Alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n", |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3668 | curproxy->id, srv->id, |
| 3669 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
| 3670 | cfgerr++; |
| 3671 | } |
| 3672 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3673 | else { |
| 3674 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 3675 | Alert("Proxy '%s', server '%s' [%s:%d] verify is enabled by default but no CA file specified. If you're running on a LAN where you're certain to trust the server's certificate, please set an explicit 'verify none' statement on the 'server' line, or use 'ssl-server-verify none' in the global section to disable server-side verifications by default.\n", |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3676 | curproxy->id, srv->id, |
| 3677 | srv->conf.file, srv->conf.line); |
| 3678 | else |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 3679 | Alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3680 | curproxy->id, srv->id, |
| 3681 | srv->conf.file, srv->conf.line); |
| 3682 | cfgerr++; |
| 3683 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3684 | #ifdef X509_V_FLAG_CRL_CHECK |
| 3685 | if (srv->ssl_ctx.crl_file) { |
| 3686 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 3687 | |
| 3688 | if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) { |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 3689 | Alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3690 | curproxy->id, srv->id, |
| 3691 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
| 3692 | cfgerr++; |
| 3693 | } |
| 3694 | else { |
| 3695 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 3696 | } |
| 3697 | } |
| 3698 | #endif |
| 3699 | } |
| 3700 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3701 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF); |
| 3702 | if (srv->ssl_ctx.ciphers && |
| 3703 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
| 3704 | Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 3705 | curproxy->id, srv->id, |
| 3706 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
| 3707 | cfgerr++; |
| 3708 | } |
| 3709 | |
| 3710 | return cfgerr; |
| 3711 | } |
| 3712 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3713 | /* 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] | 3714 | * be NULL, in which case nothing is done. Returns the number of errors |
| 3715 | * encountered. |
| 3716 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3717 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3718 | { |
| 3719 | struct ebmb_node *node; |
| 3720 | struct sni_ctx *sni; |
| 3721 | int err = 0; |
| 3722 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3723 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3724 | return 0; |
| 3725 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 3726 | /* Automatic memory computations need to know we use SSL there */ |
| 3727 | global.ssl_used_frontend = 1; |
| 3728 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3729 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 3730 | if (!ssl_initialize_random()) { |
| 3731 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 3732 | err++; |
| 3733 | } |
| 3734 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 3735 | if (!bind_conf->initial_ctx) { |
| 3736 | bind_conf->initial_ctx = ssl_sock_initial_ctx(bind_conf); |
| 3737 | /* It should not be necessary to call this function, but it's |
| 3738 | necessary first to check and move all initialisation related |
| 3739 | to initial_ctx in ssl_sock_initial_ctx. */ |
| 3740 | err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx); |
| 3741 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 3742 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3743 | 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] | 3744 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3745 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3746 | while (node) { |
| 3747 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 3748 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 3749 | /* only initialize the CTX on its first occurrence and |
| 3750 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3751 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3752 | node = ebmb_next(node); |
| 3753 | } |
| 3754 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3755 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3756 | while (node) { |
| 3757 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 3758 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 3759 | /* only initialize the CTX on its first occurrence and |
| 3760 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3761 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3762 | node = ebmb_next(node); |
| 3763 | } |
| 3764 | return err; |
| 3765 | } |
| 3766 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 3767 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 3768 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 3769 | * alerts are directly emitted since the rest of the stack does it below. |
| 3770 | */ |
| 3771 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 3772 | { |
| 3773 | struct proxy *px = bind_conf->frontend; |
| 3774 | int alloc_ctx; |
| 3775 | int err; |
| 3776 | |
| 3777 | if (!bind_conf->is_ssl) { |
| 3778 | if (bind_conf->default_ctx) { |
| 3779 | Warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 3780 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3781 | } |
| 3782 | return 0; |
| 3783 | } |
| 3784 | if (!bind_conf->default_ctx) { |
| 3785 | Alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 3786 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3787 | return -1; |
| 3788 | } |
| 3789 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3790 | alloc_ctx = shared_context_init(global.tune.sslcachesize, (!global_ssl.private_cache && (global.nbproc > 1)) ? 1 : 0); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 3791 | if (alloc_ctx < 0) { |
| 3792 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 3793 | 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"); |
| 3794 | else |
| 3795 | Alert("Unable to allocate SSL session cache.\n"); |
| 3796 | return -1; |
| 3797 | } |
| 3798 | |
| 3799 | err = 0; |
| 3800 | /* initialize all certificate contexts */ |
| 3801 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 3802 | |
| 3803 | /* initialize CA variables if the certificates generation is enabled */ |
| 3804 | err += ssl_sock_load_ca(bind_conf); |
| 3805 | |
| 3806 | return -err; |
| 3807 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 3808 | |
| 3809 | /* release ssl context allocated for servers. */ |
| 3810 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 3811 | { |
| 3812 | if (srv->ssl_ctx.ctx) |
| 3813 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 3814 | } |
| 3815 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3816 | /* 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] | 3817 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 3818 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3819 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3820 | { |
| 3821 | struct ebmb_node *node, *back; |
| 3822 | struct sni_ctx *sni; |
| 3823 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3824 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3825 | return; |
| 3826 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3827 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3828 | while (node) { |
| 3829 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 3830 | back = ebmb_next(node); |
| 3831 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3832 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3833 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3834 | ssl_sock_free_ssl_conf(sni->conf); |
| 3835 | free(sni->conf); |
| 3836 | sni->conf = NULL; |
| 3837 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3838 | free(sni); |
| 3839 | node = back; |
| 3840 | } |
| 3841 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3842 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3843 | while (node) { |
| 3844 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 3845 | back = ebmb_next(node); |
| 3846 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3847 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3848 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3849 | ssl_sock_free_ssl_conf(sni->conf); |
| 3850 | free(sni->conf); |
| 3851 | sni->conf = NULL; |
| 3852 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3853 | free(sni); |
| 3854 | node = back; |
| 3855 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3856 | SSL_CTX_free(bind_conf->initial_ctx); |
| 3857 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3858 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3859 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3860 | } |
| 3861 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3862 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 3863 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 3864 | { |
| 3865 | ssl_sock_free_ca(bind_conf); |
| 3866 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3867 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3868 | free(bind_conf->ca_sign_file); |
| 3869 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3870 | if (bind_conf->keys_ref) { |
| 3871 | free(bind_conf->keys_ref->filename); |
| 3872 | free(bind_conf->keys_ref->tlskeys); |
| 3873 | LIST_DEL(&bind_conf->keys_ref->list); |
| 3874 | free(bind_conf->keys_ref); |
| 3875 | } |
| 3876 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3877 | bind_conf->ca_sign_pass = NULL; |
| 3878 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3879 | } |
| 3880 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3881 | /* Load CA cert file and private key used to generate certificates */ |
| 3882 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3883 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3884 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3885 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3886 | FILE *fp; |
| 3887 | X509 *cacert = NULL; |
| 3888 | EVP_PKEY *capkey = NULL; |
| 3889 | int err = 0; |
| 3890 | |
| 3891 | if (!bind_conf || !bind_conf->generate_certs) |
| 3892 | return err; |
| 3893 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3894 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3895 | if (global_ssl.ctx_cache) |
| 3896 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 3897 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 3898 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 3899 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3900 | if (!bind_conf->ca_sign_file) { |
| 3901 | Alert("Proxy '%s': cannot enable certificate generation, " |
| 3902 | "no CA certificate File configured at [%s:%d].\n", |
| 3903 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3904 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3905 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3906 | |
| 3907 | /* read in the CA certificate */ |
| 3908 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
| 3909 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 3910 | 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] | 3911 | goto load_error; |
| 3912 | } |
| 3913 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
| 3914 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 3915 | 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] | 3916 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3917 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3918 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3919 | if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) { |
| 3920 | Alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 3921 | 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] | 3922 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3923 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3924 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3925 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3926 | bind_conf->ca_sign_cert = cacert; |
| 3927 | bind_conf->ca_sign_pkey = capkey; |
| 3928 | return err; |
| 3929 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3930 | read_error: |
| 3931 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3932 | if (capkey) EVP_PKEY_free(capkey); |
| 3933 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3934 | load_error: |
| 3935 | bind_conf->generate_certs = 0; |
| 3936 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3937 | return err; |
| 3938 | } |
| 3939 | |
| 3940 | /* Release CA cert and private key used to generate certificated */ |
| 3941 | void |
| 3942 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 3943 | { |
| 3944 | if (!bind_conf) |
| 3945 | return; |
| 3946 | |
| 3947 | if (bind_conf->ca_sign_pkey) |
| 3948 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 3949 | if (bind_conf->ca_sign_cert) |
| 3950 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 3951 | bind_conf->ca_sign_pkey = NULL; |
| 3952 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3953 | } |
| 3954 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3955 | /* |
| 3956 | * This function is called if SSL * context is not yet allocated. The function |
| 3957 | * is designed to be called before any other data-layer operation and sets the |
| 3958 | * handshake flag on the connection. It is safe to call it multiple times. |
| 3959 | * It returns 0 on success and -1 in error case. |
| 3960 | */ |
| 3961 | static int ssl_sock_init(struct connection *conn) |
| 3962 | { |
| 3963 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3964 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3965 | return 0; |
| 3966 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3967 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 3968 | return 0; |
| 3969 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3970 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 3971 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3972 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3973 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3974 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3975 | /* If it is in client mode initiate SSL session |
| 3976 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3977 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3978 | int may_retry = 1; |
| 3979 | |
| 3980 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3981 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3982 | conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3983 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3984 | if (may_retry--) { |
| 3985 | pool_gc2(); |
| 3986 | goto retry_connect; |
| 3987 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3988 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3989 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3990 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3991 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3992 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3993 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 3994 | SSL_free(conn->xprt_ctx); |
| 3995 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3996 | if (may_retry--) { |
| 3997 | pool_gc2(); |
| 3998 | goto retry_connect; |
| 3999 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4000 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4001 | return -1; |
| 4002 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4003 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4004 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4005 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 4006 | SSL_free(conn->xprt_ctx); |
| 4007 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4008 | if (may_retry--) { |
| 4009 | pool_gc2(); |
| 4010 | goto retry_connect; |
| 4011 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4012 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4013 | return -1; |
| 4014 | } |
| 4015 | |
| 4016 | SSL_set_connect_state(conn->xprt_ctx); |
| 4017 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 4018 | if(!SSL_set_session(conn->xprt_ctx, objt_server(conn->target)->ssl_ctx.reused_sess)) { |
| 4019 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 4020 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 4021 | } |
| 4022 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4023 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4024 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4025 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4026 | |
| 4027 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 4028 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4029 | return 0; |
| 4030 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4031 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4032 | int may_retry = 1; |
| 4033 | |
| 4034 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4035 | /* Alloc a new SSL session ctx */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4036 | 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] | 4037 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4038 | if (may_retry--) { |
| 4039 | pool_gc2(); |
| 4040 | goto retry_accept; |
| 4041 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4042 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4043 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4044 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4045 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4046 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4047 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 4048 | SSL_free(conn->xprt_ctx); |
| 4049 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4050 | if (may_retry--) { |
| 4051 | pool_gc2(); |
| 4052 | goto retry_accept; |
| 4053 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4054 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4055 | return -1; |
| 4056 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4057 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4058 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4059 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 4060 | SSL_free(conn->xprt_ctx); |
| 4061 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4062 | if (may_retry--) { |
| 4063 | pool_gc2(); |
| 4064 | goto retry_accept; |
| 4065 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4066 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4067 | return -1; |
| 4068 | } |
| 4069 | |
| 4070 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4071 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4072 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4073 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4074 | |
| 4075 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 4076 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4077 | return 0; |
| 4078 | } |
| 4079 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4080 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4081 | return -1; |
| 4082 | } |
| 4083 | |
| 4084 | |
| 4085 | /* This is the callback which is used when an SSL handshake is pending. It |
| 4086 | * updates the FD status if it wants some polling before being called again. |
| 4087 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 4088 | * otherwise it returns non-zero and removes itself from the connection's |
| 4089 | * flags (the bit is provided in <flag> by the caller). |
| 4090 | */ |
| 4091 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 4092 | { |
| 4093 | int ret; |
| 4094 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4095 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4096 | return 0; |
| 4097 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4098 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4099 | goto out_error; |
| 4100 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4101 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 4102 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 4103 | * Usually SSL_write and SSL_read are used and process implicitly |
| 4104 | * the reneg handshake. |
| 4105 | * Here we use SSL_peek as a workaround for reneg. |
| 4106 | */ |
| 4107 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4108 | char c; |
| 4109 | |
| 4110 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 4111 | if (ret <= 0) { |
| 4112 | /* handshake may have not been completed, let's find why */ |
| 4113 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 4114 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 4115 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 4116 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4117 | __conn_sock_want_send(conn); |
| 4118 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4119 | return 0; |
| 4120 | } |
| 4121 | else if (ret == SSL_ERROR_WANT_READ) { |
| 4122 | /* handshake may have been completed but we have |
| 4123 | * no more data to read. |
| 4124 | */ |
| 4125 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4126 | ret = 1; |
| 4127 | goto reneg_ok; |
| 4128 | } |
| 4129 | /* SSL handshake needs to read, L4 connection is ready */ |
| 4130 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 4131 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4132 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4133 | __conn_sock_want_recv(conn); |
| 4134 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4135 | return 0; |
| 4136 | } |
| 4137 | else if (ret == SSL_ERROR_SYSCALL) { |
| 4138 | /* if errno is null, then connection was successfully established */ |
| 4139 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 4140 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4141 | if (!conn->err_code) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4142 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 4143 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4144 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4145 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 4146 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4147 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 4148 | empty_handshake = state == TLS_ST_BEFORE; |
| 4149 | #else |
| 4150 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
| 4151 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4152 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4153 | if (!errno) { |
| 4154 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4155 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4156 | else |
| 4157 | conn->err_code = CO_ER_SSL_EMPTY; |
| 4158 | } |
| 4159 | else { |
| 4160 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4161 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4162 | else |
| 4163 | conn->err_code = CO_ER_SSL_ABORT; |
| 4164 | } |
| 4165 | } |
| 4166 | else { |
| 4167 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4168 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4169 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4170 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4171 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4172 | #endif |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4173 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4174 | goto out_error; |
| 4175 | } |
| 4176 | else { |
| 4177 | /* Fail on all other handshake errors */ |
| 4178 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 4179 | * buffer, causing an RST to be emitted upon close() on |
| 4180 | * TCP sockets. We first try to drain possibly pending |
| 4181 | * data to avoid this as much as possible. |
| 4182 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 4183 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4184 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 4185 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 4186 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4187 | goto out_error; |
| 4188 | } |
| 4189 | } |
| 4190 | /* read some data: consider handshake completed */ |
| 4191 | goto reneg_ok; |
| 4192 | } |
| 4193 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4194 | ret = SSL_do_handshake(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4195 | if (ret != 1) { |
| 4196 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4197 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4198 | |
| 4199 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 4200 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 4201 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4202 | __conn_sock_want_send(conn); |
| 4203 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4204 | return 0; |
| 4205 | } |
| 4206 | else if (ret == SSL_ERROR_WANT_READ) { |
| 4207 | /* SSL handshake needs to read, L4 connection is ready */ |
| 4208 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 4209 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4210 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4211 | __conn_sock_want_recv(conn); |
| 4212 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4213 | return 0; |
| 4214 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 4215 | else if (ret == SSL_ERROR_SYSCALL) { |
| 4216 | /* if errno is null, then connection was successfully established */ |
| 4217 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 4218 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4219 | if (!conn->err_code) { |
| 4220 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 4221 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4222 | #else |
| 4223 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 4224 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4225 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 4226 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4227 | #else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4228 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4229 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4230 | if (empty_handshake) { |
| 4231 | if (!errno) { |
| 4232 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4233 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4234 | else |
| 4235 | conn->err_code = CO_ER_SSL_EMPTY; |
| 4236 | } |
| 4237 | else { |
| 4238 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4239 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4240 | else |
| 4241 | conn->err_code = CO_ER_SSL_ABORT; |
| 4242 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4243 | } |
| 4244 | else { |
| 4245 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4246 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4247 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4248 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4249 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4250 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4251 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 4252 | goto out_error; |
| 4253 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4254 | else { |
| 4255 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 4256 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 4257 | * buffer, causing an RST to be emitted upon close() on |
| 4258 | * TCP sockets. We first try to drain possibly pending |
| 4259 | * data to avoid this as much as possible. |
| 4260 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 4261 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4262 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 4263 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 4264 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4265 | goto out_error; |
| 4266 | } |
| 4267 | } |
| 4268 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4269 | reneg_ok: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4270 | /* Handshake succeeded */ |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 4271 | if (!SSL_session_reused(conn->xprt_ctx)) { |
| 4272 | if (objt_server(conn->target)) { |
| 4273 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 4274 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 4275 | global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr; |
| 4276 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4277 | /* check if session was reused, if not store current session on server for reuse */ |
Willy Tarreau | 30fd4bd | 2016-12-22 21:54:21 +0100 | [diff] [blame] | 4278 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4279 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
Willy Tarreau | 30fd4bd | 2016-12-22 21:54:21 +0100 | [diff] [blame] | 4280 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 4281 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4282 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 4283 | if (!(objt_server(conn->target)->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) |
| 4284 | objt_server(conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4285 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 4286 | else { |
| 4287 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 4288 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 4289 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 4290 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4291 | } |
| 4292 | |
| 4293 | /* The connection is now established at both layers, it's time to leave */ |
| 4294 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 4295 | return 1; |
| 4296 | |
| 4297 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4298 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4299 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4300 | ERR_clear_error(); |
| 4301 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 4302 | /* free resumed session if exists */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4303 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 4304 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 4305 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4308 | /* Fail on all other handshake errors */ |
| 4309 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4310 | if (!conn->err_code) |
| 4311 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4312 | return 0; |
| 4313 | } |
| 4314 | |
| 4315 | /* 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] | 4316 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4317 | * buffer wraps, in which case a second call may be performed. The connection's |
| 4318 | * flags are updated with whatever special event is detected (error, read0, |
| 4319 | * empty). The caller is responsible for taking care of those events and |
| 4320 | * avoiding the call if inappropriate. The function does not call the |
| 4321 | * connection's polling update function, so the caller is responsible for this. |
| 4322 | */ |
| 4323 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 4324 | { |
| 4325 | int ret, done = 0; |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 4326 | int try; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4327 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4328 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4329 | goto out_error; |
| 4330 | |
| 4331 | if (conn->flags & CO_FL_HANDSHAKE) |
| 4332 | /* a handshake was requested */ |
| 4333 | return 0; |
| 4334 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 4335 | /* let's realign the buffer to optimize I/O */ |
| 4336 | if (buffer_empty(buf)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4337 | buf->p = buf->data; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4338 | |
| 4339 | /* read the largest possible block. For this, we perform only one call |
| 4340 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 4341 | * in which case we accept to do it once again. A new attempt is made on |
| 4342 | * EINTR too. |
| 4343 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 4344 | while (count > 0) { |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 4345 | /* first check if we have some room after p+i */ |
| 4346 | try = buf->data + buf->size - (buf->p + buf->i); |
| 4347 | /* otherwise continue between data and p-o */ |
| 4348 | if (try <= 0) { |
| 4349 | try = buf->p - (buf->data + buf->o); |
| 4350 | if (try <= 0) |
| 4351 | break; |
| 4352 | } |
| 4353 | if (try > count) |
| 4354 | try = count; |
| 4355 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4356 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4357 | if (conn->flags & CO_FL_ERROR) { |
| 4358 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4359 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4360 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4361 | if (ret > 0) { |
| 4362 | buf->i += ret; |
| 4363 | done += ret; |
| 4364 | if (ret < try) |
| 4365 | break; |
| 4366 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4367 | } |
| 4368 | else if (ret == 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4369 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 4370 | if (ret != SSL_ERROR_ZERO_RETURN) { |
Emeric Brun | 1c64686 | 2012-12-14 12:33:41 +0100 | [diff] [blame] | 4371 | /* error on protocol or underlying transport */ |
| 4372 | if ((ret != SSL_ERROR_SYSCALL) |
| 4373 | || (errno && (errno != EAGAIN))) |
| 4374 | conn->flags |= CO_FL_ERROR; |
| 4375 | |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4376 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4377 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4378 | ERR_clear_error(); |
| 4379 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4380 | goto read0; |
| 4381 | } |
| 4382 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4383 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4384 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4385 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4386 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4387 | __conn_sock_want_send(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4388 | break; |
| 4389 | } |
| 4390 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 4391 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4392 | /* handshake is running, and it may need to re-enable read */ |
| 4393 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 4394 | __conn_sock_want_recv(conn); |
| 4395 | break; |
| 4396 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4397 | /* we need to poll for retry a read later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4398 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4399 | break; |
| 4400 | } |
| 4401 | /* otherwise it's a real error */ |
| 4402 | goto out_error; |
| 4403 | } |
| 4404 | } |
| 4405 | return done; |
| 4406 | |
| 4407 | read0: |
| 4408 | conn_sock_read0(conn); |
| 4409 | return done; |
| 4410 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4411 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4412 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4413 | ERR_clear_error(); |
| 4414 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4415 | conn->flags |= CO_FL_ERROR; |
| 4416 | return done; |
| 4417 | } |
| 4418 | |
| 4419 | |
| 4420 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 4421 | * <flags> may contain some CO_SFL_* flags to hint the system about other |
| 4422 | * pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4423 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 4424 | * a second call may be performed. The connection's flags are updated with |
| 4425 | * whatever special event is detected (error, empty). The caller is responsible |
| 4426 | * for taking care of those events and avoiding the call if inappropriate. The |
| 4427 | * function does not call the connection's polling update function, so the caller |
| 4428 | * is responsible for this. |
| 4429 | */ |
| 4430 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 4431 | { |
| 4432 | int ret, try, done; |
| 4433 | |
| 4434 | done = 0; |
| 4435 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4436 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4437 | goto out_error; |
| 4438 | |
| 4439 | if (conn->flags & CO_FL_HANDSHAKE) |
| 4440 | /* a handshake was requested */ |
| 4441 | return 0; |
| 4442 | |
| 4443 | /* send the largest possible block. For this we perform only one call |
| 4444 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 4445 | * in which case we accept to do it once again. |
| 4446 | */ |
| 4447 | while (buf->o) { |
Kevin Hester | cad8234 | 2013-05-30 15:12:41 -0700 | [diff] [blame] | 4448 | try = bo_contig_data(buf); |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 4449 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 4450 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4451 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4452 | global_ssl.max_record && try > global_ssl.max_record) { |
| 4453 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4454 | } |
| 4455 | else { |
| 4456 | /* we need to keep the information about the fact that |
| 4457 | * we're not limiting the upcoming send(), because if it |
| 4458 | * fails, we'll have to retry with at least as many data. |
| 4459 | */ |
| 4460 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 4461 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 4462 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4463 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4464 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4465 | if (conn->flags & CO_FL_ERROR) { |
| 4466 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4467 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4468 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4469 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4470 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
| 4471 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4472 | buf->o -= ret; |
| 4473 | done += ret; |
| 4474 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 4475 | if (likely(buffer_empty(buf))) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4476 | /* optimize data alignment in the buffer */ |
| 4477 | buf->p = buf->data; |
| 4478 | |
| 4479 | /* if the system buffer is full, don't insist */ |
| 4480 | if (ret < try) |
| 4481 | break; |
| 4482 | } |
| 4483 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4484 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4485 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 4486 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4487 | /* handshake is running, and it may need to re-enable write */ |
| 4488 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 4489 | __conn_sock_want_send(conn); |
| 4490 | break; |
| 4491 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4492 | /* we need to poll to retry a write later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4493 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4494 | break; |
| 4495 | } |
| 4496 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4497 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4498 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4499 | __conn_sock_want_recv(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4500 | break; |
| 4501 | } |
| 4502 | goto out_error; |
| 4503 | } |
| 4504 | } |
| 4505 | return done; |
| 4506 | |
| 4507 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4508 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4509 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4510 | ERR_clear_error(); |
| 4511 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4512 | conn->flags |= CO_FL_ERROR; |
| 4513 | return done; |
| 4514 | } |
| 4515 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4516 | static void ssl_sock_close(struct connection *conn) { |
| 4517 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4518 | if (conn->xprt_ctx) { |
| 4519 | SSL_free(conn->xprt_ctx); |
| 4520 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4521 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4522 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 4526 | * any case, flags the connection as reusable if no handshake was in progress. |
| 4527 | */ |
| 4528 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 4529 | { |
| 4530 | if (conn->flags & CO_FL_HANDSHAKE) |
| 4531 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 4532 | if (!clean) |
| 4533 | /* don't sent notify on SSL_shutdown */ |
Willy Tarreau | e3cc3a3 | 2017-02-13 11:12:29 +0100 | [diff] [blame] | 4534 | SSL_set_quiet_shutdown(conn->xprt_ctx, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4535 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 4536 | if (SSL_shutdown(conn->xprt_ctx) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4537 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4538 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4539 | ERR_clear_error(); |
| 4540 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4541 | } |
| 4542 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 4543 | /* used for logging, may be changed for a sample fetch later */ |
| 4544 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 4545 | { |
| 4546 | if (!conn->xprt && !conn->xprt_ctx) |
| 4547 | return NULL; |
| 4548 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 4549 | } |
| 4550 | |
| 4551 | /* used for logging, may be changed for a sample fetch later */ |
| 4552 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 4553 | { |
| 4554 | if (!conn->xprt && !conn->xprt_ctx) |
| 4555 | return NULL; |
| 4556 | return SSL_get_version(conn->xprt_ctx); |
| 4557 | } |
| 4558 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4559 | /* Extract a serial from a cert, and copy it to a chunk. |
| 4560 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 4561 | * -1 if output is not large enough. |
| 4562 | */ |
| 4563 | static int |
| 4564 | ssl_sock_get_serial(X509 *crt, struct chunk *out) |
| 4565 | { |
| 4566 | ASN1_INTEGER *serial; |
| 4567 | |
| 4568 | serial = X509_get_serialNumber(crt); |
| 4569 | if (!serial) |
| 4570 | return 0; |
| 4571 | |
| 4572 | if (out->size < serial->length) |
| 4573 | return -1; |
| 4574 | |
| 4575 | memcpy(out->str, serial->data, serial->length); |
| 4576 | out->len = serial->length; |
| 4577 | return 1; |
| 4578 | } |
| 4579 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4580 | /* Extract a cert to der, and copy it to a chunk. |
| 4581 | * Returns 1 if cert is found and copied, 0 on der convertion failure and |
| 4582 | * -1 if output is not large enough. |
| 4583 | */ |
| 4584 | static int |
| 4585 | ssl_sock_crt2der(X509 *crt, struct chunk *out) |
| 4586 | { |
| 4587 | int len; |
| 4588 | unsigned char *p = (unsigned char *)out->str;; |
| 4589 | |
| 4590 | len =i2d_X509(crt, NULL); |
| 4591 | if (len <= 0) |
| 4592 | return 1; |
| 4593 | |
| 4594 | if (out->size < len) |
| 4595 | return -1; |
| 4596 | |
| 4597 | i2d_X509(crt,&p); |
| 4598 | out->len = len; |
| 4599 | return 1; |
| 4600 | } |
| 4601 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4602 | |
| 4603 | /* Copy Date in ASN1_UTCTIME format in struct chunk out. |
| 4604 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 4605 | * and -1 if output is not large enough. |
| 4606 | */ |
| 4607 | static int |
| 4608 | ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out) |
| 4609 | { |
| 4610 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 4611 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 4612 | |
| 4613 | if (gentm->length < 12) |
| 4614 | return 0; |
| 4615 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 4616 | return 0; |
| 4617 | if (out->size < gentm->length-2) |
| 4618 | return -1; |
| 4619 | |
| 4620 | memcpy(out->str, gentm->data+2, gentm->length-2); |
| 4621 | out->len = gentm->length-2; |
| 4622 | return 1; |
| 4623 | } |
| 4624 | else if (tm->type == V_ASN1_UTCTIME) { |
| 4625 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 4626 | |
| 4627 | if (utctm->length < 10) |
| 4628 | return 0; |
| 4629 | if (utctm->data[0] >= 0x35) |
| 4630 | return 0; |
| 4631 | if (out->size < utctm->length) |
| 4632 | return -1; |
| 4633 | |
| 4634 | memcpy(out->str, utctm->data, utctm->length); |
| 4635 | out->len = utctm->length; |
| 4636 | return 1; |
| 4637 | } |
| 4638 | |
| 4639 | return 0; |
| 4640 | } |
| 4641 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4642 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 4643 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 4644 | */ |
| 4645 | static int |
| 4646 | ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out) |
| 4647 | { |
| 4648 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4649 | ASN1_OBJECT *obj; |
| 4650 | ASN1_STRING *data; |
| 4651 | const unsigned char *data_ptr; |
| 4652 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4653 | int i, j, n; |
| 4654 | int cur = 0; |
| 4655 | const char *s; |
| 4656 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4657 | int name_count; |
| 4658 | |
| 4659 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4660 | |
| 4661 | out->len = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4662 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4663 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4664 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4665 | else |
| 4666 | j = i; |
| 4667 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4668 | ne = X509_NAME_get_entry(a, j); |
| 4669 | obj = X509_NAME_ENTRY_get_object(ne); |
| 4670 | data = X509_NAME_ENTRY_get_data(ne); |
| 4671 | data_ptr = ASN1_STRING_get0_data(data); |
| 4672 | data_len = ASN1_STRING_length(data); |
| 4673 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4674 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4675 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4676 | s = tmp; |
| 4677 | } |
| 4678 | |
| 4679 | if (chunk_strcasecmp(entry, s) != 0) |
| 4680 | continue; |
| 4681 | |
| 4682 | if (pos < 0) |
| 4683 | cur--; |
| 4684 | else |
| 4685 | cur++; |
| 4686 | |
| 4687 | if (cur != pos) |
| 4688 | continue; |
| 4689 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4690 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4691 | return -1; |
| 4692 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4693 | memcpy(out->str, data_ptr, data_len); |
| 4694 | out->len = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4695 | return 1; |
| 4696 | } |
| 4697 | |
| 4698 | return 0; |
| 4699 | |
| 4700 | } |
| 4701 | |
| 4702 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 4703 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 4704 | */ |
| 4705 | static int |
| 4706 | ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out) |
| 4707 | { |
| 4708 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4709 | ASN1_OBJECT *obj; |
| 4710 | ASN1_STRING *data; |
| 4711 | const unsigned char *data_ptr; |
| 4712 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4713 | int i, n, ln; |
| 4714 | int l = 0; |
| 4715 | const char *s; |
| 4716 | char *p; |
| 4717 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4718 | int name_count; |
| 4719 | |
| 4720 | |
| 4721 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4722 | |
| 4723 | out->len = 0; |
| 4724 | p = out->str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4725 | for (i = 0; i < name_count; i++) { |
| 4726 | ne = X509_NAME_get_entry(a, i); |
| 4727 | obj = X509_NAME_ENTRY_get_object(ne); |
| 4728 | data = X509_NAME_ENTRY_get_data(ne); |
| 4729 | data_ptr = ASN1_STRING_get0_data(data); |
| 4730 | data_len = ASN1_STRING_length(data); |
| 4731 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4732 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4733 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4734 | s = tmp; |
| 4735 | } |
| 4736 | ln = strlen(s); |
| 4737 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4738 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4739 | if (l > out->size) |
| 4740 | return -1; |
| 4741 | out->len = l; |
| 4742 | |
| 4743 | *(p++)='/'; |
| 4744 | memcpy(p, s, ln); |
| 4745 | p += ln; |
| 4746 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4747 | memcpy(p, data_ptr, data_len); |
| 4748 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4749 | } |
| 4750 | |
| 4751 | if (!out->len) |
| 4752 | return 0; |
| 4753 | |
| 4754 | return 1; |
| 4755 | } |
| 4756 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4757 | char *ssl_sock_get_version(struct connection *conn) |
| 4758 | { |
| 4759 | if (!ssl_sock_is_ssl(conn)) |
| 4760 | return NULL; |
| 4761 | |
| 4762 | return (char *)SSL_get_version(conn->xprt_ctx); |
| 4763 | } |
| 4764 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 4765 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 4766 | * to disable SNI. |
| 4767 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 4768 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 4769 | { |
| 4770 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 4771 | char *prev_name; |
| 4772 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 4773 | if (!ssl_sock_is_ssl(conn)) |
| 4774 | return; |
| 4775 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 4776 | /* if the SNI changes, we must destroy the reusable context so that a |
| 4777 | * new connection will present a new SNI. As an optimization we could |
| 4778 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 4779 | * server. |
| 4780 | */ |
| 4781 | prev_name = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 4782 | if ((!prev_name && hostname) || |
| 4783 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
| 4784 | SSL_set_session(conn->xprt_ctx, NULL); |
| 4785 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 4786 | SSL_set_tlsext_host_name(conn->xprt_ctx, hostname); |
| 4787 | #endif |
| 4788 | } |
| 4789 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4790 | /* Extract peer certificate's common name into the chunk dest |
| 4791 | * Returns |
| 4792 | * the len of the extracted common name |
| 4793 | * or 0 if no CN found in DN |
| 4794 | * or -1 on error case (i.e. no peer certificate) |
| 4795 | */ |
| 4796 | int ssl_sock_get_remote_common_name(struct connection *conn, struct chunk *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4797 | { |
| 4798 | X509 *crt = NULL; |
| 4799 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4800 | const char find_cn[] = "CN"; |
| 4801 | const struct chunk find_cn_chunk = { |
| 4802 | .str = (char *)&find_cn, |
| 4803 | .len = sizeof(find_cn)-1 |
| 4804 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4805 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4806 | |
| 4807 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4808 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4809 | |
| 4810 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4811 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4812 | if (!crt) |
| 4813 | goto out; |
| 4814 | |
| 4815 | name = X509_get_subject_name(crt); |
| 4816 | if (!name) |
| 4817 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4818 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4819 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 4820 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4821 | if (crt) |
| 4822 | X509_free(crt); |
| 4823 | |
| 4824 | return result; |
| 4825 | } |
| 4826 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 4827 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 4828 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 4829 | { |
| 4830 | X509 *crt = NULL; |
| 4831 | |
| 4832 | if (!ssl_sock_is_ssl(conn)) |
| 4833 | return 0; |
| 4834 | |
| 4835 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4836 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4837 | if (!crt) |
| 4838 | return 0; |
| 4839 | |
| 4840 | X509_free(crt); |
| 4841 | return 1; |
| 4842 | } |
| 4843 | |
| 4844 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 4845 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4846 | { |
| 4847 | if (!ssl_sock_is_ssl(conn)) |
| 4848 | return 0; |
| 4849 | |
| 4850 | return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
| 4851 | } |
| 4852 | |
| 4853 | /* returns result from SSL verify */ |
| 4854 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 4855 | { |
| 4856 | if (!ssl_sock_is_ssl(conn)) |
| 4857 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 4858 | |
| 4859 | return (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
| 4860 | } |
| 4861 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 4862 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 4863 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4864 | /* boolean, returns true if client cert was present */ |
| 4865 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4866 | 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] | 4867 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4868 | struct connection *conn; |
| 4869 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4870 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4871 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4872 | return 0; |
| 4873 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4874 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4875 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4876 | return 0; |
| 4877 | } |
| 4878 | |
| 4879 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4880 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4881 | 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] | 4882 | |
| 4883 | return 1; |
| 4884 | } |
| 4885 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4886 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 4887 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4888 | * should be use. |
| 4889 | */ |
| 4890 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4891 | 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] | 4892 | { |
| 4893 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 4894 | X509 *crt = NULL; |
| 4895 | int ret = 0; |
| 4896 | struct chunk *smp_trash; |
| 4897 | struct connection *conn; |
| 4898 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4899 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4900 | if (!conn || conn->xprt != &ssl_sock) |
| 4901 | return 0; |
| 4902 | |
| 4903 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 4904 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4905 | return 0; |
| 4906 | } |
| 4907 | |
| 4908 | if (cert_peer) |
| 4909 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4910 | else |
| 4911 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 4912 | |
| 4913 | if (!crt) |
| 4914 | goto out; |
| 4915 | |
| 4916 | smp_trash = get_trash_chunk(); |
| 4917 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 4918 | goto out; |
| 4919 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4920 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4921 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4922 | ret = 1; |
| 4923 | out: |
| 4924 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4925 | if (cert_peer && crt) |
| 4926 | X509_free(crt); |
| 4927 | return ret; |
| 4928 | } |
| 4929 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4930 | /* binary, returns serial of certificate in a binary chunk. |
| 4931 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4932 | * should be use. |
| 4933 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4934 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4935 | 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] | 4936 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4937 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4938 | X509 *crt = NULL; |
| 4939 | int ret = 0; |
| 4940 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4941 | struct connection *conn; |
| 4942 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4943 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4944 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4945 | return 0; |
| 4946 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4947 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4948 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4949 | return 0; |
| 4950 | } |
| 4951 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4952 | if (cert_peer) |
| 4953 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4954 | else |
| 4955 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 4956 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4957 | if (!crt) |
| 4958 | goto out; |
| 4959 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4960 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4961 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 4962 | goto out; |
| 4963 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4964 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4965 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4966 | ret = 1; |
| 4967 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4968 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4969 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4970 | X509_free(crt); |
| 4971 | return ret; |
| 4972 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4973 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4974 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 4975 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4976 | * should be use. |
| 4977 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4978 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4979 | 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] | 4980 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4981 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4982 | X509 *crt = NULL; |
| 4983 | const EVP_MD *digest; |
| 4984 | int ret = 0; |
| 4985 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4986 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4987 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4988 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4989 | if (!conn || conn->xprt != &ssl_sock) |
| 4990 | return 0; |
| 4991 | |
| 4992 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4993 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4994 | return 0; |
| 4995 | } |
| 4996 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4997 | if (cert_peer) |
| 4998 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4999 | else |
| 5000 | crt = SSL_get_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5001 | if (!crt) |
| 5002 | goto out; |
| 5003 | |
| 5004 | smp_trash = get_trash_chunk(); |
| 5005 | digest = EVP_sha1(); |
| 5006 | X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len); |
| 5007 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5008 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5009 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5010 | ret = 1; |
| 5011 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5012 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5013 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 5014 | X509_free(crt); |
| 5015 | return ret; |
| 5016 | } |
| 5017 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5018 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 5019 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5020 | * should be use. |
| 5021 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5022 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5023 | 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] | 5024 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5025 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5026 | X509 *crt = NULL; |
| 5027 | int ret = 0; |
| 5028 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5029 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5030 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5031 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5032 | if (!conn || conn->xprt != &ssl_sock) |
| 5033 | return 0; |
| 5034 | |
| 5035 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5036 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5037 | return 0; |
| 5038 | } |
| 5039 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5040 | if (cert_peer) |
| 5041 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5042 | else |
| 5043 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5044 | if (!crt) |
| 5045 | goto out; |
| 5046 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5047 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5048 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 5049 | goto out; |
| 5050 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5051 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5052 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5053 | ret = 1; |
| 5054 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5055 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5056 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5057 | X509_free(crt); |
| 5058 | return ret; |
| 5059 | } |
| 5060 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5061 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 5062 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5063 | * should be use. |
| 5064 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5065 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5066 | 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] | 5067 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5068 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5069 | X509 *crt = NULL; |
| 5070 | X509_NAME *name; |
| 5071 | int ret = 0; |
| 5072 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5073 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5074 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5075 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5076 | if (!conn || conn->xprt != &ssl_sock) |
| 5077 | return 0; |
| 5078 | |
| 5079 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5080 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5081 | return 0; |
| 5082 | } |
| 5083 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5084 | if (cert_peer) |
| 5085 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5086 | else |
| 5087 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5088 | if (!crt) |
| 5089 | goto out; |
| 5090 | |
| 5091 | name = X509_get_issuer_name(crt); |
| 5092 | if (!name) |
| 5093 | goto out; |
| 5094 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5095 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5096 | if (args && args[0].type == ARGT_STR) { |
| 5097 | int pos = 1; |
| 5098 | |
| 5099 | if (args[1].type == ARGT_SINT) |
| 5100 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5101 | |
| 5102 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 5103 | goto out; |
| 5104 | } |
| 5105 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 5106 | goto out; |
| 5107 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5108 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5109 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5110 | ret = 1; |
| 5111 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5112 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5113 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5114 | X509_free(crt); |
| 5115 | return ret; |
| 5116 | } |
| 5117 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5118 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 5119 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5120 | * should be use. |
| 5121 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5122 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5123 | 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] | 5124 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5125 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5126 | X509 *crt = NULL; |
| 5127 | int ret = 0; |
| 5128 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5129 | struct connection *conn; |
| 5130 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5131 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5132 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5133 | return 0; |
| 5134 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5135 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5136 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5137 | return 0; |
| 5138 | } |
| 5139 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5140 | if (cert_peer) |
| 5141 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5142 | else |
| 5143 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5144 | if (!crt) |
| 5145 | goto out; |
| 5146 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5147 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5148 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 5149 | goto out; |
| 5150 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5151 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5152 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5153 | ret = 1; |
| 5154 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5155 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5156 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5157 | X509_free(crt); |
| 5158 | return ret; |
| 5159 | } |
| 5160 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5161 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 5162 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5163 | * should be use. |
| 5164 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5165 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5166 | 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] | 5167 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5168 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5169 | X509 *crt = NULL; |
| 5170 | X509_NAME *name; |
| 5171 | int ret = 0; |
| 5172 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5173 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5174 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5175 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5176 | if (!conn || conn->xprt != &ssl_sock) |
| 5177 | return 0; |
| 5178 | |
| 5179 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5180 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5181 | return 0; |
| 5182 | } |
| 5183 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5184 | if (cert_peer) |
| 5185 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5186 | else |
| 5187 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5188 | if (!crt) |
| 5189 | goto out; |
| 5190 | |
| 5191 | name = X509_get_subject_name(crt); |
| 5192 | if (!name) |
| 5193 | goto out; |
| 5194 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5195 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5196 | if (args && args[0].type == ARGT_STR) { |
| 5197 | int pos = 1; |
| 5198 | |
| 5199 | if (args[1].type == ARGT_SINT) |
| 5200 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5201 | |
| 5202 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 5203 | goto out; |
| 5204 | } |
| 5205 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 5206 | goto out; |
| 5207 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5208 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5209 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5210 | ret = 1; |
| 5211 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5212 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5213 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5214 | X509_free(crt); |
| 5215 | return ret; |
| 5216 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5217 | |
| 5218 | /* integer, returns true if current session use a client certificate */ |
| 5219 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5220 | 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] | 5221 | { |
| 5222 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5223 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5224 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5225 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5226 | if (!conn || conn->xprt != &ssl_sock) |
| 5227 | return 0; |
| 5228 | |
| 5229 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5230 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5231 | return 0; |
| 5232 | } |
| 5233 | |
| 5234 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5235 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5236 | if (crt) { |
| 5237 | X509_free(crt); |
| 5238 | } |
| 5239 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5240 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5241 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5242 | return 1; |
| 5243 | } |
| 5244 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5245 | /* integer, returns the certificate version |
| 5246 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5247 | * should be use. |
| 5248 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5249 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5250 | 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] | 5251 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5252 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5253 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5254 | struct connection *conn; |
| 5255 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5256 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5257 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5258 | return 0; |
| 5259 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5260 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5261 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5262 | return 0; |
| 5263 | } |
| 5264 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5265 | if (cert_peer) |
| 5266 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5267 | else |
| 5268 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5269 | if (!crt) |
| 5270 | return 0; |
| 5271 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5272 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5273 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5274 | if (cert_peer) |
| 5275 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5276 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5277 | |
| 5278 | return 1; |
| 5279 | } |
| 5280 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5281 | /* string, returns the certificate's signature algorithm. |
| 5282 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5283 | * should be use. |
| 5284 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5285 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5286 | 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] | 5287 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5288 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5289 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5290 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5291 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5292 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5293 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5294 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5295 | if (!conn || conn->xprt != &ssl_sock) |
| 5296 | return 0; |
| 5297 | |
| 5298 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5299 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5300 | return 0; |
| 5301 | } |
| 5302 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5303 | if (cert_peer) |
| 5304 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5305 | else |
| 5306 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5307 | if (!crt) |
| 5308 | return 0; |
| 5309 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5310 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 5311 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5312 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5313 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 5314 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5315 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5316 | if (cert_peer) |
| 5317 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5318 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 5319 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5320 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5321 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5322 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5323 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5324 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5325 | if (cert_peer) |
| 5326 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5327 | |
| 5328 | return 1; |
| 5329 | } |
| 5330 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5331 | /* string, returns the certificate's key algorithm. |
| 5332 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5333 | * should be use. |
| 5334 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5335 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5336 | 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] | 5337 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5338 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5339 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5340 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5341 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5342 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5343 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5344 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5345 | if (!conn || conn->xprt != &ssl_sock) |
| 5346 | return 0; |
| 5347 | |
| 5348 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5349 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5350 | return 0; |
| 5351 | } |
| 5352 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5353 | if (cert_peer) |
| 5354 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5355 | else |
| 5356 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5357 | if (!crt) |
| 5358 | return 0; |
| 5359 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5360 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 5361 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5362 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5363 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 5364 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5365 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5366 | if (cert_peer) |
| 5367 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5368 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 5369 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5370 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5371 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5372 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5373 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5374 | if (cert_peer) |
| 5375 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5376 | |
| 5377 | return 1; |
| 5378 | } |
| 5379 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5380 | /* boolean, returns true if front conn. transport layer is SSL. |
| 5381 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5382 | * char is 'b'. |
| 5383 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5384 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5385 | 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] | 5386 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5387 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5388 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5389 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5390 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5391 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5392 | return 1; |
| 5393 | } |
| 5394 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5395 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5396 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5397 | 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] | 5398 | { |
| 5399 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5400 | struct connection *conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5401 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5402 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5403 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5404 | conn->xprt_ctx && |
| 5405 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5406 | return 1; |
| 5407 | #else |
| 5408 | return 0; |
| 5409 | #endif |
| 5410 | } |
| 5411 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 5412 | /* boolean, returns true if client session has been resumed */ |
| 5413 | static int |
| 5414 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 5415 | { |
| 5416 | struct connection *conn = objt_conn(smp->sess->origin); |
| 5417 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5418 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5419 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 5420 | conn->xprt_ctx && |
| 5421 | SSL_session_reused(conn->xprt_ctx); |
| 5422 | return 1; |
| 5423 | } |
| 5424 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5425 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 5426 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5427 | * char is 'b'. |
| 5428 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5429 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5430 | 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] | 5431 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5432 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5433 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5434 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5435 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5436 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5437 | return 0; |
| 5438 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5439 | smp->data.u.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
| 5440 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5441 | return 0; |
| 5442 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5443 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5444 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5445 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5446 | |
| 5447 | return 1; |
| 5448 | } |
| 5449 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5450 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 5451 | * is SSL. |
| 5452 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5453 | * char is 'b'. |
| 5454 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5455 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5456 | 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] | 5457 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5458 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5459 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5460 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5461 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5462 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5463 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5464 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5465 | return 0; |
| 5466 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 5467 | if (!SSL_get_cipher_bits(conn->xprt_ctx, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5468 | return 0; |
| 5469 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5470 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5471 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5472 | |
| 5473 | return 1; |
| 5474 | } |
| 5475 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5476 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 5477 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5478 | * char is 'b'. |
| 5479 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5480 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5481 | 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] | 5482 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5483 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5484 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5485 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5486 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5487 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5488 | return 0; |
| 5489 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5490 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
| 5491 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5492 | return 0; |
| 5493 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5494 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5495 | |
| 5496 | return 1; |
| 5497 | } |
| 5498 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5499 | #ifdef OPENSSL_NPN_NEGOTIATED |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5500 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5501 | 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] | 5502 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5503 | struct connection *conn; |
| 5504 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5505 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5506 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5507 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5508 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5509 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5510 | return 0; |
| 5511 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5512 | smp->data.u.str.str = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5513 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5514 | (const unsigned char **)&smp->data.u.str.str, (unsigned *)&smp->data.u.str.len); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5515 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5516 | if (!smp->data.u.str.str) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5517 | return 0; |
| 5518 | |
| 5519 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5520 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5521 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5522 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5523 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5524 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5525 | 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] | 5526 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5527 | struct connection *conn; |
| 5528 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5529 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5530 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5531 | |
Willy Tarreau | e26bf05 | 2015-05-12 10:30:12 +0200 | [diff] [blame] | 5532 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5533 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5534 | return 0; |
| 5535 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5536 | smp->data.u.str.str = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5537 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5538 | (const unsigned char **)&smp->data.u.str.str, (unsigned *)&smp->data.u.str.len); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5539 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5540 | if (!smp->data.u.str.str) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5541 | return 0; |
| 5542 | |
| 5543 | return 1; |
| 5544 | } |
| 5545 | #endif |
| 5546 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5547 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 5548 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5549 | * char is 'b'. |
| 5550 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5551 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5552 | 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] | 5553 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5554 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5555 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5556 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5557 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5558 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5559 | return 0; |
| 5560 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5561 | smp->data.u.str.str = (char *)SSL_get_version(conn->xprt_ctx); |
| 5562 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5563 | return 0; |
| 5564 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5565 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5566 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5567 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5568 | |
| 5569 | return 1; |
| 5570 | } |
| 5571 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5572 | /* 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] | 5573 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5574 | * char is 'b'. |
| 5575 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5576 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5577 | 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] | 5578 | { |
| 5579 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5580 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5581 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 5582 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5583 | SSL_SESSION *ssl_sess; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5584 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5585 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5586 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 5587 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5588 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5589 | return 0; |
| 5590 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5591 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 5592 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 5593 | return 0; |
| 5594 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5595 | smp->data.u.str.str = (char *)SSL_SESSION_get_id(ssl_sess, (unsigned int *)&smp->data.u.str.len); |
| 5596 | if (!smp->data.u.str.str || !smp->data.u.str.len) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 5597 | return 0; |
| 5598 | |
| 5599 | return 1; |
| 5600 | #else |
| 5601 | return 0; |
| 5602 | #endif |
| 5603 | } |
| 5604 | |
| 5605 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5606 | 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] | 5607 | { |
| 5608 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5609 | struct connection *conn; |
| 5610 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5611 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5612 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5613 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5614 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5615 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5616 | return 0; |
| 5617 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5618 | smp->data.u.str.str = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 5619 | if (!smp->data.u.str.str) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 5620 | return 0; |
| 5621 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5622 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5623 | return 1; |
| 5624 | #else |
| 5625 | return 0; |
| 5626 | #endif |
| 5627 | } |
| 5628 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5629 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 5630 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 5631 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 5632 | struct connection *conn; |
| 5633 | struct ssl_capture *capture; |
| 5634 | |
| 5635 | conn = objt_conn(smp->sess->origin); |
| 5636 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5637 | return 0; |
| 5638 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 5639 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 5640 | if (!capture) |
| 5641 | return 0; |
| 5642 | |
| 5643 | smp->flags = SMP_F_CONST; |
| 5644 | smp->data.type = SMP_T_BIN; |
| 5645 | smp->data.u.str.str = capture->ciphersuite; |
| 5646 | smp->data.u.str.len = capture->ciphersuite_len; |
| 5647 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 5648 | } |
| 5649 | |
| 5650 | static int |
| 5651 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 5652 | { |
| 5653 | struct chunk *data; |
| 5654 | |
| 5655 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 5656 | return 0; |
| 5657 | |
| 5658 | data = get_trash_chunk(); |
| 5659 | dump_binary(data, smp->data.u.str.str, smp->data.u.str.len); |
| 5660 | smp->data.type = SMP_T_BIN; |
| 5661 | smp->data.u.str = *data; |
| 5662 | return 1; |
| 5663 | } |
| 5664 | |
| 5665 | static int |
| 5666 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 5667 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 5668 | struct connection *conn; |
| 5669 | struct ssl_capture *capture; |
| 5670 | |
| 5671 | conn = objt_conn(smp->sess->origin); |
| 5672 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5673 | return 0; |
| 5674 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 5675 | capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 5676 | if (!capture) |
| 5677 | return 0; |
| 5678 | |
| 5679 | smp->data.type = SMP_T_SINT; |
| 5680 | smp->data.u.sint = capture->xxh64; |
| 5681 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | static int |
| 5685 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 5686 | { |
| 5687 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && !defined(OPENSSL_NO_SSL_TRACE) |
| 5688 | struct chunk *data; |
| 5689 | SSL_CIPHER cipher; |
| 5690 | int i; |
| 5691 | const char *str; |
| 5692 | unsigned char *bin; |
| 5693 | |
| 5694 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 5695 | return 0; |
| 5696 | |
| 5697 | /* The cipher algorith must not be SSL_SSLV2, because this |
| 5698 | * SSL version seems to not have the same cipher encoding, |
| 5699 | * and it is not supported by OpenSSL. Unfortunately, the |
| 5700 | * #define SSL_SSLV2, SSL_SSLV3 and others are not available |
| 5701 | * with standard defines. We just set the variable to 0, |
| 5702 | * ensure that the match with SSL_SSLV2 fails. |
| 5703 | */ |
| 5704 | cipher.algorithm_ssl = 0; |
| 5705 | |
| 5706 | data = get_trash_chunk(); |
| 5707 | for (i = 0; i + 1 < smp->data.u.str.len; i += 2) { |
| 5708 | bin = (unsigned char *)smp->data.u.str.str + i; |
| 5709 | cipher.id = (unsigned int)(bin[0] << 8) | bin[1]; |
| 5710 | str = SSL_CIPHER_standard_name(&cipher); |
| 5711 | if (!str || strcmp(str, "UNKNOWN") == 0) |
| 5712 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", (unsigned int)cipher.id); |
| 5713 | else |
| 5714 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 5715 | } |
| 5716 | smp->data.type = SMP_T_STR; |
| 5717 | smp->data.u.str = *data; |
| 5718 | return 1; |
| 5719 | #else |
| 5720 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 5721 | #endif |
| 5722 | } |
| 5723 | |
| 5724 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5725 | 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] | 5726 | { |
| 5727 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5728 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5729 | smp->strm ? smp->strm->si[1].end : NULL); |
| 5730 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5731 | int finished_len; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5732 | struct chunk *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5733 | |
| 5734 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5735 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5736 | return 0; |
| 5737 | |
| 5738 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 5739 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5740 | return 0; |
| 5741 | } |
| 5742 | |
| 5743 | finished_trash = get_trash_chunk(); |
| 5744 | if (!SSL_session_reused(conn->xprt_ctx)) |
| 5745 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 5746 | else |
| 5747 | finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 5748 | |
| 5749 | if (!finished_len) |
| 5750 | return 0; |
| 5751 | |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 5752 | finished_trash->len = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5753 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5754 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5755 | |
| 5756 | return 1; |
| 5757 | #else |
| 5758 | return 0; |
| 5759 | #endif |
| 5760 | } |
| 5761 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5762 | /* 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] | 5763 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5764 | 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] | 5765 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5766 | struct connection *conn; |
| 5767 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5768 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5769 | if (!conn || conn->xprt != &ssl_sock) |
| 5770 | return 0; |
| 5771 | |
| 5772 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5773 | smp->flags = SMP_F_MAY_CHANGE; |
| 5774 | return 0; |
| 5775 | } |
| 5776 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5777 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5778 | 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] | 5779 | smp->flags = 0; |
| 5780 | |
| 5781 | return 1; |
| 5782 | } |
| 5783 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5784 | /* 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] | 5785 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5786 | 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] | 5787 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5788 | struct connection *conn; |
| 5789 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5790 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5791 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5792 | return 0; |
| 5793 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5794 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5795 | smp->flags = SMP_F_MAY_CHANGE; |
| 5796 | return 0; |
| 5797 | } |
| 5798 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5799 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5800 | 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] | 5801 | smp->flags = 0; |
| 5802 | |
| 5803 | return 1; |
| 5804 | } |
| 5805 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5806 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5807 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5808 | 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] | 5809 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5810 | struct connection *conn; |
| 5811 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5812 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5813 | if (!conn || conn->xprt != &ssl_sock) |
| 5814 | return 0; |
| 5815 | |
| 5816 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5817 | smp->flags = SMP_F_MAY_CHANGE; |
| 5818 | return 0; |
| 5819 | } |
| 5820 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5821 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5822 | 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] | 5823 | smp->flags = 0; |
| 5824 | |
| 5825 | return 1; |
| 5826 | } |
| 5827 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5828 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 5829 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5830 | 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] | 5831 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5832 | struct connection *conn; |
| 5833 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5834 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5835 | if (!conn || conn->xprt != &ssl_sock) |
| 5836 | return 0; |
| 5837 | |
| 5838 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 5839 | smp->flags = SMP_F_MAY_CHANGE; |
| 5840 | return 0; |
| 5841 | } |
| 5842 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5843 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 5844 | return 0; |
| 5845 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5846 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5847 | 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] | 5848 | smp->flags = 0; |
| 5849 | |
| 5850 | return 1; |
| 5851 | } |
| 5852 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 5853 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5854 | 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] | 5855 | { |
| 5856 | if (!*args[cur_arg + 1]) { |
| 5857 | if (err) |
| 5858 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 5859 | return ERR_ALERT | ERR_FATAL; |
| 5860 | } |
| 5861 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5862 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 5863 | 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] | 5864 | else |
| 5865 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5866 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5867 | return 0; |
| 5868 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5869 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5870 | { |
| 5871 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 5872 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5873 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5874 | /* parse the "ca-sign-file" bind keyword */ |
| 5875 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5876 | { |
| 5877 | if (!*args[cur_arg + 1]) { |
| 5878 | if (err) |
| 5879 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 5880 | return ERR_ALERT | ERR_FATAL; |
| 5881 | } |
| 5882 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5883 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 5884 | 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] | 5885 | else |
| 5886 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 5887 | |
| 5888 | return 0; |
| 5889 | } |
| 5890 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 5891 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5892 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5893 | { |
| 5894 | if (!*args[cur_arg + 1]) { |
| 5895 | if (err) |
| 5896 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 5897 | return ERR_ALERT | ERR_FATAL; |
| 5898 | } |
| 5899 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 5900 | return 0; |
| 5901 | } |
| 5902 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5903 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5904 | 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] | 5905 | { |
| 5906 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 5907 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5908 | return ERR_ALERT | ERR_FATAL; |
| 5909 | } |
| 5910 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 5911 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5912 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5913 | return 0; |
| 5914 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5915 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5916 | { |
| 5917 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 5918 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5919 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5920 | 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] | 5921 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 5922 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 5923 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5924 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 5925 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5926 | return ERR_ALERT | ERR_FATAL; |
| 5927 | } |
| 5928 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5929 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 5930 | 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] | 5931 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 5932 | return ERR_ALERT | ERR_FATAL; |
| 5933 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5934 | 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] | 5935 | if (ssl_sock_load_cert(path, conf, err) > 0) |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5936 | return ERR_ALERT | ERR_FATAL; |
| 5937 | |
| 5938 | return 0; |
| 5939 | } |
| 5940 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5941 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5942 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5943 | |
| 5944 | return 0; |
| 5945 | } |
| 5946 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5947 | /* parse the "crt-list" bind keyword */ |
| 5948 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5949 | { |
| 5950 | if (!*args[cur_arg + 1]) { |
| 5951 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 5952 | return ERR_ALERT | ERR_FATAL; |
| 5953 | } |
| 5954 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5955 | 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] | 5956 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5957 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 5958 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5959 | |
| 5960 | return 0; |
| 5961 | } |
| 5962 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 5963 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5964 | 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] | 5965 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5966 | #ifndef X509_V_FLAG_CRL_CHECK |
| 5967 | if (err) |
| 5968 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 5969 | return ERR_ALERT | ERR_FATAL; |
| 5970 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5971 | if (!*args[cur_arg + 1]) { |
| 5972 | if (err) |
| 5973 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 5974 | return ERR_ALERT | ERR_FATAL; |
| 5975 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5976 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5977 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 5978 | 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] | 5979 | else |
| 5980 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5981 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5982 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5983 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5984 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5985 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5986 | { |
| 5987 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 5988 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5989 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5990 | /* parse the "curves" bind keyword keyword */ |
| 5991 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 5992 | { |
| 5993 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 5994 | if (!*args[cur_arg + 1]) { |
| 5995 | if (err) |
| 5996 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 5997 | return ERR_ALERT | ERR_FATAL; |
| 5998 | } |
| 5999 | conf->curves = strdup(args[cur_arg + 1]); |
| 6000 | return 0; |
| 6001 | #else |
| 6002 | if (err) |
| 6003 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 6004 | return ERR_ALERT | ERR_FATAL; |
| 6005 | #endif |
| 6006 | } |
| 6007 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6008 | { |
| 6009 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 6010 | } |
| 6011 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 6012 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6013 | 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] | 6014 | { |
| 6015 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 6016 | if (err) |
| 6017 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 6018 | return ERR_ALERT | ERR_FATAL; |
| 6019 | #elif defined(OPENSSL_NO_ECDH) |
| 6020 | if (err) |
| 6021 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 6022 | return ERR_ALERT | ERR_FATAL; |
| 6023 | #else |
| 6024 | if (!*args[cur_arg + 1]) { |
| 6025 | if (err) |
| 6026 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 6027 | return ERR_ALERT | ERR_FATAL; |
| 6028 | } |
| 6029 | |
| 6030 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6031 | |
| 6032 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 6033 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6034 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6035 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6036 | { |
| 6037 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 6038 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6039 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 6040 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 6041 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6042 | { |
| 6043 | int code; |
| 6044 | char *p = args[cur_arg + 1]; |
| 6045 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 6046 | |
| 6047 | if (!*p) { |
| 6048 | if (err) |
| 6049 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 6050 | return ERR_ALERT | ERR_FATAL; |
| 6051 | } |
| 6052 | |
| 6053 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 6054 | ignerr = &conf->ca_ignerr; |
| 6055 | |
| 6056 | if (strcmp(p, "all") == 0) { |
| 6057 | *ignerr = ~0ULL; |
| 6058 | return 0; |
| 6059 | } |
| 6060 | |
| 6061 | while (p) { |
| 6062 | code = atoi(p); |
| 6063 | if ((code <= 0) || (code > 63)) { |
| 6064 | if (err) |
| 6065 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 6066 | args[cur_arg], code, args[cur_arg + 1]); |
| 6067 | return ERR_ALERT | ERR_FATAL; |
| 6068 | } |
| 6069 | *ignerr |= 1ULL << code; |
| 6070 | p = strchr(p, ','); |
| 6071 | if (p) |
| 6072 | p++; |
| 6073 | } |
| 6074 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6075 | return 0; |
| 6076 | } |
| 6077 | |
| 6078 | /* parse the "force-sslv3" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6079 | static int bind_parse_force_sslv3(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] | 6080 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 6081 | #ifndef OPENSSL_NO_SSL3 |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6082 | conf->ssl_options |= BC_SSL_O_USE_SSLV3; |
| 6083 | return 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 6084 | #else |
| 6085 | if (err) |
| 6086 | memprintf(err, "'%s' : library does not support protocol SSLv3", args[cur_arg]); |
| 6087 | return ERR_ALERT | ERR_FATAL; |
| 6088 | #endif |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6089 | } |
| 6090 | |
| 6091 | /* parse the "force-tlsv10" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6092 | static int bind_parse_force_tlsv10(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] | 6093 | { |
| 6094 | conf->ssl_options |= BC_SSL_O_USE_TLSV10; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 6095 | return 0; |
| 6096 | } |
| 6097 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6098 | /* parse the "force-tlsv11" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6099 | static int bind_parse_force_tlsv11(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] | 6100 | { |
| 6101 | #if SSL_OP_NO_TLSv1_1 |
| 6102 | conf->ssl_options |= BC_SSL_O_USE_TLSV11; |
| 6103 | return 0; |
| 6104 | #else |
| 6105 | if (err) |
| 6106 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[cur_arg]); |
| 6107 | return ERR_ALERT | ERR_FATAL; |
| 6108 | #endif |
| 6109 | } |
| 6110 | |
| 6111 | /* parse the "force-tlsv12" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6112 | static int bind_parse_force_tlsv12(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] | 6113 | { |
| 6114 | #if SSL_OP_NO_TLSv1_2 |
| 6115 | conf->ssl_options |= BC_SSL_O_USE_TLSV12; |
| 6116 | return 0; |
| 6117 | #else |
| 6118 | if (err) |
| 6119 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[cur_arg]); |
| 6120 | return ERR_ALERT | ERR_FATAL; |
| 6121 | #endif |
| 6122 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 6123 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 6124 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6125 | 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] | 6126 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 6127 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 6128 | return 0; |
| 6129 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 6130 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 6131 | /* parse the "no-sslv3" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6132 | static int bind_parse_no_sslv3(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] | 6133 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 6134 | conf->ssl_options |= BC_SSL_O_NO_SSLV3; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6135 | return 0; |
| 6136 | } |
| 6137 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 6138 | /* parse the "no-tlsv10" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6139 | static int bind_parse_no_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 6140 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 6141 | conf->ssl_options |= BC_SSL_O_NO_TLSV10; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 6142 | return 0; |
| 6143 | } |
| 6144 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 6145 | /* parse the "no-tlsv11" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6146 | static int bind_parse_no_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 6147 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 6148 | conf->ssl_options |= BC_SSL_O_NO_TLSV11; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 6149 | return 0; |
| 6150 | } |
| 6151 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 6152 | /* parse the "no-tlsv12" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6153 | static int bind_parse_no_tlsv12(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] | 6154 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 6155 | conf->ssl_options |= BC_SSL_O_NO_TLSV12; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6156 | return 0; |
| 6157 | } |
| 6158 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6159 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6160 | 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] | 6161 | { |
| 6162 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 6163 | char *p1, *p2; |
| 6164 | |
| 6165 | if (!*args[cur_arg + 1]) { |
| 6166 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 6167 | return ERR_ALERT | ERR_FATAL; |
| 6168 | } |
| 6169 | |
| 6170 | free(conf->npn_str); |
| 6171 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 6172 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 6173 | * so we reuse each comma to store the next <len> and need |
| 6174 | * one more for the end of the string. |
| 6175 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6176 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 6177 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 6178 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 6179 | |
| 6180 | /* replace commas with the name length */ |
| 6181 | p1 = conf->npn_str; |
| 6182 | p2 = p1 + 1; |
| 6183 | while (1) { |
| 6184 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 6185 | if (!p2) |
| 6186 | p2 = p1 + 1 + strlen(p1 + 1); |
| 6187 | |
| 6188 | if (p2 - (p1 + 1) > 255) { |
| 6189 | *p2 = '\0'; |
| 6190 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 6191 | return ERR_ALERT | ERR_FATAL; |
| 6192 | } |
| 6193 | |
| 6194 | *p1 = p2 - (p1 + 1); |
| 6195 | p1 = p2; |
| 6196 | |
| 6197 | if (!*p2) |
| 6198 | break; |
| 6199 | |
| 6200 | *(p2++) = '\0'; |
| 6201 | } |
| 6202 | return 0; |
| 6203 | #else |
| 6204 | if (err) |
| 6205 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 6206 | return ERR_ALERT | ERR_FATAL; |
| 6207 | #endif |
| 6208 | } |
| 6209 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6210 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6211 | { |
| 6212 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 6213 | } |
| 6214 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6215 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6216 | 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] | 6217 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6218 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6219 | char *p1, *p2; |
| 6220 | |
| 6221 | if (!*args[cur_arg + 1]) { |
| 6222 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 6223 | return ERR_ALERT | ERR_FATAL; |
| 6224 | } |
| 6225 | |
| 6226 | free(conf->alpn_str); |
| 6227 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 6228 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 6229 | * so we reuse each comma to store the next <len> and need |
| 6230 | * one more for the end of the string. |
| 6231 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6232 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 6233 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6234 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 6235 | |
| 6236 | /* replace commas with the name length */ |
| 6237 | p1 = conf->alpn_str; |
| 6238 | p2 = p1 + 1; |
| 6239 | while (1) { |
| 6240 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 6241 | if (!p2) |
| 6242 | p2 = p1 + 1 + strlen(p1 + 1); |
| 6243 | |
| 6244 | if (p2 - (p1 + 1) > 255) { |
| 6245 | *p2 = '\0'; |
| 6246 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 6247 | return ERR_ALERT | ERR_FATAL; |
| 6248 | } |
| 6249 | |
| 6250 | *p1 = p2 - (p1 + 1); |
| 6251 | p1 = p2; |
| 6252 | |
| 6253 | if (!*p2) |
| 6254 | break; |
| 6255 | |
| 6256 | *(p2++) = '\0'; |
| 6257 | } |
| 6258 | return 0; |
| 6259 | #else |
| 6260 | if (err) |
| 6261 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 6262 | return ERR_ALERT | ERR_FATAL; |
| 6263 | #endif |
| 6264 | } |
| 6265 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6266 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6267 | { |
| 6268 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 6269 | } |
| 6270 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6271 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6272 | 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] | 6273 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 6274 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6275 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 6276 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6277 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 6278 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 6279 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 6280 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6281 | return 0; |
| 6282 | } |
| 6283 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6284 | /* parse the "generate-certificates" bind keyword */ |
| 6285 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6286 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6287 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6288 | conf->generate_certs = 1; |
| 6289 | #else |
| 6290 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 6291 | err && *err ? *err : ""); |
| 6292 | #endif |
| 6293 | return 0; |
| 6294 | } |
| 6295 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 6296 | /* parse the "strict-sni" bind keyword */ |
| 6297 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6298 | { |
| 6299 | conf->strict_sni = 1; |
| 6300 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6301 | } |
| 6302 | |
| 6303 | /* parse the "tls-ticket-keys" bind keyword */ |
| 6304 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6305 | { |
| 6306 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6307 | FILE *f; |
| 6308 | int i = 0; |
| 6309 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6310 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6311 | |
| 6312 | if (!*args[cur_arg + 1]) { |
| 6313 | if (err) |
| 6314 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 6315 | return ERR_ALERT | ERR_FATAL; |
| 6316 | } |
| 6317 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 6318 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
| 6319 | if(keys_ref) { |
| 6320 | conf->keys_ref = keys_ref; |
| 6321 | return 0; |
| 6322 | } |
| 6323 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 6324 | keys_ref = malloc(sizeof(*keys_ref)); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6325 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct tls_sess_key)); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6326 | |
| 6327 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 6328 | if (err) |
| 6329 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 6330 | return ERR_ALERT | ERR_FATAL; |
| 6331 | } |
| 6332 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6333 | keys_ref->filename = strdup(args[cur_arg + 1]); |
| 6334 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6335 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 6336 | int len = strlen(thisline); |
| 6337 | /* Strip newline characters from the end */ |
| 6338 | if(thisline[len - 1] == '\n') |
| 6339 | thisline[--len] = 0; |
| 6340 | |
| 6341 | if(thisline[len - 1] == '\r') |
| 6342 | thisline[--len] = 0; |
| 6343 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6344 | 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] | 6345 | if (err) |
| 6346 | 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] | 6347 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6348 | return ERR_ALERT | ERR_FATAL; |
| 6349 | } |
| 6350 | i++; |
| 6351 | } |
| 6352 | |
| 6353 | if (i < TLS_TICKETS_NO) { |
| 6354 | if (err) |
| 6355 | 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] | 6356 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6357 | return ERR_ALERT | ERR_FATAL; |
| 6358 | } |
| 6359 | |
| 6360 | fclose(f); |
| 6361 | |
| 6362 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 6363 | i -= 2; |
| 6364 | 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] | 6365 | keys_ref->unique_id = -1; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6366 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6367 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 6368 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 6369 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6370 | return 0; |
| 6371 | #else |
| 6372 | if (err) |
| 6373 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 6374 | return ERR_ALERT | ERR_FATAL; |
| 6375 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 6376 | } |
| 6377 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6378 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6379 | 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] | 6380 | { |
| 6381 | if (!*args[cur_arg + 1]) { |
| 6382 | if (err) |
| 6383 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 6384 | return ERR_ALERT | ERR_FATAL; |
| 6385 | } |
| 6386 | |
| 6387 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6388 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6389 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6390 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6391 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6392 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6393 | else { |
| 6394 | if (err) |
| 6395 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 6396 | args[cur_arg], args[cur_arg + 1]); |
| 6397 | return ERR_ALERT | ERR_FATAL; |
| 6398 | } |
| 6399 | |
| 6400 | return 0; |
| 6401 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6402 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6403 | { |
| 6404 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 6405 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6406 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6407 | /************** "server" keywords ****************/ |
| 6408 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6409 | /* parse the "ca-file" server keyword */ |
| 6410 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6411 | { |
| 6412 | if (!*args[*cur_arg + 1]) { |
| 6413 | if (err) |
| 6414 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 6415 | return ERR_ALERT | ERR_FATAL; |
| 6416 | } |
| 6417 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6418 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6419 | 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] | 6420 | else |
| 6421 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 6422 | |
| 6423 | return 0; |
| 6424 | } |
| 6425 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6426 | /* parse the "check-ssl" server keyword */ |
| 6427 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6428 | { |
| 6429 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6430 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 6431 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
| 6432 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6433 | return 0; |
| 6434 | } |
| 6435 | |
| 6436 | /* parse the "ciphers" server keyword */ |
| 6437 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6438 | { |
| 6439 | if (!*args[*cur_arg + 1]) { |
| 6440 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 6441 | return ERR_ALERT | ERR_FATAL; |
| 6442 | } |
| 6443 | |
| 6444 | free(newsrv->ssl_ctx.ciphers); |
| 6445 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 6446 | return 0; |
| 6447 | } |
| 6448 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6449 | /* parse the "crl-file" server keyword */ |
| 6450 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6451 | { |
| 6452 | #ifndef X509_V_FLAG_CRL_CHECK |
| 6453 | if (err) |
| 6454 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 6455 | return ERR_ALERT | ERR_FATAL; |
| 6456 | #else |
| 6457 | if (!*args[*cur_arg + 1]) { |
| 6458 | if (err) |
| 6459 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 6460 | return ERR_ALERT | ERR_FATAL; |
| 6461 | } |
| 6462 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6463 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6464 | 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] | 6465 | else |
| 6466 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 6467 | |
| 6468 | return 0; |
| 6469 | #endif |
| 6470 | } |
| 6471 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6472 | /* parse the "crt" server keyword */ |
| 6473 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6474 | { |
| 6475 | if (!*args[*cur_arg + 1]) { |
| 6476 | if (err) |
| 6477 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 6478 | return ERR_ALERT | ERR_FATAL; |
| 6479 | } |
| 6480 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6481 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
| 6482 | memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6483 | else |
| 6484 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 6485 | |
| 6486 | return 0; |
| 6487 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6488 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6489 | /* parse the "force-sslv3" server keyword */ |
| 6490 | static int srv_parse_force_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6491 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 6492 | #ifndef OPENSSL_NO_SSL3 |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6493 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_SSLV3; |
| 6494 | return 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 6495 | #else |
| 6496 | if (err) |
| 6497 | memprintf(err, "'%s' : library does not support protocol SSLv3", args[*cur_arg]); |
| 6498 | return ERR_ALERT | ERR_FATAL; |
| 6499 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6500 | } |
| 6501 | |
| 6502 | /* parse the "force-tlsv10" server keyword */ |
| 6503 | static int srv_parse_force_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6504 | { |
| 6505 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV10; |
| 6506 | return 0; |
| 6507 | } |
| 6508 | |
| 6509 | /* parse the "force-tlsv11" server keyword */ |
| 6510 | static int srv_parse_force_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6511 | { |
| 6512 | #if SSL_OP_NO_TLSv1_1 |
| 6513 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV11; |
| 6514 | return 0; |
| 6515 | #else |
| 6516 | if (err) |
| 6517 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[*cur_arg]); |
| 6518 | return ERR_ALERT | ERR_FATAL; |
| 6519 | #endif |
| 6520 | } |
| 6521 | |
| 6522 | /* parse the "force-tlsv12" server keyword */ |
| 6523 | static int srv_parse_force_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6524 | { |
| 6525 | #if SSL_OP_NO_TLSv1_2 |
| 6526 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV12; |
| 6527 | return 0; |
| 6528 | #else |
| 6529 | if (err) |
| 6530 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[*cur_arg]); |
| 6531 | return ERR_ALERT | ERR_FATAL; |
| 6532 | #endif |
| 6533 | } |
| 6534 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 6535 | /* parse the "no-ssl-reuse" server keyword */ |
| 6536 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6537 | { |
| 6538 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 6539 | return 0; |
| 6540 | } |
| 6541 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6542 | /* parse the "no-sslv3" server keyword */ |
| 6543 | static int srv_parse_no_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6544 | { |
| 6545 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_SSLV3; |
| 6546 | return 0; |
| 6547 | } |
| 6548 | |
| 6549 | /* parse the "no-tlsv10" server keyword */ |
| 6550 | static int srv_parse_no_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6551 | { |
| 6552 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV10; |
| 6553 | return 0; |
| 6554 | } |
| 6555 | |
| 6556 | /* parse the "no-tlsv11" server keyword */ |
| 6557 | static int srv_parse_no_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6558 | { |
| 6559 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV11; |
| 6560 | return 0; |
| 6561 | } |
| 6562 | |
| 6563 | /* parse the "no-tlsv12" server keyword */ |
| 6564 | static int srv_parse_no_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6565 | { |
| 6566 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV12; |
| 6567 | return 0; |
| 6568 | } |
| 6569 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 6570 | /* parse the "no-tls-tickets" server keyword */ |
| 6571 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6572 | { |
| 6573 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 6574 | return 0; |
| 6575 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6576 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 6577 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6578 | { |
| 6579 | newsrv->pp_opts |= SRV_PP_V2; |
| 6580 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 6581 | return 0; |
| 6582 | } |
| 6583 | |
| 6584 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 6585 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6586 | { |
| 6587 | newsrv->pp_opts |= SRV_PP_V2; |
| 6588 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 6589 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 6590 | return 0; |
| 6591 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 6592 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6593 | /* parse the "sni" server keyword */ |
| 6594 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6595 | { |
| 6596 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 6597 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 6598 | return ERR_ALERT | ERR_FATAL; |
| 6599 | #else |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 6600 | int idx; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6601 | struct sample_expr *expr; |
| 6602 | |
| 6603 | if (!*args[*cur_arg + 1]) { |
| 6604 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 6605 | return ERR_ALERT | ERR_FATAL; |
| 6606 | } |
| 6607 | |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 6608 | idx = (*cur_arg) + 1; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6609 | proxy->conf.args.ctx = ARGC_SRV; |
| 6610 | |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 6611 | expr = sample_parse_expr((char **)args, &idx, px->conf.file, px->conf.line, err, &proxy->conf.args); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6612 | if (!expr) { |
| 6613 | memprintf(err, "error detected while parsing sni expression : %s", *err); |
| 6614 | return ERR_ALERT | ERR_FATAL; |
| 6615 | } |
| 6616 | |
| 6617 | if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) { |
| 6618 | memprintf(err, "error detected while parsing sni expression : " |
| 6619 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 6620 | args[idx-1], sample_src_names(expr->fetch->use)); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6621 | return ERR_ALERT | ERR_FATAL; |
| 6622 | } |
| 6623 | |
| 6624 | px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 6625 | newsrv->ssl_ctx.sni = expr; |
| 6626 | return 0; |
| 6627 | #endif |
| 6628 | } |
| 6629 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6630 | /* parse the "ssl" server keyword */ |
| 6631 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6632 | { |
| 6633 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6634 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 6635 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6636 | return 0; |
| 6637 | } |
| 6638 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6639 | /* parse the "verify" server keyword */ |
| 6640 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6641 | { |
| 6642 | if (!*args[*cur_arg + 1]) { |
| 6643 | if (err) |
| 6644 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 6645 | return ERR_ALERT | ERR_FATAL; |
| 6646 | } |
| 6647 | |
| 6648 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6649 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6650 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6651 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6652 | else { |
| 6653 | if (err) |
| 6654 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 6655 | args[*cur_arg], args[*cur_arg + 1]); |
| 6656 | return ERR_ALERT | ERR_FATAL; |
| 6657 | } |
| 6658 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6659 | return 0; |
| 6660 | } |
| 6661 | |
| 6662 | /* parse the "verifyhost" server keyword */ |
| 6663 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6664 | { |
| 6665 | if (!*args[*cur_arg + 1]) { |
| 6666 | if (err) |
| 6667 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 6668 | return ERR_ALERT | ERR_FATAL; |
| 6669 | } |
| 6670 | |
| 6671 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 6672 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6673 | return 0; |
| 6674 | } |
| 6675 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6676 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 6677 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 6678 | struct proxy *defpx, const char *file, int line, |
| 6679 | char **err) { |
| 6680 | int i = 1; |
| 6681 | |
| 6682 | if (*(args[i]) == 0) { |
| 6683 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 6684 | return -1; |
| 6685 | } |
| 6686 | while (*(args[i])) { |
| 6687 | if (!strcmp(args[i], "no-sslv3")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6688 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_SSLV3; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6689 | else if (!strcmp(args[i], "no-tlsv10")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6690 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLSV10; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6691 | else if (!strcmp(args[i], "no-tlsv11")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6692 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLSV11; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6693 | else if (!strcmp(args[i], "no-tlsv12")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6694 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLSV12; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6695 | else if (!strcmp(args[i], "force-sslv3")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6696 | global_ssl.listen_default_ssloptions |= BC_SSL_O_USE_SSLV3; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6697 | else if (!strcmp(args[i], "force-tlsv10")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6698 | global_ssl.listen_default_ssloptions |= BC_SSL_O_USE_TLSV10; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6699 | else if (!strcmp(args[i], "force-tlsv11")) { |
| 6700 | #if SSL_OP_NO_TLSv1_1 |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6701 | global_ssl.listen_default_ssloptions |= BC_SSL_O_USE_TLSV11; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6702 | #else |
| 6703 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.1", args[0], args[i]); |
| 6704 | return -1; |
| 6705 | #endif |
| 6706 | } |
| 6707 | else if (!strcmp(args[i], "force-tlsv12")) { |
| 6708 | #if SSL_OP_NO_TLSv1_2 |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6709 | global_ssl.listen_default_ssloptions |= BC_SSL_O_USE_TLSV12; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6710 | #else |
| 6711 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.2", args[0], args[i]); |
| 6712 | return -1; |
| 6713 | #endif |
| 6714 | } |
| 6715 | else if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6716 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6717 | else { |
| 6718 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 6719 | return -1; |
| 6720 | } |
| 6721 | i++; |
| 6722 | } |
| 6723 | return 0; |
| 6724 | } |
| 6725 | |
| 6726 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 6727 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 6728 | struct proxy *defpx, const char *file, int line, |
| 6729 | char **err) { |
| 6730 | int i = 1; |
| 6731 | |
| 6732 | if (*(args[i]) == 0) { |
| 6733 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 6734 | return -1; |
| 6735 | } |
| 6736 | while (*(args[i])) { |
| 6737 | if (!strcmp(args[i], "no-sslv3")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6738 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_SSLV3; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6739 | else if (!strcmp(args[i], "no-tlsv10")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6740 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV10; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6741 | else if (!strcmp(args[i], "no-tlsv11")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6742 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV11; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6743 | else if (!strcmp(args[i], "no-tlsv12")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6744 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV12; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6745 | else if (!strcmp(args[i], "force-sslv3")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6746 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_USE_SSLV3; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6747 | else if (!strcmp(args[i], "force-tlsv10")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6748 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV10; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6749 | else if (!strcmp(args[i], "force-tlsv11")) { |
| 6750 | #if SSL_OP_NO_TLSv1_1 |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6751 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV11; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6752 | #else |
| 6753 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.1", args[0], args[i]); |
| 6754 | return -1; |
| 6755 | #endif |
| 6756 | } |
| 6757 | else if (!strcmp(args[i], "force-tlsv12")) { |
| 6758 | #if SSL_OP_NO_TLSv1_2 |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6759 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV12; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6760 | #else |
| 6761 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.2", args[0], args[i]); |
| 6762 | return -1; |
| 6763 | #endif |
| 6764 | } |
| 6765 | else if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6766 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6767 | else { |
| 6768 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 6769 | return -1; |
| 6770 | } |
| 6771 | i++; |
| 6772 | } |
| 6773 | return 0; |
| 6774 | } |
| 6775 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 6776 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 6777 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6778 | */ |
| 6779 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 6780 | struct proxy *defpx, const char *file, int line, |
| 6781 | char **err) |
| 6782 | { |
| 6783 | char **target; |
| 6784 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6785 | 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] | 6786 | |
| 6787 | if (too_many_args(1, args, err, NULL)) |
| 6788 | return -1; |
| 6789 | |
| 6790 | if (*target) { |
| 6791 | memprintf(err, "'%s' already specified.", args[0]); |
| 6792 | return -1; |
| 6793 | } |
| 6794 | |
| 6795 | if (*(args[1]) == 0) { |
| 6796 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 6797 | return -1; |
| 6798 | } |
| 6799 | *target = strdup(args[1]); |
| 6800 | return 0; |
| 6801 | } |
| 6802 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 6803 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 6804 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 6805 | */ |
| 6806 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 6807 | struct proxy *defpx, const char *file, int line, |
| 6808 | char **err) |
| 6809 | { |
| 6810 | char **target; |
| 6811 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6812 | 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] | 6813 | |
| 6814 | if (too_many_args(1, args, err, NULL)) |
| 6815 | return -1; |
| 6816 | |
| 6817 | if (*(args[1]) == 0) { |
| 6818 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 6819 | return -1; |
| 6820 | } |
| 6821 | |
| 6822 | free(*target); |
| 6823 | *target = strdup(args[1]); |
| 6824 | return 0; |
| 6825 | } |
| 6826 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6827 | /* parse various global tune.ssl settings consisting in positive integers. |
| 6828 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6829 | */ |
| 6830 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 6831 | struct proxy *defpx, const char *file, int line, |
| 6832 | char **err) |
| 6833 | { |
| 6834 | int *target; |
| 6835 | |
| 6836 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 6837 | target = &global.tune.sslcachesize; |
| 6838 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6839 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6840 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6841 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 6842 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 6843 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6844 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 6845 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6846 | else { |
| 6847 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 6848 | return -1; |
| 6849 | } |
| 6850 | |
| 6851 | if (too_many_args(1, args, err, NULL)) |
| 6852 | return -1; |
| 6853 | |
| 6854 | if (*(args[1]) == 0) { |
| 6855 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 6856 | return -1; |
| 6857 | } |
| 6858 | |
| 6859 | *target = atoi(args[1]); |
| 6860 | if (*target < 0) { |
| 6861 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 6862 | return -1; |
| 6863 | } |
| 6864 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6865 | } |
| 6866 | |
| 6867 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 6868 | struct proxy *defpx, const char *file, int line, |
| 6869 | char **err) |
| 6870 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 6871 | int ret; |
| 6872 | |
| 6873 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 6874 | if (ret != 0) |
| 6875 | return ret; |
| 6876 | |
| 6877 | if (pool2_ssl_capture) { |
| 6878 | memprintf(err, "'%s' is already configured.", args[0]); |
| 6879 | return -1; |
| 6880 | } |
| 6881 | |
| 6882 | pool2_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 6883 | if (!pool2_ssl_capture) { |
| 6884 | memprintf(err, "Out of memory error."); |
| 6885 | return -1; |
| 6886 | } |
| 6887 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6888 | } |
| 6889 | |
| 6890 | /* parse "ssl.force-private-cache". |
| 6891 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6892 | */ |
| 6893 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 6894 | struct proxy *defpx, const char *file, int line, |
| 6895 | char **err) |
| 6896 | { |
| 6897 | if (too_many_args(0, args, err, NULL)) |
| 6898 | return -1; |
| 6899 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6900 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6901 | return 0; |
| 6902 | } |
| 6903 | |
| 6904 | /* parse "ssl.lifetime". |
| 6905 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6906 | */ |
| 6907 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 6908 | struct proxy *defpx, const char *file, int line, |
| 6909 | char **err) |
| 6910 | { |
| 6911 | const char *res; |
| 6912 | |
| 6913 | if (too_many_args(1, args, err, NULL)) |
| 6914 | return -1; |
| 6915 | |
| 6916 | if (*(args[1]) == 0) { |
| 6917 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 6918 | return -1; |
| 6919 | } |
| 6920 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6921 | 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] | 6922 | if (res) { |
| 6923 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 6924 | return -1; |
| 6925 | } |
| 6926 | return 0; |
| 6927 | } |
| 6928 | |
| 6929 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 6930 | /* parse "ssl-dh-param-file". |
| 6931 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6932 | */ |
| 6933 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 6934 | struct proxy *defpx, const char *file, int line, |
| 6935 | char **err) |
| 6936 | { |
| 6937 | if (too_many_args(1, args, err, NULL)) |
| 6938 | return -1; |
| 6939 | |
| 6940 | if (*(args[1]) == 0) { |
| 6941 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 6942 | return -1; |
| 6943 | } |
| 6944 | |
| 6945 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 6946 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 6947 | return -1; |
| 6948 | } |
| 6949 | return 0; |
| 6950 | } |
| 6951 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6952 | /* parse "ssl.default-dh-param". |
| 6953 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6954 | */ |
| 6955 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 6956 | struct proxy *defpx, const char *file, int line, |
| 6957 | char **err) |
| 6958 | { |
| 6959 | if (too_many_args(1, args, err, NULL)) |
| 6960 | return -1; |
| 6961 | |
| 6962 | if (*(args[1]) == 0) { |
| 6963 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 6964 | return -1; |
| 6965 | } |
| 6966 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6967 | global_ssl.default_dh_param = atoi(args[1]); |
| 6968 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6969 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 6970 | return -1; |
| 6971 | } |
| 6972 | return 0; |
| 6973 | } |
| 6974 | #endif |
| 6975 | |
| 6976 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6977 | /* This function is used with TLS ticket keys management. It permits to browse |
| 6978 | * each reference. The variable <getnext> must contain the current node, |
| 6979 | * <end> point to the root node. |
| 6980 | */ |
| 6981 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6982 | static inline |
| 6983 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 6984 | { |
| 6985 | struct tls_keys_ref *ref = getnext; |
| 6986 | |
| 6987 | while (1) { |
| 6988 | |
| 6989 | /* Get next list entry. */ |
| 6990 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 6991 | |
| 6992 | /* If the entry is the last of the list, return NULL. */ |
| 6993 | if (&ref->list == end) |
| 6994 | return NULL; |
| 6995 | |
| 6996 | return ref; |
| 6997 | } |
| 6998 | } |
| 6999 | |
| 7000 | static inline |
| 7001 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 7002 | { |
| 7003 | int id; |
| 7004 | char *error; |
| 7005 | |
| 7006 | /* If the reference starts by a '#', this is numeric id. */ |
| 7007 | if (reference[0] == '#') { |
| 7008 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 7009 | id = strtol(reference + 1, &error, 10); |
| 7010 | if (*error != '\0') |
| 7011 | return NULL; |
| 7012 | |
| 7013 | /* Perform the unique id lookup. */ |
| 7014 | return tlskeys_ref_lookupid(id); |
| 7015 | } |
| 7016 | |
| 7017 | /* Perform the string lookup. */ |
| 7018 | return tlskeys_ref_lookup(reference); |
| 7019 | } |
| 7020 | #endif |
| 7021 | |
| 7022 | |
| 7023 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7024 | |
| 7025 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 7026 | |
| 7027 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 7028 | return cli_io_handler_tlskeys_files(appctx); |
| 7029 | } |
| 7030 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7031 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 7032 | * (next index to be dumped), and cli.p0 (next key reference). |
| 7033 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7034 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 7035 | |
| 7036 | struct stream_interface *si = appctx->owner; |
| 7037 | |
| 7038 | switch (appctx->st2) { |
| 7039 | case STAT_ST_INIT: |
| 7040 | /* Display the column headers. If the message cannot be sent, |
| 7041 | * quit the fucntion with returning 0. The function is called |
| 7042 | * later and restart at the state "STAT_ST_INIT". |
| 7043 | */ |
| 7044 | chunk_reset(&trash); |
| 7045 | |
| 7046 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 7047 | chunk_appendf(&trash, "# id secret\n"); |
| 7048 | else |
| 7049 | chunk_appendf(&trash, "# id (file)\n"); |
| 7050 | |
| 7051 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 7052 | si_applet_cant_put(si); |
| 7053 | return 0; |
| 7054 | } |
| 7055 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7056 | /* Now, we start the browsing of the references lists. |
| 7057 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 7058 | * available field of this pointer is <list>. It is used with the function |
| 7059 | * tlskeys_list_get_next() for retruning the first available entry |
| 7060 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7061 | if (appctx->ctx.cli.p0 == NULL) { |
| 7062 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 7063 | 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] | 7064 | } |
| 7065 | |
| 7066 | appctx->st2 = STAT_ST_LIST; |
| 7067 | /* fall through */ |
| 7068 | |
| 7069 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7070 | while (appctx->ctx.cli.p0) { |
| 7071 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
| 7072 | int head = ref->tls_ticket_enc_index; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7073 | |
| 7074 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7075 | 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] | 7076 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7077 | |
| 7078 | if (appctx->ctx.cli.i1 == 0) |
| 7079 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 7080 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7081 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7082 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7083 | struct chunk *t2 = get_trash_chunk(); |
| 7084 | |
| 7085 | chunk_reset(t2); |
| 7086 | /* should never fail here because we dump only a key in the t2 buffer */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7087 | t2->len = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7088 | sizeof(struct tls_sess_key), t2->str, t2->size); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7089 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, t2->str); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7090 | |
| 7091 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 7092 | /* let's try again later from this stream. We add ourselves into |
| 7093 | * this stream's users so that it can remove us upon termination. |
| 7094 | */ |
| 7095 | si_applet_cant_put(si); |
| 7096 | return 0; |
| 7097 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7098 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7099 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7100 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7101 | } |
| 7102 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 7103 | /* let's try again later from this stream. We add ourselves into |
| 7104 | * this stream's users so that it can remove us upon termination. |
| 7105 | */ |
| 7106 | si_applet_cant_put(si); |
| 7107 | return 0; |
| 7108 | } |
| 7109 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7110 | 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] | 7111 | break; |
| 7112 | |
| 7113 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7114 | 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] | 7115 | } |
| 7116 | |
| 7117 | appctx->st2 = STAT_ST_FIN; |
| 7118 | /* fall through */ |
| 7119 | |
| 7120 | default: |
| 7121 | appctx->st2 = STAT_ST_FIN; |
| 7122 | return 1; |
| 7123 | } |
| 7124 | return 0; |
| 7125 | } |
| 7126 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7127 | /* sets cli.i0 to non-zero if only file lists should be dumped */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7128 | static int cli_parse_show_tlskeys(char **args, struct appctx *appctx, void *private) |
| 7129 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7130 | /* no parameter, shows only file list */ |
| 7131 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7132 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7133 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 7134 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7135 | } |
| 7136 | |
| 7137 | if (args[2][0] == '*') { |
| 7138 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7139 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7140 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7141 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
| 7142 | if (!appctx->ctx.cli.p0) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7143 | 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] | 7144 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7145 | return 1; |
| 7146 | } |
| 7147 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7148 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 7149 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7150 | } |
| 7151 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7152 | static int cli_parse_set_tlskeys(char **args, struct appctx *appctx, void *private) |
| 7153 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7154 | struct tls_keys_ref *ref; |
| 7155 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7156 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 7157 | if (!*args[3] || !*args[4]) { |
| 7158 | 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] | 7159 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7160 | return 1; |
| 7161 | } |
| 7162 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7163 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 7164 | if (!ref) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7165 | 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] | 7166 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7167 | return 1; |
| 7168 | } |
| 7169 | |
| 7170 | trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size); |
| 7171 | if (trash.len != sizeof(struct tls_sess_key)) { |
| 7172 | 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] | 7173 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7174 | return 1; |
| 7175 | } |
| 7176 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7177 | memcpy(ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len); |
| 7178 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7179 | |
| 7180 | appctx->ctx.cli.msg = "TLS ticket key updated!"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 7181 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7182 | return 1; |
| 7183 | |
| 7184 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7185 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7186 | |
| 7187 | static int cli_parse_set_ocspresponse(char **args, struct appctx *appctx, void *private) |
| 7188 | { |
| 7189 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 7190 | char *err = NULL; |
| 7191 | |
| 7192 | /* Expect one parameter: the new response in base64 encoding */ |
| 7193 | if (!*args[3]) { |
| 7194 | 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] | 7195 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7196 | return 1; |
| 7197 | } |
| 7198 | |
| 7199 | trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size); |
| 7200 | if (trash.len < 0) { |
| 7201 | 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] | 7202 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7203 | return 1; |
| 7204 | } |
| 7205 | |
| 7206 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
| 7207 | if (err) { |
| 7208 | memprintf(&err, "%s.\n", err); |
| 7209 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 7210 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7211 | } |
| 7212 | return 1; |
| 7213 | } |
| 7214 | appctx->ctx.cli.msg = "OCSP Response updated!"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 7215 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7216 | return 1; |
| 7217 | #else |
| 7218 | 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] | 7219 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7220 | return 1; |
| 7221 | #endif |
| 7222 | |
| 7223 | } |
| 7224 | |
| 7225 | /* register cli keywords */ |
| 7226 | static struct cli_kw_list cli_kws = {{ },{ |
| 7227 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7228 | { { "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 }, |
| 7229 | { { "set", "ssl", "tls-keys", NULL }, NULL, cli_parse_set_tlskeys, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7230 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7231 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7232 | { { NULL }, NULL, NULL, NULL } |
| 7233 | }}; |
| 7234 | |
| 7235 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7236 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7237 | * Please take care of keeping this list alphabetically sorted. |
| 7238 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 7239 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7240 | { "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] | 7241 | { "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] | 7242 | { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 7243 | { "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] | 7244 | { "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] | 7245 | { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV }, |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7246 | { "ssl_bc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 7247 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 7248 | { "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] | 7249 | { "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] | 7250 | { "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] | 7251 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7252 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7253 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7254 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7255 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7256 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7257 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 7258 | { "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] | 7259 | { "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] | 7260 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 7261 | { "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] | 7262 | { "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] | 7263 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7264 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7265 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7266 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7267 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7268 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7269 | { "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] | 7270 | { "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] | 7271 | { "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] | 7272 | { "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] | 7273 | { "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] | 7274 | { "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] | 7275 | { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 7276 | { "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] | 7277 | { "ssl_fc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7278 | #ifdef OPENSSL_NPN_NEGOTIATED |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7279 | { "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] | 7280 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7281 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7282 | { "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] | 7283 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7284 | { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 7285 | { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 7286 | { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7287 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 7288 | { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7289 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7290 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 7291 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7292 | { "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] | 7293 | { NULL, NULL, 0, 0, 0 }, |
| 7294 | }}; |
| 7295 | |
| 7296 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7297 | * Please take care of keeping this list alphabetically sorted. |
| 7298 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 7299 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 7300 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 7301 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 7302 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7303 | }}; |
| 7304 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7305 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7306 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 7307 | * all code contributors. |
| 7308 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 7309 | * the config parser can report an appropriate error when a known keyword was |
| 7310 | * not enabled. |
| 7311 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7312 | static struct ssl_bind_kw ssl_bind_kws[] = { |
| 7313 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 7314 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 7315 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 7316 | { "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] | 7317 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7318 | { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7319 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 7320 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 7321 | { NULL, NULL, 0 }, |
| 7322 | }; |
| 7323 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 7324 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7325 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 7326 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 7327 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7328 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 7329 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7330 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 7331 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 7332 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 7333 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 7334 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 7335 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7336 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 7337 | { "force-sslv3", bind_parse_force_sslv3, 0 }, /* force SSLv3 */ |
| 7338 | { "force-tlsv10", bind_parse_force_tlsv10, 0 }, /* force TLSv10 */ |
| 7339 | { "force-tlsv11", bind_parse_force_tlsv11, 0 }, /* force TLSv11 */ |
| 7340 | { "force-tlsv12", bind_parse_force_tlsv12, 0 }, /* force TLSv12 */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7341 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7342 | { "no-sslv3", bind_parse_no_sslv3, 0 }, /* disable SSLv3 */ |
| 7343 | { "no-tlsv10", bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */ |
| 7344 | { "no-tlsv11", bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */ |
| 7345 | { "no-tlsv12", bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */ |
| 7346 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 7347 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
| 7348 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 7349 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 7350 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 7351 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7352 | { NULL, NULL, 0 }, |
| 7353 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7354 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7355 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7356 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 7357 | * all code contributors. |
| 7358 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 7359 | * the config parser can report an appropriate error when a known keyword was |
| 7360 | * not enabled. |
| 7361 | */ |
| 7362 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7363 | { "ca-file", srv_parse_ca_file, 1, 0 }, /* set CAfile to process verify server cert */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 7364 | { "check-ssl", srv_parse_check_ssl, 0, 0 }, /* enable SSL for health checks */ |
| 7365 | { "ciphers", srv_parse_ciphers, 1, 0 }, /* select the cipher suite */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7366 | { "crl-file", srv_parse_crl_file, 1, 0 }, /* set certificate revocation list file use on server cert verify */ |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 7367 | { "crt", srv_parse_crt, 1, 0 }, /* set client certificate */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 7368 | { "force-sslv3", srv_parse_force_sslv3, 0, 0 }, /* force SSLv3 */ |
| 7369 | { "force-tlsv10", srv_parse_force_tlsv10, 0, 0 }, /* force TLSv10 */ |
| 7370 | { "force-tlsv11", srv_parse_force_tlsv11, 0, 0 }, /* force TLSv11 */ |
| 7371 | { "force-tlsv12", srv_parse_force_tlsv12, 0, 0 }, /* force TLSv12 */ |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 7372 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 0 }, /* disable session reuse */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 7373 | { "no-sslv3", srv_parse_no_sslv3, 0, 0 }, /* disable SSLv3 */ |
| 7374 | { "no-tlsv10", srv_parse_no_tlsv10, 0, 0 }, /* disable TLSv10 */ |
| 7375 | { "no-tlsv11", srv_parse_no_tlsv11, 0, 0 }, /* disable TLSv11 */ |
| 7376 | { "no-tlsv12", srv_parse_no_tlsv12, 0, 0 }, /* disable TLSv12 */ |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7377 | { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 0 }, /* disable session resumption tickets */ |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7378 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 0 }, /* send PROXY protocol header v2 with SSL info */ |
| 7379 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 0 }, /* send PROXY protocol header v2 with CN */ |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 7380 | { "sni", srv_parse_sni, 1, 0 }, /* send SNI extension */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 7381 | { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7382 | { "verify", srv_parse_verify, 1, 0 }, /* set SSL verify method */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 7383 | { "verifyhost", srv_parse_verifyhost, 1, 0 }, /* require that SSL cert verifies for hostname */ |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7384 | { NULL, NULL, 0, 0 }, |
| 7385 | }}; |
| 7386 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7387 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 7388 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 7389 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 7390 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7391 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 7392 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 7393 | #ifndef OPENSSL_NO_DH |
| 7394 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 7395 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7396 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 7397 | #ifndef OPENSSL_NO_DH |
| 7398 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 7399 | #endif |
| 7400 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 7401 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 7402 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 7403 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7404 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 7405 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 7406 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7407 | { 0, NULL, NULL }, |
| 7408 | }}; |
| 7409 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 7410 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 7411 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7412 | .snd_buf = ssl_sock_from_buf, |
| 7413 | .rcv_buf = ssl_sock_to_buf, |
| 7414 | .rcv_pipe = NULL, |
| 7415 | .snd_pipe = NULL, |
| 7416 | .shutr = NULL, |
| 7417 | .shutw = ssl_sock_shutw, |
| 7418 | .close = ssl_sock_close, |
| 7419 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 7420 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 7421 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 7422 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 7423 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 7424 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7425 | }; |
| 7426 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 7427 | #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] | 7428 | |
| 7429 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7430 | { |
| 7431 | if (ptr) { |
| 7432 | chunk_destroy(ptr); |
| 7433 | free(ptr); |
| 7434 | } |
| 7435 | } |
| 7436 | |
| 7437 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7438 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7439 | { |
| 7440 | pool_free2(pool2_ssl_capture, ptr); |
| 7441 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 7442 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7443 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7444 | static void __ssl_sock_init(void) |
| 7445 | { |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7446 | char *ptr; |
| 7447 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7448 | STACK_OF(SSL_COMP)* cm; |
| 7449 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7450 | if (global_ssl.listen_default_ciphers) |
| 7451 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 7452 | if (global_ssl.connect_default_ciphers) |
| 7453 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 7454 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 7455 | xprt_register(XPRT_SSL, &ssl_sock); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7456 | SSL_library_init(); |
| 7457 | cm = SSL_COMP_get_compression_methods(); |
| 7458 | sk_SSL_COMP_zero(cm); |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 7459 | #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] | 7460 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 7461 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7462 | ssl_capture_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7463 | sample_register_fetches(&sample_fetch_keywords); |
| 7464 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7465 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7466 | srv_register_keywords(&srv_kws); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7467 | cfg_register_keywords(&cfg_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7468 | cli_register_kw(&cli_kws); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 7469 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7470 | hap_register_post_check(tlskeys_finalize_config); |
| 7471 | #endif |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 7472 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7473 | ptr = NULL; |
| 7474 | memprintf(&ptr, "Built with OpenSSL version : " |
| 7475 | #ifdef OPENSSL_IS_BORINGSSL |
| 7476 | "BoringSSL\n"); |
| 7477 | #else /* OPENSSL_IS_BORINGSSL */ |
| 7478 | OPENSSL_VERSION_TEXT |
| 7479 | "\nRunning on OpenSSL version : %s%s", |
| 7480 | SSLeay_version(SSLEAY_VERSION), |
| 7481 | ((OPENSSL_VERSION_NUMBER ^ SSLeay()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
| 7482 | #endif |
| 7483 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
| 7484 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 7485 | "no (library version too old)" |
| 7486 | #elif defined(OPENSSL_NO_TLSEXT) |
| 7487 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 7488 | #else |
| 7489 | "yes" |
| 7490 | #endif |
| 7491 | "", ptr); |
| 7492 | |
| 7493 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 7494 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7495 | "yes" |
| 7496 | #else |
| 7497 | #ifdef OPENSSL_NO_TLSEXT |
| 7498 | "no (because of OPENSSL_NO_TLSEXT)" |
| 7499 | #else |
| 7500 | "no (version might be too old, 0.9.8f min needed)" |
| 7501 | #endif |
| 7502 | #endif |
| 7503 | "", ptr); |
| 7504 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7505 | hap_register_build_opts(ptr, 1); |
| 7506 | |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 7507 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 7508 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 7509 | |
| 7510 | #ifndef OPENSSL_NO_DH |
| 7511 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 7512 | #endif |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7513 | |
| 7514 | /* Load SSL string for the verbose & debug mode. */ |
| 7515 | ERR_load_SSL_strings(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7516 | } |
| 7517 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7518 | __attribute__((destructor)) |
| 7519 | static void __ssl_sock_deinit(void) |
| 7520 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7521 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7522 | lru64_destroy(ssl_ctx_lru_tree); |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 7523 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7524 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7525 | #ifndef OPENSSL_NO_DH |
| 7526 | if (local_dh_1024) { |
| 7527 | DH_free(local_dh_1024); |
| 7528 | local_dh_1024 = NULL; |
| 7529 | } |
| 7530 | |
| 7531 | if (local_dh_2048) { |
| 7532 | DH_free(local_dh_2048); |
| 7533 | local_dh_2048 = NULL; |
| 7534 | } |
| 7535 | |
| 7536 | if (local_dh_4096) { |
| 7537 | DH_free(local_dh_4096); |
| 7538 | local_dh_4096 = NULL; |
| 7539 | } |
| 7540 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 7541 | if (global_dh) { |
| 7542 | DH_free(global_dh); |
| 7543 | global_dh = NULL; |
| 7544 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7545 | #endif |
| 7546 | |
| 7547 | ERR_remove_state(0); |
| 7548 | ERR_free_strings(); |
| 7549 | |
| 7550 | EVP_cleanup(); |
| 7551 | |
| 7552 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 7553 | CRYPTO_cleanup_all_ex_data(); |
| 7554 | #endif |
| 7555 | } |
| 7556 | |
| 7557 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7558 | /* |
| 7559 | * Local variables: |
| 7560 | * c-indent-level: 8 |
| 7561 | * c-basic-offset: 8 |
| 7562 | * End: |
| 7563 | */ |