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> |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 47 | #ifdef SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB |
| 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 | 42a3e20 | 2014-10-30 15:56:50 +0100 | [diff] [blame^] | 59 | #include <common/cfgparse.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 60 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 61 | #include <ebsttree.h> |
| 62 | |
| 63 | #include <types/global.h> |
| 64 | #include <types/ssl_sock.h> |
| 65 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 66 | #include <proto/acl.h> |
| 67 | #include <proto/arg.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 68 | #include <proto/connection.h> |
| 69 | #include <proto/fd.h> |
| 70 | #include <proto/freq_ctr.h> |
| 71 | #include <proto/frontend.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 72 | #include <proto/listener.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 73 | #include <proto/pattern.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 74 | #include <proto/server.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 75 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 76 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 77 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 78 | #include <proto/ssl_sock.h> |
| 79 | #include <proto/task.h> |
| 80 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 81 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 82 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 83 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 84 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 85 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 86 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 87 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 88 | |
| 89 | /* Verify errors macros */ |
| 90 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 91 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 92 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 93 | |
| 94 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 95 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 96 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 97 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 98 | /* server and bind verify method, it uses a global value as default */ |
| 99 | enum { |
| 100 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 101 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 102 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 103 | SSL_SOCK_VERIFY_NONE = 3, |
| 104 | }; |
| 105 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 106 | int sslconns = 0; |
| 107 | int totalsslconns = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 108 | |
Remi Gacogne | 60d7aeb | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 109 | #ifndef OPENSSL_NO_DH |
| 110 | static DH *local_dh_1024 = NULL; |
| 111 | static DH *local_dh_2048 = NULL; |
| 112 | static DH *local_dh_4096 = NULL; |
| 113 | static DH *local_dh_8192 = NULL; |
| 114 | #endif /* OPENSSL_NO_DH */ |
| 115 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 116 | #ifdef SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB |
| 117 | struct certificate_ocsp { |
| 118 | struct ebmb_node key; |
| 119 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 120 | struct chunk response; |
Emeric Brun | 5848437 | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 121 | long expire; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
Emeric Brun | 5848437 | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 124 | /* |
| 125 | * This function returns the number of seconds elapsed |
| 126 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 127 | * date presented un ASN1_GENERALIZEDTIME. |
| 128 | * |
| 129 | * In parsing error case, it returns -1. |
| 130 | */ |
| 131 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 132 | { |
| 133 | long epoch; |
| 134 | char *p, *end; |
| 135 | const unsigned short month_offset[12] = { |
| 136 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 137 | }; |
| 138 | int year, month; |
| 139 | |
| 140 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 141 | |
| 142 | p = (char *)d->data; |
| 143 | end = p + d->length; |
| 144 | |
| 145 | if (end - p < 4) return -1; |
| 146 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 147 | p += 4; |
| 148 | if (end - p < 2) return -1; |
| 149 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 150 | if (month < 1 || month > 12) return -1; |
| 151 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 152 | We consider leap years and the current month (<marsh or not) */ |
| 153 | epoch = ( ((year - 1970) * 365) |
| 154 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 155 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 156 | + month_offset[month-1] |
| 157 | ) * 24 * 60 * 60; |
| 158 | p += 2; |
| 159 | if (end - p < 2) return -1; |
| 160 | /* Add the number of seconds of completed days of current month */ |
| 161 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 162 | p += 2; |
| 163 | if (end - p < 2) return -1; |
| 164 | /* Add the completed hours of the current day */ |
| 165 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 166 | p += 2; |
| 167 | if (end - p < 2) return -1; |
| 168 | /* Add the completed minutes of the current hour */ |
| 169 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 170 | p += 2; |
| 171 | if (p == end) return -1; |
| 172 | /* Test if there is available seconds */ |
| 173 | if (p[0] < '0' || p[0] > '9') |
| 174 | goto nosec; |
| 175 | if (end - p < 2) return -1; |
| 176 | /* Add the seconds of the current minute */ |
| 177 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 178 | p += 2; |
| 179 | if (p == end) return -1; |
| 180 | /* Ignore seconds float part if present */ |
| 181 | if (p[0] == '.') { |
| 182 | do { |
| 183 | if (++p == end) return -1; |
| 184 | } while (p[0] >= '0' && p[0] <= '9'); |
| 185 | } |
| 186 | |
| 187 | nosec: |
| 188 | if (p[0] == 'Z') { |
| 189 | if (end - p != 1) return -1; |
| 190 | return epoch; |
| 191 | } |
| 192 | else if (p[0] == '+') { |
| 193 | if (end - p != 5) return -1; |
| 194 | /* Apply timezone offset */ |
| 195 | return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 196 | } |
| 197 | else if (p[0] == '-') { |
| 198 | if (end - p != 5) return -1; |
| 199 | /* Apply timezone offset */ |
| 200 | return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
| 201 | } |
| 202 | |
| 203 | return -1; |
| 204 | } |
| 205 | |
Emeric Brun | 8d914d1 | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 206 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 207 | |
| 208 | /* This function starts to check if the OCSP response (in DER format) contained |
| 209 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 210 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 211 | * contained in the OCSP Response and exits on error if no match. |
| 212 | * If it's a valid OCSP Response: |
| 213 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 214 | * pointed by 'ocsp'. |
| 215 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 216 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 217 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 218 | * already present in the container, it will be overwritten. |
| 219 | * |
| 220 | * Note: OCSP response containing more than one OCSP Single response is not |
| 221 | * considered valid. |
| 222 | * |
| 223 | * Returns 0 on success, 1 in error case. |
| 224 | */ |
| 225 | static int ssl_sock_load_ocsp_response(struct chunk *ocsp_response, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 226 | { |
| 227 | OCSP_RESPONSE *resp; |
| 228 | OCSP_BASICRESP *bs = NULL; |
| 229 | OCSP_SINGLERESP *sr; |
| 230 | unsigned char *p = (unsigned char *)ocsp_response->str; |
| 231 | int rc , count_sr; |
Emeric Brun | 1135ea4 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 232 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 233 | int reason; |
| 234 | int ret = 1; |
| 235 | |
| 236 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, ocsp_response->len); |
| 237 | if (!resp) { |
| 238 | memprintf(err, "Unable to parse OCSP response"); |
| 239 | goto out; |
| 240 | } |
| 241 | |
| 242 | rc = OCSP_response_status(resp); |
| 243 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 244 | memprintf(err, "OCSP response status not successful"); |
| 245 | goto out; |
| 246 | } |
| 247 | |
| 248 | bs = OCSP_response_get1_basic(resp); |
| 249 | if (!bs) { |
| 250 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 251 | goto out; |
| 252 | } |
| 253 | |
| 254 | count_sr = OCSP_resp_count(bs); |
| 255 | if (count_sr > 1) { |
| 256 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 257 | goto out; |
| 258 | } |
| 259 | |
| 260 | sr = OCSP_resp_get0(bs, 0); |
| 261 | if (!sr) { |
| 262 | memprintf(err, "Failed to get OCSP single response"); |
| 263 | goto out; |
| 264 | } |
| 265 | |
| 266 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
| 267 | if (rc != V_OCSP_CERTSTATUS_GOOD) { |
| 268 | memprintf(err, "OCSP single response: certificate status not good"); |
| 269 | goto out; |
| 270 | } |
| 271 | |
Emeric Brun | 1135ea4 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 272 | if (!nextupd) { |
| 273 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 274 | goto out; |
| 275 | } |
| 276 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 277 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 278 | if (!rc) { |
| 279 | memprintf(err, "OCSP single response: no longer valid."); |
| 280 | goto out; |
| 281 | } |
| 282 | |
| 283 | if (cid) { |
| 284 | if (OCSP_id_cmp(sr->certId, cid)) { |
| 285 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 286 | goto out; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (!ocsp) { |
| 291 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 292 | unsigned char *p; |
| 293 | |
| 294 | rc = i2d_OCSP_CERTID(sr->certId, NULL); |
| 295 | if (!rc) { |
| 296 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 297 | goto out; |
| 298 | } |
| 299 | |
| 300 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 301 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 302 | goto out; |
| 303 | } |
| 304 | |
| 305 | p = key; |
| 306 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 307 | i2d_OCSP_CERTID(sr->certId, &p); |
| 308 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 309 | if (!ocsp) { |
| 310 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 311 | goto out; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /* According to comments on "chunk_dup", the |
| 316 | previous chunk buffer will be freed */ |
| 317 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 318 | memprintf(err, "OCSP response: Memory allocation error"); |
| 319 | goto out; |
| 320 | } |
| 321 | |
Emeric Brun | 5848437 | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 322 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 323 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 324 | ret = 0; |
| 325 | out: |
| 326 | if (bs) |
| 327 | OCSP_BASICRESP_free(bs); |
| 328 | |
| 329 | if (resp) |
| 330 | OCSP_RESPONSE_free(resp); |
| 331 | |
| 332 | return ret; |
| 333 | } |
| 334 | /* |
| 335 | * External function use to update the OCSP response in the OCSP response's |
| 336 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 337 | * to update in DER format. |
| 338 | * |
| 339 | * Returns 0 on success, 1 in error case. |
| 340 | */ |
| 341 | int ssl_sock_update_ocsp_response(struct chunk *ocsp_response, char **err) |
| 342 | { |
| 343 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * This function load the OCSP Resonse in DER format contained in file at |
| 348 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 349 | * |
| 350 | * Returns 0 on success, 1 in error case. |
| 351 | */ |
| 352 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 353 | { |
| 354 | int fd = -1; |
| 355 | int r = 0; |
| 356 | int ret = 1; |
| 357 | |
| 358 | fd = open(ocsp_path, O_RDONLY); |
| 359 | if (fd == -1) { |
| 360 | memprintf(err, "Error opening OCSP response file"); |
| 361 | goto end; |
| 362 | } |
| 363 | |
| 364 | trash.len = 0; |
| 365 | while (trash.len < trash.size) { |
| 366 | r = read(fd, trash.str + trash.len, trash.size - trash.len); |
| 367 | if (r < 0) { |
| 368 | if (errno == EINTR) |
| 369 | continue; |
| 370 | |
| 371 | memprintf(err, "Error reading OCSP response from file"); |
| 372 | goto end; |
| 373 | } |
| 374 | else if (r == 0) { |
| 375 | break; |
| 376 | } |
| 377 | trash.len += r; |
| 378 | } |
| 379 | |
| 380 | close(fd); |
| 381 | fd = -1; |
| 382 | |
| 383 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 384 | end: |
| 385 | if (fd != -1) |
| 386 | close(fd); |
| 387 | |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Callback used to set OCSP status extension content in server hello. |
| 393 | */ |
| 394 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 395 | { |
| 396 | struct certificate_ocsp *ocsp = (struct certificate_ocsp *)arg; |
| 397 | char* ssl_buf; |
| 398 | |
| 399 | if (!ocsp || |
| 400 | !ocsp->response.str || |
Emeric Brun | 5848437 | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 401 | !ocsp->response.len || |
| 402 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 403 | return SSL_TLSEXT_ERR_NOACK; |
| 404 | |
| 405 | ssl_buf = OPENSSL_malloc(ocsp->response.len); |
| 406 | if (!ssl_buf) |
| 407 | return SSL_TLSEXT_ERR_NOACK; |
| 408 | |
| 409 | memcpy(ssl_buf, ocsp->response.str, ocsp->response.len); |
| 410 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.len); |
| 411 | |
| 412 | return SSL_TLSEXT_ERR_OK; |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 417 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 418 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 419 | * It should be present in the certificate's extra chain builded from file |
| 420 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 421 | * named 'cert_path' suffixed using '.issuer'. |
| 422 | * |
| 423 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 424 | * response. If file is empty or content is not a valid OCSP response, |
| 425 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 426 | * is displayed). |
| 427 | * |
| 428 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
| 429 | * succesfully enabled, or -1 in other error case. |
| 430 | */ |
| 431 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 432 | { |
| 433 | |
| 434 | BIO *in = NULL; |
| 435 | X509 *x, *xi = NULL, *issuer = NULL; |
| 436 | STACK_OF(X509) *chain = NULL; |
| 437 | OCSP_CERTID *cid = NULL; |
| 438 | SSL *ssl; |
| 439 | char ocsp_path[MAXPATHLEN+1]; |
| 440 | int i, ret = -1; |
| 441 | struct stat st; |
| 442 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 443 | char *warn = NULL; |
| 444 | unsigned char *p; |
| 445 | |
| 446 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 447 | |
| 448 | if (stat(ocsp_path, &st)) |
| 449 | return 1; |
| 450 | |
| 451 | ssl = SSL_new(ctx); |
| 452 | if (!ssl) |
| 453 | goto out; |
| 454 | |
| 455 | x = SSL_get_certificate(ssl); |
| 456 | if (!x) |
| 457 | goto out; |
| 458 | |
| 459 | /* Try to lookup for issuer in certificate extra chain */ |
| 460 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 461 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 462 | #else |
| 463 | chain = ctx->extra_certs; |
| 464 | #endif |
| 465 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 466 | issuer = sk_X509_value(chain, i); |
| 467 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 468 | break; |
| 469 | else |
| 470 | issuer = NULL; |
| 471 | } |
| 472 | |
| 473 | /* If not found try to load issuer from a suffixed file */ |
| 474 | if (!issuer) { |
| 475 | char issuer_path[MAXPATHLEN+1]; |
| 476 | |
| 477 | in = BIO_new(BIO_s_file()); |
| 478 | if (!in) |
| 479 | goto out; |
| 480 | |
| 481 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 482 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 483 | goto out; |
| 484 | |
| 485 | xi = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata); |
| 486 | if (!xi) |
| 487 | goto out; |
| 488 | |
| 489 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 490 | goto out; |
| 491 | |
| 492 | issuer = xi; |
| 493 | } |
| 494 | |
| 495 | cid = OCSP_cert_to_id(0, x, issuer); |
| 496 | if (!cid) |
| 497 | goto out; |
| 498 | |
| 499 | i = i2d_OCSP_CERTID(cid, NULL); |
| 500 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 501 | goto out; |
| 502 | |
| 503 | ocsp = calloc(1, sizeof(struct certificate_ocsp)); |
| 504 | if (!ocsp) |
| 505 | goto out; |
| 506 | |
| 507 | p = ocsp->key_data; |
| 508 | i2d_OCSP_CERTID(cid, &p); |
| 509 | |
| 510 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 511 | if (iocsp == ocsp) |
| 512 | ocsp = NULL; |
| 513 | |
| 514 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 515 | SSL_CTX_set_tlsext_status_arg(ctx, iocsp); |
| 516 | |
| 517 | ret = 0; |
| 518 | |
| 519 | warn = NULL; |
| 520 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 521 | memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure"); |
| 522 | Warning("%s.\n", warn); |
| 523 | } |
| 524 | |
| 525 | out: |
| 526 | if (ssl) |
| 527 | SSL_free(ssl); |
| 528 | |
| 529 | if (in) |
| 530 | BIO_free(in); |
| 531 | |
| 532 | if (xi) |
| 533 | X509_free(xi); |
| 534 | |
| 535 | if (cid) |
| 536 | OCSP_CERTID_free(cid); |
| 537 | |
| 538 | if (ocsp) |
| 539 | free(ocsp); |
| 540 | |
| 541 | if (warn) |
| 542 | free(warn); |
| 543 | |
| 544 | |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | #endif |
| 549 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 550 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 551 | { |
| 552 | struct connection *conn = (struct connection *)SSL_get_app_data(ssl); |
| 553 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 554 | BIO *write_bio; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 555 | |
| 556 | if (where & SSL_CB_HANDSHAKE_START) { |
| 557 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 558 | if (conn->flags & CO_FL_CONNECTED) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 559 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 560 | conn->err_code = CO_ER_SSL_RENEG; |
| 561 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 562 | } |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 563 | |
| 564 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
| 565 | if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
| 566 | /* Long certificate chains optimz |
| 567 | If write and read bios are differents, we |
| 568 | consider that the buffering was activated, |
| 569 | so we rise the output buffer size from 4k |
| 570 | to 16k */ |
| 571 | write_bio = SSL_get_wbio(ssl); |
| 572 | if (write_bio != SSL_get_rbio(ssl)) { |
| 573 | BIO_set_write_buffer_size(write_bio, 16384); |
| 574 | conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
| 575 | } |
| 576 | } |
| 577 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 580 | /* Callback is called for each certificate of the chain during a verify |
| 581 | ok is set to 1 if preverify detect no error on current certificate. |
| 582 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 583 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 584 | { |
| 585 | SSL *ssl; |
| 586 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 587 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 588 | |
| 589 | ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 590 | conn = (struct connection *)SSL_get_app_data(ssl); |
| 591 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 592 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 593 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 594 | if (ok) /* no errors */ |
| 595 | return ok; |
| 596 | |
| 597 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 598 | err = X509_STORE_CTX_get_error(x_store); |
| 599 | |
| 600 | /* check if CA error needs to be ignored */ |
| 601 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 602 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 603 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 604 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 605 | } |
| 606 | |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 607 | if (objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) { |
| 608 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 609 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 610 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 611 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 612 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 613 | return 0; |
| 614 | } |
| 615 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 616 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 617 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 618 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 619 | /* check if certificate error needs to be ignored */ |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 620 | if (objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) { |
| 621 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 622 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 623 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 624 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 625 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 626 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 629 | /* Callback is called for ssl protocol analyse */ |
| 630 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 631 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 632 | #ifdef TLS1_RT_HEARTBEAT |
| 633 | /* test heartbeat received (write_p is set to 0 |
| 634 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 635 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Willy Tarreau | 8481500 | 2014-04-25 21:40:27 +0200 | [diff] [blame] | 636 | struct connection *conn = (struct connection *)SSL_get_app_data(ssl); |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 637 | const unsigned char *p = buf; |
| 638 | unsigned int payload; |
| 639 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 640 | conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 641 | |
| 642 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 643 | if (*p != TLS1_HB_REQUEST) |
| 644 | return; |
| 645 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 646 | 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] | 647 | goto kill_it; |
| 648 | |
| 649 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 650 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 651 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 652 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 653 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 654 | * advertised payload is larger than the advertised packet |
| 655 | * length, so we have garbage in the buffer between the |
| 656 | * payload and the end of the buffer (p+len). We can't know |
| 657 | * if the SSL stack is patched, and we don't know if we can |
| 658 | * safely wipe out the area between p+3+len and payload. |
| 659 | * So instead, we prevent the response from being sent by |
| 660 | * setting the max_send_fragment to 0 and we report an SSL |
| 661 | * error, which will kill this connection. It will be reported |
| 662 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 663 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 664 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 665 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 666 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 667 | return; |
| 668 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 669 | #endif |
| 670 | } |
| 671 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 672 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 673 | /* This callback is used so that the server advertises the list of |
| 674 | * negociable protocols for NPN. |
| 675 | */ |
| 676 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 677 | unsigned int *len, void *arg) |
| 678 | { |
| 679 | struct bind_conf *conf = arg; |
| 680 | |
| 681 | *data = (const unsigned char *)conf->npn_str; |
| 682 | *len = conf->npn_len; |
| 683 | return SSL_TLSEXT_ERR_OK; |
| 684 | } |
| 685 | #endif |
| 686 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 687 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 688 | /* This callback is used so that the server advertises the list of |
| 689 | * negociable protocols for ALPN. |
| 690 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 691 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 692 | unsigned char *outlen, |
| 693 | const unsigned char *server, |
| 694 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 695 | { |
| 696 | struct bind_conf *conf = arg; |
| 697 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 698 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 699 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 700 | return SSL_TLSEXT_ERR_NOACK; |
| 701 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 702 | return SSL_TLSEXT_ERR_OK; |
| 703 | } |
| 704 | #endif |
| 705 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 706 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 707 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 708 | * warning when no match is found, which implies the default (first) cert |
| 709 | * will keep being used. |
| 710 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 711 | 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] | 712 | { |
| 713 | const char *servername; |
| 714 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 715 | struct ebmb_node *node, *n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 716 | int i; |
| 717 | (void)al; /* shut gcc stupid warning */ |
| 718 | |
| 719 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 720 | if (!servername) { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 721 | return (s->strict_sni ? |
| 722 | SSL_TLSEXT_ERR_ALERT_FATAL : |
Emmanuel Hocdet | 79274e2 | 2013-05-31 12:47:44 +0200 | [diff] [blame] | 723 | SSL_TLSEXT_ERR_NOACK); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 724 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 725 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 726 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 727 | if (!servername[i]) |
| 728 | break; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 729 | trash.str[i] = tolower(servername[i]); |
| 730 | if (!wildp && (trash.str[i] == '.')) |
| 731 | wildp = &trash.str[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 732 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 733 | trash.str[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 734 | |
| 735 | /* lookup in full qualified names */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 736 | node = ebst_lookup(&s->sni_ctx, trash.str); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 737 | |
| 738 | /* lookup a not neg filter */ |
| 739 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 740 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 741 | node = n; |
| 742 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 743 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 744 | } |
| 745 | if (!node && wildp) { |
| 746 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 747 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 748 | } |
| 749 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
| 750 | return (s->strict_sni ? |
| 751 | SSL_TLSEXT_ERR_ALERT_FATAL : |
| 752 | SSL_TLSEXT_ERR_ALERT_WARNING); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* switch ctx */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 756 | 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] | 757 | return SSL_TLSEXT_ERR_OK; |
| 758 | } |
| 759 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 760 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 761 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 762 | |
| 763 | static DH * ssl_get_dh_1024(void) |
| 764 | { |
| 765 | #if OPENSSL_VERSION_NUMBER < 0x0090801fL |
| 766 | static const unsigned char rfc_2409_prime_1024[] = { |
| 767 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2, |
| 768 | 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1, |
| 769 | 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6, |
| 770 | 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD, |
| 771 | 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D, |
| 772 | 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45, |
| 773 | 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9, |
| 774 | 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED, |
| 775 | 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11, |
| 776 | 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE6,0x53,0x81, |
| 777 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 778 | }; |
| 779 | #endif |
| 780 | DH *dh = DH_new(); |
| 781 | if (dh) { |
| 782 | #if OPENSSL_VERSION_NUMBER >= 0x0090801fL |
| 783 | dh->p = get_rfc2409_prime_1024(NULL); |
| 784 | #else |
| 785 | dh->p = BN_bin2bn(rfc_2409_prime_1024, sizeof rfc_2409_prime_1024, NULL); |
| 786 | #endif |
| 787 | /* See RFC 2409, Section 6 "Oakley Groups" |
| 788 | for the reason why 2 is used as generator. |
| 789 | */ |
| 790 | BN_dec2bn(&dh->g, "2"); |
| 791 | if (!dh->p || !dh->g) { |
| 792 | DH_free(dh); |
| 793 | dh = NULL; |
| 794 | } |
| 795 | } |
| 796 | return dh; |
| 797 | } |
| 798 | |
| 799 | static DH *ssl_get_dh_2048(void) |
| 800 | { |
| 801 | #if OPENSSL_VERSION_NUMBER < 0x0090801fL |
| 802 | static const unsigned char rfc_3526_prime_2048[] = { |
| 803 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2, |
| 804 | 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1, |
| 805 | 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6, |
| 806 | 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD, |
| 807 | 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D, |
| 808 | 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45, |
| 809 | 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9, |
| 810 | 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED, |
| 811 | 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11, |
| 812 | 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D, |
| 813 | 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36, |
| 814 | 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F, |
| 815 | 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56, |
| 816 | 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D, |
| 817 | 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08, |
| 818 | 0xCA,0x18,0x21,0x7C,0x32,0x90,0x5E,0x46,0x2E,0x36,0xCE,0x3B, |
| 819 | 0xE3,0x9E,0x77,0x2C,0x18,0x0E,0x86,0x03,0x9B,0x27,0x83,0xA2, |
| 820 | 0xEC,0x07,0xA2,0x8F,0xB5,0xC5,0x5D,0xF0,0x6F,0x4C,0x52,0xC9, |
| 821 | 0xDE,0x2B,0xCB,0xF6,0x95,0x58,0x17,0x18,0x39,0x95,0x49,0x7C, |
| 822 | 0xEA,0x95,0x6A,0xE5,0x15,0xD2,0x26,0x18,0x98,0xFA,0x05,0x10, |
| 823 | 0x15,0x72,0x8E,0x5A,0x8A,0xAC,0xAA,0x68,0xFF,0xFF,0xFF,0xFF, |
| 824 | 0xFF,0xFF,0xFF,0xFF, |
| 825 | }; |
| 826 | #endif |
| 827 | DH *dh = DH_new(); |
| 828 | if (dh) { |
| 829 | #if OPENSSL_VERSION_NUMBER >= 0x0090801fL |
| 830 | dh->p = get_rfc3526_prime_2048(NULL); |
| 831 | #else |
| 832 | dh->p = BN_bin2bn(rfc_3526_prime_2048, sizeof rfc_3526_prime_2048, NULL); |
| 833 | #endif |
| 834 | /* See RFC 3526, Section 3 "2048-bit MODP Group" |
| 835 | for the reason why 2 is used as generator. |
| 836 | */ |
| 837 | BN_dec2bn(&dh->g, "2"); |
| 838 | if (!dh->p || !dh->g) { |
| 839 | DH_free(dh); |
| 840 | dh = NULL; |
| 841 | } |
| 842 | } |
| 843 | return dh; |
| 844 | } |
| 845 | |
| 846 | static DH *ssl_get_dh_4096(void) |
| 847 | { |
| 848 | #if OPENSSL_VERSION_NUMBER < 0x0090801fL |
| 849 | static const unsigned char rfc_3526_prime_4096[] = { |
| 850 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2, |
| 851 | 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1, |
| 852 | 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6, |
| 853 | 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD, |
| 854 | 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D, |
| 855 | 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45, |
| 856 | 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9, |
| 857 | 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED, |
| 858 | 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11, |
| 859 | 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D, |
| 860 | 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36, |
| 861 | 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F, |
| 862 | 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56, |
| 863 | 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D, |
| 864 | 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08, |
| 865 | 0xCA,0x18,0x21,0x7C,0x32,0x90,0x5E,0x46,0x2E,0x36,0xCE,0x3B, |
| 866 | 0xE3,0x9E,0x77,0x2C,0x18,0x0E,0x86,0x03,0x9B,0x27,0x83,0xA2, |
| 867 | 0xEC,0x07,0xA2,0x8F,0xB5,0xC5,0x5D,0xF0,0x6F,0x4C,0x52,0xC9, |
| 868 | 0xDE,0x2B,0xCB,0xF6,0x95,0x58,0x17,0x18,0x39,0x95,0x49,0x7C, |
| 869 | 0xEA,0x95,0x6A,0xE5,0x15,0xD2,0x26,0x18,0x98,0xFA,0x05,0x10, |
| 870 | 0x15,0x72,0x8E,0x5A,0x8A,0xAA,0xC4,0x2D,0xAD,0x33,0x17,0x0D, |
| 871 | 0x04,0x50,0x7A,0x33,0xA8,0x55,0x21,0xAB,0xDF,0x1C,0xBA,0x64, |
| 872 | 0xEC,0xFB,0x85,0x04,0x58,0xDB,0xEF,0x0A,0x8A,0xEA,0x71,0x57, |
| 873 | 0x5D,0x06,0x0C,0x7D,0xB3,0x97,0x0F,0x85,0xA6,0xE1,0xE4,0xC7, |
| 874 | 0xAB,0xF5,0xAE,0x8C,0xDB,0x09,0x33,0xD7,0x1E,0x8C,0x94,0xE0, |
| 875 | 0x4A,0x25,0x61,0x9D,0xCE,0xE3,0xD2,0x26,0x1A,0xD2,0xEE,0x6B, |
| 876 | 0xF1,0x2F,0xFA,0x06,0xD9,0x8A,0x08,0x64,0xD8,0x76,0x02,0x73, |
| 877 | 0x3E,0xC8,0x6A,0x64,0x52,0x1F,0x2B,0x18,0x17,0x7B,0x20,0x0C, |
| 878 | 0xBB,0xE1,0x17,0x57,0x7A,0x61,0x5D,0x6C,0x77,0x09,0x88,0xC0, |
| 879 | 0xBA,0xD9,0x46,0xE2,0x08,0xE2,0x4F,0xA0,0x74,0xE5,0xAB,0x31, |
| 880 | 0x43,0xDB,0x5B,0xFC,0xE0,0xFD,0x10,0x8E,0x4B,0x82,0xD1,0x20, |
| 881 | 0xA9,0x21,0x08,0x01,0x1A,0x72,0x3C,0x12,0xA7,0x87,0xE6,0xD7, |
| 882 | 0x88,0x71,0x9A,0x10,0xBD,0xBA,0x5B,0x26,0x99,0xC3,0x27,0x18, |
| 883 | 0x6A,0xF4,0xE2,0x3C,0x1A,0x94,0x68,0x34,0xB6,0x15,0x0B,0xDA, |
| 884 | 0x25,0x83,0xE9,0xCA,0x2A,0xD4,0x4C,0xE8,0xDB,0xBB,0xC2,0xDB, |
| 885 | 0x04,0xDE,0x8E,0xF9,0x2E,0x8E,0xFC,0x14,0x1F,0xBE,0xCA,0xA6, |
| 886 | 0x28,0x7C,0x59,0x47,0x4E,0x6B,0xC0,0x5D,0x99,0xB2,0x96,0x4F, |
| 887 | 0xA0,0x90,0xC3,0xA2,0x23,0x3B,0xA1,0x86,0x51,0x5B,0xE7,0xED, |
| 888 | 0x1F,0x61,0x29,0x70,0xCE,0xE2,0xD7,0xAF,0xB8,0x1B,0xDD,0x76, |
| 889 | 0x21,0x70,0x48,0x1C,0xD0,0x06,0x91,0x27,0xD5,0xB0,0x5A,0xA9, |
| 890 | 0x93,0xB4,0xEA,0x98,0x8D,0x8F,0xDD,0xC1,0x86,0xFF,0xB7,0xDC, |
| 891 | 0x90,0xA6,0xC0,0x8F,0x4D,0xF4,0x35,0xC9,0x34,0x06,0x31,0x99, |
| 892 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 893 | }; |
| 894 | #endif |
| 895 | DH *dh = DH_new(); |
| 896 | if (dh) { |
| 897 | #if OPENSSL_VERSION_NUMBER >= 0x0090801fL |
| 898 | dh->p = get_rfc3526_prime_4096(NULL); |
| 899 | #else |
| 900 | dh->p = BN_bin2bn(rfc_3526_prime_4096, sizeof rfc_3526_prime_4096, NULL); |
| 901 | #endif |
| 902 | /* See RFC 3526, Section 5 "4096-bit MODP Group" |
| 903 | for the reason why 2 is used as generator. |
| 904 | */ |
| 905 | BN_dec2bn(&dh->g, "2"); |
| 906 | if (!dh->p || !dh->g) { |
| 907 | DH_free(dh); |
| 908 | dh = NULL; |
| 909 | } |
| 910 | } |
| 911 | return dh; |
| 912 | } |
| 913 | |
| 914 | static DH *ssl_get_dh_8192(void) |
| 915 | { |
| 916 | #if OPENSSL_VERSION_NUMBER < 0x0090801fL |
| 917 | static const unsigned char rfc_3526_prime_8192[] = { |
| 918 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2, |
| 919 | 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1, |
| 920 | 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6, |
| 921 | 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD, |
| 922 | 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D, |
| 923 | 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45, |
| 924 | 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9, |
| 925 | 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED, |
| 926 | 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11, |
| 927 | 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D, |
| 928 | 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36, |
| 929 | 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F, |
| 930 | 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56, |
| 931 | 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D, |
| 932 | 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08, |
| 933 | 0xCA,0x18,0x21,0x7C,0x32,0x90,0x5E,0x46,0x2E,0x36,0xCE,0x3B, |
| 934 | 0xE3,0x9E,0x77,0x2C,0x18,0x0E,0x86,0x03,0x9B,0x27,0x83,0xA2, |
| 935 | 0xEC,0x07,0xA2,0x8F,0xB5,0xC5,0x5D,0xF0,0x6F,0x4C,0x52,0xC9, |
| 936 | 0xDE,0x2B,0xCB,0xF6,0x95,0x58,0x17,0x18,0x39,0x95,0x49,0x7C, |
| 937 | 0xEA,0x95,0x6A,0xE5,0x15,0xD2,0x26,0x18,0x98,0xFA,0x05,0x10, |
| 938 | 0x15,0x72,0x8E,0x5A,0x8A,0xAA,0xC4,0x2D,0xAD,0x33,0x17,0x0D, |
| 939 | 0x04,0x50,0x7A,0x33,0xA8,0x55,0x21,0xAB,0xDF,0x1C,0xBA,0x64, |
| 940 | 0xEC,0xFB,0x85,0x04,0x58,0xDB,0xEF,0x0A,0x8A,0xEA,0x71,0x57, |
| 941 | 0x5D,0x06,0x0C,0x7D,0xB3,0x97,0x0F,0x85,0xA6,0xE1,0xE4,0xC7, |
| 942 | 0xAB,0xF5,0xAE,0x8C,0xDB,0x09,0x33,0xD7,0x1E,0x8C,0x94,0xE0, |
| 943 | 0x4A,0x25,0x61,0x9D,0xCE,0xE3,0xD2,0x26,0x1A,0xD2,0xEE,0x6B, |
| 944 | 0xF1,0x2F,0xFA,0x06,0xD9,0x8A,0x08,0x64,0xD8,0x76,0x02,0x73, |
| 945 | 0x3E,0xC8,0x6A,0x64,0x52,0x1F,0x2B,0x18,0x17,0x7B,0x20,0x0C, |
| 946 | 0xBB,0xE1,0x17,0x57,0x7A,0x61,0x5D,0x6C,0x77,0x09,0x88,0xC0, |
| 947 | 0xBA,0xD9,0x46,0xE2,0x08,0xE2,0x4F,0xA0,0x74,0xE5,0xAB,0x31, |
| 948 | 0x43,0xDB,0x5B,0xFC,0xE0,0xFD,0x10,0x8E,0x4B,0x82,0xD1,0x20, |
| 949 | 0xA9,0x21,0x08,0x01,0x1A,0x72,0x3C,0x12,0xA7,0x87,0xE6,0xD7, |
| 950 | 0x88,0x71,0x9A,0x10,0xBD,0xBA,0x5B,0x26,0x99,0xC3,0x27,0x18, |
| 951 | 0x6A,0xF4,0xE2,0x3C,0x1A,0x94,0x68,0x34,0xB6,0x15,0x0B,0xDA, |
| 952 | 0x25,0x83,0xE9,0xCA,0x2A,0xD4,0x4C,0xE8,0xDB,0xBB,0xC2,0xDB, |
| 953 | 0x04,0xDE,0x8E,0xF9,0x2E,0x8E,0xFC,0x14,0x1F,0xBE,0xCA,0xA6, |
| 954 | 0x28,0x7C,0x59,0x47,0x4E,0x6B,0xC0,0x5D,0x99,0xB2,0x96,0x4F, |
| 955 | 0xA0,0x90,0xC3,0xA2,0x23,0x3B,0xA1,0x86,0x51,0x5B,0xE7,0xED, |
| 956 | 0x1F,0x61,0x29,0x70,0xCE,0xE2,0xD7,0xAF,0xB8,0x1B,0xDD,0x76, |
| 957 | 0x21,0x70,0x48,0x1C,0xD0,0x06,0x91,0x27,0xD5,0xB0,0x5A,0xA9, |
| 958 | 0x93,0xB4,0xEA,0x98,0x8D,0x8F,0xDD,0xC1,0x86,0xFF,0xB7,0xDC, |
| 959 | 0x90,0xA6,0xC0,0x8F,0x4D,0xF4,0x35,0xC9,0x34,0x02,0x84,0x92, |
| 960 | 0x36,0xC3,0xFA,0xB4,0xD2,0x7C,0x70,0x26,0xC1,0xD4,0xDC,0xB2, |
| 961 | 0x60,0x26,0x46,0xDE,0xC9,0x75,0x1E,0x76,0x3D,0xBA,0x37,0xBD, |
| 962 | 0xF8,0xFF,0x94,0x06,0xAD,0x9E,0x53,0x0E,0xE5,0xDB,0x38,0x2F, |
| 963 | 0x41,0x30,0x01,0xAE,0xB0,0x6A,0x53,0xED,0x90,0x27,0xD8,0x31, |
| 964 | 0x17,0x97,0x27,0xB0,0x86,0x5A,0x89,0x18,0xDA,0x3E,0xDB,0xEB, |
| 965 | 0xCF,0x9B,0x14,0xED,0x44,0xCE,0x6C,0xBA,0xCE,0xD4,0xBB,0x1B, |
| 966 | 0xDB,0x7F,0x14,0x47,0xE6,0xCC,0x25,0x4B,0x33,0x20,0x51,0x51, |
| 967 | 0x2B,0xD7,0xAF,0x42,0x6F,0xB8,0xF4,0x01,0x37,0x8C,0xD2,0xBF, |
| 968 | 0x59,0x83,0xCA,0x01,0xC6,0x4B,0x92,0xEC,0xF0,0x32,0xEA,0x15, |
| 969 | 0xD1,0x72,0x1D,0x03,0xF4,0x82,0xD7,0xCE,0x6E,0x74,0xFE,0xF6, |
| 970 | 0xD5,0x5E,0x70,0x2F,0x46,0x98,0x0C,0x82,0xB5,0xA8,0x40,0x31, |
| 971 | 0x90,0x0B,0x1C,0x9E,0x59,0xE7,0xC9,0x7F,0xBE,0xC7,0xE8,0xF3, |
| 972 | 0x23,0xA9,0x7A,0x7E,0x36,0xCC,0x88,0xBE,0x0F,0x1D,0x45,0xB7, |
| 973 | 0xFF,0x58,0x5A,0xC5,0x4B,0xD4,0x07,0xB2,0x2B,0x41,0x54,0xAA, |
| 974 | 0xCC,0x8F,0x6D,0x7E,0xBF,0x48,0xE1,0xD8,0x14,0xCC,0x5E,0xD2, |
| 975 | 0x0F,0x80,0x37,0xE0,0xA7,0x97,0x15,0xEE,0xF2,0x9B,0xE3,0x28, |
| 976 | 0x06,0xA1,0xD5,0x8B,0xB7,0xC5,0xDA,0x76,0xF5,0x50,0xAA,0x3D, |
| 977 | 0x8A,0x1F,0xBF,0xF0,0xEB,0x19,0xCC,0xB1,0xA3,0x13,0xD5,0x5C, |
| 978 | 0xDA,0x56,0xC9,0xEC,0x2E,0xF2,0x96,0x32,0x38,0x7F,0xE8,0xD7, |
| 979 | 0x6E,0x3C,0x04,0x68,0x04,0x3E,0x8F,0x66,0x3F,0x48,0x60,0xEE, |
| 980 | 0x12,0xBF,0x2D,0x5B,0x0B,0x74,0x74,0xD6,0xE6,0x94,0xF9,0x1E, |
| 981 | 0x6D,0xBE,0x11,0x59,0x74,0xA3,0x92,0x6F,0x12,0xFE,0xE5,0xE4, |
| 982 | 0x38,0x77,0x7C,0xB6,0xA9,0x32,0xDF,0x8C,0xD8,0xBE,0xC4,0xD0, |
| 983 | 0x73,0xB9,0x31,0xBA,0x3B,0xC8,0x32,0xB6,0x8D,0x9D,0xD3,0x00, |
| 984 | 0x74,0x1F,0xA7,0xBF,0x8A,0xFC,0x47,0xED,0x25,0x76,0xF6,0x93, |
| 985 | 0x6B,0xA4,0x24,0x66,0x3A,0xAB,0x63,0x9C,0x5A,0xE4,0xF5,0x68, |
| 986 | 0x34,0x23,0xB4,0x74,0x2B,0xF1,0xC9,0x78,0x23,0x8F,0x16,0xCB, |
| 987 | 0xE3,0x9D,0x65,0x2D,0xE3,0xFD,0xB8,0xBE,0xFC,0x84,0x8A,0xD9, |
| 988 | 0x22,0x22,0x2E,0x04,0xA4,0x03,0x7C,0x07,0x13,0xEB,0x57,0xA8, |
| 989 | 0x1A,0x23,0xF0,0xC7,0x34,0x73,0xFC,0x64,0x6C,0xEA,0x30,0x6B, |
| 990 | 0x4B,0xCB,0xC8,0x86,0x2F,0x83,0x85,0xDD,0xFA,0x9D,0x4B,0x7F, |
| 991 | 0xA2,0xC0,0x87,0xE8,0x79,0x68,0x33,0x03,0xED,0x5B,0xDD,0x3A, |
| 992 | 0x06,0x2B,0x3C,0xF5,0xB3,0xA2,0x78,0xA6,0x6D,0x2A,0x13,0xF8, |
| 993 | 0x3F,0x44,0xF8,0x2D,0xDF,0x31,0x0E,0xE0,0x74,0xAB,0x6A,0x36, |
| 994 | 0x45,0x97,0xE8,0x99,0xA0,0x25,0x5D,0xC1,0x64,0xF3,0x1C,0xC5, |
| 995 | 0x08,0x46,0x85,0x1D,0xF9,0xAB,0x48,0x19,0x5D,0xED,0x7E,0xA1, |
| 996 | 0xB1,0xD5,0x10,0xBD,0x7E,0xE7,0x4D,0x73,0xFA,0xF3,0x6B,0xC3, |
| 997 | 0x1E,0xCF,0xA2,0x68,0x35,0x90,0x46,0xF4,0xEB,0x87,0x9F,0x92, |
| 998 | 0x40,0x09,0x43,0x8B,0x48,0x1C,0x6C,0xD7,0x88,0x9A,0x00,0x2E, |
| 999 | 0xD5,0xEE,0x38,0x2B,0xC9,0x19,0x0D,0xA6,0xFC,0x02,0x6E,0x47, |
| 1000 | 0x95,0x58,0xE4,0x47,0x56,0x77,0xE9,0xAA,0x9E,0x30,0x50,0xE2, |
| 1001 | 0x76,0x56,0x94,0xDF,0xC8,0x1F,0x56,0xE8,0x80,0xB9,0x6E,0x71, |
| 1002 | 0x60,0xC9,0x80,0xDD,0x98,0xED,0xD3,0xDF,0xFF,0xFF,0xFF,0xFF, |
| 1003 | 0xFF,0xFF,0xFF,0xFF, |
| 1004 | }; |
| 1005 | #endif |
| 1006 | DH *dh = DH_new(); |
| 1007 | if (dh) { |
| 1008 | #if OPENSSL_VERSION_NUMBER >= 0x0090801fL |
| 1009 | dh->p = get_rfc3526_prime_8192(NULL); |
| 1010 | #else |
| 1011 | dh->p = BN_bin2bn(rfc_3526_prime_8192, sizeof rfc_3526_prime_8192, NULL); |
| 1012 | #endif |
| 1013 | /* See RFC 3526, Section 7 "8192-bit MODP Group" |
| 1014 | for the reason why 2 is used as generator. |
| 1015 | */ |
| 1016 | BN_dec2bn(&dh->g, "2"); |
| 1017 | if (!dh->p || !dh->g) { |
| 1018 | DH_free(dh); |
| 1019 | dh = NULL; |
| 1020 | } |
| 1021 | } |
| 1022 | return dh; |
| 1023 | } |
| 1024 | |
| 1025 | /* Returns Diffie-Hellman parameters matching the private key length |
| 1026 | but not exceeding global.tune.ssl_default_dh_param */ |
| 1027 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 1028 | { |
| 1029 | DH *dh = NULL; |
| 1030 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
| 1031 | int type = pkey ? EVP_PKEY_type(pkey->type) : EVP_PKEY_NONE; |
| 1032 | |
| 1033 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 1034 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 1035 | */ |
| 1036 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 1037 | keylen = EVP_PKEY_bits(pkey); |
| 1038 | } |
| 1039 | |
| 1040 | if (keylen > global.tune.ssl_default_dh_param) { |
| 1041 | keylen = global.tune.ssl_default_dh_param; |
| 1042 | } |
| 1043 | |
| 1044 | if (keylen >= 8192) { |
Remi Gacogne | 60d7aeb | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1045 | dh = local_dh_8192; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1046 | } |
| 1047 | else if (keylen >= 4096) { |
Remi Gacogne | 60d7aeb | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1048 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1049 | } |
| 1050 | else if (keylen >= 2048) { |
Remi Gacogne | 60d7aeb | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1051 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1052 | } |
| 1053 | else { |
Remi Gacogne | 60d7aeb | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1054 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | return dh; |
| 1058 | } |
| 1059 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1060 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 1061 | if an error occured, and 0 if parameter not found. */ |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 1062 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1063 | { |
| 1064 | int ret = -1; |
| 1065 | BIO *in; |
| 1066 | DH *dh = NULL; |
| 1067 | |
| 1068 | in = BIO_new(BIO_s_file()); |
| 1069 | if (in == NULL) |
| 1070 | goto end; |
| 1071 | |
| 1072 | if (BIO_read_filename(in, file) <= 0) |
| 1073 | goto end; |
| 1074 | |
| 1075 | 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] | 1076 | if (dh) { |
| 1077 | ret = 1; |
| 1078 | SSL_CTX_set_tmp_dh(ctx, dh); |
| 1079 | /* Setting ssl default dh param to the size of the static DH params |
| 1080 | found in the file. This way we know that there is no use |
| 1081 | complaining later about ssl-default-dh-param not being set. */ |
| 1082 | global.tune.ssl_default_dh_param = DH_size(dh) * 8; |
| 1083 | } |
| 1084 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 1085 | /* Clear openssl global errors stack */ |
| 1086 | ERR_clear_error(); |
| 1087 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1088 | if (global.tune.ssl_default_dh_param <= 1024) { |
| 1089 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | 60d7aeb | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1090 | local_dh_1024 = ssl_get_dh_1024(); |
| 1091 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1092 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 1093 | |
Remi Gacogne | 60d7aeb | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1094 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1095 | } |
| 1096 | else { |
| 1097 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 1098 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 1099 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 1100 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1101 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1102 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1103 | end: |
| 1104 | if (dh) |
| 1105 | DH_free(dh); |
| 1106 | |
| 1107 | if (in) |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 1108 | BIO_free(in); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1109 | |
| 1110 | return ret; |
| 1111 | } |
| 1112 | #endif |
| 1113 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1114 | 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] | 1115 | { |
| 1116 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1117 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1118 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1119 | if (*name == '!') { |
| 1120 | neg = 1; |
| 1121 | name++; |
| 1122 | } |
| 1123 | if (*name == '*') { |
| 1124 | wild = 1; |
| 1125 | name++; |
| 1126 | } |
| 1127 | /* !* filter is a nop */ |
| 1128 | if (neg && wild) |
| 1129 | return order; |
| 1130 | if (*name) { |
| 1131 | int j, len; |
| 1132 | len = strlen(name); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1133 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
| 1134 | for (j = 0; j < len; j++) |
| 1135 | sc->name.key[j] = tolower(name[j]); |
| 1136 | sc->name.key[len] = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1137 | sc->ctx = ctx; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1138 | sc->order = order++; |
| 1139 | sc->neg = neg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1140 | if (wild) |
| 1141 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 1142 | else |
| 1143 | ebst_insert(&s->sni_ctx, &sc->name); |
| 1144 | } |
| 1145 | return order; |
| 1146 | } |
| 1147 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1148 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 1149 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 1150 | */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1151 | 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] | 1152 | { |
| 1153 | BIO *in; |
| 1154 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1155 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1156 | int ret = -1; |
| 1157 | int order = 0; |
| 1158 | X509_NAME *xname; |
| 1159 | char *str; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1160 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1161 | STACK_OF(GENERAL_NAME) *names; |
| 1162 | #endif |
| 1163 | |
| 1164 | in = BIO_new(BIO_s_file()); |
| 1165 | if (in == NULL) |
| 1166 | goto end; |
| 1167 | |
| 1168 | if (BIO_read_filename(in, file) <= 0) |
| 1169 | goto end; |
| 1170 | |
| 1171 | x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata); |
| 1172 | if (x == NULL) |
| 1173 | goto end; |
| 1174 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1175 | if (fcount) { |
| 1176 | while (fcount--) |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1177 | order = ssl_sock_add_cert_sni(ctx, s, sni_filter[fcount], order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1178 | } |
| 1179 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1180 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1181 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 1182 | if (names) { |
| 1183 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 1184 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 1185 | if (name->type == GEN_DNS) { |
| 1186 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1187 | order = ssl_sock_add_cert_sni(ctx, s, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1188 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1189 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1190 | } |
| 1191 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1192 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1193 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1194 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1195 | xname = X509_get_subject_name(x); |
| 1196 | i = -1; |
| 1197 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 1198 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
| 1199 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1200 | order = ssl_sock_add_cert_sni(ctx, s, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1201 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1202 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 1207 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 1208 | goto end; |
| 1209 | |
| 1210 | if (ctx->extra_certs != NULL) { |
| 1211 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 1212 | ctx->extra_certs = NULL; |
| 1213 | } |
| 1214 | |
| 1215 | while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata))) { |
| 1216 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 1217 | X509_free(ca); |
| 1218 | goto end; |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | err = ERR_get_error(); |
| 1223 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 1224 | /* we successfully reached the last cert in the file */ |
| 1225 | ret = 1; |
| 1226 | } |
| 1227 | ERR_clear_error(); |
| 1228 | |
| 1229 | end: |
| 1230 | if (x) |
| 1231 | X509_free(x); |
| 1232 | |
| 1233 | if (in) |
| 1234 | BIO_free(in); |
| 1235 | |
| 1236 | return ret; |
| 1237 | } |
| 1238 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1239 | 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] | 1240 | { |
| 1241 | int ret; |
| 1242 | SSL_CTX *ctx; |
| 1243 | |
| 1244 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 1245 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1246 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 1247 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1248 | return 1; |
| 1249 | } |
| 1250 | |
| 1251 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1252 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 1253 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1254 | SSL_CTX_free(ctx); |
| 1255 | return 1; |
| 1256 | } |
| 1257 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1258 | 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] | 1259 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1260 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 1261 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1262 | if (ret < 0) /* serious error, must do that ourselves */ |
| 1263 | SSL_CTX_free(ctx); |
| 1264 | return 1; |
| 1265 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 1266 | |
| 1267 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 1268 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 1269 | err && *err ? *err : "", path); |
| 1270 | return 1; |
| 1271 | } |
| 1272 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1273 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 1274 | * the tree, so it will be discovered and cleaned in time. |
| 1275 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1276 | #ifndef OPENSSL_NO_DH |
| 1277 | ret = ssl_sock_load_dh_params(ctx, path); |
| 1278 | if (ret < 0) { |
| 1279 | if (err) |
| 1280 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 1281 | *err ? *err : "", path); |
| 1282 | return 1; |
| 1283 | } |
| 1284 | #endif |
| 1285 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1286 | #ifdef SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB |
| 1287 | ret = ssl_sock_load_ocsp(ctx, path); |
| 1288 | if (ret < 0) { |
| 1289 | if (err) |
| 1290 | 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", |
| 1291 | *err ? *err : "", path); |
| 1292 | return 1; |
| 1293 | } |
| 1294 | #endif |
| 1295 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1296 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1297 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1298 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 1299 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1300 | return 1; |
| 1301 | } |
| 1302 | #endif |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1303 | if (!bind_conf->default_ctx) |
| 1304 | bind_conf->default_ctx = ctx; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1305 | |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1309 | 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] | 1310 | { |
| 1311 | struct dirent *de; |
| 1312 | DIR *dir; |
| 1313 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 1314 | char *end; |
| 1315 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1316 | int cfgerr = 0; |
| 1317 | |
| 1318 | if (!(dir = opendir(path))) |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1319 | 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] | 1320 | |
| 1321 | /* strip trailing slashes, including first one */ |
| 1322 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 1323 | *end = 0; |
| 1324 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1325 | while ((de = readdir(dir))) { |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 1326 | end = strrchr(de->d_name, '.'); |
| 1327 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp"))) |
| 1328 | continue; |
| 1329 | |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 1330 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1331 | if (stat(fp, &buf) != 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1332 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 1333 | err && *err ? *err : "", fp, strerror(errno)); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1334 | cfgerr++; |
| 1335 | continue; |
| 1336 | } |
| 1337 | if (!S_ISREG(buf.st_mode)) |
| 1338 | continue; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1339 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, curproxy, NULL, 0, err); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1340 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1341 | closedir(dir); |
| 1342 | return cfgerr; |
| 1343 | } |
| 1344 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 1345 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 1346 | * done once. Zero is returned if the operation fails. No error is returned |
| 1347 | * if the random is said as not implemented, because we expect that openssl |
| 1348 | * will use another method once needed. |
| 1349 | */ |
| 1350 | static int ssl_initialize_random() |
| 1351 | { |
| 1352 | unsigned char random; |
| 1353 | static int random_initialized = 0; |
| 1354 | |
| 1355 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 1356 | random_initialized = 1; |
| 1357 | |
| 1358 | return random_initialized; |
| 1359 | } |
| 1360 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1361 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 1362 | { |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1363 | char thisline[LINESIZE]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1364 | FILE *f; |
| 1365 | int linenum = 0; |
| 1366 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1367 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1368 | if ((f = fopen(file, "r")) == NULL) { |
| 1369 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1370 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1371 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1372 | |
| 1373 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 1374 | int arg; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1375 | int newarg; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1376 | char *end; |
| 1377 | char *args[MAX_LINE_ARGS + 1]; |
| 1378 | char *line = thisline; |
| 1379 | |
| 1380 | linenum++; |
| 1381 | end = line + strlen(line); |
| 1382 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 1383 | /* Check if we reached the limit and the last char is not \n. |
| 1384 | * Watch out for the last line without the terminating '\n'! |
| 1385 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1386 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 1387 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1388 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1389 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1392 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1393 | newarg = 1; |
| 1394 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1395 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 1396 | /* end of string, end of loop */ |
| 1397 | *line = 0; |
| 1398 | break; |
| 1399 | } |
| 1400 | else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1401 | newarg = 1; |
| 1402 | *line = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1403 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1404 | else if (newarg) { |
| 1405 | if (arg == MAX_LINE_ARGS) { |
| 1406 | memprintf(err, "too many args on line %d in file '%s'.", |
| 1407 | linenum, file); |
| 1408 | cfgerr = 1; |
| 1409 | break; |
| 1410 | } |
| 1411 | newarg = 0; |
| 1412 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1413 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1414 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1415 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 1416 | if (cfgerr) |
| 1417 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1418 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1419 | /* empty line */ |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1420 | if (!arg) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1421 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1422 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 1423 | 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] | 1424 | if (cfgerr) { |
| 1425 | 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] | 1426 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 1427 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1428 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 1429 | fclose(f); |
| 1430 | return cfgerr; |
| 1431 | } |
| 1432 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1433 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 1434 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 1435 | #endif |
| 1436 | |
| 1437 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 1438 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
Willy Tarreau | 7d588ee | 2012-11-26 18:47:31 +0100 | [diff] [blame] | 1439 | #define SSL_renegotiate_pending(arg) 0 |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1440 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1441 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 1442 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1443 | #endif |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 1444 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 1445 | #define SSL_OP_NO_TICKET 0 |
| 1446 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1447 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 1448 | #define SSL_OP_NO_COMPRESSION 0 |
| 1449 | #endif |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 1450 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 1451 | #define SSL_OP_NO_TLSv1_1 0 |
| 1452 | #endif |
| 1453 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 1454 | #define SSL_OP_NO_TLSv1_2 0 |
| 1455 | #endif |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1456 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 1457 | #define SSL_OP_SINGLE_DH_USE 0 |
| 1458 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1459 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 1460 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 1461 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1462 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 1463 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 1464 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1465 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1466 | 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] | 1467 | { |
| 1468 | int cfgerr = 0; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1469 | int verify = SSL_VERIFY_NONE; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 1470 | long ssloptions = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1471 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 1472 | SSL_OP_NO_SSLv2 | |
| 1473 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 1474 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1475 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 1476 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
| 1477 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 1478 | long sslmode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1479 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 1480 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 1481 | SSL_MODE_RELEASE_BUFFERS; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1482 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 1483 | SSL_CIPHER * cipher = NULL; |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 1484 | char cipher_description[128]; |
| 1485 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 1486 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 1487 | which is not ephemeral DH. */ |
| 1488 | const char dhe_description[] = " Kx=DH "; |
| 1489 | const char dhe_export_description[] = " Kx=DH("; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1490 | int idx = 0; |
| 1491 | int dhe_found = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1492 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 1493 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 1494 | if (!ssl_initialize_random()) { |
| 1495 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 1496 | cfgerr++; |
| 1497 | } |
| 1498 | |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1499 | if (bind_conf->ssl_options & BC_SSL_O_NO_SSLV3) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1500 | ssloptions |= SSL_OP_NO_SSLv3; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1501 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV10) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1502 | ssloptions |= SSL_OP_NO_TLSv1; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1503 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV11) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 1504 | ssloptions |= SSL_OP_NO_TLSv1_1; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1505 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV12) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 1506 | ssloptions |= SSL_OP_NO_TLSv1_2; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1507 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 1508 | ssloptions |= SSL_OP_NO_TICKET; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 1509 | if (bind_conf->ssl_options & BC_SSL_O_USE_SSLV3) |
| 1510 | SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()); |
| 1511 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV10) |
| 1512 | SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()); |
| 1513 | #if SSL_OP_NO_TLSv1_1 |
| 1514 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV11) |
| 1515 | SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method()); |
| 1516 | #endif |
| 1517 | #if SSL_OP_NO_TLSv1_2 |
| 1518 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV12) |
| 1519 | SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method()); |
| 1520 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1521 | |
| 1522 | SSL_CTX_set_options(ctx, ssloptions); |
| 1523 | SSL_CTX_set_mode(ctx, sslmode); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1524 | switch (bind_conf->verify) { |
| 1525 | case SSL_SOCK_VERIFY_NONE: |
| 1526 | verify = SSL_VERIFY_NONE; |
| 1527 | break; |
| 1528 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 1529 | verify = SSL_VERIFY_PEER; |
| 1530 | break; |
| 1531 | case SSL_SOCK_VERIFY_REQUIRED: |
| 1532 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1533 | break; |
| 1534 | } |
| 1535 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 1536 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1537 | if (bind_conf->ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1538 | /* load CAfile to verify */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1539 | if (!SSL_CTX_load_verify_locations(ctx, bind_conf->ca_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1540 | 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] | 1541 | 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] | 1542 | cfgerr++; |
| 1543 | } |
| 1544 | /* set CA names fo client cert request, function returns void */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1545 | 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] | 1546 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1547 | else { |
| 1548 | Alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 1549 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 1550 | cfgerr++; |
| 1551 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 1552 | #ifdef X509_V_FLAG_CRL_CHECK |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1553 | if (bind_conf->crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1554 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 1555 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1556 | if (!store || !X509_STORE_load_locations(store, bind_conf->crl_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1557 | Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1558 | 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] | 1559 | cfgerr++; |
| 1560 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 1561 | else { |
| 1562 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 1563 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1564 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 1565 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 1566 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1567 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1568 | |
Emeric Brun | 4f65bff | 2012-11-16 15:11:00 +0100 | [diff] [blame] | 1569 | if (global.tune.ssllifetime) |
| 1570 | SSL_CTX_set_timeout(ctx, global.tune.ssllifetime); |
| 1571 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1572 | shared_context_set_cache(ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1573 | if (bind_conf->ciphers && |
| 1574 | !SSL_CTX_set_cipher_list(ctx, bind_conf->ciphers)) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1575 | 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] | 1576 | 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] | 1577 | cfgerr++; |
| 1578 | } |
| 1579 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1580 | /* If tune.ssl.default-dh-param has not been set and |
| 1581 | no static DH params were in the certificate file. */ |
| 1582 | if (global.tune.ssl_default_dh_param == 0) { |
| 1583 | ciphers = ctx->cipher_list; |
| 1584 | |
| 1585 | if (ciphers) { |
| 1586 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 1587 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 1588 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 1589 | if (strstr(cipher_description, dhe_description) != NULL || |
| 1590 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 1591 | dhe_found = 1; |
| 1592 | break; |
| 1593 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | if (dhe_found) { |
| 1598 | 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"); |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | global.tune.ssl_default_dh_param = 1024; |
| 1603 | } |
Remi Gacogne | 60d7aeb | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 1604 | |
| 1605 | #ifndef OPENSSL_NO_DH |
| 1606 | if (global.tune.ssl_default_dh_param >= 1024) { |
| 1607 | if (local_dh_1024 == NULL) { |
| 1608 | local_dh_1024 = ssl_get_dh_1024(); |
| 1609 | } |
| 1610 | if (global.tune.ssl_default_dh_param >= 2048) { |
| 1611 | if (local_dh_2048 == NULL) { |
| 1612 | local_dh_2048 = ssl_get_dh_2048(); |
| 1613 | } |
| 1614 | if (global.tune.ssl_default_dh_param >= 4096) { |
| 1615 | if (local_dh_4096 == NULL) { |
| 1616 | local_dh_4096 = ssl_get_dh_4096(); |
| 1617 | } |
| 1618 | if (global.tune.ssl_default_dh_param >= 8192 && |
| 1619 | local_dh_8192 == NULL) { |
| 1620 | local_dh_8192 = ssl_get_dh_8192(); |
| 1621 | } |
| 1622 | } |
| 1623 | } |
| 1624 | } |
| 1625 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 1626 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1627 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 1628 | #if OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1629 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 1630 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1631 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1632 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 1633 | if (bind_conf->npn_str) |
| 1634 | SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, bind_conf); |
| 1635 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1636 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1637 | if (bind_conf->alpn_str) |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1638 | 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] | 1639 | #endif |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1640 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1641 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1642 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1643 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1644 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1645 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emeric Brun | 6924ef8 | 2013-03-06 14:08:53 +0100 | [diff] [blame] | 1646 | { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1647 | int i; |
| 1648 | EC_KEY *ecdh; |
| 1649 | |
Emeric Brun | 6924ef8 | 2013-03-06 14:08:53 +0100 | [diff] [blame] | 1650 | i = OBJ_sn2nid(bind_conf->ecdhe ? bind_conf->ecdhe : ECDHE_DEFAULT_CURVE); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1651 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
| 1652 | 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] | 1653 | curproxy->id, bind_conf->ecdhe ? bind_conf->ecdhe : ECDHE_DEFAULT_CURVE, |
| 1654 | bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1655 | cfgerr++; |
| 1656 | } |
| 1657 | else { |
| 1658 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 1659 | EC_KEY_free(ecdh); |
| 1660 | } |
| 1661 | } |
| 1662 | #endif |
| 1663 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1664 | return cfgerr; |
| 1665 | } |
| 1666 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1667 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 1668 | { |
| 1669 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 1670 | size_t prefixlen, suffixlen; |
| 1671 | |
| 1672 | /* Trivial case */ |
| 1673 | if (strcmp(pattern, hostname) == 0) |
| 1674 | return 1; |
| 1675 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1676 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 1677 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 1678 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 1679 | pattern_wildcard = NULL; |
| 1680 | pattern_left_label_end = pattern; |
| 1681 | while (*pattern_left_label_end != '.') { |
| 1682 | switch (*pattern_left_label_end) { |
| 1683 | case 0: |
| 1684 | /* End of label not found */ |
| 1685 | return 0; |
| 1686 | case '*': |
| 1687 | /* If there is more than one wildcards */ |
| 1688 | if (pattern_wildcard) |
| 1689 | return 0; |
| 1690 | pattern_wildcard = pattern_left_label_end; |
| 1691 | break; |
| 1692 | } |
| 1693 | pattern_left_label_end++; |
| 1694 | } |
| 1695 | |
| 1696 | /* If it's not trivial and there is no wildcard, it can't |
| 1697 | * match */ |
| 1698 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1699 | return 0; |
| 1700 | |
| 1701 | /* Make sure all labels match except the leftmost */ |
| 1702 | hostname_left_label_end = strchr(hostname, '.'); |
| 1703 | if (!hostname_left_label_end |
| 1704 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 1705 | return 0; |
| 1706 | |
| 1707 | /* Make sure the leftmost label of the hostname is long enough |
| 1708 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 1709 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1710 | return 0; |
| 1711 | |
| 1712 | /* Finally compare the string on either side of the |
| 1713 | * wildcard */ |
| 1714 | prefixlen = pattern_wildcard - pattern; |
| 1715 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 1716 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 1717 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1718 | return 0; |
| 1719 | |
| 1720 | return 1; |
| 1721 | } |
| 1722 | |
| 1723 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 1724 | { |
| 1725 | SSL *ssl; |
| 1726 | struct connection *conn; |
| 1727 | char *servername; |
| 1728 | |
| 1729 | int depth; |
| 1730 | X509 *cert; |
| 1731 | STACK_OF(GENERAL_NAME) *alt_names; |
| 1732 | int i; |
| 1733 | X509_NAME *cert_subject; |
| 1734 | char *str; |
| 1735 | |
| 1736 | if (ok == 0) |
| 1737 | return ok; |
| 1738 | |
| 1739 | ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 1740 | conn = (struct connection *)SSL_get_app_data(ssl); |
| 1741 | |
| 1742 | servername = objt_server(conn->target)->ssl_ctx.verify_host; |
| 1743 | |
| 1744 | /* We only need to verify the CN on the actual server cert, |
| 1745 | * not the indirect CAs */ |
| 1746 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 1747 | if (depth != 0) |
| 1748 | return ok; |
| 1749 | |
| 1750 | /* At this point, the cert is *not* OK unless we can find a |
| 1751 | * hostname match */ |
| 1752 | ok = 0; |
| 1753 | |
| 1754 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 1755 | /* It seems like this might happen if verify peer isn't set */ |
| 1756 | if (!cert) |
| 1757 | return ok; |
| 1758 | |
| 1759 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 1760 | if (alt_names) { |
| 1761 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 1762 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 1763 | if (name->type == GEN_DNS) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 1764 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 1765 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 1766 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1767 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 1768 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1769 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 1770 | OPENSSL_free(str); |
| 1771 | } |
| 1772 | } |
| 1773 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 1774 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | cert_subject = X509_get_subject_name(cert); |
| 1778 | i = -1; |
| 1779 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 1780 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
| 1781 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) { |
| 1782 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 1783 | OPENSSL_free(str); |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | return ok; |
| 1788 | } |
| 1789 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1790 | /* prepare ssl context from servers options. Returns an error count */ |
| 1791 | int ssl_sock_prepare_srv_ctx(struct server *srv, struct proxy *curproxy) |
| 1792 | { |
| 1793 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 1794 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1795 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 1796 | SSL_OP_NO_SSLv2 | |
| 1797 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 1798 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1799 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 1800 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 1801 | SSL_MODE_RELEASE_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1802 | int verify = SSL_VERIFY_NONE; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1803 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 1804 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 1805 | if (!ssl_initialize_random()) { |
| 1806 | Alert("OpenSSL random data generator initialization failed.\n"); |
| 1807 | cfgerr++; |
| 1808 | } |
| 1809 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1810 | /* Initiate SSL context for current server */ |
| 1811 | srv->ssl_ctx.reused_sess = NULL; |
| 1812 | if (srv->use_ssl) |
| 1813 | srv->xprt = &ssl_sock; |
| 1814 | if (srv->check.use_ssl) |
Simon Horman | 6618300 | 2013-02-23 10:16:43 +0900 | [diff] [blame] | 1815 | srv->check_common.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1816 | |
| 1817 | srv->ssl_ctx.ctx = SSL_CTX_new(SSLv23_client_method()); |
| 1818 | if (!srv->ssl_ctx.ctx) { |
| 1819 | Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 1820 | proxy_type_str(curproxy), curproxy->id, |
| 1821 | srv->id); |
| 1822 | cfgerr++; |
| 1823 | return cfgerr; |
| 1824 | } |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 1825 | if (srv->ssl_ctx.client_crt) { |
| 1826 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
| 1827 | Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 1828 | proxy_type_str(curproxy), curproxy->id, |
| 1829 | srv->id, srv->ssl_ctx.client_crt); |
| 1830 | cfgerr++; |
| 1831 | } |
| 1832 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
| 1833 | Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 1834 | proxy_type_str(curproxy), curproxy->id, |
| 1835 | srv->id, srv->ssl_ctx.client_crt); |
| 1836 | cfgerr++; |
| 1837 | } |
| 1838 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
| 1839 | Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 1840 | proxy_type_str(curproxy), curproxy->id, |
| 1841 | srv->id, srv->ssl_ctx.client_crt); |
| 1842 | cfgerr++; |
| 1843 | } |
| 1844 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1845 | |
| 1846 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_SSLV3) |
| 1847 | options |= SSL_OP_NO_SSLv3; |
| 1848 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV10) |
| 1849 | options |= SSL_OP_NO_TLSv1; |
| 1850 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV11) |
| 1851 | options |= SSL_OP_NO_TLSv1_1; |
| 1852 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV12) |
| 1853 | options |= SSL_OP_NO_TLSv1_2; |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 1854 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 1855 | options |= SSL_OP_NO_TICKET; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1856 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_SSLV3) |
| 1857 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, SSLv3_client_method()); |
| 1858 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV10) |
| 1859 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_client_method()); |
| 1860 | #if SSL_OP_NO_TLSv1_1 |
| 1861 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV11) |
| 1862 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_1_client_method()); |
| 1863 | #endif |
| 1864 | #if SSL_OP_NO_TLSv1_2 |
| 1865 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV12) |
| 1866 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_2_client_method()); |
| 1867 | #endif |
| 1868 | |
| 1869 | SSL_CTX_set_options(srv->ssl_ctx.ctx, options); |
| 1870 | SSL_CTX_set_mode(srv->ssl_ctx.ctx, mode); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1871 | |
| 1872 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 1873 | verify = SSL_VERIFY_PEER; |
| 1874 | |
| 1875 | switch (srv->ssl_ctx.verify) { |
| 1876 | case SSL_SOCK_VERIFY_NONE: |
| 1877 | verify = SSL_VERIFY_NONE; |
| 1878 | break; |
| 1879 | case SSL_SOCK_VERIFY_REQUIRED: |
| 1880 | verify = SSL_VERIFY_PEER; |
| 1881 | break; |
| 1882 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1883 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1884 | verify, |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1885 | srv->ssl_ctx.verify_host ? ssl_sock_srv_verifycbk : NULL); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1886 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 1887 | if (srv->ssl_ctx.ca_file) { |
| 1888 | /* load CAfile to verify */ |
| 1889 | 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] | 1890 | 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] | 1891 | curproxy->id, srv->id, |
| 1892 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
| 1893 | cfgerr++; |
| 1894 | } |
| 1895 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 1896 | else { |
| 1897 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 1898 | 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] | 1899 | curproxy->id, srv->id, |
| 1900 | srv->conf.file, srv->conf.line); |
| 1901 | else |
Willy Tarreau | 07ba08b | 2014-02-16 19:22:08 +0100 | [diff] [blame] | 1902 | 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] | 1903 | curproxy->id, srv->id, |
| 1904 | srv->conf.file, srv->conf.line); |
| 1905 | cfgerr++; |
| 1906 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 1907 | #ifdef X509_V_FLAG_CRL_CHECK |
| 1908 | if (srv->ssl_ctx.crl_file) { |
| 1909 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 1910 | |
| 1911 | 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] | 1912 | 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] | 1913 | curproxy->id, srv->id, |
| 1914 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
| 1915 | cfgerr++; |
| 1916 | } |
| 1917 | else { |
| 1918 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 1919 | } |
| 1920 | } |
| 1921 | #endif |
| 1922 | } |
| 1923 | |
Emeric Brun | 4f65bff | 2012-11-16 15:11:00 +0100 | [diff] [blame] | 1924 | if (global.tune.ssllifetime) |
| 1925 | SSL_CTX_set_timeout(srv->ssl_ctx.ctx, global.tune.ssllifetime); |
| 1926 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 1927 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF); |
| 1928 | if (srv->ssl_ctx.ciphers && |
| 1929 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
| 1930 | Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 1931 | curproxy->id, srv->id, |
| 1932 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
| 1933 | cfgerr++; |
| 1934 | } |
| 1935 | |
| 1936 | return cfgerr; |
| 1937 | } |
| 1938 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1939 | /* 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] | 1940 | * be NULL, in which case nothing is done. Returns the number of errors |
| 1941 | * encountered. |
| 1942 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1943 | 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] | 1944 | { |
| 1945 | struct ebmb_node *node; |
| 1946 | struct sni_ctx *sni; |
| 1947 | int err = 0; |
| 1948 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1949 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1950 | return 0; |
| 1951 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1952 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1953 | while (node) { |
| 1954 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 1955 | if (!sni->order) /* only initialize the CTX on its first occurrence */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1956 | err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1957 | node = ebmb_next(node); |
| 1958 | } |
| 1959 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1960 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1961 | while (node) { |
| 1962 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 1963 | if (!sni->order) /* only initialize the CTX on its first occurrence */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 1964 | err += ssl_sock_prepare_ctx(bind_conf, sni-> |