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