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