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 | |
| 42 | #include <openssl/ssl.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 43 | #include <openssl/x509.h> |
| 44 | #include <openssl/x509v3.h> |
| 45 | #include <openssl/x509.h> |
| 46 | #include <openssl/err.h> |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 47 | #include <openssl/rand.h> |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 48 | #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] | 49 | #include <openssl/ocsp.h> |
| 50 | #endif |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 51 | #ifndef OPENSSL_NO_DH |
| 52 | #include <openssl/dh.h> |
| 53 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 54 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 55 | #include <import/lru.h> |
| 56 | #include <import/xxhash.h> |
| 57 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 58 | #include <common/buffer.h> |
| 59 | #include <common/compat.h> |
| 60 | #include <common/config.h> |
| 61 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 62 | #include <common/errors.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 63 | #include <common/standard.h> |
| 64 | #include <common/ticks.h> |
| 65 | #include <common/time.h> |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 66 | #include <common/cfgparse.h> |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 67 | #include <common/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 68 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 69 | #include <ebsttree.h> |
| 70 | |
| 71 | #include <types/global.h> |
| 72 | #include <types/ssl_sock.h> |
| 73 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 74 | #include <proto/acl.h> |
| 75 | #include <proto/arg.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 76 | #include <proto/connection.h> |
| 77 | #include <proto/fd.h> |
| 78 | #include <proto/freq_ctr.h> |
| 79 | #include <proto/frontend.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 80 | #include <proto/listener.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 81 | #include <proto/pattern.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 82 | #include <proto/proto_tcp.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 83 | #include <proto/server.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 84 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 85 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 86 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 87 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 88 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 89 | #include <proto/task.h> |
| 90 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 91 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 92 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 93 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 94 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 95 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 96 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 97 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 98 | |
| 99 | /* Verify errors macros */ |
| 100 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 101 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 102 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 103 | |
| 104 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 105 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 106 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 107 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 108 | /* Supported hash function for TLS tickets */ |
| 109 | #ifdef OPENSSL_NO_SHA256 |
| 110 | #define HASH_FUNCT EVP_sha1 |
| 111 | #else |
| 112 | #define HASH_FUNCT EVP_sha256 |
| 113 | #endif /* OPENSSL_NO_SHA256 */ |
| 114 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 115 | /* server and bind verify method, it uses a global value as default */ |
| 116 | enum { |
| 117 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 118 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 119 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 120 | SSL_SOCK_VERIFY_NONE = 3, |
| 121 | }; |
| 122 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 123 | int sslconns = 0; |
| 124 | int totalsslconns = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 125 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 126 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 127 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 128 | #endif |
| 129 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 130 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 131 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 132 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 133 | static DH *local_dh_1024 = NULL; |
| 134 | static DH *local_dh_2048 = NULL; |
| 135 | static DH *local_dh_4096 = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 136 | #endif /* OPENSSL_NO_DH */ |
| 137 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 138 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 139 | /* X509V3 Extensions that will be added on generated certificates */ |
| 140 | #define X509V3_EXT_SIZE 5 |
| 141 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 142 | "basicConstraints", |
| 143 | "nsComment", |
| 144 | "subjectKeyIdentifier", |
| 145 | "authorityKeyIdentifier", |
| 146 | "keyUsage", |
| 147 | }; |
| 148 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 149 | "CA:FALSE", |
| 150 | "\"OpenSSL Generated Certificate\"", |
| 151 | "hash", |
| 152 | "keyid,issuer:always", |
| 153 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 154 | }; |
| 155 | |
| 156 | /* LRU cache to store generated certificate */ |
| 157 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 158 | static unsigned int ssl_ctx_lru_seed = 0; |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 159 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 160 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 161 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 162 | /* The order here matters for picking a default context, |
| 163 | * keep the most common keytype at the bottom of the list |
| 164 | */ |
| 165 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 166 | "dsa", |
| 167 | "ecdsa", |
| 168 | "rsa" |
| 169 | }; |
| 170 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 171 | #else |
| 172 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 173 | #endif |
| 174 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 175 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 176 | /* |
| 177 | * struct alignment works here such that the key.key is the same as key_data |
| 178 | * Do not change the placement of key_data |
| 179 | */ |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 180 | struct certificate_ocsp { |
| 181 | struct ebmb_node key; |
| 182 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 183 | struct chunk response; |
| 184 | long expire; |
| 185 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 186 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 187 | struct ocsp_cbk_arg { |
| 188 | int is_single; |
| 189 | int single_kt; |
| 190 | union { |
| 191 | struct certificate_ocsp *s_ocsp; |
| 192 | /* |
| 193 | * m_ocsp will have multiple entries dependent on key type |
| 194 | * Entry 0 - DSA |
| 195 | * Entry 1 - ECDSA |
| 196 | * Entry 2 - RSA |
| 197 | */ |
| 198 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 199 | }; |
| 200 | }; |
| 201 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 202 | /* |
| 203 | * This function returns the number of seconds elapsed |
| 204 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 205 | * date presented un ASN1_GENERALIZEDTIME. |
| 206 | * |
| 207 | * In parsing error case, it returns -1. |
| 208 | */ |
| 209 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 210 | { |
| 211 | long epoch; |
| 212 | char *p, *end; |
| 213 | const unsigned short month_offset[12] = { |
| 214 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 215 | }; |
| 216 | int year, month; |
| 217 | |
| 218 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 219 | |
| 220 | p = (char *)d->data; |
| 221 | end = p + d->length; |
| 222 | |
| 223 | if (end - p < 4) return -1; |
| 224 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 225 | p += 4; |
| 226 | if (end - p < 2) return -1; |
| 227 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 228 | if (month < 1 || month > 12) return -1; |
| 229 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 230 | We consider leap years and the current month (<marsh or not) */ |
| 231 | epoch = ( ((year - 1970) * 365) |
| 232 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 233 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 234 | + month_offset[month-1] |
| 235 | ) * 24 * 60 * 60; |
| 236 | p += 2; |
| 237 | if (end - p < 2) return -1; |
| 238 | /* Add the number of seconds of completed days of current month */ |
| 239 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 240 | p += 2; |
| 241 | if (end - p < 2) return -1; |
| 242 | /* Add the completed hours of the current day */ |
| 243 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 244 | p += 2; |
| 245 | if (end - p < 2) return -1; |
| 246 | /* Add the completed minutes of the current hour */ |
| 247 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 248 | p += 2; |
| 249 | if (p == end) return -1; |
| 250 | /* Test if there is available seconds */ |
| 251 | if (p[0] < '0' || p[0] > '9') |
| 252 | goto nosec; |
| 253 | if (end - p < 2) return -1; |
| 254 | /* Add the seconds of the current minute */ |
| 255 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 256 | p += 2; |
| 257 | if (p == end) return -1; |
| 258 | /* Ignore seconds float part if present */ |
| 259 | if (p[0] == '.') { |
| 260 | do { |
| 261 | if (++p == end) return -1; |
| 262 | } while (p[0] >= '0' && p[0] <= '9'); |
| 263 | } |
| 264 | |
| 265 | nosec: |
| 266 | if (p[0] == 'Z') { |
| 267 | if (end - p != 1) return -1; |
| 268 | return epoch; |
| 269 | } |
| 270 | else if (p[0] == '+') { |
| 271 | if (end - p != 5) return -1; |
| 272 | /* Apply timezone offset */ |
| 273 | return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 274 | } |
| 275 | else if (p[0] == '-') { |
| 276 | if (end - p != 5) return -1; |
| 277 | /* Apply timezone offset */ |
| 278 | return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 279 | } |
| 280 | |
| 281 | return -1; |
| 282 | } |
| 283 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 284 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 285 | |
| 286 | /* This function starts to check if the OCSP response (in DER format) contained |
| 287 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 288 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 289 | * contained in the OCSP Response and exits on error if no match. |
| 290 | * If it's a valid OCSP Response: |
| 291 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 292 | * pointed by 'ocsp'. |
| 293 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 294 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 295 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 296 | * already present in the container, it will be overwritten. |
| 297 | * |
| 298 | * Note: OCSP response containing more than one OCSP Single response is not |
| 299 | * considered valid. |
| 300 | * |
| 301 | * Returns 0 on success, 1 in error case. |
| 302 | */ |
| 303 | static int ssl_sock_load_ocsp_response(struct chunk *ocsp_response, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 304 | { |
| 305 | OCSP_RESPONSE *resp; |
| 306 | OCSP_BASICRESP *bs = NULL; |
| 307 | OCSP_SINGLERESP *sr; |
| 308 | unsigned char *p = (unsigned char *)ocsp_response->str; |
| 309 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 310 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 311 | int reason; |
| 312 | int ret = 1; |
| 313 | |
| 314 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, ocsp_response->len); |
| 315 | if (!resp) { |
| 316 | memprintf(err, "Unable to parse OCSP response"); |
| 317 | goto out; |
| 318 | } |
| 319 | |
| 320 | rc = OCSP_response_status(resp); |
| 321 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 322 | memprintf(err, "OCSP response status not successful"); |
| 323 | goto out; |
| 324 | } |
| 325 | |
| 326 | bs = OCSP_response_get1_basic(resp); |
| 327 | if (!bs) { |
| 328 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 329 | goto out; |
| 330 | } |
| 331 | |
| 332 | count_sr = OCSP_resp_count(bs); |
| 333 | if (count_sr > 1) { |
| 334 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 335 | goto out; |
| 336 | } |
| 337 | |
| 338 | sr = OCSP_resp_get0(bs, 0); |
| 339 | if (!sr) { |
| 340 | memprintf(err, "Failed to get OCSP single response"); |
| 341 | goto out; |
| 342 | } |
| 343 | |
| 344 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
| 345 | if (rc != V_OCSP_CERTSTATUS_GOOD) { |
| 346 | memprintf(err, "OCSP single response: certificate status not good"); |
| 347 | goto out; |
| 348 | } |
| 349 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 350 | if (!nextupd) { |
| 351 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 352 | goto out; |
| 353 | } |
| 354 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 355 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 356 | if (!rc) { |
| 357 | memprintf(err, "OCSP single response: no longer valid."); |
| 358 | goto out; |
| 359 | } |
| 360 | |
| 361 | if (cid) { |
| 362 | if (OCSP_id_cmp(sr->certId, cid)) { |
| 363 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 364 | goto out; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (!ocsp) { |
| 369 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 370 | unsigned char *p; |
| 371 | |
| 372 | rc = i2d_OCSP_CERTID(sr->certId, NULL); |
| 373 | if (!rc) { |
| 374 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 375 | goto out; |
| 376 | } |
| 377 | |
| 378 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 379 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 380 | goto out; |
| 381 | } |
| 382 | |
| 383 | p = key; |
| 384 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 385 | i2d_OCSP_CERTID(sr->certId, &p); |
| 386 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 387 | if (!ocsp) { |
| 388 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 389 | goto out; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /* According to comments on "chunk_dup", the |
| 394 | previous chunk buffer will be freed */ |
| 395 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 396 | memprintf(err, "OCSP response: Memory allocation error"); |
| 397 | goto out; |
| 398 | } |
| 399 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 400 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 401 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 402 | ret = 0; |
| 403 | out: |
| 404 | if (bs) |
| 405 | OCSP_BASICRESP_free(bs); |
| 406 | |
| 407 | if (resp) |
| 408 | OCSP_RESPONSE_free(resp); |
| 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | /* |
| 413 | * External function use to update the OCSP response in the OCSP response's |
| 414 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 415 | * to update in DER format. |
| 416 | * |
| 417 | * Returns 0 on success, 1 in error case. |
| 418 | */ |
| 419 | int ssl_sock_update_ocsp_response(struct chunk *ocsp_response, char **err) |
| 420 | { |
| 421 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * This function load the OCSP Resonse in DER format contained in file at |
| 426 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 427 | * |
| 428 | * Returns 0 on success, 1 in error case. |
| 429 | */ |
| 430 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 431 | { |
| 432 | int fd = -1; |
| 433 | int r = 0; |
| 434 | int ret = 1; |
| 435 | |
| 436 | fd = open(ocsp_path, O_RDONLY); |
| 437 | if (fd == -1) { |
| 438 | memprintf(err, "Error opening OCSP response file"); |
| 439 | goto end; |
| 440 | } |
| 441 | |
| 442 | trash.len = 0; |
| 443 | while (trash.len < trash.size) { |
| 444 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 445 | if (r < 0) { |
| 446 | if (errno == EINTR) |
| 447 | continue; |
| 448 | |
| 449 | memprintf(err, "Error reading OCSP response from file"); |
| 450 | goto end; |
| 451 | } |
| 452 | else if (r == 0) { |
| 453 | break; |
| 454 | } |
| 455 | trash.len += r; |
| 456 | } |
| 457 | |
| 458 | close(fd); |
| 459 | fd = -1; |
| 460 | |
| 461 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 462 | end: |
| 463 | if (fd != -1) |
| 464 | close(fd); |
| 465 | |
| 466 | return ret; |
| 467 | } |
| 468 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 469 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 470 | 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) |
| 471 | { |
| 472 | struct tls_sess_key *keys; |
| 473 | struct connection *conn; |
| 474 | int head; |
| 475 | int i; |
| 476 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 477 | conn = SSL_get_app_data(s); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 478 | keys = objt_listener(conn->target)->bind_conf->keys_ref->tlskeys; |
| 479 | 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] | 480 | |
| 481 | if (enc) { |
| 482 | memcpy(key_name, keys[head].name, 16); |
| 483 | |
| 484 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
| 485 | return -1; |
| 486 | |
| 487 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].aes_key, iv)) |
| 488 | return -1; |
| 489 | |
| 490 | HMAC_Init_ex(hctx, keys[head].hmac_key, 16, HASH_FUNCT(), NULL); |
| 491 | |
| 492 | return 1; |
| 493 | } else { |
| 494 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 495 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 496 | goto found; |
| 497 | } |
| 498 | return 0; |
| 499 | |
| 500 | found: |
| 501 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].hmac_key, 16, HASH_FUNCT(), NULL); |
| 502 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].aes_key, iv)) |
| 503 | return -1; |
| 504 | /* 2 for key renewal, 1 if current key is still valid */ |
| 505 | return i ? 2 : 1; |
| 506 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 510 | { |
| 511 | struct tls_keys_ref *ref; |
| 512 | |
| 513 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 514 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 515 | return ref; |
| 516 | return NULL; |
| 517 | } |
| 518 | |
| 519 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 520 | { |
| 521 | struct tls_keys_ref *ref; |
| 522 | |
| 523 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 524 | if (ref->unique_id == unique_id) |
| 525 | return ref; |
| 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | int ssl_sock_update_tlskey(char *filename, struct chunk *tlskey, char **err) { |
| 530 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 531 | |
| 532 | if(!ref) { |
| 533 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 534 | return 1; |
| 535 | } |
| 536 | |
Pradeep Jindal | cc79b00 | 2015-08-20 18:25:17 +0530 | [diff] [blame] | 537 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), tlskey->str, tlskey->len); |
| 538 | 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] | 539 | |
| 540 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 541 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 542 | |
| 543 | /* This function finalize the configuration parsing. Its set all the |
| 544 | * automatic ids |
| 545 | */ |
| 546 | void tlskeys_finalize_config(void) |
| 547 | { |
| 548 | int i = 0; |
| 549 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 550 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 551 | |
| 552 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 553 | if (ref->unique_id == -1) { |
| 554 | /* Look for the first free id. */ |
| 555 | while (1) { |
| 556 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 557 | if (ref2->unique_id == i) { |
| 558 | i++; |
| 559 | break; |
| 560 | } |
| 561 | } |
| 562 | if (&ref2->list == &tlskeys_reference) |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | /* Uses the unique id and increment it for the next entry. */ |
| 567 | ref->unique_id = i; |
| 568 | i++; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /* This sort the reference list by id. */ |
| 573 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 574 | LIST_DEL(&ref->list); |
| 575 | list_for_each_entry(ref3, &tkr, list) { |
| 576 | if (ref->unique_id < ref3->unique_id) { |
| 577 | LIST_ADDQ(&ref3->list, &ref->list); |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | if (&ref3->list == &tkr) |
| 582 | LIST_ADDQ(&tkr, &ref->list); |
| 583 | } |
| 584 | |
| 585 | /* swap root */ |
| 586 | LIST_ADD(&tkr, &tlskeys_reference); |
| 587 | LIST_DEL(&tkr); |
| 588 | } |
| 589 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 590 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 591 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 592 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 593 | { |
| 594 | switch (evp_keytype) { |
| 595 | case EVP_PKEY_RSA: |
| 596 | return 2; |
| 597 | case EVP_PKEY_DSA: |
| 598 | return 0; |
| 599 | case EVP_PKEY_EC: |
| 600 | return 1; |
| 601 | } |
| 602 | |
| 603 | return -1; |
| 604 | } |
| 605 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 606 | /* |
| 607 | * Callback used to set OCSP status extension content in server hello. |
| 608 | */ |
| 609 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 610 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 611 | struct certificate_ocsp *ocsp; |
| 612 | struct ocsp_cbk_arg *ocsp_arg; |
| 613 | char *ssl_buf; |
| 614 | EVP_PKEY *ssl_pkey; |
| 615 | int key_type; |
| 616 | int index; |
| 617 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 618 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 619 | |
| 620 | ssl_pkey = SSL_get_privatekey(ssl); |
| 621 | if (!ssl_pkey) |
| 622 | return SSL_TLSEXT_ERR_NOACK; |
| 623 | |
| 624 | key_type = EVP_PKEY_type(ssl_pkey->type); |
| 625 | |
| 626 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 627 | ocsp = ocsp_arg->s_ocsp; |
| 628 | else { |
| 629 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 630 | * the certificate type |
| 631 | */ |
| 632 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 633 | |
| 634 | if (index < 0) |
| 635 | return SSL_TLSEXT_ERR_NOACK; |
| 636 | |
| 637 | ocsp = ocsp_arg->m_ocsp[index]; |
| 638 | |
| 639 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 640 | |
| 641 | if (!ocsp || |
| 642 | !ocsp->response.str || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 643 | !ocsp->response.len || |
| 644 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 645 | return SSL_TLSEXT_ERR_NOACK; |
| 646 | |
| 647 | ssl_buf = OPENSSL_malloc(ocsp->response.len); |
| 648 | if (!ssl_buf) |
| 649 | return SSL_TLSEXT_ERR_NOACK; |
| 650 | |
| 651 | memcpy(ssl_buf, ocsp->response.str, ocsp->response.len); |
| 652 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.len); |
| 653 | |
| 654 | return SSL_TLSEXT_ERR_OK; |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 659 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 660 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 661 | * It should be present in the certificate's extra chain builded from file |
| 662 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 663 | * named 'cert_path' suffixed using '.issuer'. |
| 664 | * |
| 665 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 666 | * response. If file is empty or content is not a valid OCSP response, |
| 667 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 668 | * is displayed). |
| 669 | * |
| 670 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
| 671 | * succesfully enabled, or -1 in other error case. |
| 672 | */ |
| 673 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 674 | { |
| 675 | |
| 676 | BIO *in = NULL; |
| 677 | X509 *x, *xi = NULL, *issuer = NULL; |
| 678 | STACK_OF(X509) *chain = NULL; |
| 679 | OCSP_CERTID *cid = NULL; |
| 680 | SSL *ssl; |
| 681 | char ocsp_path[MAXPATHLEN+1]; |
| 682 | int i, ret = -1; |
| 683 | struct stat st; |
| 684 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 685 | char *warn = NULL; |
| 686 | unsigned char *p; |
| 687 | |
| 688 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 689 | |
| 690 | if (stat(ocsp_path, &st)) |
| 691 | return 1; |
| 692 | |
| 693 | ssl = SSL_new(ctx); |
| 694 | if (!ssl) |
| 695 | goto out; |
| 696 | |
| 697 | x = SSL_get_certificate(ssl); |
| 698 | if (!x) |
| 699 | goto out; |
| 700 | |
| 701 | /* Try to lookup for issuer in certificate extra chain */ |
| 702 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 703 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 704 | #else |
| 705 | chain = ctx->extra_certs; |
| 706 | #endif |
| 707 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 708 | issuer = sk_X509_value(chain, i); |
| 709 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 710 | break; |
| 711 | else |
| 712 | issuer = NULL; |
| 713 | } |
| 714 | |
| 715 | /* If not found try to load issuer from a suffixed file */ |
| 716 | if (!issuer) { |
| 717 | char issuer_path[MAXPATHLEN+1]; |
| 718 | |
| 719 | in = BIO_new(BIO_s_file()); |
| 720 | if (!in) |
| 721 | goto out; |
| 722 | |
| 723 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 724 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 725 | goto out; |
| 726 | |
| 727 | xi = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata); |
| 728 | if (!xi) |
| 729 | goto out; |
| 730 | |
| 731 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 732 | goto out; |
| 733 | |
| 734 | issuer = xi; |
| 735 | } |
| 736 | |
| 737 | cid = OCSP_cert_to_id(0, x, issuer); |
| 738 | if (!cid) |
| 739 | goto out; |
| 740 | |
| 741 | i = i2d_OCSP_CERTID(cid, NULL); |
| 742 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 743 | goto out; |
| 744 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 745 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 746 | if (!ocsp) |
| 747 | goto out; |
| 748 | |
| 749 | p = ocsp->key_data; |
| 750 | i2d_OCSP_CERTID(cid, &p); |
| 751 | |
| 752 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 753 | if (iocsp == ocsp) |
| 754 | ocsp = NULL; |
| 755 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 756 | if (!ctx->tlsext_status_cb) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 757 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 758 | |
| 759 | cb_arg->is_single = 1; |
| 760 | cb_arg->s_ocsp = iocsp; |
| 761 | cb_arg->single_kt = EVP_PKEY_type(X509_get_pubkey(x)->type); |
| 762 | |
| 763 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 764 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 765 | } else { |
| 766 | /* |
| 767 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 768 | * Update that cb_arg with the new cert's staple |
| 769 | */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 770 | struct ocsp_cbk_arg *cb_arg = ctx->tlsext_status_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 771 | struct certificate_ocsp *tmp_ocsp; |
| 772 | int index; |
| 773 | |
| 774 | /* |
| 775 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 776 | * the order of operations below matter, take care when changing it |
| 777 | */ |
| 778 | tmp_ocsp = cb_arg->s_ocsp; |
| 779 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 780 | cb_arg->s_ocsp = NULL; |
| 781 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 782 | cb_arg->is_single = 0; |
| 783 | cb_arg->single_kt = 0; |
| 784 | |
| 785 | index = ssl_sock_get_ocsp_arg_kt_index(EVP_PKEY_type(X509_get_pubkey(x)->type)); |
| 786 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 787 | cb_arg->m_ocsp[index] = iocsp; |
| 788 | |
| 789 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 790 | |
| 791 | ret = 0; |
| 792 | |
| 793 | warn = NULL; |
| 794 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 795 | memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure"); |
| 796 | Warning("%s.\n", warn); |
| 797 | } |
| 798 | |
| 799 | out: |
| 800 | if (ssl) |
| 801 | SSL_free(ssl); |
| 802 | |
| 803 | if (in) |
| 804 | BIO_free(in); |
| 805 | |
| 806 | if (xi) |
| 807 | X509_free(xi); |
| 808 | |
| 809 | if (cid) |
| 810 | OCSP_CERTID_free(cid); |
| 811 | |
| 812 | if (ocsp) |
| 813 | free(ocsp); |
| 814 | |
| 815 | if (warn) |
| 816 | free(warn); |
| 817 | |
| 818 | |
| 819 | return ret; |
| 820 | } |
| 821 | |
| 822 | #endif |
| 823 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 824 | #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] | 825 | |
| 826 | #define CT_EXTENSION_TYPE 18 |
| 827 | |
| 828 | static int sctl_ex_index = -1; |
| 829 | |
| 830 | /* |
| 831 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 832 | * makes only basic test if the data seems like SCTL. No signature validation |
| 833 | * is performed. |
| 834 | */ |
| 835 | static int ssl_sock_parse_sctl(struct chunk *sctl) |
| 836 | { |
| 837 | int ret = 1; |
| 838 | int len, pos, sct_len; |
| 839 | unsigned char *data; |
| 840 | |
| 841 | if (sctl->len < 2) |
| 842 | goto out; |
| 843 | |
| 844 | data = (unsigned char *)sctl->str; |
| 845 | len = (data[0] << 8) | data[1]; |
| 846 | |
| 847 | if (len + 2 != sctl->len) |
| 848 | goto out; |
| 849 | |
| 850 | data = data + 2; |
| 851 | pos = 0; |
| 852 | while (pos < len) { |
| 853 | if (len - pos < 2) |
| 854 | goto out; |
| 855 | |
| 856 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 857 | if (pos + sct_len + 2 > len) |
| 858 | goto out; |
| 859 | |
| 860 | pos += sct_len + 2; |
| 861 | } |
| 862 | |
| 863 | ret = 0; |
| 864 | |
| 865 | out: |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, struct chunk **sctl) |
| 870 | { |
| 871 | int fd = -1; |
| 872 | int r = 0; |
| 873 | int ret = 1; |
| 874 | |
| 875 | *sctl = NULL; |
| 876 | |
| 877 | fd = open(sctl_path, O_RDONLY); |
| 878 | if (fd == -1) |
| 879 | goto end; |
| 880 | |
| 881 | trash.len = 0; |
| 882 | while (trash.len < trash.size) { |
| 883 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 884 | if (r < 0) { |
| 885 | if (errno == EINTR) |
| 886 | continue; |
| 887 | |
| 888 | goto end; |
| 889 | } |
| 890 | else if (r == 0) { |
| 891 | break; |
| 892 | } |
| 893 | trash.len += r; |
| 894 | } |
| 895 | |
| 896 | ret = ssl_sock_parse_sctl(&trash); |
| 897 | if (ret) |
| 898 | goto end; |
| 899 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 900 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 901 | if (!chunk_dup(*sctl, &trash)) { |
| 902 | free(*sctl); |
| 903 | *sctl = NULL; |
| 904 | goto end; |
| 905 | } |
| 906 | |
| 907 | end: |
| 908 | if (fd != -1) |
| 909 | close(fd); |
| 910 | |
| 911 | return ret; |
| 912 | } |
| 913 | |
| 914 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 915 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 916 | struct chunk *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 917 | |
| 918 | *out = (unsigned char *)sctl->str; |
| 919 | *outlen = sctl->len; |
| 920 | |
| 921 | return 1; |
| 922 | } |
| 923 | |
| 924 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 925 | { |
| 926 | return 1; |
| 927 | } |
| 928 | |
| 929 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 930 | { |
| 931 | char sctl_path[MAXPATHLEN+1]; |
| 932 | int ret = -1; |
| 933 | struct stat st; |
| 934 | struct chunk *sctl = NULL; |
| 935 | |
| 936 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 937 | |
| 938 | if (stat(sctl_path, &st)) |
| 939 | return 1; |
| 940 | |
| 941 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 942 | goto out; |
| 943 | |
| 944 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 945 | free(sctl); |
| 946 | goto out; |
| 947 | } |
| 948 | |
| 949 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 950 | |
| 951 | ret = 0; |
| 952 | |
| 953 | out: |
| 954 | return ret; |
| 955 | } |
| 956 | |
| 957 | #endif |
| 958 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 959 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 960 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 961 | struct connection *conn = SSL_get_app_data(ssl); |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 962 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 963 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 964 | |
| 965 | if (where & SSL_CB_HANDSHAKE_START) { |
| 966 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 967 | if (conn->flags & CO_FL_CONNECTED) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 968 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 969 | conn->err_code = CO_ER_SSL_RENEG; |
| 970 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 971 | } |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 972 | |
| 973 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 974 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 975 | /* Long certificate chains optimz |
| 976 | If write and read bios are differents, we |
| 977 | consider that the buffering was activated, |
| 978 | so we rise the output buffer size from 4k |
| 979 | to 16k */ |
| 980 | write_bio = SSL_get_wbio(ssl); |
| 981 | if (write_bio != SSL_get_rbio(ssl)) { |
| 982 | BIO_set_write_buffer_size(write_bio, 16384); |
| 983 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 984 | } |
| 985 | } |
| 986 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 987 | } |
| 988 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 989 | /* Callback is called for each certificate of the chain during a verify |
| 990 | ok is set to 1 if preverify detect no error on current certificate. |
| 991 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 992 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 993 | { |
| 994 | SSL *ssl; |
| 995 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 996 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 997 | |
| 998 | 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] | 999 | conn = SSL_get_app_data(ssl); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1000 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1001 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1002 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1003 | if (ok) /* no errors */ |
| 1004 | return ok; |
| 1005 | |
| 1006 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1007 | err = X509_STORE_CTX_get_error(x_store); |
| 1008 | |
| 1009 | /* check if CA error needs to be ignored */ |
| 1010 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1011 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 1012 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1013 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1014 | } |
| 1015 | |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1016 | if (objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) { |
| 1017 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1018 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1019 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1020 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1021 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1022 | return 0; |
| 1023 | } |
| 1024 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1025 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 1026 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1027 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1028 | /* check if certificate error needs to be ignored */ |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1029 | if (objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) { |
| 1030 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1031 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1032 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1033 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1034 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1035 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1038 | /* Callback is called for ssl protocol analyse */ |
| 1039 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1040 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1041 | #ifdef TLS1_RT_HEARTBEAT |
| 1042 | /* test heartbeat received (write_p is set to 0 |
| 1043 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1044 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1045 | struct connection *conn = SSL_get_app_data(ssl); |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1046 | const unsigned char *p = buf; |
| 1047 | unsigned int payload; |
| 1048 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1049 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1050 | |
| 1051 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1052 | if (*p != TLS1_HB_REQUEST) |
| 1053 | return; |
| 1054 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1055 | 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] | 1056 | goto kill_it; |
| 1057 | |
| 1058 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1059 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1060 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1061 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1062 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1063 | * advertised payload is larger than the advertised packet |
| 1064 | * length, so we have garbage in the buffer between the |
| 1065 | * payload and the end of the buffer (p+len). We can't know |
| 1066 | * if the SSL stack is patched, and we don't know if we can |
| 1067 | * safely wipe out the area between p+3+len and payload. |
| 1068 | * So instead, we prevent the response from being sent by |
| 1069 | * setting the max_send_fragment to 0 and we report an SSL |
| 1070 | * error, which will kill this connection. It will be reported |
| 1071 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1072 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1073 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1074 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1075 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1076 | return; |
| 1077 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1078 | #endif |
| 1079 | } |
| 1080 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1081 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 1082 | /* This callback is used so that the server advertises the list of |
| 1083 | * negociable protocols for NPN. |
| 1084 | */ |
| 1085 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1086 | unsigned int *len, void *arg) |
| 1087 | { |
| 1088 | struct bind_conf *conf = arg; |
| 1089 | |
| 1090 | *data = (const unsigned char *)conf->npn_str; |
| 1091 | *len = conf->npn_len; |
| 1092 | return SSL_TLSEXT_ERR_OK; |
| 1093 | } |
| 1094 | #endif |
| 1095 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1096 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1097 | /* This callback is used so that the server advertises the list of |
| 1098 | * negociable protocols for ALPN. |
| 1099 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1100 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1101 | unsigned char *outlen, |
| 1102 | const unsigned char *server, |
| 1103 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1104 | { |
| 1105 | struct bind_conf *conf = arg; |
| 1106 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1107 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1108 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1109 | return SSL_TLSEXT_ERR_NOACK; |
| 1110 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1111 | return SSL_TLSEXT_ERR_OK; |
| 1112 | } |
| 1113 | #endif |
| 1114 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1115 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1116 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
| 1117 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1118 | /* Create a X509 certificate with the specified servername and serial. This |
| 1119 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1120 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1121 | 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] | 1122 | { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1123 | static unsigned int serial = 0; |
| 1124 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1125 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1126 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1127 | SSL_CTX *ssl_ctx = NULL; |
| 1128 | X509 *newcrt = NULL; |
| 1129 | EVP_PKEY *pkey = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1130 | X509_NAME *name; |
| 1131 | const EVP_MD *digest; |
| 1132 | X509V3_CTX ctx; |
| 1133 | unsigned int i; |
| 1134 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1135 | /* Get the private key of the defautl certificate and use it */ |
| 1136 | if (!(pkey = SSL_get_privatekey(ssl))) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1137 | goto mkcert_error; |
| 1138 | |
| 1139 | /* Create the certificate */ |
| 1140 | if (!(newcrt = X509_new())) |
| 1141 | goto mkcert_error; |
| 1142 | |
| 1143 | /* Set version number for the certificate (X509v3) and the serial |
| 1144 | * number */ |
| 1145 | if (X509_set_version(newcrt, 2L) != 1) |
| 1146 | goto mkcert_error; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1147 | if (!serial) |
| 1148 | serial = now_ms; |
| 1149 | ASN1_INTEGER_set(X509_get_serialNumber(newcrt), serial++); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1150 | |
| 1151 | /* Set duration for the certificate */ |
| 1152 | if (!X509_gmtime_adj(X509_get_notBefore(newcrt), (long)-60*60*24) || |
| 1153 | !X509_gmtime_adj(X509_get_notAfter(newcrt),(long)60*60*24*365)) |
| 1154 | goto mkcert_error; |
| 1155 | |
| 1156 | /* set public key in the certificate */ |
| 1157 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1158 | goto mkcert_error; |
| 1159 | |
| 1160 | /* Set issuer name from the CA */ |
| 1161 | if (!(name = X509_get_subject_name(cacert))) |
| 1162 | goto mkcert_error; |
| 1163 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1164 | goto mkcert_error; |
| 1165 | |
| 1166 | /* Set the subject name using the same, but the CN */ |
| 1167 | name = X509_NAME_dup(name); |
| 1168 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1169 | (const unsigned char *)servername, |
| 1170 | -1, -1, 0) != 1) { |
| 1171 | X509_NAME_free(name); |
| 1172 | goto mkcert_error; |
| 1173 | } |
| 1174 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1175 | X509_NAME_free(name); |
| 1176 | goto mkcert_error; |
| 1177 | } |
| 1178 | X509_NAME_free(name); |
| 1179 | |
| 1180 | /* Add x509v3 extensions as specified */ |
| 1181 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1182 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1183 | X509_EXTENSION *ext; |
| 1184 | |
| 1185 | if (!(ext = X509V3_EXT_conf(NULL, &ctx, x509v3_ext_names[i], x509v3_ext_values[i]))) |
| 1186 | goto mkcert_error; |
| 1187 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1188 | X509_EXTENSION_free(ext); |
| 1189 | goto mkcert_error; |
| 1190 | } |
| 1191 | X509_EXTENSION_free(ext); |
| 1192 | } |
| 1193 | |
| 1194 | /* Sign the certificate with the CA private key */ |
| 1195 | if (EVP_PKEY_type(capkey->type) == EVP_PKEY_DSA) |
| 1196 | digest = EVP_dss1(); |
| 1197 | else if (EVP_PKEY_type (capkey->type) == EVP_PKEY_RSA) |
| 1198 | digest = EVP_sha256(); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1199 | else if (EVP_PKEY_type (capkey->type) == EVP_PKEY_EC) |
| 1200 | digest = EVP_sha256(); |
| 1201 | else { |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1202 | #if (OPENSSL_VERSION_NUMBER >= 0x1000000fL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1203 | int nid; |
| 1204 | |
| 1205 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1206 | goto mkcert_error; |
| 1207 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1208 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1209 | #else |
| 1210 | goto mkcert_error; |
| 1211 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1212 | } |
| 1213 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1214 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1215 | goto mkcert_error; |
| 1216 | |
| 1217 | /* Create and set the new SSL_CTX */ |
| 1218 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1219 | goto mkcert_error; |
| 1220 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1221 | goto mkcert_error; |
| 1222 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1223 | goto mkcert_error; |
| 1224 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1225 | goto mkcert_error; |
| 1226 | |
| 1227 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1228 | |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1229 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
| 1230 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1231 | { |
| 1232 | const char *ecdhe = (bind_conf->ecdhe ? bind_conf->ecdhe : ECDHE_DEFAULT_CURVE); |
| 1233 | EC_KEY *ecc; |
| 1234 | int nid; |
| 1235 | |
| 1236 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1237 | goto end; |
| 1238 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1239 | goto end; |
| 1240 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1241 | EC_KEY_free(ecc); |
| 1242 | } |
| 1243 | #endif |
| 1244 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1245 | return ssl_ctx; |
| 1246 | |
| 1247 | mkcert_error: |
| 1248 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1249 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1250 | return NULL; |
| 1251 | } |
| 1252 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1253 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1254 | 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] | 1255 | { |
| 1256 | struct bind_conf *bind_conf = objt_listener(conn->target)->bind_conf; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1257 | |
| 1258 | return ssl_sock_do_create_cert(servername, bind_conf, conn->xprt_ctx); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1261 | /* Do a lookup for a certificate in the LRU cache used to store generated |
| 1262 | * certificates. */ |
| 1263 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1264 | 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] | 1265 | { |
| 1266 | struct lru64 *lru = NULL; |
| 1267 | |
| 1268 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1269 | 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] | 1270 | if (lru && lru->domain) |
| 1271 | return (SSL_CTX *)lru->data; |
| 1272 | } |
| 1273 | return NULL; |
| 1274 | } |
| 1275 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1276 | /* Set a certificate int the LRU cache used to store generated |
| 1277 | * certificate. Return 0 on success, otherwise -1 */ |
| 1278 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1279 | 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] | 1280 | { |
| 1281 | struct lru64 *lru = NULL; |
| 1282 | |
| 1283 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1284 | 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] | 1285 | if (!lru) |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1286 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1287 | if (lru->domain && lru->data) |
| 1288 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1289 | 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] | 1290 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1291 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1292 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1295 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1296 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1297 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1298 | { |
| 1299 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 1300 | } |
| 1301 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1302 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 1303 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 1304 | */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1305 | static SSL_CTX * |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1306 | 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] | 1307 | { |
| 1308 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1309 | SSL_CTX *ssl_ctx = NULL; |
| 1310 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1311 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1312 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1313 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1314 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1315 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1316 | if (lru && lru->domain) |
| 1317 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1318 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1319 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1320 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1321 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1322 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1323 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1324 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1325 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1326 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 1327 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 1328 | SSL_CTX_free(ssl_ctx); |
| 1329 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1330 | return ssl_ctx; |
| 1331 | } |
| 1332 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1333 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 1334 | * warning when no match is found, which implies the default (first) cert |
| 1335 | * will keep being used. |
| 1336 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1337 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, struct bind_conf *s) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1338 | { |
| 1339 | const char *servername; |
| 1340 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1341 | struct ebmb_node *node, *n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1342 | int i; |
| 1343 | (void)al; /* shut gcc stupid warning */ |
| 1344 | |
| 1345 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1346 | if (!servername) { |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1347 | if (s->generate_certs) { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1348 | struct connection *conn = SSL_get_app_data(ssl); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1349 | unsigned int key; |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1350 | SSL_CTX *ctx; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1351 | |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1352 | conn_get_to_addr(conn); |
| 1353 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1354 | key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to)); |
| 1355 | ctx = ssl_sock_get_generated_cert(key, s); |
Willy Tarreau | f672145 | 2015-07-07 18:04:38 +0200 | [diff] [blame] | 1356 | if (ctx) { |
| 1357 | /* switch ctx */ |
| 1358 | SSL_set_SSL_CTX(ssl, ctx); |
| 1359 | return SSL_TLSEXT_ERR_OK; |
| 1360 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1364 | return (s->strict_sni ? |
| 1365 | SSL_TLSEXT_ERR_ALERT_FATAL : |
Emmanuel Hocdet | 79274e2 | 2013-05-31 12:47:44 +0200 | [diff] [blame] | 1366 | SSL_TLSEXT_ERR_NOACK); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1367 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1368 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1369 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1370 | if (!servername[i]) |
| 1371 | break; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1372 | trash.str[i] = tolower(servername[i]); |
| 1373 | if (!wildp && (trash.str[i] == '.')) |
| 1374 | wildp = &trash.str[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1375 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1376 | trash.str[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1377 | |
| 1378 | /* lookup in full qualified names */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1379 | node = ebst_lookup(&s->sni_ctx, trash.str); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1380 | |
| 1381 | /* lookup a not neg filter */ |
| 1382 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 1383 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 1384 | node = n; |
| 1385 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1386 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1387 | } |
| 1388 | if (!node && wildp) { |
| 1389 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1390 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1391 | } |
| 1392 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1393 | SSL_CTX *ctx; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1394 | if (s->generate_certs && |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1395 | (ctx = ssl_sock_generate_certificate(servername, s, ssl))) { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1396 | /* switch ctx */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1397 | return SSL_TLSEXT_ERR_OK; |
| 1398 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1399 | return (s->strict_sni ? |
| 1400 | SSL_TLSEXT_ERR_ALERT_FATAL : |
| 1401 | SSL_TLSEXT_ERR_ALERT_WARNING); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | /* switch ctx */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1405 | SSL_set_SSL_CTX(ssl, container_of(node, struct sni_ctx, name)->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1406 | return SSL_TLSEXT_ERR_OK; |
| 1407 | } |
| 1408 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 1409 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1410 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1411 | |
| 1412 | static DH * ssl_get_dh_1024(void) |
| 1413 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1414 | static unsigned char dh1024_p[]={ |
| 1415 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 1416 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 1417 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 1418 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 1419 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 1420 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 1421 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 1422 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 1423 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 1424 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 1425 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 1426 | }; |
| 1427 | static unsigned char dh1024_g[]={ |
| 1428 | 0x02, |
| 1429 | }; |
| 1430 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1431 | DH *dh = DH_new(); |
| 1432 | if (dh) { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1433 | dh->p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 1434 | dh->g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
| 1435 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1436 | if (!dh->p || !dh->g) { |
| 1437 | DH_free(dh); |
| 1438 | dh = NULL; |
| 1439 | } |
| 1440 | } |
| 1441 | return dh; |
| 1442 | } |
| 1443 | |
| 1444 | static DH *ssl_get_dh_2048(void) |
| 1445 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1446 | static unsigned char dh2048_p[]={ |
| 1447 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 1448 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 1449 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 1450 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 1451 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 1452 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 1453 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 1454 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 1455 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 1456 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 1457 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 1458 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 1459 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 1460 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 1461 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 1462 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 1463 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 1464 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 1465 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 1466 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 1467 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 1468 | 0xB7,0x1F,0x77,0xF3, |
| 1469 | }; |
| 1470 | static unsigned char dh2048_g[]={ |
| 1471 | 0x02, |
| 1472 | }; |
| 1473 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1474 | DH *dh = DH_new(); |
| 1475 | if (dh) { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1476 | dh->p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 1477 | dh->g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
| 1478 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1479 | if (!dh->p || !dh->g) { |
| 1480 | DH_free(dh); |
| 1481 | dh = NULL; |
| 1482 | } |
| 1483 | } |
| 1484 | return dh; |
| 1485 | } |
| 1486 | |
| 1487 | static DH *ssl_get_dh_4096(void) |
| 1488 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1489 | static unsigned char dh4096_p[]={ |
| 1490 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 1491 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 1492 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 1493 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 1494 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 1495 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 1496 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 1497 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 1498 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 1499 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 1500 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 1501 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 1502 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 1503 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 1504 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 1505 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 1506 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 1507 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 1508 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 1509 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 1510 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 1511 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 1512 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 1513 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 1514 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 1515 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 1516 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 1517 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 1518 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 1519 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 1520 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 1521 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 1522 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 1523 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 1524 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 1525 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 1526 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 1527 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 1528 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 1529 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 1530 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 1531 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 1532 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1533 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1534 | static unsigned char dh4096_g[]={ |
| 1535 | 0x02, |
| 1536 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1537 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1538 | DH *dh = DH_new(); |
| 1539 | if (dh) { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1540 | dh->p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 1541 | dh->g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
| 1542 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1543 | if (!dh->p || !dh->g) { |
| 1544 | DH_free(dh); |
| 1545 | dh = NULL; |
| 1546 | } |
| 1547 | } |
| 1548 | return dh; |
| 1549 | } |
| 1550 | |
| 1551 | /* Returns Diffie-Hellman parameters matching the private key length |
| 1552 | but not exceeding global.tune.ssl_default_dh_param */ |
| 1553 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 1554 | { |
| 1555 | DH *dh = NULL; |
| 1556 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
| 1557 | int type = pkey ? EVP_PKEY_type(pkey->type) : EVP_PKEY_NONE; |
| 1558 | |
| 1559 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 1560 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 1561 | */ |
| 1562 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 1563 | keylen = EVP_PKEY_bits(pkey); |
| 1564 | } |
| 1565 | |
| 1566 | if (keylen > global.tune.ssl_default_dh_param) { |
| 1567 | keylen = global.tune.ssl_default_dh_param; |
| 1568 | } |
| 1569 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 1570 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1571 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1572 | } |
| 1573 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1574 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1575 | } |
| 1576 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1577 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | return dh; |
| 1581 | } |
| 1582 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1583 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1584 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1585 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1586 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1587 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1588 | if (in == NULL) |
| 1589 | goto end; |
| 1590 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1591 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1592 | goto end; |
| 1593 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1594 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 1595 | |
| 1596 | end: |
| 1597 | if (in) |
| 1598 | BIO_free(in); |
| 1599 | |
| 1600 | return dh; |
| 1601 | } |
| 1602 | |
| 1603 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 1604 | { |
| 1605 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 1606 | |
| 1607 | if (global_dh) { |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
| 1611 | return -1; |
| 1612 | } |
| 1613 | |
| 1614 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 1615 | if an error occured, and 0 if parameter not found. */ |
| 1616 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 1617 | { |
| 1618 | int ret = -1; |
| 1619 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 1620 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1621 | if (dh) { |
| 1622 | ret = 1; |
| 1623 | SSL_CTX_set_tmp_dh(ctx, dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 1624 | |
| 1625 | if (ssl_dh_ptr_index >= 0) { |
| 1626 | /* store a pointer to the DH params to avoid complaining about |
| 1627 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 1628 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 1629 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1630 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 1631 | else if (global_dh) { |
| 1632 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 1633 | ret = 0; /* DH params not found */ |
| 1634 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1635 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 1636 | /* Clear openssl global errors stack */ |
| 1637 | ERR_clear_error(); |
| 1638 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1639 | if (global.tune.ssl_default_dh_param <= 1024) { |
| 1640 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 1641 | if (local_dh_1024 == NULL) |
| 1642 | local_dh_1024 = ssl_get_dh_1024(); |
| 1643 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1644 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1645 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 1646 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1647 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1648 | } |
| 1649 | else { |
| 1650 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 1651 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 1652 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 1653 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1654 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1655 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1656 | end: |
| 1657 | if (dh) |
| 1658 | DH_free(dh); |
| 1659 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1660 | return ret; |
| 1661 | } |
| 1662 | #endif |
| 1663 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1664 | static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1665 | { |
| 1666 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1667 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1668 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1669 | if (*name == '!') { |
| 1670 | neg = 1; |
| 1671 | name++; |
| 1672 | } |
| 1673 | if (*name == '*') { |
| 1674 | wild = 1; |
| 1675 | name++; |
| 1676 | } |
| 1677 | /* !* filter is a nop */ |
| 1678 | if (neg && wild) |
| 1679 | return order; |
| 1680 | if (*name) { |
| 1681 | int j, len; |
| 1682 | len = strlen(name); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1683 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
| 1684 | for (j = 0; j < len; j++) |
| 1685 | sc->name.key[j] = tolower(name[j]); |
| 1686 | sc->name.key[len] = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1687 | sc->ctx = ctx; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1688 | sc->order = order++; |
| 1689 | sc->neg = neg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1690 | if (wild) |
| 1691 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 1692 | else |
| 1693 | ebst_insert(&s->sni_ctx, &sc->name); |
| 1694 | } |
| 1695 | return order; |
| 1696 | } |
| 1697 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 1698 | |
| 1699 | /* The following code is used for loading multiple crt files into |
| 1700 | * SSL_CTX's based on CN/SAN |
| 1701 | */ |
| 1702 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 1703 | /* This is used to preload the certifcate, private key |
| 1704 | * and Cert Chain of a file passed in via the crt |
| 1705 | * argument |
| 1706 | * |
| 1707 | * This way, we do not have to read the file multiple times |
| 1708 | */ |
| 1709 | struct cert_key_and_chain { |
| 1710 | X509 *cert; |
| 1711 | EVP_PKEY *key; |
| 1712 | unsigned int num_chain_certs; |
| 1713 | /* This is an array of X509 pointers */ |
| 1714 | X509 **chain_certs; |
| 1715 | }; |
| 1716 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 1717 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 1718 | |
| 1719 | struct key_combo_ctx { |
| 1720 | SSL_CTX *ctx; |
| 1721 | int order; |
| 1722 | }; |
| 1723 | |
| 1724 | /* Map used for processing multiple keypairs for a single purpose |
| 1725 | * |
| 1726 | * This maps CN/SNI name to certificate type |
| 1727 | */ |
| 1728 | struct sni_keytype { |
| 1729 | int keytypes; /* BITMASK for keytypes */ |
| 1730 | struct ebmb_node name; /* node holding the servername value */ |
| 1731 | }; |
| 1732 | |
| 1733 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 1734 | /* Frees the contents of a cert_key_and_chain |
| 1735 | */ |
| 1736 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 1737 | { |
| 1738 | int i; |
| 1739 | |
| 1740 | if (!ckch) |
| 1741 | return; |
| 1742 | |
| 1743 | /* Free the certificate and set pointer to NULL */ |
| 1744 | if (ckch->cert) |
| 1745 | X509_free(ckch->cert); |
| 1746 | ckch->cert = NULL; |
| 1747 | |
| 1748 | /* Free the key and set pointer to NULL */ |
| 1749 | if (ckch->key) |
| 1750 | EVP_PKEY_free(ckch->key); |
| 1751 | ckch->key = NULL; |
| 1752 | |
| 1753 | /* Free each certificate in the chain */ |
| 1754 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 1755 | if (ckch->chain_certs[i]) |
| 1756 | X509_free(ckch->chain_certs[i]); |
| 1757 | } |
| 1758 | |
| 1759 | /* Free the chain obj itself and set to NULL */ |
| 1760 | if (ckch->num_chain_certs > 0) { |
| 1761 | free(ckch->chain_certs); |
| 1762 | ckch->num_chain_certs = 0; |
| 1763 | ckch->chain_certs = NULL; |
| 1764 | } |
| 1765 | |
| 1766 | } |
| 1767 | |
| 1768 | /* checks if a key and cert exists in the ckch |
| 1769 | */ |
| 1770 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 1771 | { |
| 1772 | return (ckch->cert != NULL && ckch->key != NULL); |
| 1773 | } |
| 1774 | |
| 1775 | |
| 1776 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 1777 | * This allows us to carry the contents of the file without having to |
| 1778 | * read the file multiple times. |
| 1779 | * |
| 1780 | * returns: |
| 1781 | * 0 on Success |
| 1782 | * 1 on SSL Failure |
| 1783 | * 2 on file not found |
| 1784 | */ |
| 1785 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 1786 | { |
| 1787 | |
| 1788 | BIO *in; |
| 1789 | X509 *ca = NULL; |
| 1790 | int ret = 1; |
| 1791 | |
| 1792 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 1793 | |
| 1794 | in = BIO_new(BIO_s_file()); |
| 1795 | if (in == NULL) |
| 1796 | goto end; |
| 1797 | |
| 1798 | if (BIO_read_filename(in, path) <= 0) |
| 1799 | goto end; |
| 1800 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 1801 | /* Read Private Key */ |
| 1802 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 1803 | if (ckch->key == NULL) { |
| 1804 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 1805 | err && *err ? *err : "", path); |
| 1806 | goto end; |
| 1807 | } |
| 1808 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 1809 | /* Seek back to beginning of file */ |
| 1810 | BIO_reset(in); |
| 1811 | |
| 1812 | /* Read Certificate */ |
| 1813 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 1814 | if (ckch->cert == NULL) { |
| 1815 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 1816 | err && *err ? *err : "", path); |
| 1817 | goto end; |
| 1818 | } |
| 1819 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 1820 | /* Read Certificate Chain */ |
| 1821 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 1822 | /* Grow the chain certs */ |
| 1823 | ckch->num_chain_certs++; |
| 1824 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 1825 | |
| 1826 | /* use - 1 here since we just incremented it above */ |
| 1827 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 1828 | } |
| 1829 | ret = ERR_get_error(); |
| 1830 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 1831 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 1832 | err && *err ? *err : "", path); |
| 1833 | ret = 1; |
| 1834 | goto end; |
| 1835 | } |
| 1836 | |
| 1837 | ret = 0; |
| 1838 | |
| 1839 | end: |
| 1840 | |
| 1841 | ERR_clear_error(); |
| 1842 | if (in) |
| 1843 | BIO_free(in); |
| 1844 | |
| 1845 | /* Something went wrong in one of the reads */ |
| 1846 | if (ret != 0) |
| 1847 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 1848 | |
| 1849 | return ret; |
| 1850 | } |
| 1851 | |
| 1852 | /* Loads the info in ckch into ctx |
| 1853 | * Currently, this does not process any information about ocsp, dhparams or |
| 1854 | * sctl |
| 1855 | * Returns |
| 1856 | * 0 on success |
| 1857 | * 1 on failure |
| 1858 | */ |
| 1859 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 1860 | { |
| 1861 | int i = 0; |
| 1862 | |
| 1863 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 1864 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 1865 | err && *err ? *err : "", path); |
| 1866 | return 1; |
| 1867 | } |
| 1868 | |
| 1869 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 1870 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 1871 | err && *err ? *err : "", path); |
| 1872 | return 1; |
| 1873 | } |
| 1874 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 1875 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 1876 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 1877 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 1878 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 1879 | err && *err ? *err : "", (i+1), path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 1880 | return 1; |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 1885 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 1886 | err && *err ? *err : "", path); |
| 1887 | return 1; |
| 1888 | } |
| 1889 | |
| 1890 | return 0; |
| 1891 | } |
| 1892 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 1893 | |
| 1894 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 1895 | { |
| 1896 | struct sni_keytype *s_kt = NULL; |
| 1897 | struct ebmb_node *node; |
| 1898 | int i; |
| 1899 | |
| 1900 | for (i = 0; i < trash.size; i++) { |
| 1901 | if (!str[i]) |
| 1902 | break; |
| 1903 | trash.str[i] = tolower(str[i]); |
| 1904 | } |
| 1905 | trash.str[i] = 0; |
| 1906 | node = ebst_lookup(sni_keytypes, trash.str); |
| 1907 | if (!node) { |
| 1908 | /* CN not found in tree */ |
| 1909 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 1910 | /* Using memcpy here instead of strncpy. |
| 1911 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 1912 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 1913 | */ |
| 1914 | memcpy(s_kt->name.key, trash.str, i+1); |
| 1915 | s_kt->keytypes = 0; |
| 1916 | ebst_insert(sni_keytypes, &s_kt->name); |
| 1917 | } else { |
| 1918 | /* CN found in tree */ |
| 1919 | s_kt = container_of(node, struct sni_keytype, name); |
| 1920 | } |
| 1921 | |
| 1922 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 1923 | s_kt->keytypes |= 1<<key_index; |
| 1924 | |
| 1925 | } |
| 1926 | |
| 1927 | |
| 1928 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 1929 | * If any are found, group these files into a set of SSL_CTX* |
| 1930 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 1931 | * |
| 1932 | * This will allow the user to explictly group multiple cert/keys for a single purpose |
| 1933 | * |
| 1934 | * Returns |
| 1935 | * 0 on success |
| 1936 | * 1 on failure |
| 1937 | */ |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 1938 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 1939 | { |
| 1940 | char fp[MAXPATHLEN+1] = {0}; |
| 1941 | int n = 0; |
| 1942 | int i = 0; |
| 1943 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 1944 | struct eb_root sni_keytypes_map = { {0} }; |
| 1945 | struct ebmb_node *node; |
| 1946 | struct ebmb_node *next; |
| 1947 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 1948 | * of keytypes |
| 1949 | */ |
| 1950 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 1951 | int rv = 0; |
| 1952 | X509_NAME *xname = NULL; |
| 1953 | char *str = NULL; |
| 1954 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1955 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 1956 | #endif |
| 1957 | |
| 1958 | /* Load all possible certs and keys */ |
| 1959 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 1960 | struct stat buf; |
| 1961 | |
| 1962 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 1963 | if (stat(fp, &buf) == 0) { |
| 1964 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
| 1965 | rv = 1; |
| 1966 | goto end; |
| 1967 | } |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | /* Process each ckch and update keytypes for each CN/SAN |
| 1972 | * for example, if CN/SAN www.a.com is associated with |
| 1973 | * certs with keytype 0 and 2, then at the end of the loop, |
| 1974 | * www.a.com will have: |
| 1975 | * keyindex = 0 | 1 | 4 = 5 |
| 1976 | */ |
| 1977 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 1978 | |
| 1979 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 1980 | continue; |
| 1981 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 1982 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 1983 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 1984 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 1985 | } else { |
| 1986 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 1987 | * so the line that contains logic is marked via comments |
| 1988 | */ |
| 1989 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 1990 | i = -1; |
| 1991 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 1992 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 1993 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 1994 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) { |
| 1995 | /* Important line is here */ |
| 1996 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 1997 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 1998 | OPENSSL_free(str); |
| 1999 | str = NULL; |
| 2000 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2001 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2002 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2003 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2004 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2005 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 2006 | if (names) { |
| 2007 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 2008 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2009 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2010 | if (name->type == GEN_DNS) { |
| 2011 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 2012 | /* Important line is here */ |
| 2013 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2014 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2015 | OPENSSL_free(str); |
| 2016 | str = NULL; |
| 2017 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | } |
| 2021 | } |
| 2022 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2023 | } |
| 2024 | |
| 2025 | /* If no files found, return error */ |
| 2026 | if (eb_is_empty(&sni_keytypes_map)) { |
| 2027 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 2028 | err && *err ? *err : "", path); |
| 2029 | rv = 1; |
| 2030 | goto end; |
| 2031 | } |
| 2032 | |
| 2033 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 2034 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 2035 | * and add each CTX to the SNI tree |
| 2036 | * |
| 2037 | * Some math here: |
| 2038 | * There are 2^n - 1 possibile combinations, each unique |
| 2039 | * combination is denoted by the key in the map. Each key |
| 2040 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 2041 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 2042 | * entry in the array to correspond to the unique combo (key) |
| 2043 | * associated with i. This unique key combo (i) will be associated |
| 2044 | * with combos[i-1] |
| 2045 | */ |
| 2046 | |
| 2047 | node = ebmb_first(&sni_keytypes_map); |
| 2048 | while (node) { |
| 2049 | SSL_CTX *cur_ctx; |
| 2050 | |
| 2051 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 2052 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 2053 | cur_ctx = key_combos[i-1].ctx; |
| 2054 | |
| 2055 | if (cur_ctx == NULL) { |
| 2056 | /* need to create SSL_CTX */ |
| 2057 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
| 2058 | if (cur_ctx == NULL) { |
| 2059 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 2060 | err && *err ? *err : ""); |
| 2061 | rv = 1; |
| 2062 | goto end; |
| 2063 | } |
| 2064 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2065 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2066 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 2067 | if (i & (1<<n)) { |
| 2068 | /* Key combo contains ckch[n] */ |
| 2069 | snprintf(trash.str, trash.size, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 2070 | if (ssl_sock_put_ckch_into_ctx(trash.str, &certs_and_keys[n], cur_ctx, err) != 0) { |
| 2071 | SSL_CTX_free(cur_ctx); |
| 2072 | rv = 1; |
| 2073 | goto end; |
| 2074 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 2075 | |
| 2076 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 2077 | /* Load OCSP Info into context */ |
| 2078 | if (ssl_sock_load_ocsp(cur_ctx, trash.str) < 0) { |
| 2079 | if (err) |
| 2080 | 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", |
| 2081 | *err ? *err : "", path); |
| 2082 | SSL_CTX_free(cur_ctx); |
| 2083 | rv = 1; |
| 2084 | goto end; |
| 2085 | } |
| 2086 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | /* Load DH params into the ctx to support DHE keys */ |
| 2091 | #ifndef OPENSSL_NO_DH |
| 2092 | if (ssl_dh_ptr_index >= 0) |
| 2093 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 2094 | |
| 2095 | rv = ssl_sock_load_dh_params(cur_ctx, NULL); |
| 2096 | if (rv < 0) { |
| 2097 | if (err) |
| 2098 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 2099 | *err ? *err : "", path); |
| 2100 | rv = 1; |
| 2101 | goto end; |
| 2102 | } |
| 2103 | #endif |
| 2104 | |
| 2105 | /* Update key_combos */ |
| 2106 | key_combos[i-1].ctx = cur_ctx; |
| 2107 | } |
| 2108 | |
| 2109 | /* Update SNI Tree */ |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2110 | key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2111 | node = ebmb_next(node); |
| 2112 | } |
| 2113 | |
| 2114 | |
| 2115 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 2116 | if (!bind_conf->default_ctx) { |
| 2117 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 2118 | if (key_combos[i].ctx) { |
| 2119 | bind_conf->default_ctx = key_combos[i].ctx; |
| 2120 | break; |
| 2121 | } |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | end: |
| 2126 | |
| 2127 | if (names) |
| 2128 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 2129 | |
| 2130 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 2131 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 2132 | |
| 2133 | node = ebmb_first(&sni_keytypes_map); |
| 2134 | while (node) { |
| 2135 | next = ebmb_next(node); |
| 2136 | ebmb_delete(node); |
| 2137 | node = next; |
| 2138 | } |
| 2139 | |
| 2140 | return rv; |
| 2141 | } |
| 2142 | #else |
| 2143 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2144 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2145 | { |
| 2146 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 2147 | err && *err ? *err : "", path, strerror(errno)); |
| 2148 | return 1; |
| 2149 | } |
| 2150 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2151 | #endif /* #if OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
| 2152 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2153 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 2154 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 2155 | */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2156 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2157 | { |
| 2158 | BIO *in; |
| 2159 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2160 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2161 | int ret = -1; |
| 2162 | int order = 0; |
| 2163 | X509_NAME *xname; |
| 2164 | char *str; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2165 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2166 | STACK_OF(GENERAL_NAME) *names; |
| 2167 | #endif |
| 2168 | |
| 2169 | in = BIO_new(BIO_s_file()); |
| 2170 | if (in == NULL) |
| 2171 | goto end; |
| 2172 | |
| 2173 | if (BIO_read_filename(in, file) <= 0) |
| 2174 | goto end; |
| 2175 | |
| 2176 | x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata); |
| 2177 | if (x == NULL) |
| 2178 | goto end; |
| 2179 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2180 | if (fcount) { |
| 2181 | while (fcount--) |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2182 | order = ssl_sock_add_cert_sni(ctx, s, sni_filter[fcount], order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2183 | } |
| 2184 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2185 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2186 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 2187 | if (names) { |
| 2188 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 2189 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 2190 | if (name->type == GEN_DNS) { |
| 2191 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2192 | order = ssl_sock_add_cert_sni(ctx, s, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2193 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2194 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2195 | } |
| 2196 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2197 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2198 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2199 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2200 | xname = X509_get_subject_name(x); |
| 2201 | i = -1; |
| 2202 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 2203 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
| 2204 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2205 | order = ssl_sock_add_cert_sni(ctx, s, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2206 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2207 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 2212 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 2213 | goto end; |
| 2214 | |
| 2215 | if (ctx->extra_certs != NULL) { |
| 2216 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 2217 | ctx->extra_certs = NULL; |
| 2218 | } |
| 2219 | |
| 2220 | while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata))) { |
| 2221 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 2222 | X509_free(ca); |
| 2223 | goto end; |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | err = ERR_get_error(); |
| 2228 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 2229 | /* we successfully reached the last cert in the file */ |
| 2230 | ret = 1; |
| 2231 | } |
| 2232 | ERR_clear_error(); |
| 2233 | |
| 2234 | end: |
| 2235 | if (x) |
| 2236 | X509_free(x); |
| 2237 | |
| 2238 | if (in) |
| 2239 | BIO_free(in); |
| 2240 | |
| 2241 | return ret; |
| 2242 | } |
| 2243 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2244 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2245 | { |
| 2246 | int ret; |
| 2247 | SSL_CTX *ctx; |
| 2248 | |
| 2249 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 2250 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2251 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 2252 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2253 | return 1; |
| 2254 | } |
| 2255 | |
| 2256 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2257 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 2258 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2259 | SSL_CTX_free(ctx); |
| 2260 | return 1; |
| 2261 | } |
| 2262 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2263 | ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf, sni_filter, fcount); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2264 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2265 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 2266 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2267 | if (ret < 0) /* serious error, must do that ourselves */ |
| 2268 | SSL_CTX_free(ctx); |
| 2269 | return 1; |
| 2270 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 2271 | |
| 2272 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 2273 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2274 | err && *err ? *err : "", path); |
| 2275 | return 1; |
| 2276 | } |
| 2277 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2278 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 2279 | * the tree, so it will be discovered and cleaned in time. |
| 2280 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2281 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2282 | /* store a NULL pointer to indicate we have not yet loaded |
| 2283 | a custom DH param file */ |
| 2284 | if (ssl_dh_ptr_index >= 0) { |
| 2285 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 2286 | } |
| 2287 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2288 | ret = ssl_sock_load_dh_params(ctx, path); |
| 2289 | if (ret < 0) { |
| 2290 | if (err) |
| 2291 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 2292 | *err ? *err : "", path); |
| 2293 | return 1; |
| 2294 | } |
| 2295 | #endif |
| 2296 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 2297 | #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] | 2298 | ret = ssl_sock_load_ocsp(ctx, path); |
| 2299 | if (ret < 0) { |
| 2300 | if (err) |
| 2301 | 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", |
| 2302 | *err ? *err : "", path); |
| 2303 | return 1; |
| 2304 | } |
| 2305 | #endif |
| 2306 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 2307 | #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] | 2308 | if (sctl_ex_index >= 0) { |
| 2309 | ret = ssl_sock_load_sctl(ctx, path); |
| 2310 | if (ret < 0) { |
| 2311 | if (err) |
| 2312 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 2313 | *err ? *err : "", path); |
| 2314 | return 1; |
| 2315 | } |
| 2316 | } |
| 2317 | #endif |
| 2318 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2319 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2320 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2321 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 2322 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2323 | return 1; |
| 2324 | } |
| 2325 | #endif |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2326 | if (!bind_conf->default_ctx) |
| 2327 | bind_conf->default_ctx = ctx; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2328 | |
| 2329 | return 0; |
| 2330 | } |
| 2331 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 2332 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2333 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 2334 | struct dirent **de_list; |
| 2335 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2336 | DIR *dir; |
| 2337 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 2338 | char *end; |
| 2339 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2340 | int cfgerr = 0; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 2341 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 2342 | int is_bundle; |
| 2343 | int j; |
| 2344 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2345 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2346 | if (stat(path, &buf) == 0) { |
| 2347 | dir = opendir(path); |
| 2348 | if (!dir) |
| 2349 | return ssl_sock_load_cert_file(path, bind_conf, curproxy, NULL, 0, err); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2350 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2351 | /* strip trailing slashes, including first one */ |
| 2352 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 2353 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2354 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2355 | n = scandir(path, &de_list, 0, alphasort); |
| 2356 | if (n < 0) { |
| 2357 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 2358 | err && *err ? *err : "", path, strerror(errno)); |
| 2359 | cfgerr++; |
| 2360 | } |
| 2361 | else { |
| 2362 | for (i = 0; i < n; i++) { |
| 2363 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 2364 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2365 | end = strrchr(de->d_name, '.'); |
| 2366 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 2367 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 2368 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2369 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 2370 | if (stat(fp, &buf) != 0) { |
| 2371 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 2372 | err && *err ? *err : "", fp, strerror(errno)); |
| 2373 | cfgerr++; |
| 2374 | goto ignore_entry; |
| 2375 | } |
| 2376 | if (!S_ISREG(buf.st_mode)) |
| 2377 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 2378 | |
| 2379 | #if OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 2380 | is_bundle = 0; |
| 2381 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 2382 | |
| 2383 | if (end) { |
| 2384 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 2385 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 2386 | is_bundle = 1; |
| 2387 | break; |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | if (is_bundle) { |
| 2392 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 2393 | int dp_len; |
| 2394 | |
| 2395 | dp_len = end - de->d_name; |
| 2396 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 2397 | |
| 2398 | /* increment i and free de until we get to a non-bundle cert |
| 2399 | * Note here that we look at de_list[i + 1] before freeing de |
| 2400 | * this is important since ignore_entry will free de |
| 2401 | */ |
| 2402 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 2403 | free(de); |
| 2404 | i++; |
| 2405 | de = de_list[i]; |
| 2406 | } |
| 2407 | |
| 2408 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2409 | ssl_sock_load_multi_cert(fp, bind_conf, curproxy, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 2410 | |
| 2411 | /* Successfully processed the bundle */ |
| 2412 | goto ignore_entry; |
| 2413 | } |
| 2414 | } |
| 2415 | |
| 2416 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2417 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, curproxy, NULL, 0, err); |
| 2418 | ignore_entry: |
| 2419 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 2420 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2421 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2422 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2423 | closedir(dir); |
| 2424 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2425 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2426 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2427 | cfgerr = ssl_sock_load_multi_cert(path, bind_conf, curproxy, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2428 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2429 | return cfgerr; |
| 2430 | } |
| 2431 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 2432 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 2433 | * done once. Zero is returned if the operation fails. No error is returned |
| 2434 | * if the random is said as not implemented, because we expect that openssl |
| 2435 | * will use another method once needed. |
| 2436 | */ |
| 2437 | static int ssl_initialize_random() |
| 2438 | { |
| 2439 | unsigned char random; |
| 2440 | static int random_initialized = 0; |
| 2441 | |
| 2442 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 2443 | random_initialized = 1; |
| 2444 | |
| 2445 | return random_initialized; |
| 2446 | } |
| 2447 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2448 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 2449 | { |
Emmanuel Hocdet | 5e0e6e4 | 2016-05-13 11:18:50 +0200 | [diff] [blame] | 2450 | char thisline[LINESIZE*CRTLIST_FACTOR]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2451 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 2452 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2453 | int linenum = 0; |
| 2454 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2455 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2456 | if ((f = fopen(file, "r")) == NULL) { |
| 2457 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2458 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2459 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2460 | |
| 2461 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 2462 | int arg; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2463 | int newarg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2464 | char *end; |
Emmanuel Hocdet | 5e0e6e4 | 2016-05-13 11:18:50 +0200 | [diff] [blame] | 2465 | char *args[MAX_LINE_ARGS*CRTLIST_FACTOR + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2466 | char *line = thisline; |
| 2467 | |
| 2468 | linenum++; |
| 2469 | end = line + strlen(line); |
| 2470 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 2471 | /* Check if we reached the limit and the last char is not \n. |
| 2472 | * Watch out for the last line without the terminating '\n'! |
| 2473 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2474 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 2475 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2476 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2477 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2478 | } |
| 2479 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2480 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2481 | newarg = 1; |
| 2482 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2483 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 2484 | /* end of string, end of loop */ |
| 2485 | *line = 0; |
| 2486 | break; |
| 2487 | } |
| 2488 | else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2489 | newarg = 1; |
| 2490 | *line = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2491 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2492 | else if (newarg) { |
Emmanuel Hocdet | 5e0e6e4 | 2016-05-13 11:18:50 +0200 | [diff] [blame] | 2493 | if (arg == MAX_LINE_ARGS*CRTLIST_FACTOR) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2494 | memprintf(err, "too many args on line %d in file '%s'.", |
| 2495 | linenum, file); |
| 2496 | cfgerr = 1; |
| 2497 | break; |
| 2498 | } |
| 2499 | newarg = 0; |
| 2500 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2501 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2502 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2503 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2504 | if (cfgerr) |
| 2505 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2506 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2507 | /* empty line */ |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 2508 | if (!arg) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2509 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2510 | |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 2511 | if (stat(args[0], &buf) == 0) { |
| 2512 | cfgerr = ssl_sock_load_cert_file(args[0], bind_conf, curproxy, &args[1], arg-1, err); |
| 2513 | } else { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 2514 | cfgerr = ssl_sock_load_multi_cert(args[0], bind_conf, curproxy, &args[1], arg-1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 2515 | } |
| 2516 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2517 | if (cfgerr) { |
| 2518 | 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] | 2519 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 2520 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2521 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2522 | fclose(f); |
| 2523 | return cfgerr; |
| 2524 | } |
| 2525 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2526 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 2527 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 2528 | #endif |
| 2529 | |
| 2530 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 2531 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
Willy Tarreau | 7d588ee | 2012-11-26 18:47:31 +0100 | [diff] [blame] | 2532 | #define SSL_renegotiate_pending(arg) 0 |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2533 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2534 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 2535 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 2536 | #endif |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 2537 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 2538 | #define SSL_OP_NO_TICKET 0 |
| 2539 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2540 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 2541 | #define SSL_OP_NO_COMPRESSION 0 |
| 2542 | #endif |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 2543 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 2544 | #define SSL_OP_NO_TLSv1_1 0 |
| 2545 | #endif |
| 2546 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 2547 | #define SSL_OP_NO_TLSv1_2 0 |
| 2548 | #endif |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2549 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 2550 | #define SSL_OP_SINGLE_DH_USE 0 |
| 2551 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2552 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 2553 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 2554 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2555 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 2556 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 2557 | #endif |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 2558 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 2559 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 2560 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2561 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2562 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, SSL_CTX *ctx, struct proxy *curproxy) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2563 | { |
| 2564 | int cfgerr = 0; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 2565 | int verify = SSL_VERIFY_NONE; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 2566 | long ssloptions = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2567 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 2568 | SSL_OP_NO_SSLv2 | |
| 2569 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2570 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2571 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 2572 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
| 2573 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 2574 | long sslmode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2575 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 2576 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 2577 | SSL_MODE_RELEASE_BUFFERS | |
| 2578 | SSL_MODE_SMALL_BUFFERS; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2579 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
Willy Tarreau | a616ba6 | 2014-10-26 06:49:19 +0100 | [diff] [blame] | 2580 | SSL_CIPHER * cipher = NULL; |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 2581 | char cipher_description[128]; |
| 2582 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 2583 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 2584 | which is not ephemeral DH. */ |
| 2585 | const char dhe_description[] = " Kx=DH "; |
| 2586 | const char dhe_export_description[] = " Kx=DH("; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2587 | int idx = 0; |
| 2588 | int dhe_found = 0; |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 2589 | SSL *ssl = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2590 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 2591 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 2592 | if (!ssl_initialize_random()) { |
| 2593 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 2594 | cfgerr++; |
| 2595 | } |
| 2596 | |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 2597 | if (bind_conf->ssl_options & BC_SSL_O_NO_SSLV3) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2598 | ssloptions |= SSL_OP_NO_SSLv3; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 2599 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV10) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2600 | ssloptions |= SSL_OP_NO_TLSv1; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 2601 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV11) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 2602 | ssloptions |= SSL_OP_NO_TLSv1_1; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 2603 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV12) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 2604 | ssloptions |= SSL_OP_NO_TLSv1_2; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 2605 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 2606 | ssloptions |= SSL_OP_NO_TICKET; |
Jérémie Courrèges-Anglas | 17c3f62 | 2015-07-25 16:50:52 -0600 | [diff] [blame] | 2607 | if (bind_conf->ssl_options & BC_SSL_O_USE_SSLV3) { |
| 2608 | #ifndef OPENSSL_NO_SSL3 |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 2609 | 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] | 2610 | #else |
| 2611 | Alert("SSLv3 support requested but unavailable.\n"); |
| 2612 | cfgerr++; |
| 2613 | #endif |
| 2614 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 2615 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV10) |
| 2616 | SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()); |
| 2617 | #if SSL_OP_NO_TLSv1_1 |
| 2618 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV11) |
| 2619 | SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method()); |
| 2620 | #endif |
| 2621 | #if SSL_OP_NO_TLSv1_2 |
| 2622 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV12) |
| 2623 | SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method()); |
| 2624 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2625 | |
| 2626 | SSL_CTX_set_options(ctx, ssloptions); |
| 2627 | SSL_CTX_set_mode(ctx, sslmode); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 2628 | switch (bind_conf->verify) { |
| 2629 | case SSL_SOCK_VERIFY_NONE: |
| 2630 | verify = SSL_VERIFY_NONE; |
| 2631 | break; |
| 2632 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 2633 | verify = SSL_VERIFY_PEER; |
| 2634 | break; |
| 2635 | case SSL_SOCK_VERIFY_REQUIRED: |
| 2636 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 2637 | break; |
| 2638 | } |
| 2639 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 2640 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 2641 | if (bind_conf->ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2642 | /* load CAfile to verify */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 2643 | if (!SSL_CTX_load_verify_locations(ctx, bind_conf->ca_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2644 | Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 2645 | curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2646 | cfgerr++; |
| 2647 | } |
| 2648 | /* set CA names fo client cert request, function returns void */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 2649 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(bind_conf->ca_file)); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2650 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 2651 | else { |
| 2652 | Alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 2653 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 2654 | cfgerr++; |
| 2655 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 2656 | #ifdef X509_V_FLAG_CRL_CHECK |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 2657 | if (bind_conf->crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2658 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 2659 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 2660 | if (!store || !X509_STORE_load_locations(store, bind_conf->crl_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2661 | Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
Alexander Rigbo | fc65af0 | 2015-04-07 14:02:16 +0200 | [diff] [blame] | 2662 | curproxy->id, bind_conf->crl_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2663 | cfgerr++; |
| 2664 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 2665 | else { |
| 2666 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 2667 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2668 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 2669 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2670 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 2671 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2672 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 2673 | #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] | 2674 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 2675 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
| 2676 | Alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 2677 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 2678 | cfgerr++; |
| 2679 | } |
| 2680 | } |
| 2681 | #endif |
| 2682 | |
Emeric Brun | 4f65bff | 2012-11-16 15:11:00 +0100 | [diff] [blame] | 2683 | if (global.tune.ssllifetime) |
| 2684 | SSL_CTX_set_timeout(ctx, global.tune.ssllifetime); |
| 2685 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2686 | shared_context_set_cache(ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2687 | if (bind_conf->ciphers && |
| 2688 | !SSL_CTX_set_cipher_list(ctx, bind_conf->ciphers)) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2689 | Alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2690 | curproxy->id, bind_conf->ciphers, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2691 | cfgerr++; |
| 2692 | } |
| 2693 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2694 | /* If tune.ssl.default-dh-param has not been set, |
| 2695 | neither has ssl-default-dh-file and no static DH |
| 2696 | params were in the certificate file. */ |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2697 | if (global.tune.ssl_default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2698 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2699 | (ssl_dh_ptr_index == -1 || |
| 2700 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 2701 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 2702 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2703 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 2704 | if (ssl) { |
| 2705 | ciphers = SSL_get_ciphers(ssl); |
| 2706 | |
| 2707 | if (ciphers) { |
| 2708 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 2709 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 2710 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 2711 | if (strstr(cipher_description, dhe_description) != NULL || |
| 2712 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 2713 | dhe_found = 1; |
| 2714 | break; |
| 2715 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 2716 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2717 | } |
| 2718 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 2719 | SSL_free(ssl); |
| 2720 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 2721 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2722 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 2723 | if (dhe_found) { |
| 2724 | 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] | 2725 | } |
| 2726 | |
| 2727 | global.tune.ssl_default_dh_param = 1024; |
| 2728 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2729 | |
| 2730 | #ifndef OPENSSL_NO_DH |
| 2731 | if (global.tune.ssl_default_dh_param >= 1024) { |
| 2732 | if (local_dh_1024 == NULL) { |
| 2733 | local_dh_1024 = ssl_get_dh_1024(); |
| 2734 | } |
| 2735 | if (global.tune.ssl_default_dh_param >= 2048) { |
| 2736 | if (local_dh_2048 == NULL) { |
| 2737 | local_dh_2048 = ssl_get_dh_2048(); |
| 2738 | } |
| 2739 | if (global.tune.ssl_default_dh_param >= 4096) { |
| 2740 | if (local_dh_4096 == NULL) { |
| 2741 | local_dh_4096 = ssl_get_dh_4096(); |
| 2742 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2743 | } |
| 2744 | } |
| 2745 | } |
| 2746 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2747 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2748 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 2749 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 2750 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 2751 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 2752 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 2753 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 2754 | if (bind_conf->npn_str) |
| 2755 | SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, bind_conf); |
| 2756 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 2757 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2758 | if (bind_conf->alpn_str) |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 2759 | SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, bind_conf); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2760 | #endif |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 2761 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2762 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2763 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2764 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2765 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2766 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emeric Brun | 6924ef8 | 2013-03-06 14:08:53 +0100 | [diff] [blame] | 2767 | { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2768 | int i; |
| 2769 | EC_KEY *ecdh; |
| 2770 | |
Emeric Brun | 6924ef8 | 2013-03-06 14:08:53 +0100 | [diff] [blame] | 2771 | i = OBJ_sn2nid(bind_conf->ecdhe ? bind_conf->ecdhe : ECDHE_DEFAULT_CURVE); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2772 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
| 2773 | Alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
Emeric Brun | 6924ef8 | 2013-03-06 14:08:53 +0100 | [diff] [blame] | 2774 | curproxy->id, bind_conf->ecdhe ? bind_conf->ecdhe : ECDHE_DEFAULT_CURVE, |
| 2775 | bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 2776 | cfgerr++; |
| 2777 | } |
| 2778 | else { |
| 2779 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 2780 | EC_KEY_free(ecdh); |
| 2781 | } |
| 2782 | } |
| 2783 | #endif |
| 2784 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2785 | return cfgerr; |
| 2786 | } |
| 2787 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2788 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 2789 | { |
| 2790 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 2791 | size_t prefixlen, suffixlen; |
| 2792 | |
| 2793 | /* Trivial case */ |
| 2794 | if (strcmp(pattern, hostname) == 0) |
| 2795 | return 1; |
| 2796 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2797 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 2798 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 2799 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 2800 | pattern_wildcard = NULL; |
| 2801 | pattern_left_label_end = pattern; |
| 2802 | while (*pattern_left_label_end != '.') { |
| 2803 | switch (*pattern_left_label_end) { |
| 2804 | case 0: |
| 2805 | /* End of label not found */ |
| 2806 | return 0; |
| 2807 | case '*': |
| 2808 | /* If there is more than one wildcards */ |
| 2809 | if (pattern_wildcard) |
| 2810 | return 0; |
| 2811 | pattern_wildcard = pattern_left_label_end; |
| 2812 | break; |
| 2813 | } |
| 2814 | pattern_left_label_end++; |
| 2815 | } |
| 2816 | |
| 2817 | /* If it's not trivial and there is no wildcard, it can't |
| 2818 | * match */ |
| 2819 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2820 | return 0; |
| 2821 | |
| 2822 | /* Make sure all labels match except the leftmost */ |
| 2823 | hostname_left_label_end = strchr(hostname, '.'); |
| 2824 | if (!hostname_left_label_end |
| 2825 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 2826 | return 0; |
| 2827 | |
| 2828 | /* Make sure the leftmost label of the hostname is long enough |
| 2829 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 2830 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2831 | return 0; |
| 2832 | |
| 2833 | /* Finally compare the string on either side of the |
| 2834 | * wildcard */ |
| 2835 | prefixlen = pattern_wildcard - pattern; |
| 2836 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 2837 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 2838 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2839 | return 0; |
| 2840 | |
| 2841 | return 1; |
| 2842 | } |
| 2843 | |
| 2844 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 2845 | { |
| 2846 | SSL *ssl; |
| 2847 | struct connection *conn; |
| 2848 | char *servername; |
| 2849 | |
| 2850 | int depth; |
| 2851 | X509 *cert; |
| 2852 | STACK_OF(GENERAL_NAME) *alt_names; |
| 2853 | int i; |
| 2854 | X509_NAME *cert_subject; |
| 2855 | char *str; |
| 2856 | |
| 2857 | if (ok == 0) |
| 2858 | return ok; |
| 2859 | |
| 2860 | 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] | 2861 | conn = SSL_get_app_data(ssl); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2862 | |
| 2863 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
| 2864 | |
| 2865 | /* We only need to verify the CN on the actual server cert, |
| 2866 | * not the indirect CAs */ |
| 2867 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 2868 | if (depth != 0) |
| 2869 | return ok; |
| 2870 | |
| 2871 | /* At this point, the cert is *not* OK unless we can find a |
| 2872 | * hostname match */ |
| 2873 | ok = 0; |
| 2874 | |
| 2875 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 2876 | /* It seems like this might happen if verify peer isn't set */ |
| 2877 | if (!cert) |
| 2878 | return ok; |
| 2879 | |
| 2880 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 2881 | if (alt_names) { |
| 2882 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 2883 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 2884 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 2885 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 2886 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 2887 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2888 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 2889 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2890 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 2891 | OPENSSL_free(str); |
| 2892 | } |
| 2893 | } |
| 2894 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 2895 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | cert_subject = X509_get_subject_name(cert); |
| 2899 | i = -1; |
| 2900 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 2901 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
| 2902 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) { |
| 2903 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 2904 | OPENSSL_free(str); |
| 2905 | } |
| 2906 | } |
| 2907 | |
| 2908 | return ok; |
| 2909 | } |
| 2910 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2911 | /* prepare ssl context from servers options. Returns an error count */ |
| 2912 | int ssl_sock_prepare_srv_ctx(struct server *srv, struct proxy *curproxy) |
| 2913 | { |
| 2914 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 2915 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2916 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 2917 | SSL_OP_NO_SSLv2 | |
| 2918 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 2919 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2920 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 2921 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 2922 | SSL_MODE_RELEASE_BUFFERS | |
| 2923 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 2924 | int verify = SSL_VERIFY_NONE; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2925 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 2926 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 2927 | if (!ssl_initialize_random()) { |
| 2928 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 2929 | cfgerr++; |
| 2930 | } |
| 2931 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 2932 | /* Automatic memory computations need to know we use SSL there */ |
| 2933 | global.ssl_used_backend = 1; |
| 2934 | |
| 2935 | /* Initiate SSL context for current server */ |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2936 | srv->ssl_ctx.reused_sess = NULL; |
| 2937 | if (srv->use_ssl) |
| 2938 | srv->xprt = &ssl_sock; |
| 2939 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 2940 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2941 | |
| 2942 | srv->ssl_ctx.ctx = SSL_CTX_new(SSLv23_client_method()); |
| 2943 | if (!srv->ssl_ctx.ctx) { |
| 2944 | Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 2945 | proxy_type_str(curproxy), curproxy->id, |
| 2946 | srv->id); |
| 2947 | cfgerr++; |
| 2948 | return cfgerr; |
| 2949 | } |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 2950 | if (srv->ssl_ctx.client_crt) { |
| 2951 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
| 2952 | Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 2953 | proxy_type_str(curproxy), curproxy->id, |
| 2954 | srv->id, srv->ssl_ctx.client_crt); |
| 2955 | cfgerr++; |
| 2956 | } |
| 2957 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
| 2958 | Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 2959 | proxy_type_str(curproxy), curproxy->id, |
| 2960 | srv->id, srv->ssl_ctx.client_crt); |
| 2961 | cfgerr++; |
| 2962 | } |
| 2963 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
| 2964 | Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2965 | proxy_type_str(curproxy), curproxy->id, |
| 2966 | srv->id, srv->ssl_ctx.client_crt); |
| 2967 | cfgerr++; |
| 2968 | } |
| 2969 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2970 | |
| 2971 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_SSLV3) |
| 2972 | options |= SSL_OP_NO_SSLv3; |
| 2973 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV10) |
| 2974 | options |= SSL_OP_NO_TLSv1; |
| 2975 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV11) |
| 2976 | options |= SSL_OP_NO_TLSv1_1; |
| 2977 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV12) |
| 2978 | options |= SSL_OP_NO_TLSv1_2; |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 2979 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 2980 | options |= SSL_OP_NO_TICKET; |
Jérémie Courrèges-Anglas | 17c3f62 | 2015-07-25 16:50:52 -0600 | [diff] [blame] | 2981 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_SSLV3) { |
| 2982 | #ifndef OPENSSL_NO_SSL3 |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2983 | 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] | 2984 | #else |
Thierry FOURNIER | bc96534 | 2015-08-26 08:21:26 +0200 | [diff] [blame] | 2985 | Alert("SSLv3 support requested but unavailable.\n"); |
Jérémie Courrèges-Anglas | 17c3f62 | 2015-07-25 16:50:52 -0600 | [diff] [blame] | 2986 | cfgerr++; |
| 2987 | #endif |
| 2988 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2989 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV10) |
| 2990 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_client_method()); |
| 2991 | #if SSL_OP_NO_TLSv1_1 |
| 2992 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV11) |
| 2993 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_1_client_method()); |
| 2994 | #endif |
| 2995 | #if SSL_OP_NO_TLSv1_2 |
| 2996 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV12) |
| 2997 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_2_client_method()); |
| 2998 | #endif |
| 2999 | |
| 3000 | SSL_CTX_set_options(srv->ssl_ctx.ctx, options); |
| 3001 | SSL_CTX_set_mode(srv->ssl_ctx.ctx, mode); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3002 | |
| 3003 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 3004 | verify = SSL_VERIFY_PEER; |
| 3005 | |
| 3006 | switch (srv->ssl_ctx.verify) { |
| 3007 | case SSL_SOCK_VERIFY_NONE: |
| 3008 | verify = SSL_VERIFY_NONE; |
| 3009 | break; |
| 3010 | case SSL_SOCK_VERIFY_REQUIRED: |
| 3011 | verify = SSL_VERIFY_PEER; |
| 3012 | break; |
| 3013 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3014 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3015 | verify, |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3016 | srv->ssl_ctx.verify_host ? ssl_sock_srv_verifycbk : NULL); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3017 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3018 | if (srv->ssl_ctx.ca_file) { |
| 3019 | /* load CAfile to verify */ |
| 3020 | 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] | 3021 | 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] | 3022 | curproxy->id, srv->id, |
| 3023 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
| 3024 | cfgerr++; |
| 3025 | } |
| 3026 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 3027 | else { |
| 3028 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 3029 | 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] | 3030 | curproxy->id, srv->id, |
| 3031 | srv->conf.file, srv->conf.line); |
| 3032 | else |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 3033 | 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] | 3034 | curproxy->id, srv->id, |
| 3035 | srv->conf.file, srv->conf.line); |
| 3036 | cfgerr++; |
| 3037 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 3038 | #ifdef X509_V_FLAG_CRL_CHECK |
| 3039 | if (srv->ssl_ctx.crl_file) { |
| 3040 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 3041 | |
| 3042 | 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] | 3043 | 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] | 3044 | curproxy->id, srv->id, |
| 3045 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
| 3046 | cfgerr++; |
| 3047 | } |
| 3048 | else { |
| 3049 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 3050 | } |
| 3051 | } |
| 3052 | #endif |
| 3053 | } |
| 3054 | |
Emeric Brun | 4f65bff | 2012-11-16 15:11:00 +0100 | [diff] [blame] | 3055 | if (global.tune.ssllifetime) |
| 3056 | SSL_CTX_set_timeout(srv->ssl_ctx.ctx, global.tune.ssllifetime); |
| 3057 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 3058 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF); |
| 3059 | if (srv->ssl_ctx.ciphers && |
| 3060 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
| 3061 | Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 3062 | curproxy->id, srv->id, |
| 3063 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
| 3064 | cfgerr++; |
| 3065 | } |
| 3066 | |
| 3067 | return cfgerr; |
| 3068 | } |
| 3069 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3070 | /* 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] | 3071 | * be NULL, in which case nothing is done. Returns the number of errors |
| 3072 | * encountered. |
| 3073 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3074 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf, struct proxy *px) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3075 | { |
| 3076 | struct ebmb_node *node; |
| 3077 | struct sni_ctx *sni; |
| 3078 | int err = 0; |
| 3079 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3080 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3081 | return 0; |
| 3082 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 3083 | /* Automatic memory computations need to know we use SSL there */ |
| 3084 | global.ssl_used_frontend = 1; |
| 3085 | |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 3086 | if (bind_conf->default_ctx) |
| 3087 | err += ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ctx, px); |
| 3088 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3089 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3090 | while (node) { |
| 3091 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 3092 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 3093 | /* only initialize the CTX on its first occurrence and |
| 3094 | if it is not the default_ctx */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3095 | err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3096 | node = ebmb_next(node); |
| 3097 | } |
| 3098 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3099 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3100 | while (node) { |
| 3101 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 3102 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 3103 | /* only initialize the CTX on its first occurrence and |
| 3104 | if it is not the default_ctx */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3105 | err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3106 | node = ebmb_next(node); |
| 3107 | } |
| 3108 | return err; |
| 3109 | } |
| 3110 | |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 3111 | |
| 3112 | /* release ssl context allocated for servers. */ |
| 3113 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 3114 | { |
| 3115 | if (srv->ssl_ctx.ctx) |
| 3116 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 3117 | } |
| 3118 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3119 | /* 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] | 3120 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 3121 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3122 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3123 | { |
| 3124 | struct ebmb_node *node, *back; |
| 3125 | struct sni_ctx *sni; |
| 3126 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3127 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3128 | return; |
| 3129 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3130 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3131 | while (node) { |
| 3132 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 3133 | back = ebmb_next(node); |
| 3134 | ebmb_delete(node); |
| 3135 | if (!sni->order) /* only free the CTX on its first occurrence */ |
| 3136 | SSL_CTX_free(sni->ctx); |
| 3137 | free(sni); |
| 3138 | node = back; |
| 3139 | } |
| 3140 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3141 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3142 | while (node) { |
| 3143 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 3144 | back = ebmb_next(node); |
| 3145 | ebmb_delete(node); |
| 3146 | if (!sni->order) /* only free the CTX on its first occurrence */ |
| 3147 | SSL_CTX_free(sni->ctx); |
| 3148 | free(sni); |
| 3149 | node = back; |
| 3150 | } |
| 3151 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3152 | bind_conf->default_ctx = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3153 | } |
| 3154 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3155 | /* Load CA cert file and private key used to generate certificates */ |
| 3156 | int |
| 3157 | ssl_sock_load_ca(struct bind_conf *bind_conf, struct proxy *px) |
| 3158 | { |
| 3159 | FILE *fp; |
| 3160 | X509 *cacert = NULL; |
| 3161 | EVP_PKEY *capkey = NULL; |
| 3162 | int err = 0; |
| 3163 | |
| 3164 | if (!bind_conf || !bind_conf->generate_certs) |
| 3165 | return err; |
| 3166 | |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 3167 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 3168 | if (global.tune.ssl_ctx_cache) |
| 3169 | ssl_ctx_lru_tree = lru64_new(global.tune.ssl_ctx_cache); |
| 3170 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 3171 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 3172 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3173 | if (!bind_conf->ca_sign_file) { |
| 3174 | Alert("Proxy '%s': cannot enable certificate generation, " |
| 3175 | "no CA certificate File configured at [%s:%d].\n", |
| 3176 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3177 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3178 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3179 | |
| 3180 | /* read in the CA certificate */ |
| 3181 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
| 3182 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 3183 | 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] | 3184 | goto load_error; |
| 3185 | } |
| 3186 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
| 3187 | Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 3188 | 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] | 3189 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3190 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3191 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3192 | if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) { |
| 3193 | Alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 3194 | 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] | 3195 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3196 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3197 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3198 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3199 | bind_conf->ca_sign_cert = cacert; |
| 3200 | bind_conf->ca_sign_pkey = capkey; |
| 3201 | return err; |
| 3202 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3203 | read_error: |
| 3204 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3205 | if (capkey) EVP_PKEY_free(capkey); |
| 3206 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 3207 | load_error: |
| 3208 | bind_conf->generate_certs = 0; |
| 3209 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3210 | return err; |
| 3211 | } |
| 3212 | |
| 3213 | /* Release CA cert and private key used to generate certificated */ |
| 3214 | void |
| 3215 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 3216 | { |
| 3217 | if (!bind_conf) |
| 3218 | return; |
| 3219 | |
| 3220 | if (bind_conf->ca_sign_pkey) |
| 3221 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 3222 | if (bind_conf->ca_sign_cert) |
| 3223 | X509_free(bind_conf->ca_sign_cert); |
| 3224 | } |
| 3225 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3226 | /* |
| 3227 | * This function is called if SSL * context is not yet allocated. The function |
| 3228 | * is designed to be called before any other data-layer operation and sets the |
| 3229 | * handshake flag on the connection. It is safe to call it multiple times. |
| 3230 | * It returns 0 on success and -1 in error case. |
| 3231 | */ |
| 3232 | static int ssl_sock_init(struct connection *conn) |
| 3233 | { |
| 3234 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3235 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3236 | return 0; |
| 3237 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3238 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 3239 | return 0; |
| 3240 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3241 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 3242 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3243 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3244 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3245 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3246 | /* If it is in client mode initiate SSL session |
| 3247 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3248 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3249 | int may_retry = 1; |
| 3250 | |
| 3251 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3252 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3253 | conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3254 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3255 | if (may_retry--) { |
| 3256 | pool_gc2(); |
| 3257 | goto retry_connect; |
| 3258 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3259 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3260 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3261 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3262 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3263 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3264 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 3265 | SSL_free(conn->xprt_ctx); |
| 3266 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3267 | if (may_retry--) { |
| 3268 | pool_gc2(); |
| 3269 | goto retry_connect; |
| 3270 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3271 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 3272 | return -1; |
| 3273 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3274 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3275 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3276 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 3277 | SSL_free(conn->xprt_ctx); |
| 3278 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3279 | if (may_retry--) { |
| 3280 | pool_gc2(); |
| 3281 | goto retry_connect; |
| 3282 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3283 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 3284 | return -1; |
| 3285 | } |
| 3286 | |
| 3287 | SSL_set_connect_state(conn->xprt_ctx); |
| 3288 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 3289 | if(!SSL_set_session(conn->xprt_ctx, objt_server(conn->target)->ssl_ctx.reused_sess)) { |
| 3290 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 3291 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 3292 | } |
| 3293 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 3294 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3295 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 3296 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3297 | |
| 3298 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 3299 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3300 | return 0; |
| 3301 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3302 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3303 | int may_retry = 1; |
| 3304 | |
| 3305 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3306 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3307 | 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] | 3308 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3309 | if (may_retry--) { |
| 3310 | pool_gc2(); |
| 3311 | goto retry_accept; |
| 3312 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3313 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3314 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3315 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3316 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3317 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3318 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 3319 | SSL_free(conn->xprt_ctx); |
| 3320 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3321 | if (may_retry--) { |
| 3322 | pool_gc2(); |
| 3323 | goto retry_accept; |
| 3324 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3325 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 3326 | return -1; |
| 3327 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3328 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3329 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3330 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 3331 | SSL_free(conn->xprt_ctx); |
| 3332 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 3333 | if (may_retry--) { |
| 3334 | pool_gc2(); |
| 3335 | goto retry_accept; |
| 3336 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 3337 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 3338 | return -1; |
| 3339 | } |
| 3340 | |
| 3341 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3342 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3343 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 3344 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3345 | |
| 3346 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 3347 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3348 | return 0; |
| 3349 | } |
| 3350 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3351 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3352 | return -1; |
| 3353 | } |
| 3354 | |
| 3355 | |
| 3356 | /* This is the callback which is used when an SSL handshake is pending. It |
| 3357 | * updates the FD status if it wants some polling before being called again. |
| 3358 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 3359 | * otherwise it returns non-zero and removes itself from the connection's |
| 3360 | * flags (the bit is provided in <flag> by the caller). |
| 3361 | */ |
| 3362 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 3363 | { |
| 3364 | int ret; |
| 3365 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3366 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 3367 | return 0; |
| 3368 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3369 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3370 | goto out_error; |
| 3371 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3372 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 3373 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 3374 | * Usually SSL_write and SSL_read are used and process implicitly |
| 3375 | * the reneg handshake. |
| 3376 | * Here we use SSL_peek as a workaround for reneg. |
| 3377 | */ |
| 3378 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 3379 | char c; |
| 3380 | |
| 3381 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 3382 | if (ret <= 0) { |
| 3383 | /* handshake may have not been completed, let's find why */ |
| 3384 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 3385 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 3386 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 3387 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 3388 | __conn_sock_want_send(conn); |
| 3389 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3390 | return 0; |
| 3391 | } |
| 3392 | else if (ret == SSL_ERROR_WANT_READ) { |
| 3393 | /* handshake may have been completed but we have |
| 3394 | * no more data to read. |
| 3395 | */ |
| 3396 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 3397 | ret = 1; |
| 3398 | goto reneg_ok; |
| 3399 | } |
| 3400 | /* SSL handshake needs to read, L4 connection is ready */ |
| 3401 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 3402 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 3403 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 3404 | __conn_sock_want_recv(conn); |
| 3405 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3406 | return 0; |
| 3407 | } |
| 3408 | else if (ret == SSL_ERROR_SYSCALL) { |
| 3409 | /* if errno is null, then connection was successfully established */ |
| 3410 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 3411 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3412 | if (!conn->err_code) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3413 | if (!((SSL *)conn->xprt_ctx)->packet_length) { |
| 3414 | if (!errno) { |
| 3415 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 3416 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 3417 | else |
| 3418 | conn->err_code = CO_ER_SSL_EMPTY; |
| 3419 | } |
| 3420 | else { |
| 3421 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 3422 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 3423 | else |
| 3424 | conn->err_code = CO_ER_SSL_ABORT; |
| 3425 | } |
| 3426 | } |
| 3427 | else { |
| 3428 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 3429 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3430 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3431 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 3432 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3433 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3434 | goto out_error; |
| 3435 | } |
| 3436 | else { |
| 3437 | /* Fail on all other handshake errors */ |
| 3438 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 3439 | * buffer, causing an RST to be emitted upon close() on |
| 3440 | * TCP sockets. We first try to drain possibly pending |
| 3441 | * data to avoid this as much as possible. |
| 3442 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 3443 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3444 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 3445 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 3446 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3447 | goto out_error; |
| 3448 | } |
| 3449 | } |
| 3450 | /* read some data: consider handshake completed */ |
| 3451 | goto reneg_ok; |
| 3452 | } |
| 3453 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3454 | ret = SSL_do_handshake(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3455 | if (ret != 1) { |
| 3456 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3457 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3458 | |
| 3459 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 3460 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 3461 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 3462 | __conn_sock_want_send(conn); |
| 3463 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3464 | return 0; |
| 3465 | } |
| 3466 | else if (ret == SSL_ERROR_WANT_READ) { |
| 3467 | /* SSL handshake needs to read, L4 connection is ready */ |
| 3468 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 3469 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 3470 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 3471 | __conn_sock_want_recv(conn); |
| 3472 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3473 | return 0; |
| 3474 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 3475 | else if (ret == SSL_ERROR_SYSCALL) { |
| 3476 | /* if errno is null, then connection was successfully established */ |
| 3477 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 3478 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3479 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3480 | if (!((SSL *)conn->xprt_ctx)->packet_length) { |
| 3481 | if (!errno) { |
| 3482 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 3483 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 3484 | else |
| 3485 | conn->err_code = CO_ER_SSL_EMPTY; |
| 3486 | } |
| 3487 | else { |
| 3488 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 3489 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 3490 | else |
| 3491 | conn->err_code = CO_ER_SSL_ABORT; |
| 3492 | } |
| 3493 | } |
| 3494 | else { |
| 3495 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 3496 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3497 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 3498 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 3499 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 3500 | goto out_error; |
| 3501 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3502 | else { |
| 3503 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 3504 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 3505 | * buffer, causing an RST to be emitted upon close() on |
| 3506 | * TCP sockets. We first try to drain possibly pending |
| 3507 | * data to avoid this as much as possible. |
| 3508 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 3509 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3510 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 3511 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 3512 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3513 | goto out_error; |
| 3514 | } |
| 3515 | } |
| 3516 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 3517 | reneg_ok: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3518 | /* Handshake succeeded */ |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 3519 | if (!SSL_session_reused(conn->xprt_ctx)) { |
| 3520 | if (objt_server(conn->target)) { |
| 3521 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 3522 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 3523 | global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr; |
| 3524 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3525 | /* check if session was reused, if not store current session on server for reuse */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3526 | if (objt_server(conn->target)->ssl_ctx.reused_sess) |
| 3527 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3528 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 3529 | if (!(objt_server(conn->target)->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) |
| 3530 | 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] | 3531 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 3532 | else { |
| 3533 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 3534 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 3535 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 3536 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | /* The connection is now established at both layers, it's time to leave */ |
| 3540 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 3541 | return 1; |
| 3542 | |
| 3543 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3544 | /* Clear openssl global errors stack */ |
| 3545 | ERR_clear_error(); |
| 3546 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 3547 | /* free resumed session if exists */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3548 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 3549 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 3550 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 3551 | } |
| 3552 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3553 | /* Fail on all other handshake errors */ |
| 3554 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 3555 | if (!conn->err_code) |
| 3556 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3557 | return 0; |
| 3558 | } |
| 3559 | |
| 3560 | /* 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] | 3561 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3562 | * buffer wraps, in which case a second call may be performed. The connection's |
| 3563 | * flags are updated with whatever special event is detected (error, read0, |
| 3564 | * empty). The caller is responsible for taking care of those events and |
| 3565 | * avoiding the call if inappropriate. The function does not call the |
| 3566 | * connection's polling update function, so the caller is responsible for this. |
| 3567 | */ |
| 3568 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 3569 | { |
| 3570 | int ret, done = 0; |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 3571 | int try; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3572 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3573 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3574 | goto out_error; |
| 3575 | |
| 3576 | if (conn->flags & CO_FL_HANDSHAKE) |
| 3577 | /* a handshake was requested */ |
| 3578 | return 0; |
| 3579 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 3580 | /* let's realign the buffer to optimize I/O */ |
| 3581 | if (buffer_empty(buf)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3582 | buf->p = buf->data; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3583 | |
| 3584 | /* read the largest possible block. For this, we perform only one call |
| 3585 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 3586 | * in which case we accept to do it once again. A new attempt is made on |
| 3587 | * EINTR too. |
| 3588 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 3589 | while (count > 0) { |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 3590 | /* first check if we have some room after p+i */ |
| 3591 | try = buf->data + buf->size - (buf->p + buf->i); |
| 3592 | /* otherwise continue between data and p-o */ |
| 3593 | if (try <= 0) { |
| 3594 | try = buf->p - (buf->data + buf->o); |
| 3595 | if (try <= 0) |
| 3596 | break; |
| 3597 | } |
| 3598 | if (try > count) |
| 3599 | try = count; |
| 3600 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3601 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3602 | if (conn->flags & CO_FL_ERROR) { |
| 3603 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3604 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3605 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3606 | if (ret > 0) { |
| 3607 | buf->i += ret; |
| 3608 | done += ret; |
| 3609 | if (ret < try) |
| 3610 | break; |
| 3611 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3612 | } |
| 3613 | else if (ret == 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3614 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 3615 | if (ret != SSL_ERROR_ZERO_RETURN) { |
Emeric Brun | 1c64686 | 2012-12-14 12:33:41 +0100 | [diff] [blame] | 3616 | /* error on protocol or underlying transport */ |
| 3617 | if ((ret != SSL_ERROR_SYSCALL) |
| 3618 | || (errno && (errno != EAGAIN))) |
| 3619 | conn->flags |= CO_FL_ERROR; |
| 3620 | |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3621 | /* Clear openssl global errors stack */ |
| 3622 | ERR_clear_error(); |
| 3623 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3624 | goto read0; |
| 3625 | } |
| 3626 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3627 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3628 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 3629 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3630 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 3631 | __conn_sock_want_send(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3632 | break; |
| 3633 | } |
| 3634 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 3635 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 3636 | /* handshake is running, and it may need to re-enable read */ |
| 3637 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 3638 | __conn_sock_want_recv(conn); |
| 3639 | break; |
| 3640 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3641 | /* we need to poll for retry a read later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 3642 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3643 | break; |
| 3644 | } |
| 3645 | /* otherwise it's a real error */ |
| 3646 | goto out_error; |
| 3647 | } |
| 3648 | } |
| 3649 | return done; |
| 3650 | |
| 3651 | read0: |
| 3652 | conn_sock_read0(conn); |
| 3653 | return done; |
| 3654 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3655 | /* Clear openssl global errors stack */ |
| 3656 | ERR_clear_error(); |
| 3657 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3658 | conn->flags |= CO_FL_ERROR; |
| 3659 | return done; |
| 3660 | } |
| 3661 | |
| 3662 | |
| 3663 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 3664 | * <flags> may contain some CO_SFL_* flags to hint the system about other |
| 3665 | * pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3666 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 3667 | * a second call may be performed. The connection's flags are updated with |
| 3668 | * whatever special event is detected (error, empty). The caller is responsible |
| 3669 | * for taking care of those events and avoiding the call if inappropriate. The |
| 3670 | * function does not call the connection's polling update function, so the caller |
| 3671 | * is responsible for this. |
| 3672 | */ |
| 3673 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 3674 | { |
| 3675 | int ret, try, done; |
| 3676 | |
| 3677 | done = 0; |
| 3678 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3679 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3680 | goto out_error; |
| 3681 | |
| 3682 | if (conn->flags & CO_FL_HANDSHAKE) |
| 3683 | /* a handshake was requested */ |
| 3684 | return 0; |
| 3685 | |
| 3686 | /* send the largest possible block. For this we perform only one call |
| 3687 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 3688 | * in which case we accept to do it once again. |
| 3689 | */ |
| 3690 | while (buf->o) { |
Kevin Hester | cad8234 | 2013-05-30 15:12:41 -0700 | [diff] [blame] | 3691 | try = bo_contig_data(buf); |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 3692 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 3693 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 3694 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
| 3695 | global.tune.ssl_max_record && try > global.tune.ssl_max_record) { |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 3696 | try = global.tune.ssl_max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 3697 | } |
| 3698 | else { |
| 3699 | /* we need to keep the information about the fact that |
| 3700 | * we're not limiting the upcoming send(), because if it |
| 3701 | * fails, we'll have to retry with at least as many data. |
| 3702 | */ |
| 3703 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 3704 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 3705 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3706 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 3707 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3708 | if (conn->flags & CO_FL_ERROR) { |
| 3709 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3710 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 3711 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3712 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 3713 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
| 3714 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3715 | buf->o -= ret; |
| 3716 | done += ret; |
| 3717 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 3718 | if (likely(buffer_empty(buf))) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3719 | /* optimize data alignment in the buffer */ |
| 3720 | buf->p = buf->data; |
| 3721 | |
| 3722 | /* if the system buffer is full, don't insist */ |
| 3723 | if (ret < try) |
| 3724 | break; |
| 3725 | } |
| 3726 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3727 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3728 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 3729 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 3730 | /* handshake is running, and it may need to re-enable write */ |
| 3731 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 3732 | __conn_sock_want_send(conn); |
| 3733 | break; |
| 3734 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3735 | /* we need to poll to retry a write later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 3736 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3737 | break; |
| 3738 | } |
| 3739 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 3740 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3741 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 3742 | __conn_sock_want_recv(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3743 | break; |
| 3744 | } |
| 3745 | goto out_error; |
| 3746 | } |
| 3747 | } |
| 3748 | return done; |
| 3749 | |
| 3750 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3751 | /* Clear openssl global errors stack */ |
| 3752 | ERR_clear_error(); |
| 3753 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3754 | conn->flags |= CO_FL_ERROR; |
| 3755 | return done; |
| 3756 | } |
| 3757 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3758 | static void ssl_sock_close(struct connection *conn) { |
| 3759 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3760 | if (conn->xprt_ctx) { |
| 3761 | SSL_free(conn->xprt_ctx); |
| 3762 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 3763 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3764 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 3768 | * any case, flags the connection as reusable if no handshake was in progress. |
| 3769 | */ |
| 3770 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 3771 | { |
| 3772 | if (conn->flags & CO_FL_HANDSHAKE) |
| 3773 | return; |
| 3774 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 3775 | if (clean && (SSL_shutdown(conn->xprt_ctx) <= 0)) { |
| 3776 | /* Clear openssl global errors stack */ |
| 3777 | ERR_clear_error(); |
| 3778 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3779 | |
| 3780 | /* force flag on ssl to keep session in cache regardless shutdown result */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3781 | SSL_set_shutdown(conn->xprt_ctx, SSL_SENT_SHUTDOWN); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3782 | } |
| 3783 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 3784 | /* used for logging, may be changed for a sample fetch later */ |
| 3785 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 3786 | { |
| 3787 | if (!conn->xprt && !conn->xprt_ctx) |
| 3788 | return NULL; |
| 3789 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 3790 | } |
| 3791 | |
| 3792 | /* used for logging, may be changed for a sample fetch later */ |
| 3793 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 3794 | { |
| 3795 | if (!conn->xprt && !conn->xprt_ctx) |
| 3796 | return NULL; |
| 3797 | return SSL_get_version(conn->xprt_ctx); |
| 3798 | } |
| 3799 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 3800 | /* Extract a serial from a cert, and copy it to a chunk. |
| 3801 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 3802 | * -1 if output is not large enough. |
| 3803 | */ |
| 3804 | static int |
| 3805 | ssl_sock_get_serial(X509 *crt, struct chunk *out) |
| 3806 | { |
| 3807 | ASN1_INTEGER *serial; |
| 3808 | |
| 3809 | serial = X509_get_serialNumber(crt); |
| 3810 | if (!serial) |
| 3811 | return 0; |
| 3812 | |
| 3813 | if (out->size < serial->length) |
| 3814 | return -1; |
| 3815 | |
| 3816 | memcpy(out->str, serial->data, serial->length); |
| 3817 | out->len = serial->length; |
| 3818 | return 1; |
| 3819 | } |
| 3820 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 3821 | /* Extract a cert to der, and copy it to a chunk. |
| 3822 | * Returns 1 if cert is found and copied, 0 on der convertion failure and |
| 3823 | * -1 if output is not large enough. |
| 3824 | */ |
| 3825 | static int |
| 3826 | ssl_sock_crt2der(X509 *crt, struct chunk *out) |
| 3827 | { |
| 3828 | int len; |
| 3829 | unsigned char *p = (unsigned char *)out->str;; |
| 3830 | |
| 3831 | len =i2d_X509(crt, NULL); |
| 3832 | if (len <= 0) |
| 3833 | return 1; |
| 3834 | |
| 3835 | if (out->size < len) |
| 3836 | return -1; |
| 3837 | |
| 3838 | i2d_X509(crt,&p); |
| 3839 | out->len = len; |
| 3840 | return 1; |
| 3841 | } |
| 3842 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3843 | |
| 3844 | /* Copy Date in ASN1_UTCTIME format in struct chunk out. |
| 3845 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 3846 | * and -1 if output is not large enough. |
| 3847 | */ |
| 3848 | static int |
| 3849 | ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out) |
| 3850 | { |
| 3851 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 3852 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 3853 | |
| 3854 | if (gentm->length < 12) |
| 3855 | return 0; |
| 3856 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 3857 | return 0; |
| 3858 | if (out->size < gentm->length-2) |
| 3859 | return -1; |
| 3860 | |
| 3861 | memcpy(out->str, gentm->data+2, gentm->length-2); |
| 3862 | out->len = gentm->length-2; |
| 3863 | return 1; |
| 3864 | } |
| 3865 | else if (tm->type == V_ASN1_UTCTIME) { |
| 3866 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 3867 | |
| 3868 | if (utctm->length < 10) |
| 3869 | return 0; |
| 3870 | if (utctm->data[0] >= 0x35) |
| 3871 | return 0; |
| 3872 | if (out->size < utctm->length) |
| 3873 | return -1; |
| 3874 | |
| 3875 | memcpy(out->str, utctm->data, utctm->length); |
| 3876 | out->len = utctm->length; |
| 3877 | return 1; |
| 3878 | } |
| 3879 | |
| 3880 | return 0; |
| 3881 | } |
| 3882 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3883 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 3884 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 3885 | */ |
| 3886 | static int |
| 3887 | ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out) |
| 3888 | { |
| 3889 | X509_NAME_ENTRY *ne; |
| 3890 | int i, j, n; |
| 3891 | int cur = 0; |
| 3892 | const char *s; |
| 3893 | char tmp[128]; |
| 3894 | |
| 3895 | out->len = 0; |
| 3896 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
| 3897 | if (pos < 0) |
| 3898 | j = (sk_X509_NAME_ENTRY_num(a->entries)-1) - i; |
| 3899 | else |
| 3900 | j = i; |
| 3901 | |
| 3902 | ne = sk_X509_NAME_ENTRY_value(a->entries, j); |
| 3903 | n = OBJ_obj2nid(ne->object); |
| 3904 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
| 3905 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), ne->object); |
| 3906 | s = tmp; |
| 3907 | } |
| 3908 | |
| 3909 | if (chunk_strcasecmp(entry, s) != 0) |
| 3910 | continue; |
| 3911 | |
| 3912 | if (pos < 0) |
| 3913 | cur--; |
| 3914 | else |
| 3915 | cur++; |
| 3916 | |
| 3917 | if (cur != pos) |
| 3918 | continue; |
| 3919 | |
| 3920 | if (ne->value->length > out->size) |
| 3921 | return -1; |
| 3922 | |
| 3923 | memcpy(out->str, ne->value->data, ne->value->length); |
| 3924 | out->len = ne->value->length; |
| 3925 | return 1; |
| 3926 | } |
| 3927 | |
| 3928 | return 0; |
| 3929 | |
| 3930 | } |
| 3931 | |
| 3932 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 3933 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 3934 | */ |
| 3935 | static int |
| 3936 | ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out) |
| 3937 | { |
| 3938 | X509_NAME_ENTRY *ne; |
| 3939 | int i, n, ln; |
| 3940 | int l = 0; |
| 3941 | const char *s; |
| 3942 | char *p; |
| 3943 | char tmp[128]; |
| 3944 | |
| 3945 | out->len = 0; |
| 3946 | p = out->str; |
| 3947 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
| 3948 | ne = sk_X509_NAME_ENTRY_value(a->entries, i); |
| 3949 | n = OBJ_obj2nid(ne->object); |
| 3950 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
| 3951 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), ne->object); |
| 3952 | s = tmp; |
| 3953 | } |
| 3954 | ln = strlen(s); |
| 3955 | |
| 3956 | l += 1 + ln + 1 + ne->value->length; |
| 3957 | if (l > out->size) |
| 3958 | return -1; |
| 3959 | out->len = l; |
| 3960 | |
| 3961 | *(p++)='/'; |
| 3962 | memcpy(p, s, ln); |
| 3963 | p += ln; |
| 3964 | *(p++)='='; |
| 3965 | memcpy(p, ne->value->data, ne->value->length); |
| 3966 | p += ne->value->length; |
| 3967 | } |
| 3968 | |
| 3969 | if (!out->len) |
| 3970 | return 0; |
| 3971 | |
| 3972 | return 1; |
| 3973 | } |
| 3974 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 3975 | char *ssl_sock_get_version(struct connection *conn) |
| 3976 | { |
| 3977 | if (!ssl_sock_is_ssl(conn)) |
| 3978 | return NULL; |
| 3979 | |
| 3980 | return (char *)SSL_get_version(conn->xprt_ctx); |
| 3981 | } |
| 3982 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 3983 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 3984 | { |
| 3985 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3986 | if (!ssl_sock_is_ssl(conn)) |
| 3987 | return; |
| 3988 | |
| 3989 | SSL_set_tlsext_host_name(conn->xprt_ctx, hostname); |
| 3990 | #endif |
| 3991 | } |
| 3992 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 3993 | /* Extract peer certificate's common name into the chunk dest |
| 3994 | * Returns |
| 3995 | * the len of the extracted common name |
| 3996 | * or 0 if no CN found in DN |
| 3997 | * or -1 on error case (i.e. no peer certificate) |
| 3998 | */ |
| 3999 | 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] | 4000 | { |
| 4001 | X509 *crt = NULL; |
| 4002 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4003 | const char find_cn[] = "CN"; |
| 4004 | const struct chunk find_cn_chunk = { |
| 4005 | .str = (char *)&find_cn, |
| 4006 | .len = sizeof(find_cn)-1 |
| 4007 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4008 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4009 | |
| 4010 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4011 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4012 | |
| 4013 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4014 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4015 | if (!crt) |
| 4016 | goto out; |
| 4017 | |
| 4018 | name = X509_get_subject_name(crt); |
| 4019 | if (!name) |
| 4020 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4021 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 4022 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 4023 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4024 | if (crt) |
| 4025 | X509_free(crt); |
| 4026 | |
| 4027 | return result; |
| 4028 | } |
| 4029 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 4030 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 4031 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 4032 | { |
| 4033 | X509 *crt = NULL; |
| 4034 | |
| 4035 | if (!ssl_sock_is_ssl(conn)) |
| 4036 | return 0; |
| 4037 | |
| 4038 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4039 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4040 | if (!crt) |
| 4041 | return 0; |
| 4042 | |
| 4043 | X509_free(crt); |
| 4044 | return 1; |
| 4045 | } |
| 4046 | |
| 4047 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 4048 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4049 | { |
| 4050 | if (!ssl_sock_is_ssl(conn)) |
| 4051 | return 0; |
| 4052 | |
| 4053 | return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
| 4054 | } |
| 4055 | |
| 4056 | /* returns result from SSL verify */ |
| 4057 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 4058 | { |
| 4059 | if (!ssl_sock_is_ssl(conn)) |
| 4060 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 4061 | |
| 4062 | return (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
| 4063 | } |
| 4064 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 4065 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 4066 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4067 | /* boolean, returns true if client cert was present */ |
| 4068 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4069 | 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] | 4070 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4071 | struct connection *conn; |
| 4072 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4073 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4074 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4075 | return 0; |
| 4076 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4077 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4078 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4079 | return 0; |
| 4080 | } |
| 4081 | |
| 4082 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4083 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4084 | 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] | 4085 | |
| 4086 | return 1; |
| 4087 | } |
| 4088 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4089 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 4090 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4091 | * should be use. |
| 4092 | */ |
| 4093 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4094 | 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] | 4095 | { |
| 4096 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 4097 | X509 *crt = NULL; |
| 4098 | int ret = 0; |
| 4099 | struct chunk *smp_trash; |
| 4100 | struct connection *conn; |
| 4101 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4102 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4103 | if (!conn || conn->xprt != &ssl_sock) |
| 4104 | return 0; |
| 4105 | |
| 4106 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 4107 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4108 | return 0; |
| 4109 | } |
| 4110 | |
| 4111 | if (cert_peer) |
| 4112 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4113 | else |
| 4114 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 4115 | |
| 4116 | if (!crt) |
| 4117 | goto out; |
| 4118 | |
| 4119 | smp_trash = get_trash_chunk(); |
| 4120 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 4121 | goto out; |
| 4122 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4123 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4124 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4125 | ret = 1; |
| 4126 | out: |
| 4127 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4128 | if (cert_peer && crt) |
| 4129 | X509_free(crt); |
| 4130 | return ret; |
| 4131 | } |
| 4132 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4133 | /* binary, returns serial of certificate in a binary chunk. |
| 4134 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4135 | * should be use. |
| 4136 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4137 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4138 | 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] | 4139 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4140 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4141 | X509 *crt = NULL; |
| 4142 | int ret = 0; |
| 4143 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4144 | struct connection *conn; |
| 4145 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4146 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4147 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4148 | return 0; |
| 4149 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4150 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4151 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4152 | return 0; |
| 4153 | } |
| 4154 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4155 | if (cert_peer) |
| 4156 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4157 | else |
| 4158 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 4159 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4160 | if (!crt) |
| 4161 | goto out; |
| 4162 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4163 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4164 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 4165 | goto out; |
| 4166 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4167 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4168 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4169 | ret = 1; |
| 4170 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4171 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4172 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 4173 | X509_free(crt); |
| 4174 | return ret; |
| 4175 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 4176 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4177 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 4178 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4179 | * should be use. |
| 4180 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4181 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4182 | 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] | 4183 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4184 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4185 | X509 *crt = NULL; |
| 4186 | const EVP_MD *digest; |
| 4187 | int ret = 0; |
| 4188 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4189 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4190 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4191 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4192 | if (!conn || conn->xprt != &ssl_sock) |
| 4193 | return 0; |
| 4194 | |
| 4195 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4196 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4197 | return 0; |
| 4198 | } |
| 4199 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4200 | if (cert_peer) |
| 4201 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4202 | else |
| 4203 | crt = SSL_get_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4204 | if (!crt) |
| 4205 | goto out; |
| 4206 | |
| 4207 | smp_trash = get_trash_chunk(); |
| 4208 | digest = EVP_sha1(); |
| 4209 | X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len); |
| 4210 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4211 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4212 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4213 | ret = 1; |
| 4214 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4215 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4216 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 4217 | X509_free(crt); |
| 4218 | return ret; |
| 4219 | } |
| 4220 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4221 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 4222 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4223 | * should be use. |
| 4224 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4225 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4226 | 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] | 4227 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4228 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4229 | X509 *crt = NULL; |
| 4230 | int ret = 0; |
| 4231 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4232 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4233 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4234 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4235 | if (!conn || conn->xprt != &ssl_sock) |
| 4236 | return 0; |
| 4237 | |
| 4238 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4239 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4240 | return 0; |
| 4241 | } |
| 4242 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4243 | if (cert_peer) |
| 4244 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4245 | else |
| 4246 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4247 | if (!crt) |
| 4248 | goto out; |
| 4249 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4250 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4251 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 4252 | goto out; |
| 4253 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4254 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4255 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4256 | ret = 1; |
| 4257 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4258 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4259 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4260 | X509_free(crt); |
| 4261 | return ret; |
| 4262 | } |
| 4263 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4264 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 4265 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4266 | * should be use. |
| 4267 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4268 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4269 | 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] | 4270 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4271 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4272 | X509 *crt = NULL; |
| 4273 | X509_NAME *name; |
| 4274 | int ret = 0; |
| 4275 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4276 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4277 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4278 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4279 | if (!conn || conn->xprt != &ssl_sock) |
| 4280 | return 0; |
| 4281 | |
| 4282 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4283 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4284 | return 0; |
| 4285 | } |
| 4286 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4287 | if (cert_peer) |
| 4288 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4289 | else |
| 4290 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4291 | if (!crt) |
| 4292 | goto out; |
| 4293 | |
| 4294 | name = X509_get_issuer_name(crt); |
| 4295 | if (!name) |
| 4296 | goto out; |
| 4297 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4298 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4299 | if (args && args[0].type == ARGT_STR) { |
| 4300 | int pos = 1; |
| 4301 | |
| 4302 | if (args[1].type == ARGT_SINT) |
| 4303 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4304 | |
| 4305 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 4306 | goto out; |
| 4307 | } |
| 4308 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 4309 | goto out; |
| 4310 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4311 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4312 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4313 | ret = 1; |
| 4314 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4315 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4316 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4317 | X509_free(crt); |
| 4318 | return ret; |
| 4319 | } |
| 4320 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4321 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 4322 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4323 | * should be use. |
| 4324 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4325 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4326 | 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] | 4327 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4328 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4329 | X509 *crt = NULL; |
| 4330 | int ret = 0; |
| 4331 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4332 | struct connection *conn; |
| 4333 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4334 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4335 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4336 | return 0; |
| 4337 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4338 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4339 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4340 | return 0; |
| 4341 | } |
| 4342 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4343 | if (cert_peer) |
| 4344 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4345 | else |
| 4346 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4347 | if (!crt) |
| 4348 | goto out; |
| 4349 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4350 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4351 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 4352 | goto out; |
| 4353 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4354 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4355 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4356 | ret = 1; |
| 4357 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4358 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4359 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 4360 | X509_free(crt); |
| 4361 | return ret; |
| 4362 | } |
| 4363 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4364 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 4365 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4366 | * should be use. |
| 4367 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4368 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4369 | 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] | 4370 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4371 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4372 | X509 *crt = NULL; |
| 4373 | X509_NAME *name; |
| 4374 | int ret = 0; |
| 4375 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4376 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4377 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4378 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4379 | if (!conn || conn->xprt != &ssl_sock) |
| 4380 | return 0; |
| 4381 | |
| 4382 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4383 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4384 | return 0; |
| 4385 | } |
| 4386 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4387 | if (cert_peer) |
| 4388 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4389 | else |
| 4390 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4391 | if (!crt) |
| 4392 | goto out; |
| 4393 | |
| 4394 | name = X509_get_subject_name(crt); |
| 4395 | if (!name) |
| 4396 | goto out; |
| 4397 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 4398 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4399 | if (args && args[0].type == ARGT_STR) { |
| 4400 | int pos = 1; |
| 4401 | |
| 4402 | if (args[1].type == ARGT_SINT) |
| 4403 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4404 | |
| 4405 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 4406 | goto out; |
| 4407 | } |
| 4408 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 4409 | goto out; |
| 4410 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4411 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4412 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4413 | ret = 1; |
| 4414 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4415 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 4416 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 4417 | X509_free(crt); |
| 4418 | return ret; |
| 4419 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 4420 | |
| 4421 | /* integer, returns true if current session use a client certificate */ |
| 4422 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4423 | 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] | 4424 | { |
| 4425 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4426 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 4427 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4428 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4429 | if (!conn || conn->xprt != &ssl_sock) |
| 4430 | return 0; |
| 4431 | |
| 4432 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 4433 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4434 | return 0; |
| 4435 | } |
| 4436 | |
| 4437 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4438 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 4439 | if (crt) { |
| 4440 | X509_free(crt); |
| 4441 | } |
| 4442 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4443 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4444 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 4445 | return 1; |
| 4446 | } |
| 4447 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4448 | /* integer, returns the certificate version |
| 4449 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4450 | * should be use. |
| 4451 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 4452 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4453 | 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] | 4454 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4455 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 4456 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4457 | struct connection *conn; |
| 4458 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4459 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4460 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 4461 | return 0; |
| 4462 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4463 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 4464 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4465 | return 0; |
| 4466 | } |
| 4467 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4468 | if (cert_peer) |
| 4469 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4470 | else |
| 4471 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 4472 | if (!crt) |
| 4473 | return 0; |
| 4474 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4475 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4476 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 4477 | if (cert_peer) |
| 4478 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4479 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 4480 | |
| 4481 | return 1; |
| 4482 | } |
| 4483 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4484 | /* string, returns the certificate's signature algorithm. |
| 4485 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4486 | * should be use. |
| 4487 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4488 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4489 | 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] | 4490 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4491 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4492 | X509 *crt; |
| 4493 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4494 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4495 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4496 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4497 | if (!conn || conn->xprt != &ssl_sock) |
| 4498 | return 0; |
| 4499 | |
| 4500 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4501 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4502 | return 0; |
| 4503 | } |
| 4504 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4505 | if (cert_peer) |
| 4506 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4507 | else |
| 4508 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4509 | if (!crt) |
| 4510 | return 0; |
| 4511 | |
| 4512 | nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->signature->algorithm)); |
| 4513 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4514 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 4515 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4516 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 4517 | if (cert_peer) |
| 4518 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4519 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 4520 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4521 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4522 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4523 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4524 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4525 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 4526 | if (cert_peer) |
| 4527 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 4528 | |
| 4529 | return 1; |
| 4530 | } |
| 4531 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4532 | /* string, returns the certificate's key algorithm. |
| 4533 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 4534 | * should be use. |
| 4535 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4536 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4537 | 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] | 4538 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4539 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4540 | X509 *crt; |
| 4541 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4542 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4543 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4544 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4545 | if (!conn || conn->xprt != &ssl_sock) |
| 4546 | return 0; |
| 4547 | |
| 4548 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4549 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4550 | return 0; |
| 4551 | } |
| 4552 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4553 | if (cert_peer) |
| 4554 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 4555 | else |
| 4556 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4557 | if (!crt) |
| 4558 | return 0; |
| 4559 | |
| 4560 | nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->key->algor->algorithm)); |
| 4561 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4562 | smp->data.u.str.str = (char *)OBJ_nid2sn(nid); |
| 4563 | if (!smp->data.u.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4564 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 4565 | if (cert_peer) |
| 4566 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4567 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 4568 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4569 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4570 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4571 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4572 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4573 | if (cert_peer) |
| 4574 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 4575 | |
| 4576 | return 1; |
| 4577 | } |
| 4578 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 4579 | /* boolean, returns true if front conn. transport layer is SSL. |
| 4580 | * This function is also usable on backend conn if the fetch keyword 5th |
| 4581 | * char is 'b'. |
| 4582 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 4583 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4584 | 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] | 4585 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 4586 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 4587 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4588 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4589 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4590 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 4591 | return 1; |
| 4592 | } |
| 4593 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 4594 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 4595 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4596 | 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] | 4597 | { |
| 4598 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4599 | struct connection *conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4600 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4601 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4602 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4603 | conn->xprt_ctx && |
| 4604 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 4605 | return 1; |
| 4606 | #else |
| 4607 | return 0; |
| 4608 | #endif |
| 4609 | } |
| 4610 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 4611 | /* boolean, returns true if client session has been resumed */ |
| 4612 | static int |
| 4613 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 4614 | { |
| 4615 | struct connection *conn = objt_conn(smp->sess->origin); |
| 4616 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4617 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4618 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 4619 | conn->xprt_ctx && |
| 4620 | SSL_session_reused(conn->xprt_ctx); |
| 4621 | return 1; |
| 4622 | } |
| 4623 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 4624 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 4625 | * This function is also usable on backend conn if the fetch keyword 5th |
| 4626 | * char is 'b'. |
| 4627 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4628 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4629 | 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] | 4630 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 4631 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 4632 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4633 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 4634 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4635 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4636 | return 0; |
| 4637 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4638 | smp->data.u.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
| 4639 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4640 | return 0; |
| 4641 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4642 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4643 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4644 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4645 | |
| 4646 | return 1; |
| 4647 | } |
| 4648 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 4649 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 4650 | * is SSL. |
| 4651 | * This function is also usable on backend conn if the fetch keyword 5th |
| 4652 | * char is 'b'. |
| 4653 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4654 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4655 | 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] | 4656 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 4657 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 4658 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4659 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 4660 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 4661 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4662 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4663 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4664 | return 0; |
| 4665 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 4666 | if (!SSL_get_cipher_bits(conn->xprt_ctx, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4667 | return 0; |
| 4668 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4669 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4670 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4671 | |
| 4672 | return 1; |
| 4673 | } |
| 4674 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 4675 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 4676 | * This function is also usable on backend conn if the fetch keyword 5th |
| 4677 | * char is 'b'. |
| 4678 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4679 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4680 | 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] | 4681 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 4682 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 4683 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 4684 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4685 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4686 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 4687 | return 0; |
| 4688 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4689 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
| 4690 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4691 | return 0; |
| 4692 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4693 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4694 | |
| 4695 | return 1; |
| 4696 | } |
| 4697 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 4698 | #ifdef OPENSSL_NPN_NEGOTIATED |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 4699 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4700 | 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] | 4701 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4702 | struct connection *conn; |
| 4703 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4704 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4705 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 4706 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4707 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4708 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 4709 | return 0; |
| 4710 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4711 | smp->data.u.str.str = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4712 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4713 | (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] | 4714 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4715 | if (!smp->data.u.str.str) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 4716 | return 0; |
| 4717 | |
| 4718 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 4719 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 4720 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 4721 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4722 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 4723 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4724 | 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] | 4725 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4726 | struct connection *conn; |
| 4727 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4728 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4729 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 4730 | |
Willy Tarreau | e26bf05 | 2015-05-12 10:30:12 +0200 | [diff] [blame] | 4731 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4732 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 4733 | return 0; |
| 4734 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4735 | smp->data.u.str.str = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4736 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4737 | (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] | 4738 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4739 | if (!smp->data.u.str.str) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 4740 | return 0; |
| 4741 | |
| 4742 | return 1; |
| 4743 | } |
| 4744 | #endif |
| 4745 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 4746 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 4747 | * This function is also usable on backend conn if the fetch keyword 5th |
| 4748 | * char is 'b'. |
| 4749 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 4750 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4751 | 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] | 4752 | { |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 4753 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 4754 | smp->strm ? smp->strm->si[1].end : NULL); |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 4755 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4756 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4757 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 4758 | return 0; |
| 4759 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4760 | smp->data.u.str.str = (char *)SSL_get_version(conn->xprt_ctx); |
| 4761 | if (!smp->data.u.str.str) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4762 | return 0; |
| 4763 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4764 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4765 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4766 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4767 | |
| 4768 | return 1; |
| 4769 | } |
| 4770 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4771 | /* 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] | 4772 | * This function is also usable on backend conn if the fetch keyword 5th |
| 4773 | * char is 'b'. |
| 4774 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 4775 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4776 | 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] | 4777 | { |
| 4778 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 4779 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 4780 | smp->strm ? smp->strm->si[1].end : NULL); |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 4781 | |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 4782 | SSL_SESSION *ssl_sess; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 4783 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4784 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4785 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 4786 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4787 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 4788 | return 0; |
| 4789 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4790 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 4791 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 4792 | return 0; |
| 4793 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4794 | smp->data.u.str.str = (char *)SSL_SESSION_get_id(ssl_sess, (unsigned int *)&smp->data.u.str.len); |
| 4795 | if (!smp->data.u.str.str || !smp->data.u.str.len) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 4796 | return 0; |
| 4797 | |
| 4798 | return 1; |
| 4799 | #else |
| 4800 | return 0; |
| 4801 | #endif |
| 4802 | } |
| 4803 | |
| 4804 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4805 | 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] | 4806 | { |
| 4807 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4808 | struct connection *conn; |
| 4809 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4810 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4811 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 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_ctx || conn->xprt != &ssl_sock) |
| 4815 | return 0; |
| 4816 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4817 | smp->data.u.str.str = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
| 4818 | if (!smp->data.u.str.str) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 4819 | return 0; |
| 4820 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4821 | smp->data.u.str.len = strlen(smp->data.u.str.str); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 4822 | return 1; |
| 4823 | #else |
| 4824 | return 0; |
| 4825 | #endif |
| 4826 | } |
| 4827 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 4828 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4829 | 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] | 4830 | { |
| 4831 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 4832 | struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin : |
| 4833 | smp->strm ? smp->strm->si[1].end : NULL); |
| 4834 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 4835 | int finished_len; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 4836 | struct chunk *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 4837 | |
| 4838 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 4839 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 4840 | return 0; |
| 4841 | |
| 4842 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 4843 | smp->flags |= SMP_F_MAY_CHANGE; |
| 4844 | return 0; |
| 4845 | } |
| 4846 | |
| 4847 | finished_trash = get_trash_chunk(); |
| 4848 | if (!SSL_session_reused(conn->xprt_ctx)) |
| 4849 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 4850 | else |
| 4851 | finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 4852 | |
| 4853 | if (!finished_len) |
| 4854 | return 0; |
| 4855 | |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 4856 | finished_trash->len = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4857 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4858 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 4859 | |
| 4860 | return 1; |
| 4861 | #else |
| 4862 | return 0; |
| 4863 | #endif |
| 4864 | } |
| 4865 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 4866 | /* 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] | 4867 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4868 | 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] | 4869 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4870 | struct connection *conn; |
| 4871 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4872 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4873 | if (!conn || conn->xprt != &ssl_sock) |
| 4874 | return 0; |
| 4875 | |
| 4876 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 4877 | smp->flags = SMP_F_MAY_CHANGE; |
| 4878 | return 0; |
| 4879 | } |
| 4880 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4881 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4882 | 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] | 4883 | smp->flags = 0; |
| 4884 | |
| 4885 | return 1; |
| 4886 | } |
| 4887 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 4888 | /* 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] | 4889 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4890 | 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] | 4891 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4892 | struct connection *conn; |
| 4893 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4894 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4895 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 4896 | return 0; |
| 4897 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4898 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 4899 | smp->flags = SMP_F_MAY_CHANGE; |
| 4900 | return 0; |
| 4901 | } |
| 4902 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4903 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4904 | 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] | 4905 | smp->flags = 0; |
| 4906 | |
| 4907 | return 1; |
| 4908 | } |
| 4909 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 4910 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 4911 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4912 | 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] | 4913 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4914 | struct connection *conn; |
| 4915 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4916 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4917 | if (!conn || conn->xprt != &ssl_sock) |
| 4918 | return 0; |
| 4919 | |
| 4920 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 4921 | smp->flags = SMP_F_MAY_CHANGE; |
| 4922 | return 0; |
| 4923 | } |
| 4924 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4925 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4926 | 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] | 4927 | smp->flags = 0; |
| 4928 | |
| 4929 | return 1; |
| 4930 | } |
| 4931 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 4932 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 4933 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4934 | 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] | 4935 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4936 | struct connection *conn; |
| 4937 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4938 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4939 | if (!conn || conn->xprt != &ssl_sock) |
| 4940 | return 0; |
| 4941 | |
| 4942 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 4943 | smp->flags = SMP_F_MAY_CHANGE; |
| 4944 | return 0; |
| 4945 | } |
| 4946 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4947 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 4948 | return 0; |
| 4949 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4950 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 4951 | 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] | 4952 | smp->flags = 0; |
| 4953 | |
| 4954 | return 1; |
| 4955 | } |
| 4956 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 4957 | /* parse the "ca-file" bind keyword */ |
| 4958 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4959 | { |
| 4960 | if (!*args[cur_arg + 1]) { |
| 4961 | if (err) |
| 4962 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 4963 | return ERR_ALERT | ERR_FATAL; |
| 4964 | } |
| 4965 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4966 | if ((*args[cur_arg + 1] != '/') && global.ca_base) |
| 4967 | memprintf(&conf->ca_file, "%s/%s", global.ca_base, args[cur_arg + 1]); |
| 4968 | else |
| 4969 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 4970 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4971 | return 0; |
| 4972 | } |
| 4973 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4974 | /* parse the "ca-sign-file" bind keyword */ |
| 4975 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4976 | { |
| 4977 | if (!*args[cur_arg + 1]) { |
| 4978 | if (err) |
| 4979 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 4980 | return ERR_ALERT | ERR_FATAL; |
| 4981 | } |
| 4982 | |
| 4983 | if ((*args[cur_arg + 1] != '/') && global.ca_base) |
| 4984 | memprintf(&conf->ca_sign_file, "%s/%s", global.ca_base, args[cur_arg + 1]); |
| 4985 | else |
| 4986 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 4987 | |
| 4988 | return 0; |
| 4989 | } |
| 4990 | |
| 4991 | /* parse the ca-sign-pass bind keyword */ |
| 4992 | |
| 4993 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4994 | { |
| 4995 | if (!*args[cur_arg + 1]) { |
| 4996 | if (err) |
| 4997 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 4998 | return ERR_ALERT | ERR_FATAL; |
| 4999 | } |
| 5000 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 5001 | return 0; |
| 5002 | } |
| 5003 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5004 | /* parse the "ciphers" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5005 | static int bind_parse_ciphers(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] | 5006 | { |
| 5007 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 5008 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5009 | return ERR_ALERT | ERR_FATAL; |
| 5010 | } |
| 5011 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 5012 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5013 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5014 | return 0; |
| 5015 | } |
| 5016 | |
| 5017 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5018 | 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] | 5019 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 5020 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 5021 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5022 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 5023 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5024 | return ERR_ALERT | ERR_FATAL; |
| 5025 | } |
| 5026 | |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5027 | if ((*args[cur_arg + 1] != '/' ) && global.crt_base) { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 5028 | if ((strlen(global.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) { |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5029 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 5030 | return ERR_ALERT | ERR_FATAL; |
| 5031 | } |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 5032 | snprintf(path, sizeof(path), "%s/%s", global.crt_base, args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5033 | if (ssl_sock_load_cert(path, conf, px, err) > 0) |
| 5034 | return ERR_ALERT | ERR_FATAL; |
| 5035 | |
| 5036 | return 0; |
| 5037 | } |
| 5038 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5039 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, px, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5040 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5041 | |
| 5042 | return 0; |
| 5043 | } |
| 5044 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5045 | /* parse the "crt-list" bind keyword */ |
| 5046 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5047 | { |
| 5048 | if (!*args[cur_arg + 1]) { |
| 5049 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 5050 | return ERR_ALERT | ERR_FATAL; |
| 5051 | } |
| 5052 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 5053 | if (ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err) > 0) { |
| 5054 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5055 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 5056 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 5057 | |
| 5058 | return 0; |
| 5059 | } |
| 5060 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 5061 | /* parse the "crl-file" bind keyword */ |
| 5062 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5063 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5064 | #ifndef X509_V_FLAG_CRL_CHECK |
| 5065 | if (err) |
| 5066 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 5067 | return ERR_ALERT | ERR_FATAL; |
| 5068 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5069 | if (!*args[cur_arg + 1]) { |
| 5070 | if (err) |
| 5071 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 5072 | return ERR_ALERT | ERR_FATAL; |
| 5073 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5074 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5075 | if ((*args[cur_arg + 1] != '/') && global.ca_base) |
| 5076 | memprintf(&conf->crl_file, "%s/%s", global.ca_base, args[cur_arg + 1]); |
| 5077 | else |
| 5078 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 5079 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5080 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5081 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5082 | } |
| 5083 | |
| 5084 | /* parse the "ecdhe" bind keyword keywords */ |
| 5085 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5086 | { |
| 5087 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 5088 | if (err) |
| 5089 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 5090 | return ERR_ALERT | ERR_FATAL; |
| 5091 | #elif defined(OPENSSL_NO_ECDH) |
| 5092 | if (err) |
| 5093 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 5094 | return ERR_ALERT | ERR_FATAL; |
| 5095 | #else |
| 5096 | if (!*args[cur_arg + 1]) { |
| 5097 | if (err) |
| 5098 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 5099 | return ERR_ALERT | ERR_FATAL; |
| 5100 | } |
| 5101 | |
| 5102 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5103 | |
| 5104 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5105 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5106 | } |
| 5107 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 5108 | /* parse the "crt_ignerr" and "ca_ignerr" bind keywords */ |
| 5109 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5110 | { |
| 5111 | int code; |
| 5112 | char *p = args[cur_arg + 1]; |
| 5113 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 5114 | |
| 5115 | if (!*p) { |
| 5116 | if (err) |
| 5117 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 5118 | return ERR_ALERT | ERR_FATAL; |
| 5119 | } |
| 5120 | |
| 5121 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 5122 | ignerr = &conf->ca_ignerr; |
| 5123 | |
| 5124 | if (strcmp(p, "all") == 0) { |
| 5125 | *ignerr = ~0ULL; |
| 5126 | return 0; |
| 5127 | } |
| 5128 | |
| 5129 | while (p) { |
| 5130 | code = atoi(p); |
| 5131 | if ((code <= 0) || (code > 63)) { |
| 5132 | if (err) |
| 5133 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 5134 | args[cur_arg], code, args[cur_arg + 1]); |
| 5135 | return ERR_ALERT | ERR_FATAL; |
| 5136 | } |
| 5137 | *ignerr |= 1ULL << code; |
| 5138 | p = strchr(p, ','); |
| 5139 | if (p) |
| 5140 | p++; |
| 5141 | } |
| 5142 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5143 | return 0; |
| 5144 | } |
| 5145 | |
| 5146 | /* parse the "force-sslv3" bind keyword */ |
| 5147 | static int bind_parse_force_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5148 | { |
| 5149 | conf->ssl_options |= BC_SSL_O_USE_SSLV3; |
| 5150 | return 0; |
| 5151 | } |
| 5152 | |
| 5153 | /* parse the "force-tlsv10" bind keyword */ |
| 5154 | static int bind_parse_force_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5155 | { |
| 5156 | conf->ssl_options |= BC_SSL_O_USE_TLSV10; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 5157 | return 0; |
| 5158 | } |
| 5159 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 5160 | /* parse the "force-tlsv11" bind keyword */ |
| 5161 | static int bind_parse_force_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5162 | { |
| 5163 | #if SSL_OP_NO_TLSv1_1 |
| 5164 | conf->ssl_options |= BC_SSL_O_USE_TLSV11; |
| 5165 | return 0; |
| 5166 | #else |
| 5167 | if (err) |
| 5168 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[cur_arg]); |
| 5169 | return ERR_ALERT | ERR_FATAL; |
| 5170 | #endif |
| 5171 | } |
| 5172 | |
| 5173 | /* parse the "force-tlsv12" bind keyword */ |
| 5174 | static int bind_parse_force_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5175 | { |
| 5176 | #if SSL_OP_NO_TLSv1_2 |
| 5177 | conf->ssl_options |= BC_SSL_O_USE_TLSV12; |
| 5178 | return 0; |
| 5179 | #else |
| 5180 | if (err) |
| 5181 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[cur_arg]); |
| 5182 | return ERR_ALERT | ERR_FATAL; |
| 5183 | #endif |
| 5184 | } |
| 5185 | |
| 5186 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 5187 | /* parse the "no-tls-tickets" bind keyword */ |
| 5188 | static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5189 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5190 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 5191 | return 0; |
| 5192 | } |
| 5193 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 5194 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 5195 | /* parse the "no-sslv3" bind keyword */ |
| 5196 | static int bind_parse_no_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5197 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5198 | conf->ssl_options |= BC_SSL_O_NO_SSLV3; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5199 | return 0; |
| 5200 | } |
| 5201 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 5202 | /* parse the "no-tlsv10" bind keyword */ |
| 5203 | static int bind_parse_no_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5204 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5205 | conf->ssl_options |= BC_SSL_O_NO_TLSV10; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5206 | return 0; |
| 5207 | } |
| 5208 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 5209 | /* parse the "no-tlsv11" bind keyword */ |
| 5210 | static int bind_parse_no_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5211 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5212 | conf->ssl_options |= BC_SSL_O_NO_TLSV11; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 5213 | return 0; |
| 5214 | } |
| 5215 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 5216 | /* parse the "no-tlsv12" bind keyword */ |
| 5217 | static int bind_parse_no_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5218 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 5219 | conf->ssl_options |= BC_SSL_O_NO_TLSV12; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5220 | return 0; |
| 5221 | } |
| 5222 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5223 | /* parse the "npn" bind keyword */ |
| 5224 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5225 | { |
| 5226 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 5227 | char *p1, *p2; |
| 5228 | |
| 5229 | if (!*args[cur_arg + 1]) { |
| 5230 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 5231 | return ERR_ALERT | ERR_FATAL; |
| 5232 | } |
| 5233 | |
| 5234 | free(conf->npn_str); |
| 5235 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 5236 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 5237 | * so we reuse each comma to store the next <len> and need |
| 5238 | * one more for the end of the string. |
| 5239 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5240 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 5241 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 5242 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 5243 | |
| 5244 | /* replace commas with the name length */ |
| 5245 | p1 = conf->npn_str; |
| 5246 | p2 = p1 + 1; |
| 5247 | while (1) { |
| 5248 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 5249 | if (!p2) |
| 5250 | p2 = p1 + 1 + strlen(p1 + 1); |
| 5251 | |
| 5252 | if (p2 - (p1 + 1) > 255) { |
| 5253 | *p2 = '\0'; |
| 5254 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 5255 | return ERR_ALERT | ERR_FATAL; |
| 5256 | } |
| 5257 | |
| 5258 | *p1 = p2 - (p1 + 1); |
| 5259 | p1 = p2; |
| 5260 | |
| 5261 | if (!*p2) |
| 5262 | break; |
| 5263 | |
| 5264 | *(p2++) = '\0'; |
| 5265 | } |
| 5266 | return 0; |
| 5267 | #else |
| 5268 | if (err) |
| 5269 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 5270 | return ERR_ALERT | ERR_FATAL; |
| 5271 | #endif |
| 5272 | } |
| 5273 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5274 | /* parse the "alpn" bind keyword */ |
| 5275 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5276 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5277 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5278 | char *p1, *p2; |
| 5279 | |
| 5280 | if (!*args[cur_arg + 1]) { |
| 5281 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 5282 | return ERR_ALERT | ERR_FATAL; |
| 5283 | } |
| 5284 | |
| 5285 | free(conf->alpn_str); |
| 5286 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 5287 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 5288 | * so we reuse each comma to store the next <len> and need |
| 5289 | * one more for the end of the string. |
| 5290 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5291 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 5292 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 5293 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 5294 | |
| 5295 | /* replace commas with the name length */ |
| 5296 | p1 = conf->alpn_str; |
| 5297 | p2 = p1 + 1; |
| 5298 | while (1) { |
| 5299 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 5300 | if (!p2) |
| 5301 | p2 = p1 + 1 + strlen(p1 + 1); |
| 5302 | |
| 5303 | if (p2 - (p1 + 1) > 255) { |
| 5304 | *p2 = '\0'; |
| 5305 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 5306 | return ERR_ALERT | ERR_FATAL; |
| 5307 | } |
| 5308 | |
| 5309 | *p1 = p2 - (p1 + 1); |
| 5310 | p1 = p2; |
| 5311 | |
| 5312 | if (!*p2) |
| 5313 | break; |
| 5314 | |
| 5315 | *(p2++) = '\0'; |
| 5316 | } |
| 5317 | return 0; |
| 5318 | #else |
| 5319 | if (err) |
| 5320 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 5321 | return ERR_ALERT | ERR_FATAL; |
| 5322 | #endif |
| 5323 | } |
| 5324 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5325 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5326 | 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] | 5327 | { |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 5328 | struct listener *l; |
| 5329 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 5330 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 5331 | |
| 5332 | if (global.listen_default_ciphers && !conf->ciphers) |
| 5333 | conf->ciphers = strdup(global.listen_default_ciphers); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 5334 | conf->ssl_options |= global.listen_default_ssloptions; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 5335 | |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 5336 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5337 | l->xprt = &ssl_sock; |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 5338 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5339 | return 0; |
| 5340 | } |
| 5341 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5342 | /* parse the "generate-certificates" bind keyword */ |
| 5343 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5344 | { |
| 5345 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5346 | conf->generate_certs = 1; |
| 5347 | #else |
| 5348 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 5349 | err && *err ? *err : ""); |
| 5350 | #endif |
| 5351 | return 0; |
| 5352 | } |
| 5353 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 5354 | /* parse the "strict-sni" bind keyword */ |
| 5355 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5356 | { |
| 5357 | conf->strict_sni = 1; |
| 5358 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5359 | } |
| 5360 | |
| 5361 | /* parse the "tls-ticket-keys" bind keyword */ |
| 5362 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5363 | { |
| 5364 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 5365 | FILE *f; |
| 5366 | int i = 0; |
| 5367 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 5368 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5369 | |
| 5370 | if (!*args[cur_arg + 1]) { |
| 5371 | if (err) |
| 5372 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 5373 | return ERR_ALERT | ERR_FATAL; |
| 5374 | } |
| 5375 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 5376 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
| 5377 | if(keys_ref) { |
| 5378 | conf->keys_ref = keys_ref; |
| 5379 | return 0; |
| 5380 | } |
| 5381 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 5382 | keys_ref = malloc(sizeof(*keys_ref)); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 5383 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct tls_sess_key)); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5384 | |
| 5385 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 5386 | if (err) |
| 5387 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 5388 | return ERR_ALERT | ERR_FATAL; |
| 5389 | } |
| 5390 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 5391 | keys_ref->filename = strdup(args[cur_arg + 1]); |
| 5392 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5393 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 5394 | int len = strlen(thisline); |
| 5395 | /* Strip newline characters from the end */ |
| 5396 | if(thisline[len - 1] == '\n') |
| 5397 | thisline[--len] = 0; |
| 5398 | |
| 5399 | if(thisline[len - 1] == '\r') |
| 5400 | thisline[--len] = 0; |
| 5401 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 5402 | 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] | 5403 | if (err) |
| 5404 | 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] | 5405 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5406 | return ERR_ALERT | ERR_FATAL; |
| 5407 | } |
| 5408 | i++; |
| 5409 | } |
| 5410 | |
| 5411 | if (i < TLS_TICKETS_NO) { |
| 5412 | if (err) |
| 5413 | 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] | 5414 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5415 | return ERR_ALERT | ERR_FATAL; |
| 5416 | } |
| 5417 | |
| 5418 | fclose(f); |
| 5419 | |
| 5420 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 5421 | i -= 2; |
| 5422 | 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] | 5423 | keys_ref->unique_id = -1; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 5424 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5425 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 5426 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 5427 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5428 | return 0; |
| 5429 | #else |
| 5430 | if (err) |
| 5431 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 5432 | return ERR_ALERT | ERR_FATAL; |
| 5433 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 5434 | } |
| 5435 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5436 | /* parse the "verify" bind keyword */ |
| 5437 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 5438 | { |
| 5439 | if (!*args[cur_arg + 1]) { |
| 5440 | if (err) |
| 5441 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 5442 | return ERR_ALERT | ERR_FATAL; |
| 5443 | } |
| 5444 | |
| 5445 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5446 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5447 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5448 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5449 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5450 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5451 | else { |
| 5452 | if (err) |
| 5453 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 5454 | args[cur_arg], args[cur_arg + 1]); |
| 5455 | return ERR_ALERT | ERR_FATAL; |
| 5456 | } |
| 5457 | |
| 5458 | return 0; |
| 5459 | } |
| 5460 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 5461 | /************** "server" keywords ****************/ |
| 5462 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5463 | /* parse the "ca-file" server keyword */ |
| 5464 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5465 | { |
| 5466 | if (!*args[*cur_arg + 1]) { |
| 5467 | if (err) |
| 5468 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 5469 | return ERR_ALERT | ERR_FATAL; |
| 5470 | } |
| 5471 | |
| 5472 | if ((*args[*cur_arg + 1] != '/') && global.ca_base) |
| 5473 | memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global.ca_base, args[*cur_arg + 1]); |
| 5474 | else |
| 5475 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 5476 | |
| 5477 | return 0; |
| 5478 | } |
| 5479 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 5480 | /* parse the "check-ssl" server keyword */ |
| 5481 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5482 | { |
| 5483 | newsrv->check.use_ssl = 1; |
| 5484 | if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 5485 | newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 5486 | newsrv->ssl_ctx.options |= global.connect_default_ssloptions; |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 5487 | return 0; |
| 5488 | } |
| 5489 | |
| 5490 | /* parse the "ciphers" server keyword */ |
| 5491 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5492 | { |
| 5493 | if (!*args[*cur_arg + 1]) { |
| 5494 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 5495 | return ERR_ALERT | ERR_FATAL; |
| 5496 | } |
| 5497 | |
| 5498 | free(newsrv->ssl_ctx.ciphers); |
| 5499 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 5500 | return 0; |
| 5501 | } |
| 5502 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5503 | /* parse the "crl-file" server keyword */ |
| 5504 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5505 | { |
| 5506 | #ifndef X509_V_FLAG_CRL_CHECK |
| 5507 | if (err) |
| 5508 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 5509 | return ERR_ALERT | ERR_FATAL; |
| 5510 | #else |
| 5511 | if (!*args[*cur_arg + 1]) { |
| 5512 | if (err) |
| 5513 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 5514 | return ERR_ALERT | ERR_FATAL; |
| 5515 | } |
| 5516 | |
| 5517 | if ((*args[*cur_arg + 1] != '/') && global.ca_base) |
| 5518 | memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global.ca_base, args[*cur_arg + 1]); |
| 5519 | else |
| 5520 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 5521 | |
| 5522 | return 0; |
| 5523 | #endif |
| 5524 | } |
| 5525 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5526 | /* parse the "crt" server keyword */ |
| 5527 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5528 | { |
| 5529 | if (!*args[*cur_arg + 1]) { |
| 5530 | if (err) |
| 5531 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 5532 | return ERR_ALERT | ERR_FATAL; |
| 5533 | } |
| 5534 | |
| 5535 | if ((*args[*cur_arg + 1] != '/') && global.crt_base) |
| 5536 | memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global.ca_base, args[*cur_arg + 1]); |
| 5537 | else |
| 5538 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 5539 | |
| 5540 | return 0; |
| 5541 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5542 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 5543 | /* parse the "force-sslv3" server keyword */ |
| 5544 | static int srv_parse_force_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5545 | { |
| 5546 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_SSLV3; |
| 5547 | return 0; |
| 5548 | } |
| 5549 | |
| 5550 | /* parse the "force-tlsv10" server keyword */ |
| 5551 | static int srv_parse_force_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5552 | { |
| 5553 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV10; |
| 5554 | return 0; |
| 5555 | } |
| 5556 | |
| 5557 | /* parse the "force-tlsv11" server keyword */ |
| 5558 | static int srv_parse_force_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5559 | { |
| 5560 | #if SSL_OP_NO_TLSv1_1 |
| 5561 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV11; |
| 5562 | return 0; |
| 5563 | #else |
| 5564 | if (err) |
| 5565 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[*cur_arg]); |
| 5566 | return ERR_ALERT | ERR_FATAL; |
| 5567 | #endif |
| 5568 | } |
| 5569 | |
| 5570 | /* parse the "force-tlsv12" server keyword */ |
| 5571 | static int srv_parse_force_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5572 | { |
| 5573 | #if SSL_OP_NO_TLSv1_2 |
| 5574 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV12; |
| 5575 | return 0; |
| 5576 | #else |
| 5577 | if (err) |
| 5578 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[*cur_arg]); |
| 5579 | return ERR_ALERT | ERR_FATAL; |
| 5580 | #endif |
| 5581 | } |
| 5582 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 5583 | /* parse the "no-ssl-reuse" server keyword */ |
| 5584 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5585 | { |
| 5586 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 5587 | return 0; |
| 5588 | } |
| 5589 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 5590 | /* parse the "no-sslv3" server keyword */ |
| 5591 | static int srv_parse_no_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5592 | { |
| 5593 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_SSLV3; |
| 5594 | return 0; |
| 5595 | } |
| 5596 | |
| 5597 | /* parse the "no-tlsv10" server keyword */ |
| 5598 | static int srv_parse_no_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5599 | { |
| 5600 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV10; |
| 5601 | return 0; |
| 5602 | } |
| 5603 | |
| 5604 | /* parse the "no-tlsv11" server keyword */ |
| 5605 | static int srv_parse_no_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5606 | { |
| 5607 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV11; |
| 5608 | return 0; |
| 5609 | } |
| 5610 | |
| 5611 | /* parse the "no-tlsv12" server keyword */ |
| 5612 | static int srv_parse_no_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5613 | { |
| 5614 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV12; |
| 5615 | return 0; |
| 5616 | } |
| 5617 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 5618 | /* parse the "no-tls-tickets" server keyword */ |
| 5619 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5620 | { |
| 5621 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 5622 | return 0; |
| 5623 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5624 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 5625 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5626 | { |
| 5627 | newsrv->pp_opts |= SRV_PP_V2; |
| 5628 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 5629 | return 0; |
| 5630 | } |
| 5631 | |
| 5632 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 5633 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5634 | { |
| 5635 | newsrv->pp_opts |= SRV_PP_V2; |
| 5636 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 5637 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 5638 | return 0; |
| 5639 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 5640 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 5641 | /* parse the "sni" server keyword */ |
| 5642 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5643 | { |
| 5644 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5645 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 5646 | return ERR_ALERT | ERR_FATAL; |
| 5647 | #else |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 5648 | int idx; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 5649 | struct sample_expr *expr; |
| 5650 | |
| 5651 | if (!*args[*cur_arg + 1]) { |
| 5652 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 5653 | return ERR_ALERT | ERR_FATAL; |
| 5654 | } |
| 5655 | |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 5656 | idx = (*cur_arg) + 1; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 5657 | proxy->conf.args.ctx = ARGC_SRV; |
| 5658 | |
Cyril Bonté | 23d19d6 | 2016-03-07 22:13:22 +0100 | [diff] [blame] | 5659 | 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] | 5660 | if (!expr) { |
| 5661 | memprintf(err, "error detected while parsing sni expression : %s", *err); |
| 5662 | return ERR_ALERT | ERR_FATAL; |
| 5663 | } |
| 5664 | |
| 5665 | if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) { |
| 5666 | memprintf(err, "error detected while parsing sni expression : " |
| 5667 | " 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] | 5668 | args[idx-1], sample_src_names(expr->fetch->use)); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 5669 | return ERR_ALERT | ERR_FATAL; |
| 5670 | } |
| 5671 | |
| 5672 | px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 5673 | newsrv->ssl_ctx.sni = expr; |
| 5674 | return 0; |
| 5675 | #endif |
| 5676 | } |
| 5677 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 5678 | /* parse the "ssl" server keyword */ |
| 5679 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5680 | { |
| 5681 | newsrv->use_ssl = 1; |
| 5682 | if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 5683 | newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers); |
| 5684 | return 0; |
| 5685 | } |
| 5686 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5687 | /* parse the "verify" server keyword */ |
| 5688 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5689 | { |
| 5690 | if (!*args[*cur_arg + 1]) { |
| 5691 | if (err) |
| 5692 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 5693 | return ERR_ALERT | ERR_FATAL; |
| 5694 | } |
| 5695 | |
| 5696 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5697 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5698 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5699 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5700 | else { |
| 5701 | if (err) |
| 5702 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 5703 | args[*cur_arg], args[*cur_arg + 1]); |
| 5704 | return ERR_ALERT | ERR_FATAL; |
| 5705 | } |
| 5706 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5707 | return 0; |
| 5708 | } |
| 5709 | |
| 5710 | /* parse the "verifyhost" server keyword */ |
| 5711 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 5712 | { |
| 5713 | if (!*args[*cur_arg + 1]) { |
| 5714 | if (err) |
| 5715 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 5716 | return ERR_ALERT | ERR_FATAL; |
| 5717 | } |
| 5718 | |
| 5719 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 5720 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5721 | return 0; |
| 5722 | } |
| 5723 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 5724 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 5725 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 5726 | struct proxy *defpx, const char *file, int line, |
| 5727 | char **err) { |
| 5728 | int i = 1; |
| 5729 | |
| 5730 | if (*(args[i]) == 0) { |
| 5731 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 5732 | return -1; |
| 5733 | } |
| 5734 | while (*(args[i])) { |
| 5735 | if (!strcmp(args[i], "no-sslv3")) |
| 5736 | global.listen_default_ssloptions |= BC_SSL_O_NO_SSLV3; |
| 5737 | else if (!strcmp(args[i], "no-tlsv10")) |
| 5738 | global.listen_default_ssloptions |= BC_SSL_O_NO_TLSV10; |
| 5739 | else if (!strcmp(args[i], "no-tlsv11")) |
| 5740 | global.listen_default_ssloptions |= BC_SSL_O_NO_TLSV11; |
| 5741 | else if (!strcmp(args[i], "no-tlsv12")) |
| 5742 | global.listen_default_ssloptions |= BC_SSL_O_NO_TLSV12; |
| 5743 | else if (!strcmp(args[i], "force-sslv3")) |
| 5744 | global.listen_default_ssloptions |= BC_SSL_O_USE_SSLV3; |
| 5745 | else if (!strcmp(args[i], "force-tlsv10")) |
| 5746 | global.listen_default_ssloptions |= BC_SSL_O_USE_TLSV10; |
| 5747 | else if (!strcmp(args[i], "force-tlsv11")) { |
| 5748 | #if SSL_OP_NO_TLSv1_1 |
| 5749 | global.listen_default_ssloptions |= BC_SSL_O_USE_TLSV11; |
| 5750 | #else |
| 5751 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.1", args[0], args[i]); |
| 5752 | return -1; |
| 5753 | #endif |
| 5754 | } |
| 5755 | else if (!strcmp(args[i], "force-tlsv12")) { |
| 5756 | #if SSL_OP_NO_TLSv1_2 |
| 5757 | global.listen_default_ssloptions |= BC_SSL_O_USE_TLSV12; |
| 5758 | #else |
| 5759 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.2", args[0], args[i]); |
| 5760 | return -1; |
| 5761 | #endif |
| 5762 | } |
| 5763 | else if (!strcmp(args[i], "no-tls-tickets")) |
| 5764 | global.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
| 5765 | else { |
| 5766 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 5767 | return -1; |
| 5768 | } |
| 5769 | i++; |
| 5770 | } |
| 5771 | return 0; |
| 5772 | } |
| 5773 | |
| 5774 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 5775 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 5776 | struct proxy *defpx, const char *file, int line, |
| 5777 | char **err) { |
| 5778 | int i = 1; |
| 5779 | |
| 5780 | if (*(args[i]) == 0) { |
| 5781 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 5782 | return -1; |
| 5783 | } |
| 5784 | while (*(args[i])) { |
| 5785 | if (!strcmp(args[i], "no-sslv3")) |
| 5786 | global.connect_default_ssloptions |= SRV_SSL_O_NO_SSLV3; |
| 5787 | else if (!strcmp(args[i], "no-tlsv10")) |
| 5788 | global.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV10; |
| 5789 | else if (!strcmp(args[i], "no-tlsv11")) |
| 5790 | global.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV11; |
| 5791 | else if (!strcmp(args[i], "no-tlsv12")) |
| 5792 | global.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV12; |
| 5793 | else if (!strcmp(args[i], "force-sslv3")) |
| 5794 | global.connect_default_ssloptions |= SRV_SSL_O_USE_SSLV3; |
| 5795 | else if (!strcmp(args[i], "force-tlsv10")) |
| 5796 | global.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV10; |
| 5797 | else if (!strcmp(args[i], "force-tlsv11")) { |
| 5798 | #if SSL_OP_NO_TLSv1_1 |
| 5799 | global.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV11; |
| 5800 | #else |
| 5801 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.1", args[0], args[i]); |
| 5802 | return -1; |
| 5803 | #endif |
| 5804 | } |
| 5805 | else if (!strcmp(args[i], "force-tlsv12")) { |
| 5806 | #if SSL_OP_NO_TLSv1_2 |
| 5807 | global.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV12; |
| 5808 | #else |
| 5809 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.2", args[0], args[i]); |
| 5810 | return -1; |
| 5811 | #endif |
| 5812 | } |
| 5813 | else if (!strcmp(args[i], "no-tls-tickets")) |
| 5814 | global.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
| 5815 | else { |
| 5816 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 5817 | return -1; |
| 5818 | } |
| 5819 | i++; |
| 5820 | } |
| 5821 | return 0; |
| 5822 | } |
| 5823 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5824 | /* Note: must not be declared <const> as its list will be overwritten. |
| 5825 | * Please take care of keeping this list alphabetically sorted. |
| 5826 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 5827 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 5828 | { "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] | 5829 | { "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] | 5830 | { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 5831 | { "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] | 5832 | { "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] | 5833 | { "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] | 5834 | { "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] | 5835 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 5836 | { "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] | 5837 | { "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] | 5838 | { "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] | 5839 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 5840 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 5841 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 5842 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 5843 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 5844 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 5845 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 5846 | { "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] | 5847 | { "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] | 5848 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 5849 | { "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] | 5850 | { "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] | 5851 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 5852 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 5853 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 5854 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 5855 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 5856 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 5857 | { "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] | 5858 | { "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] | 5859 | { "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] | 5860 | { "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] | 5861 | { "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] | 5862 | { "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] | 5863 | { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 5864 | { "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] | 5865 | { "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] | 5866 | #ifdef OPENSSL_NPN_NEGOTIATED |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5867 | { "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] | 5868 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5869 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5870 | { "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] | 5871 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 5872 | { "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] | 5873 | { "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] | 5874 | { "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] | 5875 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 5876 | { "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] | 5877 | { NULL, NULL, 0, 0, 0 }, |
| 5878 | }}; |
| 5879 | |
| 5880 | /* Note: must not be declared <const> as its list will be overwritten. |
| 5881 | * Please take care of keeping this list alphabetically sorted. |
| 5882 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 5883 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 5884 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 5885 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 5886 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5887 | }}; |
| 5888 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5889 | /* Note: must not be declared <const> as its list will be overwritten. |
| 5890 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 5891 | * all code contributors. |
| 5892 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 5893 | * the config parser can report an appropriate error when a known keyword was |
| 5894 | * not enabled. |
| 5895 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 5896 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5897 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 5898 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 5899 | { "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] | 5900 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 5901 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5902 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 5903 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 5904 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 5905 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 5906 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 5907 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 5908 | { "force-sslv3", bind_parse_force_sslv3, 0 }, /* force SSLv3 */ |
| 5909 | { "force-tlsv10", bind_parse_force_tlsv10, 0 }, /* force TLSv10 */ |
| 5910 | { "force-tlsv11", bind_parse_force_tlsv11, 0 }, /* force TLSv11 */ |
| 5911 | { "force-tlsv12", bind_parse_force_tlsv12, 0 }, /* force TLSv12 */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5912 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5913 | { "no-sslv3", bind_parse_no_sslv3, 0 }, /* disable SSLv3 */ |
| 5914 | { "no-tlsv10", bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */ |
| 5915 | { "no-tlsv11", bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */ |
| 5916 | { "no-tlsv12", bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */ |
| 5917 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 5918 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
| 5919 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 5920 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 5921 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 5922 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5923 | { NULL, NULL, 0 }, |
| 5924 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5925 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 5926 | /* Note: must not be declared <const> as its list will be overwritten. |
| 5927 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 5928 | * all code contributors. |
| 5929 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 5930 | * the config parser can report an appropriate error when a known keyword was |
| 5931 | * not enabled. |
| 5932 | */ |
| 5933 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5934 | { "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] | 5935 | { "check-ssl", srv_parse_check_ssl, 0, 0 }, /* enable SSL for health checks */ |
| 5936 | { "ciphers", srv_parse_ciphers, 1, 0 }, /* select the cipher suite */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5937 | { "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] | 5938 | { "crt", srv_parse_crt, 1, 0 }, /* set client certificate */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 5939 | { "force-sslv3", srv_parse_force_sslv3, 0, 0 }, /* force SSLv3 */ |
| 5940 | { "force-tlsv10", srv_parse_force_tlsv10, 0, 0 }, /* force TLSv10 */ |
| 5941 | { "force-tlsv11", srv_parse_force_tlsv11, 0, 0 }, /* force TLSv11 */ |
| 5942 | { "force-tlsv12", srv_parse_force_tlsv12, 0, 0 }, /* force TLSv12 */ |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 5943 | { "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] | 5944 | { "no-sslv3", srv_parse_no_sslv3, 0, 0 }, /* disable SSLv3 */ |
| 5945 | { "no-tlsv10", srv_parse_no_tlsv10, 0, 0 }, /* disable TLSv10 */ |
| 5946 | { "no-tlsv11", srv_parse_no_tlsv11, 0, 0 }, /* disable TLSv11 */ |
| 5947 | { "no-tlsv12", srv_parse_no_tlsv12, 0, 0 }, /* disable TLSv12 */ |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 5948 | { "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] | 5949 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 0 }, /* send PROXY protocol header v2 with SSL info */ |
| 5950 | { "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] | 5951 | { "sni", srv_parse_sni, 1, 0 }, /* send SNI extension */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 5952 | { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5953 | { "verify", srv_parse_verify, 1, 0 }, /* set SSL verify method */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5954 | { "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] | 5955 | { NULL, NULL, 0, 0 }, |
| 5956 | }}; |
| 5957 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 5958 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 5959 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 5960 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
| 5961 | { 0, NULL, NULL }, |
| 5962 | }}; |
| 5963 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5964 | /* transport-layer operations for SSL sockets */ |
| 5965 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5966 | .snd_buf = ssl_sock_from_buf, |
| 5967 | .rcv_buf = ssl_sock_to_buf, |
| 5968 | .rcv_pipe = NULL, |
| 5969 | .snd_pipe = NULL, |
| 5970 | .shutr = NULL, |
| 5971 | .shutw = ssl_sock_shutw, |
| 5972 | .close = ssl_sock_close, |
| 5973 | .init = ssl_sock_init, |
| 5974 | }; |
| 5975 | |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 5976 | #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] | 5977 | |
| 5978 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 5979 | { |
| 5980 | if (ptr) { |
| 5981 | chunk_destroy(ptr); |
| 5982 | free(ptr); |
| 5983 | } |
| 5984 | } |
| 5985 | |
| 5986 | #endif |
| 5987 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5988 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 5989 | static void __ssl_sock_init(void) |
| 5990 | { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5991 | STACK_OF(SSL_COMP)* cm; |
| 5992 | |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 5993 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 5994 | global.listen_default_ciphers = LISTEN_DEFAULT_CIPHERS; |
| 5995 | #endif |
| 5996 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 5997 | global.connect_default_ciphers = CONNECT_DEFAULT_CIPHERS; |
| 5998 | #endif |
| 5999 | if (global.listen_default_ciphers) |
| 6000 | global.listen_default_ciphers = strdup(global.listen_default_ciphers); |
| 6001 | if (global.connect_default_ciphers) |
| 6002 | global.connect_default_ciphers = strdup(global.connect_default_ciphers); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6003 | global.listen_default_ssloptions = BC_SSL_O_NONE; |
| 6004 | global.connect_default_ssloptions = SRV_SSL_O_NONE; |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 6005 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6006 | SSL_library_init(); |
| 6007 | cm = SSL_COMP_get_compression_methods(); |
| 6008 | sk_SSL_COMP_zero(cm); |
Daniel Jakots | 54ffb91 | 2015-11-06 20:02:41 +0100 | [diff] [blame] | 6009 | #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] | 6010 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 6011 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6012 | sample_register_fetches(&sample_fetch_keywords); |
| 6013 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 6014 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6015 | srv_register_keywords(&srv_kws); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 6016 | cfg_register_keywords(&cfg_kws); |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 6017 | |
| 6018 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 6019 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 6020 | |
| 6021 | #ifndef OPENSSL_NO_DH |
| 6022 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 6023 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6024 | } |
| 6025 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6026 | __attribute__((destructor)) |
| 6027 | static void __ssl_sock_deinit(void) |
| 6028 | { |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 6029 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6030 | lru64_destroy(ssl_ctx_lru_tree); |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 6031 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6032 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6033 | #ifndef OPENSSL_NO_DH |
| 6034 | if (local_dh_1024) { |
| 6035 | DH_free(local_dh_1024); |
| 6036 | local_dh_1024 = NULL; |
| 6037 | } |
| 6038 | |
| 6039 | if (local_dh_2048) { |
| 6040 | DH_free(local_dh_2048); |
| 6041 | local_dh_2048 = NULL; |
| 6042 | } |
| 6043 | |
| 6044 | if (local_dh_4096) { |
| 6045 | DH_free(local_dh_4096); |
| 6046 | local_dh_4096 = NULL; |
| 6047 | } |
| 6048 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 6049 | if (global_dh) { |
| 6050 | DH_free(global_dh); |
| 6051 | global_dh = NULL; |
| 6052 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6053 | #endif |
| 6054 | |
| 6055 | ERR_remove_state(0); |
| 6056 | ERR_free_strings(); |
| 6057 | |
| 6058 | EVP_cleanup(); |
| 6059 | |
| 6060 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
| 6061 | CRYPTO_cleanup_all_ex_data(); |
| 6062 | #endif |
| 6063 | } |
| 6064 | |
| 6065 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6066 | /* |
| 6067 | * Local variables: |
| 6068 | * c-indent-level: 8 |
| 6069 | * c-basic-offset: 8 |
| 6070 | * End: |
| 6071 | */ |