Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2 | * SSL/TLS transport layer over SOCK_STREAM sockets |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
Willy Tarreau | 69845df | 2012-09-10 09:43:09 +0200 | [diff] [blame] | 11 | * Acknowledgement: |
| 12 | * We'd like to specially thank the Stud project authors for a very clean |
| 13 | * and well documented code which helped us understand how the OpenSSL API |
| 14 | * ought to be used in non-blocking mode. This is one difficult part which |
| 15 | * is not easy to get from the OpenSSL doc, and reading the Stud code made |
| 16 | * it much more obvious than the examples in the OpenSSL package. Keep up |
| 17 | * the good works, guys ! |
| 18 | * |
| 19 | * Stud is an extremely efficient and scalable SSL/TLS proxy which combines |
| 20 | * particularly well with haproxy. For more info about this project, visit : |
| 21 | * https://github.com/bumptech/stud |
| 22 | * |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #define _GNU_SOURCE |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 26 | #include <ctype.h> |
| 27 | #include <dirent.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 32 | #include <string.h> |
| 33 | #include <unistd.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 34 | |
| 35 | #include <sys/socket.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/types.h> |
| 38 | |
| 39 | #include <netinet/tcp.h> |
| 40 | |
| 41 | #include <openssl/ssl.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 42 | #include <openssl/x509.h> |
| 43 | #include <openssl/x509v3.h> |
| 44 | #include <openssl/x509.h> |
| 45 | #include <openssl/err.h> |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 46 | #include <openssl/rand.h> |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 47 | #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] | 48 | #include <openssl/ocsp.h> |
| 49 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 50 | |
| 51 | #include <common/buffer.h> |
| 52 | #include <common/compat.h> |
| 53 | #include <common/config.h> |
| 54 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 55 | #include <common/errors.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 56 | #include <common/standard.h> |
| 57 | #include <common/ticks.h> |
| 58 | #include <common/time.h> |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 59 | #include <common/cfgparse.h> |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 60 | #include <common/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 61 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 62 | #include <ebsttree.h> |
| 63 | |
| 64 | #include <types/global.h> |
| 65 | #include <types/ssl_sock.h> |
| 66 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 67 | #include <proto/acl.h> |
| 68 | #include <proto/arg.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 69 | #include <proto/connection.h> |
| 70 | #include <proto/fd.h> |
| 71 | #include <proto/freq_ctr.h> |
| 72 | #include <proto/frontend.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 73 | #include <proto/listener.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 74 | #include <proto/pattern.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 75 | #include <proto/server.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 76 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 77 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 78 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 79 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 80 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 81 | #include <proto/task.h> |
| 82 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 83 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 84 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 85 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 86 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 87 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 88 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 89 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 90 | |
| 91 | /* Verify errors macros */ |
| 92 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 93 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 94 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 95 | |
| 96 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 97 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 98 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 99 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 100 | /* Supported hash function for TLS tickets */ |
| 101 | #ifdef OPENSSL_NO_SHA256 |
| 102 | #define HASH_FUNCT EVP_sha1 |
| 103 | #else |
| 104 | #define HASH_FUNCT EVP_sha256 |
| 105 | #endif /* OPENSSL_NO_SHA256 */ |
| 106 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 107 | /* server and bind verify method, it uses a global value as default */ |
| 108 | enum { |
| 109 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 110 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 111 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 112 | SSL_SOCK_VERIFY_NONE = 3, |
| 113 | }; |
| 114 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 115 | int sslconns = 0; |
| 116 | int totalsslconns = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 117 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 118 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 119 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 120 | #endif |
| 121 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 122 | #ifndef OPENSSL_NO_DH |
| 123 | static DH *local_dh_1024 = NULL; |
| 124 | static DH *local_dh_2048 = NULL; |
| 125 | static DH *local_dh_4096 = NULL; |
| 126 | static DH *local_dh_8192 = NULL; |
| 127 | #endif /* OPENSSL_NO_DH */ |
| 128 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 129 | #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] | 130 | struct certificate_ocsp { |
| 131 | struct ebmb_node key; |
| 132 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 133 | struct chunk response; |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 134 | long expire; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 135 | }; |
| 136 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 137 | /* |
| 138 | * This function returns the number of seconds elapsed |
| 139 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 140 | * date presented un ASN1_GENERALIZEDTIME. |
| 141 | * |
| 142 | * In parsing error case, it returns -1. |
| 143 | */ |
| 144 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 145 | { |
| 146 | long epoch; |
| 147 | char *p, *end; |
| 148 | const unsigned short month_offset[12] = { |
| 149 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 150 | }; |
| 151 | int year, month; |
| 152 | |
| 153 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 154 | |
| 155 | p = (char *)d->data; |
| 156 | end = p + d->length; |
| 157 | |
| 158 | if (end - p < 4) return -1; |
| 159 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 160 | p += 4; |
| 161 | if (end - p < 2) return -1; |
| 162 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 163 | if (month < 1 || month > 12) return -1; |
| 164 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 165 | We consider leap years and the current month (<marsh or not) */ |
| 166 | epoch = ( ((year - 1970) * 365) |
| 167 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 168 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 169 | + month_offset[month-1] |
| 170 | ) * 24 * 60 * 60; |
| 171 | p += 2; |
| 172 | if (end - p < 2) return -1; |
| 173 | /* Add the number of seconds of completed days of current month */ |
| 174 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 175 | p += 2; |
| 176 | if (end - p < 2) return -1; |
| 177 | /* Add the completed hours of the current day */ |
| 178 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 179 | p += 2; |
| 180 | if (end - p < 2) return -1; |
| 181 | /* Add the completed minutes of the current hour */ |
| 182 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 183 | p += 2; |
| 184 | if (p == end) return -1; |
| 185 | /* Test if there is available seconds */ |
| 186 | if (p[0] < '0' || p[0] > '9') |
| 187 | goto nosec; |
| 188 | if (end - p < 2) return -1; |
| 189 | /* Add the seconds of the current minute */ |
| 190 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 191 | p += 2; |
| 192 | if (p == end) return -1; |
| 193 | /* Ignore seconds float part if present */ |
| 194 | if (p[0] == '.') { |
| 195 | do { |
| 196 | if (++p == end) return -1; |
| 197 | } while (p[0] >= '0' && p[0] <= '9'); |
| 198 | } |
| 199 | |
| 200 | nosec: |
| 201 | if (p[0] == 'Z') { |
| 202 | if (end - p != 1) return -1; |
| 203 | return epoch; |
| 204 | } |
| 205 | else if (p[0] == '+') { |
| 206 | if (end - p != 5) return -1; |
| 207 | /* Apply timezone offset */ |
| 208 | return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 209 | } |
| 210 | else if (p[0] == '-') { |
| 211 | if (end - p != 5) return -1; |
| 212 | /* Apply timezone offset */ |
| 213 | return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 214 | } |
| 215 | |
| 216 | return -1; |
| 217 | } |
| 218 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 219 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 220 | |
| 221 | /* This function starts to check if the OCSP response (in DER format) contained |
| 222 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 223 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 224 | * contained in the OCSP Response and exits on error if no match. |
| 225 | * If it's a valid OCSP Response: |
| 226 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 227 | * pointed by 'ocsp'. |
| 228 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 229 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 230 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 231 | * already present in the container, it will be overwritten. |
| 232 | * |
| 233 | * Note: OCSP response containing more than one OCSP Single response is not |
| 234 | * considered valid. |
| 235 | * |
| 236 | * Returns 0 on success, 1 in error case. |
| 237 | */ |
| 238 | static int ssl_sock_load_ocsp_response(struct chunk *ocsp_response, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 239 | { |
| 240 | OCSP_RESPONSE *resp; |
| 241 | OCSP_BASICRESP *bs = NULL; |
| 242 | OCSP_SINGLERESP *sr; |
| 243 | unsigned char *p = (unsigned char *)ocsp_response->str; |
| 244 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 245 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 246 | int reason; |
| 247 | int ret = 1; |
| 248 | |
| 249 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, ocsp_response->len); |
| 250 | if (!resp) { |
| 251 | memprintf(err, "Unable to parse OCSP response"); |
| 252 | goto out; |
| 253 | } |
| 254 | |
| 255 | rc = OCSP_response_status(resp); |
| 256 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 257 | memprintf(err, "OCSP response status not successful"); |
| 258 | goto out; |
| 259 | } |
| 260 | |
| 261 | bs = OCSP_response_get1_basic(resp); |
| 262 | if (!bs) { |
| 263 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 264 | goto out; |
| 265 | } |
| 266 | |
| 267 | count_sr = OCSP_resp_count(bs); |
| 268 | if (count_sr > 1) { |
| 269 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 270 | goto out; |
| 271 | } |
| 272 | |
| 273 | sr = OCSP_resp_get0(bs, 0); |
| 274 | if (!sr) { |
| 275 | memprintf(err, "Failed to get OCSP single response"); |
| 276 | goto out; |
| 277 | } |
| 278 | |
| 279 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
| 280 | if (rc != V_OCSP_CERTSTATUS_GOOD) { |
| 281 | memprintf(err, "OCSP single response: certificate status not good"); |
| 282 | goto out; |
| 283 | } |
| 284 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 285 | if (!nextupd) { |
| 286 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 287 | goto out; |
| 288 | } |
| 289 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 290 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 291 | if (!rc) { |
| 292 | memprintf(err, "OCSP single response: no longer valid."); |
| 293 | goto out; |
| 294 | } |
| 295 | |
| 296 | if (cid) { |
| 297 | if (OCSP_id_cmp(sr->certId, cid)) { |
| 298 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 299 | goto out; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if (!ocsp) { |
| 304 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 305 | unsigned char *p; |
| 306 | |
| 307 | rc = i2d_OCSP_CERTID(sr->certId, NULL); |
| 308 | if (!rc) { |
| 309 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 310 | goto out; |
| 311 | } |
| 312 | |
| 313 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 314 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 315 | goto out; |
| 316 | } |
| 317 | |
| 318 | p = key; |
| 319 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 320 | i2d_OCSP_CERTID(sr->certId, &p); |
| 321 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 322 | if (!ocsp) { |
| 323 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 324 | goto out; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /* According to comments on "chunk_dup", the |
| 329 | previous chunk buffer will be freed */ |
| 330 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 331 | memprintf(err, "OCSP response: Memory allocation error"); |
| 332 | goto out; |
| 333 | } |
| 334 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 335 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 336 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 337 | ret = 0; |
| 338 | out: |
| 339 | if (bs) |
| 340 | OCSP_BASICRESP_free(bs); |
| 341 | |
| 342 | if (resp) |
| 343 | OCSP_RESPONSE_free(resp); |
| 344 | |
| 345 | return ret; |
| 346 | } |
| 347 | /* |
| 348 | * External function use to update the OCSP response in the OCSP response's |
| 349 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 350 | * to update in DER format. |
| 351 | * |
| 352 | * Returns 0 on success, 1 in error case. |
| 353 | */ |
| 354 | int ssl_sock_update_ocsp_response(struct chunk *ocsp_response, char **err) |
| 355 | { |
| 356 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * This function load the OCSP Resonse in DER format contained in file at |
| 361 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 362 | * |
| 363 | * Returns 0 on success, 1 in error case. |
| 364 | */ |
| 365 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 366 | { |
| 367 | int fd = -1; |
| 368 | int r = 0; |
| 369 | int ret = 1; |
| 370 | |
| 371 | fd = open(ocsp_path, O_RDONLY); |
| 372 | if (fd == -1) { |
| 373 | memprintf(err, "Error opening OCSP response file"); |
| 374 | goto end; |
| 375 | } |
| 376 | |
| 377 | trash.len = 0; |
| 378 | while (trash.len < trash.size) { |
| 379 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 380 | if (r < 0) { |
| 381 | if (errno == EINTR) |
| 382 | continue; |
| 383 | |
| 384 | memprintf(err, "Error reading OCSP response from file"); |
| 385 | goto end; |
| 386 | } |
| 387 | else if (r == 0) { |
| 388 | break; |
| 389 | } |
| 390 | trash.len += r; |
| 391 | } |
| 392 | |
| 393 | close(fd); |
| 394 | fd = -1; |
| 395 | |
| 396 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 397 | end: |
| 398 | if (fd != -1) |
| 399 | close(fd); |
| 400 | |
| 401 | return ret; |
| 402 | } |
| 403 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 404 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 405 | 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) |
| 406 | { |
| 407 | struct tls_sess_key *keys; |
| 408 | struct connection *conn; |
| 409 | int head; |
| 410 | int i; |
| 411 | |
| 412 | conn = (struct connection *)SSL_get_app_data(s); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 413 | keys = objt_listener(conn->target)->bind_conf->keys_ref->tlskeys; |
| 414 | 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] | 415 | |
| 416 | if (enc) { |
| 417 | memcpy(key_name, keys[head].name, 16); |
| 418 | |
| 419 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
| 420 | return -1; |
| 421 | |
| 422 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].aes_key, iv)) |
| 423 | return -1; |
| 424 | |
| 425 | HMAC_Init_ex(hctx, keys[head].hmac_key, 16, HASH_FUNCT(), NULL); |
| 426 | |
| 427 | return 1; |
| 428 | } else { |
| 429 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 430 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 431 | goto found; |
| 432 | } |
| 433 | return 0; |
| 434 | |
| 435 | found: |
| 436 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].hmac_key, 16, HASH_FUNCT(), NULL); |
| 437 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].aes_key, iv)) |
| 438 | return -1; |
| 439 | /* 2 for key renewal, 1 if current key is still valid */ |
| 440 | return i ? 2 : 1; |
| 441 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 445 | { |
| 446 | struct tls_keys_ref *ref; |
| 447 | |
| 448 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 449 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 450 | return ref; |
| 451 | return NULL; |
| 452 | } |
| 453 | |
| 454 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 455 | { |
| 456 | struct tls_keys_ref *ref; |
| 457 | |
| 458 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 459 | if (ref->unique_id == unique_id) |
| 460 | return ref; |
| 461 | return NULL; |
| 462 | } |
| 463 | |
| 464 | int ssl_sock_update_tlskey(char *filename, struct chunk *tlskey, char **err) { |
| 465 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 466 | |
| 467 | if(!ref) { |
| 468 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 469 | return 1; |
| 470 | } |
| 471 | |
| 472 | memcpy((char *) (ref->tlskeys + 2 % TLS_TICKETS_NO), tlskey->str, tlskey->len); |
| 473 | ref->tls_ticket_enc_index = ref->tls_ticket_enc_index + 1 % TLS_TICKETS_NO; |
| 474 | |
| 475 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 476 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 477 | |
| 478 | /* This function finalize the configuration parsing. Its set all the |
| 479 | * automatic ids |
| 480 | */ |
| 481 | void tlskeys_finalize_config(void) |
| 482 | { |
| 483 | int i = 0; |
| 484 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 485 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 486 | |
| 487 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 488 | if (ref->unique_id == -1) { |
| 489 | /* Look for the first free id. */ |
| 490 | while (1) { |
| 491 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 492 | if (ref2->unique_id == i) { |
| 493 | i++; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | if (&ref2->list == &tlskeys_reference) |
| 498 | break; |
| 499 | } |
| 500 | |
| 501 | /* Uses the unique id and increment it for the next entry. */ |
| 502 | ref->unique_id = i; |
| 503 | i++; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /* This sort the reference list by id. */ |
| 508 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 509 | LIST_DEL(&ref->list); |
| 510 | list_for_each_entry(ref3, &tkr, list) { |
| 511 | if (ref->unique_id < ref3->unique_id) { |
| 512 | LIST_ADDQ(&ref3->list, &ref->list); |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | if (&ref3->list == &tkr) |
| 517 | LIST_ADDQ(&tkr, &ref->list); |
| 518 | } |
| 519 | |
| 520 | /* swap root */ |
| 521 | LIST_ADD(&tkr, &tlskeys_reference); |
| 522 | LIST_DEL(&tkr); |
| 523 | } |
| 524 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 525 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 526 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 527 | /* |
| 528 | * Callback used to set OCSP status extension content in server hello. |
| 529 | */ |
| 530 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 531 | { |
| 532 | struct certificate_ocsp *ocsp = (struct certificate_ocsp *)arg; |
| 533 | char* ssl_buf; |
| 534 | |
| 535 | if (!ocsp || |
| 536 | !ocsp->response.str || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 537 | !ocsp->response.len || |
| 538 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 539 | return SSL_TLSEXT_ERR_NOACK; |
| 540 | |
| 541 | ssl_buf = OPENSSL_malloc(ocsp->response.len); |
| 542 | if (!ssl_buf) |
| 543 | return SSL_TLSEXT_ERR_NOACK; |
| 544 | |
| 545 | memcpy(ssl_buf, ocsp->response.str, ocsp->response.len); |
| 546 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.len); |
| 547 | |
| 548 | return SSL_TLSEXT_ERR_OK; |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 553 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 554 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 555 | * It should be present in the certificate's extra chain builded from file |
| 556 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 557 | * named 'cert_path' suffixed using '.issuer'. |
| 558 | * |
| 559 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 560 | * response. If file is empty or content is not a valid OCSP response, |
| 561 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 562 | * is displayed). |
| 563 | * |
| 564 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
| 565 | * succesfully enabled, or -1 in other error case. |
| 566 | */ |
| 567 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 568 | { |
| 569 | |
| 570 | BIO *in = NULL; |
| 571 | X509 *x, *xi = NULL, *issuer = NULL; |
| 572 | STACK_OF(X509) *chain = NULL; |
| 573 | OCSP_CERTID *cid = NULL; |
| 574 | SSL *ssl; |
| 575 | char ocsp_path[MAXPATHLEN+1]; |
| 576 | int i, ret = -1; |
| 577 | struct stat st; |
| 578 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 579 | char *warn = NULL; |
| 580 | unsigned char *p; |
| 581 | |
| 582 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 583 | |
| 584 | if (stat(ocsp_path, &st)) |
| 585 | return 1; |
| 586 | |
| 587 | ssl = SSL_new(ctx); |
| 588 | if (!ssl) |
| 589 | goto out; |
| 590 | |
| 591 | x = SSL_get_certificate(ssl); |
| 592 | if (!x) |
| 593 | goto out; |
| 594 | |
| 595 | /* Try to lookup for issuer in certificate extra chain */ |
| 596 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 597 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 598 | #else |
| 599 | chain = ctx->extra_certs; |
| 600 | #endif |
| 601 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 602 | issuer = sk_X509_value(chain, i); |
| 603 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 604 | break; |
| 605 | else |
| 606 | issuer = NULL; |
| 607 | } |
| 608 | |
| 609 | /* If not found try to load issuer from a suffixed file */ |
| 610 | if (!issuer) { |
| 611 | char issuer_path[MAXPATHLEN+1]; |
| 612 | |
| 613 | in = BIO_new(BIO_s_file()); |
| 614 | if (!in) |
| 615 | goto out; |
| 616 | |
| 617 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 618 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 619 | goto out; |
| 620 | |
| 621 | xi = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata); |
| 622 | if (!xi) |
| 623 | goto out; |
| 624 | |
| 625 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 626 | goto out; |
| 627 | |
| 628 | issuer = xi; |
| 629 | } |
| 630 | |
| 631 | cid = OCSP_cert_to_id(0, x, issuer); |
| 632 | if (!cid) |
| 633 | goto out; |
| 634 | |
| 635 | i = i2d_OCSP_CERTID(cid, NULL); |
| 636 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 637 | goto out; |
| 638 | |
| 639 | ocsp = calloc(1, sizeof(struct certificate_ocsp)); |
| 640 | if (!ocsp) |
| 641 | goto out; |
| 642 | |
| 643 | p = ocsp->key_data; |
| 644 | i2d_OCSP_CERTID(cid, &p); |
| 645 | |
| 646 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 647 | if (iocsp == ocsp) |
| 648 | ocsp = NULL; |
| 649 | |
| 650 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 651 | SSL_CTX_set_tlsext_status_arg(ctx, iocsp); |
| 652 | |
| 653 | ret = 0; |
| 654 | |
| 655 | warn = NULL; |
| 656 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 657 | memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure"); |
| 658 | Warning("%s.\n", warn); |
| 659 | } |
| 660 | |
| 661 | out: |
| 662 | if (ssl) |
| 663 | SSL_free(ssl); |
| 664 | |
| 665 | if (in) |
| 666 | BIO_free(in); |
| 667 | |
| 668 | if (xi) |
| 669 | X509_free(xi); |
| 670 | |
| 671 | if (cid) |
| 672 | OCSP_CERTID_free(cid); |
| 673 | |
| 674 | if (ocsp) |
| 675 | free(ocsp); |
| 676 | |
| 677 | if (warn) |
| 678 | free(warn); |
| 679 | |
| 680 | |
| 681 | return ret; |
| 682 | } |
| 683 | |
| 684 | #endif |
| 685 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 686 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 687 | |
| 688 | #define CT_EXTENSION_TYPE 18 |
| 689 | |
| 690 | static int sctl_ex_index = -1; |
| 691 | |
| 692 | /* |
| 693 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 694 | * makes only basic test if the data seems like SCTL. No signature validation |
| 695 | * is performed. |
| 696 | */ |
| 697 | static int ssl_sock_parse_sctl(struct chunk *sctl) |
| 698 | { |
| 699 | int ret = 1; |
| 700 | int len, pos, sct_len; |
| 701 | unsigned char *data; |
| 702 | |
| 703 | if (sctl->len < 2) |
| 704 | goto out; |
| 705 | |
| 706 | data = (unsigned char *)sctl->str; |
| 707 | len = (data[0] << 8) | data[1]; |
| 708 | |
| 709 | if (len + 2 != sctl->len) |
| 710 | goto out; |
| 711 | |
| 712 | data = data + 2; |
| 713 | pos = 0; |
| 714 | while (pos < len) { |
| 715 | if (len - pos < 2) |
| 716 | goto out; |
| 717 | |
| 718 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 719 | if (pos + sct_len + 2 > len) |
| 720 | goto out; |
| 721 | |
| 722 | pos += sct_len + 2; |
| 723 | } |
| 724 | |
| 725 | ret = 0; |
| 726 | |
| 727 | out: |
| 728 | return ret; |
| 729 | } |
| 730 | |
| 731 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, struct chunk **sctl) |
| 732 | { |
| 733 | int fd = -1; |
| 734 | int r = 0; |
| 735 | int ret = 1; |
| 736 | |
| 737 | *sctl = NULL; |
| 738 | |
| 739 | fd = open(sctl_path, O_RDONLY); |
| 740 | if (fd == -1) |
| 741 | goto end; |
| 742 | |
| 743 | trash.len = 0; |
| 744 | while (trash.len < trash.size) { |
| 745 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 746 | if (r < 0) { |
| 747 | if (errno == EINTR) |
| 748 | continue; |
| 749 | |
| 750 | goto end; |
| 751 | } |
| 752 | else if (r == 0) { |
| 753 | break; |
| 754 | } |
| 755 | trash.len += r; |
| 756 | } |
| 757 | |
| 758 | ret = ssl_sock_parse_sctl(&trash); |
| 759 | if (ret) |
| 760 | goto end; |
| 761 | |
| 762 | *sctl = calloc(1, sizeof(struct chunk)); |
| 763 | if (!chunk_dup(*sctl, &trash)) { |
| 764 | free(*sctl); |
| 765 | *sctl = NULL; |
| 766 | goto end; |
| 767 | } |
| 768 | |
| 769 | end: |
| 770 | if (fd != -1) |
| 771 | close(fd); |
| 772 | |
| 773 | return ret; |
| 774 | } |
| 775 | |
| 776 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 777 | { |
| 778 | struct chunk *sctl = (struct chunk *)add_arg; |
| 779 | |
| 780 | *out = (unsigned char *)sctl->str; |
| 781 | *outlen = sctl->len; |
| 782 | |
| 783 | return 1; |
| 784 | } |
| 785 | |
| 786 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 787 | { |
| 788 | return 1; |
| 789 | } |
| 790 | |
| 791 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 792 | { |
| 793 | char sctl_path[MAXPATHLEN+1]; |
| 794 | int ret = -1; |
| 795 | struct stat st; |
| 796 | struct chunk *sctl = NULL; |
| 797 | |
| 798 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 799 | |
| 800 | if (stat(sctl_path, &st)) |
| 801 | return 1; |
| 802 | |
| 803 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 804 | goto out; |
| 805 | |
| 806 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 807 | free(sctl); |
| 808 | goto out; |
| 809 | } |
| 810 | |
| 811 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 812 | |
| 813 | ret = 0; |
| 814 | |
| 815 | out: |
| 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | #endif |
| 820 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 821 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 822 | { |
| 823 | struct connection *conn = (struct connection *)SSL_get_app_data(ssl); |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 824 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 825 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 826 | |
| 827 | if (where & SSL_CB_HANDSHAKE_START) { |
| 828 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 829 | if (conn->flags & CO_FL_CONNECTED) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 830 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 831 | conn->err_code = CO_ER_SSL_RENEG; |
| 832 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 833 | } |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 834 | |
| 835 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 836 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 837 | /* Long certificate chains optimz |
| 838 | If write and read bios are differents, we |
| 839 | consider that the buffering was activated, |
| 840 | so we rise the output buffer size from 4k |
| 841 | to 16k */ |
| 842 | write_bio = SSL_get_wbio(ssl); |
| 843 | if (write_bio != SSL_get_rbio(ssl)) { |
| 844 | BIO_set_write_buffer_size(write_bio, 16384); |
| 845 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 846 | } |
| 847 | } |
| 848 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 849 | } |
| 850 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 851 | /* Callback is called for each certificate of the chain during a verify |
| 852 | ok is set to 1 if preverify detect no error on current certificate. |
| 853 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 854 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 855 | { |
| 856 | SSL *ssl; |
| 857 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 858 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 859 | |
| 860 | ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 861 | conn = (struct connection *)SSL_get_app_data(ssl); |
| 862 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 863 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 864 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 865 | if (ok) /* no errors */ |
| 866 | return ok; |
| 867 | |
| 868 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 869 | err = X509_STORE_CTX_get_error(x_store); |
| 870 | |
| 871 | /* check if CA error needs to be ignored */ |
| 872 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 873 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 874 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 875 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 876 | } |
| 877 | |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 878 | if (objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) { |
| 879 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 880 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 881 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 882 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 883 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 884 | return 0; |
| 885 | } |
| 886 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 887 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 888 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 889 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 890 | /* check if certificate error needs to be ignored */ |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 891 | if (objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) { |
| 892 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 893 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 894 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 895 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 896 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 897 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 900 | /* Callback is called for ssl protocol analyse */ |
| 901 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 902 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 903 | #ifdef TLS1_RT_HEARTBEAT |
| 904 | /* test heartbeat received (write_p is set to 0 |
| 905 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 906 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Willy Tarreau | 8481500 | 2014-04-25 21:40:27 +0200 | [diff] [blame] | 907 | struct connection *conn = (struct connection *)SSL_get_app_data(ssl); |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 908 | const unsigned char *p = buf; |
| 909 | unsigned int payload; |
| 910 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 911 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 912 | |
| 913 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 914 | if (*p != TLS1_HB_REQUEST) |
| 915 | return; |
| 916 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 917 | 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] | 918 | goto kill_it; |
| 919 | |
| 920 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 921 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 922 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 923 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 924 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 925 | * advertised payload is larger than the advertised packet |
| 926 | * length, so we have garbage in the buffer between the |
| 927 | * payload and the end of the buffer (p+len). We can't know |
| 928 | * if the SSL stack is patched, and we don't know if we can |
| 929 | * safely wipe out the area between p+3+len and payload. |
| 930 | * So instead, we prevent the response from being sent by |
| 931 | * setting the max_send_fragment to 0 and we report an SSL |
| 932 | * error, which will kill this connection. It will be reported |
| 933 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 934 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 935 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 936 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 937 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 938 | return; |
| 939 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 940 | #endif |
| 941 | } |
| 942 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 943 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 944 | /* This callback is used so that the server advertises the list of |
| 945 | * negociable protocols for NPN. |
| 946 | */ |
| 947 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 948 | unsigned int *len, void *arg) |
| 949 | { |
| 950 | struct bind_conf *conf = arg; |
| 951 | |
| 952 | *data = (const unsigned char *)conf->npn_str; |
| 953 | *len = conf->npn_len; |
| 954 | return SSL_TLSEXT_ERR_OK; |
| 955 | } |
| 956 | #endif |
| 957 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 958 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 959 | /* This callback is used so that the server advertises the list of |
| 960 | * negociable protocols for ALPN. |
| 961 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 962 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 963 | unsigned char *outlen, |
| 964 | const unsigned char *server, |
| 965 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 966 | { |
| 967 | struct bind_conf *conf = arg; |
| 968 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 969 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 970 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 971 | return SSL_TLSEXT_ERR_NOACK; |
| 972 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 973 | return SSL_TLSEXT_ERR_OK; |
| 974 | } |
| 975 | #endif |
| 976 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 977 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 978 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 979 | * warning when no match is found, which implies the default (first) cert |
| 980 | * will keep being used. |
| 981 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 982 | 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] | 983 | { |
| 984 | const char *servername; |
| 985 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 986 | struct ebmb_node *node, *n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 987 | int i; |
| 988 | (void)al; /* shut gcc stupid warning */ |
| 989 | |
| 990 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 991 | if (!servername) { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 992 | return (s->strict_sni ? |
| 993 | SSL_TLSEXT_ERR_ALERT_FATAL : |
Emmanuel Hocdet | 79274e2 | 2013-05-31 12:47:44 +0200 | [diff] [blame] | 994 | SSL_TLSEXT_ERR_NOACK); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 995 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 996 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 997 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 998 | if (!servername[i]) |
| 999 | break; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1000 | trash.str[i] = tolower(servername[i]); |
| 1001 | if (!wildp && (trash.str[i] == '.')) |
| 1002 | wildp = &trash.str[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1003 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1004 | trash.str[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1005 | |
| 1006 | /* lookup in full qualified names */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1007 | node = ebst_lookup(&s->sni_ctx, trash.str); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1008 | |
| 1009 | /* lookup a not neg filter */ |
| 1010 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 1011 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 1012 | node = n; |
| 1013 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 1014 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1015 | } |
| 1016 | if (!node && wildp) { |
| 1017 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1018 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1019 | } |
| 1020 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
| 1021 | return (s->strict_sni ? |
| 1022 | SSL_TLSEXT_ERR_ALERT_FATAL : |
| 1023 | SSL_TLSEXT_ERR_ALERT_WARNING); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | /* switch ctx */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1027 | 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] | 1028 | return SSL_TLSEXT_ERR_OK; |
| 1029 | } |
| 1030 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 1031 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1032 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1033 | |
| 1034 | static DH * ssl_get_dh_1024(void) |
| 1035 | { |
Lukas Tribus | 4c0d45a | 2014-08-18 00:56:32 +0200 | [diff] [blame] | 1036 | #if (OPENSSL_VERSION_NUMBER < 0x0090801fL || defined OPENSSL_IS_BORINGSSL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1037 | static const unsigned char rfc_2409_prime_1024[] = { |
| 1038 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2, |
| 1039 | 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1, |
| 1040 | 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6, |
| 1041 | 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD, |
| 1042 | 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D, |
| 1043 | 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45, |
| 1044 | 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9, |
| 1045 | 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED, |
| 1046 | 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11, |
| 1047 | 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE6,0x53,0x81, |
| 1048 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 1049 | }; |
| 1050 | #endif |
| 1051 | DH *dh = DH_new(); |
| 1052 | if (dh) { |
Lukas Tribus | 4c0d45a | 2014-08-18 00:56:32 +0200 | [diff] [blame] | 1053 | #if (OPENSSL_VERSION_NUMBER >= 0x0090801fL && !defined OPENSSL_IS_BORINGSSL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1054 | dh->p = get_rfc2409_prime_1024(NULL); |
| 1055 | #else |
| 1056 | dh->p = BN_bin2bn(rfc_2409_prime_1024, sizeof rfc_2409_prime_1024, NULL); |
| 1057 | #endif |
| 1058 | /* See RFC 2409, Section 6 "Oakley Groups" |
| 1059 | for the reason why 2 is used as generator. |
| 1060 | */ |
| 1061 | BN_dec2bn(&dh->g, "2"); |
| 1062 | if (!dh->p || !dh->g) { |
| 1063 | DH_free(dh); |
| 1064 | dh = NULL; |
| 1065 | } |
| 1066 | } |
| 1067 | return dh; |
| 1068 | } |
| 1069 | |
| 1070 | static DH *ssl_get_dh_2048(void) |
| 1071 | { |
Lukas Tribus | 4c0d45a | 2014-08-18 00:56:32 +0200 | [diff] [blame] | 1072 | #if (OPENSSL_VERSION_NUMBER < 0x0090801fL || defined OPENSSL_IS_BORINGSSL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1073 | static const unsigned char rfc_3526_prime_2048[] = { |
| 1074 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2, |
| 1075 | 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1, |
| 1076 | 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6, |
| 1077 | 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD, |
| 1078 | 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D, |
| 1079 | 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45, |
| 1080 | 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9, |
| 1081 | 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED, |
| 1082 | 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11, |
| 1083 | 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D, |
| 1084 | 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36, |
| 1085 | 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F, |
| 1086 | 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56, |
| 1087 | 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D, |
| 1088 | 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08, |
| 1089 | 0xCA,0x18,0x21,0x7C,0x32,0x90,0x5E,0x46,0x2E,0x36,0xCE,0x3B, |
| 1090 | 0xE3,0x9E,0x77,0x2C,0x18,0x0E,0x86,0x03,0x9B,0x27,0x83,0xA2, |
| 1091 | 0xEC,0x07,0xA2,0x8F,0xB5,0xC5,0x5D,0xF0,0x6F,0x4C,0x52,0xC9, |
| 1092 | 0xDE,0x2B,0xCB,0xF6,0x95,0x58,0x17,0x18,0x39,0x95,0x49,0x7C, |
| 1093 | 0xEA,0x95,0x6A,0xE5,0x15,0xD2,0x26,0x18,0x98,0xFA,0x05,0x10, |
| 1094 | 0x15,0x72,0x8E,0x5A,0x8A,0xAC,0xAA,0x68,0xFF,0xFF,0xFF,0xFF, |
| 1095 | 0xFF,0xFF,0xFF,0xFF, |
| 1096 | }; |
| 1097 | #endif |
| 1098 | DH *dh = DH_new(); |
| 1099 | if (dh) { |
Lukas Tribus | 4c0d45a | 2014-08-18 00:56:32 +0200 | [diff] [blame] | 1100 | #if (OPENSSL_VERSION_NUMBER >= 0x0090801fL && !defined OPENSSL_IS_BORINGSSL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1101 | dh->p = get_rfc3526_prime_2048(NULL); |
| 1102 | #else |
| 1103 | dh->p = BN_bin2bn(rfc_3526_prime_2048, sizeof rfc_3526_prime_2048, NULL); |
| 1104 | #endif |
| 1105 | /* See RFC 3526, Section 3 "2048-bit MODP Group" |
| 1106 | for the reason why 2 is used as generator. |
| 1107 | */ |
| 1108 | BN_dec2bn(&dh->g, "2"); |
| 1109 | if (!dh->p || !dh->g) { |
| 1110 | DH_free(dh); |
| 1111 | dh = NULL; |
| 1112 | } |
| 1113 | } |
| 1114 | return dh; |
| 1115 | } |
| 1116 | |
| 1117 | static DH *ssl_get_dh_4096(void) |
| 1118 | { |
Lukas Tribus | 4c0d45a | 2014-08-18 00:56:32 +0200 | [diff] [blame] | 1119 | #if (OPENSSL_VERSION_NUMBER < 0x0090801fL || defined OPENSSL_IS_BORINGSSL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1120 | static const unsigned char rfc_3526_prime_4096[] = { |
| 1121 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2, |
| 1122 | 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1, |
| 1123 | 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6, |
| 1124 | 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD, |
| 1125 | 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D, |
| 1126 | 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45, |
| 1127 | 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9, |
| 1128 | 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED, |
| 1129 | 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11, |
| 1130 | 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D, |
| 1131 | 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36, |
| 1132 | 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F, |
| 1133 | 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56, |
| 1134 | 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D, |
| 1135 | 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08, |
| 1136 | 0xCA,0x18,0x21,0x7C,0x32,0x90,0x5E,0x46,0x2E,0x36,0xCE,0x3B, |
| 1137 | 0xE3,0x9E,0x77,0x2C,0x18,0x0E,0x86,0x03,0x9B,0x27,0x83,0xA2, |
| 1138 | 0xEC,0x07,0xA2,0x8F,0xB5,0xC5,0x5D,0xF0,0x6F,0x4C,0x52,0xC9, |
| 1139 | 0xDE,0x2B,0xCB,0xF6,0x95,0x58,0x17,0x18,0x39,0x95,0x49,0x7C, |
| 1140 | 0xEA,0x95,0x6A,0xE5,0x15,0xD2,0x26,0x18,0x98,0xFA,0x05,0x10, |
| 1141 | 0x15,0x72,0x8E,0x5A,0x8A,0xAA,0xC4,0x2D,0xAD,0x33,0x17,0x0D, |
| 1142 | 0x04,0x50,0x7A,0x33,0xA8,0x55,0x21,0xAB,0xDF,0x1C,0xBA,0x64, |
| 1143 | 0xEC,0xFB,0x85,0x04,0x58,0xDB,0xEF,0x0A,0x8A,0xEA,0x71,0x57, |
| 1144 | 0x5D,0x06,0x0C,0x7D,0xB3,0x97,0x0F,0x85,0xA6,0xE1,0xE4,0xC7, |
| 1145 | 0xAB,0xF5,0xAE,0x8C,0xDB,0x09,0x33,0xD7,0x1E,0x8C,0x94,0xE0, |
| 1146 | 0x4A,0x25,0x61,0x9D,0xCE,0xE3,0xD2,0x26,0x1A,0xD2,0xEE,0x6B, |
| 1147 | 0xF1,0x2F,0xFA,0x06,0xD9,0x8A,0x08,0x64,0xD8,0x76,0x02,0x73, |
| 1148 | 0x3E,0xC8,0x6A,0x64,0x52,0x1F,0x2B,0x18,0x17,0x7B,0x20,0x0C, |
| 1149 | 0xBB,0xE1,0x17,0x57,0x7A,0x61,0x5D,0x6C,0x77,0x09,0x88,0xC0, |
| 1150 | 0xBA,0xD9,0x46,0xE2,0x08,0xE2,0x4F,0xA0,0x74,0xE5,0xAB,0x31, |
| 1151 | 0x43,0xDB,0x5B,0xFC,0xE0,0xFD,0x10,0x8E,0x4B,0x82,0xD1,0x20, |
| 1152 | 0xA9,0x21,0x08,0x01,0x1A,0x72,0x3C,0x12,0xA7,0x87,0xE6,0xD7, |
| 1153 | 0x88,0x71,0x9A,0x10,0xBD,0xBA,0x5B,0x26,0x99,0xC3,0x27,0x18, |
| 1154 | 0x6A,0xF4,0xE2,0x3C,0x1A,0x94,0x68,0x34,0xB6,0x15,0x0B,0xDA, |
| 1155 | 0x25,0x83,0xE9,0xCA,0x2A,0xD4,0x4C,0xE8,0xDB,0xBB,0xC2,0xDB, |
| 1156 | 0x04,0xDE,0x8E,0xF9,0x2E,0x8E,0xFC,0x14,0x1F,0xBE,0xCA,0xA6, |
| 1157 | 0x28,0x7C,0x59,0x47,0x4E,0x6B,0xC0,0x5D,0x99,0xB2,0x96,0x4F, |
| 1158 | 0xA0,0x90,0xC3,0xA2,0x23,0x3B,0xA1,0x86,0x51,0x5B,0xE7,0xED, |
| 1159 | 0x1F,0x61,0x29,0x70,0xCE,0xE2,0xD7,0xAF,0xB8,0x1B,0xDD,0x76, |
| 1160 | 0x21,0x70,0x48,0x1C,0xD0,0x06,0x91,0x27,0xD5,0xB0,0x5A,0xA9, |
| 1161 | 0x93,0xB4,0xEA,0x98,0x8D,0x8F,0xDD,0xC1,0x86,0xFF,0xB7,0xDC, |
| 1162 | 0x90,0xA6,0xC0,0x8F,0x4D,0xF4,0x35,0xC9,0x34,0x06,0x31,0x99, |
| 1163 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 1164 | }; |
| 1165 | #endif |
| 1166 | DH *dh = DH_new(); |
| 1167 | if (dh) { |
Lukas Tribus | 4c0d45a | 2014-08-18 00:56:32 +0200 | [diff] [blame] | 1168 | #if (OPENSSL_VERSION_NUMBER >= 0x0090801fL && !defined OPENSSL_IS_BORINGSSL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1169 | dh->p = get_rfc3526_prime_4096(NULL); |
| 1170 | #else |
| 1171 | dh->p = BN_bin2bn(rfc_3526_prime_4096, sizeof rfc_3526_prime_4096, NULL); |
| 1172 | #endif |
| 1173 | /* See RFC 3526, Section 5 "4096-bit MODP Group" |
| 1174 | for the reason why 2 is used as generator. |
| 1175 | */ |
| 1176 | BN_dec2bn(&dh->g, "2"); |
| 1177 | if (!dh->p || !dh->g) { |
| 1178 | DH_free(dh); |
| 1179 | dh = NULL; |
| 1180 | } |
| 1181 | } |
| 1182 | return dh; |
| 1183 | } |
| 1184 | |
| 1185 | static DH *ssl_get_dh_8192(void) |
| 1186 | { |
Lukas Tribus | 4c0d45a | 2014-08-18 00:56:32 +0200 | [diff] [blame] | 1187 | #if (OPENSSL_VERSION_NUMBER < 0x0090801fL || defined OPENSSL_IS_BORINGSSL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1188 | static const unsigned char rfc_3526_prime_8192[] = { |
| 1189 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2, |
| 1190 | 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1, |
| 1191 | 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6, |
| 1192 | 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD, |
| 1193 | 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D, |
| 1194 | 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45, |
| 1195 | 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9, |
| 1196 | 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED, |
| 1197 | 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11, |
| 1198 | 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D, |
| 1199 | 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36, |
| 1200 | 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F, |
| 1201 | 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56, |
| 1202 | 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D, |
| 1203 | 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08, |
| 1204 | 0xCA,0x18,0x21,0x7C,0x32,0x90,0x5E,0x46,0x2E,0x36,0xCE,0x3B, |
| 1205 | 0xE3,0x9E,0x77,0x2C,0x18,0x0E,0x86,0x03,0x9B,0x27,0x83,0xA2, |
| 1206 | 0xEC,0x07,0xA2,0x8F,0xB5,0xC5,0x5D,0xF0,0x6F,0x4C,0x52,0xC9, |
| 1207 | 0xDE,0x2B,0xCB,0xF6,0x95,0x58,0x17,0x18,0x39,0x95,0x49,0x7C, |
| 1208 | 0xEA,0x95,0x6A,0xE5,0x15,0xD2,0x26,0x18,0x98,0xFA,0x05,0x10, |
| 1209 | 0x15,0x72,0x8E,0x5A,0x8A,0xAA,0xC4,0x2D,0xAD,0x33,0x17,0x0D, |
| 1210 | 0x04,0x50,0x7A,0x33,0xA8,0x55,0x21,0xAB,0xDF,0x1C,0xBA,0x64, |
| 1211 | 0xEC,0xFB,0x85,0x04,0x58,0xDB,0xEF,0x0A,0x8A,0xEA,0x71,0x57, |
| 1212 | 0x5D,0x06,0x0C,0x7D,0xB3,0x97,0x0F,0x85,0xA6,0xE1,0xE4,0xC7, |
| 1213 | 0xAB,0xF5,0xAE,0x8C,0xDB,0x09,0x33,0xD7,0x1E,0x8C,0x94,0xE0, |
| 1214 | 0x4A,0x25,0x61,0x9D,0xCE,0xE3,0xD2,0x26,0x1A,0xD2,0xEE,0x6B, |
| 1215 | 0xF1,0x2F,0xFA,0x06,0xD9,0x8A,0x08,0x64,0xD8,0x76,0x02,0x73, |
| 1216 | 0x3E,0xC8,0x6A,0x64,0x52,0x1F,0x2B,0x18,0x17,0x7B,0x20,0x0C, |
| 1217 | 0xBB,0xE1,0x17,0x57,0x7A,0x61,0x5D,0x6C,0x77,0x09,0x88,0xC0, |
| 1218 | 0xBA,0xD9,0x46,0xE2,0x08,0xE2,0x4F,0xA0,0x74,0xE5,0xAB,0x31, |
| 1219 | 0x43,0xDB,0x5B,0xFC,0xE0,0xFD,0x10,0x8E,0x4B,0x82,0xD1,0x20, |
| 1220 | 0xA9,0x21,0x08,0x01,0x1A,0x72,0x3C,0x12,0xA7,0x87,0xE6,0xD7, |
| 1221 | 0x88,0x71,0x9A,0x10,0xBD,0xBA,0x5B,0x26,0x99,0xC3,0x27,0x18, |
| 1222 | 0x6A,0xF4,0xE2,0x3C,0x1A,0x94,0x68,0x34,0xB6,0x15,0x0B,0xDA, |
| 1223 | 0x25,0x83,0xE9,0xCA,0x2A,0xD4,0x4C,0xE8,0xDB,0xBB,0xC2,0xDB, |
| 1224 | 0x04,0xDE,0x8E,0xF9,0x2E,0x8E,0xFC,0x14,0x1F,0xBE,0xCA,0xA6, |
| 1225 | 0x28,0x7C,0x59,0x47,0x4E,0x6B,0xC0,0x5D,0x99,0xB2,0x96,0x4F, |
| 1226 | 0xA0,0x90,0xC3,0xA2,0x23,0x3B,0xA1,0x86,0x51,0x5B,0xE7,0xED, |
| 1227 | 0x1F,0x61,0x29,0x70,0xCE,0xE2,0xD7,0xAF,0xB8,0x1B,0xDD,0x76, |
| 1228 | 0x21,0x70,0x48,0x1C,0xD0,0x06,0x91,0x27,0xD5,0xB0,0x5A,0xA9, |
| 1229 | 0x93,0xB4,0xEA,0x98,0x8D,0x8F,0xDD,0xC1,0x86,0xFF,0xB7,0xDC, |
| 1230 | 0x90,0xA6,0xC0,0x8F,0x4D,0xF4,0x35,0xC9,0x34,0x02,0x84,0x92, |
| 1231 | 0x36,0xC3,0xFA,0xB4,0xD2,0x7C,0x70,0x26,0xC1,0xD4,0xDC,0xB2, |
| 1232 | 0x60,0x26,0x46,0xDE,0xC9,0x75,0x1E,0x76,0x3D,0xBA,0x37,0xBD, |
| 1233 | 0xF8,0xFF,0x94,0x06,0xAD,0x9E,0x53,0x0E,0xE5,0xDB,0x38,0x2F, |
| 1234 | 0x41,0x30,0x01,0xAE,0xB0,0x6A,0x53,0xED,0x90,0x27,0xD8,0x31, |
| 1235 | 0x17,0x97,0x27,0xB0,0x86,0x5A,0x89,0x18,0xDA,0x3E,0xDB,0xEB, |
| 1236 | 0xCF,0x9B,0x14,0xED,0x44,0xCE,0x6C,0xBA,0xCE,0xD4,0xBB,0x1B, |
| 1237 | 0xDB,0x7F,0x14,0x47,0xE6,0xCC,0x25,0x4B,0x33,0x20,0x51,0x51, |
| 1238 | 0x2B,0xD7,0xAF,0x42,0x6F,0xB8,0xF4,0x01,0x37,0x8C,0xD2,0xBF, |
| 1239 | 0x59,0x83,0xCA,0x01,0xC6,0x4B,0x92,0xEC,0xF0,0x32,0xEA,0x15, |
| 1240 | 0xD1,0x72,0x1D,0x03,0xF4,0x82,0xD7,0xCE,0x6E,0x74,0xFE,0xF6, |
| 1241 | 0xD5,0x5E,0x70,0x2F,0x46,0x98,0x0C,0x82,0xB5,0xA8,0x40,0x31, |
| 1242 | 0x90,0x0B,0x1C,0x9E,0x59,0xE7,0xC9,0x7F,0xBE,0xC7,0xE8,0xF3, |
| 1243 | 0x23,0xA9,0x7A,0x7E,0x36,0xCC,0x88,0xBE,0x0F,0x1D,0x45,0xB7, |
| 1244 | 0xFF,0x58,0x5A,0xC5,0x4B,0xD4,0x07,0xB2,0x2B,0x41,0x54,0xAA, |
| 1245 | 0xCC,0x8F,0x6D,0x7E,0xBF,0x48,0xE1,0xD8,0x14,0xCC,0x5E,0xD2, |
| 1246 | 0x0F,0x80,0x37,0xE0,0xA7,0x97,0x15,0xEE,0xF2,0x9B,0xE3,0x28, |
| 1247 | 0x06,0xA1,0xD5,0x8B,0xB7,0xC5,0xDA,0x76,0xF5,0x50,0xAA,0x3D, |
| 1248 | 0x8A,0x1F,0xBF,0xF0,0xEB,0x19,0xCC,0xB1,0xA3,0x13,0xD5,0x5C, |
| 1249 | 0xDA,0x56,0xC9,0xEC,0x2E,0xF2,0x96,0x32,0x38,0x7F,0xE8,0xD7, |
| 1250 | 0x6E,0x3C,0x04,0x68,0x04,0x3E,0x8F,0x66,0x3F,0x48,0x60,0xEE, |
| 1251 | 0x12,0xBF,0x2D,0x5B,0x0B,0x74,0x74,0xD6,0xE6,0x94,0xF9,0x1E, |
| 1252 | 0x6D,0xBE,0x11,0x59,0x74,0xA3,0x92,0x6F,0x12,0xFE,0xE5,0xE4, |
| 1253 | 0x38,0x77,0x7C,0xB6,0xA9,0x32,0xDF,0x8C,0xD8,0xBE,0xC4,0xD0, |
| 1254 | 0x73,0xB9,0x31,0xBA,0x3B,0xC8,0x32,0xB6,0x8D,0x9D,0xD3,0x00, |
| 1255 | 0x74,0x1F,0xA7,0xBF,0x8A,0xFC,0x47,0xED,0x25,0x76,0xF6,0x93, |
| 1256 | 0x6B,0xA4,0x24,0x66,0x3A,0xAB,0x63,0x9C,0x5A,0xE4,0xF5,0x68, |
| 1257 | 0x34,0x23,0xB4,0x74,0x2B,0xF1,0xC9,0x78,0x23,0x8F,0x16,0xCB, |
| 1258 | 0xE3,0x9D,0x65,0x2D,0xE3,0xFD,0xB8,0xBE,0xFC,0x84,0x8A,0xD9, |
| 1259 | 0x22,0x22,0x2E,0x04,0xA4,0x03,0x7C,0x07,0x13,0xEB,0x57,0xA8, |
| 1260 | 0x1A,0x23,0xF0,0xC7,0x34,0x73,0xFC,0x64,0x6C,0xEA,0x30,0x6B, |
| 1261 | 0x4B,0xCB,0xC8,0x86,0x2F,0x83,0x85,0xDD,0xFA,0x9D,0x4B,0x7F, |
| 1262 | 0xA2,0xC0,0x87,0xE8,0x79,0x68,0x33,0x03,0xED,0x5B,0xDD,0x3A, |
| 1263 | 0x06,0x2B,0x3C,0xF5,0xB3,0xA2,0x78,0xA6,0x6D,0x2A,0x13,0xF8, |
| 1264 | 0x3F,0x44,0xF8,0x2D,0xDF,0x31,0x0E,0xE0,0x74,0xAB,0x6A,0x36, |
| 1265 | 0x45,0x97,0xE8,0x99,0xA0,0x25,0x5D,0xC1,0x64,0xF3,0x1C,0xC5, |
| 1266 | 0x08,0x46,0x85,0x1D,0xF9,0xAB,0x48,0x19,0x5D,0xED,0x7E,0xA1, |
| 1267 | 0xB1,0xD5,0x10,0xBD,0x7E,0xE7,0x4D,0x73,0xFA,0xF3,0x6B,0xC3, |
| 1268 | 0x1E,0xCF,0xA2,0x68,0x35,0x90,0x46,0xF4,0xEB,0x87,0x9F,0x92, |
| 1269 | 0x40,0x09,0x43,0x8B,0x48,0x1C,0x6C,0xD7,0x88,0x9A,0x00,0x2E, |
| 1270 | 0xD5,0xEE,0x38,0x2B,0xC9,0x19,0x0D,0xA6,0xFC,0x02,0x6E,0x47, |
| 1271 | 0x95,0x58,0xE4,0x47,0x56,0x77,0xE9,0xAA,0x9E,0x30,0x50,0xE2, |
| 1272 | 0x76,0x56,0x94,0xDF,0xC8,0x1F,0x56,0xE8,0x80,0xB9,0x6E,0x71, |
| 1273 | 0x60,0xC9,0x80,0xDD,0x98,0xED,0xD3,0xDF,0xFF,0xFF,0xFF,0xFF, |
| 1274 | 0xFF,0xFF,0xFF,0xFF, |
| 1275 | }; |
| 1276 | #endif |
| 1277 | DH *dh = DH_new(); |
| 1278 | if (dh) { |
Lukas Tribus | 4c0d45a | 2014-08-18 00:56:32 +0200 | [diff] [blame] | 1279 | #if (OPENSSL_VERSION_NUMBER >= 0x0090801fL && !defined OPENSSL_IS_BORINGSSL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1280 | dh->p = get_rfc3526_prime_8192(NULL); |
| 1281 | #else |
| 1282 | dh->p = BN_bin2bn(rfc_3526_prime_8192, sizeof rfc_3526_prime_8192, NULL); |
| 1283 | #endif |
| 1284 | /* See RFC 3526, Section 7 "8192-bit MODP Group" |
| 1285 | for the reason why 2 is used as generator. |
| 1286 | */ |
| 1287 | BN_dec2bn(&dh->g, "2"); |
| 1288 | if (!dh->p || !dh->g) { |
| 1289 | DH_free(dh); |
| 1290 | dh = NULL; |
| 1291 | } |
| 1292 | } |
| 1293 | return dh; |
| 1294 | } |
| 1295 | |
| 1296 | /* Returns Diffie-Hellman parameters matching the private key length |
| 1297 | but not exceeding global.tune.ssl_default_dh_param */ |
| 1298 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 1299 | { |
| 1300 | DH *dh = NULL; |
| 1301 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
| 1302 | int type = pkey ? EVP_PKEY_type(pkey->type) : EVP_PKEY_NONE; |
| 1303 | |
| 1304 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 1305 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 1306 | */ |
| 1307 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 1308 | keylen = EVP_PKEY_bits(pkey); |
| 1309 | } |
| 1310 | |
| 1311 | if (keylen > global.tune.ssl_default_dh_param) { |
| 1312 | keylen = global.tune.ssl_default_dh_param; |
| 1313 | } |
| 1314 | |
| 1315 | if (keylen >= 8192) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1316 | dh = local_dh_8192; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1317 | } |
| 1318 | else if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1319 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1320 | } |
| 1321 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1322 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1323 | } |
| 1324 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1325 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | return dh; |
| 1329 | } |
| 1330 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1331 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 1332 | if an error occured, and 0 if parameter not found. */ |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 1333 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1334 | { |
| 1335 | int ret = -1; |
| 1336 | BIO *in; |
| 1337 | DH *dh = NULL; |
| 1338 | |
| 1339 | in = BIO_new(BIO_s_file()); |
| 1340 | if (in == NULL) |
| 1341 | goto end; |
| 1342 | |
| 1343 | if (BIO_read_filename(in, file) <= 0) |
| 1344 | goto end; |
| 1345 | |
| 1346 | dh = PEM_read_bio_DHparams(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1347 | if (dh) { |
| 1348 | ret = 1; |
| 1349 | SSL_CTX_set_tmp_dh(ctx, dh); |
| 1350 | /* Setting ssl default dh param to the size of the static DH params |
| 1351 | found in the file. This way we know that there is no use |
| 1352 | complaining later about ssl-default-dh-param not being set. */ |
| 1353 | global.tune.ssl_default_dh_param = DH_size(dh) * 8; |
| 1354 | } |
| 1355 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 1356 | /* Clear openssl global errors stack */ |
| 1357 | ERR_clear_error(); |
| 1358 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1359 | if (global.tune.ssl_default_dh_param <= 1024) { |
| 1360 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1361 | local_dh_1024 = ssl_get_dh_1024(); |
| 1362 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1363 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 1364 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1365 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1366 | } |
| 1367 | else { |
| 1368 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 1369 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 1370 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 1371 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1372 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1373 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1374 | end: |
| 1375 | if (dh) |
| 1376 | DH_free(dh); |
| 1377 | |
| 1378 | if (in) |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 1379 | BIO_free(in); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1380 | |
| 1381 | return ret; |
| 1382 | } |
| 1383 | #endif |
| 1384 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1385 | 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] | 1386 | { |
| 1387 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1388 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1389 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1390 | if (*name == '!') { |
| 1391 | neg = 1; |
| 1392 | name++; |
| 1393 | } |
| 1394 | if (*name == '*') { |
| 1395 | wild = 1; |
| 1396 | name++; |
| 1397 | } |
| 1398 | /* !* filter is a nop */ |
| 1399 | if (neg && wild) |
| 1400 | return order; |
| 1401 | if (*name) { |
| 1402 | int j, len; |
| 1403 | len = strlen(name); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1404 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
| 1405 | for (j = 0; j < len; j++) |
| 1406 | sc->name.key[j] = tolower(name[j]); |
| 1407 | sc->name.key[len] = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1408 | sc->ctx = ctx; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1409 | sc->order = order++; |
| 1410 | sc->neg = neg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1411 | if (wild) |
| 1412 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 1413 | else |
| 1414 | ebst_insert(&s->sni_ctx, &sc->name); |
| 1415 | } |
| 1416 | return order; |
| 1417 | } |
| 1418 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1419 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 1420 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 1421 | */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1422 | 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] | 1423 | { |
| 1424 | BIO *in; |
| 1425 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1426 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1427 | int ret = -1; |
| 1428 | int order = 0; |
| 1429 | X509_NAME *xname; |
| 1430 | char *str; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1431 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1432 | STACK_OF(GENERAL_NAME) *names; |
| 1433 | #endif |
| 1434 | |
| 1435 | in = BIO_new(BIO_s_file()); |
| 1436 | if (in == NULL) |
| 1437 | goto end; |
| 1438 | |
| 1439 | if (BIO_read_filename(in, file) <= 0) |
| 1440 | goto end; |
| 1441 | |
| 1442 | x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata); |
| 1443 | if (x == NULL) |
| 1444 | goto end; |
| 1445 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1446 | if (fcount) { |
| 1447 | while (fcount--) |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1448 | order = ssl_sock_add_cert_sni(ctx, s, sni_filter[fcount], order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1449 | } |
| 1450 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1451 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1452 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 1453 | if (names) { |
| 1454 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 1455 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 1456 | if (name->type == GEN_DNS) { |
| 1457 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1458 | order = ssl_sock_add_cert_sni(ctx, s, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1459 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1460 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1461 | } |
| 1462 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1463 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1464 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1465 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1466 | xname = X509_get_subject_name(x); |
| 1467 | i = -1; |
| 1468 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 1469 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
| 1470 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1471 | order = ssl_sock_add_cert_sni(ctx, s, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1472 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1473 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 1478 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 1479 | goto end; |
| 1480 | |
| 1481 | if (ctx->extra_certs != NULL) { |
| 1482 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 1483 | ctx->extra_certs = NULL; |
| 1484 | } |
| 1485 | |
| 1486 | while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata))) { |
| 1487 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 1488 | X509_free(ca); |
| 1489 | goto end; |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | err = ERR_get_error(); |
| 1494 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 1495 | /* we successfully reached the last cert in the file */ |
| 1496 | ret = 1; |
| 1497 | } |
| 1498 | ERR_clear_error(); |
| 1499 | |
| 1500 | end: |
| 1501 | if (x) |
| 1502 | X509_free(x); |
| 1503 | |
| 1504 | if (in) |
| 1505 | BIO_free(in); |
| 1506 | |
| 1507 | return ret; |
| 1508 | } |
| 1509 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1510 | 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] | 1511 | { |
| 1512 | int ret; |
| 1513 | SSL_CTX *ctx; |
| 1514 | |
| 1515 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 1516 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1517 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 1518 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1519 | return 1; |
| 1520 | } |
| 1521 | |
| 1522 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1523 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 1524 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1525 | SSL_CTX_free(ctx); |
| 1526 | return 1; |
| 1527 | } |
| 1528 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1529 | 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] | 1530 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1531 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 1532 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1533 | if (ret < 0) /* serious error, must do that ourselves */ |
| 1534 | SSL_CTX_free(ctx); |
| 1535 | return 1; |
| 1536 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 1537 | |
| 1538 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 1539 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 1540 | err && *err ? *err : "", path); |
| 1541 | return 1; |
| 1542 | } |
| 1543 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1544 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 1545 | * the tree, so it will be discovered and cleaned in time. |
| 1546 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1547 | #ifndef OPENSSL_NO_DH |
| 1548 | ret = ssl_sock_load_dh_params(ctx, path); |
| 1549 | if (ret < 0) { |
| 1550 | if (err) |
| 1551 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 1552 | *err ? *err : "", path); |
| 1553 | return 1; |
| 1554 | } |
| 1555 | #endif |
| 1556 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 1557 | #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] | 1558 | ret = ssl_sock_load_ocsp(ctx, path); |
| 1559 | if (ret < 0) { |
| 1560 | if (err) |
| 1561 | 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", |
| 1562 | *err ? *err : "", path); |
| 1563 | return 1; |
| 1564 | } |
| 1565 | #endif |
| 1566 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1567 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 1568 | if (sctl_ex_index >= 0) { |
| 1569 | ret = ssl_sock_load_sctl(ctx, path); |
| 1570 | if (ret < 0) { |
| 1571 | if (err) |
| 1572 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 1573 | *err ? *err : "", path); |
| 1574 | return 1; |
| 1575 | } |
| 1576 | } |
| 1577 | #endif |
| 1578 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1579 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1580 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1581 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 1582 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1583 | return 1; |
| 1584 | } |
| 1585 | #endif |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1586 | if (!bind_conf->default_ctx) |
| 1587 | bind_conf->default_ctx = ctx; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1588 | |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1592 | 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] | 1593 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 1594 | struct dirent **de_list; |
| 1595 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1596 | DIR *dir; |
| 1597 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 1598 | char *end; |
| 1599 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1600 | int cfgerr = 0; |
| 1601 | |
| 1602 | if (!(dir = opendir(path))) |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1603 | 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] | 1604 | |
| 1605 | /* strip trailing slashes, including first one */ |
| 1606 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 1607 | *end = 0; |
| 1608 | |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 1609 | n = scandir(path, &de_list, 0, alphasort); |
| 1610 | if (n < 0) { |
| 1611 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 1612 | err && *err ? *err : "", path, strerror(errno)); |
| 1613 | cfgerr++; |
| 1614 | } |
| 1615 | else { |
| 1616 | for (i = 0; i < n; i++) { |
| 1617 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 1618 | |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 1619 | end = strrchr(de->d_name, '.'); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1620 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 1621 | goto ignore_entry; |
| 1622 | |
| 1623 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 1624 | if (stat(fp, &buf) != 0) { |
| 1625 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 1626 | err && *err ? *err : "", fp, strerror(errno)); |
| 1627 | cfgerr++; |
| 1628 | goto ignore_entry; |
| 1629 | } |
| 1630 | if (!S_ISREG(buf.st_mode)) |
| 1631 | goto ignore_entry; |
| 1632 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, curproxy, NULL, 0, err); |
| 1633 | ignore_entry: |
| 1634 | free(de); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1635 | } |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 1636 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1637 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1638 | closedir(dir); |
| 1639 | return cfgerr; |
| 1640 | } |
| 1641 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 1642 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 1643 | * done once. Zero is returned if the operation fails. No error is returned |
| 1644 | * if the random is said as not implemented, because we expect that openssl |
| 1645 | * will use another method once needed. |
| 1646 | */ |
| 1647 | static int ssl_initialize_random() |
| 1648 | { |
| 1649 | unsigned char random; |
| 1650 | static int random_initialized = 0; |
| 1651 | |
| 1652 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 1653 | random_initialized = 1; |
| 1654 | |
| 1655 | return random_initialized; |
| 1656 | } |
| 1657 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1658 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 1659 | { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1660 | char thisline[LINESIZE]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1661 | FILE *f; |
| 1662 | int linenum = 0; |
| 1663 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1664 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1665 | if ((f = fopen(file, "r")) == NULL) { |
| 1666 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1667 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1668 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1669 | |
| 1670 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 1671 | int arg; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1672 | int newarg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1673 | char *end; |
| 1674 | char *args[MAX_LINE_ARGS + 1]; |
| 1675 | char *line = thisline; |
| 1676 | |
| 1677 | linenum++; |
| 1678 | end = line + strlen(line); |
| 1679 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 1680 | /* Check if we reached the limit and the last char is not \n. |
| 1681 | * Watch out for the last line without the terminating '\n'! |
| 1682 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1683 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 1684 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1685 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1686 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1687 | } |
| 1688 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1689 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1690 | newarg = 1; |
| 1691 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1692 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 1693 | /* end of string, end of loop */ |
| 1694 | *line = 0; |
| 1695 | break; |
| 1696 | } |
| 1697 | else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1698 | newarg = 1; |
| 1699 | *line = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1700 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1701 | else if (newarg) { |
| 1702 | if (arg == MAX_LINE_ARGS) { |
| 1703 | memprintf(err, "too many args on line %d in file '%s'.", |
| 1704 | linenum, file); |
| 1705 | cfgerr = 1; |
| 1706 | break; |
| 1707 | } |
| 1708 | newarg = 0; |
| 1709 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1710 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1711 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1712 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1713 | if (cfgerr) |
| 1714 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1715 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1716 | /* empty line */ |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1717 | if (!arg) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1718 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1719 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1720 | cfgerr = ssl_sock_load_cert_file(args[0], bind_conf, curproxy, &args[1], arg-1, err); |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1721 | if (cfgerr) { |
| 1722 | 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] | 1723 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1724 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1725 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1726 | fclose(f); |
| 1727 | return cfgerr; |
| 1728 | } |
| 1729 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1730 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 1731 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 1732 | #endif |
| 1733 | |
| 1734 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 1735 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
Willy Tarreau | 7d588ee | 2012-11-26 18:47:31 +0100 | [diff] [blame] | 1736 | #define SSL_renegotiate_pending(arg) 0 |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1737 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1738 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 1739 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1740 | #endif |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 1741 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 1742 | #define SSL_OP_NO_TICKET 0 |
| 1743 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1744 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 1745 | #define SSL_OP_NO_COMPRESSION 0 |
| 1746 | #endif |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 1747 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 1748 | #define SSL_OP_NO_TLSv1_1 0 |
| 1749 | #endif |
| 1750 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 1751 | #define SSL_OP_NO_TLSv1_2 0 |
| 1752 | #endif |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1753 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 1754 | #define SSL_OP_SINGLE_DH_USE 0 |
| 1755 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1756 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 1757 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1758 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1759 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 1760 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 1761 | #endif |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 1762 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 1763 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 1764 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1765 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1766 | 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] | 1767 | { |
| 1768 | int cfgerr = 0; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1769 | int verify = SSL_VERIFY_NONE; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 1770 | long ssloptions = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1771 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 1772 | SSL_OP_NO_SSLv2 | |
| 1773 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1774 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1775 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 1776 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
| 1777 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 1778 | long sslmode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1779 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 1780 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 1781 | SSL_MODE_RELEASE_BUFFERS | |
| 1782 | SSL_MODE_SMALL_BUFFERS; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1783 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
Willy Tarreau | a616ba6 | 2014-10-26 06:49:19 +0100 | [diff] [blame] | 1784 | SSL_CIPHER * cipher = NULL; |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 1785 | char cipher_description[128]; |
| 1786 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 1787 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 1788 | which is not ephemeral DH. */ |
| 1789 | const char dhe_description[] = " Kx=DH "; |
| 1790 | const char dhe_export_description[] = " Kx=DH("; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1791 | int idx = 0; |
| 1792 | int dhe_found = 0; |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 1793 | SSL *ssl = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1794 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 1795 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 1796 | if (!ssl_initialize_random()) { |
| 1797 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 1798 | cfgerr++; |
| 1799 | } |
| 1800 | |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1801 | if (bind_conf->ssl_options & BC_SSL_O_NO_SSLV3) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1802 | ssloptions |= SSL_OP_NO_SSLv3; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1803 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV10) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1804 | ssloptions |= SSL_OP_NO_TLSv1; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1805 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV11) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 1806 | ssloptions |= SSL_OP_NO_TLSv1_1; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1807 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV12) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 1808 | ssloptions |= SSL_OP_NO_TLSv1_2; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1809 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 1810 | ssloptions |= SSL_OP_NO_TICKET; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 1811 | if (bind_conf->ssl_options & BC_SSL_O_USE_SSLV3) |
| 1812 | SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()); |
| 1813 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV10) |
| 1814 | SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()); |
| 1815 | #if SSL_OP_NO_TLSv1_1 |
| 1816 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV11) |
| 1817 | SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method()); |
| 1818 | #endif |
| 1819 | #if SSL_OP_NO_TLSv1_2 |
| 1820 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV12) |
| 1821 | SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method()); |
| 1822 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1823 | |
| 1824 | SSL_CTX_set_options(ctx, ssloptions); |
| 1825 | SSL_CTX_set_mode(ctx, sslmode); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1826 | switch (bind_conf->verify) { |
| 1827 | case SSL_SOCK_VERIFY_NONE: |
| 1828 | verify = SSL_VERIFY_NONE; |
| 1829 | break; |
| 1830 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 1831 | verify = SSL_VERIFY_PEER; |
| 1832 | break; |
| 1833 | case SSL_SOCK_VERIFY_REQUIRED: |
| 1834 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1835 | break; |
| 1836 | } |
| 1837 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 1838 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1839 | if (bind_conf->ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1840 | /* load CAfile to verify */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1841 | if (!SSL_CTX_load_verify_locations(ctx, bind_conf->ca_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1842 | 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] | 1843 | 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] | 1844 | cfgerr++; |
| 1845 | } |
| 1846 | /* set CA names fo client cert request, function returns void */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1847 | 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] | 1848 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1849 | else { |
| 1850 | Alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 1851 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1852 | cfgerr++; |
| 1853 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 1854 | #ifdef X509_V_FLAG_CRL_CHECK |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1855 | if (bind_conf->crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1856 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 1857 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1858 | if (!store || !X509_STORE_load_locations(store, bind_conf->crl_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1859 | 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] | 1860 | 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] | 1861 | cfgerr++; |
| 1862 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 1863 | else { |
| 1864 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 1865 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1866 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 1867 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1868 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1869 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1870 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1871 | #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] | 1872 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1873 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
| 1874 | Alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 1875 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1876 | cfgerr++; |
| 1877 | } |
| 1878 | } |
| 1879 | #endif |
| 1880 | |
Emeric Brun | 4f65bff | 2012-11-16 15:11:00 +0100 | [diff] [blame] | 1881 | if (global.tune.ssllifetime) |
| 1882 | SSL_CTX_set_timeout(ctx, global.tune.ssllifetime); |
| 1883 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1884 | shared_context_set_cache(ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1885 | if (bind_conf->ciphers && |
| 1886 | !SSL_CTX_set_cipher_list(ctx, bind_conf->ciphers)) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1887 | 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] | 1888 | 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] | 1889 | cfgerr++; |
| 1890 | } |
| 1891 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1892 | /* If tune.ssl.default-dh-param has not been set and |
| 1893 | no static DH params were in the certificate file. */ |
| 1894 | if (global.tune.ssl_default_dh_param == 0) { |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 1895 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 1896 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1897 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 1898 | if (ssl) { |
| 1899 | ciphers = SSL_get_ciphers(ssl); |
| 1900 | |
| 1901 | if (ciphers) { |
| 1902 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 1903 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 1904 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 1905 | if (strstr(cipher_description, dhe_description) != NULL || |
| 1906 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 1907 | dhe_found = 1; |
| 1908 | break; |
| 1909 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 1910 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1911 | } |
| 1912 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 1913 | SSL_free(ssl); |
| 1914 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 1915 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1916 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 1917 | if (dhe_found) { |
| 1918 | 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] | 1919 | } |
| 1920 | |
| 1921 | global.tune.ssl_default_dh_param = 1024; |
| 1922 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1923 | |
| 1924 | #ifndef OPENSSL_NO_DH |
| 1925 | if (global.tune.ssl_default_dh_param >= 1024) { |
| 1926 | if (local_dh_1024 == NULL) { |
| 1927 | local_dh_1024 = ssl_get_dh_1024(); |
| 1928 | } |
| 1929 | if (global.tune.ssl_default_dh_param >= 2048) { |
| 1930 | if (local_dh_2048 == NULL) { |
| 1931 | local_dh_2048 = ssl_get_dh_2048(); |
| 1932 | } |
| 1933 | if (global.tune.ssl_default_dh_param >= 4096) { |
| 1934 | if (local_dh_4096 == NULL) { |
| 1935 | local_dh_4096 = ssl_get_dh_4096(); |
| 1936 | } |
| 1937 | if (global.tune.ssl_default_dh_param >= 8192 && |
| 1938 | local_dh_8192 == NULL) { |
| 1939 | local_dh_8192 = ssl_get_dh_8192(); |
| 1940 | } |
| 1941 | } |
| 1942 | } |
| 1943 | } |
| 1944 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1945 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1946 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 1947 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1948 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 1949 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1950 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1951 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 1952 | if (bind_conf->npn_str) |
| 1953 | SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, bind_conf); |
| 1954 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1955 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1956 | if (bind_conf->alpn_str) |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1957 | 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] | 1958 | #endif |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1959 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1960 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1961 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1962 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1963 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1964 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emeric Brun | 6924ef8 | 2013-03-06 14:08:53 +0100 | [diff] [blame] | 1965 | { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1966 | int i; |
| 1967 | EC_KEY *ecdh; |
| 1968 | |
Emeric Brun | 6924ef8 | 2013-03-06 14:08:53 +0100 | [diff] [blame] | 1969 | i = OBJ_sn2nid(bind_conf->ecdhe ? bind_conf->ecdhe : ECDHE_DEFAULT_CURVE); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1970 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
| 1971 | 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] | 1972 | curproxy->id, bind_conf->ecdhe ? bind_conf->ecdhe : ECDHE_DEFAULT_CURVE, |
| 1973 | bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1974 | cfgerr++; |
| 1975 | } |
| 1976 | else { |
| 1977 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 1978 | EC_KEY_free(ecdh); |
| 1979 | } |
| 1980 | } |
| 1981 | #endif |
| 1982 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1983 | return cfgerr; |
| 1984 | } |
| 1985 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1986 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 1987 | { |
| 1988 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 1989 | size_t prefixlen, suffixlen; |
| 1990 | |
| 1991 | /* Trivial case */ |
| 1992 | if (strcmp(pattern, hostname) == 0) |
| 1993 | return 1; |
| 1994 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1995 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 1996 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 1997 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 1998 | pattern_wildcard = NULL; |
| 1999 | pattern_left_label_end = pattern; |
| 2000 | while (*pattern_left_label_end != '.') { |
| 2001 | switch (*pattern_left_label_end) { |
| 2002 | case 0: |
| 2003 | /* End of label not found */ |
| 2004 | return 0; |
| 2005 | case '*': |
| 2006 | /* If there is more than one wildcards */ |
| 2007 | if (pattern_wildcard) |
| 2008 | return 0; |
| 2009 | pattern_wildcard = pattern_left_label_end; |
| 2010 | break; |
| 2011 | } |
| 2012 | pattern_left_label_end++; |
| 2013 | } |
| 2014 | |
| 2015 | /* If it's not trivial and there is no wildcard, it can't |
| 2016 | * match */ |
| 2017 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2018 | return 0; |
| 2019 | |
| 2020 | /* Make sure all labels match except the leftmost */ |
| 2021 | hostname_left_label_end = strchr(hostname, '.'); |
| 2022 | if (!hostname_left_label_end |
| 2023 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 2024 | return 0; |
| 2025 | |
| 2026 | /* Make sure the leftmost label of the hostname is long enough |
| 2027 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 2028 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2029 | return 0; |
| 2030 | |
| 2031 | /* Finally compare the string on either side of the |
| 2032 | * wildcard */ |
| 2033 | prefixlen = pattern_wildcard - pattern; |
| 2034 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 2035 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 2036 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2037 | return 0; |
| 2038 | |
| 2039 | return 1; |
| 2040 | } |
| 2041 | |
| 2042 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 2043 | { |
| 2044 | SSL *ssl; |
| 2045 | struct connection *conn; |
| 2046 | char *servername; |
| 2047 | |
| 2048 | int depth; |
| 2049 | X509 *cert; |
| 2050 | STACK_OF(GENERAL_NAME) *alt_names; |
| 2051 | int i; |
| 2052 | X509_NAME *cert_subject; |
| 2053 | char *str; |
| 2054 | |
| 2055 | if (ok == 0) |
| 2056 | return ok; |
| 2057 | |
| 2058 | ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 2059 | conn = (struct connection *)SSL_get_app_data(ssl); |
| 2060 | |
| 2061 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
| 2062 | |
| 2063 | /* We only need to verify the CN on the actual server cert, |
| 2064 | * not the indirect CAs */ |
| 2065 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 2066 | if (depth != 0) |
| 2067 | return ok; |
| 2068 | |
| 2069 | /* At this point, the cert is *not* OK unless we can find a |
| 2070 | * hostname match */ |
| 2071 | ok = 0; |
| 2072 | |
| 2073 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 2074 | /* It seems like this might happen if verify peer isn't set */ |
| 2075 | if (!cert) |
| 2076 | return ok; |
| 2077 | |
| 2078 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 2079 | if (alt_names) { |
| 2080 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 2081 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 2082 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 2083 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 2084 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 2085 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2086 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 2087 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2088 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 2089 | OPENSSL_free(str); |
| 2090 | } |
| 2091 | } |
| 2092 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 2093 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | cert_subject = X509_get_subject_name(cert); |
| 2097 | i = -1; |
| 2098 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 2099 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
| 2100 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) { |
| 2101 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 2102 | OPENSSL_free(str); |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | return ok; |
| 2107 | } |
| 2108 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2109 | /* prepare ssl context from servers options. Returns an error count */ |
| 2110 | int ssl_sock_prepare_srv_ctx(struct server *srv, struct proxy *curproxy) |
| 2111 | { |
| 2112 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 2113 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2114 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 2115 | SSL_OP_NO_SSLv2 | |
| 2116 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 2117 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2118 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 2119 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 2120 | SSL_MODE_RELEASE_BUFFERS | |
| 2121 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 2122 | int verify = SSL_VERIFY_NONE; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2123 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 2124 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 2125 | if (!ssl_initialize_random()) { |
| 2126 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 2127 | cfgerr++; |
| 2128 | } |
| 2129 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 2130 | /* Automatic memory computations need to know we use SSL there */ |
| 2131 | global.ssl_used_backend = 1; |
| 2132 | |
| 2133 | /* Initiate SSL context for current server */ |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2134 | srv->ssl_ctx.reused_sess = NULL; |
| 2135 | if (srv->use_ssl) |
| 2136 | srv->xprt = &ssl_sock; |
| 2137 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 2138 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2139 | |
| 2140 | srv->ssl_ctx.ctx = SSL_CTX_new(SSLv23_client_method()); |
| 2141 | if (!srv->ssl_ctx.ctx) { |
| 2142 | Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 2143 | proxy_type_str(curproxy), curproxy->id, |
| 2144 | srv->id); |
| 2145 | cfgerr++; |
| 2146 | return cfgerr; |
| 2147 | } |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 2148 | if (srv->ssl_ctx.client_crt) { |
| 2149 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
| 2150 | Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 2151 | proxy_type_str(curproxy), curproxy->id, |
| 2152 | srv->id, srv->ssl_ctx.client_crt); |
| 2153 | cfgerr++; |
| 2154 | } |
| 2155 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
| 2156 | Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 2157 | proxy_type_str(curproxy), curproxy->id, |
| 2158 | srv->id, srv->ssl_ctx.client_crt); |
| 2159 | cfgerr++; |
| 2160 | } |
| 2161 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
| 2162 | Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2163 | proxy_type_str(curproxy), curproxy->id, |
| 2164 | srv->id, srv->ssl_ctx.client_crt); |
| 2165 | cfgerr++; |
| 2166 | } |
| 2167 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2168 | |
| 2169 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_SSLV3) |
| 2170 | options |= SSL_OP_NO_SSLv3; |
| 2171 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV10) |
| 2172 | options |= SSL_OP_NO_TLSv1; |
| 2173 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV11) |
| 2174 | options |= SSL_OP_NO_TLSv1_1; |
| 2175 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV12) |
| 2176 | options |= SSL_OP_NO_TLSv1_2; |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 2177 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 2178 | options |= SSL_OP_NO_TICKET; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2179 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_SSLV3) |
| 2180 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, SSLv3_client_method()); |
| 2181 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV10) |
| 2182 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_client_method()); |
| 2183 | #if SSL_OP_NO_TLSv1_1 |
| 2184 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV11) |
| 2185 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_1_client_method()); |
| 2186 | #endif |
| 2187 | #if SSL_OP_NO_TLSv1_2 |
| 2188 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV12) |
| 2189 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_2_client_method()); |
| 2190 | #endif |
| 2191 | |
| 2192 | SSL_CTX_set_options(srv->ssl_ctx.ctx, options); |
| 2193 | SSL_CTX_set_mode(srv->ssl_ctx.ctx, mode); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 2194 | |
| 2195 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 2196 | verify = SSL_VERIFY_PEER; |
| 2197 | |
| 2198 | switch (srv->ssl_ctx.verify) { |
| 2199 | case SSL_SOCK_VERIFY_NONE: |
| 2200 | verify = SSL_VERIFY_NONE; |
| 2201 | break; |
| 2202 | case SSL_SOCK_VERIFY_REQUIRED: |
| 2203 | verify = SSL_VERIFY_PEER; |
| 2204 | break; |
| 2205 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2206 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 2207 | verify, |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2208 | srv->ssl_ctx.verify_host ? ssl_sock_srv_verifycbk : NULL); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 2209 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 2210 | if (srv->ssl_ctx.ca_file) { |
| 2211 | /* load CAfile to verify */ |
| 2212 | 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] | 2213 | 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] | 2214 | curproxy->id, srv->id, |
| 2215 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
| 2216 | cfgerr++; |
| 2217 | } |
| 2218 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 2219 | else { |
| 2220 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 2221 | 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] | 2222 | curproxy->id, srv->id, |
| 2223 | srv->conf.file, srv->conf.line); |
| 2224 | else |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 2225 | 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] | 2226 | curproxy->id, srv->id, |
| 2227 | srv->conf.file, srv->conf.line); |
| 2228 | cfgerr++; |
| 2229 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 2230 | #ifdef X509_V_FLAG_CRL_CHECK |
| 2231 | if (srv->ssl_ctx.crl_file) { |
| 2232 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 2233 | |
| 2234 | 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] | 2235 | 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] | 2236 | curproxy->id, srv->id, |
| 2237 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
| 2238 | cfgerr++; |
| 2239 | } |
| 2240 | else { |
| 2241 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 2242 | } |
| 2243 | } |
| 2244 | #endif |
| 2245 | } |
| 2246 | |
Emeric Brun | 4f65bff | 2012-11-16 15:11:00 +0100 | [diff] [blame] | 2247 | if (global.tune.ssllifetime) |
| 2248 | SSL_CTX_set_timeout(srv->ssl_ctx.ctx, global.tune.ssllifetime); |
| 2249 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 2250 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF); |
| 2251 | if (srv->ssl_ctx.ciphers && |
| 2252 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
| 2253 | Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 2254 | curproxy->id, srv->id, |
| 2255 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
| 2256 | cfgerr++; |
| 2257 | } |
| 2258 | |
| 2259 | return cfgerr; |
| 2260 | } |
| 2261 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2262 | /* 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] | 2263 | * be NULL, in which case nothing is done. Returns the number of errors |
| 2264 | * encountered. |
| 2265 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2266 | 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] | 2267 | { |
| 2268 | struct ebmb_node *node; |
| 2269 | struct sni_ctx *sni; |
| 2270 | int err = 0; |
| 2271 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2272 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2273 | return 0; |
| 2274 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 2275 | /* Automatic memory computations need to know we use SSL there */ |
| 2276 | global.ssl_used_frontend = 1; |
| 2277 | |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 2278 | if (bind_conf->default_ctx) |
| 2279 | err += ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ctx, px); |
| 2280 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2281 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2282 | while (node) { |
| 2283 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 2284 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 2285 | /* only initialize the CTX on its first occurrence and |
| 2286 | if it is not the default_ctx */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2287 | err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2288 | node = ebmb_next(node); |
| 2289 | } |
| 2290 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2291 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2292 | while (node) { |
| 2293 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 2294 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 2295 | /* only initialize the CTX on its first occurrence and |
| 2296 | if it is not the default_ctx */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2297 | err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2298 | node = ebmb_next(node); |
| 2299 | } |
| 2300 | return err; |
| 2301 | } |
| 2302 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2303 | /* 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] | 2304 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 2305 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2306 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2307 | { |
| 2308 | struct ebmb_node *node, *back; |
| 2309 | struct sni_ctx *sni; |
| 2310 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2311 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2312 | return; |
| 2313 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2314 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2315 | while (node) { |
| 2316 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 2317 | back = ebmb_next(node); |
| 2318 | ebmb_delete(node); |
| 2319 | if (!sni->order) /* only free the CTX on its first occurrence */ |
| 2320 | SSL_CTX_free(sni->ctx); |
| 2321 | free(sni); |
| 2322 | node = back; |
| 2323 | } |
| 2324 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2325 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2326 | while (node) { |
| 2327 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 2328 | back = ebmb_next(node); |
| 2329 | ebmb_delete(node); |
| 2330 | if (!sni->order) /* only free the CTX on its first occurrence */ |
| 2331 | SSL_CTX_free(sni->ctx); |
| 2332 | free(sni); |
| 2333 | node = back; |
| 2334 | } |
| 2335 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 2336 | bind_conf->default_ctx = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 2337 | } |
| 2338 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2339 | /* |
| 2340 | * This function is called if SSL * context is not yet allocated. The function |
| 2341 | * is designed to be called before any other data-layer operation and sets the |
| 2342 | * handshake flag on the connection. It is safe to call it multiple times. |
| 2343 | * It returns 0 on success and -1 in error case. |
| 2344 | */ |
| 2345 | static int ssl_sock_init(struct connection *conn) |
| 2346 | { |
| 2347 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2348 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2349 | return 0; |
| 2350 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 2351 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 2352 | return 0; |
| 2353 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2354 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 2355 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 2356 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2357 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 2358 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2359 | /* If it is in client mode initiate SSL session |
| 2360 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 2361 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 2362 | int may_retry = 1; |
| 2363 | |
| 2364 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2365 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 2366 | conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2367 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 2368 | if (may_retry--) { |
| 2369 | pool_gc2(); |
| 2370 | goto retry_connect; |
| 2371 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2372 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2373 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2374 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2375 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2376 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 2377 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 2378 | SSL_free(conn->xprt_ctx); |
| 2379 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 2380 | if (may_retry--) { |
| 2381 | pool_gc2(); |
| 2382 | goto retry_connect; |
| 2383 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 2384 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 2385 | return -1; |
| 2386 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2387 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2388 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 2389 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 2390 | SSL_free(conn->xprt_ctx); |
| 2391 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 2392 | if (may_retry--) { |
| 2393 | pool_gc2(); |
| 2394 | goto retry_connect; |
| 2395 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 2396 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 2397 | return -1; |
| 2398 | } |
| 2399 | |
| 2400 | SSL_set_connect_state(conn->xprt_ctx); |
| 2401 | if (objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 2402 | if(!SSL_set_session(conn->xprt_ctx, objt_server(conn->target)->ssl_ctx.reused_sess)) { |
| 2403 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 2404 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
| 2405 | } |
| 2406 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 2407 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2408 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 2409 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 2410 | |
| 2411 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 2412 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2413 | return 0; |
| 2414 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 2415 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 2416 | int may_retry = 1; |
| 2417 | |
| 2418 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2419 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 2420 | 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] | 2421 | if (!conn->xprt_ctx) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 2422 | if (may_retry--) { |
| 2423 | pool_gc2(); |
| 2424 | goto retry_accept; |
| 2425 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2426 | conn->err_code = CO_ER_SSL_NO_MEM; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2427 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2428 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2429 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2430 | /* set fd on SSL session context */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 2431 | if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) { |
| 2432 | SSL_free(conn->xprt_ctx); |
| 2433 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 2434 | if (may_retry--) { |
| 2435 | pool_gc2(); |
| 2436 | goto retry_accept; |
| 2437 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 2438 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 2439 | return -1; |
| 2440 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2441 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 2442 | /* set connection pointer */ |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 2443 | if (!SSL_set_app_data(conn->xprt_ctx, conn)) { |
| 2444 | SSL_free(conn->xprt_ctx); |
| 2445 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 2446 | if (may_retry--) { |
| 2447 | pool_gc2(); |
| 2448 | goto retry_accept; |
| 2449 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 2450 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 2451 | return -1; |
| 2452 | } |
| 2453 | |
| 2454 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 2455 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2456 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 2457 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 2458 | |
| 2459 | sslconns++; |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 2460 | totalsslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2461 | return 0; |
| 2462 | } |
| 2463 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2464 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2465 | return -1; |
| 2466 | } |
| 2467 | |
| 2468 | |
| 2469 | /* This is the callback which is used when an SSL handshake is pending. It |
| 2470 | * updates the FD status if it wants some polling before being called again. |
| 2471 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 2472 | * otherwise it returns non-zero and removes itself from the connection's |
| 2473 | * flags (the bit is provided in <flag> by the caller). |
| 2474 | */ |
| 2475 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 2476 | { |
| 2477 | int ret; |
| 2478 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 2479 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 2480 | return 0; |
| 2481 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2482 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2483 | goto out_error; |
| 2484 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 2485 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 2486 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 2487 | * Usually SSL_write and SSL_read are used and process implicitly |
| 2488 | * the reneg handshake. |
| 2489 | * Here we use SSL_peek as a workaround for reneg. |
| 2490 | */ |
| 2491 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 2492 | char c; |
| 2493 | |
| 2494 | ret = SSL_peek(conn->xprt_ctx, &c, 1); |
| 2495 | if (ret <= 0) { |
| 2496 | /* handshake may have not been completed, let's find why */ |
| 2497 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 2498 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 2499 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 2500 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 2501 | __conn_sock_want_send(conn); |
| 2502 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 2503 | return 0; |
| 2504 | } |
| 2505 | else if (ret == SSL_ERROR_WANT_READ) { |
| 2506 | /* handshake may have been completed but we have |
| 2507 | * no more data to read. |
| 2508 | */ |
| 2509 | if (!SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 2510 | ret = 1; |
| 2511 | goto reneg_ok; |
| 2512 | } |
| 2513 | /* SSL handshake needs to read, L4 connection is ready */ |
| 2514 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 2515 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 2516 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 2517 | __conn_sock_want_recv(conn); |
| 2518 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 2519 | return 0; |
| 2520 | } |
| 2521 | else if (ret == SSL_ERROR_SYSCALL) { |
| 2522 | /* if errno is null, then connection was successfully established */ |
| 2523 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 2524 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2525 | if (!conn->err_code) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 2526 | if (!((SSL *)conn->xprt_ctx)->packet_length) { |
| 2527 | if (!errno) { |
| 2528 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 2529 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 2530 | else |
| 2531 | conn->err_code = CO_ER_SSL_EMPTY; |
| 2532 | } |
| 2533 | else { |
| 2534 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 2535 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 2536 | else |
| 2537 | conn->err_code = CO_ER_SSL_ABORT; |
| 2538 | } |
| 2539 | } |
| 2540 | else { |
| 2541 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 2542 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2543 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 2544 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 2545 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2546 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 2547 | goto out_error; |
| 2548 | } |
| 2549 | else { |
| 2550 | /* Fail on all other handshake errors */ |
| 2551 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 2552 | * buffer, causing an RST to be emitted upon close() on |
| 2553 | * TCP sockets. We first try to drain possibly pending |
| 2554 | * data to avoid this as much as possible. |
| 2555 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 2556 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2557 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 2558 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 2559 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 2560 | goto out_error; |
| 2561 | } |
| 2562 | } |
| 2563 | /* read some data: consider handshake completed */ |
| 2564 | goto reneg_ok; |
| 2565 | } |
| 2566 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2567 | ret = SSL_do_handshake(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2568 | if (ret != 1) { |
| 2569 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2570 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2571 | |
| 2572 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 2573 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 2574 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 2575 | __conn_sock_want_send(conn); |
| 2576 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2577 | return 0; |
| 2578 | } |
| 2579 | else if (ret == SSL_ERROR_WANT_READ) { |
| 2580 | /* SSL handshake needs to read, L4 connection is ready */ |
| 2581 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 2582 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 2583 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 2584 | __conn_sock_want_recv(conn); |
| 2585 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2586 | return 0; |
| 2587 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 2588 | else if (ret == SSL_ERROR_SYSCALL) { |
| 2589 | /* if errno is null, then connection was successfully established */ |
| 2590 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 2591 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2592 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 2593 | if (!((SSL *)conn->xprt_ctx)->packet_length) { |
| 2594 | if (!errno) { |
| 2595 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 2596 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 2597 | else |
| 2598 | conn->err_code = CO_ER_SSL_EMPTY; |
| 2599 | } |
| 2600 | else { |
| 2601 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 2602 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 2603 | else |
| 2604 | conn->err_code = CO_ER_SSL_ABORT; |
| 2605 | } |
| 2606 | } |
| 2607 | else { |
| 2608 | if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
| 2609 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2610 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 2611 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 2612 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 2613 | goto out_error; |
| 2614 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2615 | else { |
| 2616 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 2617 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 2618 | * buffer, causing an RST to be emitted upon close() on |
| 2619 | * TCP sockets. We first try to drain possibly pending |
| 2620 | * data to avoid this as much as possible. |
| 2621 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 2622 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2623 | if (!conn->err_code) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 2624 | conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
| 2625 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2626 | goto out_error; |
| 2627 | } |
| 2628 | } |
| 2629 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 2630 | reneg_ok: |
| 2631 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2632 | /* Handshake succeeded */ |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 2633 | if (!SSL_session_reused(conn->xprt_ctx)) { |
| 2634 | if (objt_server(conn->target)) { |
| 2635 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 2636 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 2637 | global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr; |
| 2638 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2639 | /* 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] | 2640 | if (objt_server(conn->target)->ssl_ctx.reused_sess) |
| 2641 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2642 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 2643 | if (!(objt_server(conn->target)->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) |
| 2644 | 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] | 2645 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 2646 | else { |
| 2647 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 2648 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 2649 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 2650 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | /* The connection is now established at both layers, it's time to leave */ |
| 2654 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 2655 | return 1; |
| 2656 | |
| 2657 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2658 | /* Clear openssl global errors stack */ |
| 2659 | ERR_clear_error(); |
| 2660 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 2661 | /* free resumed session if exists */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 2662 | if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) { |
| 2663 | SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess); |
| 2664 | objt_server(conn->target)->ssl_ctx.reused_sess = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 2665 | } |
| 2666 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2667 | /* Fail on all other handshake errors */ |
| 2668 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 2669 | if (!conn->err_code) |
| 2670 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2671 | return 0; |
| 2672 | } |
| 2673 | |
| 2674 | /* 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] | 2675 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2676 | * buffer wraps, in which case a second call may be performed. The connection's |
| 2677 | * flags are updated with whatever special event is detected (error, read0, |
| 2678 | * empty). The caller is responsible for taking care of those events and |
| 2679 | * avoiding the call if inappropriate. The function does not call the |
| 2680 | * connection's polling update function, so the caller is responsible for this. |
| 2681 | */ |
| 2682 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 2683 | { |
| 2684 | int ret, done = 0; |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 2685 | int try; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2686 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2687 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2688 | goto out_error; |
| 2689 | |
| 2690 | if (conn->flags & CO_FL_HANDSHAKE) |
| 2691 | /* a handshake was requested */ |
| 2692 | return 0; |
| 2693 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 2694 | /* let's realign the buffer to optimize I/O */ |
| 2695 | if (buffer_empty(buf)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2696 | buf->p = buf->data; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2697 | |
| 2698 | /* read the largest possible block. For this, we perform only one call |
| 2699 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 2700 | * in which case we accept to do it once again. A new attempt is made on |
| 2701 | * EINTR too. |
| 2702 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 2703 | while (count > 0) { |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 2704 | /* first check if we have some room after p+i */ |
| 2705 | try = buf->data + buf->size - (buf->p + buf->i); |
| 2706 | /* otherwise continue between data and p-o */ |
| 2707 | if (try <= 0) { |
| 2708 | try = buf->p - (buf->data + buf->o); |
| 2709 | if (try <= 0) |
| 2710 | break; |
| 2711 | } |
| 2712 | if (try > count) |
| 2713 | try = count; |
| 2714 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2715 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 2716 | if (conn->flags & CO_FL_ERROR) { |
| 2717 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2718 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 2719 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2720 | if (ret > 0) { |
| 2721 | buf->i += ret; |
| 2722 | done += ret; |
| 2723 | if (ret < try) |
| 2724 | break; |
| 2725 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2726 | } |
| 2727 | else if (ret == 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2728 | ret = SSL_get_error(conn->xprt_ctx, ret); |
| 2729 | if (ret != SSL_ERROR_ZERO_RETURN) { |
Emeric Brun | 1c64686 | 2012-12-14 12:33:41 +0100 | [diff] [blame] | 2730 | /* error on protocol or underlying transport */ |
| 2731 | if ((ret != SSL_ERROR_SYSCALL) |
| 2732 | || (errno && (errno != EAGAIN))) |
| 2733 | conn->flags |= CO_FL_ERROR; |
| 2734 | |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2735 | /* Clear openssl global errors stack */ |
| 2736 | ERR_clear_error(); |
| 2737 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2738 | goto read0; |
| 2739 | } |
| 2740 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2741 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2742 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 2743 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2744 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 2745 | __conn_sock_want_send(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2746 | break; |
| 2747 | } |
| 2748 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 2749 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 2750 | /* handshake is running, and it may need to re-enable read */ |
| 2751 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 2752 | __conn_sock_want_recv(conn); |
| 2753 | break; |
| 2754 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2755 | /* we need to poll for retry a read later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 2756 | fd_cant_recv(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2757 | break; |
| 2758 | } |
| 2759 | /* otherwise it's a real error */ |
| 2760 | goto out_error; |
| 2761 | } |
| 2762 | } |
| 2763 | return done; |
| 2764 | |
| 2765 | read0: |
| 2766 | conn_sock_read0(conn); |
| 2767 | return done; |
| 2768 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2769 | /* Clear openssl global errors stack */ |
| 2770 | ERR_clear_error(); |
| 2771 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2772 | conn->flags |= CO_FL_ERROR; |
| 2773 | return done; |
| 2774 | } |
| 2775 | |
| 2776 | |
| 2777 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 2778 | * <flags> may contain some CO_SFL_* flags to hint the system about other |
| 2779 | * pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2780 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 2781 | * a second call may be performed. The connection's flags are updated with |
| 2782 | * whatever special event is detected (error, empty). The caller is responsible |
| 2783 | * for taking care of those events and avoiding the call if inappropriate. The |
| 2784 | * function does not call the connection's polling update function, so the caller |
| 2785 | * is responsible for this. |
| 2786 | */ |
| 2787 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 2788 | { |
| 2789 | int ret, try, done; |
| 2790 | |
| 2791 | done = 0; |
| 2792 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2793 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2794 | goto out_error; |
| 2795 | |
| 2796 | if (conn->flags & CO_FL_HANDSHAKE) |
| 2797 | /* a handshake was requested */ |
| 2798 | return 0; |
| 2799 | |
| 2800 | /* send the largest possible block. For this we perform only one call |
| 2801 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 2802 | * in which case we accept to do it once again. |
| 2803 | */ |
| 2804 | while (buf->o) { |
Kevin Hester | cad8234 | 2013-05-30 15:12:41 -0700 | [diff] [blame] | 2805 | try = bo_contig_data(buf); |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 2806 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 2807 | if (!(flags & CO_SFL_STREAMER) && |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 2808 | !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
| 2809 | global.tune.ssl_max_record && try > global.tune.ssl_max_record) { |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 2810 | try = global.tune.ssl_max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 2811 | } |
| 2812 | else { |
| 2813 | /* we need to keep the information about the fact that |
| 2814 | * we're not limiting the upcoming send(), because if it |
| 2815 | * fails, we'll have to retry with at least as many data. |
| 2816 | */ |
| 2817 | conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
| 2818 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 2819 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2820 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 2821 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 2822 | if (conn->flags & CO_FL_ERROR) { |
| 2823 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2824 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 2825 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2826 | if (ret > 0) { |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 2827 | conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
| 2828 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2829 | buf->o -= ret; |
| 2830 | done += ret; |
| 2831 | |
Willy Tarreau | 5fb3803 | 2012-12-16 19:39:09 +0100 | [diff] [blame] | 2832 | if (likely(buffer_empty(buf))) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2833 | /* optimize data alignment in the buffer */ |
| 2834 | buf->p = buf->data; |
| 2835 | |
| 2836 | /* if the system buffer is full, don't insist */ |
| 2837 | if (ret < try) |
| 2838 | break; |
| 2839 | } |
| 2840 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2841 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2842 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 2843 | if (SSL_renegotiate_pending(conn->xprt_ctx)) { |
| 2844 | /* handshake is running, and it may need to re-enable write */ |
| 2845 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 2846 | __conn_sock_want_send(conn); |
| 2847 | break; |
| 2848 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2849 | /* we need to poll to retry a write later */ |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 2850 | fd_cant_send(conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2851 | break; |
| 2852 | } |
| 2853 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 2854 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2855 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 2856 | __conn_sock_want_recv(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2857 | break; |
| 2858 | } |
| 2859 | goto out_error; |
| 2860 | } |
| 2861 | } |
| 2862 | return done; |
| 2863 | |
| 2864 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2865 | /* Clear openssl global errors stack */ |
| 2866 | ERR_clear_error(); |
| 2867 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2868 | conn->flags |= CO_FL_ERROR; |
| 2869 | return done; |
| 2870 | } |
| 2871 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2872 | static void ssl_sock_close(struct connection *conn) { |
| 2873 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2874 | if (conn->xprt_ctx) { |
| 2875 | SSL_free(conn->xprt_ctx); |
| 2876 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 2877 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2878 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 2882 | * any case, flags the connection as reusable if no handshake was in progress. |
| 2883 | */ |
| 2884 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 2885 | { |
| 2886 | if (conn->flags & CO_FL_HANDSHAKE) |
| 2887 | return; |
| 2888 | /* no handshake was in progress, try a clean ssl shutdown */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2889 | if (clean && (SSL_shutdown(conn->xprt_ctx) <= 0)) { |
| 2890 | /* Clear openssl global errors stack */ |
| 2891 | ERR_clear_error(); |
| 2892 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2893 | |
| 2894 | /* force flag on ssl to keep session in cache regardless shutdown result */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2895 | SSL_set_shutdown(conn->xprt_ctx, SSL_SENT_SHUTDOWN); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2896 | } |
| 2897 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2898 | /* used for logging, may be changed for a sample fetch later */ |
| 2899 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 2900 | { |
| 2901 | if (!conn->xprt && !conn->xprt_ctx) |
| 2902 | return NULL; |
| 2903 | return SSL_get_cipher_name(conn->xprt_ctx); |
| 2904 | } |
| 2905 | |
| 2906 | /* used for logging, may be changed for a sample fetch later */ |
| 2907 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 2908 | { |
| 2909 | if (!conn->xprt && !conn->xprt_ctx) |
| 2910 | return NULL; |
| 2911 | return SSL_get_version(conn->xprt_ctx); |
| 2912 | } |
| 2913 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 2914 | /* Extract a serial from a cert, and copy it to a chunk. |
| 2915 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 2916 | * -1 if output is not large enough. |
| 2917 | */ |
| 2918 | static int |
| 2919 | ssl_sock_get_serial(X509 *crt, struct chunk *out) |
| 2920 | { |
| 2921 | ASN1_INTEGER *serial; |
| 2922 | |
| 2923 | serial = X509_get_serialNumber(crt); |
| 2924 | if (!serial) |
| 2925 | return 0; |
| 2926 | |
| 2927 | if (out->size < serial->length) |
| 2928 | return -1; |
| 2929 | |
| 2930 | memcpy(out->str, serial->data, serial->length); |
| 2931 | out->len = serial->length; |
| 2932 | return 1; |
| 2933 | } |
| 2934 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 2935 | /* Extract a cert to der, and copy it to a chunk. |
| 2936 | * Returns 1 if cert is found and copied, 0 on der convertion failure and |
| 2937 | * -1 if output is not large enough. |
| 2938 | */ |
| 2939 | static int |
| 2940 | ssl_sock_crt2der(X509 *crt, struct chunk *out) |
| 2941 | { |
| 2942 | int len; |
| 2943 | unsigned char *p = (unsigned char *)out->str;; |
| 2944 | |
| 2945 | len =i2d_X509(crt, NULL); |
| 2946 | if (len <= 0) |
| 2947 | return 1; |
| 2948 | |
| 2949 | if (out->size < len) |
| 2950 | return -1; |
| 2951 | |
| 2952 | i2d_X509(crt,&p); |
| 2953 | out->len = len; |
| 2954 | return 1; |
| 2955 | } |
| 2956 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 2957 | |
| 2958 | /* Copy Date in ASN1_UTCTIME format in struct chunk out. |
| 2959 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 2960 | * and -1 if output is not large enough. |
| 2961 | */ |
| 2962 | static int |
| 2963 | ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out) |
| 2964 | { |
| 2965 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 2966 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 2967 | |
| 2968 | if (gentm->length < 12) |
| 2969 | return 0; |
| 2970 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 2971 | return 0; |
| 2972 | if (out->size < gentm->length-2) |
| 2973 | return -1; |
| 2974 | |
| 2975 | memcpy(out->str, gentm->data+2, gentm->length-2); |
| 2976 | out->len = gentm->length-2; |
| 2977 | return 1; |
| 2978 | } |
| 2979 | else if (tm->type == V_ASN1_UTCTIME) { |
| 2980 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 2981 | |
| 2982 | if (utctm->length < 10) |
| 2983 | return 0; |
| 2984 | if (utctm->data[0] >= 0x35) |
| 2985 | return 0; |
| 2986 | if (out->size < utctm->length) |
| 2987 | return -1; |
| 2988 | |
| 2989 | memcpy(out->str, utctm->data, utctm->length); |
| 2990 | out->len = utctm->length; |
| 2991 | return 1; |
| 2992 | } |
| 2993 | |
| 2994 | return 0; |
| 2995 | } |
| 2996 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 2997 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 2998 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 2999 | */ |
| 3000 | static int |
| 3001 | ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out) |
| 3002 | { |
| 3003 | X509_NAME_ENTRY *ne; |
| 3004 | int i, j, n; |
| 3005 | int cur = 0; |
| 3006 | const char *s; |
| 3007 | char tmp[128]; |
| 3008 | |
| 3009 | out->len = 0; |
| 3010 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
| 3011 | if (pos < 0) |
| 3012 | j = (sk_X509_NAME_ENTRY_num(a->entries)-1) - i; |
| 3013 | else |
| 3014 | j = i; |
| 3015 | |
| 3016 | ne = sk_X509_NAME_ENTRY_value(a->entries, j); |
| 3017 | n = OBJ_obj2nid(ne->object); |
| 3018 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
| 3019 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), ne->object); |
| 3020 | s = tmp; |
| 3021 | } |
| 3022 | |
| 3023 | if (chunk_strcasecmp(entry, s) != 0) |
| 3024 | continue; |
| 3025 | |
| 3026 | if (pos < 0) |
| 3027 | cur--; |
| 3028 | else |
| 3029 | cur++; |
| 3030 | |
| 3031 | if (cur != pos) |
| 3032 | continue; |
| 3033 | |
| 3034 | if (ne->value->length > out->size) |
| 3035 | return -1; |
| 3036 | |
| 3037 | memcpy(out->str, ne->value->data, ne->value->length); |
| 3038 | out->len = ne->value->length; |
| 3039 | return 1; |
| 3040 | } |
| 3041 | |
| 3042 | return 0; |
| 3043 | |
| 3044 | } |
| 3045 | |
| 3046 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 3047 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 3048 | */ |
| 3049 | static int |
| 3050 | ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out) |
| 3051 | { |
| 3052 | X509_NAME_ENTRY *ne; |
| 3053 | int i, n, ln; |
| 3054 | int l = 0; |
| 3055 | const char *s; |
| 3056 | char *p; |
| 3057 | char tmp[128]; |
| 3058 | |
| 3059 | out->len = 0; |
| 3060 | p = out->str; |
| 3061 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
| 3062 | ne = sk_X509_NAME_ENTRY_value(a->entries, i); |
| 3063 | n = OBJ_obj2nid(ne->object); |
| 3064 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
| 3065 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), ne->object); |
| 3066 | s = tmp; |
| 3067 | } |
| 3068 | ln = strlen(s); |
| 3069 | |
| 3070 | l += 1 + ln + 1 + ne->value->length; |
| 3071 | if (l > out->size) |
| 3072 | return -1; |
| 3073 | out->len = l; |
| 3074 | |
| 3075 | *(p++)='/'; |
| 3076 | memcpy(p, s, ln); |
| 3077 | p += ln; |
| 3078 | *(p++)='='; |
| 3079 | memcpy(p, ne->value->data, ne->value->length); |
| 3080 | p += ne->value->length; |
| 3081 | } |
| 3082 | |
| 3083 | if (!out->len) |
| 3084 | return 0; |
| 3085 | |
| 3086 | return 1; |
| 3087 | } |
| 3088 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 3089 | char *ssl_sock_get_version(struct connection *conn) |
| 3090 | { |
| 3091 | if (!ssl_sock_is_ssl(conn)) |
| 3092 | return NULL; |
| 3093 | |
| 3094 | return (char *)SSL_get_version(conn->xprt_ctx); |
| 3095 | } |
| 3096 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 3097 | /* Extract peer certificate's common name into the chunk dest |
| 3098 | * Returns |
| 3099 | * the len of the extracted common name |
| 3100 | * or 0 if no CN found in DN |
| 3101 | * or -1 on error case (i.e. no peer certificate) |
| 3102 | */ |
| 3103 | 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] | 3104 | { |
| 3105 | X509 *crt = NULL; |
| 3106 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 3107 | const char find_cn[] = "CN"; |
| 3108 | const struct chunk find_cn_chunk = { |
| 3109 | .str = (char *)&find_cn, |
| 3110 | .len = sizeof(find_cn)-1 |
| 3111 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 3112 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 3113 | |
| 3114 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 3115 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 3116 | |
| 3117 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 3118 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 3119 | if (!crt) |
| 3120 | goto out; |
| 3121 | |
| 3122 | name = X509_get_subject_name(crt); |
| 3123 | if (!name) |
| 3124 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 3125 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 3126 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 3127 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 3128 | if (crt) |
| 3129 | X509_free(crt); |
| 3130 | |
| 3131 | return result; |
| 3132 | } |
| 3133 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 3134 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 3135 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 3136 | { |
| 3137 | X509 *crt = NULL; |
| 3138 | |
| 3139 | if (!ssl_sock_is_ssl(conn)) |
| 3140 | return 0; |
| 3141 | |
| 3142 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 3143 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 3144 | if (!crt) |
| 3145 | return 0; |
| 3146 | |
| 3147 | X509_free(crt); |
| 3148 | return 1; |
| 3149 | } |
| 3150 | |
| 3151 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 3152 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 3153 | { |
| 3154 | if (!ssl_sock_is_ssl(conn)) |
| 3155 | return 0; |
| 3156 | |
| 3157 | return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
| 3158 | } |
| 3159 | |
| 3160 | /* returns result from SSL verify */ |
| 3161 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 3162 | { |
| 3163 | if (!ssl_sock_is_ssl(conn)) |
| 3164 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 3165 | |
| 3166 | return (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
| 3167 | } |
| 3168 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3169 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 3170 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 3171 | /* boolean, returns true if client cert was present */ |
| 3172 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3173 | 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] | 3174 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3175 | struct connection *conn; |
| 3176 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3177 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3178 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 3179 | return 0; |
| 3180 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3181 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 3182 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3183 | return 0; |
| 3184 | } |
| 3185 | |
| 3186 | smp->flags = 0; |
| 3187 | smp->type = SMP_T_BOOL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3188 | smp->data.uint = SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 3189 | |
| 3190 | return 1; |
| 3191 | } |
| 3192 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 3193 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 3194 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 3195 | * should be use. |
| 3196 | */ |
| 3197 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3198 | 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] | 3199 | { |
| 3200 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 3201 | X509 *crt = NULL; |
| 3202 | int ret = 0; |
| 3203 | struct chunk *smp_trash; |
| 3204 | struct connection *conn; |
| 3205 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3206 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 3207 | if (!conn || conn->xprt != &ssl_sock) |
| 3208 | return 0; |
| 3209 | |
| 3210 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 3211 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3212 | return 0; |
| 3213 | } |
| 3214 | |
| 3215 | if (cert_peer) |
| 3216 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 3217 | else |
| 3218 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 3219 | |
| 3220 | if (!crt) |
| 3221 | goto out; |
| 3222 | |
| 3223 | smp_trash = get_trash_chunk(); |
| 3224 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 3225 | goto out; |
| 3226 | |
| 3227 | smp->data.str = *smp_trash; |
| 3228 | smp->type = SMP_T_BIN; |
| 3229 | ret = 1; |
| 3230 | out: |
| 3231 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 3232 | if (cert_peer && crt) |
| 3233 | X509_free(crt); |
| 3234 | return ret; |
| 3235 | } |
| 3236 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3237 | /* binary, returns serial of certificate in a binary chunk. |
| 3238 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 3239 | * should be use. |
| 3240 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 3241 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3242 | 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] | 3243 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3244 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 3245 | X509 *crt = NULL; |
| 3246 | int ret = 0; |
| 3247 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3248 | struct connection *conn; |
| 3249 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3250 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3251 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 3252 | return 0; |
| 3253 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3254 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 3255 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3256 | return 0; |
| 3257 | } |
| 3258 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3259 | if (cert_peer) |
| 3260 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 3261 | else |
| 3262 | crt = SSL_get_certificate(conn->xprt_ctx); |
| 3263 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 3264 | if (!crt) |
| 3265 | goto out; |
| 3266 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 3267 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 3268 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 3269 | goto out; |
| 3270 | |
| 3271 | smp->data.str = *smp_trash; |
| 3272 | smp->type = SMP_T_BIN; |
| 3273 | ret = 1; |
| 3274 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3275 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 3276 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 3277 | X509_free(crt); |
| 3278 | return ret; |
| 3279 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 3280 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3281 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 3282 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 3283 | * should be use. |
| 3284 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 3285 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3286 | 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] | 3287 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3288 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 3289 | X509 *crt = NULL; |
| 3290 | const EVP_MD *digest; |
| 3291 | int ret = 0; |
| 3292 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3293 | struct connection *conn; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 3294 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3295 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3296 | if (!conn || conn->xprt != &ssl_sock) |
| 3297 | return 0; |
| 3298 | |
| 3299 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 3300 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3301 | return 0; |
| 3302 | } |
| 3303 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3304 | if (cert_peer) |
| 3305 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 3306 | else |
| 3307 | crt = SSL_get_certificate(conn->xprt_ctx); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 3308 | if (!crt) |
| 3309 | goto out; |
| 3310 | |
| 3311 | smp_trash = get_trash_chunk(); |
| 3312 | digest = EVP_sha1(); |
| 3313 | X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len); |
| 3314 | |
| 3315 | smp->data.str = *smp_trash; |
| 3316 | smp->type = SMP_T_BIN; |
| 3317 | ret = 1; |
| 3318 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3319 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 3320 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 3321 | X509_free(crt); |
| 3322 | return ret; |
| 3323 | } |
| 3324 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3325 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 3326 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 3327 | * should be use. |
| 3328 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3329 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3330 | 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] | 3331 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3332 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3333 | X509 *crt = NULL; |
| 3334 | int ret = 0; |
| 3335 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3336 | struct connection *conn; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3337 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3338 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3339 | if (!conn || conn->xprt != &ssl_sock) |
| 3340 | return 0; |
| 3341 | |
| 3342 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3343 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3344 | return 0; |
| 3345 | } |
| 3346 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3347 | if (cert_peer) |
| 3348 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 3349 | else |
| 3350 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3351 | if (!crt) |
| 3352 | goto out; |
| 3353 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 3354 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3355 | if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0) |
| 3356 | goto out; |
| 3357 | |
| 3358 | smp->data.str = *smp_trash; |
| 3359 | smp->type = SMP_T_STR; |
| 3360 | ret = 1; |
| 3361 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3362 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 3363 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3364 | X509_free(crt); |
| 3365 | return ret; |
| 3366 | } |
| 3367 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3368 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 3369 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 3370 | * should be use. |
| 3371 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3372 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3373 | 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] | 3374 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3375 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3376 | X509 *crt = NULL; |
| 3377 | X509_NAME *name; |
| 3378 | int ret = 0; |
| 3379 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3380 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3381 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3382 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3383 | if (!conn || conn->xprt != &ssl_sock) |
| 3384 | return 0; |
| 3385 | |
| 3386 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3387 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3388 | return 0; |
| 3389 | } |
| 3390 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3391 | if (cert_peer) |
| 3392 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 3393 | else |
| 3394 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3395 | if (!crt) |
| 3396 | goto out; |
| 3397 | |
| 3398 | name = X509_get_issuer_name(crt); |
| 3399 | if (!name) |
| 3400 | goto out; |
| 3401 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 3402 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3403 | if (args && args[0].type == ARGT_STR) { |
| 3404 | int pos = 1; |
| 3405 | |
| 3406 | if (args[1].type == ARGT_SINT) |
| 3407 | pos = args[1].data.sint; |
| 3408 | else if (args[1].type == ARGT_UINT) |
| 3409 | pos =(int)args[1].data.uint; |
| 3410 | |
| 3411 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 3412 | goto out; |
| 3413 | } |
| 3414 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 3415 | goto out; |
| 3416 | |
| 3417 | smp->type = SMP_T_STR; |
| 3418 | smp->data.str = *smp_trash; |
| 3419 | ret = 1; |
| 3420 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3421 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 3422 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3423 | X509_free(crt); |
| 3424 | return ret; |
| 3425 | } |
| 3426 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3427 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 3428 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 3429 | * should be use. |
| 3430 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3431 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3432 | 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] | 3433 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3434 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3435 | X509 *crt = NULL; |
| 3436 | int ret = 0; |
| 3437 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3438 | struct connection *conn; |
| 3439 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3440 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3441 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3442 | return 0; |
| 3443 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3444 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3445 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3446 | return 0; |
| 3447 | } |
| 3448 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3449 | if (cert_peer) |
| 3450 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 3451 | else |
| 3452 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3453 | if (!crt) |
| 3454 | goto out; |
| 3455 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 3456 | smp_trash = get_trash_chunk(); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3457 | if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0) |
| 3458 | goto out; |
| 3459 | |
| 3460 | smp->data.str = *smp_trash; |
| 3461 | smp->type = SMP_T_STR; |
| 3462 | ret = 1; |
| 3463 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3464 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 3465 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 3466 | X509_free(crt); |
| 3467 | return ret; |
| 3468 | } |
| 3469 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3470 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 3471 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 3472 | * should be use. |
| 3473 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3474 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3475 | 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] | 3476 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3477 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3478 | X509 *crt = NULL; |
| 3479 | X509_NAME *name; |
| 3480 | int ret = 0; |
| 3481 | struct chunk *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3482 | struct connection *conn; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3483 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3484 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3485 | if (!conn || conn->xprt != &ssl_sock) |
| 3486 | return 0; |
| 3487 | |
| 3488 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3489 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3490 | return 0; |
| 3491 | } |
| 3492 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3493 | if (cert_peer) |
| 3494 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 3495 | else |
| 3496 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3497 | if (!crt) |
| 3498 | goto out; |
| 3499 | |
| 3500 | name = X509_get_subject_name(crt); |
| 3501 | if (!name) |
| 3502 | goto out; |
| 3503 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 3504 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3505 | if (args && args[0].type == ARGT_STR) { |
| 3506 | int pos = 1; |
| 3507 | |
| 3508 | if (args[1].type == ARGT_SINT) |
| 3509 | pos = args[1].data.sint; |
| 3510 | else if (args[1].type == ARGT_UINT) |
| 3511 | pos =(int)args[1].data.uint; |
| 3512 | |
| 3513 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 3514 | goto out; |
| 3515 | } |
| 3516 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 3517 | goto out; |
| 3518 | |
| 3519 | smp->type = SMP_T_STR; |
| 3520 | smp->data.str = *smp_trash; |
| 3521 | ret = 1; |
| 3522 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3523 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 3524 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 3525 | X509_free(crt); |
| 3526 | return ret; |
| 3527 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 3528 | |
| 3529 | /* integer, returns true if current session use a client certificate */ |
| 3530 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3531 | 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] | 3532 | { |
| 3533 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3534 | struct connection *conn; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 3535 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3536 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3537 | if (!conn || conn->xprt != &ssl_sock) |
| 3538 | return 0; |
| 3539 | |
| 3540 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 3541 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3542 | return 0; |
| 3543 | } |
| 3544 | |
| 3545 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3546 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 3547 | if (crt) { |
| 3548 | X509_free(crt); |
| 3549 | } |
| 3550 | |
| 3551 | smp->type = SMP_T_BOOL; |
| 3552 | smp->data.uint = (crt != NULL); |
| 3553 | return 1; |
| 3554 | } |
| 3555 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3556 | /* integer, returns the certificate version |
| 3557 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 3558 | * should be use. |
| 3559 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 3560 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3561 | 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] | 3562 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3563 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 3564 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3565 | struct connection *conn; |
| 3566 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3567 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3568 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 3569 | return 0; |
| 3570 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3571 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 3572 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3573 | return 0; |
| 3574 | } |
| 3575 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3576 | if (cert_peer) |
| 3577 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 3578 | else |
| 3579 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 3580 | if (!crt) |
| 3581 | return 0; |
| 3582 | |
| 3583 | smp->data.uint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3584 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 3585 | if (cert_peer) |
| 3586 | X509_free(crt); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 3587 | smp->type = SMP_T_UINT; |
| 3588 | |
| 3589 | return 1; |
| 3590 | } |
| 3591 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3592 | /* string, returns the certificate's signature algorithm. |
| 3593 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 3594 | * should be use. |
| 3595 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 3596 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3597 | 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] | 3598 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3599 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 3600 | X509 *crt; |
| 3601 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3602 | struct connection *conn; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 3603 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3604 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3605 | if (!conn || conn->xprt != &ssl_sock) |
| 3606 | return 0; |
| 3607 | |
| 3608 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 3609 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3610 | return 0; |
| 3611 | } |
| 3612 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3613 | if (cert_peer) |
| 3614 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 3615 | else |
| 3616 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 3617 | if (!crt) |
| 3618 | return 0; |
| 3619 | |
| 3620 | nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->signature->algorithm)); |
| 3621 | |
| 3622 | smp->data.str.str = (char *)OBJ_nid2sn(nid); |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 3623 | if (!smp->data.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3624 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 3625 | if (cert_peer) |
| 3626 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 3627 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 3628 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 3629 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3630 | smp->type = SMP_T_STR; |
| 3631 | smp->flags |= SMP_F_CONST; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 3632 | smp->data.str.len = strlen(smp->data.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3633 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 3634 | if (cert_peer) |
| 3635 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 3636 | |
| 3637 | return 1; |
| 3638 | } |
| 3639 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3640 | /* string, returns the certificate's key algorithm. |
| 3641 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 3642 | * should be use. |
| 3643 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 3644 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3645 | 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] | 3646 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3647 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 3648 | X509 *crt; |
| 3649 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3650 | struct connection *conn; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 3651 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3652 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3653 | if (!conn || conn->xprt != &ssl_sock) |
| 3654 | return 0; |
| 3655 | |
| 3656 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 3657 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3658 | return 0; |
| 3659 | } |
| 3660 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3661 | if (cert_peer) |
| 3662 | crt = SSL_get_peer_certificate(conn->xprt_ctx); |
| 3663 | else |
| 3664 | crt = SSL_get_certificate(conn->xprt_ctx); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 3665 | if (!crt) |
| 3666 | return 0; |
| 3667 | |
| 3668 | nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->key->algor->algorithm)); |
| 3669 | |
| 3670 | smp->data.str.str = (char *)OBJ_nid2sn(nid); |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 3671 | if (!smp->data.str.str) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3672 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 3673 | if (cert_peer) |
| 3674 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 3675 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 3676 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 3677 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3678 | smp->type = SMP_T_STR; |
| 3679 | smp->flags |= SMP_F_CONST; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 3680 | smp->data.str.len = strlen(smp->data.str.str); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 3681 | if (cert_peer) |
| 3682 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 3683 | |
| 3684 | return 1; |
| 3685 | } |
| 3686 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3687 | /* boolean, returns true if front conn. transport layer is SSL. |
| 3688 | * This function is also usable on backend conn if the fetch keyword 5th |
| 3689 | * char is 'b'. |
| 3690 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3691 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3692 | 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] | 3693 | { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3694 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3695 | struct connection *conn = objt_conn(smp->strm->si[back_conn].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3696 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3697 | smp->type = SMP_T_BOOL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3698 | smp->data.uint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3699 | return 1; |
| 3700 | } |
| 3701 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 3702 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3703 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3704 | 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] | 3705 | { |
| 3706 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3707 | struct connection *conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3708 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3709 | smp->type = SMP_T_BOOL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3710 | smp->data.uint = (conn && conn->xprt == &ssl_sock) && |
| 3711 | conn->xprt_ctx && |
| 3712 | SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3713 | return 1; |
| 3714 | #else |
| 3715 | return 0; |
| 3716 | #endif |
| 3717 | } |
| 3718 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 3719 | /* boolean, returns true if client session has been resumed */ |
| 3720 | static int |
| 3721 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3722 | { |
| 3723 | struct connection *conn = objt_conn(smp->sess->origin); |
| 3724 | |
| 3725 | smp->type = SMP_T_BOOL; |
| 3726 | smp->data.uint = (conn && conn->xprt == &ssl_sock) && |
| 3727 | conn->xprt_ctx && |
| 3728 | SSL_session_reused(conn->xprt_ctx); |
| 3729 | return 1; |
| 3730 | } |
| 3731 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3732 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 3733 | * This function is also usable on backend conn if the fetch keyword 5th |
| 3734 | * char is 'b'. |
| 3735 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3736 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3737 | 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] | 3738 | { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3739 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3740 | struct connection *conn; |
| 3741 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3742 | smp->flags = 0; |
| 3743 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3744 | conn = objt_conn(smp->strm->si[back_conn].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3745 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3746 | return 0; |
| 3747 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3748 | smp->data.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3749 | if (!smp->data.str.str) |
| 3750 | return 0; |
| 3751 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3752 | smp->type = SMP_T_STR; |
| 3753 | smp->flags |= SMP_F_CONST; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3754 | smp->data.str.len = strlen(smp->data.str.str); |
| 3755 | |
| 3756 | return 1; |
| 3757 | } |
| 3758 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3759 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 3760 | * is SSL. |
| 3761 | * This function is also usable on backend conn if the fetch keyword 5th |
| 3762 | * char is 'b'. |
| 3763 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3764 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3765 | 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] | 3766 | { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3767 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3768 | struct connection *conn; |
| 3769 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3770 | smp->flags = 0; |
| 3771 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3772 | conn = objt_conn(smp->strm->si[back_conn].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3773 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3774 | return 0; |
| 3775 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3776 | if (!SSL_get_cipher_bits(conn->xprt_ctx, (int *)&smp->data.uint)) |
| 3777 | return 0; |
| 3778 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3779 | smp->type = SMP_T_UINT; |
| 3780 | |
| 3781 | return 1; |
| 3782 | } |
| 3783 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3784 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 3785 | * This function is also usable on backend conn if the fetch keyword 5th |
| 3786 | * char is 'b'. |
| 3787 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3788 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3789 | 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] | 3790 | { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3791 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3792 | struct connection *conn; |
| 3793 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3794 | smp->flags = 0; |
| 3795 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3796 | conn = objt_conn(smp->strm->si[back_conn].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3797 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 3798 | return 0; |
| 3799 | |
| 3800 | smp->data.uint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3801 | if (!smp->data.uint) |
| 3802 | return 0; |
| 3803 | |
| 3804 | smp->type = SMP_T_UINT; |
| 3805 | |
| 3806 | return 1; |
| 3807 | } |
| 3808 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3809 | #ifdef OPENSSL_NPN_NEGOTIATED |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3810 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3811 | 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] | 3812 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3813 | struct connection *conn; |
| 3814 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3815 | smp->flags = SMP_F_CONST; |
| 3816 | smp->type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 3817 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3818 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3819 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 3820 | return 0; |
| 3821 | |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 3822 | smp->data.str.str = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3823 | SSL_get0_next_proto_negotiated(conn->xprt_ctx, |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 3824 | (const unsigned char **)&smp->data.str.str, (unsigned *)&smp->data.str.len); |
| 3825 | |
| 3826 | if (!smp->data.str.str) |
| 3827 | return 0; |
| 3828 | |
| 3829 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 3830 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 3831 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 3832 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 3833 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 3834 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3835 | 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] | 3836 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3837 | struct connection *conn; |
| 3838 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3839 | smp->flags = SMP_F_CONST; |
| 3840 | smp->type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 3841 | |
Willy Tarreau | e26bf05 | 2015-05-12 10:30:12 +0200 | [diff] [blame] | 3842 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3843 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 3844 | return 0; |
| 3845 | |
| 3846 | smp->data.str.str = NULL; |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 3847 | SSL_get0_alpn_selected(conn->xprt_ctx, |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 3848 | (const unsigned char **)&smp->data.str.str, (unsigned *)&smp->data.str.len); |
| 3849 | |
| 3850 | if (!smp->data.str.str) |
| 3851 | return 0; |
| 3852 | |
| 3853 | return 1; |
| 3854 | } |
| 3855 | #endif |
| 3856 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3857 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 3858 | * This function is also usable on backend conn if the fetch keyword 5th |
| 3859 | * char is 'b'. |
| 3860 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 3861 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3862 | 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] | 3863 | { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3864 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3865 | struct connection *conn; |
| 3866 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3867 | smp->flags = 0; |
| 3868 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3869 | conn = objt_conn(smp->strm->si[back_conn].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3870 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 3871 | return 0; |
| 3872 | |
| 3873 | smp->data.str.str = (char *)SSL_get_version(conn->xprt_ctx); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3874 | if (!smp->data.str.str) |
| 3875 | return 0; |
| 3876 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3877 | smp->type = SMP_T_STR; |
| 3878 | smp->flags = SMP_F_CONST; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3879 | smp->data.str.len = strlen(smp->data.str.str); |
| 3880 | |
| 3881 | return 1; |
| 3882 | } |
| 3883 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3884 | /* 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] | 3885 | * This function is also usable on backend conn if the fetch keyword 5th |
| 3886 | * char is 'b'. |
| 3887 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 3888 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3889 | 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] | 3890 | { |
| 3891 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3892 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3893 | SSL_SESSION *ssl_sess; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3894 | struct connection *conn; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 3895 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3896 | smp->flags = SMP_F_CONST; |
| 3897 | smp->type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 3898 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3899 | conn = objt_conn(smp->strm->si[back_conn].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3900 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 3901 | return 0; |
| 3902 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3903 | ssl_sess = SSL_get_session(conn->xprt_ctx); |
| 3904 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 3905 | return 0; |
| 3906 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3907 | smp->data.str.str = (char *)SSL_SESSION_get_id(ssl_sess, (unsigned int *)&smp->data.str.len); |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 3908 | if (!smp->data.str.str || !&smp->data.str.len) |
| 3909 | return 0; |
| 3910 | |
| 3911 | return 1; |
| 3912 | #else |
| 3913 | return 0; |
| 3914 | #endif |
| 3915 | } |
| 3916 | |
| 3917 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3918 | 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] | 3919 | { |
| 3920 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3921 | struct connection *conn; |
| 3922 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3923 | smp->flags = SMP_F_CONST; |
| 3924 | smp->type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3925 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3926 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3927 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 3928 | return 0; |
| 3929 | |
| 3930 | smp->data.str.str = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 3931 | if (!smp->data.str.str) |
| 3932 | return 0; |
| 3933 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 3934 | smp->data.str.len = strlen(smp->data.str.str); |
| 3935 | return 1; |
| 3936 | #else |
| 3937 | return 0; |
| 3938 | #endif |
| 3939 | } |
| 3940 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 3941 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3942 | 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] | 3943 | { |
| 3944 | #if OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 3945 | int back_conn = (kw[4] == 'b') ? 1 : 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 3946 | struct connection *conn; |
| 3947 | int finished_len; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 3948 | struct chunk *finished_trash; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 3949 | |
| 3950 | smp->flags = 0; |
| 3951 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3952 | conn = objt_conn(smp->strm->si[back_conn].end); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 3953 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 3954 | return 0; |
| 3955 | |
| 3956 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 3957 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3958 | return 0; |
| 3959 | } |
| 3960 | |
| 3961 | finished_trash = get_trash_chunk(); |
| 3962 | if (!SSL_session_reused(conn->xprt_ctx)) |
| 3963 | finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 3964 | else |
| 3965 | finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size); |
| 3966 | |
| 3967 | if (!finished_len) |
| 3968 | return 0; |
| 3969 | |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 3970 | finished_trash->len = finished_len; |
| 3971 | smp->data.str = *finished_trash; |
| 3972 | smp->type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 3973 | |
| 3974 | return 1; |
| 3975 | #else |
| 3976 | return 0; |
| 3977 | #endif |
| 3978 | } |
| 3979 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 3980 | /* 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] | 3981 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3982 | 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] | 3983 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3984 | struct connection *conn; |
| 3985 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3986 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3987 | if (!conn || conn->xprt != &ssl_sock) |
| 3988 | return 0; |
| 3989 | |
| 3990 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 3991 | smp->flags = SMP_F_MAY_CHANGE; |
| 3992 | return 0; |
| 3993 | } |
| 3994 | |
| 3995 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3996 | smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 3997 | smp->flags = 0; |
| 3998 | |
| 3999 | return 1; |
| 4000 | } |
| 4001 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 4002 | /* 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] | 4003 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4004 | 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] | 4005 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4006 | struct connection *conn; |
| 4007 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4008 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4009 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 4010 | return 0; |
| 4011 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4012 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 4013 | smp->flags = SMP_F_MAY_CHANGE; |
| 4014 | return 0; |
| 4015 | } |
| 4016 | |
| 4017 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4018 | smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CAEDEPTH(conn->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 4019 | smp->flags = 0; |
| 4020 | |
| 4021 | return 1; |
| 4022 | } |
| 4023 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 4024 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 4025 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4026 | 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] | 4027 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4028 | struct connection *conn; |
| 4029 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4030 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4031 | if (!conn || conn->xprt != &ssl_sock) |
| 4032 | return 0; |
| 4033 | |
| 4034 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 4035 | smp->flags = SMP_F_MAY_CHANGE; |
| 4036 | return 0; |
| 4037 | } |
| 4038 | |
| 4039 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4040 | smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 4041 | smp->flags = 0; |
| 4042 | |
| 4043 | return 1; |
| 4044 | } |
| 4045 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 4046 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 4047 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4048 | 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] | 4049 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4050 | struct connection *conn; |
| 4051 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4052 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4053 | if (!conn || conn->xprt != &ssl_sock) |
| 4054 | return 0; |
| 4055 | |
| 4056 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 4057 | smp->flags = SMP_F_MAY_CHANGE; |
| 4058 | return 0; |
| 4059 | } |
| 4060 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4061 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 4062 | return 0; |
| 4063 | |
| 4064 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4065 | smp->data.uint = (unsigned int)SSL_get_verify_result(conn->xprt_ctx); |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 4066 | smp->flags = 0; |
| 4067 | |
| 4068 | return 1; |
| 4069 | } |
| 4070 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 4071 | /* parse the "ca-file" bind keyword */ |
| 4072 | 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] | 4073 | { |
| 4074 | if (!*args[cur_arg + 1]) { |
| 4075 | if (err) |
| 4076 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 4077 | return ERR_ALERT | ERR_FATAL; |
| 4078 | } |
| 4079 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4080 | if ((*args[cur_arg + 1] != '/') && global.ca_base) |
| 4081 | memprintf(&conf->ca_file, "%s/%s", global.ca_base, args[cur_arg + 1]); |
| 4082 | else |
| 4083 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 4084 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4085 | return 0; |
| 4086 | } |
| 4087 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4088 | /* parse the "ciphers" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 4089 | 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] | 4090 | { |
| 4091 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 4092 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4093 | return ERR_ALERT | ERR_FATAL; |
| 4094 | } |
| 4095 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 4096 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 4097 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4098 | return 0; |
| 4099 | } |
| 4100 | |
| 4101 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 4102 | 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] | 4103 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 4104 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 4105 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4106 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 4107 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4108 | return ERR_ALERT | ERR_FATAL; |
| 4109 | } |
| 4110 | |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 4111 | if ((*args[cur_arg + 1] != '/' ) && global.crt_base) { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 4112 | 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] | 4113 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 4114 | return ERR_ALERT | ERR_FATAL; |
| 4115 | } |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 4116 | 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] | 4117 | if (ssl_sock_load_cert(path, conf, px, err) > 0) |
| 4118 | return ERR_ALERT | ERR_FATAL; |
| 4119 | |
| 4120 | return 0; |
| 4121 | } |
| 4122 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 4123 | 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] | 4124 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4125 | |
| 4126 | return 0; |
| 4127 | } |
| 4128 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4129 | /* parse the "crt-list" bind keyword */ |
| 4130 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4131 | { |
| 4132 | if (!*args[cur_arg + 1]) { |
| 4133 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 4134 | return ERR_ALERT | ERR_FATAL; |
| 4135 | } |
| 4136 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4137 | if (ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err) > 0) { |
| 4138 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4139 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4140 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4141 | |
| 4142 | return 0; |
| 4143 | } |
| 4144 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 4145 | /* parse the "crl-file" bind keyword */ |
| 4146 | 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] | 4147 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4148 | #ifndef X509_V_FLAG_CRL_CHECK |
| 4149 | if (err) |
| 4150 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 4151 | return ERR_ALERT | ERR_FATAL; |
| 4152 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4153 | if (!*args[cur_arg + 1]) { |
| 4154 | if (err) |
| 4155 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 4156 | return ERR_ALERT | ERR_FATAL; |
| 4157 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4158 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4159 | if ((*args[cur_arg + 1] != '/') && global.ca_base) |
| 4160 | memprintf(&conf->crl_file, "%s/%s", global.ca_base, args[cur_arg + 1]); |
| 4161 | else |
| 4162 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 4163 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4164 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4165 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4166 | } |
| 4167 | |
| 4168 | /* parse the "ecdhe" bind keyword keywords */ |
| 4169 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4170 | { |
| 4171 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 4172 | if (err) |
| 4173 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 4174 | return ERR_ALERT | ERR_FATAL; |
| 4175 | #elif defined(OPENSSL_NO_ECDH) |
| 4176 | if (err) |
| 4177 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 4178 | return ERR_ALERT | ERR_FATAL; |
| 4179 | #else |
| 4180 | if (!*args[cur_arg + 1]) { |
| 4181 | if (err) |
| 4182 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 4183 | return ERR_ALERT | ERR_FATAL; |
| 4184 | } |
| 4185 | |
| 4186 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4187 | |
| 4188 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4189 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4190 | } |
| 4191 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 4192 | /* parse the "crt_ignerr" and "ca_ignerr" bind keywords */ |
| 4193 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4194 | { |
| 4195 | int code; |
| 4196 | char *p = args[cur_arg + 1]; |
| 4197 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 4198 | |
| 4199 | if (!*p) { |
| 4200 | if (err) |
| 4201 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 4202 | return ERR_ALERT | ERR_FATAL; |
| 4203 | } |
| 4204 | |
| 4205 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 4206 | ignerr = &conf->ca_ignerr; |
| 4207 | |
| 4208 | if (strcmp(p, "all") == 0) { |
| 4209 | *ignerr = ~0ULL; |
| 4210 | return 0; |
| 4211 | } |
| 4212 | |
| 4213 | while (p) { |
| 4214 | code = atoi(p); |
| 4215 | if ((code <= 0) || (code > 63)) { |
| 4216 | if (err) |
| 4217 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 4218 | args[cur_arg], code, args[cur_arg + 1]); |
| 4219 | return ERR_ALERT | ERR_FATAL; |
| 4220 | } |
| 4221 | *ignerr |= 1ULL << code; |
| 4222 | p = strchr(p, ','); |
| 4223 | if (p) |
| 4224 | p++; |
| 4225 | } |
| 4226 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 4227 | return 0; |
| 4228 | } |
| 4229 | |
| 4230 | /* parse the "force-sslv3" bind keyword */ |
| 4231 | static int bind_parse_force_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4232 | { |
| 4233 | conf->ssl_options |= BC_SSL_O_USE_SSLV3; |
| 4234 | return 0; |
| 4235 | } |
| 4236 | |
| 4237 | /* parse the "force-tlsv10" bind keyword */ |
| 4238 | static int bind_parse_force_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4239 | { |
| 4240 | conf->ssl_options |= BC_SSL_O_USE_TLSV10; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 4241 | return 0; |
| 4242 | } |
| 4243 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 4244 | /* parse the "force-tlsv11" bind keyword */ |
| 4245 | static int bind_parse_force_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4246 | { |
| 4247 | #if SSL_OP_NO_TLSv1_1 |
| 4248 | conf->ssl_options |= BC_SSL_O_USE_TLSV11; |
| 4249 | return 0; |
| 4250 | #else |
| 4251 | if (err) |
| 4252 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[cur_arg]); |
| 4253 | return ERR_ALERT | ERR_FATAL; |
| 4254 | #endif |
| 4255 | } |
| 4256 | |
| 4257 | /* parse the "force-tlsv12" bind keyword */ |
| 4258 | static int bind_parse_force_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4259 | { |
| 4260 | #if SSL_OP_NO_TLSv1_2 |
| 4261 | conf->ssl_options |= BC_SSL_O_USE_TLSV12; |
| 4262 | return 0; |
| 4263 | #else |
| 4264 | if (err) |
| 4265 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[cur_arg]); |
| 4266 | return ERR_ALERT | ERR_FATAL; |
| 4267 | #endif |
| 4268 | } |
| 4269 | |
| 4270 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 4271 | /* parse the "no-tls-tickets" bind keyword */ |
| 4272 | static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4273 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 4274 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 4275 | return 0; |
| 4276 | } |
| 4277 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 4278 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 4279 | /* parse the "no-sslv3" bind keyword */ |
| 4280 | 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] | 4281 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 4282 | conf->ssl_options |= BC_SSL_O_NO_SSLV3; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4283 | return 0; |
| 4284 | } |
| 4285 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 4286 | /* parse the "no-tlsv10" bind keyword */ |
| 4287 | 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] | 4288 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 4289 | conf->ssl_options |= BC_SSL_O_NO_TLSV10; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 4290 | return 0; |
| 4291 | } |
| 4292 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 4293 | /* parse the "no-tlsv11" bind keyword */ |
| 4294 | 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] | 4295 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 4296 | conf->ssl_options |= BC_SSL_O_NO_TLSV11; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 4297 | return 0; |
| 4298 | } |
| 4299 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 4300 | /* parse the "no-tlsv12" bind keyword */ |
| 4301 | 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] | 4302 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 4303 | conf->ssl_options |= BC_SSL_O_NO_TLSV12; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4304 | return 0; |
| 4305 | } |
| 4306 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 4307 | /* parse the "npn" bind keyword */ |
| 4308 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4309 | { |
| 4310 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 4311 | char *p1, *p2; |
| 4312 | |
| 4313 | if (!*args[cur_arg + 1]) { |
| 4314 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 4315 | return ERR_ALERT | ERR_FATAL; |
| 4316 | } |
| 4317 | |
| 4318 | free(conf->npn_str); |
| 4319 | |
| 4320 | /* the NPN string is built as a suite of (<len> <name>)* */ |
| 4321 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
| 4322 | conf->npn_str = calloc(1, conf->npn_len); |
| 4323 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 4324 | |
| 4325 | /* replace commas with the name length */ |
| 4326 | p1 = conf->npn_str; |
| 4327 | p2 = p1 + 1; |
| 4328 | while (1) { |
| 4329 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 4330 | if (!p2) |
| 4331 | p2 = p1 + 1 + strlen(p1 + 1); |
| 4332 | |
| 4333 | if (p2 - (p1 + 1) > 255) { |
| 4334 | *p2 = '\0'; |
| 4335 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 4336 | return ERR_ALERT | ERR_FATAL; |
| 4337 | } |
| 4338 | |
| 4339 | *p1 = p2 - (p1 + 1); |
| 4340 | p1 = p2; |
| 4341 | |
| 4342 | if (!*p2) |
| 4343 | break; |
| 4344 | |
| 4345 | *(p2++) = '\0'; |
| 4346 | } |
| 4347 | return 0; |
| 4348 | #else |
| 4349 | if (err) |
| 4350 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 4351 | return ERR_ALERT | ERR_FATAL; |
| 4352 | #endif |
| 4353 | } |
| 4354 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 4355 | /* parse the "alpn" bind keyword */ |
| 4356 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4357 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4358 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 4359 | char *p1, *p2; |
| 4360 | |
| 4361 | if (!*args[cur_arg + 1]) { |
| 4362 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 4363 | return ERR_ALERT | ERR_FATAL; |
| 4364 | } |
| 4365 | |
| 4366 | free(conf->alpn_str); |
| 4367 | |
| 4368 | /* the ALPN string is built as a suite of (<len> <name>)* */ |
| 4369 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
| 4370 | conf->alpn_str = calloc(1, conf->alpn_len); |
| 4371 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 4372 | |
| 4373 | /* replace commas with the name length */ |
| 4374 | p1 = conf->alpn_str; |
| 4375 | p2 = p1 + 1; |
| 4376 | while (1) { |
| 4377 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 4378 | if (!p2) |
| 4379 | p2 = p1 + 1 + strlen(p1 + 1); |
| 4380 | |
| 4381 | if (p2 - (p1 + 1) > 255) { |
| 4382 | *p2 = '\0'; |
| 4383 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 4384 | return ERR_ALERT | ERR_FATAL; |
| 4385 | } |
| 4386 | |
| 4387 | *p1 = p2 - (p1 + 1); |
| 4388 | p1 = p2; |
| 4389 | |
| 4390 | if (!*p2) |
| 4391 | break; |
| 4392 | |
| 4393 | *(p2++) = '\0'; |
| 4394 | } |
| 4395 | return 0; |
| 4396 | #else |
| 4397 | if (err) |
| 4398 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 4399 | return ERR_ALERT | ERR_FATAL; |
| 4400 | #endif |
| 4401 | } |
| 4402 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4403 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 4404 | 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] | 4405 | { |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 4406 | struct listener *l; |
| 4407 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 4408 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 4409 | |
| 4410 | if (global.listen_default_ciphers && !conf->ciphers) |
| 4411 | conf->ciphers = strdup(global.listen_default_ciphers); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 4412 | conf->ssl_options |= global.listen_default_ssloptions; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 4413 | |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 4414 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4415 | l->xprt = &ssl_sock; |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 4416 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4417 | return 0; |
| 4418 | } |
| 4419 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 4420 | /* parse the "strict-sni" bind keyword */ |
| 4421 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4422 | { |
| 4423 | conf->strict_sni = 1; |
| 4424 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | /* parse the "tls-ticket-keys" bind keyword */ |
| 4428 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4429 | { |
| 4430 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 4431 | FILE *f; |
| 4432 | int i = 0; |
| 4433 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 4434 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4435 | |
| 4436 | if (!*args[cur_arg + 1]) { |
| 4437 | if (err) |
| 4438 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 4439 | return ERR_ALERT | ERR_FATAL; |
| 4440 | } |
| 4441 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 4442 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
| 4443 | if(keys_ref) { |
| 4444 | conf->keys_ref = keys_ref; |
| 4445 | return 0; |
| 4446 | } |
| 4447 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 4448 | keys_ref = malloc(sizeof(struct tls_keys_ref)); |
| 4449 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct tls_sess_key)); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4450 | |
| 4451 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 4452 | if (err) |
| 4453 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 4454 | return ERR_ALERT | ERR_FATAL; |
| 4455 | } |
| 4456 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 4457 | keys_ref->filename = strdup(args[cur_arg + 1]); |
| 4458 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4459 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 4460 | int len = strlen(thisline); |
| 4461 | /* Strip newline characters from the end */ |
| 4462 | if(thisline[len - 1] == '\n') |
| 4463 | thisline[--len] = 0; |
| 4464 | |
| 4465 | if(thisline[len - 1] == '\r') |
| 4466 | thisline[--len] = 0; |
| 4467 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 4468 | 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] | 4469 | if (err) |
| 4470 | memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1); |
| 4471 | return ERR_ALERT | ERR_FATAL; |
| 4472 | } |
| 4473 | i++; |
| 4474 | } |
| 4475 | |
| 4476 | if (i < TLS_TICKETS_NO) { |
| 4477 | if (err) |
| 4478 | memprintf(err, "'%s' : please supply at least %d keys in the tls-tickets-file", args[cur_arg+1], TLS_TICKETS_NO); |
| 4479 | return ERR_ALERT | ERR_FATAL; |
| 4480 | } |
| 4481 | |
| 4482 | fclose(f); |
| 4483 | |
| 4484 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
| 4485 | i-=2; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 4486 | keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 4487 | keys_ref->unique_id = -1; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 4488 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4489 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 4490 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 4491 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4492 | return 0; |
| 4493 | #else |
| 4494 | if (err) |
| 4495 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 4496 | return ERR_ALERT | ERR_FATAL; |
| 4497 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 4498 | } |
| 4499 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4500 | /* parse the "verify" bind keyword */ |
| 4501 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 4502 | { |
| 4503 | if (!*args[cur_arg + 1]) { |
| 4504 | if (err) |
| 4505 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 4506 | return ERR_ALERT | ERR_FATAL; |
| 4507 | } |
| 4508 | |
| 4509 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4510 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4511 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4512 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4513 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4514 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4515 | else { |
| 4516 | if (err) |
| 4517 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 4518 | args[cur_arg], args[cur_arg + 1]); |
| 4519 | return ERR_ALERT | ERR_FATAL; |
| 4520 | } |
| 4521 | |
| 4522 | return 0; |
| 4523 | } |
| 4524 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 4525 | /************** "server" keywords ****************/ |
| 4526 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4527 | /* parse the "ca-file" server keyword */ |
| 4528 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4529 | { |
| 4530 | if (!*args[*cur_arg + 1]) { |
| 4531 | if (err) |
| 4532 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 4533 | return ERR_ALERT | ERR_FATAL; |
| 4534 | } |
| 4535 | |
| 4536 | if ((*args[*cur_arg + 1] != '/') && global.ca_base) |
| 4537 | memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global.ca_base, args[*cur_arg + 1]); |
| 4538 | else |
| 4539 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 4540 | |
| 4541 | return 0; |
| 4542 | } |
| 4543 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 4544 | /* parse the "check-ssl" server keyword */ |
| 4545 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4546 | { |
| 4547 | newsrv->check.use_ssl = 1; |
| 4548 | if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 4549 | newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 4550 | newsrv->ssl_ctx.options |= global.connect_default_ssloptions; |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 4551 | return 0; |
| 4552 | } |
| 4553 | |
| 4554 | /* parse the "ciphers" server keyword */ |
| 4555 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4556 | { |
| 4557 | if (!*args[*cur_arg + 1]) { |
| 4558 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 4559 | return ERR_ALERT | ERR_FATAL; |
| 4560 | } |
| 4561 | |
| 4562 | free(newsrv->ssl_ctx.ciphers); |
| 4563 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 4564 | return 0; |
| 4565 | } |
| 4566 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4567 | /* parse the "crl-file" server keyword */ |
| 4568 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4569 | { |
| 4570 | #ifndef X509_V_FLAG_CRL_CHECK |
| 4571 | if (err) |
| 4572 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 4573 | return ERR_ALERT | ERR_FATAL; |
| 4574 | #else |
| 4575 | if (!*args[*cur_arg + 1]) { |
| 4576 | if (err) |
| 4577 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 4578 | return ERR_ALERT | ERR_FATAL; |
| 4579 | } |
| 4580 | |
| 4581 | if ((*args[*cur_arg + 1] != '/') && global.ca_base) |
| 4582 | memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global.ca_base, args[*cur_arg + 1]); |
| 4583 | else |
| 4584 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 4585 | |
| 4586 | return 0; |
| 4587 | #endif |
| 4588 | } |
| 4589 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4590 | /* parse the "crt" server keyword */ |
| 4591 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4592 | { |
| 4593 | if (!*args[*cur_arg + 1]) { |
| 4594 | if (err) |
| 4595 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 4596 | return ERR_ALERT | ERR_FATAL; |
| 4597 | } |
| 4598 | |
| 4599 | if ((*args[*cur_arg + 1] != '/') && global.crt_base) |
| 4600 | memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global.ca_base, args[*cur_arg + 1]); |
| 4601 | else |
| 4602 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 4603 | |
| 4604 | return 0; |
| 4605 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4606 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 4607 | /* parse the "force-sslv3" server keyword */ |
| 4608 | static int srv_parse_force_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4609 | { |
| 4610 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_SSLV3; |
| 4611 | return 0; |
| 4612 | } |
| 4613 | |
| 4614 | /* parse the "force-tlsv10" server keyword */ |
| 4615 | static int srv_parse_force_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4616 | { |
| 4617 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV10; |
| 4618 | return 0; |
| 4619 | } |
| 4620 | |
| 4621 | /* parse the "force-tlsv11" server keyword */ |
| 4622 | static int srv_parse_force_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4623 | { |
| 4624 | #if SSL_OP_NO_TLSv1_1 |
| 4625 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV11; |
| 4626 | return 0; |
| 4627 | #else |
| 4628 | if (err) |
| 4629 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[*cur_arg]); |
| 4630 | return ERR_ALERT | ERR_FATAL; |
| 4631 | #endif |
| 4632 | } |
| 4633 | |
| 4634 | /* parse the "force-tlsv12" server keyword */ |
| 4635 | static int srv_parse_force_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4636 | { |
| 4637 | #if SSL_OP_NO_TLSv1_2 |
| 4638 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV12; |
| 4639 | return 0; |
| 4640 | #else |
| 4641 | if (err) |
| 4642 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[*cur_arg]); |
| 4643 | return ERR_ALERT | ERR_FATAL; |
| 4644 | #endif |
| 4645 | } |
| 4646 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 4647 | /* parse the "no-ssl-reuse" server keyword */ |
| 4648 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4649 | { |
| 4650 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 4651 | return 0; |
| 4652 | } |
| 4653 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 4654 | /* parse the "no-sslv3" server keyword */ |
| 4655 | static int srv_parse_no_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4656 | { |
| 4657 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_SSLV3; |
| 4658 | return 0; |
| 4659 | } |
| 4660 | |
| 4661 | /* parse the "no-tlsv10" server keyword */ |
| 4662 | static int srv_parse_no_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4663 | { |
| 4664 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV10; |
| 4665 | return 0; |
| 4666 | } |
| 4667 | |
| 4668 | /* parse the "no-tlsv11" server keyword */ |
| 4669 | static int srv_parse_no_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4670 | { |
| 4671 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV11; |
| 4672 | return 0; |
| 4673 | } |
| 4674 | |
| 4675 | /* parse the "no-tlsv12" server keyword */ |
| 4676 | static int srv_parse_no_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4677 | { |
| 4678 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV12; |
| 4679 | return 0; |
| 4680 | } |
| 4681 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 4682 | /* parse the "no-tls-tickets" server keyword */ |
| 4683 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4684 | { |
| 4685 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 4686 | return 0; |
| 4687 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 4688 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 4689 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4690 | { |
| 4691 | newsrv->pp_opts |= SRV_PP_V2; |
| 4692 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 4693 | return 0; |
| 4694 | } |
| 4695 | |
| 4696 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 4697 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4698 | { |
| 4699 | newsrv->pp_opts |= SRV_PP_V2; |
| 4700 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 4701 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 4702 | return 0; |
| 4703 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 4704 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 4705 | /* parse the "ssl" server keyword */ |
| 4706 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4707 | { |
| 4708 | newsrv->use_ssl = 1; |
| 4709 | if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 4710 | newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers); |
| 4711 | return 0; |
| 4712 | } |
| 4713 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4714 | /* parse the "verify" server keyword */ |
| 4715 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4716 | { |
| 4717 | if (!*args[*cur_arg + 1]) { |
| 4718 | if (err) |
| 4719 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 4720 | return ERR_ALERT | ERR_FATAL; |
| 4721 | } |
| 4722 | |
| 4723 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4724 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4725 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4726 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4727 | else { |
| 4728 | if (err) |
| 4729 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 4730 | args[*cur_arg], args[*cur_arg + 1]); |
| 4731 | return ERR_ALERT | ERR_FATAL; |
| 4732 | } |
| 4733 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4734 | return 0; |
| 4735 | } |
| 4736 | |
| 4737 | /* parse the "verifyhost" server keyword */ |
| 4738 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 4739 | { |
| 4740 | if (!*args[*cur_arg + 1]) { |
| 4741 | if (err) |
| 4742 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 4743 | return ERR_ALERT | ERR_FATAL; |
| 4744 | } |
| 4745 | |
| 4746 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 4747 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4748 | return 0; |
| 4749 | } |
| 4750 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 4751 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 4752 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 4753 | struct proxy *defpx, const char *file, int line, |
| 4754 | char **err) { |
| 4755 | int i = 1; |
| 4756 | |
| 4757 | if (*(args[i]) == 0) { |
| 4758 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 4759 | return -1; |
| 4760 | } |
| 4761 | while (*(args[i])) { |
| 4762 | if (!strcmp(args[i], "no-sslv3")) |
| 4763 | global.listen_default_ssloptions |= BC_SSL_O_NO_SSLV3; |
| 4764 | else if (!strcmp(args[i], "no-tlsv10")) |
| 4765 | global.listen_default_ssloptions |= BC_SSL_O_NO_TLSV10; |
| 4766 | else if (!strcmp(args[i], "no-tlsv11")) |
| 4767 | global.listen_default_ssloptions |= BC_SSL_O_NO_TLSV11; |
| 4768 | else if (!strcmp(args[i], "no-tlsv12")) |
| 4769 | global.listen_default_ssloptions |= BC_SSL_O_NO_TLSV12; |
| 4770 | else if (!strcmp(args[i], "force-sslv3")) |
| 4771 | global.listen_default_ssloptions |= BC_SSL_O_USE_SSLV3; |
| 4772 | else if (!strcmp(args[i], "force-tlsv10")) |
| 4773 | global.listen_default_ssloptions |= BC_SSL_O_USE_TLSV10; |
| 4774 | else if (!strcmp(args[i], "force-tlsv11")) { |
| 4775 | #if SSL_OP_NO_TLSv1_1 |
| 4776 | global.listen_default_ssloptions |= BC_SSL_O_USE_TLSV11; |
| 4777 | #else |
| 4778 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.1", args[0], args[i]); |
| 4779 | return -1; |
| 4780 | #endif |
| 4781 | } |
| 4782 | else if (!strcmp(args[i], "force-tlsv12")) { |
| 4783 | #if SSL_OP_NO_TLSv1_2 |
| 4784 | global.listen_default_ssloptions |= BC_SSL_O_USE_TLSV12; |
| 4785 | #else |
| 4786 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.2", args[0], args[i]); |
| 4787 | return -1; |
| 4788 | #endif |
| 4789 | } |
| 4790 | else if (!strcmp(args[i], "no-tls-tickets")) |
| 4791 | global.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
| 4792 | else { |
| 4793 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 4794 | return -1; |
| 4795 | } |
| 4796 | i++; |
| 4797 | } |
| 4798 | return 0; |
| 4799 | } |
| 4800 | |
| 4801 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 4802 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 4803 | struct proxy *defpx, const char *file, int line, |
| 4804 | char **err) { |
| 4805 | int i = 1; |
| 4806 | |
| 4807 | if (*(args[i]) == 0) { |
| 4808 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 4809 | return -1; |
| 4810 | } |
| 4811 | while (*(args[i])) { |
| 4812 | if (!strcmp(args[i], "no-sslv3")) |
| 4813 | global.connect_default_ssloptions |= SRV_SSL_O_NO_SSLV3; |
| 4814 | else if (!strcmp(args[i], "no-tlsv10")) |
| 4815 | global.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV10; |
| 4816 | else if (!strcmp(args[i], "no-tlsv11")) |
| 4817 | global.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV11; |
| 4818 | else if (!strcmp(args[i], "no-tlsv12")) |
| 4819 | global.connect_default_ssloptions |= SRV_SSL_O_NO_TLSV12; |
| 4820 | else if (!strcmp(args[i], "force-sslv3")) |
| 4821 | global.connect_default_ssloptions |= SRV_SSL_O_USE_SSLV3; |
| 4822 | else if (!strcmp(args[i], "force-tlsv10")) |
| 4823 | global.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV10; |
| 4824 | else if (!strcmp(args[i], "force-tlsv11")) { |
| 4825 | #if SSL_OP_NO_TLSv1_1 |
| 4826 | global.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV11; |
| 4827 | #else |
| 4828 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.1", args[0], args[i]); |
| 4829 | return -1; |
| 4830 | #endif |
| 4831 | } |
| 4832 | else if (!strcmp(args[i], "force-tlsv12")) { |
| 4833 | #if SSL_OP_NO_TLSv1_2 |
| 4834 | global.connect_default_ssloptions |= SRV_SSL_O_USE_TLSV12; |
| 4835 | #else |
| 4836 | memprintf(err, "'%s' '%s': library does not support protocol TLSv1.2", args[0], args[i]); |
| 4837 | return -1; |
| 4838 | #endif |
| 4839 | } |
| 4840 | else if (!strcmp(args[i], "no-tls-tickets")) |
| 4841 | global.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
| 4842 | else { |
| 4843 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 4844 | return -1; |
| 4845 | } |
| 4846 | i++; |
| 4847 | } |
| 4848 | return 0; |
| 4849 | } |
| 4850 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 4851 | /* Note: must not be declared <const> as its list will be overwritten. |
| 4852 | * Please take care of keeping this list alphabetically sorted. |
| 4853 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 4854 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 4855 | { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV }, |
| 4856 | { "ssl_bc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_UINT, SMP_USE_L5SRV }, |
| 4857 | { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 4858 | { "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] | 4859 | { "ssl_bc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 4860 | { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_UINT, SMP_USE_L5SRV }, |
| 4861 | { "ssl_bc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 4862 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
| 4863 | { "ssl_c_ca_err_depth", smp_fetch_ssl_c_ca_err_depth, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4864 | { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 4865 | { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4866 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 4867 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 4868 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 4869 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 4870 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 4871 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 4872 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 4873 | { "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] | 4874 | { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 4875 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4876 | { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 4877 | { "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] | 4878 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 4879 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 4880 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 4881 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 4882 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 4883 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 4884 | { "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] | 4885 | { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 4886 | { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 4887 | { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 4888 | { "ssl_fc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4889 | { "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] | 4890 | { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
| 4891 | { "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] | 4892 | { "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] | 4893 | #ifdef OPENSSL_NPN_NEGOTIATED |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4894 | { "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] | 4895 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4896 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4897 | { "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] | 4898 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4899 | { "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] | 4900 | { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 4901 | { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 4902 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 4903 | { "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] | 4904 | { NULL, NULL, 0, 0, 0 }, |
| 4905 | }}; |
| 4906 | |
| 4907 | /* Note: must not be declared <const> as its list will be overwritten. |
| 4908 | * Please take care of keeping this list alphabetically sorted. |
| 4909 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 4910 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 4911 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 4912 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 4913 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 4914 | }}; |
| 4915 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4916 | /* Note: must not be declared <const> as its list will be overwritten. |
| 4917 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 4918 | * all code contributors. |
| 4919 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 4920 | * the config parser can report an appropriate error when a known keyword was |
| 4921 | * not enabled. |
| 4922 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 4923 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4924 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 4925 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 4926 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 4927 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
| 4928 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 4929 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 4930 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 4931 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 4932 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 4933 | { "force-sslv3", bind_parse_force_sslv3, 0 }, /* force SSLv3 */ |
| 4934 | { "force-tlsv10", bind_parse_force_tlsv10, 0 }, /* force TLSv10 */ |
| 4935 | { "force-tlsv11", bind_parse_force_tlsv11, 0 }, /* force TLSv11 */ |
| 4936 | { "force-tlsv12", bind_parse_force_tlsv12, 0 }, /* force TLSv12 */ |
| 4937 | { "no-sslv3", bind_parse_no_sslv3, 0 }, /* disable SSLv3 */ |
| 4938 | { "no-tlsv10", bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */ |
| 4939 | { "no-tlsv11", bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */ |
| 4940 | { "no-tlsv12", bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */ |
| 4941 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 4942 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
| 4943 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 4944 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 4945 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 4946 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 4947 | { NULL, NULL, 0 }, |
| 4948 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4949 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 4950 | /* Note: must not be declared <const> as its list will be overwritten. |
| 4951 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 4952 | * all code contributors. |
| 4953 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 4954 | * the config parser can report an appropriate error when a known keyword was |
| 4955 | * not enabled. |
| 4956 | */ |
| 4957 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4958 | { "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] | 4959 | { "check-ssl", srv_parse_check_ssl, 0, 0 }, /* enable SSL for health checks */ |
| 4960 | { "ciphers", srv_parse_ciphers, 1, 0 }, /* select the cipher suite */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4961 | { "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] | 4962 | { "crt", srv_parse_crt, 1, 0 }, /* set client certificate */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 4963 | { "force-sslv3", srv_parse_force_sslv3, 0, 0 }, /* force SSLv3 */ |
| 4964 | { "force-tlsv10", srv_parse_force_tlsv10, 0, 0 }, /* force TLSv10 */ |
| 4965 | { "force-tlsv11", srv_parse_force_tlsv11, 0, 0 }, /* force TLSv11 */ |
| 4966 | { "force-tlsv12", srv_parse_force_tlsv12, 0, 0 }, /* force TLSv12 */ |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 4967 | { "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] | 4968 | { "no-sslv3", srv_parse_no_sslv3, 0, 0 }, /* disable SSLv3 */ |
| 4969 | { "no-tlsv10", srv_parse_no_tlsv10, 0, 0 }, /* disable TLSv10 */ |
| 4970 | { "no-tlsv11", srv_parse_no_tlsv11, 0, 0 }, /* disable TLSv11 */ |
| 4971 | { "no-tlsv12", srv_parse_no_tlsv12, 0, 0 }, /* disable TLSv12 */ |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 4972 | { "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] | 4973 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 0 }, /* send PROXY protocol header v2 with SSL info */ |
| 4974 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 0 }, /* send PROXY protocol header v2 with CN */ |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame] | 4975 | { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */ |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4976 | { "verify", srv_parse_verify, 1, 0 }, /* set SSL verify method */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4977 | { "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] | 4978 | { NULL, NULL, 0, 0 }, |
| 4979 | }}; |
| 4980 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 4981 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 4982 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 4983 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
| 4984 | { 0, NULL, NULL }, |
| 4985 | }}; |
| 4986 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 4987 | /* transport-layer operations for SSL sockets */ |
| 4988 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4989 | .snd_buf = ssl_sock_from_buf, |
| 4990 | .rcv_buf = ssl_sock_to_buf, |
| 4991 | .rcv_pipe = NULL, |
| 4992 | .snd_pipe = NULL, |
| 4993 | .shutr = NULL, |
| 4994 | .shutw = ssl_sock_shutw, |
| 4995 | .close = ssl_sock_close, |
| 4996 | .init = ssl_sock_init, |
| 4997 | }; |
| 4998 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 4999 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 5000 | |
| 5001 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 5002 | { |
| 5003 | if (ptr) { |
| 5004 | chunk_destroy(ptr); |
| 5005 | free(ptr); |
| 5006 | } |
| 5007 | } |
| 5008 | |
| 5009 | #endif |
| 5010 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5011 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 5012 | static void __ssl_sock_init(void) |
| 5013 | { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5014 | STACK_OF(SSL_COMP)* cm; |
| 5015 | |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 5016 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 5017 | global.listen_default_ciphers = LISTEN_DEFAULT_CIPHERS; |
| 5018 | #endif |
| 5019 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 5020 | global.connect_default_ciphers = CONNECT_DEFAULT_CIPHERS; |
| 5021 | #endif |
| 5022 | if (global.listen_default_ciphers) |
| 5023 | global.listen_default_ciphers = strdup(global.listen_default_ciphers); |
| 5024 | if (global.connect_default_ciphers) |
| 5025 | global.connect_default_ciphers = strdup(global.connect_default_ciphers); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 5026 | global.listen_default_ssloptions = BC_SSL_O_NONE; |
| 5027 | global.connect_default_ssloptions = SRV_SSL_O_NONE; |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 5028 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5029 | SSL_library_init(); |
| 5030 | cm = SSL_COMP_get_compression_methods(); |
| 5031 | sk_SSL_COMP_zero(cm); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 5032 | #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 5033 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 5034 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 5035 | sample_register_fetches(&sample_fetch_keywords); |
| 5036 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 5037 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 5038 | srv_register_keywords(&srv_kws); |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 5039 | cfg_register_keywords(&cfg_kws); |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 5040 | |
| 5041 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 5042 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | /* |
| 5046 | * Local variables: |
| 5047 | * c-indent-level: 8 |
| 5048 | * c-basic-offset: 8 |
| 5049 | * End: |
| 5050 | */ |