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