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. */ |
| 149 | } global_ssl = { |
| 150 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 151 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 152 | #endif |
| 153 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 154 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 155 | #endif |
| 156 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 157 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 158 | |
| 159 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 160 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 161 | #endif |
| 162 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 163 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
| 164 | }; |
| 165 | |
| 166 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 167 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 168 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 169 | #endif |
| 170 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 171 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 172 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 173 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 174 | static DH *local_dh_1024 = NULL; |
| 175 | static DH *local_dh_2048 = NULL; |
| 176 | static DH *local_dh_4096 = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 177 | #endif /* OPENSSL_NO_DH */ |
| 178 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 179 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 180 | /* X509V3 Extensions that will be added on generated certificates */ |
| 181 | #define X509V3_EXT_SIZE 5 |
| 182 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 183 | "basicConstraints", |
| 184 | "nsComment", |
| 185 | "subjectKeyIdentifier", |
| 186 | "authorityKeyIdentifier", |
| 187 | "keyUsage", |
| 188 | }; |
| 189 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 190 | "CA:FALSE", |
| 191 | "\"OpenSSL Generated Certificate\"", |
| 192 | "hash", |
| 193 | "keyid,issuer:always", |
| 194 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 195 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 196 | /* LRU cache to store generated certificate */ |
| 197 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 198 | static unsigned int ssl_ctx_lru_seed = 0; |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 199 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 200 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 201 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 202 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 203 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 204 | /* The order here matters for picking a default context, |
| 205 | * keep the most common keytype at the bottom of the list |
| 206 | */ |
| 207 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 208 | "dsa", |
| 209 | "ecdsa", |
| 210 | "rsa" |
| 211 | }; |
| 212 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 213 | #else |
| 214 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 215 | #endif |
| 216 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 217 | /* |
| 218 | * This function gives the detail of the SSL error. It is used only |
| 219 | * if the debug mode and the verbose mode are activated. It dump all |
| 220 | * the SSL error until the stack was empty. |
| 221 | */ |
| 222 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 223 | { |
| 224 | unsigned long ret; |
| 225 | |
| 226 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 227 | while(1) { |
| 228 | ret = ERR_get_error(); |
| 229 | if (ret == 0) |
| 230 | return; |
| 231 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
| 232 | (unsigned short)conn->t.sock.fd, ret, |
| 233 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 238 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 239 | /* |
| 240 | * struct alignment works here such that the key.key is the same as key_data |
| 241 | * Do not change the placement of key_data |
| 242 | */ |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 243 | struct certificate_ocsp { |
| 244 | struct ebmb_node key; |
| 245 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 246 | struct chunk response; |
| 247 | long expire; |
| 248 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 249 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 250 | struct ocsp_cbk_arg { |
| 251 | int is_single; |
| 252 | int single_kt; |
| 253 | union { |
| 254 | struct certificate_ocsp *s_ocsp; |
| 255 | /* |
| 256 | * m_ocsp will have multiple entries dependent on key type |
| 257 | * Entry 0 - DSA |
| 258 | * Entry 1 - ECDSA |
| 259 | * Entry 2 - RSA |
| 260 | */ |
| 261 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 262 | }; |
| 263 | }; |
| 264 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 265 | /* |
| 266 | * This function returns the number of seconds elapsed |
| 267 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 268 | * date presented un ASN1_GENERALIZEDTIME. |
| 269 | * |
| 270 | * In parsing error case, it returns -1. |
| 271 | */ |
| 272 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 273 | { |
| 274 | long epoch; |
| 275 | char *p, *end; |
| 276 | const unsigned short month_offset[12] = { |
| 277 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 278 | }; |
| 279 | int year, month; |
| 280 | |
| 281 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 282 | |
| 283 | p = (char *)d->data; |
| 284 | end = p + d->length; |
| 285 | |
| 286 | if (end - p < 4) return -1; |
| 287 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 288 | p += 4; |
| 289 | if (end - p < 2) return -1; |
| 290 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 291 | if (month < 1 || month > 12) return -1; |
| 292 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 293 | We consider leap years and the current month (<marsh or not) */ |
| 294 | epoch = ( ((year - 1970) * 365) |
| 295 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 296 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 297 | + month_offset[month-1] |
| 298 | ) * 24 * 60 * 60; |
| 299 | p += 2; |
| 300 | if (end - p < 2) return -1; |
| 301 | /* Add the number of seconds of completed days of current month */ |
| 302 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 303 | p += 2; |
| 304 | if (end - p < 2) return -1; |
| 305 | /* Add the completed hours of the current day */ |
| 306 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 307 | p += 2; |
| 308 | if (end - p < 2) return -1; |
| 309 | /* Add the completed minutes of the current hour */ |
| 310 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 311 | p += 2; |
| 312 | if (p == end) return -1; |
| 313 | /* Test if there is available seconds */ |
| 314 | if (p[0] < '0' || p[0] > '9') |
| 315 | goto nosec; |
| 316 | if (end - p < 2) return -1; |
| 317 | /* Add the seconds of the current minute */ |
| 318 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 319 | p += 2; |
| 320 | if (p == end) return -1; |
| 321 | /* Ignore seconds float part if present */ |
| 322 | if (p[0] == '.') { |
| 323 | do { |
| 324 | if (++p == end) return -1; |
| 325 | } while (p[0] >= '0' && p[0] <= '9'); |
| 326 | } |
| 327 | |
| 328 | nosec: |
| 329 | if (p[0] == 'Z') { |
| 330 | if (end - p != 1) return -1; |
| 331 | return epoch; |
| 332 | } |
| 333 | else if (p[0] == '+') { |
| 334 | if (end - p != 5) return -1; |
| 335 | /* Apply timezone offset */ |
| 336 | return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 337 | } |
| 338 | else if (p[0] == '-') { |
| 339 | if (end - p != 5) return -1; |
| 340 | /* Apply timezone offset */ |
| 341 | return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 342 | } |
| 343 | |
| 344 | return -1; |
| 345 | } |
| 346 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 347 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 348 | |
| 349 | /* This function starts to check if the OCSP response (in DER format) contained |
| 350 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 351 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 352 | * contained in the OCSP Response and exits on error if no match. |
| 353 | * If it's a valid OCSP Response: |
| 354 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 355 | * pointed by 'ocsp'. |
| 356 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 357 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 358 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 359 | * already present in the container, it will be overwritten. |
| 360 | * |
| 361 | * Note: OCSP response containing more than one OCSP Single response is not |
| 362 | * considered valid. |
| 363 | * |
| 364 | * Returns 0 on success, 1 in error case. |
| 365 | */ |
| 366 | static int ssl_sock_load_ocsp_response(struct chunk *ocsp_response, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 367 | { |
| 368 | OCSP_RESPONSE *resp; |
| 369 | OCSP_BASICRESP *bs = NULL; |
| 370 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 371 | OCSP_CERTID *id; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 372 | unsigned char *p = (unsigned char *)ocsp_response->str; |
| 373 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 374 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 375 | int reason; |
| 376 | int ret = 1; |
| 377 | |
| 378 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, ocsp_response->len); |
| 379 | if (!resp) { |
| 380 | memprintf(err, "Unable to parse OCSP response"); |
| 381 | goto out; |
| 382 | } |
| 383 | |
| 384 | rc = OCSP_response_status(resp); |
| 385 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 386 | memprintf(err, "OCSP response status not successful"); |
| 387 | goto out; |
| 388 | } |
| 389 | |
| 390 | bs = OCSP_response_get1_basic(resp); |
| 391 | if (!bs) { |
| 392 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 393 | goto out; |
| 394 | } |
| 395 | |
| 396 | count_sr = OCSP_resp_count(bs); |
| 397 | if (count_sr > 1) { |
| 398 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 399 | goto out; |
| 400 | } |
| 401 | |
| 402 | sr = OCSP_resp_get0(bs, 0); |
| 403 | if (!sr) { |
| 404 | memprintf(err, "Failed to get OCSP single response"); |
| 405 | goto out; |
| 406 | } |
| 407 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 408 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 409 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 410 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
| 411 | if (rc != V_OCSP_CERTSTATUS_GOOD) { |
| 412 | memprintf(err, "OCSP single response: certificate status not good"); |
| 413 | goto out; |
| 414 | } |
| 415 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 416 | if (!nextupd) { |
| 417 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 418 | goto out; |
| 419 | } |
| 420 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 421 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 422 | if (!rc) { |
| 423 | memprintf(err, "OCSP single response: no longer valid."); |
| 424 | goto out; |
| 425 | } |
| 426 | |
| 427 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 428 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 429 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 430 | goto out; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (!ocsp) { |
| 435 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 436 | unsigned char *p; |
| 437 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 438 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 439 | if (!rc) { |
| 440 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 441 | goto out; |
| 442 | } |
| 443 | |
| 444 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 445 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 446 | goto out; |
| 447 | } |
| 448 | |
| 449 | p = key; |
| 450 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 451 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 452 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 453 | if (!ocsp) { |
| 454 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 455 | goto out; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /* According to comments on "chunk_dup", the |
| 460 | previous chunk buffer will be freed */ |
| 461 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 462 | memprintf(err, "OCSP response: Memory allocation error"); |
| 463 | goto out; |
| 464 | } |
| 465 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 466 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 467 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 468 | ret = 0; |
| 469 | out: |
| 470 | if (bs) |
| 471 | OCSP_BASICRESP_free(bs); |
| 472 | |
| 473 | if (resp) |
| 474 | OCSP_RESPONSE_free(resp); |
| 475 | |
| 476 | return ret; |
| 477 | } |
| 478 | /* |
| 479 | * External function use to update the OCSP response in the OCSP response's |
| 480 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 481 | * to update in DER format. |
| 482 | * |
| 483 | * Returns 0 on success, 1 in error case. |
| 484 | */ |
| 485 | int ssl_sock_update_ocsp_response(struct chunk *ocsp_response, char **err) |
| 486 | { |
| 487 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * This function load the OCSP Resonse in DER format contained in file at |
| 492 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 493 | * |
| 494 | * Returns 0 on success, 1 in error case. |
| 495 | */ |
| 496 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 497 | { |
| 498 | int fd = -1; |
| 499 | int r = 0; |
| 500 | int ret = 1; |
| 501 | |
| 502 | fd = open(ocsp_path, O_RDONLY); |
| 503 | if (fd == -1) { |
| 504 | memprintf(err, "Error opening OCSP response file"); |
| 505 | goto end; |
| 506 | } |
| 507 | |
| 508 | trash.len = 0; |
| 509 | while (trash.len < trash.size) { |
| 510 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 511 | if (r < 0) { |
| 512 | if (errno == EINTR) |
| 513 | continue; |
| 514 | |
| 515 | memprintf(err, "Error reading OCSP response from file"); |
| 516 | goto end; |
| 517 | } |
| 518 | else if (r == 0) { |
| 519 | break; |
| 520 | } |
| 521 | trash.len += r; |
| 522 | } |
| 523 | |
| 524 | close(fd); |
| 525 | fd = -1; |
| 526 | |
| 527 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 528 | end: |
| 529 | if (fd != -1) |
| 530 | close(fd); |
| 531 | |
| 532 | return ret; |
| 533 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 534 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 535 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 536 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 537 | 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) |
| 538 | { |
| 539 | struct tls_sess_key *keys; |
| 540 | struct connection *conn; |
| 541 | int head; |
| 542 | int i; |
| 543 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 544 | conn = SSL_get_app_data(s); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 545 | keys = objt_listener(conn->target)->bind_conf->keys_ref->tlskeys; |
| 546 | 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] | 547 | |
| 548 | if (enc) { |
| 549 | memcpy(key_name, keys[head].name, 16); |
| 550 | |
| 551 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
| 552 | return -1; |
| 553 | |
| 554 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].aes_key, iv)) |
| 555 | return -1; |
| 556 | |
| 557 | HMAC_Init_ex(hctx, keys[head].hmac_key, 16, HASH_FUNCT(), NULL); |
| 558 | |
| 559 | return 1; |
| 560 | } else { |
| 561 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 562 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 563 | goto found; |
| 564 | } |
| 565 | return 0; |
| 566 | |
| 567 | found: |
| 568 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].hmac_key, 16, HASH_FUNCT(), NULL); |
| 569 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].aes_key, iv)) |
| 570 | return -1; |
| 571 | /* 2 for key renewal, 1 if current key is still valid */ |
| 572 | return i ? 2 : 1; |
| 573 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 577 | { |
| 578 | struct tls_keys_ref *ref; |
| 579 | |
| 580 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 581 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 582 | return ref; |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 587 | { |
| 588 | struct tls_keys_ref *ref; |
| 589 | |
| 590 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 591 | if (ref->unique_id == unique_id) |
| 592 | return ref; |
| 593 | return NULL; |
| 594 | } |
| 595 | |
| 596 | int ssl_sock_update_tlskey(char *filename, struct chunk *tlskey, char **err) { |
| 597 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 598 | |
| 599 | if(!ref) { |
| 600 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 601 | return 1; |
| 602 | } |
| 603 | |
Pradeep Jindal | cc79b00 | 2015-08-20 18:25:17 +0530 | [diff] [blame] | 604 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), tlskey->str, tlskey->len); |
| 605 | 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] | 606 | |
| 607 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 608 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 609 | |
| 610 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 611 | * automatic ids. It's called just after the basic checks. It returns |
| 612 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 613 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 614 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 615 | { |
| 616 | int i = 0; |
| 617 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 618 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 619 | |
| 620 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 621 | if (ref->unique_id == -1) { |
| 622 | /* Look for the first free id. */ |
| 623 | while (1) { |
| 624 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 625 | if (ref2->unique_id == i) { |
| 626 | i++; |
| 627 | break; |
| 628 | } |
| 629 | } |
| 630 | if (&ref2->list == &tlskeys_reference) |
| 631 | break; |
| 632 | } |
| 633 | |
| 634 | /* Uses the unique id and increment it for the next entry. */ |
| 635 | ref->unique_id = i; |
| 636 | i++; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /* This sort the reference list by id. */ |
| 641 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 642 | LIST_DEL(&ref->list); |
| 643 | list_for_each_entry(ref3, &tkr, list) { |
| 644 | if (ref->unique_id < ref3->unique_id) { |
| 645 | LIST_ADDQ(&ref3->list, &ref->list); |
| 646 | break; |
| 647 | } |
| 648 | } |
| 649 | if (&ref3->list == &tkr) |
| 650 | LIST_ADDQ(&tkr, &ref->list); |
| 651 | } |
| 652 | |
| 653 | /* swap root */ |
| 654 | LIST_ADD(&tkr, &tlskeys_reference); |
| 655 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 656 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 657 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 658 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 659 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 660 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 661 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 662 | { |
| 663 | switch (evp_keytype) { |
| 664 | case EVP_PKEY_RSA: |
| 665 | return 2; |
| 666 | case EVP_PKEY_DSA: |
| 667 | return 0; |
| 668 | case EVP_PKEY_EC: |
| 669 | return 1; |
| 670 | } |
| 671 | |
| 672 | return -1; |
| 673 | } |
| 674 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 675 | /* |
| 676 | * Callback used to set OCSP status extension content in server hello. |
| 677 | */ |
| 678 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 679 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 680 | struct certificate_ocsp *ocsp; |
| 681 | struct ocsp_cbk_arg *ocsp_arg; |
| 682 | char *ssl_buf; |
| 683 | EVP_PKEY *ssl_pkey; |
| 684 | int key_type; |
| 685 | int index; |
| 686 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 687 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 688 | |
| 689 | ssl_pkey = SSL_get_privatekey(ssl); |
| 690 | if (!ssl_pkey) |
| 691 | return SSL_TLSEXT_ERR_NOACK; |
| 692 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 693 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 694 | |
| 695 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 696 | ocsp = ocsp_arg->s_ocsp; |
| 697 | else { |
| 698 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 699 | * the certificate type |
| 700 | */ |
| 701 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 702 | |
| 703 | if (index < 0) |
| 704 | return SSL_TLSEXT_ERR_NOACK; |
| 705 | |
| 706 | ocsp = ocsp_arg->m_ocsp[index]; |
| 707 | |
| 708 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 709 | |
| 710 | if (!ocsp || |
| 711 | !ocsp->response.str || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 712 | !ocsp->response.len || |
| 713 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 714 | return SSL_TLSEXT_ERR_NOACK; |
| 715 | |
| 716 | ssl_buf = OPENSSL_malloc(ocsp->response.len); |
| 717 | if (!ssl_buf) |
| 718 | return SSL_TLSEXT_ERR_NOACK; |
| 719 | |
| 720 | memcpy(ssl_buf, ocsp->response.str, ocsp->response.len); |
| 721 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.len); |
| 722 | |
| 723 | return SSL_TLSEXT_ERR_OK; |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 728 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 729 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 730 | * It should be present in the certificate's extra chain builded from file |
| 731 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 732 | * named 'cert_path' suffixed using '.issuer'. |
| 733 | * |
| 734 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 735 | * response. If file is empty or content is not a valid OCSP response, |
| 736 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 737 | * is displayed). |
| 738 | * |
| 739 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
| 740 | * succesfully enabled, or -1 in other error case. |
| 741 | */ |
| 742 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 743 | { |
| 744 | |
| 745 | BIO *in = NULL; |
| 746 | X509 *x, *xi = NULL, *issuer = NULL; |
| 747 | STACK_OF(X509) *chain = NULL; |
| 748 | OCSP_CERTID *cid = NULL; |
| 749 | SSL *ssl; |
| 750 | char ocsp_path[MAXPATHLEN+1]; |
| 751 | int i, ret = -1; |
| 752 | struct stat st; |
| 753 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 754 | char *warn = NULL; |
| 755 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 756 | pem_password_cb *passwd_cb; |
| 757 | void *passwd_cb_userdata; |
| 758 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 759 | |
| 760 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 761 | |
| 762 | if (stat(ocsp_path, &st)) |
| 763 | return 1; |
| 764 | |
| 765 | ssl = SSL_new(ctx); |
| 766 | if (!ssl) |
| 767 | goto out; |
| 768 | |
| 769 | x = SSL_get_certificate(ssl); |
| 770 | if (!x) |
| 771 | goto out; |
| 772 | |
| 773 | /* Try to lookup for issuer in certificate extra chain */ |
| 774 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 775 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 776 | #else |
| 777 | chain = ctx->extra_certs; |
| 778 | #endif |
| 779 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 780 | issuer = sk_X509_value(chain, i); |
| 781 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 782 | break; |
| 783 | else |
| 784 | issuer = NULL; |
| 785 | } |
| 786 | |
| 787 | /* If not found try to load issuer from a suffixed file */ |
| 788 | if (!issuer) { |
| 789 | char issuer_path[MAXPATHLEN+1]; |
| 790 | |
| 791 | in = BIO_new(BIO_s_file()); |
| 792 | if (!in) |
| 793 | goto out; |
| 794 | |
| 795 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 796 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 797 | goto out; |
| 798 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 799 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 800 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 801 | |
| 802 | 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] | 803 | if (!xi) |
| 804 | goto out; |
| 805 | |
| 806 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 807 | goto out; |
| 808 | |
| 809 | issuer = xi; |
| 810 | } |
| 811 | |
| 812 | cid = OCSP_cert_to_id(0, x, issuer); |
| 813 | if (!cid) |
| 814 | goto out; |
| 815 | |
| 816 | i = i2d_OCSP_CERTID(cid, NULL); |
| 817 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 818 | goto out; |
| 819 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 820 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 821 | if (!ocsp) |
| 822 | goto out; |
| 823 | |
| 824 | p = ocsp->key_data; |
| 825 | i2d_OCSP_CERTID(cid, &p); |
| 826 | |
| 827 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 828 | if (iocsp == ocsp) |
| 829 | ocsp = NULL; |
| 830 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 831 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 832 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 833 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 834 | #endif |
| 835 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 836 | |
| 837 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 838 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 839 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 840 | |
| 841 | cb_arg->is_single = 1; |
| 842 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 843 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 844 | pkey = X509_get_pubkey(x); |
| 845 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 846 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 847 | |
| 848 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 849 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 850 | } else { |
| 851 | /* |
| 852 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 853 | * Update that cb_arg with the new cert's staple |
| 854 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 855 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 856 | struct certificate_ocsp *tmp_ocsp; |
| 857 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 858 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 859 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 860 | |
| 861 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 862 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 863 | #else |
| 864 | cb_arg = ctx->tlsext_status_arg; |
| 865 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 866 | |
| 867 | /* |
| 868 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 869 | * the order of operations below matter, take care when changing it |
| 870 | */ |
| 871 | tmp_ocsp = cb_arg->s_ocsp; |
| 872 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 873 | cb_arg->s_ocsp = NULL; |
| 874 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 875 | cb_arg->is_single = 0; |
| 876 | cb_arg->single_kt = 0; |
| 877 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 878 | pkey = X509_get_pubkey(x); |
| 879 | key_type = EVP_PKEY_base_id(pkey); |
| 880 | EVP_PKEY_free(pkey); |
| 881 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 882 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 883 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 884 | cb_arg->m_ocsp[index] = iocsp; |
| 885 | |
| 886 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 887 | |
| 888 | ret = 0; |
| 889 | |
| 890 | warn = NULL; |
| 891 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 892 | memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure"); |
| 893 | Warning("%s.\n", warn); |
| 894 | } |
| 895 | |
| 896 | out: |
| 897 | if (ssl) |
| 898 | SSL_free(ssl); |
| 899 | |
| 900 | if (in) |
| 901 | BIO_free(in); |
| 902 | |
| 903 | if (xi) |
| 904 | X509_free(xi); |
| 905 | |
| 906 | if (cid) |
| 907 | OCSP_CERTID_free(cid); |
| 908 | |
| 909 | if (ocsp) |
| 910 | free(ocsp); |
| 911 | |
| 912 | if (warn) |
| 913 | free(warn); |
| 914 | |
| 915 | |
| 916 | return ret; |
| 917 | } |
| 918 | |
| 919 | #endif |
| 920 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 921 | #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] | 922 | |
| 923 | #define CT_EXTENSION_TYPE 18 |
| 924 | |
| 925 | static int sctl_ex_index = -1; |
| 926 | |
| 927 | /* |
| 928 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 929 | * makes only basic test if the data seems like SCTL. No signature validation |
| 930 | * is performed. |
| 931 | */ |
| 932 | static int ssl_sock_parse_sctl(struct chunk *sctl) |
| 933 | { |
| 934 | int ret = 1; |
| 935 | int len, pos, sct_len; |
| 936 | unsigned char *data; |
| 937 | |
| 938 | if (sctl->len < 2) |
| 939 | goto out; |
| 940 | |
| 941 | data = (unsigned char *)sctl->str; |
| 942 | len = (data[0] << 8) | data[1]; |
| 943 | |
| 944 | if (len + 2 != sctl->len) |
| 945 | goto out; |
| 946 | |
| 947 | data = data + 2; |
| 948 | pos = 0; |
| 949 | while (pos < len) { |
| 950 | if (len - pos < 2) |
| 951 | goto out; |
| 952 | |
| 953 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 954 | if (pos + sct_len + 2 > len) |
| 955 | goto out; |
| 956 | |
| 957 | pos += sct_len + 2; |
| 958 | } |
| 959 | |
| 960 | ret = 0; |
| 961 | |
| 962 | out: |
| 963 | return ret; |
| 964 | } |
| 965 | |
| 966 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, struct chunk **sctl) |
| 967 | { |
| 968 | int fd = -1; |
| 969 | int r = 0; |
| 970 | int ret = 1; |
| 971 | |
| 972 | *sctl = NULL; |
| 973 | |
| 974 | fd = open(sctl_path, O_RDONLY); |
| 975 | if (fd == -1) |
| 976 | goto end; |
| 977 | |
| 978 | trash.len = 0; |
| 979 | while (trash.len < trash.size) { |
| 980 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 981 | if (r < 0) { |
| 982 | if (errno == EINTR) |
| 983 | continue; |
| 984 | |
| 985 | goto end; |
| 986 | } |
| 987 | else if (r == 0) { |
| 988 | break; |
| 989 | } |
| 990 | trash.len += r; |
| 991 | } |
| 992 | |
| 993 | ret = ssl_sock_parse_sctl(&trash); |
| 994 | if (ret) |
| 995 | goto end; |
| 996 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 997 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 998 | if (!chunk_dup(*sctl, &trash)) { |
| 999 | free(*sctl); |
| 1000 | *sctl = NULL; |
| 1001 | goto end; |
| 1002 | } |
| 1003 | |
| 1004 | end: |
| 1005 | if (fd != -1) |
| 1006 | close(fd); |
| 1007 | |
| 1008 | return ret; |
| 1009 | } |
| 1010 | |
| 1011 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1012 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1013 | struct chunk *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1014 | |
| 1015 | *out = (unsigned char *)sctl->str; |
| 1016 | *outlen = sctl->len; |
| 1017 | |
| 1018 | return 1; |
| 1019 | } |
| 1020 | |
| 1021 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1022 | { |
| 1023 | return 1; |
| 1024 | } |
| 1025 | |
| 1026 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 1027 | { |
| 1028 | char sctl_path[MAXPATHLEN+1]; |
| 1029 | int ret = -1; |
| 1030 | struct stat st; |
| 1031 | struct chunk *sctl = NULL; |
| 1032 | |
| 1033 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 1034 | |
| 1035 | if (stat(sctl_path, &st)) |
| 1036 | return 1; |
| 1037 | |
| 1038 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 1039 | goto out; |
| 1040 | |
| 1041 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 1042 | free(sctl); |
| 1043 | goto out; |
| 1044 | } |
| 1045 | |
| 1046 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1047 | |
| 1048 | ret = 0; |
| 1049 | |
| 1050 | out: |
| 1051 | return ret; |
| 1052 | } |
| 1053 | |
| 1054 | #endif |
| 1055 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1056 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1057 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1058 | struct connection *conn = SSL_get_app_data(ssl); |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1059 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1060 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1061 | |
| 1062 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1063 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1064 | if (conn->flags & CO_FL_CONNECTED) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1065 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1066 | conn->err_code = CO_ER_SSL_RENEG; |
| 1067 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1068 | } |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1069 | |
| 1070 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 1071 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 1072 | /* Long certificate chains optimz |
| 1073 | If write and read bios are differents, we |
| 1074 | consider that the buffering was activated, |
| 1075 | so we rise the output buffer size from 4k |
| 1076 | to 16k */ |
| 1077 | write_bio = SSL_get_wbio(ssl); |
| 1078 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1079 | BIO_set_write_buffer_size(write_bio, 16384); |
| 1080 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 1081 | } |
| 1082 | } |
| 1083 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1086 | /* Callback is called for each certificate of the chain during a verify |
| 1087 | ok is set to 1 if preverify detect no error on current certificate. |
| 1088 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1089 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1090 | { |
| 1091 | SSL *ssl; |
| 1092 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1093 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1094 | |
| 1095 | 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] | 1096 | conn = SSL_get_app_data(ssl); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1097 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1098 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1099 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1100 | if (ok) /* no errors */ |
| 1101 | return ok; |
| 1102 | |
| 1103 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1104 | err = X509_STORE_CTX_get_error(x_store); |
| 1105 | |
| 1106 | /* check if CA error needs to be ignored */ |
| 1107 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1108 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 1109 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1110 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1113 | 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] | 1114 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1115 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1116 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1117 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1118 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1119 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1120 | return 0; |
| 1121 | } |
| 1122 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1123 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 1124 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1125 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1126 | /* check if certificate error needs to be ignored */ |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1127 | 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] | 1128 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1129 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1130 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1131 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1132 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1133 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1134 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1135 | } |
| 1136 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1137 | /* Callback is called for ssl protocol analyse */ |
| 1138 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1139 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1140 | #ifdef TLS1_RT_HEARTBEAT |
| 1141 | /* test heartbeat received (write_p is set to 0 |
| 1142 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1143 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1144 | struct connection *conn = SSL_get_app_data(ssl); |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1145 | const unsigned char *p = buf; |
| 1146 | unsigned int payload; |
| 1147 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1148 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1149 | |
| 1150 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1151 | if (*p != TLS1_HB_REQUEST) |
| 1152 | return; |
| 1153 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1154 | 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] | 1155 | goto kill_it; |
| 1156 | |
| 1157 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1158 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1159 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1160 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1161 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1162 | * advertised payload is larger than the advertised packet |
| 1163 | * length, so we have garbage in the buffer between the |
| 1164 | * payload and the end of the buffer (p+len). We can't know |
| 1165 | * if the SSL stack is patched, and we don't know if we can |
| 1166 | * safely wipe out the area between p+3+len and payload. |
| 1167 | * So instead, we prevent the response from being sent by |
| 1168 | * setting the max_send_fragment to 0 and we report an SSL |
| 1169 | * error, which will kill this connection. It will be reported |
| 1170 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1171 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1172 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1173 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1174 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1175 | return; |
| 1176 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1177 | #endif |
| 1178 | } |
| 1179 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1180 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 1181 | /* This callback is used so that the server advertises the list of |
| 1182 | * negociable protocols for NPN. |
| 1183 | */ |
| 1184 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1185 | unsigned int *len, void *arg) |
| 1186 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1187 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1188 | |
| 1189 | *data = (const unsigned char *)conf->npn_str; |
| 1190 | *len = conf->npn_len; |
| 1191 | return SSL_TLSEXT_ERR_OK; |
| 1192 | } |
| 1193 | #endif |
| 1194 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1195 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1196 | /* This callback is used so that the server advertises the list of |
| 1197 | * negociable protocols for ALPN. |
| 1198 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1199 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1200 | unsigned char *outlen, |
| 1201 | const unsigned char *server, |
| 1202 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1203 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1204 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1205 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1206 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1207 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1208 | return SSL_TLSEXT_ERR_NOACK; |
| 1209 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1210 | return SSL_TLSEXT_ERR_OK; |
| 1211 | } |
| 1212 | #endif |
| 1213 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1214 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1215 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1216 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
| 1217 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1218 | /* Create a X509 certificate with the specified servername and serial. This |
| 1219 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1220 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1221 | 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] | 1222 | { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1223 | static unsigned int serial = 0; |
| 1224 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1225 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1226 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1227 | SSL_CTX *ssl_ctx = NULL; |
| 1228 | X509 *newcrt = NULL; |
| 1229 | EVP_PKEY *pkey = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1230 | X509_NAME *name; |
| 1231 | const EVP_MD *digest; |
| 1232 | X509V3_CTX ctx; |
| 1233 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1234 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1235 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1236 | /* Get the private key of the defautl certificate and use it */ |
| 1237 | if (!(pkey = SSL_get_privatekey(ssl))) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1238 | goto mkcert_error; |
| 1239 | |
| 1240 | /* Create the certificate */ |
| 1241 | if (!(newcrt = X509_new())) |
| 1242 | goto mkcert_error; |
| 1243 | |
| 1244 | /* Set version number for the certificate (X509v3) and the serial |
| 1245 | * number */ |
| 1246 | if (X509_set_version(newcrt, 2L) != 1) |
| 1247 | goto mkcert_error; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1248 | if (!serial) |
| 1249 | serial = now_ms; |
| 1250 | ASN1_INTEGER_set(X509_get_serialNumber(newcrt), serial++); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1251 | |
| 1252 | /* Set duration for the certificate */ |
| 1253 | if (!X509_gmtime_adj(X509_get_notBefore(newcrt), (long)-60*60*24) || |
| 1254 | !X509_gmtime_adj(X509_get_notAfter(newcrt),(long)60*60*24*365)) |
| 1255 | goto mkcert_error; |
| 1256 | |
| 1257 | /* set public key in the certificate */ |
| 1258 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1259 | goto mkcert_error; |
| 1260 | |
| 1261 | /* Set issuer name from the CA */ |
| 1262 | if (!(name = X509_get_subject_name(cacert))) |
| 1263 | goto mkcert_error; |
| 1264 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1265 | goto mkcert_error; |
| 1266 | |
| 1267 | /* Set the subject name using the same, but the CN */ |
| 1268 | name = X509_NAME_dup(name); |
| 1269 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1270 | (const unsigned char *)servername, |
| 1271 | -1, -1, 0) != 1) { |
| 1272 | X509_NAME_free(name); |
| 1273 | goto mkcert_error; |
| 1274 | } |
| 1275 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1276 | X509_NAME_free(name); |
| 1277 | goto mkcert_error; |
| 1278 | } |
| 1279 | X509_NAME_free(name); |
| 1280 | |
| 1281 | /* Add x509v3 extensions as specified */ |
| 1282 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1283 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1284 | X509_EXTENSION *ext; |
| 1285 | |
| 1286 | if (!(ext = X509V3_EXT_conf(NULL, &ctx, x509v3_ext_names[i], x509v3_ext_values[i]))) |
| 1287 | goto mkcert_error; |
| 1288 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1289 | X509_EXTENSION_free(ext); |
| 1290 | goto mkcert_error; |
| 1291 | } |
| 1292 | X509_EXTENSION_free(ext); |
| 1293 | } |
| 1294 | |
| 1295 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1296 | |
| 1297 | key_type = EVP_PKEY_base_id(capkey); |
| 1298 | |
| 1299 | if (key_type == EVP_PKEY_DSA) |
| 1300 | digest = EVP_sha1(); |
| 1301 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1302 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1303 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1304 | digest = EVP_sha256(); |
| 1305 | else { |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1306 | #if (OPENSSL_VERSION_NUMBER >= 0x1000000fL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1307 | int nid; |
| 1308 | |
| 1309 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1310 | goto mkcert_error; |
| 1311 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1312 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1313 | #else |
| 1314 | goto mkcert_error; |
| 1315 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1316 | } |
| 1317 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1318 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1319 | goto mkcert_error; |
| 1320 | |
| 1321 | /* Create and set the new SSL_CTX */ |
| 1322 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1323 | goto mkcert_error; |
| 1324 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1325 | goto mkcert_error; |
| 1326 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1327 | goto mkcert_error; |
| 1328 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1329 | goto mkcert_error; |
| 1330 | |
| 1331 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1332 | |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1333 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
| 1334 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1335 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1336 | 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] | 1337 | EC_KEY *ecc; |
| 1338 | int nid; |
| 1339 | |
| 1340 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1341 | goto end; |
| 1342 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1343 | goto end; |
| 1344 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1345 | EC_KEY_free(ecc); |
| 1346 | } |
| 1347 | #endif |
| 1348 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1349 | return ssl_ctx; |
| 1350 | |
| 1351 | mkcert_error: |
| 1352 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1353 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1354 | return NULL; |
| 1355 | } |
| 1356 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1357 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1358 | 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] | 1359 | { |
| 1360 | struct bind_conf *bind_conf = objt_listener(conn->target)->bind_conf; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1361 | |
| 1362 | return ssl_sock_do_create_cert(servername, bind_conf, conn->xprt_ctx); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1363 | } |
| 1364 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1365 | /* Do a lookup for a certificate in the LRU cache used to store generated |
| 1366 | * certificates. */ |
| 1367 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1368 | 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] | 1369 | { |
| 1370 | struct lru64 *lru = NULL; |
| 1371 | |
| 1372 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1373 | 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] | 1374 | if (lru && lru->domain) |
| 1375 | return (SSL_CTX *)lru->data; |
| 1376 | } |
| 1377 | return NULL; |
| 1378 | } |
| 1379 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1380 | /* Set a certificate int the LRU cache used to store generated |
| 1381 | * certificate. Return 0 on success, otherwise -1 */ |
| 1382 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1383 | 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] | 1384 | { |
| 1385 | struct lru64 *lru = NULL; |
| 1386 | |
| 1387 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1388 | 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] | 1389 | if (!lru) |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1390 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1391 | if (lru->domain && lru->data) |
| 1392 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1393 | 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] | 1394 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1395 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1396 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1397 | } |
| 1398 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1399 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1400 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1401 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1402 | { |
| 1403 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 1404 | } |
| 1405 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1406 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 1407 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 1408 | */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1409 | static SSL_CTX * |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1410 | 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] | 1411 | { |
| 1412 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1413 | SSL_CTX *ssl_ctx = NULL; |
| 1414 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1415 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1416 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1417 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1418 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1419 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1420 | if (lru && lru->domain) |
| 1421 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1422 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1423 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1424 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1425 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1426 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1427 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1428 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1429 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1430 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 1431 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 1432 | SSL_CTX_free(ssl_ctx); |
| 1433 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1434 | return ssl_ctx; |
| 1435 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1436 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1437 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1438 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 1439 | * warning when no match is found, which implies the default (first) cert |
| 1440 | * will keep being used. |
| 1441 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1442 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1443 | { |
| 1444 | const char *servername; |
| 1445 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1446 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1447 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1448 | int i; |
| 1449 | (void)al; /* shut gcc stupid warning */ |
| 1450 | |
| 1451 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1452 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1453 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1454 | if (s->generate_certs) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1455 | struct connection *conn = SSL_get_app_data(ssl); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1456 | unsigned int key; |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1457 | SSL_CTX *ctx; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1458 | |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1459 | conn_get_to_addr(conn); |
| 1460 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1461 | key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to)); |
| 1462 | ctx = ssl_sock_get_generated_cert(key, s); |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1463 | if (ctx) { |
| 1464 | /* switch ctx */ |
| 1465 | SSL_set_SSL_CTX(ssl, ctx); |
| 1466 | return SSL_TLSEXT_ERR_OK; |
| 1467 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1468 | } |
| 1469 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1470 | #endif |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1471 | return (s->strict_sni ? |
| 1472 | SSL_TLSEXT_ERR_ALERT_FATAL : |
Emmanuel Hocdet | 79274e2 | 2013-05-31 12:47:44 +0200 | [diff] [blame] | 1473 | SSL_TLSEXT_ERR_NOACK); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1474 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1475 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1476 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1477 | if (!servername[i]) |
| 1478 | break; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1479 | trash.str[i] = tolower(servername[i]); |
| 1480 | if (!wildp && (trash.str[i] == '.')) |
| 1481 | wildp = &trash.str[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1482 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1483 | trash.str[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1484 | |
| 1485 | /* lookup in full qualified names */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1486 | node = ebst_lookup(&s->sni_ctx, trash.str); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1487 | |
| 1488 | /* lookup a not neg filter */ |
| 1489 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 1490 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 1491 | node = n; |
| 1492 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1493 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1494 | } |
| 1495 | if (!node && wildp) { |
| 1496 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1497 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1498 | } |
| 1499 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1500 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1501 | SSL_CTX *ctx; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1502 | if (s->generate_certs && |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1503 | (ctx = ssl_sock_generate_certificate(servername, s, ssl))) { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1504 | /* switch ctx */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1505 | return SSL_TLSEXT_ERR_OK; |
| 1506 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1507 | #endif |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1508 | return (s->strict_sni ? |
| 1509 | SSL_TLSEXT_ERR_ALERT_FATAL : |
| 1510 | SSL_TLSEXT_ERR_ALERT_WARNING); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | /* switch ctx */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1514 | SSL_set_SSL_CTX(ssl, container_of(node, struct sni_ctx, name)->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1515 | return SSL_TLSEXT_ERR_OK; |
| 1516 | } |
| 1517 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 1518 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1519 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1520 | |
| 1521 | static DH * ssl_get_dh_1024(void) |
| 1522 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1523 | static unsigned char dh1024_p[]={ |
| 1524 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 1525 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 1526 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 1527 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 1528 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 1529 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 1530 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 1531 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 1532 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 1533 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 1534 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 1535 | }; |
| 1536 | static unsigned char dh1024_g[]={ |
| 1537 | 0x02, |
| 1538 | }; |
| 1539 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1540 | BIGNUM *p; |
| 1541 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1542 | DH *dh = DH_new(); |
| 1543 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1544 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 1545 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1546 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1547 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1548 | DH_free(dh); |
| 1549 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1550 | } else { |
| 1551 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | return dh; |
| 1555 | } |
| 1556 | |
| 1557 | static DH *ssl_get_dh_2048(void) |
| 1558 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1559 | static unsigned char dh2048_p[]={ |
| 1560 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 1561 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 1562 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 1563 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 1564 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 1565 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 1566 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 1567 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 1568 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 1569 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 1570 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 1571 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 1572 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 1573 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 1574 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 1575 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 1576 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 1577 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 1578 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 1579 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 1580 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 1581 | 0xB7,0x1F,0x77,0xF3, |
| 1582 | }; |
| 1583 | static unsigned char dh2048_g[]={ |
| 1584 | 0x02, |
| 1585 | }; |
| 1586 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1587 | BIGNUM *p; |
| 1588 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1589 | DH *dh = DH_new(); |
| 1590 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1591 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 1592 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1593 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1594 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1595 | DH_free(dh); |
| 1596 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1597 | } else { |
| 1598 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | return dh; |
| 1602 | } |
| 1603 | |
| 1604 | static DH *ssl_get_dh_4096(void) |
| 1605 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1606 | static unsigned char dh4096_p[]={ |
| 1607 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 1608 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 1609 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 1610 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 1611 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 1612 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 1613 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 1614 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 1615 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 1616 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 1617 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 1618 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 1619 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 1620 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 1621 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 1622 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 1623 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 1624 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 1625 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 1626 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 1627 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 1628 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 1629 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 1630 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 1631 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 1632 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 1633 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 1634 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 1635 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 1636 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 1637 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 1638 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 1639 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 1640 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 1641 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 1642 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 1643 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 1644 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 1645 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 1646 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 1647 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 1648 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 1649 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1650 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1651 | static unsigned char dh4096_g[]={ |
| 1652 | 0x02, |
| 1653 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1654 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1655 | BIGNUM *p; |
| 1656 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1657 | DH *dh = DH_new(); |
| 1658 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1659 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 1660 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1661 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1662 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1663 | DH_free(dh); |
| 1664 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1665 | } else { |
| 1666 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1667 | } |
| 1668 | } |
| 1669 | return dh; |
| 1670 | } |
| 1671 | |
| 1672 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 1673 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1674 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 1675 | { |
| 1676 | DH *dh = NULL; |
| 1677 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1678 | int type; |
| 1679 | |
| 1680 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1681 | |
| 1682 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 1683 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 1684 | */ |
| 1685 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 1686 | keylen = EVP_PKEY_bits(pkey); |
| 1687 | } |
| 1688 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 1689 | if (keylen > global_ssl.default_dh_param) { |
| 1690 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1691 | } |
| 1692 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1693 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1694 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1695 | } |
| 1696 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1697 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1698 | } |
| 1699 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1700 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | return dh; |
| 1704 | } |
| 1705 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1706 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1707 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1708 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1709 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1710 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1711 | if (in == NULL) |
| 1712 | goto end; |
| 1713 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1714 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1715 | goto end; |
| 1716 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1717 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 1718 | |
| 1719 | end: |
| 1720 | if (in) |
| 1721 | BIO_free(in); |
| 1722 | |
| 1723 | return dh; |
| 1724 | } |
| 1725 | |
| 1726 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 1727 | { |
| 1728 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 1729 | |
| 1730 | if (global_dh) { |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
| 1734 | return -1; |
| 1735 | } |
| 1736 | |
| 1737 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 1738 | if an error occured, and 0 if parameter not found. */ |
| 1739 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 1740 | { |
| 1741 | int ret = -1; |
| 1742 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 1743 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1744 | if (dh) { |
| 1745 | ret = 1; |
| 1746 | SSL_CTX_set_tmp_dh(ctx, dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 1747 | |
| 1748 | if (ssl_dh_ptr_index >= 0) { |
| 1749 | /* store a pointer to the DH params to avoid complaining about |
| 1750 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 1751 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 1752 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1753 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1754 | else if (global_dh) { |
| 1755 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 1756 | ret = 0; /* DH params not found */ |
| 1757 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1758 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 1759 | /* Clear openssl global errors stack */ |
| 1760 | ERR_clear_error(); |
| 1761 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 1762 | if (global_ssl.default_dh_param <= 1024) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1763 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 1764 | if (local_dh_1024 == NULL) |
| 1765 | local_dh_1024 = ssl_get_dh_1024(); |
| 1766 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1767 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1768 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 1769 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1770 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1771 | } |
| 1772 | else { |
| 1773 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 1774 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 1775 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 1776 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1777 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1778 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1779 | end: |
| 1780 | if (dh) |
| 1781 | DH_free(dh); |
| 1782 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1783 | return ret; |
| 1784 | } |
| 1785 | #endif |
| 1786 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1787 | static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, |
| 1788 | struct ssl_bind_conf *conf, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1789 | { |
| 1790 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1791 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 1792 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1793 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1794 | if (*name == '!') { |
| 1795 | neg = 1; |
| 1796 | name++; |
| 1797 | } |
| 1798 | if (*name == '*') { |
| 1799 | wild = 1; |
| 1800 | name++; |
| 1801 | } |
| 1802 | /* !* filter is a nop */ |
| 1803 | if (neg && wild) |
| 1804 | return order; |
| 1805 | if (*name) { |
| 1806 | int j, len; |
| 1807 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 1808 | for (j = 0; j < len && j < trash.size; j++) |
| 1809 | trash.str[j] = tolower(name[j]); |
| 1810 | if (j >= trash.size) |
| 1811 | return order; |
| 1812 | trash.str[j] = 0; |
| 1813 | |
| 1814 | /* Check for duplicates. */ |
| 1815 | if (wild) |
| 1816 | node = ebst_lookup(&s->sni_w_ctx, trash.str); |
| 1817 | else |
| 1818 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 1819 | for (; node; node = ebmb_next_dup(node)) { |
| 1820 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1821 | if (sc->ctx == ctx && sc->conf == conf && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 1822 | return order; |
| 1823 | } |
| 1824 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1825 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 1826 | if (!sc) |
| 1827 | return order; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 1828 | memcpy(sc->name.key, trash.str, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1829 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1830 | sc->conf = conf; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1831 | sc->order = order++; |
| 1832 | sc->neg = neg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1833 | if (wild) |
| 1834 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 1835 | else |
| 1836 | ebst_insert(&s->sni_ctx, &sc->name); |
| 1837 | } |
| 1838 | return order; |
| 1839 | } |
| 1840 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 1841 | |
| 1842 | /* The following code is used for loading multiple crt files into |
| 1843 | * SSL_CTX's based on CN/SAN |
| 1844 | */ |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 1845 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(LIBRESSL_VERSION_NUMBER) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 1846 | /* This is used to preload the certifcate, private key |
| 1847 | * and Cert Chain of a file passed in via the crt |
| 1848 | * argument |
| 1849 | * |
| 1850 | * This way, we do not have to read the file multiple times |
| 1851 | */ |
| 1852 | struct cert_key_and_chain { |
| 1853 | X509 *cert; |
| 1854 | EVP_PKEY *key; |
| 1855 | unsigned int num_chain_certs; |
| 1856 | /* This is an array of X509 pointers */ |
| 1857 | X509 **chain_certs; |
| 1858 | }; |
| 1859 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 1860 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 1861 | |
| 1862 | struct key_combo_ctx { |
| 1863 | SSL_CTX *ctx; |
| 1864 | int order; |
| 1865 | }; |
| 1866 | |
| 1867 | /* Map used for processing multiple keypairs for a single purpose |
| 1868 | * |
| 1869 | * This maps CN/SNI name to certificate type |
| 1870 | */ |
| 1871 | struct sni_keytype { |
| 1872 | int keytypes; /* BITMASK for keytypes */ |
| 1873 | struct ebmb_node name; /* node holding the servername value */ |
| 1874 | }; |
| 1875 | |
| 1876 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 1877 | /* Frees the contents of a cert_key_and_chain |
| 1878 | */ |
| 1879 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 1880 | { |
| 1881 | int i; |
| 1882 | |
| 1883 | if (!ckch) |
| 1884 | return; |
| 1885 | |
| 1886 | /* Free the certificate and set pointer to NULL */ |
| 1887 | if (ckch->cert) |
| 1888 | X509_free(ckch->cert); |
| 1889 | ckch->cert = NULL; |
| 1890 | |
| 1891 | /* Free the key and set pointer to NULL */ |
| 1892 | if (ckch->key) |
| 1893 | EVP_PKEY_free(ckch->key); |
| 1894 | ckch->key = NULL; |
| 1895 | |
| 1896 | /* Free each certificate in the chain */ |
| 1897 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 1898 | if (ckch->chain_certs[i]) |
| 1899 | X509_free(ckch->chain_certs[i]); |
| 1900 | } |
| 1901 | |
| 1902 | /* Free the chain obj itself and set to NULL */ |
| 1903 | if (ckch->num_chain_certs > 0) { |
| 1904 | free(ckch->chain_certs); |
| 1905 | ckch->num_chain_certs = 0; |
| 1906 | ckch->chain_certs = NULL; |
| 1907 | } |
| 1908 | |
| 1909 | } |
| 1910 | |
| 1911 | /* checks if a key and cert exists in the ckch |
| 1912 | */ |
| 1913 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 1914 | { |
| 1915 | return (ckch->cert != NULL && ckch->key != NULL); |
| 1916 | } |
| 1917 | |
| 1918 | |
| 1919 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 1920 | * This allows us to carry the contents of the file without having to |
| 1921 | * read the file multiple times. |
| 1922 | * |
| 1923 | * returns: |
| 1924 | * 0 on Success |
| 1925 | * 1 on SSL Failure |
| 1926 | * 2 on file not found |
| 1927 | */ |
| 1928 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 1929 | { |
| 1930 | |
| 1931 | BIO *in; |
| 1932 | X509 *ca = NULL; |
| 1933 | int ret = 1; |
| 1934 | |
| 1935 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 1936 | |
| 1937 | in = BIO_new(BIO_s_file()); |
| 1938 | if (in == NULL) |
| 1939 | goto end; |
| 1940 | |
| 1941 | if (BIO_read_filename(in, path) <= 0) |
| 1942 | goto end; |
| 1943 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 1944 | /* Read Private Key */ |
| 1945 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 1946 | if (ckch->key == NULL) { |
| 1947 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 1948 | err && *err ? *err : "", path); |
| 1949 | goto end; |
| 1950 | } |
| 1951 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 1952 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 1953 | if (BIO_reset(in) == -1) { |
| 1954 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 1955 | err && *err ? *err : "", path); |
| 1956 | goto end; |
| 1957 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 1958 | |
| 1959 | /* Read Certificate */ |
| 1960 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 1961 | if (ckch->cert == NULL) { |
| 1962 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 1963 | err && *err ? *err : "", path); |
| 1964 | goto end; |
| 1965 | } |
| 1966 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 1967 | /* Read Certificate Chain */ |
| 1968 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 1969 | /* Grow the chain certs */ |
| 1970 | ckch->num_chain_certs++; |
| 1971 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 1972 | |
| 1973 | /* use - 1 here since we just incremented it above */ |
| 1974 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 1975 | } |
| 1976 | ret = ERR_get_error(); |
| 1977 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 1978 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 1979 | err && *err ? *err : "", path); |
| 1980 | ret = 1; |
| 1981 | goto end; |
| 1982 | } |
| 1983 | |
| 1984 | ret = 0; |
| 1985 | |
| 1986 | end: |
| 1987 | |
| 1988 | ERR_clear_error(); |
| 1989 | if (in) |
| 1990 | BIO_free(in); |
| 1991 | |
| 1992 | /* Something went wrong in one of the reads */ |
| 1993 | if (ret != 0) |
| 1994 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 1995 | |
| 1996 | return ret; |
| 1997 | } |
| 1998 | |
| 1999 | /* Loads the info in ckch into ctx |
| 2000 | * Currently, this does not process any information about ocsp, dhparams or |
| 2001 | * sctl |
| 2002 | * Returns |
| 2003 | * 0 on success |
| 2004 | * 1 on failure |
| 2005 | */ |
| 2006 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2007 | { |
| 2008 | int i = 0; |
| 2009 | |
| 2010 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2011 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2012 | err && *err ? *err : "", path); |
| 2013 | return 1; |
| 2014 | } |
| 2015 | |
| 2016 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 2017 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 2018 | err && *err ? *err : "", path); |
| 2019 | return 1; |
| 2020 | } |
| 2021 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2022 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 2023 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2024 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2025 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 2026 | err && *err ? *err : "", (i+1), path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2027 | return 1; |
| 2028 | } |
| 2029 | } |
| 2030 | |
| 2031 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2032 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2033 | err && *err ? *err : "", path); |
| 2034 | return 1; |
| 2035 | } |
| 2036 | |
| 2037 | return 0; |
| 2038 | } |
| 2039 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2040 | |
| 2041 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 2042 | { |
| 2043 | struct sni_keytype *s_kt = NULL; |
| 2044 | struct ebmb_node *node; |
| 2045 | int i; |
| 2046 | |
| 2047 | for (i = 0; i < trash.size; i++) { |
| 2048 | if (!str[i]) |
| 2049 | break; |
| 2050 | trash.str[i] = tolower(str[i]); |
| 2051 | } |
| 2052 | trash.str[i] = 0; |
| 2053 | node = ebst_lookup(sni_keytypes, trash.str); |
| 2054 | if (!node) { |
| 2055 | /* CN not found in tree */ |
| 2056 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 2057 | /* Using memcpy here instead of strncpy. |
| 2058 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 2059 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 2060 | */ |
| 2061 | memcpy(s_kt->name.key, trash.str, i+1); |
| 2062 | s_kt->keytypes = 0; |
| 2063 | ebst_insert(sni_keytypes, &s_kt->name); |
| 2064 | } else { |
| 2065 | /* CN found in tree */ |
| 2066 | s_kt = container_of(node, struct sni_keytype, name); |
| 2067 | } |
| 2068 | |
| 2069 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 2070 | s_kt->keytypes |= 1<<key_index; |
| 2071 | |
| 2072 | } |
| 2073 | |
| 2074 | |
| 2075 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 2076 | * If any are found, group these files into a set of SSL_CTX* |
| 2077 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 2078 | * |
| 2079 | * This will allow the user to explictly group multiple cert/keys for a single purpose |
| 2080 | * |
| 2081 | * Returns |
| 2082 | * 0 on success |
| 2083 | * 1 on failure |
| 2084 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2085 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2086 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2087 | { |
| 2088 | char fp[MAXPATHLEN+1] = {0}; |
| 2089 | int n = 0; |
| 2090 | int i = 0; |
| 2091 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 2092 | struct eb_root sni_keytypes_map = { {0} }; |
| 2093 | struct ebmb_node *node; |
| 2094 | struct ebmb_node *next; |
| 2095 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 2096 | * of keytypes |
| 2097 | */ |
| 2098 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 2099 | int rv = 0; |
| 2100 | X509_NAME *xname = NULL; |
| 2101 | char *str = NULL; |
| 2102 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2103 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 2104 | #endif |
| 2105 | |
| 2106 | /* Load all possible certs and keys */ |
| 2107 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2108 | struct stat buf; |
| 2109 | |
| 2110 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2111 | if (stat(fp, &buf) == 0) { |
| 2112 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
| 2113 | rv = 1; |
| 2114 | goto end; |
| 2115 | } |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | /* Process each ckch and update keytypes for each CN/SAN |
| 2120 | * for example, if CN/SAN www.a.com is associated with |
| 2121 | * certs with keytype 0 and 2, then at the end of the loop, |
| 2122 | * www.a.com will have: |
| 2123 | * keyindex = 0 | 1 | 4 = 5 |
| 2124 | */ |
| 2125 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2126 | |
| 2127 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 2128 | continue; |
| 2129 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2130 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 2131 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2132 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 2133 | } else { |
| 2134 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 2135 | * so the line that contains logic is marked via comments |
| 2136 | */ |
| 2137 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 2138 | i = -1; |
| 2139 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 2140 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2141 | ASN1_STRING *value; |
| 2142 | value = X509_NAME_ENTRY_get_data(entry); |
| 2143 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2144 | /* Important line is here */ |
| 2145 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2146 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2147 | OPENSSL_free(str); |
| 2148 | str = NULL; |
| 2149 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2150 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2151 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2152 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2153 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2154 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 2155 | if (names) { |
| 2156 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 2157 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2158 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2159 | if (name->type == GEN_DNS) { |
| 2160 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 2161 | /* Important line is here */ |
| 2162 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2163 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2164 | OPENSSL_free(str); |
| 2165 | str = NULL; |
| 2166 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2167 | } |
| 2168 | } |
| 2169 | } |
| 2170 | } |
| 2171 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2172 | } |
| 2173 | |
| 2174 | /* If no files found, return error */ |
| 2175 | if (eb_is_empty(&sni_keytypes_map)) { |
| 2176 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 2177 | err && *err ? *err : "", path); |
| 2178 | rv = 1; |
| 2179 | goto end; |
| 2180 | } |
| 2181 | |
| 2182 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 2183 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 2184 | * and add each CTX to the SNI tree |
| 2185 | * |
| 2186 | * Some math here: |
| 2187 | * There are 2^n - 1 possibile combinations, each unique |
| 2188 | * combination is denoted by the key in the map. Each key |
| 2189 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 2190 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 2191 | * entry in the array to correspond to the unique combo (key) |
| 2192 | * associated with i. This unique key combo (i) will be associated |
| 2193 | * with combos[i-1] |
| 2194 | */ |
| 2195 | |
| 2196 | node = ebmb_first(&sni_keytypes_map); |
| 2197 | while (node) { |
| 2198 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2199 | char cur_file[MAXPATHLEN+1]; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2200 | |
| 2201 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 2202 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 2203 | cur_ctx = key_combos[i-1].ctx; |
| 2204 | |
| 2205 | if (cur_ctx == NULL) { |
| 2206 | /* need to create SSL_CTX */ |
| 2207 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
| 2208 | if (cur_ctx == NULL) { |
| 2209 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 2210 | err && *err ? *err : ""); |
| 2211 | rv = 1; |
| 2212 | goto end; |
| 2213 | } |
| 2214 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2215 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2216 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2217 | if (i & (1<<n)) { |
| 2218 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2219 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2220 | 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] | 2221 | SSL_CTX_free(cur_ctx); |
| 2222 | rv = 1; |
| 2223 | goto end; |
| 2224 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2225 | |
| 2226 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 2227 | /* Load OCSP Info into context */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2228 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2229 | if (err) |
| 2230 | 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] | 2231 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2232 | SSL_CTX_free(cur_ctx); |
| 2233 | rv = 1; |
| 2234 | goto end; |
| 2235 | } |
| 2236 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | /* Load DH params into the ctx to support DHE keys */ |
| 2241 | #ifndef OPENSSL_NO_DH |
| 2242 | if (ssl_dh_ptr_index >= 0) |
| 2243 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 2244 | |
| 2245 | rv = ssl_sock_load_dh_params(cur_ctx, NULL); |
| 2246 | if (rv < 0) { |
| 2247 | if (err) |
| 2248 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 2249 | *err ? *err : "", path); |
| 2250 | rv = 1; |
| 2251 | goto end; |
| 2252 | } |
| 2253 | #endif |
| 2254 | |
| 2255 | /* Update key_combos */ |
| 2256 | key_combos[i-1].ctx = cur_ctx; |
| 2257 | } |
| 2258 | |
| 2259 | /* Update SNI Tree */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2260 | key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2261 | node = ebmb_next(node); |
| 2262 | } |
| 2263 | |
| 2264 | |
| 2265 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 2266 | if (!bind_conf->default_ctx) { |
| 2267 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 2268 | if (key_combos[i].ctx) { |
| 2269 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2270 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2271 | break; |
| 2272 | } |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | end: |
| 2277 | |
| 2278 | if (names) |
| 2279 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 2280 | |
| 2281 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 2282 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 2283 | |
| 2284 | node = ebmb_first(&sni_keytypes_map); |
| 2285 | while (node) { |
| 2286 | next = ebmb_next(node); |
| 2287 | ebmb_delete(node); |
| 2288 | node = next; |
| 2289 | } |
| 2290 | |
| 2291 | return rv; |
| 2292 | } |
| 2293 | #else |
| 2294 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2295 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2296 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2297 | { |
| 2298 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 2299 | err && *err ? *err : "", path, strerror(errno)); |
| 2300 | return 1; |
| 2301 | } |
| 2302 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2303 | #endif /* #if OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
| 2304 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2305 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 2306 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 2307 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2308 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, |
| 2309 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2310 | { |
| 2311 | BIO *in; |
| 2312 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2313 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2314 | int ret = -1; |
| 2315 | int order = 0; |
| 2316 | X509_NAME *xname; |
| 2317 | char *str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2318 | pem_password_cb *passwd_cb; |
| 2319 | void *passwd_cb_userdata; |
| 2320 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2321 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2322 | STACK_OF(GENERAL_NAME) *names; |
| 2323 | #endif |
| 2324 | |
| 2325 | in = BIO_new(BIO_s_file()); |
| 2326 | if (in == NULL) |
| 2327 | goto end; |
| 2328 | |
| 2329 | if (BIO_read_filename(in, file) <= 0) |
| 2330 | goto end; |
| 2331 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2332 | |
| 2333 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 2334 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 2335 | |
| 2336 | 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] | 2337 | if (x == NULL) |
| 2338 | goto end; |
| 2339 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2340 | if (fcount) { |
| 2341 | while (fcount--) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2342 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, sni_filter[fcount], order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2343 | } |
| 2344 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2345 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2346 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 2347 | if (names) { |
| 2348 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 2349 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 2350 | if (name->type == GEN_DNS) { |
| 2351 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2352 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2353 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2354 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2355 | } |
| 2356 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2357 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2358 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2359 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2360 | xname = X509_get_subject_name(x); |
| 2361 | i = -1; |
| 2362 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 2363 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2364 | ASN1_STRING *value; |
| 2365 | |
| 2366 | value = X509_NAME_ENTRY_get_data(entry); |
| 2367 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2368 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2369 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2370 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 2375 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 2376 | goto end; |
| 2377 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2378 | #ifdef SSL_CTX_clear_extra_chain_certs |
| 2379 | SSL_CTX_clear_extra_chain_certs(ctx); |
| 2380 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2381 | if (ctx->extra_certs != NULL) { |
| 2382 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 2383 | ctx->extra_certs = NULL; |
| 2384 | } |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2385 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2386 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2387 | 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] | 2388 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 2389 | X509_free(ca); |
| 2390 | goto end; |
| 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | err = ERR_get_error(); |
| 2395 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 2396 | /* we successfully reached the last cert in the file */ |
| 2397 | ret = 1; |
| 2398 | } |
| 2399 | ERR_clear_error(); |
| 2400 | |
| 2401 | end: |
| 2402 | if (x) |
| 2403 | X509_free(x); |
| 2404 | |
| 2405 | if (in) |
| 2406 | BIO_free(in); |
| 2407 | |
| 2408 | return ret; |
| 2409 | } |
| 2410 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2411 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2412 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2413 | { |
| 2414 | int ret; |
| 2415 | SSL_CTX *ctx; |
| 2416 | |
| 2417 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 2418 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2419 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 2420 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2421 | return 1; |
| 2422 | } |
| 2423 | |
| 2424 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2425 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 2426 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2427 | SSL_CTX_free(ctx); |
| 2428 | return 1; |
| 2429 | } |
| 2430 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2431 | 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] | 2432 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2433 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 2434 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2435 | if (ret < 0) /* serious error, must do that ourselves */ |
| 2436 | SSL_CTX_free(ctx); |
| 2437 | return 1; |
| 2438 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 2439 | |
| 2440 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2441 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2442 | err && *err ? *err : "", path); |
| 2443 | return 1; |
| 2444 | } |
| 2445 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2446 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 2447 | * the tree, so it will be discovered and cleaned in time. |
| 2448 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2449 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2450 | /* store a NULL pointer to indicate we have not yet loaded |
| 2451 | a custom DH param file */ |
| 2452 | if (ssl_dh_ptr_index >= 0) { |
| 2453 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 2454 | } |
| 2455 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2456 | ret = ssl_sock_load_dh_params(ctx, path); |
| 2457 | if (ret < 0) { |
| 2458 | if (err) |
| 2459 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 2460 | *err ? *err : "", path); |
| 2461 | return 1; |
| 2462 | } |
| 2463 | #endif |
| 2464 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 2465 | #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] | 2466 | ret = ssl_sock_load_ocsp(ctx, path); |
| 2467 | if (ret < 0) { |
| 2468 | if (err) |
| 2469 | 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", |
| 2470 | *err ? *err : "", path); |
| 2471 | return 1; |
| 2472 | } |
| 2473 | #endif |
| 2474 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 2475 | #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] | 2476 | if (sctl_ex_index >= 0) { |
| 2477 | ret = ssl_sock_load_sctl(ctx, path); |
| 2478 | if (ret < 0) { |
| 2479 | if (err) |
| 2480 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 2481 | *err ? *err : "", path); |
| 2482 | return 1; |
| 2483 | } |
| 2484 | } |
| 2485 | #endif |
| 2486 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2487 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2488 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2489 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 2490 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2491 | return 1; |
| 2492 | } |
| 2493 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2494 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2495 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2496 | bind_conf->default_ssl_conf = ssl_conf; |
| 2497 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2498 | |
| 2499 | return 0; |
| 2500 | } |
| 2501 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 2502 | 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] | 2503 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 2504 | struct dirent **de_list; |
| 2505 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2506 | DIR *dir; |
| 2507 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 2508 | char *end; |
| 2509 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2510 | int cfgerr = 0; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 2511 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 2512 | int is_bundle; |
| 2513 | int j; |
| 2514 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2515 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2516 | if (stat(path, &buf) == 0) { |
| 2517 | dir = opendir(path); |
| 2518 | if (!dir) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2519 | 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] | 2520 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2521 | /* strip trailing slashes, including first one */ |
| 2522 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 2523 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2524 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2525 | n = scandir(path, &de_list, 0, alphasort); |
| 2526 | if (n < 0) { |
| 2527 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 2528 | err && *err ? *err : "", path, strerror(errno)); |
| 2529 | cfgerr++; |
| 2530 | } |
| 2531 | else { |
| 2532 | for (i = 0; i < n; i++) { |
| 2533 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 2534 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2535 | end = strrchr(de->d_name, '.'); |
| 2536 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 2537 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 2538 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2539 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 2540 | if (stat(fp, &buf) != 0) { |
| 2541 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 2542 | err && *err ? *err : "", fp, strerror(errno)); |
| 2543 | cfgerr++; |
| 2544 | goto ignore_entry; |
| 2545 | } |
| 2546 | if (!S_ISREG(buf.st_mode)) |
| 2547 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 2548 | |
| 2549 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 2550 | is_bundle = 0; |
| 2551 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 2552 | |
| 2553 | if (end) { |
| 2554 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 2555 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 2556 | is_bundle = 1; |
| 2557 | break; |
| 2558 | } |
| 2559 | } |
| 2560 | |
| 2561 | if (is_bundle) { |
| 2562 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 2563 | int dp_len; |
| 2564 | |
| 2565 | dp_len = end - de->d_name; |
| 2566 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 2567 | |
| 2568 | /* increment i and free de until we get to a non-bundle cert |
| 2569 | * Note here that we look at de_list[i + 1] before freeing de |
| 2570 | * this is important since ignore_entry will free de |
| 2571 | */ |
| 2572 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 2573 | free(de); |
| 2574 | i++; |
| 2575 | de = de_list[i]; |
| 2576 | } |
| 2577 | |
| 2578 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2579 | ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 2580 | |
| 2581 | /* Successfully processed the bundle */ |
| 2582 | goto ignore_entry; |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2587 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2588 | ignore_entry: |
| 2589 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 2590 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2591 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2592 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2593 | closedir(dir); |
| 2594 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2595 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2596 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2597 | cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2598 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2599 | return cfgerr; |
| 2600 | } |
| 2601 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 2602 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 2603 | * done once. Zero is returned if the operation fails. No error is returned |
| 2604 | * if the random is said as not implemented, because we expect that openssl |
| 2605 | * will use another method once needed. |
| 2606 | */ |
| 2607 | static int ssl_initialize_random() |
| 2608 | { |
| 2609 | unsigned char random; |
| 2610 | static int random_initialized = 0; |
| 2611 | |
| 2612 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 2613 | random_initialized = 1; |
| 2614 | |
| 2615 | return random_initialized; |
| 2616 | } |
| 2617 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2618 | /* release ssl bind conf */ |
| 2619 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2620 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2621 | if (conf) { |
| 2622 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 2623 | free(conf->npn_str); |
| 2624 | conf->npn_str = NULL; |
| 2625 | #endif |
| 2626 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 2627 | free(conf->alpn_str); |
| 2628 | conf->alpn_str = NULL; |
| 2629 | #endif |
| 2630 | free(conf->ca_file); |
| 2631 | conf->ca_file = NULL; |
| 2632 | free(conf->crl_file); |
| 2633 | conf->crl_file = NULL; |
| 2634 | free(conf->ciphers); |
| 2635 | conf->ciphers = NULL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 2636 | free(conf->curves); |
| 2637 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2638 | free(conf->ecdhe); |
| 2639 | conf->ecdhe = NULL; |
| 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 2644 | { |
| 2645 | char thisline[CRT_LINESIZE]; |
| 2646 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2647 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 2648 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2649 | int linenum = 0; |
| 2650 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2651 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2652 | if ((f = fopen(file, "r")) == NULL) { |
| 2653 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2654 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2655 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2656 | |
| 2657 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2658 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2659 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2660 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2661 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2662 | char *crt_path; |
| 2663 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2664 | |
| 2665 | linenum++; |
| 2666 | end = line + strlen(line); |
| 2667 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 2668 | /* Check if we reached the limit and the last char is not \n. |
| 2669 | * Watch out for the last line without the terminating '\n'! |
| 2670 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2671 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 2672 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2673 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2674 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2675 | } |
| 2676 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2677 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2678 | newarg = 1; |
| 2679 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2680 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 2681 | /* end of string, end of loop */ |
| 2682 | *line = 0; |
| 2683 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2684 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2685 | newarg = 1; |
| 2686 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2687 | } else if (*line == '[') { |
| 2688 | if (ssl_b) { |
| 2689 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 2690 | cfgerr = 1; |
| 2691 | break; |
| 2692 | } |
| 2693 | if (!arg) { |
| 2694 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 2695 | cfgerr = 1; |
| 2696 | break; |
| 2697 | } |
| 2698 | ssl_b = arg; |
| 2699 | newarg = 1; |
| 2700 | *line = 0; |
| 2701 | } else if (*line == ']') { |
| 2702 | if (ssl_e) { |
| 2703 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2704 | cfgerr = 1; |
| 2705 | break; |
| 2706 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2707 | if (!ssl_b) { |
| 2708 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 2709 | cfgerr = 1; |
| 2710 | break; |
| 2711 | } |
| 2712 | ssl_e = arg; |
| 2713 | newarg = 1; |
| 2714 | *line = 0; |
| 2715 | } else if (newarg) { |
| 2716 | if (arg == MAX_CRT_ARGS) { |
| 2717 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 2718 | cfgerr = 1; |
| 2719 | break; |
| 2720 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2721 | newarg = 0; |
| 2722 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2723 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2724 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2725 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2726 | if (cfgerr) |
| 2727 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2728 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2729 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2730 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2731 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2732 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2733 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2734 | crt_path = args[0]; |
| 2735 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 2736 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 2737 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 2738 | crt_path, linenum, file); |
| 2739 | cfgerr = 1; |
| 2740 | break; |
| 2741 | } |
| 2742 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 2743 | crt_path = path; |
| 2744 | } |
| 2745 | |
| 2746 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 2747 | cur_arg = ssl_b ? ssl_b : 1; |
| 2748 | while (cur_arg < ssl_e) { |
| 2749 | newarg = 0; |
| 2750 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 2751 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 2752 | newarg = 1; |
| 2753 | cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
| 2754 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 2755 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 2756 | args[cur_arg], linenum, file); |
| 2757 | cfgerr = 1; |
| 2758 | } |
| 2759 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 2760 | break; |
| 2761 | } |
| 2762 | } |
| 2763 | if (!cfgerr && !newarg) { |
| 2764 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 2765 | args[cur_arg], linenum, file); |
| 2766 | cfgerr = 1; |
| 2767 | break; |
| 2768 | } |
| 2769 | } |
| 2770 | if (cfgerr) { |
| 2771 | ssl_sock_free_ssl_conf(ssl_conf); |
| 2772 | free(ssl_conf); |
| 2773 | ssl_conf = NULL; |
| 2774 | break; |
| 2775 | } |
| 2776 | |
| 2777 | if (stat(crt_path, &buf) == 0) { |
| 2778 | cfgerr = ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf, |
| 2779 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 2780 | } else { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2781 | cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf, |
| 2782 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 2783 | } |
| 2784 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2785 | if (cfgerr) { |
| 2786 | 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] | 2787 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2788 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2789 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2790 | fclose(f); |
| 2791 | return cfgerr; |
| 2792 | } |
| 2793 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2794 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 2795 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 2796 | #endif |
| 2797 | |
| 2798 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 2799 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
Willy Tarreau | 7d588ee | 2012-11-26 18:47:31 +0100 | [diff] [blame] | 2800 | #define SSL_renegotiate_pending(arg) 0 |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2801 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2802 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 2803 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 2804 | #endif |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 2805 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 2806 | #define SSL_OP_NO_TICKET 0 |
| 2807 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2808 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 2809 | #define SSL_OP_NO_COMPRESSION 0 |
| 2810 | #endif |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 2811 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 2812 | #define SSL_OP_NO_TLSv1_1 0 |
| 2813 | #endif |
| 2814 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 2815 | #define SSL_OP_NO_TLSv1_2 0 |
| 2816 | #endif |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2817 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 2818 | #define SSL_OP_SINGLE_DH_USE 0 |
| 2819 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2820 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 2821 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 2822 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2823 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 2824 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 2825 | #endif |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 2826 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 2827 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 2828 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2829 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2830 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2831 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 2832 | struct proxy *curproxy = bind_conf->frontend; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2833 | int cfgerr = 0; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 2834 | int verify = SSL_VERIFY_NONE; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 2835 | long ssloptions = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2836 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 2837 | SSL_OP_NO_SSLv2 | |
| 2838 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2839 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2840 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 2841 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
| 2842 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 2843 | long sslmode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2844 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 2845 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 2846 | SSL_MODE_RELEASE_BUFFERS | |
| 2847 | SSL_MODE_SMALL_BUFFERS; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2848 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2849 | const SSL_CIPHER * cipher = NULL; |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 2850 | char cipher_description[128]; |
| 2851 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 2852 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 2853 | which is not ephemeral DH. */ |
| 2854 | const char dhe_description[] = " Kx=DH "; |
| 2855 | const char dhe_export_description[] = " Kx=DH("; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2856 | int idx = 0; |
| 2857 | int dhe_found = 0; |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 2858 | SSL *ssl = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2859 | struct ssl_bind_conf *ssl_conf_cur; |
| 2860 | int conf_ssl_options = bind_conf->ssl_conf.ssl_options | (ssl_conf ? ssl_conf->ssl_options : 0); |
| 2861 | const char *conf_ciphers; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 2862 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2863 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 2864 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 2865 | if (!ssl_initialize_random()) { |
| 2866 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 2867 | cfgerr++; |
| 2868 | } |
| 2869 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2870 | if (conf_ssl_options & BC_SSL_O_NO_SSLV3) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2871 | ssloptions |= SSL_OP_NO_SSLv3; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2872 | if (conf_ssl_options & BC_SSL_O_NO_TLSV10) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2873 | ssloptions |= SSL_OP_NO_TLSv1; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2874 | if (conf_ssl_options & BC_SSL_O_NO_TLSV11) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 2875 | ssloptions |= SSL_OP_NO_TLSv1_1; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2876 | if (conf_ssl_options & BC_SSL_O_NO_TLSV12) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 2877 | ssloptions |= SSL_OP_NO_TLSv1_2; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2878 | if (conf_ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 2879 | ssloptions |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2880 | #ifndef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2881 | if (conf_ssl_options & BC_SSL_O_USE_SSLV3) { |
Jérémie Courrèges-Anglas | 17c3f62 | 2015-07-25 16:50:52 -0600 | [diff] [blame] | 2882 | #ifndef OPENSSL_NO_SSL3 |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 2883 | SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()); |
Jérémie Courrèges-Anglas | 17c3f62 | 2015-07-25 16:50:52 -0600 | [diff] [blame] | 2884 | #else |
| 2885 | Alert("SSLv3 support requested but unavailable.\n"); |
| 2886 | cfgerr++; |
| 2887 | #endif |
| 2888 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2889 | if (conf_ssl_options & BC_SSL_O_USE_TLSV10) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 2890 | SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()); |
| 2891 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2892 | if (conf_ssl_options & BC_SSL_O_USE_TLSV11) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 2893 | SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method()); |
| 2894 | #endif |
| 2895 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2896 | if (conf_ssl_options & BC_SSL_O_USE_TLSV12) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 2897 | SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method()); |
| 2898 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2899 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2900 | SSL_CTX_set_options(ctx, ssloptions); |
| 2901 | SSL_CTX_set_mode(ctx, sslmode); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2902 | 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] | 2903 | case SSL_SOCK_VERIFY_NONE: |
| 2904 | verify = SSL_VERIFY_NONE; |
| 2905 | break; |
| 2906 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 2907 | verify = SSL_VERIFY_PEER; |
| 2908 | break; |
| 2909 | case SSL_SOCK_VERIFY_REQUIRED: |
| 2910 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 2911 | break; |
| 2912 | } |
| 2913 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 2914 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2915 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 2916 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 2917 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2918 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2919 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2920 | 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] | 2921 | 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] | 2922 | cfgerr++; |
| 2923 | } |
| 2924 | /* set CA names fo client cert request, function returns void */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2925 | 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] | 2926 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 2927 | else { |
| 2928 | Alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 2929 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 2930 | cfgerr++; |
| 2931 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 2932 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2933 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2934 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 2935 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2936 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2937 | 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] | 2938 | 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] | 2939 | cfgerr++; |
| 2940 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 2941 | else { |
| 2942 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 2943 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2944 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 2945 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2946 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2947 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 2948 | #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] | 2949 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 2950 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
| 2951 | Alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 2952 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 2953 | cfgerr++; |
| 2954 | } |
| 2955 | } |
| 2956 | #endif |
| 2957 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2958 | if (global_ssl.life_time) |
| 2959 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emeric Brun | 4f65bff | 2012-11-16 15:11:00 +0100 | [diff] [blame] | 2960 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2961 | shared_context_set_cache(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2962 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 2963 | if (conf_ciphers && |
| 2964 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2965 | 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] | 2966 | 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] | 2967 | cfgerr++; |
| 2968 | } |
| 2969 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2970 | /* If tune.ssl.default-dh-param has not been set, |
| 2971 | neither has ssl-default-dh-file and no static DH |
| 2972 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2973 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2974 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2975 | (ssl_dh_ptr_index == -1 || |
| 2976 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 2977 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 2978 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2979 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 2980 | if (ssl) { |
| 2981 | ciphers = SSL_get_ciphers(ssl); |
| 2982 | |
| 2983 | if (ciphers) { |
| 2984 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 2985 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 2986 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 2987 | if (strstr(cipher_description, dhe_description) != NULL || |
| 2988 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 2989 | dhe_found = 1; |
| 2990 | break; |
| 2991 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 2992 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2993 | } |
| 2994 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 2995 | SSL_free(ssl); |
| 2996 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 2997 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2998 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 2999 | if (dhe_found) { |
| 3000 | 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] | 3001 | } |
| 3002 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3003 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3004 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3005 | |
| 3006 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3007 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3008 | if (local_dh_1024 == NULL) { |
| 3009 | local_dh_1024 = ssl_get_dh_1024(); |
| 3010 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3011 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3012 | if (local_dh_2048 == NULL) { |
| 3013 | local_dh_2048 = ssl_get_dh_2048(); |
| 3014 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3015 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3016 | if (local_dh_4096 == NULL) { |
| 3017 | local_dh_4096 = ssl_get_dh_4096(); |
| 3018 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3019 | } |
| 3020 | } |
| 3021 | } |
| 3022 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3023 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3024 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 3025 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3026 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 3027 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3028 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3029 | #ifdef OPENSSL_NPN_NEGOTIATED |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3030 | ssl_conf_cur = NULL; |
| 3031 | if (ssl_conf && ssl_conf->npn_str) |
| 3032 | ssl_conf_cur = ssl_conf; |
| 3033 | else if (bind_conf->ssl_conf.npn_str) |
| 3034 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 3035 | if (ssl_conf_cur) |
| 3036 | 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] | 3037 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 3038 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3039 | ssl_conf_cur = NULL; |
| 3040 | if (ssl_conf && ssl_conf->alpn_str) |
| 3041 | ssl_conf_cur = ssl_conf; |
| 3042 | else if (bind_conf->ssl_conf.alpn_str) |
| 3043 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 3044 | if (ssl_conf_cur) |
| 3045 | 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] | 3046 | #endif |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3047 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3048 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3049 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3050 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3051 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3052 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3053 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 3054 | if (conf_curves) { |
| 3055 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
| 3056 | Alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 3057 | curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3058 | cfgerr++; |
| 3059 | } |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3060 | else |
| 3061 | SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3062 | } |
| 3063 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3064 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3065 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3066 | int i; |
| 3067 | EC_KEY *ecdh; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3068 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 3069 | (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] | 3070 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3071 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3072 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
| 3073 | 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] | 3074 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3075 | cfgerr++; |
| 3076 | } |
| 3077 | else { |
| 3078 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 3079 | EC_KEY_free(ecdh); |
| 3080 | } |
| 3081 | } |
| 3082 | #endif |
| 3083 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3084 | return cfgerr; |
| 3085 | } |
| 3086 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3087 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 3088 | { |
| 3089 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 3090 | size_t prefixlen, suffixlen; |
| 3091 | |
| 3092 | /* Trivial case */ |
| 3093 | if (strcmp(pattern, hostname) == 0) |
| 3094 | return 1; |
| 3095 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3096 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 3097 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 3098 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 3099 | pattern_wildcard = NULL; |
| 3100 | pattern_left_label_end = pattern; |
| 3101 | while (*pattern_left_label_end != '.') { |
| 3102 | switch (*pattern_left_label_end) { |
| 3103 | case 0: |
| 3104 | /* End of label not found */ |
| 3105 | return 0; |
| 3106 | case '*': |
| 3107 | /* If there is more than one wildcards */ |
| 3108 | if (pattern_wildcard) |
| 3109 | return 0; |
| 3110 | pattern_wildcard = pattern_left_label_end; |
| 3111 | break; |
| 3112 | } |
| 3113 | pattern_left_label_end++; |
| 3114 | } |
| 3115 | |
| 3116 | /* If it's not trivial and there is no wildcard, it can't |
| 3117 | * match */ |
| 3118 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3119 | return 0; |
| 3120 | |
| 3121 | /* Make sure all labels match except the leftmost */ |
| 3122 | hostname_left_label_end = strchr(hostname, '.'); |
| 3123 | if (!hostname_left_label_end |
| 3124 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 3125 | return 0; |
| 3126 | |
| 3127 | /* Make sure the leftmost label of the hostname is long enough |
| 3128 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 3129 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3130 | return 0; |
| 3131 | |
| 3132 | /* Finally compare the string on either side of the |
| 3133 | * wildcard */ |
| 3134 | prefixlen = pattern_wildcard - pattern; |
| 3135 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 3136 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 3137 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3138 | return 0; |
| 3139 | |
| 3140 | return 1; |
| 3141 | } |
| 3142 | |
| 3143 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 3144 | { |
| 3145 | SSL *ssl; |
| 3146 | struct connection *conn; |
| 3147 | char *servername; |
| 3148 | |
| 3149 | int depth; |
| 3150 | X509 *cert; |
| 3151 | STACK_OF(GENERAL_NAME) *alt_names; |
| 3152 | int i; |
| 3153 | X509_NAME *cert_subject; |
| 3154 | char *str; |
| 3155 | |
| 3156 | if (ok == 0) |
| 3157 | return ok; |
| 3158 | |
| 3159 | 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] | 3160 | conn = SSL_get_app_data(ssl); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3161 | |
| 3162 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
| 3163 | |
| 3164 | /* We only need to verify the CN on the actual server cert, |
| 3165 | * not the indirect CAs */ |
| 3166 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 3167 | if (depth != 0) |
| 3168 | return ok; |
| 3169 | |
| 3170 | /* At this point, the cert is *not* OK unless we can find a |
| 3171 | * hostname match */ |
| 3172 | ok = 0; |
| 3173 | |
| 3174 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 3175 | /* It seems like this might happen if verify peer isn't set */ |
| 3176 | if (!cert) |
| 3177 | return ok; |
| 3178 | |
| 3179 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 3180 | if (alt_names) { |
| 3181 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 3182 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 3183 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 3184 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 3185 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 3186 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3187 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 3188 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3189 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 3190 | OPENSSL_free(str); |
| 3191 | } |
| 3192 | } |
| 3193 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 3194 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | cert_subject = X509_get_subject_name(cert); |
| 3198 | i = -1; |
| 3199 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 3200 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3201 | ASN1_STRING *value; |
| 3202 | value = X509_NAME_ENTRY_get_data(entry); |
| 3203 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3204 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 3205 | OPENSSL_free(str); |
| 3206 | } |
| 3207 | } |
| 3208 | |
| 3209 | return ok; |
| 3210 | } |
| 3211 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3212 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3213 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3214 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3215 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3216 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 3217 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3218 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3219 | SSL_OP_NO_SSLv2 | |
| 3220 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 3221 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3222 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3223 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3224 | SSL_MODE_RELEASE_BUFFERS | |
| 3225 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3226 | int verify = SSL_VERIFY_NONE; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3227 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3228 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 3229 | if (!ssl_initialize_random()) { |
| 3230 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 3231 | cfgerr++; |
| 3232 | } |
| 3233 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 3234 | /* Automatic memory computations need to know we use SSL there */ |
| 3235 | global.ssl_used_backend = 1; |
| 3236 | |
| 3237 | /* Initiate SSL context for current server */ |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3238 | srv->ssl_ctx.reused_sess = NULL; |
| 3239 | if (srv->use_ssl) |
| 3240 | srv->xprt = &ssl_sock; |
| 3241 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 3242 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3243 | |
| 3244 | srv->ssl_ctx.ctx = SSL_CTX_new(SSLv23_client_method()); |
| 3245 | if (!srv->ssl_ctx.ctx) { |
| 3246 | Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 3247 | proxy_type_str(curproxy), curproxy->id, |
| 3248 | srv->id); |
| 3249 | cfgerr++; |
| 3250 | return cfgerr; |
| 3251 | } |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 3252 | if (srv->ssl_ctx.client_crt) { |
| 3253 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
| 3254 | Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 3255 | proxy_type_str(curproxy), curproxy->id, |
| 3256 | srv->id, srv->ssl_ctx.client_crt); |
| 3257 | cfgerr++; |
| 3258 | } |
| 3259 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
| 3260 | Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 3261 | proxy_type_str(curproxy), curproxy->id, |
| 3262 | srv->id, srv->ssl_ctx.client_crt); |
| 3263 | cfgerr++; |
| 3264 | } |
| 3265 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
| 3266 | Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3267 | proxy_type_str(curproxy), curproxy->id, |
| 3268 | srv->id, srv->ssl_ctx.client_crt); |
| 3269 | cfgerr++; |
| 3270 | } |
| 3271 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3272 | |
| 3273 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_SSLV3) |
| 3274 | options |= SSL_OP_NO_SSLv3; |
| 3275 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV10) |
| 3276 | options |= SSL_OP_NO_TLSv1; |
| 3277 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV11) |
| 3278 | options |= SSL_OP_NO_TLSv1_1; |
| 3279 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV12) |
| 3280 | options |= SSL_OP_NO_TLSv1_2; |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 3281 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 3282 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3283 | #ifndef OPENSSL_IS_BORINGSSL |
Jérémie Courrèges-Anglas | 17c3f62 | 2015-07-25 16:50:52 -0600 | [diff] [blame] | 3284 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_SSLV3) { |
| 3285 | #ifndef OPENSSL_NO_SSL3 |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3286 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, SSLv3_client_method()); |
Jérémie Courrèges-Anglas | 17c3f62 | 2015-07-25 16:50:52 -0600 | [diff] [blame] | 3287 | #else |
Thierry FOURNIER | bc96534 | 2015-08-26 08:21:26 +0200 | [diff] [blame] | 3288 | Alert("SSLv3 support requested but unavailable.\n"); |
Jérémie Courrèges-Anglas | 17c3f62 | 2015-07-25 16:50:52 -0600 | [diff] [blame] | 3289 | cfgerr++; |
| 3290 | #endif |
| 3291 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3292 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV10) |
| 3293 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_client_method()); |
| 3294 | #if SSL_OP_NO_TLSv1_1 |
| 3295 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV11) |
| 3296 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_1_client_method()); |
| 3297 | #endif |
| 3298 | #if SSL_OP_NO_TLSv1_2 |
| 3299 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV12) |
| 3300 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_2_client_method()); |
| 3301 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3302 | #endif |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3303 | SSL_CTX_set_options(srv->ssl_ctx.ctx, options); |
| 3304 | SSL_CTX_set_mode(srv->ssl_ctx.ctx, mode); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3305 | |
| 3306 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 3307 | verify = SSL_VERIFY_PEER; |
| 3308 | |
| 3309 | switch (srv->ssl_ctx.verify) { |
| 3310 | case SSL_SOCK_VERIFY_NONE: |
| 3311 | verify = SSL_VERIFY_NONE; |
| 3312 | break; |
| 3313 | case SSL_SOCK_VERIFY_REQUIRED: |
| 3314 | verify = SSL_VERIFY_PEER; |
| 3315 | break; |
| 3316 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3317 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3318 | verify, |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3319 | srv->ssl_ctx.verify_host ? ssl_sock_srv_verifycbk : NULL); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3320 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3321 | if (srv->ssl_ctx.ca_file) { |
| 3322 | /* load CAfile to verify */ |
| 3323 | 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] | 3324 | 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] | 3325 | curproxy->id, srv->id, |
| 3326 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
| 3327 | cfgerr++; |
| 3328 | } |
| 3329 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3330 | else { |
| 3331 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 3332 | 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] | 3333 | curproxy->id, srv->id, |
| 3334 | srv->conf.file, srv->conf.line); |
| 3335 | else |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 3336 | 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] | 3337 | curproxy->id, srv->id, |
| 3338 | srv->conf.file, srv->conf.line); |
| 3339 | cfgerr++; |
| 3340 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3341 | #ifdef X509_V_FLAG_CRL_CHECK |
| 3342 | if (srv->ssl_ctx.crl_file) { |
| 3343 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 3344 | |
| 3345 | 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] | 3346 | 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] | 3347 | curproxy->id, srv->id, |
| 3348 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
| 3349 | cfgerr++; |
| 3350 | } |
| 3351 | else { |
| 3352 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 3353 | } |
| 3354 | } |
| 3355 | #endif |
| 3356 | } |
| 3357 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3358 | if (global_ssl.life_time) |
| 3359 | SSL_CTX_set_timeout(srv->ssl_ctx.ctx, global_ssl.life_time); |
Emeric Brun | 4f65bff | 2012-11-16 15:11:00 +0100 | [diff] [blame] | 3360 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3361 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF); |
| 3362 | if (srv->ssl_ctx.ciphers && |
| 3363 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
| 3364 | Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 3365 | curproxy->id, srv->id, |
| 3366 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
| 3367 | cfgerr++; |
| 3368 | } |
| 3369 | |
| 3370 | return cfgerr; |
| 3371 | } |
| 3372 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3373 | /* 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] | 3374 | * be NULL, in which case nothing is done. Returns the number of errors |
| 3375 | * encountered. |
| 3376 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3377 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3378 | { |
| 3379 | struct ebmb_node *node; |
| 3380 | struct sni_ctx *sni; |
| 3381 | int err = 0; |
| 3382 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3383 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3384 | return 0; |
| 3385 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 3386 | /* Automatic memory computations need to know we use SSL there */ |
| 3387 | global.ssl_used_frontend = 1; |
| 3388 | |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 3389 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3390 | 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] | 3391 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3392 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3393 | while (node) { |
| 3394 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 3395 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 3396 | /* only initialize the CTX on its first occurrence and |
| 3397 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3398 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3399 | node = ebmb_next(node); |
| 3400 | } |
| 3401 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3402 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3403 | while (node) { |
| 3404 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 3405 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 3406 | /* only initialize the CTX on its first occurrence and |
| 3407 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3408 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3409 | node = ebmb_next(node); |
| 3410 | } |
| 3411 | return err; |
| 3412 | } |
| 3413 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 3414 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 3415 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 3416 | * alerts are directly emitted since the rest of the stack does it below. |
| 3417 | */ |
| 3418 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 3419 | { |
| 3420 | struct proxy *px = bind_conf->frontend; |
| 3421 | int alloc_ctx; |
| 3422 | int err; |
| 3423 | |
| 3424 | if (!bind_conf->is_ssl) { |
| 3425 | if (bind_conf->default_ctx) { |
| 3426 | Warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 3427 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3428 | } |
| 3429 | return 0; |
| 3430 | } |
| 3431 | if (!bind_conf->default_ctx) { |
| 3432 | Alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 3433 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3434 | return -1; |
| 3435 | } |
| 3436 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3437 | 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] | 3438 | if (alloc_ctx < 0) { |
| 3439 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 3440 | 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"); |
| 3441 | else |
| 3442 | Alert("Unable to allocate SSL session cache.\n"); |
| 3443 | return -1; |
| 3444 | } |
| 3445 | |
| 3446 | err = 0; |
| 3447 | /* initialize all certificate contexts */ |
| 3448 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 3449 | |
| 3450 | /* initialize CA variables if the certificates generation is enabled */ |
| 3451 | err += ssl_sock_load_ca(bind_conf); |
| 3452 | |
| 3453 | return -err; |
| 3454 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 3455 | |
| 3456 | /* release ssl context allocated for servers. */ |
| 3457 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 3458 | { |
| 3459 | if (srv->ssl_ctx.ctx) |
| 3460 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 3461 | } |
| 3462 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3463 | /* 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] | 3464 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 3465 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3466 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3467 | { |
| 3468 | struct ebmb_node *node, *back; |
| 3469 | struct sni_ctx *sni; |
| 3470 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3471 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3472 | return; |
| 3473 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3474 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3475 | while (node) { |
| 3476 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 3477 | back = ebmb_next(node); |
| 3478 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3479 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3480 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3481 | ssl_sock_free_ssl_conf(sni->conf); |
| 3482 | free(sni->conf); |
| 3483 | sni->conf = NULL; |
| 3484 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3485 | free(sni); |
| 3486 | node = back; |
| 3487 | } |
| 3488 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3489 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3490 | while (node) { |
| 3491 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 3492 | back = ebmb_next(node); |
| 3493 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3494 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3495 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3496 | ssl_sock_free_ssl_conf(sni->conf); |
| 3497 | free(sni->conf); |
| 3498 | sni->conf = NULL; |
| 3499 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3500 | free(sni); |
| 3501 | node = back; |
| 3502 | } |
| 3503 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3504 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3505 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3506 | } |
| 3507 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3508 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 3509 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 3510 | { |
| 3511 | ssl_sock_free_ca(bind_conf); |
| 3512 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3513 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3514 | free(bind_conf->ca_sign_file); |
| 3515 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3516 | if (bind_conf->keys_ref) { |
| 3517 | free(bind_conf->keys_ref->filename); |
| 3518 | free(bind_conf->keys_ref->tlskeys); |
| 3519 | LIST_DEL(&bind_conf->keys_ref->list); |
| 3520 | free(bind_conf->keys_ref); |
| 3521 | } |
| 3522 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3523 | bind_conf->ca_sign_pass = NULL; |
| 3524 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3525 | } |
| 3526 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3527 | /* Load CA cert file and private key used to generate certificates */ |
| 3528 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3529 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3530 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3531 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3532 | FILE *fp; |
| 3533 | X509 *cacert = NULL; |
| 3534 | EVP_PKEY *capkey = NULL; |
| 3535 | int err = 0; |
| 3536 | |
| 3537 | if (!bind_conf || !bind_conf->generate_certs) |
| 3538 | return err; |
| 3539 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3540 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3541 | if (global_ssl.ctx_cache) |
| 3542 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 3543 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 3544 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 3545 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3546 | if (!bind_conf->ca_sign_file) { |
| 3547 | Alert("Proxy '%s': cannot enable certificate generation, " |
| 3548 | "no CA certificate File configured at [%s:%d].\n", |
| 3549 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3550 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3551 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3552 | |
| 3553 | /* read in the CA certificate */ |
| 3554 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
| 3555 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 3556 | 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] | 3557 | goto load_error; |
| 3558 | } |
| 3559 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
| 3560 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 3561 | 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] | 3562 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3563 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3564 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3565 | if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) { |
| 3566 | Alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 3567 | 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] | 3568 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3569 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3570 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3571 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3572 | bind_conf->ca_sign_cert = cacert; |
| 3573 | bind_conf->ca_sign_pkey = capkey; |
| 3574 | return err; |
| 3575 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3576 | read_error: |
| 3577 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3578 | if (capkey) EVP_PKEY_free(capkey); |
| 3579 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3580 | load_error: |
| 3581 | bind_conf->generate_certs = 0; |
| 3582 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3583 | return err; |
| 3584 | } |
| 3585 | |
| 3586 | /* Release CA cert and private key used to generate certificated */ |
| 3587 | void |
| 3588 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 3589 | { |
| 3590 | if (!bind_conf) |
| 3591 | return; |
| 3592 | |
| 3593 | if (bind_conf->ca_sign_pkey) |
| 3594 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 3595 | if (bind_conf->ca_sign_cert) |
| 3596 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 3597 | bind_conf->ca_sign_pkey = NULL; |
| 3598 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3599 | } |
| 3600 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3601 | /* |
| 3602 | * This function is called if SSL * context is not yet allocated. The function |
| 3603 | * is designed to be called before any other data-layer operation and sets the |
| 3604 | * handshake flag on the connection. It is safe to call it multiple times. |
| 3605 | * It returns 0 on success and -1 in error case. |
| 3606 | */ |
| 3607 | static int ssl_sock_init(struct connection *conn) |
| 3608 | { |
| 3609 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3610 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3611 | return 0; |
| 3612 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3613 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 3614 | return 0; |
| 3615 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3616 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 3617 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3618 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3619 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3620 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3621 | /* If it is in client mode initiate SSL session |
| 3622 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3623 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3624 | int may_retry = 1; |
| 3625 | |
| 3626 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3627 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3628 | conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3629 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3630 | if (may_retry--) { |
| 3631 | pool_gc2(); |
| 3632 | goto retry_connect; |
| 3633 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3634 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3635 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3636 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3637 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3638 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3639 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 3640 | SSL_free(conn->xprt_ctx); |
| 3641 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3642 | if (may_retry--) { |
| 3643 | pool_gc2(); |
| 3644 | goto retry_connect; |
| 3645 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3646 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 3647 | return -1; |
| 3648 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3649 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3650 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3651 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 3652 | SSL_free(conn->xprt_ctx); |
| 3653 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3654 | if (may_retry--) { |
| 3655 | pool_gc2(); |
| 3656 | goto retry_connect; |
| 3657 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3658 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 3659 | return -1; |
| 3660 | } |
| 3661 | |
| 3662 | SSL_set_connect_state(conn->xprt_ctx); |
| 3663 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 3664 | if(!SSL_set_session(conn->xprt_ctx, objt_server(conn->target)->ssl_ctx.reused_sess)) { |
| 3665 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 3666 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 3667 | } |
| 3668 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3669 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3670 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 3671 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3672 | |
| 3673 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 3674 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3675 | return 0; |
| 3676 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3677 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3678 | int may_retry = 1; |
| 3679 | |
| 3680 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3681 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3682 | conn->xprt_ctx = SSL_new(objt_listener(conn->target)->bind_conf->default_ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3683 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3684 | if (may_retry--) { |
| 3685 | pool_gc2(); |
| 3686 | goto retry_accept; |
| 3687 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3688 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3689 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3690 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3691 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3692 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3693 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 3694 | SSL_free(conn->xprt_ctx); |
| 3695 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3696 | if (may_retry--) { |
| 3697 | pool_gc2(); |
| 3698 | goto retry_accept; |
| 3699 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3700 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 3701 | return -1; |
| 3702 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3703 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3704 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3705 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 3706 | SSL_free(conn->xprt_ctx); |
| 3707 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3708 | if (may_retry--) { |
| 3709 | pool_gc2(); |
| 3710 | goto retry_accept; |
| 3711 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3712 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 3713 | return -1; |
| 3714 | } |
| 3715 | |
| 3716 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3717 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3718 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 3719 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3720 | |
| 3721 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 3722 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3723 | return 0; |
| 3724 | } |
| 3725 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3726 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3727 | return -1; |
| 3728 | } |
| 3729 | |
| 3730 | |
| 3731 | /* This is the callback which is used when an SSL handshake is pending. It |
| 3732 | * updates the FD status if it wants some polling before being called again. |
| 3733 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 3734 | * otherwise it returns non-zero and removes itself from the connection's |
| 3735 | * flags (the bit is provided in <flag> by the caller). |
| 3736 | */ |
| 3737 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 3738 | { |
| 3739 | int ret; |
| 3740 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3741 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 3742 | return 0; |
| 3743 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3744 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3745 | goto out_error; |
| 3746 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3747 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 3748 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 3749 | * Usually SSL_write and SSL_read are used and process implicitly |
| 3750 | * the reneg handshake. |
| 3751 | * Here we use SSL_peek as a workaround for reneg. |
| 3752 | */ |
| 3753 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 3754 | char c; |
| 3755 | |
| 3756 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 3757 | if (ret <= 0) { |
| 3758 | /* handshake may have not been completed, let's find why */ |
| 3759 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 3760 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 3761 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 3762 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 3763 | __conn_sock_want_send(conn); |
| 3764 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3765 | return 0; |
| 3766 | } |
| 3767 | else if (ret == SSL_ERROR_WANT_READ) { |
| 3768 | /* handshake may have been completed but we have |
| 3769 | * no more data to read. |
| 3770 | */ |
| 3771 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 3772 | ret = 1; |
| 3773 | goto reneg_ok; |
| 3774 | } |
| 3775 | /* SSL handshake needs to read, L4 connection is ready */ |
| 3776 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 3777 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 3778 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 3779 | __conn_sock_want_recv(conn); |
| 3780 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3781 | return 0; |
| 3782 | } |
| 3783 | else if (ret == SSL_ERROR_SYSCALL) { |
| 3784 | /* if errno is null, then connection was successfully established */ |
| 3785 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 3786 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3787 | if (!conn->err_code) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3788 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 3789 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 3790 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3791 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 3792 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3793 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 3794 | empty_handshake = state == TLS_ST_BEFORE; |
| 3795 | #else |
| 3796 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
| 3797 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3798 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3799 | if (!errno) { |
| 3800 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 3801 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 3802 | else |
| 3803 | conn->err_code = CO_ER_SSL_EMPTY; |
| 3804 | } |
| 3805 | else { |
| 3806 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 3807 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 3808 | else |
| 3809 | conn->err_code = CO_ER_SSL_ABORT; |
| 3810 | } |
| 3811 | } |
| 3812 | else { |
| 3813 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 3814 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3815 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3816 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 3817 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3818 | #endif |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3819 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3820 | goto out_error; |
| 3821 | } |
| 3822 | else { |
| 3823 | /* Fail on all other handshake errors */ |
| 3824 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 3825 | * buffer, causing an RST to be emitted upon close() on |
| 3826 | * TCP sockets. We first try to drain possibly pending |
| 3827 | * data to avoid this as much as possible. |
| 3828 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 3829 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3830 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 3831 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 3832 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3833 | goto out_error; |
| 3834 | } |
| 3835 | } |
| 3836 | /* read some data: consider handshake completed */ |
| 3837 | goto reneg_ok; |
| 3838 | } |
| 3839 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3840 | ret = SSL_do_handshake(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3841 | if (ret != 1) { |
| 3842 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3843 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3844 | |
| 3845 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 3846 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 3847 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 3848 | __conn_sock_want_send(conn); |
| 3849 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3850 | return 0; |
| 3851 | } |
| 3852 | else if (ret == SSL_ERROR_WANT_READ) { |
| 3853 | /* SSL handshake needs to read, L4 connection is ready */ |
| 3854 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 3855 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 3856 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 3857 | __conn_sock_want_recv(conn); |
| 3858 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3859 | return 0; |
| 3860 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 3861 | else if (ret == SSL_ERROR_SYSCALL) { |
| 3862 | /* if errno is null, then connection was successfully established */ |
| 3863 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 3864 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3865 | if (!conn->err_code) { |
| 3866 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 3867 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 3868 | #else |
| 3869 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 3870 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3871 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 3872 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3873 | #else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3874 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3875 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3876 | if (empty_handshake) { |
| 3877 | if (!errno) { |
| 3878 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 3879 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 3880 | else |
| 3881 | conn->err_code = CO_ER_SSL_EMPTY; |
| 3882 | } |
| 3883 | else { |
| 3884 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 3885 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 3886 | else |
| 3887 | conn->err_code = CO_ER_SSL_ABORT; |
| 3888 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3889 | } |
| 3890 | else { |
| 3891 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 3892 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 3893 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3894 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3895 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3896 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3897 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 3898 | goto out_error; |
| 3899 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3900 | else { |
| 3901 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 3902 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 3903 | * buffer, causing an RST to be emitted upon close() on |
| 3904 | * TCP sockets. We first try to drain possibly pending |
| 3905 | * data to avoid this as much as possible. |
| 3906 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 3907 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3908 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 3909 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 3910 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3911 | goto out_error; |
| 3912 | } |
| 3913 | } |
| 3914 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3915 | reneg_ok: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3916 | /* Handshake succeeded */ |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 3917 | if (!SSL_session_reused(conn->xprt_ctx)) { |
| 3918 | if (objt_server(conn->target)) { |
| 3919 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 3920 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 3921 | global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr; |
| 3922 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3923 | /* 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] | 3924 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3925 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
Willy Tarreau | 30fd4bd | 2016-12-22 21:54:21 +0100 | [diff] [blame] | 3926 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 3927 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3928 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 3929 | if (!(objt_server(conn->target)->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) |
| 3930 | 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] | 3931 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 3932 | else { |
| 3933 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 3934 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 3935 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 3936 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | /* The connection is now established at both layers, it's time to leave */ |
| 3940 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 3941 | return 1; |
| 3942 | |
| 3943 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3944 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 3945 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3946 | ERR_clear_error(); |
| 3947 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 3948 | /* free resumed session if exists */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3949 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 3950 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 3951 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 3952 | } |
| 3953 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3954 | /* Fail on all other handshake errors */ |
| 3955 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3956 | if (!conn->err_code) |
| 3957 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3958 | return 0; |
| 3959 | } |
| 3960 | |
| 3961 | /* 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] | 3962 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3963 | * buffer wraps, in which case a second call may be performed. The connection's |
| 3964 | * flags are updated with whatever special event is detected (error, read0, |
| 3965 | * empty). The caller is responsible for taking care of those events and |
| 3966 | * avoiding the call if inappropriate. The function does not call the |
| 3967 | * connection's polling update function, so the caller is responsible for this. |
| 3968 | */ |
| 3969 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 3970 | { |
| 3971 | int ret, done = 0; |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 3972 | int try; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3973 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3974 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3975 | goto out_error; |
| 3976 | |
| 3977 | if (conn->flags & CO_FL_HANDSHAKE) |
| 3978 | /* a handshake was requested */ |
| 3979 | return 0; |
| 3980 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 3981 | /* let's realign the buffer to optimize I/O */ |
| 3982 | if (buffer_empty(buf)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3983 | buf->p = buf->data; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3984 | |
| 3985 | /* read the largest possible block. For this, we perform only one call |
| 3986 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 3987 | * in which case we accept to do it once again. A new attempt is made on |
| 3988 | * EINTR too. |
| 3989 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 3990 | while (count > 0) { |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 3991 | /* first check if we have some room after p+i */ |
| 3992 | try = buf->data + buf->size - (buf->p + buf->i); |
| 3993 | /* otherwise continue between data and p-o */ |
| 3994 | if (try <= 0) { |
| 3995 | try = buf->p - (buf->data + buf->o); |
| 3996 | if (try <= 0) |
| 3997 | break; |
| 3998 | } |
| 3999 | if (try > count) |
| 4000 | try = count; |
| 4001 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4002 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4003 | if (conn->flags & CO_FL_ERROR) { |
| 4004 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4005 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4006 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4007 | if (ret > 0) { |
| 4008 | buf->i += ret; |
| 4009 | done += ret; |
| 4010 | if (ret < try) |
| 4011 | break; |
| 4012 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4013 | } |
| 4014 | else if (ret == 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4015 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 4016 | if (ret != SSL_ERROR_ZERO_RETURN) { |
Emeric Brun | 1c64686 | 2012-12-14 12:33:41 +0100 | [diff] [blame] | 4017 | /* error on protocol or underlying transport */ |
| 4018 | if ((ret != SSL_ERROR_SYSCALL) |
| 4019 | || (errno && (errno != EAGAIN))) |
| 4020 | conn->flags |= CO_FL_ERROR; |
| 4021 | |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4022 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4023 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4024 | ERR_clear_error(); |
| 4025 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4026 | goto read0; |
| 4027 | } |
| 4028 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4029 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4030 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4031 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4032 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4033 | __conn_sock_want_send(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4034 | break; |
| 4035 | } |
| 4036 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 4037 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4038 | /* handshake is running, and it may need to re-enable read */ |
| 4039 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 4040 | __conn_sock_want_recv(conn); |
| 4041 | break; |
| 4042 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4043 | /* we need to poll for retry a read later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4044 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4045 | break; |
| 4046 | } |
| 4047 | /* otherwise it's a real error */ |
| 4048 | goto out_error; |
| 4049 | } |
| 4050 | } |
| 4051 | return done; |
| 4052 | |
| 4053 | read0: |
| 4054 | conn_sock_read0(conn); |
| 4055 | return done; |
| 4056 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4057 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4058 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4059 | ERR_clear_error(); |
| 4060 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4061 | conn->flags |= CO_FL_ERROR; |
| 4062 | return done; |
| 4063 | } |
| 4064 | |
| 4065 | |
| 4066 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 4067 | * <flags> may contain some CO_SFL_* flags to hint the system about other |
| 4068 | * pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4069 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 4070 | * a second call may be performed. The connection's flags are updated with |
| 4071 | * whatever special event is detected (error, empty). The caller is responsible |
| 4072 | * for taking care of those events and avoiding the call if inappropriate. The |
| 4073 | * function does not call the connection's polling update function, so the caller |
| 4074 | * is responsible for this. |
| 4075 | */ |
| 4076 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 4077 | { |
| 4078 | int ret, try, done; |
| 4079 | |
| 4080 | done = 0; |
| 4081 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4082 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4083 | goto out_error; |
| 4084 | |
| 4085 | if (conn->flags & CO_FL_HANDSHAKE) |
| 4086 | /* a handshake was requested */ |
| 4087 | return 0; |
| 4088 | |
| 4089 | /* send the largest possible block. For this we perform only one call |
| 4090 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 4091 | * in which case we accept to do it once again. |
| 4092 | */ |
| 4093 | while (buf->o) { |
Kevin Hester | cad8234 | 2013-05-30 15:12:41 -0700 | [diff] [blame] | 4094 | try = bo_contig_data(buf); |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 4095 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 4096 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4097 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4098 | global_ssl.max_record && try > global_ssl.max_record) { |
| 4099 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4100 | } |
| 4101 | else { |
| 4102 | /* we need to keep the information about the fact that |
| 4103 | * we're not limiting the upcoming send(), because if it |
| 4104 | * fails, we'll have to retry with at least as many data. |
| 4105 | */ |
| 4106 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 4107 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 4108 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4109 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4110 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4111 | if (conn->flags & CO_FL_ERROR) { |
| 4112 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4113 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4114 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4115 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4116 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
| 4117 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4118 | buf->o -= ret; |
| 4119 | done += ret; |
| 4120 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 4121 | if (likely(buffer_empty(buf))) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4122 | /* optimize data alignment in the buffer */ |
| 4123 | buf->p = buf->data; |
| 4124 | |
| 4125 | /* if the system buffer is full, don't insist */ |
| 4126 | if (ret < try) |
| 4127 | break; |
| 4128 | } |
| 4129 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4130 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4131 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 4132 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4133 | /* handshake is running, and it may need to re-enable write */ |
| 4134 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 4135 | __conn_sock_want_send(conn); |
| 4136 | break; |
| 4137 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4138 | /* we need to poll to retry a write later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4139 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4140 | break; |
| 4141 | } |
| 4142 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4143 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4144 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4145 | __conn_sock_want_recv(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4146 | break; |
| 4147 | } |
| 4148 | goto out_error; |
| 4149 | } |
| 4150 | } |
| 4151 | return done; |
| 4152 | |
| 4153 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4154 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4155 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4156 | ERR_clear_error(); |
| 4157 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4158 | conn->flags |= CO_FL_ERROR; |
| 4159 | return done; |
| 4160 | } |
| 4161 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4162 | static void ssl_sock_close(struct connection *conn) { |
| 4163 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4164 | if (conn->xprt_ctx) { |
| 4165 | SSL_free(conn->xprt_ctx); |
| 4166 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4167 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4168 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4169 | } |
| 4170 | |
| 4171 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 4172 | * any case, flags the connection as reusable if no handshake was in progress. |
| 4173 | */ |
| 4174 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 4175 | { |
| 4176 | if (conn->flags & CO_FL_HANDSHAKE) |
| 4177 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 4178 | if (!clean) |
| 4179 | /* don't sent notify on SSL_shutdown */ |
Willy Tarreau | e3cc3a3 | 2017-02-13 11:12:29 +0100 | [diff] [blame^] | 4180 | SSL_set_quiet_shutdown(conn->xprt_ctx, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4181 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 4182 | if (SSL_shutdown(conn->xprt_ctx) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4183 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4184 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4185 | ERR_clear_error(); |
| 4186 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4187 | } |
| 4188 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 4189 | /* used for logging, may be changed for a sample fetch later */ |
| 4190 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 4191 | { |
| 4192 | if (!conn->xprt && !conn->xprt_ctx) |
| 4193 | return NULL; |
| 4194 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 4195 | } |
| 4196 | |
| 4197 | /* used for logging, may be changed for a sample fetch later */ |
| 4198 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 4199 | { |
| 4200 | if (!conn->xprt && !conn->xprt_ctx) |
| 4201 | return NULL; |
| 4202 | return SSL_get_version(conn->xprt_ctx); |
| 4203 | } |
| 4204 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4205 | /* Extract a serial from a cert, and copy it to a chunk. |
| 4206 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 4207 | * -1 if output is not large enough. |
| 4208 | */ |
| 4209 | static int |
| 4210 | ssl_sock_get_serial(X509 *crt, struct chunk *out) |
| 4211 | { |
| 4212 | ASN1_INTEGER *serial; |
| 4213 | |
| 4214 | serial = X509_get_serialNumber(crt); |
| 4215 | if (!serial) |
| 4216 | return 0; |
| 4217 | |
| 4218 | if (out->size < serial->length) |
| 4219 | return -1; |
| 4220 | |
| 4221 | memcpy(out->str, serial->data, serial->length); |
| 4222 | out->len = serial->length; |
| 4223 | return 1; |
| 4224 | } |
| 4225 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4226 | /* Extract a cert to der, and copy it to a chunk. |
| 4227 | * Returns 1 if cert is found and copied, 0 on der convertion failure and |
| 4228 | * -1 if output is not large enough. |
| 4229 | */ |
| 4230 | static int |
| 4231 | ssl_sock_crt2der(X509 *crt, struct chunk *out) |
| 4232 | { |
| 4233 | int len; |
| 4234 | unsigned char *p = (unsigned char *)out->str;; |
| 4235 | |
| 4236 | len =i2d_X509(crt, NULL); |
| 4237 | if (len <= 0) |
| 4238 | return 1; |
| 4239 | |
| 4240 | if (out->size < len) |
| 4241 | return -1; |
| 4242 | |
| 4243 | i2d_X509(crt,&p); |
| 4244 | out->len = len; |
| 4245 | return 1; |
| 4246 | } |
| 4247 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4248 | |
| 4249 | /* Copy Date in ASN1_UTCTIME format in struct chunk out. |
| 4250 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 4251 | * and -1 if output is not large enough. |
| 4252 | */ |
| 4253 | static int |
| 4254 | ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out) |
| 4255 | { |
| 4256 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 4257 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 4258 | |
| 4259 | if (gentm->length < 12) |
| 4260 | return 0; |
| 4261 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 4262 | return 0; |
| 4263 | if (out->size < gentm->length-2) |
| 4264 | return -1; |
| 4265 | |
| 4266 | memcpy(out->str, gentm->data+2, gentm->length-2); |
| 4267 | out->len = gentm->length-2; |
| 4268 | return 1; |
| 4269 | } |
| 4270 | else if (tm->type == V_ASN1_UTCTIME) { |
| 4271 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 4272 | |
| 4273 | if (utctm->length < 10) |
| 4274 | return 0; |
| 4275 | if (utctm->data[0] >= 0x35) |
| 4276 | return 0; |
| 4277 | if (out->size < utctm->length) |
| 4278 | return -1; |
| 4279 | |
| 4280 | memcpy(out->str, utctm->data, utctm->length); |
| 4281 | out->len = utctm->length; |
| 4282 | return 1; |
| 4283 | } |
| 4284 | |
| 4285 | return 0; |
| 4286 | } |
| 4287 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4288 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 4289 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 4290 | */ |
| 4291 | static int |
| 4292 | ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out) |
| 4293 | { |
| 4294 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4295 | ASN1_OBJECT *obj; |
| 4296 | ASN1_STRING *data; |
| 4297 | const unsigned char *data_ptr; |
| 4298 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4299 | int i, j, n; |
| 4300 | int cur = 0; |
| 4301 | const char *s; |
| 4302 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4303 | int name_count; |
| 4304 | |
| 4305 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4306 | |
| 4307 | out->len = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4308 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4309 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4310 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4311 | else |
| 4312 | j = i; |
| 4313 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4314 | ne = X509_NAME_get_entry(a, j); |
| 4315 | obj = X509_NAME_ENTRY_get_object(ne); |
| 4316 | data = X509_NAME_ENTRY_get_data(ne); |
| 4317 | data_ptr = ASN1_STRING_get0_data(data); |
| 4318 | data_len = ASN1_STRING_length(data); |
| 4319 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4320 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4321 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4322 | s = tmp; |
| 4323 | } |
| 4324 | |
| 4325 | if (chunk_strcasecmp(entry, s) != 0) |
| 4326 | continue; |
| 4327 | |
| 4328 | if (pos < 0) |
| 4329 | cur--; |
| 4330 | else |
| 4331 | cur++; |
| 4332 | |
| 4333 | if (cur != pos) |
| 4334 | continue; |
| 4335 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4336 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4337 | return -1; |
| 4338 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4339 | memcpy(out->str, data_ptr, data_len); |
| 4340 | out->len = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4341 | return 1; |
| 4342 | } |
| 4343 | |
| 4344 | return 0; |
| 4345 | |
| 4346 | } |
| 4347 | |
| 4348 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 4349 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 4350 | */ |
| 4351 | static int |
| 4352 | ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out) |
| 4353 | { |
| 4354 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4355 | ASN1_OBJECT *obj; |
| 4356 | ASN1_STRING *data; |
| 4357 | const unsigned char *data_ptr; |
| 4358 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4359 | int i, n, ln; |
| 4360 | int l = 0; |
| 4361 | const char *s; |
| 4362 | char *p; |
| 4363 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4364 | int name_count; |
| 4365 | |
| 4366 | |
| 4367 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4368 | |
| 4369 | out->len = 0; |
| 4370 | p = out->str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4371 | for (i = 0; i < name_count; i++) { |
| 4372 | ne = X509_NAME_get_entry(a, i); |
| 4373 | obj = X509_NAME_ENTRY_get_object(ne); |
| 4374 | data = X509_NAME_ENTRY_get_data(ne); |
| 4375 | data_ptr = ASN1_STRING_get0_data(data); |
| 4376 | data_len = ASN1_STRING_length(data); |
| 4377 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4378 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4379 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4380 | s = tmp; |
| 4381 | } |
| 4382 | ln = strlen(s); |
| 4383 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4384 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4385 | if (l > out->size) |
| 4386 | return -1; |
| 4387 | out->len = l; |
| 4388 | |
| 4389 | *(p++)='/'; |
| 4390 | memcpy(p, s, ln); |
| 4391 | p += ln; |
| 4392 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4393 | memcpy(p, data_ptr, data_len); |
| 4394 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | if (!out->len) |
| 4398 | return 0; |
| 4399 | |
| 4400 | return 1; |
| 4401 | } |
| 4402 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4403 | char *ssl_sock_get_version(struct connection *conn) |
| 4404 | { |
| 4405 | if (!ssl_sock_is_ssl(conn)) |
| 4406 | return NULL; |
| 4407 | |
| 4408 | return (char *)SSL_get_version(conn->xprt_ctx); |
| 4409 | } |
| 4410 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 4411 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 4412 | * to disable SNI. |
| 4413 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 4414 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 4415 | { |
| 4416 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 4417 | char *prev_name; |
| 4418 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 4419 | if (!ssl_sock_is_ssl(conn)) |
| 4420 | return; |
| 4421 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 4422 | /* if the SNI changes, we must destroy the reusable context so that a |
| 4423 | * new connection will present a new SNI. As an optimization we could |
| 4424 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 4425 | * server. |
| 4426 | */ |
| 4427 | prev_name = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 4428 | if ((!prev_name && hostname) || |
| 4429 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
| 4430 | SSL_set_session(conn->xprt_ctx, NULL); |
| 4431 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 4432 | SSL_set_tlsext_host_name(conn->xprt_ctx, hostname); |
| 4433 | #endif |
| 4434 | } |
| 4435 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4436 | /* Extract peer certificate's common name into the chunk dest |
| 4437 | * Returns |
| 4438 | * the len of the extracted common name |
| 4439 | * or 0 if no CN found in DN |
| 4440 | * or -1 on error case (i.e. no peer certificate) |
| 4441 | */ |
| 4442 | 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] | 4443 | { |
| 4444 | X509 *crt = NULL; |
| 4445 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4446 | const char find_cn[] = "CN"; |
| 4447 | const struct chunk find_cn_chunk = { |
| 4448 | .str = (char *)&find_cn, |
| 4449 | .len = sizeof(find_cn)-1 |
| 4450 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4451 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4452 | |
| 4453 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4454 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4455 | |
| 4456 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4457 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4458 | if (!crt) |
| 4459 | goto out; |
| 4460 | |
| 4461 | name = X509_get_subject_name(crt); |
| 4462 | if (!name) |
| 4463 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4464 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4465 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 4466 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4467 | if (crt) |
| 4468 | X509_free(crt); |
| 4469 | |
| 4470 | return result; |
| 4471 | } |
| 4472 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 4473 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 4474 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 4475 | { |
| 4476 | X509 *crt = NULL; |
| 4477 | |
| 4478 | if (!ssl_sock_is_ssl(conn)) |
| 4479 | return 0; |
| 4480 | |
| 4481 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4482 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4483 | if (!crt) |
| 4484 | return 0; |
| 4485 | |
| 4486 | X509_free(crt); |
| 4487 | return 1; |
| 4488 | } |
| 4489 | |
| 4490 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 4491 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4492 | { |
| 4493 | if (!ssl_sock_is_ssl(conn)) |
| 4494 | return 0; |
| 4495 | |
| 4496 | return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
| 4497 | } |
| 4498 | |
| 4499 | /* returns result from SSL verify */ |
| 4500 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 4501 | { |
| 4502 | if (!ssl_sock_is_ssl(conn)) |
| 4503 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 4504 | |
| 4505 | return (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
| 4506 | } |
| 4507 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 4508 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 4509 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4510 | /* boolean, returns true if client cert was present */ |
| 4511 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4512 | 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] | 4513 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4514 | struct connection *conn; |
| 4515 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4516 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4517 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4518 | return 0; |
| 4519 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4520 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4521 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4522 | return 0; |
| 4523 | } |
| 4524 | |
| 4525 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4526 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4527 | 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] | 4528 | |
| 4529 | return 1; |
| 4530 | } |
| 4531 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4532 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 4533 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4534 | * should be use. |
| 4535 | */ |
| 4536 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4537 | 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] | 4538 | { |
| 4539 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 4540 | X509 *crt = NULL; |
| 4541 | int ret = 0; |
| 4542 | struct chunk *smp_trash; |
| 4543 | struct connection *conn; |
| 4544 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4545 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4546 | if (!conn || conn->xprt != &ssl_sock) |
| 4547 | return 0; |
| 4548 | |
| 4549 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 4550 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4551 | return 0; |
| 4552 | } |
| 4553 | |
| 4554 | if (cert_peer) |
| 4555 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4556 | else |
| 4557 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 4558 | |
| 4559 | if (!crt) |
| 4560 | goto out; |
| 4561 | |
| 4562 | smp_trash = get_trash_chunk(); |
| 4563 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 4564 | goto out; |
| 4565 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4566 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4567 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4568 | ret = 1; |
| 4569 | out: |
| 4570 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4571 | if (cert_peer && crt) |
| 4572 | X509_free(crt); |
| 4573 | return ret; |
| 4574 | } |
| 4575 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4576 | /* binary, returns serial of certificate in a binary chunk. |
| 4577 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4578 | * should be use. |
| 4579 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4580 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4581 | 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] | 4582 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4583 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4584 | X509 *crt = NULL; |
| 4585 | int ret = 0; |
| 4586 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4587 | struct connection *conn; |
| 4588 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4589 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4590 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4591 | return 0; |
| 4592 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4593 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4594 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4595 | return 0; |
| 4596 | } |
| 4597 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4598 | if (cert_peer) |
| 4599 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4600 | else |
| 4601 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 4602 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4603 | if (!crt) |
| 4604 | goto out; |
| 4605 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4606 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4607 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 4608 | goto out; |
| 4609 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4610 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4611 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4612 | ret = 1; |
| 4613 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4614 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4615 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4616 | X509_free(crt); |
| 4617 | return ret; |
| 4618 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4619 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4620 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 4621 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4622 | * should be use. |
| 4623 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4624 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4625 | 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] | 4626 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4627 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4628 | X509 *crt = NULL; |
| 4629 | const EVP_MD *digest; |
| 4630 | int ret = 0; |
| 4631 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4632 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4633 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4634 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4635 | if (!conn || conn->xprt != &ssl_sock) |
| 4636 | return 0; |
| 4637 | |
| 4638 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4639 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4640 | return 0; |
| 4641 | } |
| 4642 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4643 | if (cert_peer) |
| 4644 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4645 | else |
| 4646 | crt = SSL_get_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4647 | if (!crt) |
| 4648 | goto out; |
| 4649 | |
| 4650 | smp_trash = get_trash_chunk(); |
| 4651 | digest = EVP_sha1(); |
| 4652 | X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len); |
| 4653 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4654 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4655 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4656 | ret = 1; |
| 4657 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4658 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4659 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4660 | X509_free(crt); |
| 4661 | return ret; |
| 4662 | } |
| 4663 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4664 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 4665 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4666 | * should be use. |
| 4667 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4668 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4669 | 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] | 4670 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4671 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4672 | X509 *crt = NULL; |
| 4673 | int ret = 0; |
| 4674 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4675 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4676 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4677 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4678 | if (!conn || conn->xprt != &ssl_sock) |
| 4679 | return 0; |
| 4680 | |
| 4681 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4682 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4683 | return 0; |
| 4684 | } |
| 4685 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4686 | if (cert_peer) |
| 4687 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4688 | else |
| 4689 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4690 | if (!crt) |
| 4691 | goto out; |
| 4692 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4693 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4694 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 4695 | goto out; |
| 4696 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4697 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4698 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4699 | ret = 1; |
| 4700 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4701 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4702 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4703 | X509_free(crt); |
| 4704 | return ret; |
| 4705 | } |
| 4706 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4707 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 4708 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4709 | * should be use. |
| 4710 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4711 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4712 | 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] | 4713 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4714 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4715 | X509 *crt = NULL; |
| 4716 | X509_NAME *name; |
| 4717 | int ret = 0; |
| 4718 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4719 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4720 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4721 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4722 | if (!conn || conn->xprt != &ssl_sock) |
| 4723 | return 0; |
| 4724 | |
| 4725 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4726 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4727 | return 0; |
| 4728 | } |
| 4729 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4730 | if (cert_peer) |
| 4731 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4732 | else |
| 4733 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4734 | if (!crt) |
| 4735 | goto out; |
| 4736 | |
| 4737 | name = X509_get_issuer_name(crt); |
| 4738 | if (!name) |
| 4739 | goto out; |
| 4740 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4741 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4742 | if (args && args[0].type == ARGT_STR) { |
| 4743 | int pos = 1; |
| 4744 | |
| 4745 | if (args[1].type == ARGT_SINT) |
| 4746 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4747 | |
| 4748 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 4749 | goto out; |
| 4750 | } |
| 4751 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 4752 | goto out; |
| 4753 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4754 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4755 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4756 | ret = 1; |
| 4757 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4758 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4759 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4760 | X509_free(crt); |
| 4761 | return ret; |
| 4762 | } |
| 4763 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4764 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 4765 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4766 | * should be use. |
| 4767 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4768 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4769 | 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] | 4770 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4771 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4772 | X509 *crt = NULL; |
| 4773 | int ret = 0; |
| 4774 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4775 | struct connection *conn; |
| 4776 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4777 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4778 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4779 | return 0; |
| 4780 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4781 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4782 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4783 | return 0; |
| 4784 | } |
| 4785 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4786 | if (cert_peer) |
| 4787 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4788 | else |
| 4789 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4790 | if (!crt) |
| 4791 | goto out; |
| 4792 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4793 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4794 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 4795 | goto out; |
| 4796 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4797 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4798 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4799 | ret = 1; |
| 4800 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4801 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4802 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4803 | X509_free(crt); |
| 4804 | return ret; |
| 4805 | } |
| 4806 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4807 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 4808 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4809 | * should be use. |
| 4810 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4811 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4812 | 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] | 4813 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4814 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4815 | X509 *crt = NULL; |
| 4816 | X509_NAME *name; |
| 4817 | int ret = 0; |
| 4818 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4819 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4820 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4821 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4822 | if (!conn || conn->xprt != &ssl_sock) |
| 4823 | return 0; |
| 4824 | |
| 4825 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4826 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4827 | return 0; |
| 4828 | } |
| 4829 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4830 | if (cert_peer) |
| 4831 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4832 | else |
| 4833 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4834 | if (!crt) |
| 4835 | goto out; |
| 4836 | |
| 4837 | name = X509_get_subject_name(crt); |
| 4838 | if (!name) |
| 4839 | goto out; |
| 4840 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4841 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4842 | if (args && args[0].type == ARGT_STR) { |
| 4843 | int pos = 1; |
| 4844 | |
| 4845 | if (args[1].type == ARGT_SINT) |
| 4846 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4847 | |
| 4848 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 4849 | goto out; |
| 4850 | } |
| 4851 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 4852 | goto out; |
| 4853 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4854 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4855 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4856 | ret = 1; |
| 4857 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4858 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4859 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4860 | X509_free(crt); |
| 4861 | return ret; |
| 4862 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 4863 | |
| 4864 | /* integer, returns true if current session use a client certificate */ |
| 4865 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4866 | 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] | 4867 | { |
| 4868 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4869 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 4870 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4871 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4872 | if (!conn || conn->xprt != &ssl_sock) |
| 4873 | return 0; |
| 4874 | |
| 4875 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 4876 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4877 | return 0; |
| 4878 | } |
| 4879 | |
| 4880 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4881 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 4882 | if (crt) { |
| 4883 | X509_free(crt); |
| 4884 | } |
| 4885 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4886 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4887 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 4888 | return 1; |
| 4889 | } |
| 4890 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4891 | /* integer, returns the certificate version |
| 4892 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4893 | * should be use. |
| 4894 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 4895 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4896 | 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] | 4897 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4898 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 4899 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4900 | struct connection *conn; |
| 4901 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4902 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4903 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 4904 | return 0; |
| 4905 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4906 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 4907 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4908 | return 0; |
| 4909 | } |
| 4910 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4911 | if (cert_peer) |
| 4912 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4913 | else |
| 4914 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 4915 | if (!crt) |
| 4916 | return 0; |
| 4917 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4918 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4919 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 4920 | if (cert_peer) |
| 4921 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4922 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 4923 | |
| 4924 | return 1; |
| 4925 | } |
| 4926 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4927 | /* string, returns the certificate's signature algorithm. |
| 4928 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4929 | * should be use. |
| 4930 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4931 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4932 | 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] | 4933 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4934 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4935 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4936 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4937 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4938 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4939 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4940 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4941 | if (!conn || conn->xprt != &ssl_sock) |
| 4942 | return 0; |
| 4943 | |
| 4944 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4945 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4946 | return 0; |
| 4947 | } |
| 4948 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4949 | if (cert_peer) |
| 4950 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4951 | else |
| 4952 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4953 | if (!crt) |
| 4954 | return 0; |
| 4955 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4956 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 4957 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4958 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4959 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 4960 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4961 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 4962 | if (cert_peer) |
| 4963 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4964 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 4965 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4966 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4967 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4968 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4969 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4970 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 4971 | if (cert_peer) |
| 4972 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4973 | |
| 4974 | return 1; |
| 4975 | } |
| 4976 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4977 | /* string, returns the certificate's key algorithm. |
| 4978 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4979 | * should be use. |
| 4980 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4981 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4982 | 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] | 4983 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4984 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4985 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4986 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4987 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4988 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4989 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4990 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4991 | if (!conn || conn->xprt != &ssl_sock) |
| 4992 | return 0; |
| 4993 | |
| 4994 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4995 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4996 | return 0; |
| 4997 | } |
| 4998 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4999 | if (cert_peer) |
| 5000 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5001 | else |
| 5002 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5003 | if (!crt) |
| 5004 | return 0; |
| 5005 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5006 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 5007 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5008 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5009 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 5010 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5011 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5012 | if (cert_peer) |
| 5013 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5014 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 5015 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5016 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5017 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5018 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5019 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5020 | if (cert_peer) |
| 5021 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5022 | |
| 5023 | return 1; |
| 5024 | } |
| 5025 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5026 | /* boolean, returns true if front conn. transport layer is SSL. |
| 5027 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5028 | * char is 'b'. |
| 5029 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5030 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5031 | 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] | 5032 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5033 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5034 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5035 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5036 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5037 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5038 | return 1; |
| 5039 | } |
| 5040 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5041 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5042 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5043 | 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] | 5044 | { |
| 5045 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5046 | struct connection *conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5047 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5048 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5049 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5050 | conn->xprt_ctx && |
| 5051 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5052 | return 1; |
| 5053 | #else |
| 5054 | return 0; |
| 5055 | #endif |
| 5056 | } |
| 5057 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 5058 | /* boolean, returns true if client session has been resumed */ |
| 5059 | static int |
| 5060 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 5061 | { |
| 5062 | struct connection *conn = objt_conn(smp->sess->origin); |
| 5063 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5064 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5065 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 5066 | conn->xprt_ctx && |
| 5067 | SSL_session_reused(conn->xprt_ctx); |
| 5068 | return 1; |
| 5069 | } |
| 5070 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5071 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 5072 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5073 | * char is 'b'. |
| 5074 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5075 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5076 | 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] | 5077 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5078 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5079 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5080 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5081 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5082 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5083 | return 0; |
| 5084 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5085 | smp->data.u.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
| 5086 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5087 | return 0; |
| 5088 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5089 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5090 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5091 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5092 | |
| 5093 | return 1; |
| 5094 | } |
| 5095 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5096 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 5097 | * is SSL. |
| 5098 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5099 | * char is 'b'. |
| 5100 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5101 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5102 | 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] | 5103 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5104 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5105 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5106 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5107 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5108 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5109 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5110 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5111 | return 0; |
| 5112 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 5113 | if (!SSL_get_cipher_bits(conn->xprt_ctx, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5114 | return 0; |
| 5115 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5116 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5117 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5118 | |
| 5119 | return 1; |
| 5120 | } |
| 5121 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5122 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 5123 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5124 | * char is 'b'. |
| 5125 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5126 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5127 | 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] | 5128 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5129 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5130 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5131 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5132 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5133 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5134 | return 0; |
| 5135 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5136 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
| 5137 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5138 | return 0; |
| 5139 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5140 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5141 | |
| 5142 | return 1; |
| 5143 | } |
| 5144 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5145 | #ifdef OPENSSL_NPN_NEGOTIATED |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5146 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5147 | 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] | 5148 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5149 | struct connection *conn; |
| 5150 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5151 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5152 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5153 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5154 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5155 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5156 | return 0; |
| 5157 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5158 | smp->data.u.str.str = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5159 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5160 | (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] | 5161 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5162 | if (!smp->data.u.str.str) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5163 | return 0; |
| 5164 | |
| 5165 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5166 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5167 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5168 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5169 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5170 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5171 | 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] | 5172 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5173 | struct connection *conn; |
| 5174 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5175 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5176 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5177 | |
Willy Tarreau | e26bf05 | 2015-05-12 10:30:12 +0200 | [diff] [blame] | 5178 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5179 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5180 | return 0; |
| 5181 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5182 | smp->data.u.str.str = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5183 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5184 | (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] | 5185 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5186 | if (!smp->data.u.str.str) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5187 | return 0; |
| 5188 | |
| 5189 | return 1; |
| 5190 | } |
| 5191 | #endif |
| 5192 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5193 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 5194 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5195 | * char is 'b'. |
| 5196 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5197 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5198 | 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] | 5199 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5200 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5201 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5202 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5203 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5204 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5205 | return 0; |
| 5206 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5207 | smp->data.u.str.str = (char *)SSL_get_version(conn->xprt_ctx); |
| 5208 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5209 | return 0; |
| 5210 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5211 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5212 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5213 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5214 | |
| 5215 | return 1; |
| 5216 | } |
| 5217 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5218 | /* 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] | 5219 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5220 | * char is 'b'. |
| 5221 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5222 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5223 | 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] | 5224 | { |
| 5225 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5226 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5227 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 5228 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5229 | SSL_SESSION *ssl_sess; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5230 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5231 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5232 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 5233 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5234 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5235 | return 0; |
| 5236 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5237 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 5238 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 5239 | return 0; |
| 5240 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5241 | smp->data.u.str.str = (char *)SSL_SESSION_get_id(ssl_sess, (unsigned int *)&smp->data.u.str.len); |
| 5242 | if (!smp->data.u.str.str || !smp->data.u.str.len) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 5243 | return 0; |
| 5244 | |
| 5245 | return 1; |
| 5246 | #else |
| 5247 | return 0; |
| 5248 | #endif |
| 5249 | } |
| 5250 | |
| 5251 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5252 | 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] | 5253 | { |
| 5254 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5255 | struct connection *conn; |
| 5256 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5257 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5258 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5259 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5260 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5261 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5262 | return 0; |
| 5263 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5264 | smp->data.u.str.str = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 5265 | if (!smp->data.u.str.str) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 5266 | return 0; |
| 5267 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5268 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5269 | return 1; |
| 5270 | #else |
| 5271 | return 0; |
| 5272 | #endif |
| 5273 | } |
| 5274 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5275 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5276 | 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] | 5277 | { |
| 5278 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5279 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5280 | smp->strm ? smp->strm->si[1].end : NULL); |
| 5281 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5282 | int finished_len; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5283 | struct chunk *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5284 | |
| 5285 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5286 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5287 | return 0; |
| 5288 | |
| 5289 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 5290 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5291 | return 0; |
| 5292 | } |
| 5293 | |
| 5294 | finished_trash = get_trash_chunk(); |
| 5295 | if (!SSL_session_reused(conn->xprt_ctx)) |
| 5296 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 5297 | else |
| 5298 | finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 5299 | |
| 5300 | if (!finished_len) |
| 5301 | return 0; |
| 5302 | |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 5303 | finished_trash->len = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5304 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5305 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5306 | |
| 5307 | return 1; |
| 5308 | #else |
| 5309 | return 0; |
| 5310 | #endif |
| 5311 | } |
| 5312 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5313 | /* 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] | 5314 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5315 | 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] | 5316 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5317 | struct connection *conn; |
| 5318 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5319 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5320 | if (!conn || conn->xprt != &ssl_sock) |
| 5321 | return 0; |
| 5322 | |
| 5323 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5324 | smp->flags = SMP_F_MAY_CHANGE; |
| 5325 | return 0; |
| 5326 | } |
| 5327 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5328 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5329 | 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] | 5330 | smp->flags = 0; |
| 5331 | |
| 5332 | return 1; |
| 5333 | } |
| 5334 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5335 | /* 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] | 5336 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5337 | 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] | 5338 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5339 | struct connection *conn; |
| 5340 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5341 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5342 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5343 | return 0; |
| 5344 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5345 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5346 | smp->flags = SMP_F_MAY_CHANGE; |
| 5347 | return 0; |
| 5348 | } |
| 5349 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5350 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5351 | 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] | 5352 | smp->flags = 0; |
| 5353 | |
| 5354 | return 1; |
| 5355 | } |
| 5356 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5357 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5358 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5359 | 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] | 5360 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5361 | struct connection *conn; |
| 5362 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5363 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5364 | if (!conn || conn->xprt != &ssl_sock) |
| 5365 | return 0; |
| 5366 | |
| 5367 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5368 | smp->flags = SMP_F_MAY_CHANGE; |
| 5369 | return 0; |
| 5370 | } |
| 5371 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5372 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5373 | 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] | 5374 | smp->flags = 0; |
| 5375 | |
| 5376 | return 1; |
| 5377 | } |
| 5378 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5379 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 5380 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5381 | 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] | 5382 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5383 | struct connection *conn; |
| 5384 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5385 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5386 | if (!conn || conn->xprt != &ssl_sock) |
| 5387 | return 0; |
| 5388 | |
| 5389 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 5390 | smp->flags = SMP_F_MAY_CHANGE; |
| 5391 | return 0; |
| 5392 | } |
| 5393 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5394 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 5395 | return 0; |
| 5396 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5397 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5398 | 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] | 5399 | smp->flags = 0; |
| 5400 | |
| 5401 | return 1; |
| 5402 | } |
| 5403 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 5404 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5405 | 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] | 5406 | { |
| 5407 | if (!*args[cur_arg + 1]) { |
| 5408 | if (err) |
| 5409 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 5410 | return ERR_ALERT | ERR_FATAL; |
| 5411 | } |
| 5412 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5413 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 5414 | 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] | 5415 | else |
| 5416 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5417 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5418 | return 0; |
| 5419 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5420 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5421 | { |
| 5422 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 5423 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5424 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5425 | /* parse the "ca-sign-file" bind keyword */ |
| 5426 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5427 | { |
| 5428 | if (!*args[cur_arg + 1]) { |
| 5429 | if (err) |
| 5430 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 5431 | return ERR_ALERT | ERR_FATAL; |
| 5432 | } |
| 5433 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5434 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 5435 | 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] | 5436 | else |
| 5437 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 5438 | |
| 5439 | return 0; |
| 5440 | } |
| 5441 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 5442 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5443 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5444 | { |
| 5445 | if (!*args[cur_arg + 1]) { |
| 5446 | if (err) |
| 5447 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 5448 | return ERR_ALERT | ERR_FATAL; |
| 5449 | } |
| 5450 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 5451 | return 0; |
| 5452 | } |
| 5453 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5454 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5455 | 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] | 5456 | { |
| 5457 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 5458 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5459 | return ERR_ALERT | ERR_FATAL; |
| 5460 | } |
| 5461 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 5462 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5463 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5464 | return 0; |
| 5465 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5466 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5467 | { |
| 5468 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 5469 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5470 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5471 | 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] | 5472 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 5473 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 5474 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5475 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 5476 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5477 | return ERR_ALERT | ERR_FATAL; |
| 5478 | } |
| 5479 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5480 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 5481 | 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] | 5482 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 5483 | return ERR_ALERT | ERR_FATAL; |
| 5484 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5485 | 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] | 5486 | if (ssl_sock_load_cert(path, conf, err) > 0) |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5487 | return ERR_ALERT | ERR_FATAL; |
| 5488 | |
| 5489 | return 0; |
| 5490 | } |
| 5491 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5492 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5493 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5494 | |
| 5495 | return 0; |
| 5496 | } |
| 5497 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5498 | /* parse the "crt-list" bind keyword */ |
| 5499 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5500 | { |
| 5501 | if (!*args[cur_arg + 1]) { |
| 5502 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 5503 | return ERR_ALERT | ERR_FATAL; |
| 5504 | } |
| 5505 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5506 | 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] | 5507 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5508 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 5509 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5510 | |
| 5511 | return 0; |
| 5512 | } |
| 5513 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 5514 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5515 | 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] | 5516 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5517 | #ifndef X509_V_FLAG_CRL_CHECK |
| 5518 | if (err) |
| 5519 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 5520 | return ERR_ALERT | ERR_FATAL; |
| 5521 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5522 | if (!*args[cur_arg + 1]) { |
| 5523 | if (err) |
| 5524 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 5525 | return ERR_ALERT | ERR_FATAL; |
| 5526 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5527 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5528 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 5529 | 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] | 5530 | else |
| 5531 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5532 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5533 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5534 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5535 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5536 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5537 | { |
| 5538 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 5539 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5540 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5541 | /* parse the "curves" bind keyword keyword */ |
| 5542 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 5543 | { |
| 5544 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 5545 | if (!*args[cur_arg + 1]) { |
| 5546 | if (err) |
| 5547 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 5548 | return ERR_ALERT | ERR_FATAL; |
| 5549 | } |
| 5550 | conf->curves = strdup(args[cur_arg + 1]); |
| 5551 | return 0; |
| 5552 | #else |
| 5553 | if (err) |
| 5554 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 5555 | return ERR_ALERT | ERR_FATAL; |
| 5556 | #endif |
| 5557 | } |
| 5558 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5559 | { |
| 5560 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 5561 | } |
| 5562 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 5563 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5564 | 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] | 5565 | { |
| 5566 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 5567 | if (err) |
| 5568 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 5569 | return ERR_ALERT | ERR_FATAL; |
| 5570 | #elif defined(OPENSSL_NO_ECDH) |
| 5571 | if (err) |
| 5572 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 5573 | return ERR_ALERT | ERR_FATAL; |
| 5574 | #else |
| 5575 | if (!*args[cur_arg + 1]) { |
| 5576 | if (err) |
| 5577 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 5578 | return ERR_ALERT | ERR_FATAL; |
| 5579 | } |
| 5580 | |
| 5581 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5582 | |
| 5583 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5584 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5585 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5586 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5587 | { |
| 5588 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 5589 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5590 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 5591 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 5592 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5593 | { |
| 5594 | int code; |
| 5595 | char *p = args[cur_arg + 1]; |
| 5596 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 5597 | |
| 5598 | if (!*p) { |
| 5599 | if (err) |
| 5600 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 5601 | return ERR_ALERT | ERR_FATAL; |
| 5602 | } |
| 5603 | |
| 5604 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 5605 | ignerr = &conf->ca_ignerr; |
| 5606 | |
| 5607 | if (strcmp(p, "all") == 0) { |
| 5608 | *ignerr = ~0ULL; |
| 5609 | return 0; |
| 5610 | } |
| 5611 | |
| 5612 | while (p) { |
| 5613 | code = atoi(p); |
| 5614 | if ((code <= 0) || (code > 63)) { |
| 5615 | if (err) |
| 5616 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 5617 | args[cur_arg], code, args[cur_arg + 1]); |
| 5618 | return ERR_ALERT | ERR_FATAL; |
| 5619 | } |
| 5620 | *ignerr |= 1ULL << code; |
| 5621 | p = strchr(p, ','); |
| 5622 | if (p) |
| 5623 | p++; |
| 5624 | } |
| 5625 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5626 | return 0; |
| 5627 | } |
| 5628 | |
| 5629 | /* parse the "force-sslv3" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5630 | static int ssl_bind_parse_force_sslv3(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5631 | { |
| 5632 | conf->ssl_options |= BC_SSL_O_USE_SSLV3; |
| 5633 | return 0; |
| 5634 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5635 | static int bind_parse_force_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5636 | { |
| 5637 | return ssl_bind_parse_force_sslv3(args, cur_arg, px, &conf->ssl_conf, err); |
| 5638 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5639 | |
| 5640 | /* parse the "force-tlsv10" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5641 | static int ssl_bind_parse_force_tlsv10(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5642 | { |
| 5643 | conf->ssl_options |= BC_SSL_O_USE_TLSV10; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 5644 | return 0; |
| 5645 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5646 | static int bind_parse_force_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5647 | { |
| 5648 | return ssl_bind_parse_force_tlsv10(args, cur_arg, px, &conf->ssl_conf, err); |
| 5649 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 5650 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5651 | /* parse the "force-tlsv11" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5652 | static int ssl_bind_parse_force_tlsv11(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5653 | { |
| 5654 | #if SSL_OP_NO_TLSv1_1 |
| 5655 | conf->ssl_options |= BC_SSL_O_USE_TLSV11; |
| 5656 | return 0; |
| 5657 | #else |
| 5658 | if (err) |
| 5659 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[cur_arg]); |
| 5660 | return ERR_ALERT | ERR_FATAL; |
| 5661 | #endif |
| 5662 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5663 | static int bind_parse_force_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5664 | { |
| 5665 | return ssl_bind_parse_force_tlsv11(args, cur_arg, px, &conf->ssl_conf, err); |
| 5666 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5667 | |
| 5668 | /* parse the "force-tlsv12" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5669 | static int ssl_bind_parse_force_tlsv12(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5670 | { |
| 5671 | #if SSL_OP_NO_TLSv1_2 |
| 5672 | conf->ssl_options |= BC_SSL_O_USE_TLSV12; |
| 5673 | return 0; |
| 5674 | #else |
| 5675 | if (err) |
| 5676 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[cur_arg]); |
| 5677 | return ERR_ALERT | ERR_FATAL; |
| 5678 | #endif |
| 5679 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5680 | static int bind_parse_force_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5681 | { |
| 5682 | return ssl_bind_parse_force_tlsv12(args, cur_arg, px, &conf->ssl_conf, err); |
| 5683 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5684 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 5685 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5686 | static int ssl_bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 5687 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5688 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 5689 | return 0; |
| 5690 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5691 | static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5692 | { |
| 5693 | return ssl_bind_parse_no_tls_tickets(args, cur_arg, px, &conf->ssl_conf, err); |
| 5694 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 5695 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 5696 | /* parse the "no-sslv3" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5697 | static int ssl_bind_parse_no_sslv3(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] | 5698 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5699 | conf->ssl_options |= BC_SSL_O_NO_SSLV3; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5700 | return 0; |
| 5701 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5702 | static int bind_parse_no_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5703 | { |
| 5704 | return ssl_bind_parse_no_sslv3(args, cur_arg, px, &conf->ssl_conf, err); |
| 5705 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5706 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 5707 | /* parse the "no-tlsv10" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5708 | static int ssl_bind_parse_no_tlsv10(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5709 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5710 | conf->ssl_options |= BC_SSL_O_NO_TLSV10; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5711 | return 0; |
| 5712 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5713 | static int bind_parse_no_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5714 | { |
| 5715 | return ssl_bind_parse_no_tlsv10(args, cur_arg, px, &conf->ssl_conf, err); |
| 5716 | } |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5717 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 5718 | /* parse the "no-tlsv11" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5719 | static int ssl_bind_parse_no_tlsv11(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5720 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5721 | conf->ssl_options |= BC_SSL_O_NO_TLSV11; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5722 | return 0; |
| 5723 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5724 | static int bind_parse_no_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5725 | { |
| 5726 | return ssl_bind_parse_no_tlsv11(args, cur_arg, px, &conf->ssl_conf, err); |
| 5727 | } |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5728 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 5729 | /* parse the "no-tlsv12" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5730 | static int ssl_bind_parse_no_tlsv12(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] | 5731 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5732 | conf->ssl_options |= BC_SSL_O_NO_TLSV12; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5733 | return 0; |
| 5734 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5735 | static int bind_parse_no_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5736 | { |
| 5737 | return ssl_bind_parse_no_tlsv12(args, cur_arg, px, &conf->ssl_conf, err); |
| 5738 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5739 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5740 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5741 | 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] | 5742 | { |
| 5743 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 5744 | char *p1, *p2; |
| 5745 | |
| 5746 | if (!*args[cur_arg + 1]) { |
| 5747 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 5748 | return ERR_ALERT | ERR_FATAL; |
| 5749 | } |
| 5750 | |
| 5751 | free(conf->npn_str); |
| 5752 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 5753 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 5754 | * so we reuse each comma to store the next <len> and need |
| 5755 | * one more for the end of the string. |
| 5756 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5757 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 5758 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5759 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 5760 | |
| 5761 | /* replace commas with the name length */ |
| 5762 | p1 = conf->npn_str; |
| 5763 | p2 = p1 + 1; |
| 5764 | while (1) { |
| 5765 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 5766 | if (!p2) |
| 5767 | p2 = p1 + 1 + strlen(p1 + 1); |
| 5768 | |
| 5769 | if (p2 - (p1 + 1) > 255) { |
| 5770 | *p2 = '\0'; |
| 5771 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 5772 | return ERR_ALERT | ERR_FATAL; |
| 5773 | } |
| 5774 | |
| 5775 | *p1 = p2 - (p1 + 1); |
| 5776 | p1 = p2; |
| 5777 | |
| 5778 | if (!*p2) |
| 5779 | break; |
| 5780 | |
| 5781 | *(p2++) = '\0'; |
| 5782 | } |
| 5783 | return 0; |
| 5784 | #else |
| 5785 | if (err) |
| 5786 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 5787 | return ERR_ALERT | ERR_FATAL; |
| 5788 | #endif |
| 5789 | } |
| 5790 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5791 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5792 | { |
| 5793 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 5794 | } |
| 5795 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5796 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5797 | 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] | 5798 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5799 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5800 | char *p1, *p2; |
| 5801 | |
| 5802 | if (!*args[cur_arg + 1]) { |
| 5803 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 5804 | return ERR_ALERT | ERR_FATAL; |
| 5805 | } |
| 5806 | |
| 5807 | free(conf->alpn_str); |
| 5808 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 5809 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 5810 | * so we reuse each comma to store the next <len> and need |
| 5811 | * one more for the end of the string. |
| 5812 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5813 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 5814 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5815 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 5816 | |
| 5817 | /* replace commas with the name length */ |
| 5818 | p1 = conf->alpn_str; |
| 5819 | p2 = p1 + 1; |
| 5820 | while (1) { |
| 5821 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 5822 | if (!p2) |
| 5823 | p2 = p1 + 1 + strlen(p1 + 1); |
| 5824 | |
| 5825 | if (p2 - (p1 + 1) > 255) { |
| 5826 | *p2 = '\0'; |
| 5827 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 5828 | return ERR_ALERT | ERR_FATAL; |
| 5829 | } |
| 5830 | |
| 5831 | *p1 = p2 - (p1 + 1); |
| 5832 | p1 = p2; |
| 5833 | |
| 5834 | if (!*p2) |
| 5835 | break; |
| 5836 | |
| 5837 | *(p2++) = '\0'; |
| 5838 | } |
| 5839 | return 0; |
| 5840 | #else |
| 5841 | if (err) |
| 5842 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 5843 | return ERR_ALERT | ERR_FATAL; |
| 5844 | #endif |
| 5845 | } |
| 5846 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5847 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5848 | { |
| 5849 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 5850 | } |
| 5851 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5852 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5853 | 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] | 5854 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 5855 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5856 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 5857 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5858 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 5859 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
| 5860 | conf->ssl_conf.ssl_options |= global_ssl.listen_default_ssloptions; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 5861 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5862 | return 0; |
| 5863 | } |
| 5864 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5865 | /* parse the "generate-certificates" bind keyword */ |
| 5866 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5867 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5868 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5869 | conf->generate_certs = 1; |
| 5870 | #else |
| 5871 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 5872 | err && *err ? *err : ""); |
| 5873 | #endif |
| 5874 | return 0; |
| 5875 | } |
| 5876 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 5877 | /* parse the "strict-sni" bind keyword */ |
| 5878 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5879 | { |
| 5880 | conf->strict_sni = 1; |
| 5881 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5882 | } |
| 5883 | |
| 5884 | /* parse the "tls-ticket-keys" bind keyword */ |
| 5885 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5886 | { |
| 5887 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 5888 | FILE *f; |
| 5889 | int i = 0; |
| 5890 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 5891 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5892 | |
| 5893 | if (!*args[cur_arg + 1]) { |
| 5894 | if (err) |
| 5895 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 5896 | return ERR_ALERT | ERR_FATAL; |
| 5897 | } |
| 5898 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 5899 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
| 5900 | if(keys_ref) { |
| 5901 | conf->keys_ref = keys_ref; |
| 5902 | return 0; |
| 5903 | } |
| 5904 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 5905 | keys_ref = malloc(sizeof(*keys_ref)); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 5906 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct tls_sess_key)); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5907 | |
| 5908 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 5909 | if (err) |
| 5910 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 5911 | return ERR_ALERT | ERR_FATAL; |
| 5912 | } |
| 5913 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 5914 | keys_ref->filename = strdup(args[cur_arg + 1]); |
| 5915 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5916 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 5917 | int len = strlen(thisline); |
| 5918 | /* Strip newline characters from the end */ |
| 5919 | if(thisline[len - 1] == '\n') |
| 5920 | thisline[--len] = 0; |
| 5921 | |
| 5922 | if(thisline[len - 1] == '\r') |
| 5923 | thisline[--len] = 0; |
| 5924 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 5925 | 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] | 5926 | if (err) |
| 5927 | 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] | 5928 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5929 | return ERR_ALERT | ERR_FATAL; |
| 5930 | } |
| 5931 | i++; |
| 5932 | } |
| 5933 | |
| 5934 | if (i < TLS_TICKETS_NO) { |
| 5935 | if (err) |
| 5936 | 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] | 5937 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5938 | return ERR_ALERT | ERR_FATAL; |
| 5939 | } |
| 5940 | |
| 5941 | fclose(f); |
| 5942 | |
| 5943 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 5944 | i -= 2; |
| 5945 | 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] | 5946 | keys_ref->unique_id = -1; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 5947 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5948 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 5949 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 5950 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5951 | return 0; |
| 5952 | #else |
| 5953 | if (err) |
| 5954 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 5955 | return ERR_ALERT | ERR_FATAL; |
| 5956 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 5957 | } |
| 5958 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5959 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5960 | 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] | 5961 | { |
| 5962 | if (!*args[cur_arg + 1]) { |
| 5963 | if (err) |
| 5964 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 5965 | return ERR_ALERT | ERR_FATAL; |
| 5966 | } |
| 5967 | |
| 5968 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5969 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5970 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5971 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5972 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5973 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5974 | else { |
| 5975 | if (err) |
| 5976 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 5977 | args[cur_arg], args[cur_arg + 1]); |
| 5978 | return ERR_ALERT | ERR_FATAL; |
| 5979 | } |
| 5980 | |
| 5981 | return 0; |
| 5982 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5983 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5984 | { |
| 5985 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 5986 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5987 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 5988 | /************** "server" keywords ****************/ |
| 5989 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5990 | /* parse the "ca-file" server keyword */ |
| 5991 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5992 | { |
| 5993 | if (!*args[*cur_arg + 1]) { |
| 5994 | if (err) |
| 5995 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 5996 | return ERR_ALERT | ERR_FATAL; |
| 5997 | } |
| 5998 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5999 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6000 | 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] | 6001 | else |
| 6002 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 6003 | |
| 6004 | return 0; |
| 6005 | } |
| 6006 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6007 | /* parse the "check-ssl" server keyword */ |
| 6008 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6009 | { |
| 6010 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6011 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 6012 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
| 6013 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6014 | return 0; |
| 6015 | } |
| 6016 | |
| 6017 | /* parse the "ciphers" server keyword */ |
| 6018 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6019 | { |
| 6020 | if (!*args[*cur_arg + 1]) { |
| 6021 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 6022 | return ERR_ALERT | ERR_FATAL; |
| 6023 | } |
| 6024 | |
| 6025 | free(newsrv->ssl_ctx.ciphers); |
| 6026 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 6027 | return 0; |
| 6028 | } |
| 6029 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6030 | /* parse the "crl-file" server keyword */ |
| 6031 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6032 | { |
| 6033 | #ifndef X509_V_FLAG_CRL_CHECK |
| 6034 | if (err) |
| 6035 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 6036 | return ERR_ALERT | ERR_FATAL; |
| 6037 | #else |
| 6038 | if (!*args[*cur_arg + 1]) { |
| 6039 | if (err) |
| 6040 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 6041 | return ERR_ALERT | ERR_FATAL; |
| 6042 | } |
| 6043 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6044 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6045 | 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] | 6046 | else |
| 6047 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 6048 | |
| 6049 | return 0; |
| 6050 | #endif |
| 6051 | } |
| 6052 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6053 | /* parse the "crt" server keyword */ |
| 6054 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6055 | { |
| 6056 | if (!*args[*cur_arg + 1]) { |
| 6057 | if (err) |
| 6058 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 6059 | return ERR_ALERT | ERR_FATAL; |
| 6060 | } |
| 6061 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6062 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
| 6063 | 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] | 6064 | else |
| 6065 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 6066 | |
| 6067 | return 0; |
| 6068 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6069 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6070 | /* parse the "force-sslv3" server keyword */ |
| 6071 | static int srv_parse_force_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6072 | { |
| 6073 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_SSLV3; |
| 6074 | return 0; |
| 6075 | } |
| 6076 | |
| 6077 | /* parse the "force-tlsv10" server keyword */ |
| 6078 | static int srv_parse_force_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6079 | { |
| 6080 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV10; |
| 6081 | return 0; |
| 6082 | } |
| 6083 | |
| 6084 | /* parse the "force-tlsv11" server keyword */ |
| 6085 | static int srv_parse_force_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6086 | { |
| 6087 | #if SSL_OP_NO_TLSv1_1 |
| 6088 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV11; |
| 6089 | return 0; |
| 6090 | #else |
| 6091 | if (err) |
| 6092 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[*cur_arg]); |
| 6093 | return ERR_ALERT | ERR_FATAL; |
| 6094 | #endif |
| 6095 | } |
| 6096 | |
| 6097 | /* parse the "force-tlsv12" server keyword */ |
| 6098 | static int srv_parse_force_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6099 | { |
| 6100 | #if SSL_OP_NO_TLSv1_2 |
| 6101 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV12; |
| 6102 | return 0; |
| 6103 | #else |
| 6104 | if (err) |
| 6105 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[*cur_arg]); |
| 6106 | return ERR_ALERT | ERR_FATAL; |
| 6107 | #endif |
| 6108 | } |
| 6109 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 6110 | /* parse the "no-ssl-reuse" server keyword */ |
| 6111 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6112 | { |
| 6113 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 6114 | return 0; |
| 6115 | } |
| 6116 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6117 | /* parse the "no-sslv3" server keyword */ |
| 6118 | static int srv_parse_no_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6119 | { |
| 6120 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_SSLV3; |
| 6121 | return 0; |
| 6122 | } |
| 6123 | |
| 6124 | /* parse the "no-tlsv10" server keyword */ |
| 6125 | static int srv_parse_no_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6126 | { |
| 6127 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV10; |
| 6128 | return 0; |
| 6129 | } |
| 6130 | |
| 6131 | /* parse the "no-tlsv11" server keyword */ |
| 6132 | static int srv_parse_no_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6133 | { |
| 6134 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV11; |
| 6135 | return 0; |
| 6136 | } |
| 6137 | |
| 6138 | /* parse the "no-tlsv12" server keyword */ |
| 6139 | static int srv_parse_no_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6140 | { |
| 6141 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV12; |
| 6142 | return 0; |
| 6143 | } |
| 6144 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 6145 | /* parse the "no-tls-tickets" server keyword */ |
| 6146 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6147 | { |
| 6148 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 6149 | return 0; |
| 6150 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6151 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 6152 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6153 | { |
| 6154 | newsrv->pp_opts |= SRV_PP_V2; |
| 6155 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 6156 | return 0; |
| 6157 | } |
| 6158 | |
| 6159 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 6160 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6161 | { |
| 6162 | newsrv->pp_opts |= SRV_PP_V2; |
| 6163 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 6164 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 6165 | return 0; |
| 6166 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 6167 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6168 | /* parse the "sni" server keyword */ |
| 6169 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6170 | { |
| 6171 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 6172 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 6173 | return ERR_ALERT | ERR_FATAL; |
| 6174 | #else |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 6175 | int idx; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6176 | struct sample_expr *expr; |
| 6177 | |
| 6178 | if (!*args[*cur_arg + 1]) { |
| 6179 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 6180 | return ERR_ALERT | ERR_FATAL; |
| 6181 | } |
| 6182 | |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 6183 | idx = (*cur_arg) + 1; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6184 | proxy->conf.args.ctx = ARGC_SRV; |
| 6185 | |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 6186 | 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] | 6187 | if (!expr) { |
| 6188 | memprintf(err, "error detected while parsing sni expression : %s", *err); |
| 6189 | return ERR_ALERT | ERR_FATAL; |
| 6190 | } |
| 6191 | |
| 6192 | if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) { |
| 6193 | memprintf(err, "error detected while parsing sni expression : " |
| 6194 | " 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] | 6195 | args[idx-1], sample_src_names(expr->fetch->use)); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6196 | return ERR_ALERT | ERR_FATAL; |
| 6197 | } |
| 6198 | |
| 6199 | px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 6200 | newsrv->ssl_ctx.sni = expr; |
| 6201 | return 0; |
| 6202 | #endif |
| 6203 | } |
| 6204 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6205 | /* parse the "ssl" server keyword */ |
| 6206 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6207 | { |
| 6208 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6209 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 6210 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6211 | return 0; |
| 6212 | } |
| 6213 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6214 | /* parse the "verify" server keyword */ |
| 6215 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6216 | { |
| 6217 | if (!*args[*cur_arg + 1]) { |
| 6218 | if (err) |
| 6219 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 6220 | return ERR_ALERT | ERR_FATAL; |
| 6221 | } |
| 6222 | |
| 6223 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6224 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6225 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6226 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6227 | else { |
| 6228 | if (err) |
| 6229 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 6230 | args[*cur_arg], args[*cur_arg + 1]); |
| 6231 | return ERR_ALERT | ERR_FATAL; |
| 6232 | } |
| 6233 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6234 | return 0; |
| 6235 | } |
| 6236 | |
| 6237 | /* parse the "verifyhost" server keyword */ |
| 6238 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6239 | { |
| 6240 | if (!*args[*cur_arg + 1]) { |
| 6241 | if (err) |
| 6242 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 6243 | return ERR_ALERT | ERR_FATAL; |
| 6244 | } |
| 6245 | |
| 6246 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 6247 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6248 | return 0; |
| 6249 | } |
| 6250 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6251 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 6252 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 6253 | struct proxy *defpx, const char *file, int line, |
| 6254 | char **err) { |
| 6255 | int i = 1; |
| 6256 | |
| 6257 | if (*(args[i]) == 0) { |
| 6258 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 6259 | return -1; |
| 6260 | } |
| 6261 | while (*(args[i])) { |
| 6262 | if (!strcmp(args[i], "no-sslv3")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6263 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_SSLV3; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6264 | else if (!strcmp(args[i], "no-tlsv10")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6265 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLSV10; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6266 | else if (!strcmp(args[i], "no-tlsv11")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6267 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLSV11; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6268 | else if (!strcmp(args[i], "no-tlsv12")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6269 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLSV12; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6270 | else if (!strcmp(args[i], "force-sslv3")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6271 | global_ssl.listen_default_ssloptions |= BC_SSL_O_USE_SSLV3; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6272 | else if (!strcmp(args[i], "force-tlsv10")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6273 | global_ssl.listen_default_ssloptions |= BC_SSL_O_USE_TLSV10; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6274 | else if (!strcmp(args[i], "force-tlsv11")) { |
| 6275 | #if SSL_OP_NO_TLSv1_1 |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6276 | global_ssl.listen_default_ssloptions |= BC_SSL_O_USE_TLSV11; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6277 | #else |
| 6278 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.1", args[0], args[i]); |
| 6279 | return -1; |
| 6280 | #endif |
| 6281 | } |
| 6282 | else if (!strcmp(args[i], "force-tlsv12")) { |
| 6283 | #if SSL_OP_NO_TLSv1_2 |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6284 | global_ssl.listen_default_ssloptions |= BC_SSL_O_USE_TLSV12; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6285 | #else |
| 6286 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.2", args[0], args[i]); |
| 6287 | return -1; |
| 6288 | #endif |
| 6289 | } |
| 6290 | else if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6291 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6292 | else { |
| 6293 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 6294 | return -1; |
| 6295 | } |
| 6296 | i++; |
| 6297 | } |
| 6298 | return 0; |
| 6299 | } |
| 6300 | |
| 6301 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 6302 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 6303 | struct proxy *defpx, const char *file, int line, |
| 6304 | char **err) { |
| 6305 | int i = 1; |
| 6306 | |
| 6307 | if (*(args[i]) == 0) { |
| 6308 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 6309 | return -1; |
| 6310 | } |
| 6311 | while (*(args[i])) { |
| 6312 | if (!strcmp(args[i], "no-sslv3")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6313 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_SSLV3; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6314 | else if (!strcmp(args[i], "no-tlsv10")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6315 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV10; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6316 | else if (!strcmp(args[i], "no-tlsv11")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6317 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV11; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6318 | else if (!strcmp(args[i], "no-tlsv12")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6319 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV12; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6320 | else if (!strcmp(args[i], "force-sslv3")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6321 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_USE_SSLV3; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6322 | else if (!strcmp(args[i], "force-tlsv10")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6323 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV10; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6324 | else if (!strcmp(args[i], "force-tlsv11")) { |
| 6325 | #if SSL_OP_NO_TLSv1_1 |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6326 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV11; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6327 | #else |
| 6328 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.1", args[0], args[i]); |
| 6329 | return -1; |
| 6330 | #endif |
| 6331 | } |
| 6332 | else if (!strcmp(args[i], "force-tlsv12")) { |
| 6333 | #if SSL_OP_NO_TLSv1_2 |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6334 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV12; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6335 | #else |
| 6336 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.2", args[0], args[i]); |
| 6337 | return -1; |
| 6338 | #endif |
| 6339 | } |
| 6340 | else if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6341 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6342 | else { |
| 6343 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 6344 | return -1; |
| 6345 | } |
| 6346 | i++; |
| 6347 | } |
| 6348 | return 0; |
| 6349 | } |
| 6350 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 6351 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 6352 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6353 | */ |
| 6354 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 6355 | struct proxy *defpx, const char *file, int line, |
| 6356 | char **err) |
| 6357 | { |
| 6358 | char **target; |
| 6359 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6360 | 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] | 6361 | |
| 6362 | if (too_many_args(1, args, err, NULL)) |
| 6363 | return -1; |
| 6364 | |
| 6365 | if (*target) { |
| 6366 | memprintf(err, "'%s' already specified.", args[0]); |
| 6367 | return -1; |
| 6368 | } |
| 6369 | |
| 6370 | if (*(args[1]) == 0) { |
| 6371 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 6372 | return -1; |
| 6373 | } |
| 6374 | *target = strdup(args[1]); |
| 6375 | return 0; |
| 6376 | } |
| 6377 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 6378 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 6379 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 6380 | */ |
| 6381 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 6382 | struct proxy *defpx, const char *file, int line, |
| 6383 | char **err) |
| 6384 | { |
| 6385 | char **target; |
| 6386 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6387 | 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] | 6388 | |
| 6389 | if (too_many_args(1, args, err, NULL)) |
| 6390 | return -1; |
| 6391 | |
| 6392 | if (*(args[1]) == 0) { |
| 6393 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 6394 | return -1; |
| 6395 | } |
| 6396 | |
| 6397 | free(*target); |
| 6398 | *target = strdup(args[1]); |
| 6399 | return 0; |
| 6400 | } |
| 6401 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6402 | /* parse various global tune.ssl settings consisting in positive integers. |
| 6403 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6404 | */ |
| 6405 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 6406 | struct proxy *defpx, const char *file, int line, |
| 6407 | char **err) |
| 6408 | { |
| 6409 | int *target; |
| 6410 | |
| 6411 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 6412 | target = &global.tune.sslcachesize; |
| 6413 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6414 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6415 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6416 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 6417 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 6418 | target = &global.maxsslconn; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6419 | else { |
| 6420 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 6421 | return -1; |
| 6422 | } |
| 6423 | |
| 6424 | if (too_many_args(1, args, err, NULL)) |
| 6425 | return -1; |
| 6426 | |
| 6427 | if (*(args[1]) == 0) { |
| 6428 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 6429 | return -1; |
| 6430 | } |
| 6431 | |
| 6432 | *target = atoi(args[1]); |
| 6433 | if (*target < 0) { |
| 6434 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 6435 | return -1; |
| 6436 | } |
| 6437 | return 0; |
| 6438 | } |
| 6439 | |
| 6440 | /* parse "ssl.force-private-cache". |
| 6441 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6442 | */ |
| 6443 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 6444 | struct proxy *defpx, const char *file, int line, |
| 6445 | char **err) |
| 6446 | { |
| 6447 | if (too_many_args(0, args, err, NULL)) |
| 6448 | return -1; |
| 6449 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6450 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6451 | return 0; |
| 6452 | } |
| 6453 | |
| 6454 | /* parse "ssl.lifetime". |
| 6455 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6456 | */ |
| 6457 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 6458 | struct proxy *defpx, const char *file, int line, |
| 6459 | char **err) |
| 6460 | { |
| 6461 | const char *res; |
| 6462 | |
| 6463 | if (too_many_args(1, args, err, NULL)) |
| 6464 | return -1; |
| 6465 | |
| 6466 | if (*(args[1]) == 0) { |
| 6467 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 6468 | return -1; |
| 6469 | } |
| 6470 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6471 | 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] | 6472 | if (res) { |
| 6473 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 6474 | return -1; |
| 6475 | } |
| 6476 | return 0; |
| 6477 | } |
| 6478 | |
| 6479 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 6480 | /* parse "ssl-dh-param-file". |
| 6481 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6482 | */ |
| 6483 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 6484 | struct proxy *defpx, const char *file, int line, |
| 6485 | char **err) |
| 6486 | { |
| 6487 | if (too_many_args(1, args, err, NULL)) |
| 6488 | return -1; |
| 6489 | |
| 6490 | if (*(args[1]) == 0) { |
| 6491 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 6492 | return -1; |
| 6493 | } |
| 6494 | |
| 6495 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 6496 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 6497 | return -1; |
| 6498 | } |
| 6499 | return 0; |
| 6500 | } |
| 6501 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6502 | /* parse "ssl.default-dh-param". |
| 6503 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6504 | */ |
| 6505 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 6506 | struct proxy *defpx, const char *file, int line, |
| 6507 | char **err) |
| 6508 | { |
| 6509 | if (too_many_args(1, args, err, NULL)) |
| 6510 | return -1; |
| 6511 | |
| 6512 | if (*(args[1]) == 0) { |
| 6513 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 6514 | return -1; |
| 6515 | } |
| 6516 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6517 | global_ssl.default_dh_param = atoi(args[1]); |
| 6518 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6519 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 6520 | return -1; |
| 6521 | } |
| 6522 | return 0; |
| 6523 | } |
| 6524 | #endif |
| 6525 | |
| 6526 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6527 | /* This function is used with TLS ticket keys management. It permits to browse |
| 6528 | * each reference. The variable <getnext> must contain the current node, |
| 6529 | * <end> point to the root node. |
| 6530 | */ |
| 6531 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6532 | static inline |
| 6533 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 6534 | { |
| 6535 | struct tls_keys_ref *ref = getnext; |
| 6536 | |
| 6537 | while (1) { |
| 6538 | |
| 6539 | /* Get next list entry. */ |
| 6540 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 6541 | |
| 6542 | /* If the entry is the last of the list, return NULL. */ |
| 6543 | if (&ref->list == end) |
| 6544 | return NULL; |
| 6545 | |
| 6546 | return ref; |
| 6547 | } |
| 6548 | } |
| 6549 | |
| 6550 | static inline |
| 6551 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 6552 | { |
| 6553 | int id; |
| 6554 | char *error; |
| 6555 | |
| 6556 | /* If the reference starts by a '#', this is numeric id. */ |
| 6557 | if (reference[0] == '#') { |
| 6558 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 6559 | id = strtol(reference + 1, &error, 10); |
| 6560 | if (*error != '\0') |
| 6561 | return NULL; |
| 6562 | |
| 6563 | /* Perform the unique id lookup. */ |
| 6564 | return tlskeys_ref_lookupid(id); |
| 6565 | } |
| 6566 | |
| 6567 | /* Perform the string lookup. */ |
| 6568 | return tlskeys_ref_lookup(reference); |
| 6569 | } |
| 6570 | #endif |
| 6571 | |
| 6572 | |
| 6573 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6574 | |
| 6575 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 6576 | |
| 6577 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 6578 | return cli_io_handler_tlskeys_files(appctx); |
| 6579 | } |
| 6580 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6581 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 6582 | * (next index to be dumped), and cli.p0 (next key reference). |
| 6583 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6584 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 6585 | |
| 6586 | struct stream_interface *si = appctx->owner; |
| 6587 | |
| 6588 | switch (appctx->st2) { |
| 6589 | case STAT_ST_INIT: |
| 6590 | /* Display the column headers. If the message cannot be sent, |
| 6591 | * quit the fucntion with returning 0. The function is called |
| 6592 | * later and restart at the state "STAT_ST_INIT". |
| 6593 | */ |
| 6594 | chunk_reset(&trash); |
| 6595 | |
| 6596 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 6597 | chunk_appendf(&trash, "# id secret\n"); |
| 6598 | else |
| 6599 | chunk_appendf(&trash, "# id (file)\n"); |
| 6600 | |
| 6601 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 6602 | si_applet_cant_put(si); |
| 6603 | return 0; |
| 6604 | } |
| 6605 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6606 | /* Now, we start the browsing of the references lists. |
| 6607 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 6608 | * available field of this pointer is <list>. It is used with the function |
| 6609 | * tlskeys_list_get_next() for retruning the first available entry |
| 6610 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6611 | if (appctx->ctx.cli.p0 == NULL) { |
| 6612 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 6613 | 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] | 6614 | } |
| 6615 | |
| 6616 | appctx->st2 = STAT_ST_LIST; |
| 6617 | /* fall through */ |
| 6618 | |
| 6619 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6620 | while (appctx->ctx.cli.p0) { |
| 6621 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
| 6622 | int head = ref->tls_ticket_enc_index; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6623 | |
| 6624 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6625 | 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] | 6626 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6627 | |
| 6628 | if (appctx->ctx.cli.i1 == 0) |
| 6629 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 6630 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6631 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6632 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6633 | struct chunk *t2 = get_trash_chunk(); |
| 6634 | |
| 6635 | chunk_reset(t2); |
| 6636 | /* 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] | 6637 | 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] | 6638 | sizeof(struct tls_sess_key), t2->str, t2->size); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6639 | 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] | 6640 | |
| 6641 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 6642 | /* let's try again later from this stream. We add ourselves into |
| 6643 | * this stream's users so that it can remove us upon termination. |
| 6644 | */ |
| 6645 | si_applet_cant_put(si); |
| 6646 | return 0; |
| 6647 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6648 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6649 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6650 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6651 | } |
| 6652 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 6653 | /* let's try again later from this stream. We add ourselves into |
| 6654 | * this stream's users so that it can remove us upon termination. |
| 6655 | */ |
| 6656 | si_applet_cant_put(si); |
| 6657 | return 0; |
| 6658 | } |
| 6659 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6660 | 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] | 6661 | break; |
| 6662 | |
| 6663 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6664 | 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] | 6665 | } |
| 6666 | |
| 6667 | appctx->st2 = STAT_ST_FIN; |
| 6668 | /* fall through */ |
| 6669 | |
| 6670 | default: |
| 6671 | appctx->st2 = STAT_ST_FIN; |
| 6672 | return 1; |
| 6673 | } |
| 6674 | return 0; |
| 6675 | } |
| 6676 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6677 | /* 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] | 6678 | static int cli_parse_show_tlskeys(char **args, struct appctx *appctx, void *private) |
| 6679 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6680 | /* no parameter, shows only file list */ |
| 6681 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6682 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6683 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 6684 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6685 | } |
| 6686 | |
| 6687 | if (args[2][0] == '*') { |
| 6688 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6689 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6690 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6691 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
| 6692 | if (!appctx->ctx.cli.p0) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6693 | 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] | 6694 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6695 | return 1; |
| 6696 | } |
| 6697 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6698 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 6699 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6700 | } |
| 6701 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6702 | static int cli_parse_set_tlskeys(char **args, struct appctx *appctx, void *private) |
| 6703 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6704 | struct tls_keys_ref *ref; |
| 6705 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6706 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 6707 | if (!*args[3] || !*args[4]) { |
| 6708 | 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] | 6709 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6710 | return 1; |
| 6711 | } |
| 6712 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6713 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 6714 | if (!ref) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6715 | 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] | 6716 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6717 | return 1; |
| 6718 | } |
| 6719 | |
| 6720 | trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size); |
| 6721 | if (trash.len != sizeof(struct tls_sess_key)) { |
| 6722 | 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] | 6723 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6724 | return 1; |
| 6725 | } |
| 6726 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6727 | memcpy(ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len); |
| 6728 | 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] | 6729 | |
| 6730 | appctx->ctx.cli.msg = "TLS ticket key updated!"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 6731 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6732 | return 1; |
| 6733 | |
| 6734 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6735 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6736 | |
| 6737 | static int cli_parse_set_ocspresponse(char **args, struct appctx *appctx, void *private) |
| 6738 | { |
| 6739 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 6740 | char *err = NULL; |
| 6741 | |
| 6742 | /* Expect one parameter: the new response in base64 encoding */ |
| 6743 | if (!*args[3]) { |
| 6744 | 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] | 6745 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6746 | return 1; |
| 6747 | } |
| 6748 | |
| 6749 | trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size); |
| 6750 | if (trash.len < 0) { |
| 6751 | 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] | 6752 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6753 | return 1; |
| 6754 | } |
| 6755 | |
| 6756 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
| 6757 | if (err) { |
| 6758 | memprintf(&err, "%s.\n", err); |
| 6759 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 6760 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6761 | } |
| 6762 | return 1; |
| 6763 | } |
| 6764 | appctx->ctx.cli.msg = "OCSP Response updated!"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 6765 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6766 | return 1; |
| 6767 | #else |
| 6768 | 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] | 6769 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6770 | return 1; |
| 6771 | #endif |
| 6772 | |
| 6773 | } |
| 6774 | |
| 6775 | /* register cli keywords */ |
| 6776 | static struct cli_kw_list cli_kws = {{ },{ |
| 6777 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6778 | { { "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 }, |
| 6779 | { { "set", "ssl", "tls-keys", NULL }, NULL, cli_parse_set_tlskeys, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6780 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6781 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6782 | { { NULL }, NULL, NULL, NULL } |
| 6783 | }}; |
| 6784 | |
| 6785 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6786 | /* Note: must not be declared <const> as its list will be overwritten. |
| 6787 | * Please take care of keeping this list alphabetically sorted. |
| 6788 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 6789 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 6790 | { "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] | 6791 | { "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] | 6792 | { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 6793 | { "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] | 6794 | { "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] | 6795 | { "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] | 6796 | { "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] | 6797 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 6798 | { "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] | 6799 | { "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] | 6800 | { "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] | 6801 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 6802 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 6803 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 6804 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 6805 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 6806 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 6807 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 6808 | { "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] | 6809 | { "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] | 6810 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 6811 | { "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] | 6812 | { "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] | 6813 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 6814 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 6815 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 6816 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 6817 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 6818 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 6819 | { "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] | 6820 | { "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] | 6821 | { "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] | 6822 | { "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] | 6823 | { "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] | 6824 | { "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] | 6825 | { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 6826 | { "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] | 6827 | { "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] | 6828 | #ifdef OPENSSL_NPN_NEGOTIATED |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6829 | { "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] | 6830 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6831 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6832 | { "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] | 6833 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6834 | { "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] | 6835 | { "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] | 6836 | { "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] | 6837 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 6838 | { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6839 | { NULL, NULL, 0, 0, 0 }, |
| 6840 | }}; |
| 6841 | |
| 6842 | /* Note: must not be declared <const> as its list will be overwritten. |
| 6843 | * Please take care of keeping this list alphabetically sorted. |
| 6844 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 6845 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 6846 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 6847 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 6848 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6849 | }}; |
| 6850 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6851 | /* Note: must not be declared <const> as its list will be overwritten. |
| 6852 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 6853 | * all code contributors. |
| 6854 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 6855 | * the config parser can report an appropriate error when a known keyword was |
| 6856 | * not enabled. |
| 6857 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6858 | static struct ssl_bind_kw ssl_bind_kws[] = { |
| 6859 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 6860 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 6861 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 6862 | { "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] | 6863 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6864 | { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 6865 | { "force-sslv3", ssl_bind_parse_force_sslv3, 0 }, /* force SSLv3 */ |
| 6866 | { "force-tlsv10", ssl_bind_parse_force_tlsv10, 0 }, /* force TLSv10 */ |
| 6867 | { "force-tlsv11", ssl_bind_parse_force_tlsv11, 0 }, /* force TLSv11 */ |
| 6868 | { "force-tlsv12", ssl_bind_parse_force_tlsv12, 0 }, /* force TLSv12 */ |
| 6869 | { "no-sslv3", ssl_bind_parse_no_sslv3, 0 }, /* disable SSLv3 */ |
| 6870 | { "no-tlsv10", ssl_bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */ |
| 6871 | { "no-tlsv11", ssl_bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */ |
| 6872 | { "no-tlsv12", ssl_bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */ |
| 6873 | { "no-tls-tickets", ssl_bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 6874 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 6875 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 6876 | { NULL, NULL, 0 }, |
| 6877 | }; |
| 6878 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 6879 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6880 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 6881 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 6882 | { "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] | 6883 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 6884 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6885 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 6886 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 6887 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 6888 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 6889 | { "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] | 6890 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6891 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 6892 | { "force-sslv3", bind_parse_force_sslv3, 0 }, /* force SSLv3 */ |
| 6893 | { "force-tlsv10", bind_parse_force_tlsv10, 0 }, /* force TLSv10 */ |
| 6894 | { "force-tlsv11", bind_parse_force_tlsv11, 0 }, /* force TLSv11 */ |
| 6895 | { "force-tlsv12", bind_parse_force_tlsv12, 0 }, /* force TLSv12 */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6896 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6897 | { "no-sslv3", bind_parse_no_sslv3, 0 }, /* disable SSLv3 */ |
| 6898 | { "no-tlsv10", bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */ |
| 6899 | { "no-tlsv11", bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */ |
| 6900 | { "no-tlsv12", bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */ |
| 6901 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 6902 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
| 6903 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 6904 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 6905 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 6906 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6907 | { NULL, NULL, 0 }, |
| 6908 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6909 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6910 | /* Note: must not be declared <const> as its list will be overwritten. |
| 6911 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 6912 | * all code contributors. |
| 6913 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 6914 | * the config parser can report an appropriate error when a known keyword was |
| 6915 | * not enabled. |
| 6916 | */ |
| 6917 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6918 | { "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] | 6919 | { "check-ssl", srv_parse_check_ssl, 0, 0 }, /* enable SSL for health checks */ |
| 6920 | { "ciphers", srv_parse_ciphers, 1, 0 }, /* select the cipher suite */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6921 | { "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] | 6922 | { "crt", srv_parse_crt, 1, 0 }, /* set client certificate */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 6923 | { "force-sslv3", srv_parse_force_sslv3, 0, 0 }, /* force SSLv3 */ |
| 6924 | { "force-tlsv10", srv_parse_force_tlsv10, 0, 0 }, /* force TLSv10 */ |
| 6925 | { "force-tlsv11", srv_parse_force_tlsv11, 0, 0 }, /* force TLSv11 */ |
| 6926 | { "force-tlsv12", srv_parse_force_tlsv12, 0, 0 }, /* force TLSv12 */ |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 6927 | { "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] | 6928 | { "no-sslv3", srv_parse_no_sslv3, 0, 0 }, /* disable SSLv3 */ |
| 6929 | { "no-tlsv10", srv_parse_no_tlsv10, 0, 0 }, /* disable TLSv10 */ |
| 6930 | { "no-tlsv11", srv_parse_no_tlsv11, 0, 0 }, /* disable TLSv11 */ |
| 6931 | { "no-tlsv12", srv_parse_no_tlsv12, 0, 0 }, /* disable TLSv12 */ |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 6932 | { "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] | 6933 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 0 }, /* send PROXY protocol header v2 with SSL info */ |
| 6934 | { "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] | 6935 | { "sni", srv_parse_sni, 1, 0 }, /* send SNI extension */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 6936 | { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6937 | { "verify", srv_parse_verify, 1, 0 }, /* set SSL verify method */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6938 | { "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] | 6939 | { NULL, NULL, 0, 0 }, |
| 6940 | }}; |
| 6941 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6942 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 6943 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 6944 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 6945 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6946 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 6947 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 6948 | #ifndef OPENSSL_NO_DH |
| 6949 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 6950 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6951 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 6952 | #ifndef OPENSSL_NO_DH |
| 6953 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 6954 | #endif |
| 6955 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 6956 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 6957 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 6958 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 6959 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 6960 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6961 | { 0, NULL, NULL }, |
| 6962 | }}; |
| 6963 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 6964 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 6965 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6966 | .snd_buf = ssl_sock_from_buf, |
| 6967 | .rcv_buf = ssl_sock_to_buf, |
| 6968 | .rcv_pipe = NULL, |
| 6969 | .snd_pipe = NULL, |
| 6970 | .shutr = NULL, |
| 6971 | .shutw = ssl_sock_shutw, |
| 6972 | .close = ssl_sock_close, |
| 6973 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6974 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6975 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 6976 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 6977 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 6978 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6979 | }; |
| 6980 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 6981 | #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] | 6982 | |
| 6983 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6984 | { |
| 6985 | if (ptr) { |
| 6986 | chunk_destroy(ptr); |
| 6987 | free(ptr); |
| 6988 | } |
| 6989 | } |
| 6990 | |
| 6991 | #endif |
| 6992 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6993 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6994 | static void __ssl_sock_init(void) |
| 6995 | { |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6996 | char *ptr; |
| 6997 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6998 | STACK_OF(SSL_COMP)* cm; |
| 6999 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7000 | if (global_ssl.listen_default_ciphers) |
| 7001 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 7002 | if (global_ssl.connect_default_ciphers) |
| 7003 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 7004 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 7005 | xprt_register(XPRT_SSL, &ssl_sock); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7006 | SSL_library_init(); |
| 7007 | cm = SSL_COMP_get_compression_methods(); |
| 7008 | sk_SSL_COMP_zero(cm); |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 7009 | #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] | 7010 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 7011 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7012 | sample_register_fetches(&sample_fetch_keywords); |
| 7013 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7014 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7015 | srv_register_keywords(&srv_kws); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7016 | cfg_register_keywords(&cfg_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7017 | cli_register_kw(&cli_kws); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 7018 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7019 | hap_register_post_check(tlskeys_finalize_config); |
| 7020 | #endif |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 7021 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7022 | ptr = NULL; |
| 7023 | memprintf(&ptr, "Built with OpenSSL version : " |
| 7024 | #ifdef OPENSSL_IS_BORINGSSL |
| 7025 | "BoringSSL\n"); |
| 7026 | #else /* OPENSSL_IS_BORINGSSL */ |
| 7027 | OPENSSL_VERSION_TEXT |
| 7028 | "\nRunning on OpenSSL version : %s%s", |
| 7029 | SSLeay_version(SSLEAY_VERSION), |
| 7030 | ((OPENSSL_VERSION_NUMBER ^ SSLeay()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
| 7031 | #endif |
| 7032 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
| 7033 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 7034 | "no (library version too old)" |
| 7035 | #elif defined(OPENSSL_NO_TLSEXT) |
| 7036 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 7037 | #else |
| 7038 | "yes" |
| 7039 | #endif |
| 7040 | "", ptr); |
| 7041 | |
| 7042 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 7043 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7044 | "yes" |
| 7045 | #else |
| 7046 | #ifdef OPENSSL_NO_TLSEXT |
| 7047 | "no (because of OPENSSL_NO_TLSEXT)" |
| 7048 | #else |
| 7049 | "no (version might be too old, 0.9.8f min needed)" |
| 7050 | #endif |
| 7051 | #endif |
| 7052 | "", ptr); |
| 7053 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7054 | hap_register_build_opts(ptr, 1); |
| 7055 | |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 7056 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 7057 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 7058 | |
| 7059 | #ifndef OPENSSL_NO_DH |
| 7060 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 7061 | #endif |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7062 | |
| 7063 | /* Load SSL string for the verbose & debug mode. */ |
| 7064 | ERR_load_SSL_strings(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7065 | } |
| 7066 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7067 | __attribute__((destructor)) |
| 7068 | static void __ssl_sock_deinit(void) |
| 7069 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7070 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7071 | lru64_destroy(ssl_ctx_lru_tree); |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 7072 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7073 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7074 | #ifndef OPENSSL_NO_DH |
| 7075 | if (local_dh_1024) { |
| 7076 | DH_free(local_dh_1024); |
| 7077 | local_dh_1024 = NULL; |
| 7078 | } |
| 7079 | |
| 7080 | if (local_dh_2048) { |
| 7081 | DH_free(local_dh_2048); |
| 7082 | local_dh_2048 = NULL; |
| 7083 | } |
| 7084 | |
| 7085 | if (local_dh_4096) { |
| 7086 | DH_free(local_dh_4096); |
| 7087 | local_dh_4096 = NULL; |
| 7088 | } |
| 7089 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 7090 | if (global_dh) { |
| 7091 | DH_free(global_dh); |
| 7092 | global_dh = NULL; |
| 7093 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7094 | #endif |
| 7095 | |
| 7096 | ERR_remove_state(0); |
| 7097 | ERR_free_strings(); |
| 7098 | |
| 7099 | EVP_cleanup(); |
| 7100 | |
| 7101 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 7102 | CRYPTO_cleanup_all_ex_data(); |
| 7103 | #endif |
| 7104 | } |
| 7105 | |
| 7106 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7107 | /* |
| 7108 | * Local variables: |
| 7109 | * c-indent-level: 8 |
| 7110 | * c-basic-offset: 8 |
| 7111 | * End: |
| 7112 | */ |