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 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame^] | 1438 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 1439 | { |
| 1440 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 1441 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 1442 | SSL_set_SSL_CTX(ssl, ctx); |
| 1443 | } |
| 1444 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1445 | #ifdef OPENSSL_IS_BORINGSSL |
| 1446 | |
| 1447 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 1448 | { |
| 1449 | (void)al; /* shut gcc stupid warning */ |
| 1450 | (void)priv; |
| 1451 | |
| 1452 | if (!SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name)) |
| 1453 | return SSL_TLSEXT_ERR_NOACK; |
| 1454 | return SSL_TLSEXT_ERR_OK; |
| 1455 | } |
| 1456 | |
| 1457 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 1458 | { |
| 1459 | struct connection *conn; |
| 1460 | struct bind_conf *s; |
| 1461 | const uint8_t *extension_data; |
| 1462 | size_t extension_len; |
| 1463 | CBS extension, cipher_suites, server_name_list, host_name, sig_algs; |
| 1464 | const SSL_CIPHER *cipher; |
| 1465 | uint16_t cipher_suite; |
| 1466 | uint8_t name_type, hash, sign; |
| 1467 | int has_rsa = 0, has_ecdsa = 0, has_ecdsa_sig = 0; |
| 1468 | |
| 1469 | char *wildp = NULL; |
| 1470 | const uint8_t *servername; |
| 1471 | struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL; |
| 1472 | int i; |
| 1473 | |
| 1474 | conn = SSL_get_app_data(ctx->ssl); |
| 1475 | s = objt_listener(conn->target)->bind_conf; |
| 1476 | |
| 1477 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 1478 | &extension_data, &extension_len)) { |
| 1479 | CBS_init(&extension, extension_data, extension_len); |
| 1480 | |
| 1481 | if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) |
| 1482 | || !CBS_get_u8(&server_name_list, &name_type) |
| 1483 | /* Although the server_name extension was intended to be extensible to |
| 1484 | * new name types and multiple names, OpenSSL 1.0.x had a bug which meant |
| 1485 | * different name types will cause an error. Further, RFC 4366 originally |
| 1486 | * defined syntax inextensibly. RFC 6066 corrected this mistake, but |
| 1487 | * adding new name types is no longer feasible. |
| 1488 | * |
| 1489 | * Act as if the extensibility does not exist to simplify parsing. */ |
| 1490 | || !CBS_get_u16_length_prefixed(&server_name_list, &host_name) |
| 1491 | || CBS_len(&server_name_list) != 0 |
| 1492 | || CBS_len(&extension) != 0 |
| 1493 | || name_type != TLSEXT_NAMETYPE_host_name |
| 1494 | || CBS_len(&host_name) == 0 |
| 1495 | || CBS_len(&host_name) > TLSEXT_MAXLEN_host_name |
| 1496 | || CBS_contains_zero_byte(&host_name)) { |
| 1497 | goto abort; |
| 1498 | } |
| 1499 | } else { |
| 1500 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
| 1501 | if (!s->strict_sni) |
| 1502 | return 1; |
| 1503 | goto abort; |
| 1504 | } |
| 1505 | |
| 1506 | /* extract/check clientHello informations */ |
| 1507 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 1508 | CBS_init(&extension, extension_data, extension_len); |
| 1509 | |
| 1510 | if (!CBS_get_u16_length_prefixed(&extension, &sig_algs) |
| 1511 | || CBS_len(&sig_algs) == 0 |
| 1512 | || CBS_len(&extension) != 0) { |
| 1513 | goto abort; |
| 1514 | } |
| 1515 | if (CBS_len(&sig_algs) % 2 != 0) { |
| 1516 | goto abort; |
| 1517 | } |
| 1518 | while (CBS_len(&sig_algs) != 0) { |
| 1519 | if (!CBS_get_u8(&sig_algs, &hash) |
| 1520 | || !CBS_get_u8(&sig_algs, &sign)) { |
| 1521 | goto abort; |
| 1522 | } |
| 1523 | switch (sign) { |
| 1524 | case TLSEXT_signature_rsa: |
| 1525 | has_rsa = 1; |
| 1526 | break; |
| 1527 | case TLSEXT_signature_ecdsa: |
| 1528 | has_ecdsa_sig = 1; |
| 1529 | break; |
| 1530 | default: |
| 1531 | continue; |
| 1532 | } |
| 1533 | if (has_ecdsa_sig && has_rsa) |
| 1534 | break; |
| 1535 | } |
| 1536 | } else { |
| 1537 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLS 1.2) */ |
| 1538 | has_rsa = 1; |
| 1539 | } |
| 1540 | if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */ |
| 1541 | CBS_init(&cipher_suites, ctx->cipher_suites, ctx->cipher_suites_len); |
| 1542 | |
| 1543 | while (CBS_len(&cipher_suites) != 0) { |
| 1544 | if (!CBS_get_u16(&cipher_suites, &cipher_suite)) { |
| 1545 | goto abort; |
| 1546 | } |
| 1547 | cipher = SSL_get_cipher_by_value(cipher_suite); |
| 1548 | if (cipher && SSL_CIPHER_is_ECDSA(cipher)) { |
| 1549 | has_ecdsa = 1; |
| 1550 | break; |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | servername = CBS_data(&host_name); |
| 1556 | for (i = 0; i < trash.size && i < CBS_len(&host_name); i++) { |
| 1557 | trash.str[i] = tolower(servername[i]); |
| 1558 | if (!wildp && (trash.str[i] == '.')) |
| 1559 | wildp = &trash.str[i]; |
| 1560 | } |
| 1561 | trash.str[i] = 0; |
| 1562 | |
| 1563 | /* lookup in full qualified names */ |
| 1564 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 1565 | |
| 1566 | /* lookup a not neg filter */ |
| 1567 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 1568 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 1569 | switch(container_of(n, struct sni_ctx, name)->key_sig) { |
| 1570 | case TLSEXT_signature_ecdsa: |
| 1571 | if (has_ecdsa) { |
| 1572 | node_ecdsa = n; |
| 1573 | goto find_one; |
| 1574 | } |
| 1575 | break; |
| 1576 | case TLSEXT_signature_rsa: |
| 1577 | if (has_rsa && !node_rsa) { |
| 1578 | node_rsa = n; |
| 1579 | if (!has_ecdsa) |
| 1580 | goto find_one; |
| 1581 | } |
| 1582 | break; |
| 1583 | default: /* TLSEXT_signature_anonymous */ |
| 1584 | if (!node_anonymous) |
| 1585 | node_anonymous = n; |
| 1586 | break; |
| 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 | if (wildp) { |
| 1591 | /* lookup in wildcards names */ |
| 1592 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 1593 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 1594 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 1595 | switch(container_of(n, struct sni_ctx, name)->key_sig) { |
| 1596 | case TLSEXT_signature_ecdsa: |
| 1597 | if (has_ecdsa) { |
| 1598 | node_ecdsa = n; |
| 1599 | goto find_one; |
| 1600 | } |
| 1601 | break; |
| 1602 | case TLSEXT_signature_rsa: |
| 1603 | if (has_rsa && !node_rsa) { |
| 1604 | node_rsa = n; |
| 1605 | if (!has_ecdsa) |
| 1606 | goto find_one; |
| 1607 | } |
| 1608 | break; |
| 1609 | default: /* TLSEXT_signature_anonymous */ |
| 1610 | if (!node_anonymous) |
| 1611 | node_anonymous = n; |
| 1612 | break; |
| 1613 | } |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | find_one: |
| 1618 | /* select by key_signature priority order */ |
| 1619 | node = node_ecdsa ? node_ecdsa : (node_rsa ? node_rsa : node_anonymous); |
| 1620 | |
| 1621 | if (node) { |
| 1622 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame^] | 1623 | ssl_sock_switchctx_set(ctx->ssl, container_of(node, struct sni_ctx, name)->ctx); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1624 | return 1; |
| 1625 | } |
| 1626 | if (!s->strict_sni) |
| 1627 | /* no certificate match, is the default_ctx */ |
| 1628 | /* the client will alert (was SSL_TLSEXT_ERR_ALERT_WARNING, ignored by Boring) */ |
| 1629 | return 1; |
| 1630 | abort: |
| 1631 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 1632 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 1633 | return -1; |
| 1634 | } |
| 1635 | |
| 1636 | #else /* OPENSSL_IS_BORINGSSL */ |
| 1637 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1638 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 1639 | * warning when no match is found, which implies the default (first) cert |
| 1640 | * will keep being used. |
| 1641 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1642 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1643 | { |
| 1644 | const char *servername; |
| 1645 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1646 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1647 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1648 | int i; |
| 1649 | (void)al; /* shut gcc stupid warning */ |
| 1650 | |
| 1651 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1652 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1653 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1654 | if (s->generate_certs) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1655 | struct connection *conn = SSL_get_app_data(ssl); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1656 | unsigned int key; |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1657 | SSL_CTX *ctx; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1658 | |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1659 | conn_get_to_addr(conn); |
| 1660 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1661 | key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to)); |
| 1662 | ctx = ssl_sock_get_generated_cert(key, s); |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1663 | if (ctx) { |
| 1664 | /* switch ctx */ |
| 1665 | SSL_set_SSL_CTX(ssl, ctx); |
| 1666 | return SSL_TLSEXT_ERR_OK; |
| 1667 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1668 | } |
| 1669 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1670 | #endif |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1671 | return (s->strict_sni ? |
| 1672 | SSL_TLSEXT_ERR_ALERT_FATAL : |
Emmanuel Hocdet | 79274e2 | 2013-05-31 12:47:44 +0200 | [diff] [blame] | 1673 | SSL_TLSEXT_ERR_NOACK); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1674 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1675 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1676 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1677 | if (!servername[i]) |
| 1678 | break; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1679 | trash.str[i] = tolower(servername[i]); |
| 1680 | if (!wildp && (trash.str[i] == '.')) |
| 1681 | wildp = &trash.str[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1682 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1683 | trash.str[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1684 | |
| 1685 | /* lookup in full qualified names */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1686 | node = ebst_lookup(&s->sni_ctx, trash.str); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1687 | |
| 1688 | /* lookup a not neg filter */ |
| 1689 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 1690 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 1691 | node = n; |
| 1692 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1693 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1694 | } |
| 1695 | if (!node && wildp) { |
| 1696 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1697 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1698 | } |
| 1699 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1700 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1701 | SSL_CTX *ctx; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1702 | if (s->generate_certs && |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1703 | (ctx = ssl_sock_generate_certificate(servername, s, ssl))) { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1704 | /* switch ctx */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1705 | return SSL_TLSEXT_ERR_OK; |
| 1706 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1707 | #endif |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1708 | return (s->strict_sni ? |
| 1709 | SSL_TLSEXT_ERR_ALERT_FATAL : |
| 1710 | SSL_TLSEXT_ERR_ALERT_WARNING); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame^] | 1714 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1715 | return SSL_TLSEXT_ERR_OK; |
| 1716 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1717 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1718 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 1719 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1720 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1721 | |
| 1722 | static DH * ssl_get_dh_1024(void) |
| 1723 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1724 | static unsigned char dh1024_p[]={ |
| 1725 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 1726 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 1727 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 1728 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 1729 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 1730 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 1731 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 1732 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 1733 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 1734 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 1735 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 1736 | }; |
| 1737 | static unsigned char dh1024_g[]={ |
| 1738 | 0x02, |
| 1739 | }; |
| 1740 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1741 | BIGNUM *p; |
| 1742 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1743 | DH *dh = DH_new(); |
| 1744 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1745 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 1746 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1747 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1748 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1749 | DH_free(dh); |
| 1750 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1751 | } else { |
| 1752 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | return dh; |
| 1756 | } |
| 1757 | |
| 1758 | static DH *ssl_get_dh_2048(void) |
| 1759 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1760 | static unsigned char dh2048_p[]={ |
| 1761 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 1762 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 1763 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 1764 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 1765 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 1766 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 1767 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 1768 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 1769 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 1770 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 1771 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 1772 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 1773 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 1774 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 1775 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 1776 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 1777 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 1778 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 1779 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 1780 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 1781 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 1782 | 0xB7,0x1F,0x77,0xF3, |
| 1783 | }; |
| 1784 | static unsigned char dh2048_g[]={ |
| 1785 | 0x02, |
| 1786 | }; |
| 1787 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1788 | BIGNUM *p; |
| 1789 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1790 | DH *dh = DH_new(); |
| 1791 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1792 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 1793 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1794 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1795 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1796 | DH_free(dh); |
| 1797 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1798 | } else { |
| 1799 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1800 | } |
| 1801 | } |
| 1802 | return dh; |
| 1803 | } |
| 1804 | |
| 1805 | static DH *ssl_get_dh_4096(void) |
| 1806 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1807 | static unsigned char dh4096_p[]={ |
| 1808 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 1809 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 1810 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 1811 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 1812 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 1813 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 1814 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 1815 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 1816 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 1817 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 1818 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 1819 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 1820 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 1821 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 1822 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 1823 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 1824 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 1825 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 1826 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 1827 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 1828 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 1829 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 1830 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 1831 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 1832 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 1833 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 1834 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 1835 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 1836 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 1837 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 1838 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 1839 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 1840 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 1841 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 1842 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 1843 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 1844 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 1845 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 1846 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 1847 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 1848 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 1849 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 1850 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1851 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1852 | static unsigned char dh4096_g[]={ |
| 1853 | 0x02, |
| 1854 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1855 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1856 | BIGNUM *p; |
| 1857 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1858 | DH *dh = DH_new(); |
| 1859 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1860 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 1861 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1862 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1863 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1864 | DH_free(dh); |
| 1865 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1866 | } else { |
| 1867 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | return dh; |
| 1871 | } |
| 1872 | |
| 1873 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 1874 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1875 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 1876 | { |
| 1877 | DH *dh = NULL; |
| 1878 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1879 | int type; |
| 1880 | |
| 1881 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1882 | |
| 1883 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 1884 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 1885 | */ |
| 1886 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 1887 | keylen = EVP_PKEY_bits(pkey); |
| 1888 | } |
| 1889 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 1890 | if (keylen > global_ssl.default_dh_param) { |
| 1891 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1892 | } |
| 1893 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1894 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1895 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1896 | } |
| 1897 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1898 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1899 | } |
| 1900 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1901 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | return dh; |
| 1905 | } |
| 1906 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1907 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1908 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1909 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1910 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1911 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1912 | if (in == NULL) |
| 1913 | goto end; |
| 1914 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1915 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1916 | goto end; |
| 1917 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1918 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 1919 | |
| 1920 | end: |
| 1921 | if (in) |
| 1922 | BIO_free(in); |
| 1923 | |
| 1924 | return dh; |
| 1925 | } |
| 1926 | |
| 1927 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 1928 | { |
| 1929 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 1930 | |
| 1931 | if (global_dh) { |
| 1932 | return 0; |
| 1933 | } |
| 1934 | |
| 1935 | return -1; |
| 1936 | } |
| 1937 | |
| 1938 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 1939 | if an error occured, and 0 if parameter not found. */ |
| 1940 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 1941 | { |
| 1942 | int ret = -1; |
| 1943 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 1944 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1945 | if (dh) { |
| 1946 | ret = 1; |
| 1947 | SSL_CTX_set_tmp_dh(ctx, dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 1948 | |
| 1949 | if (ssl_dh_ptr_index >= 0) { |
| 1950 | /* store a pointer to the DH params to avoid complaining about |
| 1951 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 1952 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 1953 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1954 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1955 | else if (global_dh) { |
| 1956 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 1957 | ret = 0; /* DH params not found */ |
| 1958 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1959 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 1960 | /* Clear openssl global errors stack */ |
| 1961 | ERR_clear_error(); |
| 1962 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 1963 | if (global_ssl.default_dh_param <= 1024) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1964 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 1965 | if (local_dh_1024 == NULL) |
| 1966 | local_dh_1024 = ssl_get_dh_1024(); |
| 1967 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1968 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1969 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 1970 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1971 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1972 | } |
| 1973 | else { |
| 1974 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 1975 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 1976 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 1977 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1978 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1979 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1980 | end: |
| 1981 | if (dh) |
| 1982 | DH_free(dh); |
| 1983 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1984 | return ret; |
| 1985 | } |
| 1986 | #endif |
| 1987 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 1988 | static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, struct ssl_bind_conf *conf, |
| 1989 | uint8_t key_sig, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1990 | { |
| 1991 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1992 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 1993 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1994 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1995 | if (*name == '!') { |
| 1996 | neg = 1; |
| 1997 | name++; |
| 1998 | } |
| 1999 | if (*name == '*') { |
| 2000 | wild = 1; |
| 2001 | name++; |
| 2002 | } |
| 2003 | /* !* filter is a nop */ |
| 2004 | if (neg && wild) |
| 2005 | return order; |
| 2006 | if (*name) { |
| 2007 | int j, len; |
| 2008 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2009 | for (j = 0; j < len && j < trash.size; j++) |
| 2010 | trash.str[j] = tolower(name[j]); |
| 2011 | if (j >= trash.size) |
| 2012 | return order; |
| 2013 | trash.str[j] = 0; |
| 2014 | |
| 2015 | /* Check for duplicates. */ |
| 2016 | if (wild) |
| 2017 | node = ebst_lookup(&s->sni_w_ctx, trash.str); |
| 2018 | else |
| 2019 | node = ebst_lookup(&s->sni_ctx, trash.str); |
| 2020 | for (; node; node = ebmb_next_dup(node)) { |
| 2021 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2022 | if (sc->ctx == ctx && sc->conf == conf && |
| 2023 | sc->key_sig == key_sig && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2024 | return order; |
| 2025 | } |
| 2026 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2027 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2028 | if (!sc) |
| 2029 | return order; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2030 | memcpy(sc->name.key, trash.str, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2031 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2032 | sc->conf = conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2033 | sc->key_sig = key_sig; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2034 | sc->order = order++; |
| 2035 | sc->neg = neg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2036 | if (wild) |
| 2037 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 2038 | else |
| 2039 | ebst_insert(&s->sni_ctx, &sc->name); |
| 2040 | } |
| 2041 | return order; |
| 2042 | } |
| 2043 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2044 | |
| 2045 | /* The following code is used for loading multiple crt files into |
| 2046 | * SSL_CTX's based on CN/SAN |
| 2047 | */ |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 2048 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(LIBRESSL_VERSION_NUMBER) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2049 | /* This is used to preload the certifcate, private key |
| 2050 | * and Cert Chain of a file passed in via the crt |
| 2051 | * argument |
| 2052 | * |
| 2053 | * This way, we do not have to read the file multiple times |
| 2054 | */ |
| 2055 | struct cert_key_and_chain { |
| 2056 | X509 *cert; |
| 2057 | EVP_PKEY *key; |
| 2058 | unsigned int num_chain_certs; |
| 2059 | /* This is an array of X509 pointers */ |
| 2060 | X509 **chain_certs; |
| 2061 | }; |
| 2062 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2063 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 2064 | |
| 2065 | struct key_combo_ctx { |
| 2066 | SSL_CTX *ctx; |
| 2067 | int order; |
| 2068 | }; |
| 2069 | |
| 2070 | /* Map used for processing multiple keypairs for a single purpose |
| 2071 | * |
| 2072 | * This maps CN/SNI name to certificate type |
| 2073 | */ |
| 2074 | struct sni_keytype { |
| 2075 | int keytypes; /* BITMASK for keytypes */ |
| 2076 | struct ebmb_node name; /* node holding the servername value */ |
| 2077 | }; |
| 2078 | |
| 2079 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2080 | /* Frees the contents of a cert_key_and_chain |
| 2081 | */ |
| 2082 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2083 | { |
| 2084 | int i; |
| 2085 | |
| 2086 | if (!ckch) |
| 2087 | return; |
| 2088 | |
| 2089 | /* Free the certificate and set pointer to NULL */ |
| 2090 | if (ckch->cert) |
| 2091 | X509_free(ckch->cert); |
| 2092 | ckch->cert = NULL; |
| 2093 | |
| 2094 | /* Free the key and set pointer to NULL */ |
| 2095 | if (ckch->key) |
| 2096 | EVP_PKEY_free(ckch->key); |
| 2097 | ckch->key = NULL; |
| 2098 | |
| 2099 | /* Free each certificate in the chain */ |
| 2100 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2101 | if (ckch->chain_certs[i]) |
| 2102 | X509_free(ckch->chain_certs[i]); |
| 2103 | } |
| 2104 | |
| 2105 | /* Free the chain obj itself and set to NULL */ |
| 2106 | if (ckch->num_chain_certs > 0) { |
| 2107 | free(ckch->chain_certs); |
| 2108 | ckch->num_chain_certs = 0; |
| 2109 | ckch->chain_certs = NULL; |
| 2110 | } |
| 2111 | |
| 2112 | } |
| 2113 | |
| 2114 | /* checks if a key and cert exists in the ckch |
| 2115 | */ |
| 2116 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 2117 | { |
| 2118 | return (ckch->cert != NULL && ckch->key != NULL); |
| 2119 | } |
| 2120 | |
| 2121 | |
| 2122 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 2123 | * This allows us to carry the contents of the file without having to |
| 2124 | * read the file multiple times. |
| 2125 | * |
| 2126 | * returns: |
| 2127 | * 0 on Success |
| 2128 | * 1 on SSL Failure |
| 2129 | * 2 on file not found |
| 2130 | */ |
| 2131 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 2132 | { |
| 2133 | |
| 2134 | BIO *in; |
| 2135 | X509 *ca = NULL; |
| 2136 | int ret = 1; |
| 2137 | |
| 2138 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2139 | |
| 2140 | in = BIO_new(BIO_s_file()); |
| 2141 | if (in == NULL) |
| 2142 | goto end; |
| 2143 | |
| 2144 | if (BIO_read_filename(in, path) <= 0) |
| 2145 | goto end; |
| 2146 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2147 | /* Read Private Key */ |
| 2148 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 2149 | if (ckch->key == NULL) { |
| 2150 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 2151 | err && *err ? *err : "", path); |
| 2152 | goto end; |
| 2153 | } |
| 2154 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2155 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 2156 | if (BIO_reset(in) == -1) { |
| 2157 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 2158 | err && *err ? *err : "", path); |
| 2159 | goto end; |
| 2160 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2161 | |
| 2162 | /* Read Certificate */ |
| 2163 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 2164 | if (ckch->cert == NULL) { |
| 2165 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 2166 | err && *err ? *err : "", path); |
| 2167 | goto end; |
| 2168 | } |
| 2169 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2170 | /* Read Certificate Chain */ |
| 2171 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 2172 | /* Grow the chain certs */ |
| 2173 | ckch->num_chain_certs++; |
| 2174 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 2175 | |
| 2176 | /* use - 1 here since we just incremented it above */ |
| 2177 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 2178 | } |
| 2179 | ret = ERR_get_error(); |
| 2180 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 2181 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 2182 | err && *err ? *err : "", path); |
| 2183 | ret = 1; |
| 2184 | goto end; |
| 2185 | } |
| 2186 | |
| 2187 | ret = 0; |
| 2188 | |
| 2189 | end: |
| 2190 | |
| 2191 | ERR_clear_error(); |
| 2192 | if (in) |
| 2193 | BIO_free(in); |
| 2194 | |
| 2195 | /* Something went wrong in one of the reads */ |
| 2196 | if (ret != 0) |
| 2197 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2198 | |
| 2199 | return ret; |
| 2200 | } |
| 2201 | |
| 2202 | /* Loads the info in ckch into ctx |
| 2203 | * Currently, this does not process any information about ocsp, dhparams or |
| 2204 | * sctl |
| 2205 | * Returns |
| 2206 | * 0 on success |
| 2207 | * 1 on failure |
| 2208 | */ |
| 2209 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2210 | { |
| 2211 | int i = 0; |
| 2212 | |
| 2213 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2214 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2215 | err && *err ? *err : "", path); |
| 2216 | return 1; |
| 2217 | } |
| 2218 | |
| 2219 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 2220 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 2221 | err && *err ? *err : "", path); |
| 2222 | return 1; |
| 2223 | } |
| 2224 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2225 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 2226 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2227 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2228 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 2229 | err && *err ? *err : "", (i+1), path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2230 | return 1; |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2235 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2236 | err && *err ? *err : "", path); |
| 2237 | return 1; |
| 2238 | } |
| 2239 | |
| 2240 | return 0; |
| 2241 | } |
| 2242 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2243 | |
| 2244 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 2245 | { |
| 2246 | struct sni_keytype *s_kt = NULL; |
| 2247 | struct ebmb_node *node; |
| 2248 | int i; |
| 2249 | |
| 2250 | for (i = 0; i < trash.size; i++) { |
| 2251 | if (!str[i]) |
| 2252 | break; |
| 2253 | trash.str[i] = tolower(str[i]); |
| 2254 | } |
| 2255 | trash.str[i] = 0; |
| 2256 | node = ebst_lookup(sni_keytypes, trash.str); |
| 2257 | if (!node) { |
| 2258 | /* CN not found in tree */ |
| 2259 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 2260 | /* Using memcpy here instead of strncpy. |
| 2261 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 2262 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 2263 | */ |
| 2264 | memcpy(s_kt->name.key, trash.str, i+1); |
| 2265 | s_kt->keytypes = 0; |
| 2266 | ebst_insert(sni_keytypes, &s_kt->name); |
| 2267 | } else { |
| 2268 | /* CN found in tree */ |
| 2269 | s_kt = container_of(node, struct sni_keytype, name); |
| 2270 | } |
| 2271 | |
| 2272 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 2273 | s_kt->keytypes |= 1<<key_index; |
| 2274 | |
| 2275 | } |
| 2276 | |
| 2277 | |
| 2278 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 2279 | * If any are found, group these files into a set of SSL_CTX* |
| 2280 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 2281 | * |
| 2282 | * This will allow the user to explictly group multiple cert/keys for a single purpose |
| 2283 | * |
| 2284 | * Returns |
| 2285 | * 0 on success |
| 2286 | * 1 on failure |
| 2287 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2288 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2289 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2290 | { |
| 2291 | char fp[MAXPATHLEN+1] = {0}; |
| 2292 | int n = 0; |
| 2293 | int i = 0; |
| 2294 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 2295 | struct eb_root sni_keytypes_map = { {0} }; |
| 2296 | struct ebmb_node *node; |
| 2297 | struct ebmb_node *next; |
| 2298 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 2299 | * of keytypes |
| 2300 | */ |
| 2301 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 2302 | int rv = 0; |
| 2303 | X509_NAME *xname = NULL; |
| 2304 | char *str = NULL; |
| 2305 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2306 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 2307 | #endif |
| 2308 | |
| 2309 | /* Load all possible certs and keys */ |
| 2310 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2311 | struct stat buf; |
| 2312 | |
| 2313 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2314 | if (stat(fp, &buf) == 0) { |
| 2315 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
| 2316 | rv = 1; |
| 2317 | goto end; |
| 2318 | } |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | /* Process each ckch and update keytypes for each CN/SAN |
| 2323 | * for example, if CN/SAN www.a.com is associated with |
| 2324 | * certs with keytype 0 and 2, then at the end of the loop, |
| 2325 | * www.a.com will have: |
| 2326 | * keyindex = 0 | 1 | 4 = 5 |
| 2327 | */ |
| 2328 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2329 | |
| 2330 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 2331 | continue; |
| 2332 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2333 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 2334 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2335 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 2336 | } else { |
| 2337 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 2338 | * so the line that contains logic is marked via comments |
| 2339 | */ |
| 2340 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 2341 | i = -1; |
| 2342 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 2343 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2344 | ASN1_STRING *value; |
| 2345 | value = X509_NAME_ENTRY_get_data(entry); |
| 2346 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2347 | /* Important line is here */ |
| 2348 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2349 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2350 | OPENSSL_free(str); |
| 2351 | str = NULL; |
| 2352 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2353 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2354 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2355 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2356 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2357 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 2358 | if (names) { |
| 2359 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 2360 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2361 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2362 | if (name->type == GEN_DNS) { |
| 2363 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 2364 | /* Important line is here */ |
| 2365 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2366 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2367 | OPENSSL_free(str); |
| 2368 | str = NULL; |
| 2369 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | } |
| 2374 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2375 | } |
| 2376 | |
| 2377 | /* If no files found, return error */ |
| 2378 | if (eb_is_empty(&sni_keytypes_map)) { |
| 2379 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 2380 | err && *err ? *err : "", path); |
| 2381 | rv = 1; |
| 2382 | goto end; |
| 2383 | } |
| 2384 | |
| 2385 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 2386 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 2387 | * and add each CTX to the SNI tree |
| 2388 | * |
| 2389 | * Some math here: |
| 2390 | * There are 2^n - 1 possibile combinations, each unique |
| 2391 | * combination is denoted by the key in the map. Each key |
| 2392 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 2393 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 2394 | * entry in the array to correspond to the unique combo (key) |
| 2395 | * associated with i. This unique key combo (i) will be associated |
| 2396 | * with combos[i-1] |
| 2397 | */ |
| 2398 | |
| 2399 | node = ebmb_first(&sni_keytypes_map); |
| 2400 | while (node) { |
| 2401 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2402 | char cur_file[MAXPATHLEN+1]; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2403 | |
| 2404 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 2405 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 2406 | cur_ctx = key_combos[i-1].ctx; |
| 2407 | |
| 2408 | if (cur_ctx == NULL) { |
| 2409 | /* need to create SSL_CTX */ |
| 2410 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
| 2411 | if (cur_ctx == NULL) { |
| 2412 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 2413 | err && *err ? *err : ""); |
| 2414 | rv = 1; |
| 2415 | goto end; |
| 2416 | } |
| 2417 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2418 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2419 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2420 | if (i & (1<<n)) { |
| 2421 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2422 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2423 | 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] | 2424 | SSL_CTX_free(cur_ctx); |
| 2425 | rv = 1; |
| 2426 | goto end; |
| 2427 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2428 | |
| 2429 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 2430 | /* Load OCSP Info into context */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 2431 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2432 | if (err) |
| 2433 | 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] | 2434 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2435 | SSL_CTX_free(cur_ctx); |
| 2436 | rv = 1; |
| 2437 | goto end; |
| 2438 | } |
| 2439 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | /* Load DH params into the ctx to support DHE keys */ |
| 2444 | #ifndef OPENSSL_NO_DH |
| 2445 | if (ssl_dh_ptr_index >= 0) |
| 2446 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 2447 | |
| 2448 | rv = ssl_sock_load_dh_params(cur_ctx, NULL); |
| 2449 | if (rv < 0) { |
| 2450 | if (err) |
| 2451 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 2452 | *err ? *err : "", path); |
| 2453 | rv = 1; |
| 2454 | goto end; |
| 2455 | } |
| 2456 | #endif |
| 2457 | |
| 2458 | /* Update key_combos */ |
| 2459 | key_combos[i-1].ctx = cur_ctx; |
| 2460 | } |
| 2461 | |
| 2462 | /* Update SNI Tree */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2463 | key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf, |
| 2464 | TLSEXT_signature_anonymous, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2465 | node = ebmb_next(node); |
| 2466 | } |
| 2467 | |
| 2468 | |
| 2469 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 2470 | if (!bind_conf->default_ctx) { |
| 2471 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 2472 | if (key_combos[i].ctx) { |
| 2473 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2474 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2475 | break; |
| 2476 | } |
| 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | end: |
| 2481 | |
| 2482 | if (names) |
| 2483 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 2484 | |
| 2485 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 2486 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 2487 | |
| 2488 | node = ebmb_first(&sni_keytypes_map); |
| 2489 | while (node) { |
| 2490 | next = ebmb_next(node); |
| 2491 | ebmb_delete(node); |
| 2492 | node = next; |
| 2493 | } |
| 2494 | |
| 2495 | return rv; |
| 2496 | } |
| 2497 | #else |
| 2498 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2499 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2500 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2501 | { |
| 2502 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 2503 | err && *err ? *err : "", path, strerror(errno)); |
| 2504 | return 1; |
| 2505 | } |
| 2506 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2507 | #endif /* #if OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
| 2508 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2509 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 2510 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 2511 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2512 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, |
| 2513 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2514 | { |
| 2515 | BIO *in; |
| 2516 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2517 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2518 | int ret = -1; |
| 2519 | int order = 0; |
| 2520 | X509_NAME *xname; |
| 2521 | char *str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2522 | pem_password_cb *passwd_cb; |
| 2523 | void *passwd_cb_userdata; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2524 | EVP_PKEY *pkey; |
| 2525 | uint8_t key_sig = TLSEXT_signature_anonymous; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2526 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2527 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2528 | STACK_OF(GENERAL_NAME) *names; |
| 2529 | #endif |
| 2530 | |
| 2531 | in = BIO_new(BIO_s_file()); |
| 2532 | if (in == NULL) |
| 2533 | goto end; |
| 2534 | |
| 2535 | if (BIO_read_filename(in, file) <= 0) |
| 2536 | goto end; |
| 2537 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2538 | |
| 2539 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 2540 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 2541 | |
| 2542 | 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] | 2543 | if (x == NULL) |
| 2544 | goto end; |
| 2545 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2546 | pkey = X509_get_pubkey(x); |
| 2547 | if (pkey) { |
| 2548 | switch(EVP_PKEY_base_id(pkey)) { |
| 2549 | case EVP_PKEY_RSA: |
| 2550 | key_sig = TLSEXT_signature_rsa; |
| 2551 | break; |
| 2552 | case EVP_PKEY_EC: |
| 2553 | key_sig = TLSEXT_signature_ecdsa; |
| 2554 | break; |
| 2555 | } |
| 2556 | EVP_PKEY_free(pkey); |
| 2557 | } |
| 2558 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2559 | if (fcount) { |
| 2560 | while (fcount--) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2561 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, key_sig, sni_filter[fcount], order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2562 | } |
| 2563 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2564 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2565 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 2566 | if (names) { |
| 2567 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 2568 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 2569 | if (name->type == GEN_DNS) { |
| 2570 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2571 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, key_sig, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2572 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2573 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2574 | } |
| 2575 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2576 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2577 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2578 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2579 | xname = X509_get_subject_name(x); |
| 2580 | i = -1; |
| 2581 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 2582 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2583 | ASN1_STRING *value; |
| 2584 | |
| 2585 | value = X509_NAME_ENTRY_get_data(entry); |
| 2586 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2587 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, key_sig, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2588 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2589 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 2594 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 2595 | goto end; |
| 2596 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2597 | #ifdef SSL_CTX_clear_extra_chain_certs |
| 2598 | SSL_CTX_clear_extra_chain_certs(ctx); |
| 2599 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2600 | if (ctx->extra_certs != NULL) { |
| 2601 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 2602 | ctx->extra_certs = NULL; |
| 2603 | } |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2604 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2605 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2606 | 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] | 2607 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 2608 | X509_free(ca); |
| 2609 | goto end; |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | err = ERR_get_error(); |
| 2614 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 2615 | /* we successfully reached the last cert in the file */ |
| 2616 | ret = 1; |
| 2617 | } |
| 2618 | ERR_clear_error(); |
| 2619 | |
| 2620 | end: |
| 2621 | if (x) |
| 2622 | X509_free(x); |
| 2623 | |
| 2624 | if (in) |
| 2625 | BIO_free(in); |
| 2626 | |
| 2627 | return ret; |
| 2628 | } |
| 2629 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2630 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 2631 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2632 | { |
| 2633 | int ret; |
| 2634 | SSL_CTX *ctx; |
| 2635 | |
| 2636 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 2637 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2638 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 2639 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2640 | return 1; |
| 2641 | } |
| 2642 | |
| 2643 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2644 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 2645 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2646 | SSL_CTX_free(ctx); |
| 2647 | return 1; |
| 2648 | } |
| 2649 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2650 | 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] | 2651 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2652 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 2653 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2654 | if (ret < 0) /* serious error, must do that ourselves */ |
| 2655 | SSL_CTX_free(ctx); |
| 2656 | return 1; |
| 2657 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 2658 | |
| 2659 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2660 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2661 | err && *err ? *err : "", path); |
| 2662 | return 1; |
| 2663 | } |
| 2664 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2665 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 2666 | * the tree, so it will be discovered and cleaned in time. |
| 2667 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2668 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2669 | /* store a NULL pointer to indicate we have not yet loaded |
| 2670 | a custom DH param file */ |
| 2671 | if (ssl_dh_ptr_index >= 0) { |
| 2672 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 2673 | } |
| 2674 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2675 | ret = ssl_sock_load_dh_params(ctx, path); |
| 2676 | if (ret < 0) { |
| 2677 | if (err) |
| 2678 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 2679 | *err ? *err : "", path); |
| 2680 | return 1; |
| 2681 | } |
| 2682 | #endif |
| 2683 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 2684 | #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] | 2685 | ret = ssl_sock_load_ocsp(ctx, path); |
| 2686 | if (ret < 0) { |
| 2687 | if (err) |
| 2688 | 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", |
| 2689 | *err ? *err : "", path); |
| 2690 | return 1; |
| 2691 | } |
| 2692 | #endif |
| 2693 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 2694 | #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] | 2695 | if (sctl_ex_index >= 0) { |
| 2696 | ret = ssl_sock_load_sctl(ctx, path); |
| 2697 | if (ret < 0) { |
| 2698 | if (err) |
| 2699 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 2700 | *err ? *err : "", path); |
| 2701 | return 1; |
| 2702 | } |
| 2703 | } |
| 2704 | #endif |
| 2705 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2706 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2707 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2708 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 2709 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2710 | return 1; |
| 2711 | } |
| 2712 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2713 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2714 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2715 | bind_conf->default_ssl_conf = ssl_conf; |
| 2716 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2717 | |
| 2718 | return 0; |
| 2719 | } |
| 2720 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 2721 | 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] | 2722 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 2723 | struct dirent **de_list; |
| 2724 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2725 | DIR *dir; |
| 2726 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 2727 | char *end; |
| 2728 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2729 | int cfgerr = 0; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 2730 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 2731 | int is_bundle; |
| 2732 | int j; |
| 2733 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2734 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2735 | if (stat(path, &buf) == 0) { |
| 2736 | dir = opendir(path); |
| 2737 | if (!dir) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2738 | 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] | 2739 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2740 | /* strip trailing slashes, including first one */ |
| 2741 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 2742 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2743 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2744 | n = scandir(path, &de_list, 0, alphasort); |
| 2745 | if (n < 0) { |
| 2746 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 2747 | err && *err ? *err : "", path, strerror(errno)); |
| 2748 | cfgerr++; |
| 2749 | } |
| 2750 | else { |
| 2751 | for (i = 0; i < n; i++) { |
| 2752 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 2753 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2754 | end = strrchr(de->d_name, '.'); |
| 2755 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 2756 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 2757 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2758 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 2759 | if (stat(fp, &buf) != 0) { |
| 2760 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 2761 | err && *err ? *err : "", fp, strerror(errno)); |
| 2762 | cfgerr++; |
| 2763 | goto ignore_entry; |
| 2764 | } |
| 2765 | if (!S_ISREG(buf.st_mode)) |
| 2766 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 2767 | |
| 2768 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 2769 | is_bundle = 0; |
| 2770 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 2771 | |
| 2772 | if (end) { |
| 2773 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 2774 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 2775 | is_bundle = 1; |
| 2776 | break; |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2780 | if (is_bundle) { |
| 2781 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 2782 | int dp_len; |
| 2783 | |
| 2784 | dp_len = end - de->d_name; |
| 2785 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 2786 | |
| 2787 | /* increment i and free de until we get to a non-bundle cert |
| 2788 | * Note here that we look at de_list[i + 1] before freeing de |
| 2789 | * this is important since ignore_entry will free de |
| 2790 | */ |
| 2791 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 2792 | free(de); |
| 2793 | i++; |
| 2794 | de = de_list[i]; |
| 2795 | } |
| 2796 | |
| 2797 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2798 | ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 2799 | |
| 2800 | /* Successfully processed the bundle */ |
| 2801 | goto ignore_entry; |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2806 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2807 | ignore_entry: |
| 2808 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 2809 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2810 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2811 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2812 | closedir(dir); |
| 2813 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2814 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2815 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2816 | cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2817 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2818 | return cfgerr; |
| 2819 | } |
| 2820 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 2821 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 2822 | * done once. Zero is returned if the operation fails. No error is returned |
| 2823 | * if the random is said as not implemented, because we expect that openssl |
| 2824 | * will use another method once needed. |
| 2825 | */ |
| 2826 | static int ssl_initialize_random() |
| 2827 | { |
| 2828 | unsigned char random; |
| 2829 | static int random_initialized = 0; |
| 2830 | |
| 2831 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 2832 | random_initialized = 1; |
| 2833 | |
| 2834 | return random_initialized; |
| 2835 | } |
| 2836 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2837 | /* release ssl bind conf */ |
| 2838 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2839 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2840 | if (conf) { |
| 2841 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 2842 | free(conf->npn_str); |
| 2843 | conf->npn_str = NULL; |
| 2844 | #endif |
| 2845 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 2846 | free(conf->alpn_str); |
| 2847 | conf->alpn_str = NULL; |
| 2848 | #endif |
| 2849 | free(conf->ca_file); |
| 2850 | conf->ca_file = NULL; |
| 2851 | free(conf->crl_file); |
| 2852 | conf->crl_file = NULL; |
| 2853 | free(conf->ciphers); |
| 2854 | conf->ciphers = NULL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 2855 | free(conf->curves); |
| 2856 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2857 | free(conf->ecdhe); |
| 2858 | conf->ecdhe = NULL; |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 2863 | { |
| 2864 | char thisline[CRT_LINESIZE]; |
| 2865 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2866 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 2867 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2868 | int linenum = 0; |
| 2869 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2870 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2871 | if ((f = fopen(file, "r")) == NULL) { |
| 2872 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2873 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2874 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2875 | |
| 2876 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2877 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2878 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2879 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2880 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2881 | char *crt_path; |
| 2882 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2883 | |
| 2884 | linenum++; |
| 2885 | end = line + strlen(line); |
| 2886 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 2887 | /* Check if we reached the limit and the last char is not \n. |
| 2888 | * Watch out for the last line without the terminating '\n'! |
| 2889 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2890 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 2891 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2892 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2893 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2894 | } |
| 2895 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2896 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2897 | newarg = 1; |
| 2898 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2899 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 2900 | /* end of string, end of loop */ |
| 2901 | *line = 0; |
| 2902 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2903 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2904 | newarg = 1; |
| 2905 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2906 | } else if (*line == '[') { |
| 2907 | if (ssl_b) { |
| 2908 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 2909 | cfgerr = 1; |
| 2910 | break; |
| 2911 | } |
| 2912 | if (!arg) { |
| 2913 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 2914 | cfgerr = 1; |
| 2915 | break; |
| 2916 | } |
| 2917 | ssl_b = arg; |
| 2918 | newarg = 1; |
| 2919 | *line = 0; |
| 2920 | } else if (*line == ']') { |
| 2921 | if (ssl_e) { |
| 2922 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2923 | cfgerr = 1; |
| 2924 | break; |
| 2925 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2926 | if (!ssl_b) { |
| 2927 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 2928 | cfgerr = 1; |
| 2929 | break; |
| 2930 | } |
| 2931 | ssl_e = arg; |
| 2932 | newarg = 1; |
| 2933 | *line = 0; |
| 2934 | } else if (newarg) { |
| 2935 | if (arg == MAX_CRT_ARGS) { |
| 2936 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 2937 | cfgerr = 1; |
| 2938 | break; |
| 2939 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2940 | newarg = 0; |
| 2941 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2942 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2943 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2944 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2945 | if (cfgerr) |
| 2946 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2947 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2948 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2949 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2950 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2951 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2952 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2953 | crt_path = args[0]; |
| 2954 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 2955 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 2956 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 2957 | crt_path, linenum, file); |
| 2958 | cfgerr = 1; |
| 2959 | break; |
| 2960 | } |
| 2961 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 2962 | crt_path = path; |
| 2963 | } |
| 2964 | |
| 2965 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 2966 | cur_arg = ssl_b ? ssl_b : 1; |
| 2967 | while (cur_arg < ssl_e) { |
| 2968 | newarg = 0; |
| 2969 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 2970 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 2971 | newarg = 1; |
| 2972 | cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
| 2973 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 2974 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 2975 | args[cur_arg], linenum, file); |
| 2976 | cfgerr = 1; |
| 2977 | } |
| 2978 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 2979 | break; |
| 2980 | } |
| 2981 | } |
| 2982 | if (!cfgerr && !newarg) { |
| 2983 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 2984 | args[cur_arg], linenum, file); |
| 2985 | cfgerr = 1; |
| 2986 | break; |
| 2987 | } |
| 2988 | } |
| 2989 | if (cfgerr) { |
| 2990 | ssl_sock_free_ssl_conf(ssl_conf); |
| 2991 | free(ssl_conf); |
| 2992 | ssl_conf = NULL; |
| 2993 | break; |
| 2994 | } |
| 2995 | |
| 2996 | if (stat(crt_path, &buf) == 0) { |
| 2997 | cfgerr = ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf, |
| 2998 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 2999 | } else { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3000 | cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf, |
| 3001 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3002 | } |
| 3003 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3004 | if (cfgerr) { |
| 3005 | 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] | 3006 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3007 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3008 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3009 | fclose(f); |
| 3010 | return cfgerr; |
| 3011 | } |
| 3012 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3013 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 3014 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 3015 | #endif |
| 3016 | |
| 3017 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 3018 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
Willy Tarreau | 7d588ee | 2012-11-26 18:47:31 +0100 | [diff] [blame] | 3019 | #define SSL_renegotiate_pending(arg) 0 |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3020 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3021 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 3022 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 3023 | #endif |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3024 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 3025 | #define SSL_OP_NO_TICKET 0 |
| 3026 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3027 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 3028 | #define SSL_OP_NO_COMPRESSION 0 |
| 3029 | #endif |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 3030 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 3031 | #define SSL_OP_NO_TLSv1_1 0 |
| 3032 | #endif |
| 3033 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 3034 | #define SSL_OP_NO_TLSv1_2 0 |
| 3035 | #endif |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3036 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 3037 | #define SSL_OP_SINGLE_DH_USE 0 |
| 3038 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3039 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 3040 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 3041 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3042 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 3043 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 3044 | #endif |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3045 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 3046 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 3047 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3048 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3049 | 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] | 3050 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3051 | struct proxy *curproxy = bind_conf->frontend; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3052 | int cfgerr = 0; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3053 | int verify = SSL_VERIFY_NONE; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 3054 | long ssloptions = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3055 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3056 | SSL_OP_NO_SSLv2 | |
| 3057 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3058 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3059 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3060 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
| 3061 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 3062 | long sslmode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3063 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3064 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3065 | SSL_MODE_RELEASE_BUFFERS | |
| 3066 | SSL_MODE_SMALL_BUFFERS; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3067 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3068 | const SSL_CIPHER * cipher = NULL; |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 3069 | char cipher_description[128]; |
| 3070 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 3071 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 3072 | which is not ephemeral DH. */ |
| 3073 | const char dhe_description[] = " Kx=DH "; |
| 3074 | const char dhe_export_description[] = " Kx=DH("; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3075 | int idx = 0; |
| 3076 | int dhe_found = 0; |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3077 | SSL *ssl = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3078 | struct ssl_bind_conf *ssl_conf_cur; |
| 3079 | int conf_ssl_options = bind_conf->ssl_conf.ssl_options | (ssl_conf ? ssl_conf->ssl_options : 0); |
| 3080 | const char *conf_ciphers; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3081 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3082 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3083 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 3084 | if (!ssl_initialize_random()) { |
| 3085 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 3086 | cfgerr++; |
| 3087 | } |
| 3088 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3089 | if (conf_ssl_options & BC_SSL_O_NO_SSLV3) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3090 | ssloptions |= SSL_OP_NO_SSLv3; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3091 | if (conf_ssl_options & BC_SSL_O_NO_TLSV10) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3092 | ssloptions |= SSL_OP_NO_TLSv1; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3093 | if (conf_ssl_options & BC_SSL_O_NO_TLSV11) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 3094 | ssloptions |= SSL_OP_NO_TLSv1_1; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3095 | if (conf_ssl_options & BC_SSL_O_NO_TLSV12) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 3096 | ssloptions |= SSL_OP_NO_TLSv1_2; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3097 | if (conf_ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 3098 | ssloptions |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3099 | #ifndef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3100 | 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] | 3101 | #ifndef OPENSSL_NO_SSL3 |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 3102 | 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] | 3103 | #else |
| 3104 | Alert("SSLv3 support requested but unavailable.\n"); |
| 3105 | cfgerr++; |
| 3106 | #endif |
| 3107 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3108 | if (conf_ssl_options & BC_SSL_O_USE_TLSV10) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 3109 | SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()); |
| 3110 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3111 | if (conf_ssl_options & BC_SSL_O_USE_TLSV11) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 3112 | SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method()); |
| 3113 | #endif |
| 3114 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3115 | if (conf_ssl_options & BC_SSL_O_USE_TLSV12) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 3116 | SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method()); |
| 3117 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3118 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3119 | SSL_CTX_set_options(ctx, ssloptions); |
| 3120 | SSL_CTX_set_mode(ctx, sslmode); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3121 | 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] | 3122 | case SSL_SOCK_VERIFY_NONE: |
| 3123 | verify = SSL_VERIFY_NONE; |
| 3124 | break; |
| 3125 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 3126 | verify = SSL_VERIFY_PEER; |
| 3127 | break; |
| 3128 | case SSL_SOCK_VERIFY_REQUIRED: |
| 3129 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 3130 | break; |
| 3131 | } |
| 3132 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 3133 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3134 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 3135 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 3136 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3137 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3138 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3139 | 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] | 3140 | 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] | 3141 | cfgerr++; |
| 3142 | } |
| 3143 | /* set CA names fo client cert request, function returns void */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3144 | 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] | 3145 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3146 | else { |
| 3147 | Alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 3148 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3149 | cfgerr++; |
| 3150 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 3151 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3152 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3153 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 3154 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3155 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3156 | 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] | 3157 | 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] | 3158 | cfgerr++; |
| 3159 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 3160 | else { |
| 3161 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 3162 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3163 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 3164 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3165 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 3166 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 3167 | #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] | 3168 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 3169 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
| 3170 | Alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 3171 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3172 | cfgerr++; |
| 3173 | } |
| 3174 | } |
| 3175 | #endif |
| 3176 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3177 | if (global_ssl.life_time) |
| 3178 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emeric Brun | 4f65bff | 2012-11-16 15:11:00 +0100 | [diff] [blame] | 3179 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3180 | shared_context_set_cache(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3181 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 3182 | if (conf_ciphers && |
| 3183 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3184 | 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] | 3185 | 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] | 3186 | cfgerr++; |
| 3187 | } |
| 3188 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3189 | /* If tune.ssl.default-dh-param has not been set, |
| 3190 | neither has ssl-default-dh-file and no static DH |
| 3191 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3192 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3193 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3194 | (ssl_dh_ptr_index == -1 || |
| 3195 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 3196 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3197 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3198 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3199 | if (ssl) { |
| 3200 | ciphers = SSL_get_ciphers(ssl); |
| 3201 | |
| 3202 | if (ciphers) { |
| 3203 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 3204 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 3205 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 3206 | if (strstr(cipher_description, dhe_description) != NULL || |
| 3207 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 3208 | dhe_found = 1; |
| 3209 | break; |
| 3210 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 3211 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3212 | } |
| 3213 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 3214 | SSL_free(ssl); |
| 3215 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 3216 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3217 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 3218 | if (dhe_found) { |
| 3219 | 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] | 3220 | } |
| 3221 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3222 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3223 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3224 | |
| 3225 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3226 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3227 | if (local_dh_1024 == NULL) { |
| 3228 | local_dh_1024 = ssl_get_dh_1024(); |
| 3229 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3230 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3231 | if (local_dh_2048 == NULL) { |
| 3232 | local_dh_2048 = ssl_get_dh_2048(); |
| 3233 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3234 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3235 | if (local_dh_4096 == NULL) { |
| 3236 | local_dh_4096 = ssl_get_dh_4096(); |
| 3237 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3238 | } |
| 3239 | } |
| 3240 | } |
| 3241 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3242 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3243 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 3244 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3245 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 3246 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3247 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3248 | #ifdef OPENSSL_NPN_NEGOTIATED |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3249 | ssl_conf_cur = NULL; |
| 3250 | if (ssl_conf && ssl_conf->npn_str) |
| 3251 | ssl_conf_cur = ssl_conf; |
| 3252 | else if (bind_conf->ssl_conf.npn_str) |
| 3253 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 3254 | if (ssl_conf_cur) |
| 3255 | 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] | 3256 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 3257 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3258 | ssl_conf_cur = NULL; |
| 3259 | if (ssl_conf && ssl_conf->alpn_str) |
| 3260 | ssl_conf_cur = ssl_conf; |
| 3261 | else if (bind_conf->ssl_conf.alpn_str) |
| 3262 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 3263 | if (ssl_conf_cur) |
| 3264 | 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] | 3265 | #endif |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3266 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3267 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3268 | #ifdef OPENSSL_IS_BORINGSSL |
| 3269 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3270 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
| 3271 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3272 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3273 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3274 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3275 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3276 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3277 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 3278 | if (conf_curves) { |
| 3279 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
| 3280 | Alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 3281 | curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3282 | cfgerr++; |
| 3283 | } |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3284 | else |
| 3285 | SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3286 | } |
| 3287 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3288 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3289 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3290 | int i; |
| 3291 | EC_KEY *ecdh; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3292 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 3293 | (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] | 3294 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3295 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3296 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
| 3297 | 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] | 3298 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3299 | cfgerr++; |
| 3300 | } |
| 3301 | else { |
| 3302 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 3303 | EC_KEY_free(ecdh); |
| 3304 | } |
| 3305 | } |
| 3306 | #endif |
| 3307 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3308 | return cfgerr; |
| 3309 | } |
| 3310 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3311 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 3312 | { |
| 3313 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 3314 | size_t prefixlen, suffixlen; |
| 3315 | |
| 3316 | /* Trivial case */ |
| 3317 | if (strcmp(pattern, hostname) == 0) |
| 3318 | return 1; |
| 3319 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3320 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 3321 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 3322 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 3323 | pattern_wildcard = NULL; |
| 3324 | pattern_left_label_end = pattern; |
| 3325 | while (*pattern_left_label_end != '.') { |
| 3326 | switch (*pattern_left_label_end) { |
| 3327 | case 0: |
| 3328 | /* End of label not found */ |
| 3329 | return 0; |
| 3330 | case '*': |
| 3331 | /* If there is more than one wildcards */ |
| 3332 | if (pattern_wildcard) |
| 3333 | return 0; |
| 3334 | pattern_wildcard = pattern_left_label_end; |
| 3335 | break; |
| 3336 | } |
| 3337 | pattern_left_label_end++; |
| 3338 | } |
| 3339 | |
| 3340 | /* If it's not trivial and there is no wildcard, it can't |
| 3341 | * match */ |
| 3342 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3343 | return 0; |
| 3344 | |
| 3345 | /* Make sure all labels match except the leftmost */ |
| 3346 | hostname_left_label_end = strchr(hostname, '.'); |
| 3347 | if (!hostname_left_label_end |
| 3348 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 3349 | return 0; |
| 3350 | |
| 3351 | /* Make sure the leftmost label of the hostname is long enough |
| 3352 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 3353 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3354 | return 0; |
| 3355 | |
| 3356 | /* Finally compare the string on either side of the |
| 3357 | * wildcard */ |
| 3358 | prefixlen = pattern_wildcard - pattern; |
| 3359 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 3360 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 3361 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3362 | return 0; |
| 3363 | |
| 3364 | return 1; |
| 3365 | } |
| 3366 | |
| 3367 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 3368 | { |
| 3369 | SSL *ssl; |
| 3370 | struct connection *conn; |
| 3371 | char *servername; |
| 3372 | |
| 3373 | int depth; |
| 3374 | X509 *cert; |
| 3375 | STACK_OF(GENERAL_NAME) *alt_names; |
| 3376 | int i; |
| 3377 | X509_NAME *cert_subject; |
| 3378 | char *str; |
| 3379 | |
| 3380 | if (ok == 0) |
| 3381 | return ok; |
| 3382 | |
| 3383 | 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] | 3384 | conn = SSL_get_app_data(ssl); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3385 | |
| 3386 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
| 3387 | |
| 3388 | /* We only need to verify the CN on the actual server cert, |
| 3389 | * not the indirect CAs */ |
| 3390 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 3391 | if (depth != 0) |
| 3392 | return ok; |
| 3393 | |
| 3394 | /* At this point, the cert is *not* OK unless we can find a |
| 3395 | * hostname match */ |
| 3396 | ok = 0; |
| 3397 | |
| 3398 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 3399 | /* It seems like this might happen if verify peer isn't set */ |
| 3400 | if (!cert) |
| 3401 | return ok; |
| 3402 | |
| 3403 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 3404 | if (alt_names) { |
| 3405 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 3406 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 3407 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 3408 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 3409 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 3410 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3411 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 3412 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3413 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 3414 | OPENSSL_free(str); |
| 3415 | } |
| 3416 | } |
| 3417 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 3418 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | cert_subject = X509_get_subject_name(cert); |
| 3422 | i = -1; |
| 3423 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 3424 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3425 | ASN1_STRING *value; |
| 3426 | value = X509_NAME_ENTRY_get_data(entry); |
| 3427 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3428 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 3429 | OPENSSL_free(str); |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | return ok; |
| 3434 | } |
| 3435 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3436 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3437 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3438 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3439 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3440 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 3441 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3442 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3443 | SSL_OP_NO_SSLv2 | |
| 3444 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 3445 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3446 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3447 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3448 | SSL_MODE_RELEASE_BUFFERS | |
| 3449 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3450 | int verify = SSL_VERIFY_NONE; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3451 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3452 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 3453 | if (!ssl_initialize_random()) { |
| 3454 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 3455 | cfgerr++; |
| 3456 | } |
| 3457 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 3458 | /* Automatic memory computations need to know we use SSL there */ |
| 3459 | global.ssl_used_backend = 1; |
| 3460 | |
| 3461 | /* Initiate SSL context for current server */ |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3462 | srv->ssl_ctx.reused_sess = NULL; |
| 3463 | if (srv->use_ssl) |
| 3464 | srv->xprt = &ssl_sock; |
| 3465 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 3466 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3467 | |
| 3468 | srv->ssl_ctx.ctx = SSL_CTX_new(SSLv23_client_method()); |
| 3469 | if (!srv->ssl_ctx.ctx) { |
| 3470 | Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 3471 | proxy_type_str(curproxy), curproxy->id, |
| 3472 | srv->id); |
| 3473 | cfgerr++; |
| 3474 | return cfgerr; |
| 3475 | } |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 3476 | if (srv->ssl_ctx.client_crt) { |
| 3477 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
| 3478 | Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 3479 | proxy_type_str(curproxy), curproxy->id, |
| 3480 | srv->id, srv->ssl_ctx.client_crt); |
| 3481 | cfgerr++; |
| 3482 | } |
| 3483 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
| 3484 | Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 3485 | proxy_type_str(curproxy), curproxy->id, |
| 3486 | srv->id, srv->ssl_ctx.client_crt); |
| 3487 | cfgerr++; |
| 3488 | } |
| 3489 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
| 3490 | Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3491 | proxy_type_str(curproxy), curproxy->id, |
| 3492 | srv->id, srv->ssl_ctx.client_crt); |
| 3493 | cfgerr++; |
| 3494 | } |
| 3495 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3496 | |
| 3497 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_SSLV3) |
| 3498 | options |= SSL_OP_NO_SSLv3; |
| 3499 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV10) |
| 3500 | options |= SSL_OP_NO_TLSv1; |
| 3501 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV11) |
| 3502 | options |= SSL_OP_NO_TLSv1_1; |
| 3503 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV12) |
| 3504 | options |= SSL_OP_NO_TLSv1_2; |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 3505 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 3506 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3507 | #ifndef OPENSSL_IS_BORINGSSL |
Jérémie Courrèges-Anglas | 17c3f62 | 2015-07-25 16:50:52 -0600 | [diff] [blame] | 3508 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_SSLV3) { |
| 3509 | #ifndef OPENSSL_NO_SSL3 |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3510 | 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] | 3511 | #else |
Thierry FOURNIER | bc96534 | 2015-08-26 08:21:26 +0200 | [diff] [blame] | 3512 | Alert("SSLv3 support requested but unavailable.\n"); |
Jérémie Courrèges-Anglas | 17c3f62 | 2015-07-25 16:50:52 -0600 | [diff] [blame] | 3513 | cfgerr++; |
| 3514 | #endif |
| 3515 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3516 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV10) |
| 3517 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_client_method()); |
| 3518 | #if SSL_OP_NO_TLSv1_1 |
| 3519 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV11) |
| 3520 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_1_client_method()); |
| 3521 | #endif |
| 3522 | #if SSL_OP_NO_TLSv1_2 |
| 3523 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV12) |
| 3524 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_2_client_method()); |
| 3525 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3526 | #endif |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3527 | SSL_CTX_set_options(srv->ssl_ctx.ctx, options); |
| 3528 | SSL_CTX_set_mode(srv->ssl_ctx.ctx, mode); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3529 | |
| 3530 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 3531 | verify = SSL_VERIFY_PEER; |
| 3532 | |
| 3533 | switch (srv->ssl_ctx.verify) { |
| 3534 | case SSL_SOCK_VERIFY_NONE: |
| 3535 | verify = SSL_VERIFY_NONE; |
| 3536 | break; |
| 3537 | case SSL_SOCK_VERIFY_REQUIRED: |
| 3538 | verify = SSL_VERIFY_PEER; |
| 3539 | break; |
| 3540 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3541 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3542 | verify, |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3543 | srv->ssl_ctx.verify_host ? ssl_sock_srv_verifycbk : NULL); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3544 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3545 | if (srv->ssl_ctx.ca_file) { |
| 3546 | /* load CAfile to verify */ |
| 3547 | 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] | 3548 | 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] | 3549 | curproxy->id, srv->id, |
| 3550 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
| 3551 | cfgerr++; |
| 3552 | } |
| 3553 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3554 | else { |
| 3555 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 3556 | 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] | 3557 | curproxy->id, srv->id, |
| 3558 | srv->conf.file, srv->conf.line); |
| 3559 | else |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 3560 | 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] | 3561 | curproxy->id, srv->id, |
| 3562 | srv->conf.file, srv->conf.line); |
| 3563 | cfgerr++; |
| 3564 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3565 | #ifdef X509_V_FLAG_CRL_CHECK |
| 3566 | if (srv->ssl_ctx.crl_file) { |
| 3567 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 3568 | |
| 3569 | 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] | 3570 | 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] | 3571 | curproxy->id, srv->id, |
| 3572 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
| 3573 | cfgerr++; |
| 3574 | } |
| 3575 | else { |
| 3576 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 3577 | } |
| 3578 | } |
| 3579 | #endif |
| 3580 | } |
| 3581 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3582 | if (global_ssl.life_time) |
| 3583 | SSL_CTX_set_timeout(srv->ssl_ctx.ctx, global_ssl.life_time); |
Emeric Brun | 4f65bff | 2012-11-16 15:11:00 +0100 | [diff] [blame] | 3584 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3585 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF); |
| 3586 | if (srv->ssl_ctx.ciphers && |
| 3587 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
| 3588 | Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 3589 | curproxy->id, srv->id, |
| 3590 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
| 3591 | cfgerr++; |
| 3592 | } |
| 3593 | |
| 3594 | return cfgerr; |
| 3595 | } |
| 3596 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3597 | /* 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] | 3598 | * be NULL, in which case nothing is done. Returns the number of errors |
| 3599 | * encountered. |
| 3600 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3601 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3602 | { |
| 3603 | struct ebmb_node *node; |
| 3604 | struct sni_ctx *sni; |
| 3605 | int err = 0; |
| 3606 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3607 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3608 | return 0; |
| 3609 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 3610 | /* Automatic memory computations need to know we use SSL there */ |
| 3611 | global.ssl_used_frontend = 1; |
| 3612 | |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 3613 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3614 | 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] | 3615 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3616 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3617 | while (node) { |
| 3618 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 3619 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 3620 | /* only initialize the CTX on its first occurrence and |
| 3621 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3622 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3623 | node = ebmb_next(node); |
| 3624 | } |
| 3625 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3626 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3627 | while (node) { |
| 3628 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 3629 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 3630 | /* only initialize the CTX on its first occurrence and |
| 3631 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3632 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3633 | node = ebmb_next(node); |
| 3634 | } |
| 3635 | return err; |
| 3636 | } |
| 3637 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 3638 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 3639 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 3640 | * alerts are directly emitted since the rest of the stack does it below. |
| 3641 | */ |
| 3642 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 3643 | { |
| 3644 | struct proxy *px = bind_conf->frontend; |
| 3645 | int alloc_ctx; |
| 3646 | int err; |
| 3647 | |
| 3648 | if (!bind_conf->is_ssl) { |
| 3649 | if (bind_conf->default_ctx) { |
| 3650 | Warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 3651 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3652 | } |
| 3653 | return 0; |
| 3654 | } |
| 3655 | if (!bind_conf->default_ctx) { |
| 3656 | Alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 3657 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 3658 | return -1; |
| 3659 | } |
| 3660 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3661 | 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] | 3662 | if (alloc_ctx < 0) { |
| 3663 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 3664 | 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"); |
| 3665 | else |
| 3666 | Alert("Unable to allocate SSL session cache.\n"); |
| 3667 | return -1; |
| 3668 | } |
| 3669 | |
| 3670 | err = 0; |
| 3671 | /* initialize all certificate contexts */ |
| 3672 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 3673 | |
| 3674 | /* initialize CA variables if the certificates generation is enabled */ |
| 3675 | err += ssl_sock_load_ca(bind_conf); |
| 3676 | |
| 3677 | return -err; |
| 3678 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 3679 | |
| 3680 | /* release ssl context allocated for servers. */ |
| 3681 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 3682 | { |
| 3683 | if (srv->ssl_ctx.ctx) |
| 3684 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 3685 | } |
| 3686 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3687 | /* 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] | 3688 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 3689 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3690 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3691 | { |
| 3692 | struct ebmb_node *node, *back; |
| 3693 | struct sni_ctx *sni; |
| 3694 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3695 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3696 | return; |
| 3697 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3698 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3699 | while (node) { |
| 3700 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 3701 | back = ebmb_next(node); |
| 3702 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3703 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3704 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3705 | ssl_sock_free_ssl_conf(sni->conf); |
| 3706 | free(sni->conf); |
| 3707 | sni->conf = NULL; |
| 3708 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3709 | free(sni); |
| 3710 | node = back; |
| 3711 | } |
| 3712 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3713 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3714 | while (node) { |
| 3715 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 3716 | back = ebmb_next(node); |
| 3717 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3718 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3719 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3720 | ssl_sock_free_ssl_conf(sni->conf); |
| 3721 | free(sni->conf); |
| 3722 | sni->conf = NULL; |
| 3723 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3724 | free(sni); |
| 3725 | node = back; |
| 3726 | } |
| 3727 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3728 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3729 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3730 | } |
| 3731 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3732 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 3733 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 3734 | { |
| 3735 | ssl_sock_free_ca(bind_conf); |
| 3736 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3737 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3738 | free(bind_conf->ca_sign_file); |
| 3739 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3740 | if (bind_conf->keys_ref) { |
| 3741 | free(bind_conf->keys_ref->filename); |
| 3742 | free(bind_conf->keys_ref->tlskeys); |
| 3743 | LIST_DEL(&bind_conf->keys_ref->list); |
| 3744 | free(bind_conf->keys_ref); |
| 3745 | } |
| 3746 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3747 | bind_conf->ca_sign_pass = NULL; |
| 3748 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 3749 | } |
| 3750 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3751 | /* Load CA cert file and private key used to generate certificates */ |
| 3752 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3753 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3754 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3755 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3756 | FILE *fp; |
| 3757 | X509 *cacert = NULL; |
| 3758 | EVP_PKEY *capkey = NULL; |
| 3759 | int err = 0; |
| 3760 | |
| 3761 | if (!bind_conf || !bind_conf->generate_certs) |
| 3762 | return err; |
| 3763 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3764 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3765 | if (global_ssl.ctx_cache) |
| 3766 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 3767 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 3768 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 3769 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3770 | if (!bind_conf->ca_sign_file) { |
| 3771 | Alert("Proxy '%s': cannot enable certificate generation, " |
| 3772 | "no CA certificate File configured at [%s:%d].\n", |
| 3773 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3774 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3775 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3776 | |
| 3777 | /* read in the CA certificate */ |
| 3778 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
| 3779 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 3780 | 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] | 3781 | goto load_error; |
| 3782 | } |
| 3783 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
| 3784 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 3785 | 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] | 3786 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3787 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3788 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3789 | if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) { |
| 3790 | Alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 3791 | 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] | 3792 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3793 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3794 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3795 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3796 | bind_conf->ca_sign_cert = cacert; |
| 3797 | bind_conf->ca_sign_pkey = capkey; |
| 3798 | return err; |
| 3799 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3800 | read_error: |
| 3801 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3802 | if (capkey) EVP_PKEY_free(capkey); |
| 3803 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3804 | load_error: |
| 3805 | bind_conf->generate_certs = 0; |
| 3806 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3807 | return err; |
| 3808 | } |
| 3809 | |
| 3810 | /* Release CA cert and private key used to generate certificated */ |
| 3811 | void |
| 3812 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 3813 | { |
| 3814 | if (!bind_conf) |
| 3815 | return; |
| 3816 | |
| 3817 | if (bind_conf->ca_sign_pkey) |
| 3818 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 3819 | if (bind_conf->ca_sign_cert) |
| 3820 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 3821 | bind_conf->ca_sign_pkey = NULL; |
| 3822 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3823 | } |
| 3824 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3825 | /* |
| 3826 | * This function is called if SSL * context is not yet allocated. The function |
| 3827 | * is designed to be called before any other data-layer operation and sets the |
| 3828 | * handshake flag on the connection. It is safe to call it multiple times. |
| 3829 | * It returns 0 on success and -1 in error case. |
| 3830 | */ |
| 3831 | static int ssl_sock_init(struct connection *conn) |
| 3832 | { |
| 3833 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3834 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3835 | return 0; |
| 3836 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3837 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 3838 | return 0; |
| 3839 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3840 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 3841 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3842 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3843 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3844 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3845 | /* If it is in client mode initiate SSL session |
| 3846 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3847 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3848 | int may_retry = 1; |
| 3849 | |
| 3850 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3851 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3852 | conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3853 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3854 | if (may_retry--) { |
| 3855 | pool_gc2(); |
| 3856 | goto retry_connect; |
| 3857 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3858 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3859 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3860 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3861 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3862 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3863 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 3864 | SSL_free(conn->xprt_ctx); |
| 3865 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3866 | if (may_retry--) { |
| 3867 | pool_gc2(); |
| 3868 | goto retry_connect; |
| 3869 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3870 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 3871 | return -1; |
| 3872 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3873 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3874 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3875 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 3876 | SSL_free(conn->xprt_ctx); |
| 3877 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3878 | if (may_retry--) { |
| 3879 | pool_gc2(); |
| 3880 | goto retry_connect; |
| 3881 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3882 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 3883 | return -1; |
| 3884 | } |
| 3885 | |
| 3886 | SSL_set_connect_state(conn->xprt_ctx); |
| 3887 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 3888 | if(!SSL_set_session(conn->xprt_ctx, objt_server(conn->target)->ssl_ctx.reused_sess)) { |
| 3889 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 3890 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 3891 | } |
| 3892 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3893 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3894 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 3895 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3896 | |
| 3897 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 3898 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3899 | return 0; |
| 3900 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3901 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3902 | int may_retry = 1; |
| 3903 | |
| 3904 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3905 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3906 | 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] | 3907 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3908 | if (may_retry--) { |
| 3909 | pool_gc2(); |
| 3910 | goto retry_accept; |
| 3911 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3912 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3913 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3914 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3915 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3916 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3917 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 3918 | SSL_free(conn->xprt_ctx); |
| 3919 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3920 | if (may_retry--) { |
| 3921 | pool_gc2(); |
| 3922 | goto retry_accept; |
| 3923 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3924 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 3925 | return -1; |
| 3926 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3927 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3928 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3929 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 3930 | SSL_free(conn->xprt_ctx); |
| 3931 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3932 | if (may_retry--) { |
| 3933 | pool_gc2(); |
| 3934 | goto retry_accept; |
| 3935 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3936 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 3937 | return -1; |
| 3938 | } |
| 3939 | |
| 3940 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3941 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3942 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 3943 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3944 | |
| 3945 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 3946 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3947 | return 0; |
| 3948 | } |
| 3949 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3950 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3951 | return -1; |
| 3952 | } |
| 3953 | |
| 3954 | |
| 3955 | /* This is the callback which is used when an SSL handshake is pending. It |
| 3956 | * updates the FD status if it wants some polling before being called again. |
| 3957 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 3958 | * otherwise it returns non-zero and removes itself from the connection's |
| 3959 | * flags (the bit is provided in <flag> by the caller). |
| 3960 | */ |
| 3961 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 3962 | { |
| 3963 | int ret; |
| 3964 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3965 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 3966 | return 0; |
| 3967 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3968 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3969 | goto out_error; |
| 3970 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3971 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 3972 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 3973 | * Usually SSL_write and SSL_read are used and process implicitly |
| 3974 | * the reneg handshake. |
| 3975 | * Here we use SSL_peek as a workaround for reneg. |
| 3976 | */ |
| 3977 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 3978 | char c; |
| 3979 | |
| 3980 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 3981 | if (ret <= 0) { |
| 3982 | /* handshake may have not been completed, let's find why */ |
| 3983 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 3984 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 3985 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 3986 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 3987 | __conn_sock_want_send(conn); |
| 3988 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3989 | return 0; |
| 3990 | } |
| 3991 | else if (ret == SSL_ERROR_WANT_READ) { |
| 3992 | /* handshake may have been completed but we have |
| 3993 | * no more data to read. |
| 3994 | */ |
| 3995 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 3996 | ret = 1; |
| 3997 | goto reneg_ok; |
| 3998 | } |
| 3999 | /* SSL handshake needs to read, L4 connection is ready */ |
| 4000 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 4001 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4002 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4003 | __conn_sock_want_recv(conn); |
| 4004 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4005 | return 0; |
| 4006 | } |
| 4007 | else if (ret == SSL_ERROR_SYSCALL) { |
| 4008 | /* if errno is null, then connection was successfully established */ |
| 4009 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 4010 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4011 | if (!conn->err_code) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4012 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 4013 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4014 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4015 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 4016 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4017 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 4018 | empty_handshake = state == TLS_ST_BEFORE; |
| 4019 | #else |
| 4020 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
| 4021 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4022 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4023 | if (!errno) { |
| 4024 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4025 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4026 | else |
| 4027 | conn->err_code = CO_ER_SSL_EMPTY; |
| 4028 | } |
| 4029 | else { |
| 4030 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4031 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4032 | else |
| 4033 | conn->err_code = CO_ER_SSL_ABORT; |
| 4034 | } |
| 4035 | } |
| 4036 | else { |
| 4037 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4038 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4039 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4040 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4041 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4042 | #endif |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4043 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4044 | goto out_error; |
| 4045 | } |
| 4046 | else { |
| 4047 | /* Fail on all other handshake errors */ |
| 4048 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 4049 | * buffer, causing an RST to be emitted upon close() on |
| 4050 | * TCP sockets. We first try to drain possibly pending |
| 4051 | * data to avoid this as much as possible. |
| 4052 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 4053 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4054 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 4055 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 4056 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4057 | goto out_error; |
| 4058 | } |
| 4059 | } |
| 4060 | /* read some data: consider handshake completed */ |
| 4061 | goto reneg_ok; |
| 4062 | } |
| 4063 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4064 | ret = SSL_do_handshake(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4065 | if (ret != 1) { |
| 4066 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4067 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4068 | |
| 4069 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 4070 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 4071 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4072 | __conn_sock_want_send(conn); |
| 4073 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4074 | return 0; |
| 4075 | } |
| 4076 | else if (ret == SSL_ERROR_WANT_READ) { |
| 4077 | /* SSL handshake needs to read, L4 connection is ready */ |
| 4078 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 4079 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 4080 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4081 | __conn_sock_want_recv(conn); |
| 4082 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4083 | return 0; |
| 4084 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 4085 | else if (ret == SSL_ERROR_SYSCALL) { |
| 4086 | /* if errno is null, then connection was successfully established */ |
| 4087 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 4088 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4089 | if (!conn->err_code) { |
| 4090 | #ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */ |
| 4091 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 4092 | #else |
| 4093 | int empty_handshake; |
Luca Pizzamiglio | 578b169 | 2016-12-12 10:56:56 +0100 | [diff] [blame] | 4094 | #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4095 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx); |
| 4096 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4097 | #else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4098 | empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4099 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4100 | if (empty_handshake) { |
| 4101 | if (!errno) { |
| 4102 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4103 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4104 | else |
| 4105 | conn->err_code = CO_ER_SSL_EMPTY; |
| 4106 | } |
| 4107 | else { |
| 4108 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4109 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4110 | else |
| 4111 | conn->err_code = CO_ER_SSL_ABORT; |
| 4112 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4113 | } |
| 4114 | else { |
| 4115 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 4116 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 4117 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4118 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4119 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4120 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4121 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 4122 | goto out_error; |
| 4123 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4124 | else { |
| 4125 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 4126 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 4127 | * buffer, causing an RST to be emitted upon close() on |
| 4128 | * TCP sockets. We first try to drain possibly pending |
| 4129 | * data to avoid this as much as possible. |
| 4130 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 4131 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4132 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 4133 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 4134 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4135 | goto out_error; |
| 4136 | } |
| 4137 | } |
| 4138 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 4139 | reneg_ok: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4140 | /* Handshake succeeded */ |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 4141 | if (!SSL_session_reused(conn->xprt_ctx)) { |
| 4142 | if (objt_server(conn->target)) { |
| 4143 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 4144 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 4145 | global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr; |
| 4146 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4147 | /* 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] | 4148 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4149 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
Willy Tarreau | 30fd4bd | 2016-12-22 21:54:21 +0100 | [diff] [blame] | 4150 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 4151 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4152 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 4153 | if (!(objt_server(conn->target)->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) |
| 4154 | 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] | 4155 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 4156 | else { |
| 4157 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 4158 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 4159 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 4160 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | /* The connection is now established at both layers, it's time to leave */ |
| 4164 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 4165 | return 1; |
| 4166 | |
| 4167 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4168 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4169 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4170 | ERR_clear_error(); |
| 4171 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 4172 | /* free resumed session if exists */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4173 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 4174 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 4175 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 4176 | } |
| 4177 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4178 | /* Fail on all other handshake errors */ |
| 4179 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4180 | if (!conn->err_code) |
| 4181 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4182 | return 0; |
| 4183 | } |
| 4184 | |
| 4185 | /* 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] | 4186 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4187 | * buffer wraps, in which case a second call may be performed. The connection's |
| 4188 | * flags are updated with whatever special event is detected (error, read0, |
| 4189 | * empty). The caller is responsible for taking care of those events and |
| 4190 | * avoiding the call if inappropriate. The function does not call the |
| 4191 | * connection's polling update function, so the caller is responsible for this. |
| 4192 | */ |
| 4193 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 4194 | { |
| 4195 | int ret, done = 0; |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 4196 | int try; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4197 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4198 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4199 | goto out_error; |
| 4200 | |
| 4201 | if (conn->flags & CO_FL_HANDSHAKE) |
| 4202 | /* a handshake was requested */ |
| 4203 | return 0; |
| 4204 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 4205 | /* let's realign the buffer to optimize I/O */ |
| 4206 | if (buffer_empty(buf)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4207 | buf->p = buf->data; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4208 | |
| 4209 | /* read the largest possible block. For this, we perform only one call |
| 4210 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 4211 | * in which case we accept to do it once again. A new attempt is made on |
| 4212 | * EINTR too. |
| 4213 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 4214 | while (count > 0) { |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 4215 | /* first check if we have some room after p+i */ |
| 4216 | try = buf->data + buf->size - (buf->p + buf->i); |
| 4217 | /* otherwise continue between data and p-o */ |
| 4218 | if (try <= 0) { |
| 4219 | try = buf->p - (buf->data + buf->o); |
| 4220 | if (try <= 0) |
| 4221 | break; |
| 4222 | } |
| 4223 | if (try > count) |
| 4224 | try = count; |
| 4225 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4226 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4227 | if (conn->flags & CO_FL_ERROR) { |
| 4228 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4229 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4230 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4231 | if (ret > 0) { |
| 4232 | buf->i += ret; |
| 4233 | done += ret; |
| 4234 | if (ret < try) |
| 4235 | break; |
| 4236 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4237 | } |
| 4238 | else if (ret == 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4239 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 4240 | if (ret != SSL_ERROR_ZERO_RETURN) { |
Emeric Brun | 1c64686 | 2012-12-14 12:33:41 +0100 | [diff] [blame] | 4241 | /* error on protocol or underlying transport */ |
| 4242 | if ((ret != SSL_ERROR_SYSCALL) |
| 4243 | || (errno && (errno != EAGAIN))) |
| 4244 | conn->flags |= CO_FL_ERROR; |
| 4245 | |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4246 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4247 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4248 | ERR_clear_error(); |
| 4249 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4250 | goto read0; |
| 4251 | } |
| 4252 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4253 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4254 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4255 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4256 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4257 | __conn_sock_want_send(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4258 | break; |
| 4259 | } |
| 4260 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 4261 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4262 | /* handshake is running, and it may need to re-enable read */ |
| 4263 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 4264 | __conn_sock_want_recv(conn); |
| 4265 | break; |
| 4266 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4267 | /* we need to poll for retry a read later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4268 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4269 | break; |
| 4270 | } |
| 4271 | /* otherwise it's a real error */ |
| 4272 | goto out_error; |
| 4273 | } |
| 4274 | } |
| 4275 | return done; |
| 4276 | |
| 4277 | read0: |
| 4278 | conn_sock_read0(conn); |
| 4279 | return done; |
| 4280 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4281 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4282 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4283 | ERR_clear_error(); |
| 4284 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4285 | conn->flags |= CO_FL_ERROR; |
| 4286 | return done; |
| 4287 | } |
| 4288 | |
| 4289 | |
| 4290 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 4291 | * <flags> may contain some CO_SFL_* flags to hint the system about other |
| 4292 | * pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4293 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 4294 | * a second call may be performed. The connection's flags are updated with |
| 4295 | * whatever special event is detected (error, empty). The caller is responsible |
| 4296 | * for taking care of those events and avoiding the call if inappropriate. The |
| 4297 | * function does not call the connection's polling update function, so the caller |
| 4298 | * is responsible for this. |
| 4299 | */ |
| 4300 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 4301 | { |
| 4302 | int ret, try, done; |
| 4303 | |
| 4304 | done = 0; |
| 4305 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4306 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4307 | goto out_error; |
| 4308 | |
| 4309 | if (conn->flags & CO_FL_HANDSHAKE) |
| 4310 | /* a handshake was requested */ |
| 4311 | return 0; |
| 4312 | |
| 4313 | /* send the largest possible block. For this we perform only one call |
| 4314 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 4315 | * in which case we accept to do it once again. |
| 4316 | */ |
| 4317 | while (buf->o) { |
Kevin Hester | cad8234 | 2013-05-30 15:12:41 -0700 | [diff] [blame] | 4318 | try = bo_contig_data(buf); |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 4319 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 4320 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4321 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4322 | global_ssl.max_record && try > global_ssl.max_record) { |
| 4323 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4324 | } |
| 4325 | else { |
| 4326 | /* we need to keep the information about the fact that |
| 4327 | * we're not limiting the upcoming send(), because if it |
| 4328 | * fails, we'll have to retry with at least as many data. |
| 4329 | */ |
| 4330 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 4331 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 4332 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4333 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4334 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4335 | if (conn->flags & CO_FL_ERROR) { |
| 4336 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4337 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4338 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4339 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 4340 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
| 4341 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4342 | buf->o -= ret; |
| 4343 | done += ret; |
| 4344 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 4345 | if (likely(buffer_empty(buf))) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4346 | /* optimize data alignment in the buffer */ |
| 4347 | buf->p = buf->data; |
| 4348 | |
| 4349 | /* if the system buffer is full, don't insist */ |
| 4350 | if (ret < try) |
| 4351 | break; |
| 4352 | } |
| 4353 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4354 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4355 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 4356 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 4357 | /* handshake is running, and it may need to re-enable write */ |
| 4358 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 4359 | __conn_sock_want_send(conn); |
| 4360 | break; |
| 4361 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4362 | /* we need to poll to retry a write later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 4363 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4364 | break; |
| 4365 | } |
| 4366 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4367 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4368 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 4369 | __conn_sock_want_recv(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4370 | break; |
| 4371 | } |
| 4372 | goto out_error; |
| 4373 | } |
| 4374 | } |
| 4375 | return done; |
| 4376 | |
| 4377 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4378 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4379 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4380 | ERR_clear_error(); |
| 4381 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4382 | conn->flags |= CO_FL_ERROR; |
| 4383 | return done; |
| 4384 | } |
| 4385 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4386 | static void ssl_sock_close(struct connection *conn) { |
| 4387 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4388 | if (conn->xprt_ctx) { |
| 4389 | SSL_free(conn->xprt_ctx); |
| 4390 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4391 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4392 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 4396 | * any case, flags the connection as reusable if no handshake was in progress. |
| 4397 | */ |
| 4398 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 4399 | { |
| 4400 | if (conn->flags & CO_FL_HANDSHAKE) |
| 4401 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 4402 | if (!clean) |
| 4403 | /* don't sent notify on SSL_shutdown */ |
Willy Tarreau | e3cc3a3 | 2017-02-13 11:12:29 +0100 | [diff] [blame] | 4404 | SSL_set_quiet_shutdown(conn->xprt_ctx, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4405 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 4406 | if (SSL_shutdown(conn->xprt_ctx) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4407 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 4408 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4409 | ERR_clear_error(); |
| 4410 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4411 | } |
| 4412 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 4413 | /* used for logging, may be changed for a sample fetch later */ |
| 4414 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 4415 | { |
| 4416 | if (!conn->xprt && !conn->xprt_ctx) |
| 4417 | return NULL; |
| 4418 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 4419 | } |
| 4420 | |
| 4421 | /* used for logging, may be changed for a sample fetch later */ |
| 4422 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 4423 | { |
| 4424 | if (!conn->xprt && !conn->xprt_ctx) |
| 4425 | return NULL; |
| 4426 | return SSL_get_version(conn->xprt_ctx); |
| 4427 | } |
| 4428 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4429 | /* Extract a serial from a cert, and copy it to a chunk. |
| 4430 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 4431 | * -1 if output is not large enough. |
| 4432 | */ |
| 4433 | static int |
| 4434 | ssl_sock_get_serial(X509 *crt, struct chunk *out) |
| 4435 | { |
| 4436 | ASN1_INTEGER *serial; |
| 4437 | |
| 4438 | serial = X509_get_serialNumber(crt); |
| 4439 | if (!serial) |
| 4440 | return 0; |
| 4441 | |
| 4442 | if (out->size < serial->length) |
| 4443 | return -1; |
| 4444 | |
| 4445 | memcpy(out->str, serial->data, serial->length); |
| 4446 | out->len = serial->length; |
| 4447 | return 1; |
| 4448 | } |
| 4449 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4450 | /* Extract a cert to der, and copy it to a chunk. |
| 4451 | * Returns 1 if cert is found and copied, 0 on der convertion failure and |
| 4452 | * -1 if output is not large enough. |
| 4453 | */ |
| 4454 | static int |
| 4455 | ssl_sock_crt2der(X509 *crt, struct chunk *out) |
| 4456 | { |
| 4457 | int len; |
| 4458 | unsigned char *p = (unsigned char *)out->str;; |
| 4459 | |
| 4460 | len =i2d_X509(crt, NULL); |
| 4461 | if (len <= 0) |
| 4462 | return 1; |
| 4463 | |
| 4464 | if (out->size < len) |
| 4465 | return -1; |
| 4466 | |
| 4467 | i2d_X509(crt,&p); |
| 4468 | out->len = len; |
| 4469 | return 1; |
| 4470 | } |
| 4471 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4472 | |
| 4473 | /* Copy Date in ASN1_UTCTIME format in struct chunk out. |
| 4474 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 4475 | * and -1 if output is not large enough. |
| 4476 | */ |
| 4477 | static int |
| 4478 | ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out) |
| 4479 | { |
| 4480 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 4481 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 4482 | |
| 4483 | if (gentm->length < 12) |
| 4484 | return 0; |
| 4485 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 4486 | return 0; |
| 4487 | if (out->size < gentm->length-2) |
| 4488 | return -1; |
| 4489 | |
| 4490 | memcpy(out->str, gentm->data+2, gentm->length-2); |
| 4491 | out->len = gentm->length-2; |
| 4492 | return 1; |
| 4493 | } |
| 4494 | else if (tm->type == V_ASN1_UTCTIME) { |
| 4495 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 4496 | |
| 4497 | if (utctm->length < 10) |
| 4498 | return 0; |
| 4499 | if (utctm->data[0] >= 0x35) |
| 4500 | return 0; |
| 4501 | if (out->size < utctm->length) |
| 4502 | return -1; |
| 4503 | |
| 4504 | memcpy(out->str, utctm->data, utctm->length); |
| 4505 | out->len = utctm->length; |
| 4506 | return 1; |
| 4507 | } |
| 4508 | |
| 4509 | return 0; |
| 4510 | } |
| 4511 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4512 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 4513 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 4514 | */ |
| 4515 | static int |
| 4516 | ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out) |
| 4517 | { |
| 4518 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4519 | ASN1_OBJECT *obj; |
| 4520 | ASN1_STRING *data; |
| 4521 | const unsigned char *data_ptr; |
| 4522 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4523 | int i, j, n; |
| 4524 | int cur = 0; |
| 4525 | const char *s; |
| 4526 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4527 | int name_count; |
| 4528 | |
| 4529 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4530 | |
| 4531 | out->len = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4532 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4533 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4534 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4535 | else |
| 4536 | j = i; |
| 4537 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4538 | ne = X509_NAME_get_entry(a, j); |
| 4539 | obj = X509_NAME_ENTRY_get_object(ne); |
| 4540 | data = X509_NAME_ENTRY_get_data(ne); |
| 4541 | data_ptr = ASN1_STRING_get0_data(data); |
| 4542 | data_len = ASN1_STRING_length(data); |
| 4543 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4544 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4545 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4546 | s = tmp; |
| 4547 | } |
| 4548 | |
| 4549 | if (chunk_strcasecmp(entry, s) != 0) |
| 4550 | continue; |
| 4551 | |
| 4552 | if (pos < 0) |
| 4553 | cur--; |
| 4554 | else |
| 4555 | cur++; |
| 4556 | |
| 4557 | if (cur != pos) |
| 4558 | continue; |
| 4559 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4560 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4561 | return -1; |
| 4562 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4563 | memcpy(out->str, data_ptr, data_len); |
| 4564 | out->len = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4565 | return 1; |
| 4566 | } |
| 4567 | |
| 4568 | return 0; |
| 4569 | |
| 4570 | } |
| 4571 | |
| 4572 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 4573 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 4574 | */ |
| 4575 | static int |
| 4576 | ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out) |
| 4577 | { |
| 4578 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4579 | ASN1_OBJECT *obj; |
| 4580 | ASN1_STRING *data; |
| 4581 | const unsigned char *data_ptr; |
| 4582 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4583 | int i, n, ln; |
| 4584 | int l = 0; |
| 4585 | const char *s; |
| 4586 | char *p; |
| 4587 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4588 | int name_count; |
| 4589 | |
| 4590 | |
| 4591 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4592 | |
| 4593 | out->len = 0; |
| 4594 | p = out->str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4595 | for (i = 0; i < name_count; i++) { |
| 4596 | ne = X509_NAME_get_entry(a, i); |
| 4597 | obj = X509_NAME_ENTRY_get_object(ne); |
| 4598 | data = X509_NAME_ENTRY_get_data(ne); |
| 4599 | data_ptr = ASN1_STRING_get0_data(data); |
| 4600 | data_len = ASN1_STRING_length(data); |
| 4601 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4602 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4603 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4604 | s = tmp; |
| 4605 | } |
| 4606 | ln = strlen(s); |
| 4607 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4608 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4609 | if (l > out->size) |
| 4610 | return -1; |
| 4611 | out->len = l; |
| 4612 | |
| 4613 | *(p++)='/'; |
| 4614 | memcpy(p, s, ln); |
| 4615 | p += ln; |
| 4616 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4617 | memcpy(p, data_ptr, data_len); |
| 4618 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | if (!out->len) |
| 4622 | return 0; |
| 4623 | |
| 4624 | return 1; |
| 4625 | } |
| 4626 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4627 | char *ssl_sock_get_version(struct connection *conn) |
| 4628 | { |
| 4629 | if (!ssl_sock_is_ssl(conn)) |
| 4630 | return NULL; |
| 4631 | |
| 4632 | return (char *)SSL_get_version(conn->xprt_ctx); |
| 4633 | } |
| 4634 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 4635 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 4636 | * to disable SNI. |
| 4637 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 4638 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 4639 | { |
| 4640 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 4641 | char *prev_name; |
| 4642 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 4643 | if (!ssl_sock_is_ssl(conn)) |
| 4644 | return; |
| 4645 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 4646 | /* if the SNI changes, we must destroy the reusable context so that a |
| 4647 | * new connection will present a new SNI. As an optimization we could |
| 4648 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 4649 | * server. |
| 4650 | */ |
| 4651 | prev_name = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 4652 | if ((!prev_name && hostname) || |
| 4653 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
| 4654 | SSL_set_session(conn->xprt_ctx, NULL); |
| 4655 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 4656 | SSL_set_tlsext_host_name(conn->xprt_ctx, hostname); |
| 4657 | #endif |
| 4658 | } |
| 4659 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4660 | /* Extract peer certificate's common name into the chunk dest |
| 4661 | * Returns |
| 4662 | * the len of the extracted common name |
| 4663 | * or 0 if no CN found in DN |
| 4664 | * or -1 on error case (i.e. no peer certificate) |
| 4665 | */ |
| 4666 | 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] | 4667 | { |
| 4668 | X509 *crt = NULL; |
| 4669 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4670 | const char find_cn[] = "CN"; |
| 4671 | const struct chunk find_cn_chunk = { |
| 4672 | .str = (char *)&find_cn, |
| 4673 | .len = sizeof(find_cn)-1 |
| 4674 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4675 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4676 | |
| 4677 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4678 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4679 | |
| 4680 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4681 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4682 | if (!crt) |
| 4683 | goto out; |
| 4684 | |
| 4685 | name = X509_get_subject_name(crt); |
| 4686 | if (!name) |
| 4687 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4688 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4689 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 4690 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4691 | if (crt) |
| 4692 | X509_free(crt); |
| 4693 | |
| 4694 | return result; |
| 4695 | } |
| 4696 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 4697 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 4698 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 4699 | { |
| 4700 | X509 *crt = NULL; |
| 4701 | |
| 4702 | if (!ssl_sock_is_ssl(conn)) |
| 4703 | return 0; |
| 4704 | |
| 4705 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4706 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4707 | if (!crt) |
| 4708 | return 0; |
| 4709 | |
| 4710 | X509_free(crt); |
| 4711 | return 1; |
| 4712 | } |
| 4713 | |
| 4714 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 4715 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4716 | { |
| 4717 | if (!ssl_sock_is_ssl(conn)) |
| 4718 | return 0; |
| 4719 | |
| 4720 | return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
| 4721 | } |
| 4722 | |
| 4723 | /* returns result from SSL verify */ |
| 4724 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 4725 | { |
| 4726 | if (!ssl_sock_is_ssl(conn)) |
| 4727 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 4728 | |
| 4729 | return (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
| 4730 | } |
| 4731 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 4732 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 4733 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4734 | /* boolean, returns true if client cert was present */ |
| 4735 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4736 | 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] | 4737 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4738 | struct connection *conn; |
| 4739 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4740 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4741 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4742 | return 0; |
| 4743 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4744 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4745 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4746 | return 0; |
| 4747 | } |
| 4748 | |
| 4749 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4750 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4751 | 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] | 4752 | |
| 4753 | return 1; |
| 4754 | } |
| 4755 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4756 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 4757 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4758 | * should be use. |
| 4759 | */ |
| 4760 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4761 | 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] | 4762 | { |
| 4763 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 4764 | X509 *crt = NULL; |
| 4765 | int ret = 0; |
| 4766 | struct chunk *smp_trash; |
| 4767 | struct connection *conn; |
| 4768 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4769 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4770 | if (!conn || conn->xprt != &ssl_sock) |
| 4771 | return 0; |
| 4772 | |
| 4773 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 4774 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4775 | return 0; |
| 4776 | } |
| 4777 | |
| 4778 | if (cert_peer) |
| 4779 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4780 | else |
| 4781 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 4782 | |
| 4783 | if (!crt) |
| 4784 | goto out; |
| 4785 | |
| 4786 | smp_trash = get_trash_chunk(); |
| 4787 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 4788 | goto out; |
| 4789 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4790 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4791 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4792 | ret = 1; |
| 4793 | out: |
| 4794 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4795 | if (cert_peer && crt) |
| 4796 | X509_free(crt); |
| 4797 | return ret; |
| 4798 | } |
| 4799 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4800 | /* binary, returns serial of certificate in a binary chunk. |
| 4801 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4802 | * should be use. |
| 4803 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4804 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4805 | 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] | 4806 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4807 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4808 | X509 *crt = NULL; |
| 4809 | int ret = 0; |
| 4810 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4811 | struct connection *conn; |
| 4812 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4813 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4814 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4815 | return 0; |
| 4816 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4817 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4818 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4819 | return 0; |
| 4820 | } |
| 4821 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4822 | if (cert_peer) |
| 4823 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4824 | else |
| 4825 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 4826 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4827 | if (!crt) |
| 4828 | goto out; |
| 4829 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4830 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4831 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 4832 | goto out; |
| 4833 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4834 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4835 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4836 | ret = 1; |
| 4837 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4838 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4839 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4840 | X509_free(crt); |
| 4841 | return ret; |
| 4842 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4843 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4844 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 4845 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4846 | * should be use. |
| 4847 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4848 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4849 | 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] | 4850 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4851 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4852 | X509 *crt = NULL; |
| 4853 | const EVP_MD *digest; |
| 4854 | int ret = 0; |
| 4855 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4856 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4857 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4858 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4859 | if (!conn || conn->xprt != &ssl_sock) |
| 4860 | return 0; |
| 4861 | |
| 4862 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4863 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4864 | return 0; |
| 4865 | } |
| 4866 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4867 | if (cert_peer) |
| 4868 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4869 | else |
| 4870 | crt = SSL_get_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4871 | if (!crt) |
| 4872 | goto out; |
| 4873 | |
| 4874 | smp_trash = get_trash_chunk(); |
| 4875 | digest = EVP_sha1(); |
| 4876 | X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len); |
| 4877 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4878 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4879 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4880 | ret = 1; |
| 4881 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4882 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4883 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4884 | X509_free(crt); |
| 4885 | return ret; |
| 4886 | } |
| 4887 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4888 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 4889 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4890 | * should be use. |
| 4891 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4892 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4893 | 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] | 4894 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4895 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4896 | X509 *crt = NULL; |
| 4897 | int ret = 0; |
| 4898 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4899 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4900 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4901 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4902 | if (!conn || conn->xprt != &ssl_sock) |
| 4903 | return 0; |
| 4904 | |
| 4905 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4906 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4907 | return 0; |
| 4908 | } |
| 4909 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4910 | if (cert_peer) |
| 4911 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4912 | else |
| 4913 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4914 | if (!crt) |
| 4915 | goto out; |
| 4916 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4917 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4918 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 4919 | goto out; |
| 4920 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4921 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4922 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4923 | ret = 1; |
| 4924 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4925 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4926 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4927 | X509_free(crt); |
| 4928 | return ret; |
| 4929 | } |
| 4930 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4931 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 4932 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4933 | * should be use. |
| 4934 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4935 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4936 | 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] | 4937 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4938 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4939 | X509 *crt = NULL; |
| 4940 | X509_NAME *name; |
| 4941 | int ret = 0; |
| 4942 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4943 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4944 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4945 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4946 | if (!conn || conn->xprt != &ssl_sock) |
| 4947 | return 0; |
| 4948 | |
| 4949 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4950 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4951 | return 0; |
| 4952 | } |
| 4953 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4954 | if (cert_peer) |
| 4955 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4956 | else |
| 4957 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4958 | if (!crt) |
| 4959 | goto out; |
| 4960 | |
| 4961 | name = X509_get_issuer_name(crt); |
| 4962 | if (!name) |
| 4963 | goto out; |
| 4964 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4965 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4966 | if (args && args[0].type == ARGT_STR) { |
| 4967 | int pos = 1; |
| 4968 | |
| 4969 | if (args[1].type == ARGT_SINT) |
| 4970 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4971 | |
| 4972 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 4973 | goto out; |
| 4974 | } |
| 4975 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 4976 | goto out; |
| 4977 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4978 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4979 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4980 | ret = 1; |
| 4981 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4982 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4983 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4984 | X509_free(crt); |
| 4985 | return ret; |
| 4986 | } |
| 4987 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4988 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 4989 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4990 | * should be use. |
| 4991 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4992 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4993 | 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] | 4994 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4995 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4996 | X509 *crt = NULL; |
| 4997 | int ret = 0; |
| 4998 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4999 | struct connection *conn; |
| 5000 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5001 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5002 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5003 | return 0; |
| 5004 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5005 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5006 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5007 | return 0; |
| 5008 | } |
| 5009 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5010 | if (cert_peer) |
| 5011 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5012 | else |
| 5013 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5014 | if (!crt) |
| 5015 | goto out; |
| 5016 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5017 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5018 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 5019 | goto out; |
| 5020 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5021 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5022 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5023 | ret = 1; |
| 5024 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5025 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5026 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 5027 | X509_free(crt); |
| 5028 | return ret; |
| 5029 | } |
| 5030 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5031 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 5032 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5033 | * should be use. |
| 5034 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5035 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5036 | 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] | 5037 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5038 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5039 | X509 *crt = NULL; |
| 5040 | X509_NAME *name; |
| 5041 | int ret = 0; |
| 5042 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5043 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5044 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5045 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5046 | if (!conn || conn->xprt != &ssl_sock) |
| 5047 | return 0; |
| 5048 | |
| 5049 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5050 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5051 | return 0; |
| 5052 | } |
| 5053 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5054 | if (cert_peer) |
| 5055 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5056 | else |
| 5057 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5058 | if (!crt) |
| 5059 | goto out; |
| 5060 | |
| 5061 | name = X509_get_subject_name(crt); |
| 5062 | if (!name) |
| 5063 | goto out; |
| 5064 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 5065 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5066 | if (args && args[0].type == ARGT_STR) { |
| 5067 | int pos = 1; |
| 5068 | |
| 5069 | if (args[1].type == ARGT_SINT) |
| 5070 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5071 | |
| 5072 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 5073 | goto out; |
| 5074 | } |
| 5075 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 5076 | goto out; |
| 5077 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5078 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5079 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5080 | ret = 1; |
| 5081 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5082 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 5083 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 5084 | X509_free(crt); |
| 5085 | return ret; |
| 5086 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5087 | |
| 5088 | /* integer, returns true if current session use a client certificate */ |
| 5089 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5090 | 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] | 5091 | { |
| 5092 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5093 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5094 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5095 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5096 | if (!conn || conn->xprt != &ssl_sock) |
| 5097 | return 0; |
| 5098 | |
| 5099 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5100 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5101 | return 0; |
| 5102 | } |
| 5103 | |
| 5104 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5105 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5106 | if (crt) { |
| 5107 | X509_free(crt); |
| 5108 | } |
| 5109 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5110 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5111 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 5112 | return 1; |
| 5113 | } |
| 5114 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5115 | /* integer, returns the certificate version |
| 5116 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5117 | * should be use. |
| 5118 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5119 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5120 | 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] | 5121 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5122 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5123 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5124 | struct connection *conn; |
| 5125 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5126 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5127 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5128 | return 0; |
| 5129 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5130 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5131 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5132 | return 0; |
| 5133 | } |
| 5134 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5135 | if (cert_peer) |
| 5136 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5137 | else |
| 5138 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5139 | if (!crt) |
| 5140 | return 0; |
| 5141 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5142 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5143 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5144 | if (cert_peer) |
| 5145 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5146 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 5147 | |
| 5148 | return 1; |
| 5149 | } |
| 5150 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5151 | /* string, returns the certificate's signature algorithm. |
| 5152 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5153 | * should be use. |
| 5154 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5155 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5156 | 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] | 5157 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5158 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5159 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5160 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5161 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5162 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5163 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5164 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5165 | if (!conn || conn->xprt != &ssl_sock) |
| 5166 | return 0; |
| 5167 | |
| 5168 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5169 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5170 | return 0; |
| 5171 | } |
| 5172 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5173 | if (cert_peer) |
| 5174 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5175 | else |
| 5176 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5177 | if (!crt) |
| 5178 | return 0; |
| 5179 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5180 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 5181 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5182 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5183 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 5184 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5185 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5186 | if (cert_peer) |
| 5187 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5188 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 5189 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5190 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5191 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5192 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5193 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5194 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5195 | if (cert_peer) |
| 5196 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 5197 | |
| 5198 | return 1; |
| 5199 | } |
| 5200 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5201 | /* string, returns the certificate's key algorithm. |
| 5202 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 5203 | * should be use. |
| 5204 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5205 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5206 | 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] | 5207 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5208 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5209 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5210 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5211 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5212 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5213 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5214 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5215 | if (!conn || conn->xprt != &ssl_sock) |
| 5216 | return 0; |
| 5217 | |
| 5218 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5219 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5220 | return 0; |
| 5221 | } |
| 5222 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5223 | if (cert_peer) |
| 5224 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 5225 | else |
| 5226 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5227 | if (!crt) |
| 5228 | return 0; |
| 5229 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5230 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 5231 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5232 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5233 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 5234 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5235 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 5236 | if (cert_peer) |
| 5237 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5238 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 5239 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5240 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5241 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5242 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5243 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 5244 | if (cert_peer) |
| 5245 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 5246 | |
| 5247 | return 1; |
| 5248 | } |
| 5249 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5250 | /* boolean, returns true if front conn. transport layer is SSL. |
| 5251 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5252 | * char is 'b'. |
| 5253 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5254 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5255 | 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] | 5256 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5257 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5258 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5259 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5260 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5261 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5262 | return 1; |
| 5263 | } |
| 5264 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5265 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5266 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5267 | 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] | 5268 | { |
| 5269 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5270 | struct connection *conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5271 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5272 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5273 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5274 | conn->xprt_ctx && |
| 5275 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5276 | return 1; |
| 5277 | #else |
| 5278 | return 0; |
| 5279 | #endif |
| 5280 | } |
| 5281 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 5282 | /* boolean, returns true if client session has been resumed */ |
| 5283 | static int |
| 5284 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 5285 | { |
| 5286 | struct connection *conn = objt_conn(smp->sess->origin); |
| 5287 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5288 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5289 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 5290 | conn->xprt_ctx && |
| 5291 | SSL_session_reused(conn->xprt_ctx); |
| 5292 | return 1; |
| 5293 | } |
| 5294 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5295 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 5296 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5297 | * char is 'b'. |
| 5298 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5299 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5300 | 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] | 5301 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5302 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5303 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5304 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5305 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5306 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5307 | return 0; |
| 5308 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5309 | smp->data.u.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
| 5310 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5311 | return 0; |
| 5312 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5313 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5314 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5315 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5316 | |
| 5317 | return 1; |
| 5318 | } |
| 5319 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5320 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 5321 | * is SSL. |
| 5322 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5323 | * char is 'b'. |
| 5324 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5325 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5326 | 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] | 5327 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5328 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5329 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5330 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5331 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5332 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5333 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5334 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5335 | return 0; |
| 5336 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 5337 | if (!SSL_get_cipher_bits(conn->xprt_ctx, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5338 | return 0; |
| 5339 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5340 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5341 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5342 | |
| 5343 | return 1; |
| 5344 | } |
| 5345 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5346 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 5347 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5348 | * char is 'b'. |
| 5349 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5350 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5351 | 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] | 5352 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5353 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5354 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5355 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5356 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5357 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5358 | return 0; |
| 5359 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5360 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
| 5361 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5362 | return 0; |
| 5363 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5364 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5365 | |
| 5366 | return 1; |
| 5367 | } |
| 5368 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5369 | #ifdef OPENSSL_NPN_NEGOTIATED |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5370 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5371 | 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] | 5372 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5373 | struct connection *conn; |
| 5374 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5375 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5376 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5377 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5378 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5379 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5380 | return 0; |
| 5381 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5382 | smp->data.u.str.str = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5383 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5384 | (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] | 5385 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5386 | if (!smp->data.u.str.str) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5387 | return 0; |
| 5388 | |
| 5389 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5390 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5391 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5392 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5393 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5394 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5395 | 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] | 5396 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5397 | struct connection *conn; |
| 5398 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5399 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5400 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5401 | |
Willy Tarreau | e26bf05 | 2015-05-12 10:30:12 +0200 | [diff] [blame] | 5402 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5403 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5404 | return 0; |
| 5405 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5406 | smp->data.u.str.str = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5407 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5408 | (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] | 5409 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5410 | if (!smp->data.u.str.str) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5411 | return 0; |
| 5412 | |
| 5413 | return 1; |
| 5414 | } |
| 5415 | #endif |
| 5416 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5417 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 5418 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5419 | * char is 'b'. |
| 5420 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 5421 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5422 | 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] | 5423 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5424 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5425 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5426 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5427 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5428 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5429 | return 0; |
| 5430 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5431 | smp->data.u.str.str = (char *)SSL_get_version(conn->xprt_ctx); |
| 5432 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5433 | return 0; |
| 5434 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5435 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5436 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5437 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5438 | |
| 5439 | return 1; |
| 5440 | } |
| 5441 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5442 | /* 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] | 5443 | * This function is also usable on backend conn if the fetch keyword 5th |
| 5444 | * char is 'b'. |
| 5445 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 5446 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5447 | 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] | 5448 | { |
| 5449 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5450 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5451 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 5452 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5453 | SSL_SESSION *ssl_sess; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5454 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5455 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5456 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 5457 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5458 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5459 | return 0; |
| 5460 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5461 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 5462 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 5463 | return 0; |
| 5464 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5465 | smp->data.u.str.str = (char *)SSL_SESSION_get_id(ssl_sess, (unsigned int *)&smp->data.u.str.len); |
| 5466 | if (!smp->data.u.str.str || !smp->data.u.str.len) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 5467 | return 0; |
| 5468 | |
| 5469 | return 1; |
| 5470 | #else |
| 5471 | return 0; |
| 5472 | #endif |
| 5473 | } |
| 5474 | |
| 5475 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5476 | 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] | 5477 | { |
| 5478 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5479 | struct connection *conn; |
| 5480 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5481 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5482 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5483 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5484 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5485 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5486 | return 0; |
| 5487 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5488 | smp->data.u.str.str = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 5489 | if (!smp->data.u.str.str) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 5490 | return 0; |
| 5491 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5492 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5493 | return 1; |
| 5494 | #else |
| 5495 | return 0; |
| 5496 | #endif |
| 5497 | } |
| 5498 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5499 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5500 | 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] | 5501 | { |
| 5502 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 5503 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 5504 | smp->strm ? smp->strm->si[1].end : NULL); |
| 5505 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5506 | int finished_len; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5507 | struct chunk *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5508 | |
| 5509 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5510 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 5511 | return 0; |
| 5512 | |
| 5513 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 5514 | smp->flags |= SMP_F_MAY_CHANGE; |
| 5515 | return 0; |
| 5516 | } |
| 5517 | |
| 5518 | finished_trash = get_trash_chunk(); |
| 5519 | if (!SSL_session_reused(conn->xprt_ctx)) |
| 5520 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 5521 | else |
| 5522 | finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 5523 | |
| 5524 | if (!finished_len) |
| 5525 | return 0; |
| 5526 | |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 5527 | finished_trash->len = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5528 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5529 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 5530 | |
| 5531 | return 1; |
| 5532 | #else |
| 5533 | return 0; |
| 5534 | #endif |
| 5535 | } |
| 5536 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5537 | /* 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] | 5538 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5539 | 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] | 5540 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5541 | struct connection *conn; |
| 5542 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5543 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5544 | if (!conn || conn->xprt != &ssl_sock) |
| 5545 | return 0; |
| 5546 | |
| 5547 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5548 | smp->flags = SMP_F_MAY_CHANGE; |
| 5549 | return 0; |
| 5550 | } |
| 5551 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5552 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5553 | 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] | 5554 | smp->flags = 0; |
| 5555 | |
| 5556 | return 1; |
| 5557 | } |
| 5558 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5559 | /* 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] | 5560 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5561 | 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] | 5562 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5563 | struct connection *conn; |
| 5564 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5565 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5566 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5567 | return 0; |
| 5568 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5569 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5570 | smp->flags = SMP_F_MAY_CHANGE; |
| 5571 | return 0; |
| 5572 | } |
| 5573 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5574 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5575 | 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] | 5576 | smp->flags = 0; |
| 5577 | |
| 5578 | return 1; |
| 5579 | } |
| 5580 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5581 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5582 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5583 | 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] | 5584 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5585 | struct connection *conn; |
| 5586 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5587 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5588 | if (!conn || conn->xprt != &ssl_sock) |
| 5589 | return 0; |
| 5590 | |
| 5591 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 5592 | smp->flags = SMP_F_MAY_CHANGE; |
| 5593 | return 0; |
| 5594 | } |
| 5595 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5596 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5597 | 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] | 5598 | smp->flags = 0; |
| 5599 | |
| 5600 | return 1; |
| 5601 | } |
| 5602 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 5603 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 5604 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5605 | 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] | 5606 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5607 | struct connection *conn; |
| 5608 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5609 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5610 | if (!conn || conn->xprt != &ssl_sock) |
| 5611 | return 0; |
| 5612 | |
| 5613 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 5614 | smp->flags = SMP_F_MAY_CHANGE; |
| 5615 | return 0; |
| 5616 | } |
| 5617 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 5618 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 5619 | return 0; |
| 5620 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 5621 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 5622 | 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] | 5623 | smp->flags = 0; |
| 5624 | |
| 5625 | return 1; |
| 5626 | } |
| 5627 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 5628 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5629 | 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] | 5630 | { |
| 5631 | if (!*args[cur_arg + 1]) { |
| 5632 | if (err) |
| 5633 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 5634 | return ERR_ALERT | ERR_FATAL; |
| 5635 | } |
| 5636 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5637 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 5638 | 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] | 5639 | else |
| 5640 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5641 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5642 | return 0; |
| 5643 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5644 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5645 | { |
| 5646 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 5647 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5648 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5649 | /* parse the "ca-sign-file" bind keyword */ |
| 5650 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5651 | { |
| 5652 | if (!*args[cur_arg + 1]) { |
| 5653 | if (err) |
| 5654 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 5655 | return ERR_ALERT | ERR_FATAL; |
| 5656 | } |
| 5657 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5658 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 5659 | 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] | 5660 | else |
| 5661 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 5662 | |
| 5663 | return 0; |
| 5664 | } |
| 5665 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 5666 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5667 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5668 | { |
| 5669 | if (!*args[cur_arg + 1]) { |
| 5670 | if (err) |
| 5671 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 5672 | return ERR_ALERT | ERR_FATAL; |
| 5673 | } |
| 5674 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 5675 | return 0; |
| 5676 | } |
| 5677 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5678 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5679 | 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] | 5680 | { |
| 5681 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 5682 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5683 | return ERR_ALERT | ERR_FATAL; |
| 5684 | } |
| 5685 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 5686 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5687 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5688 | return 0; |
| 5689 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5690 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5691 | { |
| 5692 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 5693 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5694 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5695 | 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] | 5696 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 5697 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 5698 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5699 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 5700 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5701 | return ERR_ALERT | ERR_FATAL; |
| 5702 | } |
| 5703 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5704 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 5705 | 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] | 5706 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 5707 | return ERR_ALERT | ERR_FATAL; |
| 5708 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5709 | 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] | 5710 | if (ssl_sock_load_cert(path, conf, err) > 0) |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5711 | return ERR_ALERT | ERR_FATAL; |
| 5712 | |
| 5713 | return 0; |
| 5714 | } |
| 5715 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5716 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5717 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5718 | |
| 5719 | return 0; |
| 5720 | } |
| 5721 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5722 | /* parse the "crt-list" bind keyword */ |
| 5723 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5724 | { |
| 5725 | if (!*args[cur_arg + 1]) { |
| 5726 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 5727 | return ERR_ALERT | ERR_FATAL; |
| 5728 | } |
| 5729 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5730 | 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] | 5731 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5732 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 5733 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5734 | |
| 5735 | return 0; |
| 5736 | } |
| 5737 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 5738 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5739 | 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] | 5740 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5741 | #ifndef X509_V_FLAG_CRL_CHECK |
| 5742 | if (err) |
| 5743 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 5744 | return ERR_ALERT | ERR_FATAL; |
| 5745 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5746 | if (!*args[cur_arg + 1]) { |
| 5747 | if (err) |
| 5748 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 5749 | return ERR_ALERT | ERR_FATAL; |
| 5750 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5751 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5752 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 5753 | 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] | 5754 | else |
| 5755 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5756 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5757 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5758 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5759 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5760 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5761 | { |
| 5762 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 5763 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5764 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5765 | /* parse the "curves" bind keyword keyword */ |
| 5766 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 5767 | { |
| 5768 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 5769 | if (!*args[cur_arg + 1]) { |
| 5770 | if (err) |
| 5771 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 5772 | return ERR_ALERT | ERR_FATAL; |
| 5773 | } |
| 5774 | conf->curves = strdup(args[cur_arg + 1]); |
| 5775 | return 0; |
| 5776 | #else |
| 5777 | if (err) |
| 5778 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 5779 | return ERR_ALERT | ERR_FATAL; |
| 5780 | #endif |
| 5781 | } |
| 5782 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5783 | { |
| 5784 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 5785 | } |
| 5786 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 5787 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5788 | 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] | 5789 | { |
| 5790 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 5791 | if (err) |
| 5792 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 5793 | return ERR_ALERT | ERR_FATAL; |
| 5794 | #elif defined(OPENSSL_NO_ECDH) |
| 5795 | if (err) |
| 5796 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 5797 | return ERR_ALERT | ERR_FATAL; |
| 5798 | #else |
| 5799 | if (!*args[cur_arg + 1]) { |
| 5800 | if (err) |
| 5801 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 5802 | return ERR_ALERT | ERR_FATAL; |
| 5803 | } |
| 5804 | |
| 5805 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5806 | |
| 5807 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5808 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5809 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5810 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5811 | { |
| 5812 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 5813 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5814 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 5815 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 5816 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5817 | { |
| 5818 | int code; |
| 5819 | char *p = args[cur_arg + 1]; |
| 5820 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 5821 | |
| 5822 | if (!*p) { |
| 5823 | if (err) |
| 5824 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 5825 | return ERR_ALERT | ERR_FATAL; |
| 5826 | } |
| 5827 | |
| 5828 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 5829 | ignerr = &conf->ca_ignerr; |
| 5830 | |
| 5831 | if (strcmp(p, "all") == 0) { |
| 5832 | *ignerr = ~0ULL; |
| 5833 | return 0; |
| 5834 | } |
| 5835 | |
| 5836 | while (p) { |
| 5837 | code = atoi(p); |
| 5838 | if ((code <= 0) || (code > 63)) { |
| 5839 | if (err) |
| 5840 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 5841 | args[cur_arg], code, args[cur_arg + 1]); |
| 5842 | return ERR_ALERT | ERR_FATAL; |
| 5843 | } |
| 5844 | *ignerr |= 1ULL << code; |
| 5845 | p = strchr(p, ','); |
| 5846 | if (p) |
| 5847 | p++; |
| 5848 | } |
| 5849 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5850 | return 0; |
| 5851 | } |
| 5852 | |
| 5853 | /* parse the "force-sslv3" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5854 | 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] | 5855 | { |
| 5856 | conf->ssl_options |= BC_SSL_O_USE_SSLV3; |
| 5857 | return 0; |
| 5858 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5859 | static int bind_parse_force_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5860 | { |
| 5861 | return ssl_bind_parse_force_sslv3(args, cur_arg, px, &conf->ssl_conf, err); |
| 5862 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5863 | |
| 5864 | /* parse the "force-tlsv10" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5865 | 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] | 5866 | { |
| 5867 | conf->ssl_options |= BC_SSL_O_USE_TLSV10; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 5868 | return 0; |
| 5869 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5870 | static int bind_parse_force_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5871 | { |
| 5872 | return ssl_bind_parse_force_tlsv10(args, cur_arg, px, &conf->ssl_conf, err); |
| 5873 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 5874 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5875 | /* parse the "force-tlsv11" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5876 | 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] | 5877 | { |
| 5878 | #if SSL_OP_NO_TLSv1_1 |
| 5879 | conf->ssl_options |= BC_SSL_O_USE_TLSV11; |
| 5880 | return 0; |
| 5881 | #else |
| 5882 | if (err) |
| 5883 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[cur_arg]); |
| 5884 | return ERR_ALERT | ERR_FATAL; |
| 5885 | #endif |
| 5886 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5887 | static int bind_parse_force_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5888 | { |
| 5889 | return ssl_bind_parse_force_tlsv11(args, cur_arg, px, &conf->ssl_conf, err); |
| 5890 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5891 | |
| 5892 | /* parse the "force-tlsv12" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5893 | 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] | 5894 | { |
| 5895 | #if SSL_OP_NO_TLSv1_2 |
| 5896 | conf->ssl_options |= BC_SSL_O_USE_TLSV12; |
| 5897 | return 0; |
| 5898 | #else |
| 5899 | if (err) |
| 5900 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[cur_arg]); |
| 5901 | return ERR_ALERT | ERR_FATAL; |
| 5902 | #endif |
| 5903 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5904 | static int bind_parse_force_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5905 | { |
| 5906 | return ssl_bind_parse_force_tlsv12(args, cur_arg, px, &conf->ssl_conf, err); |
| 5907 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5908 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 5909 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5910 | 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] | 5911 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5912 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 5913 | return 0; |
| 5914 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5915 | static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5916 | { |
| 5917 | return ssl_bind_parse_no_tls_tickets(args, cur_arg, px, &conf->ssl_conf, err); |
| 5918 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 5919 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 5920 | /* parse the "no-sslv3" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5921 | 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] | 5922 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5923 | conf->ssl_options |= BC_SSL_O_NO_SSLV3; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5924 | return 0; |
| 5925 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5926 | static int bind_parse_no_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5927 | { |
| 5928 | return ssl_bind_parse_no_sslv3(args, cur_arg, px, &conf->ssl_conf, err); |
| 5929 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5930 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 5931 | /* parse the "no-tlsv10" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5932 | 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] | 5933 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5934 | conf->ssl_options |= BC_SSL_O_NO_TLSV10; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5935 | return 0; |
| 5936 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5937 | static int bind_parse_no_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5938 | { |
| 5939 | return ssl_bind_parse_no_tlsv10(args, cur_arg, px, &conf->ssl_conf, err); |
| 5940 | } |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5941 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 5942 | /* parse the "no-tlsv11" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5943 | 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] | 5944 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5945 | conf->ssl_options |= BC_SSL_O_NO_TLSV11; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5946 | return 0; |
| 5947 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5948 | static int bind_parse_no_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5949 | { |
| 5950 | return ssl_bind_parse_no_tlsv11(args, cur_arg, px, &conf->ssl_conf, err); |
| 5951 | } |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5952 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 5953 | /* parse the "no-tlsv12" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5954 | 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] | 5955 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5956 | conf->ssl_options |= BC_SSL_O_NO_TLSV12; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5957 | return 0; |
| 5958 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5959 | static int bind_parse_no_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5960 | { |
| 5961 | return ssl_bind_parse_no_tlsv12(args, cur_arg, px, &conf->ssl_conf, err); |
| 5962 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5963 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5964 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5965 | 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] | 5966 | { |
| 5967 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 5968 | char *p1, *p2; |
| 5969 | |
| 5970 | if (!*args[cur_arg + 1]) { |
| 5971 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 5972 | return ERR_ALERT | ERR_FATAL; |
| 5973 | } |
| 5974 | |
| 5975 | free(conf->npn_str); |
| 5976 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 5977 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 5978 | * so we reuse each comma to store the next <len> and need |
| 5979 | * one more for the end of the string. |
| 5980 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5981 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 5982 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5983 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 5984 | |
| 5985 | /* replace commas with the name length */ |
| 5986 | p1 = conf->npn_str; |
| 5987 | p2 = p1 + 1; |
| 5988 | while (1) { |
| 5989 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 5990 | if (!p2) |
| 5991 | p2 = p1 + 1 + strlen(p1 + 1); |
| 5992 | |
| 5993 | if (p2 - (p1 + 1) > 255) { |
| 5994 | *p2 = '\0'; |
| 5995 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 5996 | return ERR_ALERT | ERR_FATAL; |
| 5997 | } |
| 5998 | |
| 5999 | *p1 = p2 - (p1 + 1); |
| 6000 | p1 = p2; |
| 6001 | |
| 6002 | if (!*p2) |
| 6003 | break; |
| 6004 | |
| 6005 | *(p2++) = '\0'; |
| 6006 | } |
| 6007 | return 0; |
| 6008 | #else |
| 6009 | if (err) |
| 6010 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 6011 | return ERR_ALERT | ERR_FATAL; |
| 6012 | #endif |
| 6013 | } |
| 6014 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6015 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6016 | { |
| 6017 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 6018 | } |
| 6019 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6020 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6021 | 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] | 6022 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 6023 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6024 | char *p1, *p2; |
| 6025 | |
| 6026 | if (!*args[cur_arg + 1]) { |
| 6027 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 6028 | return ERR_ALERT | ERR_FATAL; |
| 6029 | } |
| 6030 | |
| 6031 | free(conf->alpn_str); |
| 6032 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 6033 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 6034 | * so we reuse each comma to store the next <len> and need |
| 6035 | * one more for the end of the string. |
| 6036 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6037 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 6038 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 6039 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 6040 | |
| 6041 | /* replace commas with the name length */ |
| 6042 | p1 = conf->alpn_str; |
| 6043 | p2 = p1 + 1; |
| 6044 | while (1) { |
| 6045 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 6046 | if (!p2) |
| 6047 | p2 = p1 + 1 + strlen(p1 + 1); |
| 6048 | |
| 6049 | if (p2 - (p1 + 1) > 255) { |
| 6050 | *p2 = '\0'; |
| 6051 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 6052 | return ERR_ALERT | ERR_FATAL; |
| 6053 | } |
| 6054 | |
| 6055 | *p1 = p2 - (p1 + 1); |
| 6056 | p1 = p2; |
| 6057 | |
| 6058 | if (!*p2) |
| 6059 | break; |
| 6060 | |
| 6061 | *(p2++) = '\0'; |
| 6062 | } |
| 6063 | return 0; |
| 6064 | #else |
| 6065 | if (err) |
| 6066 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 6067 | return ERR_ALERT | ERR_FATAL; |
| 6068 | #endif |
| 6069 | } |
| 6070 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6071 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6072 | { |
| 6073 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 6074 | } |
| 6075 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6076 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6077 | 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] | 6078 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 6079 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 6080 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 6081 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6082 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 6083 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
| 6084 | conf->ssl_conf.ssl_options |= global_ssl.listen_default_ssloptions; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 6085 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6086 | return 0; |
| 6087 | } |
| 6088 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6089 | /* parse the "generate-certificates" bind keyword */ |
| 6090 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6091 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6092 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6093 | conf->generate_certs = 1; |
| 6094 | #else |
| 6095 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 6096 | err && *err ? *err : ""); |
| 6097 | #endif |
| 6098 | return 0; |
| 6099 | } |
| 6100 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 6101 | /* parse the "strict-sni" bind keyword */ |
| 6102 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6103 | { |
| 6104 | conf->strict_sni = 1; |
| 6105 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6106 | } |
| 6107 | |
| 6108 | /* parse the "tls-ticket-keys" bind keyword */ |
| 6109 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6110 | { |
| 6111 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6112 | FILE *f; |
| 6113 | int i = 0; |
| 6114 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6115 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6116 | |
| 6117 | if (!*args[cur_arg + 1]) { |
| 6118 | if (err) |
| 6119 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 6120 | return ERR_ALERT | ERR_FATAL; |
| 6121 | } |
| 6122 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 6123 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
| 6124 | if(keys_ref) { |
| 6125 | conf->keys_ref = keys_ref; |
| 6126 | return 0; |
| 6127 | } |
| 6128 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 6129 | keys_ref = malloc(sizeof(*keys_ref)); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6130 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct tls_sess_key)); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6131 | |
| 6132 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 6133 | if (err) |
| 6134 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 6135 | return ERR_ALERT | ERR_FATAL; |
| 6136 | } |
| 6137 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6138 | keys_ref->filename = strdup(args[cur_arg + 1]); |
| 6139 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6140 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 6141 | int len = strlen(thisline); |
| 6142 | /* Strip newline characters from the end */ |
| 6143 | if(thisline[len - 1] == '\n') |
| 6144 | thisline[--len] = 0; |
| 6145 | |
| 6146 | if(thisline[len - 1] == '\r') |
| 6147 | thisline[--len] = 0; |
| 6148 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6149 | 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] | 6150 | if (err) |
| 6151 | 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] | 6152 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6153 | return ERR_ALERT | ERR_FATAL; |
| 6154 | } |
| 6155 | i++; |
| 6156 | } |
| 6157 | |
| 6158 | if (i < TLS_TICKETS_NO) { |
| 6159 | if (err) |
| 6160 | 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] | 6161 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6162 | return ERR_ALERT | ERR_FATAL; |
| 6163 | } |
| 6164 | |
| 6165 | fclose(f); |
| 6166 | |
| 6167 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 6168 | i -= 2; |
| 6169 | 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] | 6170 | keys_ref->unique_id = -1; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 6171 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6172 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 6173 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 6174 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 6175 | return 0; |
| 6176 | #else |
| 6177 | if (err) |
| 6178 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 6179 | return ERR_ALERT | ERR_FATAL; |
| 6180 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 6181 | } |
| 6182 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6183 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6184 | 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] | 6185 | { |
| 6186 | if (!*args[cur_arg + 1]) { |
| 6187 | if (err) |
| 6188 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 6189 | return ERR_ALERT | ERR_FATAL; |
| 6190 | } |
| 6191 | |
| 6192 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6193 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6194 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6195 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6196 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6197 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6198 | else { |
| 6199 | if (err) |
| 6200 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 6201 | args[cur_arg], args[cur_arg + 1]); |
| 6202 | return ERR_ALERT | ERR_FATAL; |
| 6203 | } |
| 6204 | |
| 6205 | return 0; |
| 6206 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 6207 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 6208 | { |
| 6209 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 6210 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 6211 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6212 | /************** "server" keywords ****************/ |
| 6213 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6214 | /* parse the "ca-file" server keyword */ |
| 6215 | static int srv_parse_ca_file(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 CAfile path", args[*cur_arg]); |
| 6220 | return ERR_ALERT | ERR_FATAL; |
| 6221 | } |
| 6222 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6223 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6224 | 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] | 6225 | else |
| 6226 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 6227 | |
| 6228 | return 0; |
| 6229 | } |
| 6230 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6231 | /* parse the "check-ssl" server keyword */ |
| 6232 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6233 | { |
| 6234 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6235 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 6236 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
| 6237 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6238 | return 0; |
| 6239 | } |
| 6240 | |
| 6241 | /* parse the "ciphers" server keyword */ |
| 6242 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6243 | { |
| 6244 | if (!*args[*cur_arg + 1]) { |
| 6245 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 6246 | return ERR_ALERT | ERR_FATAL; |
| 6247 | } |
| 6248 | |
| 6249 | free(newsrv->ssl_ctx.ciphers); |
| 6250 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 6251 | return 0; |
| 6252 | } |
| 6253 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6254 | /* parse the "crl-file" server keyword */ |
| 6255 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6256 | { |
| 6257 | #ifndef X509_V_FLAG_CRL_CHECK |
| 6258 | if (err) |
| 6259 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 6260 | return ERR_ALERT | ERR_FATAL; |
| 6261 | #else |
| 6262 | if (!*args[*cur_arg + 1]) { |
| 6263 | if (err) |
| 6264 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 6265 | return ERR_ALERT | ERR_FATAL; |
| 6266 | } |
| 6267 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6268 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 6269 | 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] | 6270 | else |
| 6271 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 6272 | |
| 6273 | return 0; |
| 6274 | #endif |
| 6275 | } |
| 6276 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 6277 | /* parse the "crt" server keyword */ |
| 6278 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6279 | { |
| 6280 | if (!*args[*cur_arg + 1]) { |
| 6281 | if (err) |
| 6282 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 6283 | return ERR_ALERT | ERR_FATAL; |
| 6284 | } |
| 6285 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6286 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
| 6287 | 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] | 6288 | else |
| 6289 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 6290 | |
| 6291 | return 0; |
| 6292 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6293 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6294 | /* parse the "force-sslv3" server keyword */ |
| 6295 | static int srv_parse_force_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6296 | { |
| 6297 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_SSLV3; |
| 6298 | return 0; |
| 6299 | } |
| 6300 | |
| 6301 | /* parse the "force-tlsv10" server keyword */ |
| 6302 | static int srv_parse_force_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6303 | { |
| 6304 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV10; |
| 6305 | return 0; |
| 6306 | } |
| 6307 | |
| 6308 | /* parse the "force-tlsv11" server keyword */ |
| 6309 | static int srv_parse_force_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6310 | { |
| 6311 | #if SSL_OP_NO_TLSv1_1 |
| 6312 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV11; |
| 6313 | return 0; |
| 6314 | #else |
| 6315 | if (err) |
| 6316 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[*cur_arg]); |
| 6317 | return ERR_ALERT | ERR_FATAL; |
| 6318 | #endif |
| 6319 | } |
| 6320 | |
| 6321 | /* parse the "force-tlsv12" server keyword */ |
| 6322 | static int srv_parse_force_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6323 | { |
| 6324 | #if SSL_OP_NO_TLSv1_2 |
| 6325 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV12; |
| 6326 | return 0; |
| 6327 | #else |
| 6328 | if (err) |
| 6329 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[*cur_arg]); |
| 6330 | return ERR_ALERT | ERR_FATAL; |
| 6331 | #endif |
| 6332 | } |
| 6333 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 6334 | /* parse the "no-ssl-reuse" server keyword */ |
| 6335 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6336 | { |
| 6337 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 6338 | return 0; |
| 6339 | } |
| 6340 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6341 | /* parse the "no-sslv3" server keyword */ |
| 6342 | static int srv_parse_no_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6343 | { |
| 6344 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_SSLV3; |
| 6345 | return 0; |
| 6346 | } |
| 6347 | |
| 6348 | /* parse the "no-tlsv10" server keyword */ |
| 6349 | static int srv_parse_no_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6350 | { |
| 6351 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV10; |
| 6352 | return 0; |
| 6353 | } |
| 6354 | |
| 6355 | /* parse the "no-tlsv11" server keyword */ |
| 6356 | static int srv_parse_no_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6357 | { |
| 6358 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV11; |
| 6359 | return 0; |
| 6360 | } |
| 6361 | |
| 6362 | /* parse the "no-tlsv12" server keyword */ |
| 6363 | static int srv_parse_no_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6364 | { |
| 6365 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV12; |
| 6366 | return 0; |
| 6367 | } |
| 6368 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 6369 | /* parse the "no-tls-tickets" server keyword */ |
| 6370 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6371 | { |
| 6372 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 6373 | return 0; |
| 6374 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6375 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 6376 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6377 | { |
| 6378 | newsrv->pp_opts |= SRV_PP_V2; |
| 6379 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 6380 | return 0; |
| 6381 | } |
| 6382 | |
| 6383 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 6384 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6385 | { |
| 6386 | newsrv->pp_opts |= SRV_PP_V2; |
| 6387 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 6388 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 6389 | return 0; |
| 6390 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 6391 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6392 | /* parse the "sni" server keyword */ |
| 6393 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6394 | { |
| 6395 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 6396 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 6397 | return ERR_ALERT | ERR_FATAL; |
| 6398 | #else |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 6399 | int idx; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6400 | struct sample_expr *expr; |
| 6401 | |
| 6402 | if (!*args[*cur_arg + 1]) { |
| 6403 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 6404 | return ERR_ALERT | ERR_FATAL; |
| 6405 | } |
| 6406 | |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 6407 | idx = (*cur_arg) + 1; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6408 | proxy->conf.args.ctx = ARGC_SRV; |
| 6409 | |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 6410 | 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] | 6411 | if (!expr) { |
| 6412 | memprintf(err, "error detected while parsing sni expression : %s", *err); |
| 6413 | return ERR_ALERT | ERR_FATAL; |
| 6414 | } |
| 6415 | |
| 6416 | if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) { |
| 6417 | memprintf(err, "error detected while parsing sni expression : " |
| 6418 | " 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] | 6419 | args[idx-1], sample_src_names(expr->fetch->use)); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 6420 | return ERR_ALERT | ERR_FATAL; |
| 6421 | } |
| 6422 | |
| 6423 | px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 6424 | newsrv->ssl_ctx.sni = expr; |
| 6425 | return 0; |
| 6426 | #endif |
| 6427 | } |
| 6428 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6429 | /* parse the "ssl" server keyword */ |
| 6430 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6431 | { |
| 6432 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6433 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 6434 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6435 | return 0; |
| 6436 | } |
| 6437 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6438 | /* parse the "verify" server keyword */ |
| 6439 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6440 | { |
| 6441 | if (!*args[*cur_arg + 1]) { |
| 6442 | if (err) |
| 6443 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 6444 | return ERR_ALERT | ERR_FATAL; |
| 6445 | } |
| 6446 | |
| 6447 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6448 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6449 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 6450 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6451 | else { |
| 6452 | if (err) |
| 6453 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 6454 | args[*cur_arg], args[*cur_arg + 1]); |
| 6455 | return ERR_ALERT | ERR_FATAL; |
| 6456 | } |
| 6457 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 6458 | return 0; |
| 6459 | } |
| 6460 | |
| 6461 | /* parse the "verifyhost" server keyword */ |
| 6462 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 6463 | { |
| 6464 | if (!*args[*cur_arg + 1]) { |
| 6465 | if (err) |
| 6466 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 6467 | return ERR_ALERT | ERR_FATAL; |
| 6468 | } |
| 6469 | |
| 6470 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 6471 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 6472 | return 0; |
| 6473 | } |
| 6474 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6475 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 6476 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 6477 | struct proxy *defpx, const char *file, int line, |
| 6478 | char **err) { |
| 6479 | int i = 1; |
| 6480 | |
| 6481 | if (*(args[i]) == 0) { |
| 6482 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 6483 | return -1; |
| 6484 | } |
| 6485 | while (*(args[i])) { |
| 6486 | if (!strcmp(args[i], "no-sslv3")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6487 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_SSLV3; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6488 | else if (!strcmp(args[i], "no-tlsv10")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6489 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLSV10; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6490 | else if (!strcmp(args[i], "no-tlsv11")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6491 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLSV11; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6492 | else if (!strcmp(args[i], "no-tlsv12")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6493 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLSV12; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6494 | else if (!strcmp(args[i], "force-sslv3")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6495 | global_ssl.listen_default_ssloptions |= BC_SSL_O_USE_SSLV3; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6496 | else if (!strcmp(args[i], "force-tlsv10")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6497 | global_ssl.listen_default_ssloptions |= BC_SSL_O_USE_TLSV10; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6498 | else if (!strcmp(args[i], "force-tlsv11")) { |
| 6499 | #if SSL_OP_NO_TLSv1_1 |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6500 | global_ssl.listen_default_ssloptions |= BC_SSL_O_USE_TLSV11; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6501 | #else |
| 6502 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.1", args[0], args[i]); |
| 6503 | return -1; |
| 6504 | #endif |
| 6505 | } |
| 6506 | else if (!strcmp(args[i], "force-tlsv12")) { |
| 6507 | #if SSL_OP_NO_TLSv1_2 |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6508 | global_ssl.listen_default_ssloptions |= BC_SSL_O_USE_TLSV12; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6509 | #else |
| 6510 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.2", args[0], args[i]); |
| 6511 | return -1; |
| 6512 | #endif |
| 6513 | } |
| 6514 | else if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6515 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6516 | else { |
| 6517 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 6518 | return -1; |
| 6519 | } |
| 6520 | i++; |
| 6521 | } |
| 6522 | return 0; |
| 6523 | } |
| 6524 | |
| 6525 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 6526 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 6527 | struct proxy *defpx, const char *file, int line, |
| 6528 | char **err) { |
| 6529 | int i = 1; |
| 6530 | |
| 6531 | if (*(args[i]) == 0) { |
| 6532 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 6533 | return -1; |
| 6534 | } |
| 6535 | while (*(args[i])) { |
| 6536 | if (!strcmp(args[i], "no-sslv3")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6537 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_SSLV3; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6538 | else if (!strcmp(args[i], "no-tlsv10")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6539 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV10; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6540 | else if (!strcmp(args[i], "no-tlsv11")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6541 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV11; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6542 | else if (!strcmp(args[i], "no-tlsv12")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6543 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV12; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6544 | else if (!strcmp(args[i], "force-sslv3")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6545 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_USE_SSLV3; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6546 | else if (!strcmp(args[i], "force-tlsv10")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6547 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV10; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6548 | else if (!strcmp(args[i], "force-tlsv11")) { |
| 6549 | #if SSL_OP_NO_TLSv1_1 |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6550 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV11; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6551 | #else |
| 6552 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.1", args[0], args[i]); |
| 6553 | return -1; |
| 6554 | #endif |
| 6555 | } |
| 6556 | else if (!strcmp(args[i], "force-tlsv12")) { |
| 6557 | #if SSL_OP_NO_TLSv1_2 |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6558 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV12; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6559 | #else |
| 6560 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.2", args[0], args[i]); |
| 6561 | return -1; |
| 6562 | #endif |
| 6563 | } |
| 6564 | else if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6565 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6566 | else { |
| 6567 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 6568 | return -1; |
| 6569 | } |
| 6570 | i++; |
| 6571 | } |
| 6572 | return 0; |
| 6573 | } |
| 6574 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 6575 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 6576 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6577 | */ |
| 6578 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 6579 | struct proxy *defpx, const char *file, int line, |
| 6580 | char **err) |
| 6581 | { |
| 6582 | char **target; |
| 6583 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6584 | 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] | 6585 | |
| 6586 | if (too_many_args(1, args, err, NULL)) |
| 6587 | return -1; |
| 6588 | |
| 6589 | if (*target) { |
| 6590 | memprintf(err, "'%s' already specified.", args[0]); |
| 6591 | return -1; |
| 6592 | } |
| 6593 | |
| 6594 | if (*(args[1]) == 0) { |
| 6595 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 6596 | return -1; |
| 6597 | } |
| 6598 | *target = strdup(args[1]); |
| 6599 | return 0; |
| 6600 | } |
| 6601 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 6602 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 6603 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 6604 | */ |
| 6605 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 6606 | struct proxy *defpx, const char *file, int line, |
| 6607 | char **err) |
| 6608 | { |
| 6609 | char **target; |
| 6610 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6611 | 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] | 6612 | |
| 6613 | if (too_many_args(1, args, err, NULL)) |
| 6614 | return -1; |
| 6615 | |
| 6616 | if (*(args[1]) == 0) { |
| 6617 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 6618 | return -1; |
| 6619 | } |
| 6620 | |
| 6621 | free(*target); |
| 6622 | *target = strdup(args[1]); |
| 6623 | return 0; |
| 6624 | } |
| 6625 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6626 | /* parse various global tune.ssl settings consisting in positive integers. |
| 6627 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6628 | */ |
| 6629 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 6630 | struct proxy *defpx, const char *file, int line, |
| 6631 | char **err) |
| 6632 | { |
| 6633 | int *target; |
| 6634 | |
| 6635 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 6636 | target = &global.tune.sslcachesize; |
| 6637 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6638 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6639 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6640 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 6641 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 6642 | target = &global.maxsslconn; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6643 | else { |
| 6644 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 6645 | return -1; |
| 6646 | } |
| 6647 | |
| 6648 | if (too_many_args(1, args, err, NULL)) |
| 6649 | return -1; |
| 6650 | |
| 6651 | if (*(args[1]) == 0) { |
| 6652 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 6653 | return -1; |
| 6654 | } |
| 6655 | |
| 6656 | *target = atoi(args[1]); |
| 6657 | if (*target < 0) { |
| 6658 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 6659 | return -1; |
| 6660 | } |
| 6661 | return 0; |
| 6662 | } |
| 6663 | |
| 6664 | /* parse "ssl.force-private-cache". |
| 6665 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6666 | */ |
| 6667 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 6668 | struct proxy *defpx, const char *file, int line, |
| 6669 | char **err) |
| 6670 | { |
| 6671 | if (too_many_args(0, args, err, NULL)) |
| 6672 | return -1; |
| 6673 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6674 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6675 | return 0; |
| 6676 | } |
| 6677 | |
| 6678 | /* parse "ssl.lifetime". |
| 6679 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6680 | */ |
| 6681 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 6682 | struct proxy *defpx, const char *file, int line, |
| 6683 | char **err) |
| 6684 | { |
| 6685 | const char *res; |
| 6686 | |
| 6687 | if (too_many_args(1, args, err, NULL)) |
| 6688 | return -1; |
| 6689 | |
| 6690 | if (*(args[1]) == 0) { |
| 6691 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 6692 | return -1; |
| 6693 | } |
| 6694 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6695 | 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] | 6696 | if (res) { |
| 6697 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 6698 | return -1; |
| 6699 | } |
| 6700 | return 0; |
| 6701 | } |
| 6702 | |
| 6703 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 6704 | /* parse "ssl-dh-param-file". |
| 6705 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6706 | */ |
| 6707 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 6708 | struct proxy *defpx, const char *file, int line, |
| 6709 | char **err) |
| 6710 | { |
| 6711 | if (too_many_args(1, args, err, NULL)) |
| 6712 | return -1; |
| 6713 | |
| 6714 | if (*(args[1]) == 0) { |
| 6715 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 6716 | return -1; |
| 6717 | } |
| 6718 | |
| 6719 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 6720 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 6721 | return -1; |
| 6722 | } |
| 6723 | return 0; |
| 6724 | } |
| 6725 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6726 | /* parse "ssl.default-dh-param". |
| 6727 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 6728 | */ |
| 6729 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 6730 | struct proxy *defpx, const char *file, int line, |
| 6731 | char **err) |
| 6732 | { |
| 6733 | if (too_many_args(1, args, err, NULL)) |
| 6734 | return -1; |
| 6735 | |
| 6736 | if (*(args[1]) == 0) { |
| 6737 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 6738 | return -1; |
| 6739 | } |
| 6740 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6741 | global_ssl.default_dh_param = atoi(args[1]); |
| 6742 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6743 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 6744 | return -1; |
| 6745 | } |
| 6746 | return 0; |
| 6747 | } |
| 6748 | #endif |
| 6749 | |
| 6750 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6751 | /* This function is used with TLS ticket keys management. It permits to browse |
| 6752 | * each reference. The variable <getnext> must contain the current node, |
| 6753 | * <end> point to the root node. |
| 6754 | */ |
| 6755 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6756 | static inline |
| 6757 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 6758 | { |
| 6759 | struct tls_keys_ref *ref = getnext; |
| 6760 | |
| 6761 | while (1) { |
| 6762 | |
| 6763 | /* Get next list entry. */ |
| 6764 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 6765 | |
| 6766 | /* If the entry is the last of the list, return NULL. */ |
| 6767 | if (&ref->list == end) |
| 6768 | return NULL; |
| 6769 | |
| 6770 | return ref; |
| 6771 | } |
| 6772 | } |
| 6773 | |
| 6774 | static inline |
| 6775 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 6776 | { |
| 6777 | int id; |
| 6778 | char *error; |
| 6779 | |
| 6780 | /* If the reference starts by a '#', this is numeric id. */ |
| 6781 | if (reference[0] == '#') { |
| 6782 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 6783 | id = strtol(reference + 1, &error, 10); |
| 6784 | if (*error != '\0') |
| 6785 | return NULL; |
| 6786 | |
| 6787 | /* Perform the unique id lookup. */ |
| 6788 | return tlskeys_ref_lookupid(id); |
| 6789 | } |
| 6790 | |
| 6791 | /* Perform the string lookup. */ |
| 6792 | return tlskeys_ref_lookup(reference); |
| 6793 | } |
| 6794 | #endif |
| 6795 | |
| 6796 | |
| 6797 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6798 | |
| 6799 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 6800 | |
| 6801 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 6802 | return cli_io_handler_tlskeys_files(appctx); |
| 6803 | } |
| 6804 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6805 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 6806 | * (next index to be dumped), and cli.p0 (next key reference). |
| 6807 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6808 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 6809 | |
| 6810 | struct stream_interface *si = appctx->owner; |
| 6811 | |
| 6812 | switch (appctx->st2) { |
| 6813 | case STAT_ST_INIT: |
| 6814 | /* Display the column headers. If the message cannot be sent, |
| 6815 | * quit the fucntion with returning 0. The function is called |
| 6816 | * later and restart at the state "STAT_ST_INIT". |
| 6817 | */ |
| 6818 | chunk_reset(&trash); |
| 6819 | |
| 6820 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 6821 | chunk_appendf(&trash, "# id secret\n"); |
| 6822 | else |
| 6823 | chunk_appendf(&trash, "# id (file)\n"); |
| 6824 | |
| 6825 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 6826 | si_applet_cant_put(si); |
| 6827 | return 0; |
| 6828 | } |
| 6829 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6830 | /* Now, we start the browsing of the references lists. |
| 6831 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 6832 | * available field of this pointer is <list>. It is used with the function |
| 6833 | * tlskeys_list_get_next() for retruning the first available entry |
| 6834 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6835 | if (appctx->ctx.cli.p0 == NULL) { |
| 6836 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 6837 | 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] | 6838 | } |
| 6839 | |
| 6840 | appctx->st2 = STAT_ST_LIST; |
| 6841 | /* fall through */ |
| 6842 | |
| 6843 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6844 | while (appctx->ctx.cli.p0) { |
| 6845 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
| 6846 | int head = ref->tls_ticket_enc_index; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6847 | |
| 6848 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6849 | 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] | 6850 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6851 | |
| 6852 | if (appctx->ctx.cli.i1 == 0) |
| 6853 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 6854 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6855 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6856 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6857 | struct chunk *t2 = get_trash_chunk(); |
| 6858 | |
| 6859 | chunk_reset(t2); |
| 6860 | /* 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] | 6861 | 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] | 6862 | sizeof(struct tls_sess_key), t2->str, t2->size); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6863 | 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] | 6864 | |
| 6865 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 6866 | /* let's try again later from this stream. We add ourselves into |
| 6867 | * this stream's users so that it can remove us upon termination. |
| 6868 | */ |
| 6869 | si_applet_cant_put(si); |
| 6870 | return 0; |
| 6871 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6872 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6873 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6874 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6875 | } |
| 6876 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 6877 | /* let's try again later from this stream. We add ourselves into |
| 6878 | * this stream's users so that it can remove us upon termination. |
| 6879 | */ |
| 6880 | si_applet_cant_put(si); |
| 6881 | return 0; |
| 6882 | } |
| 6883 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6884 | 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] | 6885 | break; |
| 6886 | |
| 6887 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6888 | 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] | 6889 | } |
| 6890 | |
| 6891 | appctx->st2 = STAT_ST_FIN; |
| 6892 | /* fall through */ |
| 6893 | |
| 6894 | default: |
| 6895 | appctx->st2 = STAT_ST_FIN; |
| 6896 | return 1; |
| 6897 | } |
| 6898 | return 0; |
| 6899 | } |
| 6900 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6901 | /* 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] | 6902 | static int cli_parse_show_tlskeys(char **args, struct appctx *appctx, void *private) |
| 6903 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6904 | /* no parameter, shows only file list */ |
| 6905 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6906 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6907 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 6908 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6909 | } |
| 6910 | |
| 6911 | if (args[2][0] == '*') { |
| 6912 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6913 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6914 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6915 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
| 6916 | if (!appctx->ctx.cli.p0) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6917 | 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] | 6918 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6919 | return 1; |
| 6920 | } |
| 6921 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6922 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 6923 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6924 | } |
| 6925 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6926 | static int cli_parse_set_tlskeys(char **args, struct appctx *appctx, void *private) |
| 6927 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6928 | struct tls_keys_ref *ref; |
| 6929 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6930 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 6931 | if (!*args[3] || !*args[4]) { |
| 6932 | 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] | 6933 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6934 | return 1; |
| 6935 | } |
| 6936 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6937 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 6938 | if (!ref) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6939 | 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] | 6940 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6941 | return 1; |
| 6942 | } |
| 6943 | |
| 6944 | trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size); |
| 6945 | if (trash.len != sizeof(struct tls_sess_key)) { |
| 6946 | 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] | 6947 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6948 | return 1; |
| 6949 | } |
| 6950 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6951 | memcpy(ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len); |
| 6952 | 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] | 6953 | |
| 6954 | appctx->ctx.cli.msg = "TLS ticket key updated!"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 6955 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6956 | return 1; |
| 6957 | |
| 6958 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6959 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6960 | |
| 6961 | static int cli_parse_set_ocspresponse(char **args, struct appctx *appctx, void *private) |
| 6962 | { |
| 6963 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 6964 | char *err = NULL; |
| 6965 | |
| 6966 | /* Expect one parameter: the new response in base64 encoding */ |
| 6967 | if (!*args[3]) { |
| 6968 | 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] | 6969 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6970 | return 1; |
| 6971 | } |
| 6972 | |
| 6973 | trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size); |
| 6974 | if (trash.len < 0) { |
| 6975 | 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] | 6976 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6977 | return 1; |
| 6978 | } |
| 6979 | |
| 6980 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
| 6981 | if (err) { |
| 6982 | memprintf(&err, "%s.\n", err); |
| 6983 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 6984 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6985 | } |
| 6986 | return 1; |
| 6987 | } |
| 6988 | appctx->ctx.cli.msg = "OCSP Response updated!"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 6989 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6990 | return 1; |
| 6991 | #else |
| 6992 | 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] | 6993 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6994 | return 1; |
| 6995 | #endif |
| 6996 | |
| 6997 | } |
| 6998 | |
| 6999 | /* register cli keywords */ |
| 7000 | static struct cli_kw_list cli_kws = {{ },{ |
| 7001 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7002 | { { "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 }, |
| 7003 | { { "set", "ssl", "tls-keys", NULL }, NULL, cli_parse_set_tlskeys, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7004 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7005 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7006 | { { NULL }, NULL, NULL, NULL } |
| 7007 | }}; |
| 7008 | |
| 7009 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7010 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7011 | * Please take care of keeping this list alphabetically sorted. |
| 7012 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 7013 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7014 | { "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] | 7015 | { "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] | 7016 | { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 7017 | { "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] | 7018 | { "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] | 7019 | { "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] | 7020 | { "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] | 7021 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 7022 | { "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] | 7023 | { "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] | 7024 | { "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] | 7025 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7026 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7027 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7028 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7029 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7030 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7031 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 7032 | { "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] | 7033 | { "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] | 7034 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 7035 | { "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] | 7036 | { "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] | 7037 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7038 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7039 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7040 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7041 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7042 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 7043 | { "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] | 7044 | { "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] | 7045 | { "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] | 7046 | { "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] | 7047 | { "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] | 7048 | { "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] | 7049 | { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 7050 | { "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] | 7051 | { "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] | 7052 | #ifdef OPENSSL_NPN_NEGOTIATED |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7053 | { "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] | 7054 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7055 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7056 | { "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] | 7057 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7058 | { "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] | 7059 | { "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] | 7060 | { "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] | 7061 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 7062 | { "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] | 7063 | { NULL, NULL, 0, 0, 0 }, |
| 7064 | }}; |
| 7065 | |
| 7066 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7067 | * Please take care of keeping this list alphabetically sorted. |
| 7068 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 7069 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 7070 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 7071 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 7072 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7073 | }}; |
| 7074 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7075 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7076 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 7077 | * all code contributors. |
| 7078 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 7079 | * the config parser can report an appropriate error when a known keyword was |
| 7080 | * not enabled. |
| 7081 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7082 | static struct ssl_bind_kw ssl_bind_kws[] = { |
| 7083 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 7084 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 7085 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 7086 | { "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] | 7087 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7088 | { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 7089 | { "force-sslv3", ssl_bind_parse_force_sslv3, 0 }, /* force SSLv3 */ |
| 7090 | { "force-tlsv10", ssl_bind_parse_force_tlsv10, 0 }, /* force TLSv10 */ |
| 7091 | { "force-tlsv11", ssl_bind_parse_force_tlsv11, 0 }, /* force TLSv11 */ |
| 7092 | { "force-tlsv12", ssl_bind_parse_force_tlsv12, 0 }, /* force TLSv12 */ |
| 7093 | { "no-sslv3", ssl_bind_parse_no_sslv3, 0 }, /* disable SSLv3 */ |
| 7094 | { "no-tlsv10", ssl_bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */ |
| 7095 | { "no-tlsv11", ssl_bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */ |
| 7096 | { "no-tlsv12", ssl_bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */ |
| 7097 | { "no-tls-tickets", ssl_bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 7098 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 7099 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 7100 | { NULL, NULL, 0 }, |
| 7101 | }; |
| 7102 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 7103 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7104 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 7105 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 7106 | { "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] | 7107 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 7108 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7109 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 7110 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 7111 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 7112 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 7113 | { "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] | 7114 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7115 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 7116 | { "force-sslv3", bind_parse_force_sslv3, 0 }, /* force SSLv3 */ |
| 7117 | { "force-tlsv10", bind_parse_force_tlsv10, 0 }, /* force TLSv10 */ |
| 7118 | { "force-tlsv11", bind_parse_force_tlsv11, 0 }, /* force TLSv11 */ |
| 7119 | { "force-tlsv12", bind_parse_force_tlsv12, 0 }, /* force TLSv12 */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7120 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 7121 | { "no-sslv3", bind_parse_no_sslv3, 0 }, /* disable SSLv3 */ |
| 7122 | { "no-tlsv10", bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */ |
| 7123 | { "no-tlsv11", bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */ |
| 7124 | { "no-tlsv12", bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */ |
| 7125 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 7126 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
| 7127 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 7128 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 7129 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 7130 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7131 | { NULL, NULL, 0 }, |
| 7132 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7133 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7134 | /* Note: must not be declared <const> as its list will be overwritten. |
| 7135 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 7136 | * all code contributors. |
| 7137 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 7138 | * the config parser can report an appropriate error when a known keyword was |
| 7139 | * not enabled. |
| 7140 | */ |
| 7141 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7142 | { "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] | 7143 | { "check-ssl", srv_parse_check_ssl, 0, 0 }, /* enable SSL for health checks */ |
| 7144 | { "ciphers", srv_parse_ciphers, 1, 0 }, /* select the cipher suite */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7145 | { "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] | 7146 | { "crt", srv_parse_crt, 1, 0 }, /* set client certificate */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 7147 | { "force-sslv3", srv_parse_force_sslv3, 0, 0 }, /* force SSLv3 */ |
| 7148 | { "force-tlsv10", srv_parse_force_tlsv10, 0, 0 }, /* force TLSv10 */ |
| 7149 | { "force-tlsv11", srv_parse_force_tlsv11, 0, 0 }, /* force TLSv11 */ |
| 7150 | { "force-tlsv12", srv_parse_force_tlsv12, 0, 0 }, /* force TLSv12 */ |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 7151 | { "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] | 7152 | { "no-sslv3", srv_parse_no_sslv3, 0, 0 }, /* disable SSLv3 */ |
| 7153 | { "no-tlsv10", srv_parse_no_tlsv10, 0, 0 }, /* disable TLSv10 */ |
| 7154 | { "no-tlsv11", srv_parse_no_tlsv11, 0, 0 }, /* disable TLSv11 */ |
| 7155 | { "no-tlsv12", srv_parse_no_tlsv12, 0, 0 }, /* disable TLSv12 */ |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 7156 | { "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] | 7157 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 0 }, /* send PROXY protocol header v2 with SSL info */ |
| 7158 | { "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] | 7159 | { "sni", srv_parse_sni, 1, 0 }, /* send SNI extension */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 7160 | { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 7161 | { "verify", srv_parse_verify, 1, 0 }, /* set SSL verify method */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 7162 | { "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] | 7163 | { NULL, NULL, 0, 0 }, |
| 7164 | }}; |
| 7165 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7166 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 7167 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 7168 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 7169 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7170 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 7171 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 7172 | #ifndef OPENSSL_NO_DH |
| 7173 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 7174 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7175 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 7176 | #ifndef OPENSSL_NO_DH |
| 7177 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 7178 | #endif |
| 7179 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 7180 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 7181 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 7182 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 7183 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 7184 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7185 | { 0, NULL, NULL }, |
| 7186 | }}; |
| 7187 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 7188 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 7189 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7190 | .snd_buf = ssl_sock_from_buf, |
| 7191 | .rcv_buf = ssl_sock_to_buf, |
| 7192 | .rcv_pipe = NULL, |
| 7193 | .snd_pipe = NULL, |
| 7194 | .shutr = NULL, |
| 7195 | .shutw = ssl_sock_shutw, |
| 7196 | .close = ssl_sock_close, |
| 7197 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 7198 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 7199 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 7200 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 7201 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 7202 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7203 | }; |
| 7204 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 7205 | #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] | 7206 | |
| 7207 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7208 | { |
| 7209 | if (ptr) { |
| 7210 | chunk_destroy(ptr); |
| 7211 | free(ptr); |
| 7212 | } |
| 7213 | } |
| 7214 | |
| 7215 | #endif |
| 7216 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7217 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7218 | static void __ssl_sock_init(void) |
| 7219 | { |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7220 | char *ptr; |
| 7221 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7222 | STACK_OF(SSL_COMP)* cm; |
| 7223 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7224 | if (global_ssl.listen_default_ciphers) |
| 7225 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 7226 | if (global_ssl.connect_default_ciphers) |
| 7227 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 7228 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 7229 | xprt_register(XPRT_SSL, &ssl_sock); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7230 | SSL_library_init(); |
| 7231 | cm = SSL_COMP_get_compression_methods(); |
| 7232 | sk_SSL_COMP_zero(cm); |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 7233 | #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] | 7234 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 7235 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7236 | sample_register_fetches(&sample_fetch_keywords); |
| 7237 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7238 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7239 | srv_register_keywords(&srv_kws); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 7240 | cfg_register_keywords(&cfg_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7241 | cli_register_kw(&cli_kws); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 7242 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7243 | hap_register_post_check(tlskeys_finalize_config); |
| 7244 | #endif |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 7245 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7246 | ptr = NULL; |
| 7247 | memprintf(&ptr, "Built with OpenSSL version : " |
| 7248 | #ifdef OPENSSL_IS_BORINGSSL |
| 7249 | "BoringSSL\n"); |
| 7250 | #else /* OPENSSL_IS_BORINGSSL */ |
| 7251 | OPENSSL_VERSION_TEXT |
| 7252 | "\nRunning on OpenSSL version : %s%s", |
| 7253 | SSLeay_version(SSLEAY_VERSION), |
| 7254 | ((OPENSSL_VERSION_NUMBER ^ SSLeay()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
| 7255 | #endif |
| 7256 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
| 7257 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 7258 | "no (library version too old)" |
| 7259 | #elif defined(OPENSSL_NO_TLSEXT) |
| 7260 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 7261 | #else |
| 7262 | "yes" |
| 7263 | #endif |
| 7264 | "", ptr); |
| 7265 | |
| 7266 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 7267 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7268 | "yes" |
| 7269 | #else |
| 7270 | #ifdef OPENSSL_NO_TLSEXT |
| 7271 | "no (because of OPENSSL_NO_TLSEXT)" |
| 7272 | #else |
| 7273 | "no (version might be too old, 0.9.8f min needed)" |
| 7274 | #endif |
| 7275 | #endif |
| 7276 | "", ptr); |
| 7277 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7278 | hap_register_build_opts(ptr, 1); |
| 7279 | |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 7280 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 7281 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 7282 | |
| 7283 | #ifndef OPENSSL_NO_DH |
| 7284 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 7285 | #endif |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 7286 | |
| 7287 | /* Load SSL string for the verbose & debug mode. */ |
| 7288 | ERR_load_SSL_strings(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7289 | } |
| 7290 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7291 | __attribute__((destructor)) |
| 7292 | static void __ssl_sock_deinit(void) |
| 7293 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 7294 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7295 | lru64_destroy(ssl_ctx_lru_tree); |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 7296 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7297 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7298 | #ifndef OPENSSL_NO_DH |
| 7299 | if (local_dh_1024) { |
| 7300 | DH_free(local_dh_1024); |
| 7301 | local_dh_1024 = NULL; |
| 7302 | } |
| 7303 | |
| 7304 | if (local_dh_2048) { |
| 7305 | DH_free(local_dh_2048); |
| 7306 | local_dh_2048 = NULL; |
| 7307 | } |
| 7308 | |
| 7309 | if (local_dh_4096) { |
| 7310 | DH_free(local_dh_4096); |
| 7311 | local_dh_4096 = NULL; |
| 7312 | } |
| 7313 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 7314 | if (global_dh) { |
| 7315 | DH_free(global_dh); |
| 7316 | global_dh = NULL; |
| 7317 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7318 | #endif |
| 7319 | |
| 7320 | ERR_remove_state(0); |
| 7321 | ERR_free_strings(); |
| 7322 | |
| 7323 | EVP_cleanup(); |
| 7324 | |
| 7325 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 7326 | CRYPTO_cleanup_all_ex_data(); |
| 7327 | #endif |
| 7328 | } |
| 7329 | |
| 7330 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7331 | /* |
| 7332 | * Local variables: |
| 7333 | * c-indent-level: 8 |
| 7334 | * c-basic-offset: 8 |
| 7335 | * End: |
| 7336 | */ |