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