blob: ad4b1caaa3544e475383f3428f5e746fbf582b2b [file] [log] [blame]
Emeric Brun46591952012-05-18 15:47:34 +02001/*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002 * SSL/TLS transport layer over SOCK_STREAM sockets
Emeric Brun46591952012-05-18 15:47:34 +02003 *
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 Tarreau69845df2012-09-10 09:43:09 +020011 * 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 Brun46591952012-05-18 15:47:34 +020023 */
24
25#define _GNU_SOURCE
Emeric Brunfc0421f2012-09-07 17:30:07 +020026#include <ctype.h>
27#include <dirent.h>
Emeric Brun46591952012-05-18 15:47:34 +020028#include <errno.h>
29#include <fcntl.h>
30#include <stdio.h>
31#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020032#include <string.h>
33#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020034
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 Brunfc0421f2012-09-07 17:30:07 +020042#include <openssl/x509.h>
43#include <openssl/x509v3.h>
44#include <openssl/x509.h>
45#include <openssl/err.h>
Thierry Fournier383085f2013-01-24 14:15:43 +010046#include <openssl/rand.h>
Emeric Brun4147b2e2014-06-16 18:36:30 +020047#ifdef SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB
48#include <openssl/ocsp.h>
49#endif
Emeric Brun46591952012-05-18 15:47:34 +020050
51#include <common/buffer.h>
52#include <common/compat.h>
53#include <common/config.h>
54#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020055#include <common/errors.h>
Emeric Brun46591952012-05-18 15:47:34 +020056#include <common/standard.h>
57#include <common/ticks.h>
58#include <common/time.h>
59
Emeric Brunfc0421f2012-09-07 17:30:07 +020060#include <ebsttree.h>
61
62#include <types/global.h>
63#include <types/ssl_sock.h>
64
Willy Tarreau7875d092012-09-10 08:20:03 +020065#include <proto/acl.h>
66#include <proto/arg.h>
Emeric Brun46591952012-05-18 15:47:34 +020067#include <proto/connection.h>
68#include <proto/fd.h>
69#include <proto/freq_ctr.h>
70#include <proto/frontend.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020071#include <proto/listener.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010072#include <proto/pattern.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020073#include <proto/server.h>
Emeric Brun46591952012-05-18 15:47:34 +020074#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020075#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020076#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020077#include <proto/ssl_sock.h>
78#include <proto/task.h>
79
Willy Tarreau518cedd2014-02-17 15:43:01 +010080/* Warning, these are bits, not integers! */
Emeric Brune64aef12012-09-21 13:15:06 +020081#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brund8b2bb52014-01-28 15:43:53 +010082#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
Willy Tarreau518cedd2014-02-17 15:43:01 +010083#define SSL_SOCK_SEND_UNLIMITED 0x00000004
Emeric Brun29f037d2014-04-25 19:05:36 +020084#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
85
Emeric Brunf282a812012-09-21 15:27:54 +020086/* 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 Brune64aef12012-09-21 13:15:06 +020096
Emeric Brun850efd52014-01-29 12:24:34 +010097/* server and bind verify method, it uses a global value as default */
98enum {
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 Tarreau71b734c2014-01-28 15:19:44 +0100105int sslconns = 0;
106int totalsslconns = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200107
Emeric Brun4147b2e2014-06-16 18:36:30 +0200108#ifdef SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB
109struct certificate_ocsp {
110 struct ebmb_node key;
111 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
112 struct chunk response;
113
114};
115
116static 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 */
135static 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 Brunc8b27b62014-06-19 14:16:17 +0200182 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200183 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;
228out:
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 */
244int 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 */
255static 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);
287end:
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 */
297int 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 */
333static 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
427out:
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 Brune1f38db2012-09-03 20:36:47 +0200452void 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 Brund8b2bb52014-01-28 15:43:53 +0100456 BIO *write_bio;
Emeric Brune1f38db2012-09-03 20:36:47 +0200457
458 if (where & SSL_CB_HANDSHAKE_START) {
459 /* Disable renegotiation (CVE-2009-3555) */
Willy Tarreau20879a02012-12-03 16:32:10 +0100460 if (conn->flags & CO_FL_CONNECTED) {
Emeric Brune1f38db2012-09-03 20:36:47 +0200461 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +0100462 conn->err_code = CO_ER_SSL_RENEG;
463 }
Emeric Brune1f38db2012-09-03 20:36:47 +0200464 }
Emeric Brund8b2bb52014-01-28 15:43:53 +0100465
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 Brunfc0421f2012-09-07 17:30:07 +0200480}
481
Emeric Brune64aef12012-09-21 13:15:06 +0200482/* 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 Broderbe554312013-06-27 00:05:25 -0700485int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +0200486{
487 SSL *ssl;
488 struct connection *conn;
Emeric Brun81c00f02012-09-21 14:31:21 +0200489 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +0200490
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 Tarreauf7bc57c2012-10-03 00:19:48 +0200494 conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +0200495
Emeric Brun81c00f02012-09-21 14:31:21 +0200496 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 Tarreauf7bc57c2012-10-03 00:19:48 +0200504 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 Brunf282a812012-09-21 15:27:54 +0200507 }
508
Emeric Brun1eb20ef2012-12-03 13:24:29 +0100509 if (objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
510 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +0200511 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +0100512 }
Emeric Brun81c00f02012-09-21 14:31:21 +0200513
Willy Tarreau20879a02012-12-03 16:32:10 +0100514 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +0200515 return 0;
516 }
517
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200518 if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st))
519 conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +0200520
Emeric Brun81c00f02012-09-21 14:31:21 +0200521 /* check if certificate error needs to be ignored */
Emeric Brun1eb20ef2012-12-03 13:24:29 +0100522 if (objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
523 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +0200524 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +0100525 }
Emeric Brun81c00f02012-09-21 14:31:21 +0200526
Willy Tarreau20879a02012-12-03 16:32:10 +0100527 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +0200528 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +0200529}
530
Emeric Brun29f037d2014-04-25 19:05:36 +0200531/* Callback is called for ssl protocol analyse */
532void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
533{
Emeric Brun29f037d2014-04-25 19:05:36 +0200534#ifdef TLS1_RT_HEARTBEAT
535 /* test heartbeat received (write_p is set to 0
536 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +0200537 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Willy Tarreau84815002014-04-25 21:40:27 +0200538 struct connection *conn = (struct connection *)SSL_get_app_data(ssl);
Willy Tarreauf51c6982014-04-25 20:02:39 +0200539 const unsigned char *p = buf;
540 unsigned int payload;
541
Emeric Brun29f037d2014-04-25 19:05:36 +0200542 conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +0200543
544 /* Check if this is a CVE-2014-0160 exploitation attempt. */
545 if (*p != TLS1_HB_REQUEST)
546 return;
547
Willy Tarreauaeed6722014-04-25 23:59:58 +0200548 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +0200549 goto kill_it;
550
551 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +0200552 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +0200553 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +0200554 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +0200555 /* 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 Tarreauf51c6982014-04-25 20:02:39 +0200565 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
566 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +0200567 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +0200568 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
569 return;
570 }
Emeric Brun29f037d2014-04-25 19:05:36 +0200571#endif
572}
573
Willy Tarreau6c9a3d52012-10-18 18:57:14 +0200574#ifdef OPENSSL_NPN_NEGOTIATED
575/* This callback is used so that the server advertises the list of
576 * negociable protocols for NPN.
577 */
578static 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 Bussink48f1c4e2014-02-13 12:29:42 +0100589#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +0200590/* This callback is used so that the server advertises the list of
591 * negociable protocols for ALPN.
592 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +0100593static 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 Tarreauab861d32013-04-02 02:30:41 +0200597{
598 struct bind_conf *conf = arg;
599
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +0100600 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 Tarreauab861d32013-04-02 02:30:41 +0200604 return SSL_TLSEXT_ERR_OK;
605}
606#endif
607
Emeric Brunfc0421f2012-09-07 17:30:07 +0200608#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 Tarreau2a65ff02012-09-13 17:54:29 +0200613static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200614{
615 const char *servername;
616 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +0200617 struct ebmb_node *node, *n;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200618 int i;
619 (void)al; /* shut gcc stupid warning */
620
621 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +0100622 if (!servername) {
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +0200623 return (s->strict_sni ?
624 SSL_TLSEXT_ERR_ALERT_FATAL :
Emmanuel Hocdet79274e22013-05-31 12:47:44 +0200625 SSL_TLSEXT_ERR_NOACK);
Emmanuel Hocdet65623372013-01-24 17:17:15 +0100626 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200627
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100628 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +0200629 if (!servername[i])
630 break;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100631 trash.str[i] = tolower(servername[i]);
632 if (!wildp && (trash.str[i] == '.'))
633 wildp = &trash.str[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +0200634 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100635 trash.str[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200636
637 /* lookup in full qualified names */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100638 node = ebst_lookup(&s->sni_ctx, trash.str);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +0200639
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 Hocdet65623372013-01-24 17:17:15 +0100645 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +0200646 }
647 if (!node && wildp) {
648 /* lookup in wildcards names */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200649 node = ebst_lookup(&s->sni_w_ctx, wildp);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +0200650 }
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 Brunfc0421f2012-09-07 17:30:07 +0200655 }
656
657 /* switch ctx */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +0200658 SSL_set_SSL_CTX(ssl, container_of(node, struct sni_ctx, name)->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200659 return SSL_TLSEXT_ERR_OK;
660}
661#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
662
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200663#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +0200664
665static 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
701static 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
748static 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
816static 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 */
929static 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 Bruna4bcd9a2012-09-20 16:19:02 +0200962/* 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 Tarreau6e774b42014-04-25 21:35:23 +0200964int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200965{
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 Gacognef46cd6e2014-06-12 14:58:40 +0200978 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 Brun41fdb3c2013-04-26 11:05:44 +0200987 /* Clear openssl global errors stack */
988 ERR_clear_error();
989
Remi Gacognef46cd6e2014-06-12 14:58:40 +0200990 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 Tarreau6e774b42014-04-25 21:35:23 +0200995
Remi Gacognef46cd6e2014-06-12 14:58:40 +0200996 SSL_CTX_set_tmp_dh(ctx, dh);
997 }
998 else {
999 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
1000 }
Willy Tarreau6e774b42014-04-25 21:35:23 +02001001
Emeric Brun41fdb3c2013-04-26 11:05:44 +02001002 ret = 0; /* DH params not found */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02001003 }
Emeric Brun644cde02012-12-14 11:21:13 +01001004
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02001005end:
1006 if (dh)
1007 DH_free(dh);
1008
1009 if (in)
Emeric Brun41fdb3c2013-04-26 11:05:44 +02001010 BIO_free(in);
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02001011
1012 return ret;
1013}
1014#endif
1015
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02001016static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001017{
1018 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02001019 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001020
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02001021 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 Hocdetfe616562013-01-22 15:31:15 +01001035 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 Hocdetfe616562013-01-22 15:31:15 +01001039 sc->ctx = ctx;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02001040 sc->order = order++;
1041 sc->neg = neg;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001042 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 Brunfc0421f2012-09-07 17:30:07 +02001050/* 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 Hocdet7c41a1b2013-05-07 20:20:06 +02001053static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, char **sni_filter, int fcount)
Emeric Brunfc0421f2012-09-07 17:30:07 +02001054{
1055 BIO *in;
1056 X509 *x = NULL, *ca;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02001057 int i, err;
Emeric Brunfc0421f2012-09-07 17:30:07 +02001058 int ret = -1;
1059 int order = 0;
1060 X509_NAME *xname;
1061 char *str;
Emeric Brunfc0421f2012-09-07 17:30:07 +02001062#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 Brun50bcecc2013-04-22 13:05:23 +02001077 if (fcount) {
1078 while (fcount--)
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02001079 order = ssl_sock_add_cert_sni(ctx, s, sni_filter[fcount], order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001080 }
1081 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02001082#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001083 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 Hocdet7c41a1b2013-05-07 20:20:06 +02001089 order = ssl_sock_add_cert_sni(ctx, s, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001090 OPENSSL_free(str);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001091 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001092 }
1093 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001094 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001095 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001096#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001097 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 Hocdet7c41a1b2013-05-07 20:20:06 +02001102 order = ssl_sock_add_cert_sni(ctx, s, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001103 OPENSSL_free(str);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001104 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001105 }
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
1131end:
1132 if (x)
1133 X509_free(x);
1134
1135 if (in)
1136 BIO_free(in);
1137
1138 return ret;
1139}
1140
Emeric Brun50bcecc2013-04-22 13:05:23 +02001141static 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 Brunfc0421f2012-09-07 17:30:07 +02001142{
1143 int ret;
1144 SSL_CTX *ctx;
1145
1146 ctx = SSL_CTX_new(SSLv23_server_method());
1147 if (!ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001148 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
1149 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001150 return 1;
1151 }
1152
1153 if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001154 memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
1155 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001156 SSL_CTX_free(ctx);
1157 return 1;
1158 }
1159
Emeric Brun50bcecc2013-04-22 13:05:23 +02001160 ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf, sni_filter, fcount);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001161 if (ret <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001162 memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
1163 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001164 if (ret < 0) /* serious error, must do that ourselves */
1165 SSL_CTX_free(ctx);
1166 return 1;
1167 }
Emeric Brun61694ab2012-10-26 13:35:33 +02001168
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 Brunfc0421f2012-09-07 17:30:07 +02001175 /* 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 Bruna4bcd9a2012-09-20 16:19:02 +02001178#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 Brun4147b2e2014-06-16 18:36:30 +02001188#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 Brunfc0421f2012-09-07 17:30:07 +02001198#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001199 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001200 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
1201 err && *err ? *err : "");
Emeric Brunfc0421f2012-09-07 17:30:07 +02001202 return 1;
1203 }
1204#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001205 if (!bind_conf->default_ctx)
1206 bind_conf->default_ctx = ctx;
Emeric Brunfc0421f2012-09-07 17:30:07 +02001207
1208 return 0;
1209}
1210
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001211int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02001212{
1213 struct dirent *de;
1214 DIR *dir;
1215 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01001216 char *end;
1217 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02001218 int cfgerr = 0;
1219
1220 if (!(dir = opendir(path)))
Emeric Brun50bcecc2013-04-22 13:05:23 +02001221 return ssl_sock_load_cert_file(path, bind_conf, curproxy, NULL, 0, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001222
1223 /* strip trailing slashes, including first one */
1224 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
1225 *end = 0;
1226
Emeric Brunfc0421f2012-09-07 17:30:07 +02001227 while ((de = readdir(dir))) {
Emeric Brun2aab7222014-06-18 18:15:09 +02001228 end = strrchr(de->d_name, '.');
1229 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp")))
1230 continue;
1231
Willy Tarreauee2663b2012-12-06 11:36:59 +01001232 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001233 if (stat(fp, &buf) != 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001234 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
1235 err && *err ? *err : "", fp, strerror(errno));
Emeric Brunfc0421f2012-09-07 17:30:07 +02001236 cfgerr++;
1237 continue;
1238 }
1239 if (!S_ISREG(buf.st_mode))
1240 continue;
Emeric Brun50bcecc2013-04-22 13:05:23 +02001241 cfgerr += ssl_sock_load_cert_file(fp, bind_conf, curproxy, NULL, 0, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001242 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001243 closedir(dir);
1244 return cfgerr;
1245}
1246
Thierry Fournier383085f2013-01-24 14:15:43 +01001247/* 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 */
1252static 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 Hocdetfe616562013-01-22 15:31:15 +01001263int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
1264{
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02001265 char thisline[LINESIZE];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001266 FILE *f;
1267 int linenum = 0;
1268 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001269
Willy Tarreauad1731d2013-04-02 17:35:58 +02001270 if ((f = fopen(file, "r")) == NULL) {
1271 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001272 return 1;
Willy Tarreauad1731d2013-04-02 17:35:58 +02001273 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001274
1275 while (fgets(thisline, sizeof(thisline), f) != NULL) {
1276 int arg;
Emeric Brun50bcecc2013-04-22 13:05:23 +02001277 int newarg;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001278 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 Tarreauad1731d2013-04-02 17:35:58 +02001288 memprintf(err, "line %d too long in file '%s', limit is %d characters",
1289 linenum, file, (int)sizeof(thisline)-1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001290 cfgerr = 1;
Willy Tarreauad1731d2013-04-02 17:35:58 +02001291 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001292 }
1293
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001294 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02001295 newarg = 1;
1296 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001297 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 Brun50bcecc2013-04-22 13:05:23 +02001303 newarg = 1;
1304 *line = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001305 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02001306 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 Hocdetfe616562013-01-22 15:31:15 +01001315 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02001316 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001317 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02001318 if (cfgerr)
1319 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02001320
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001321 /* empty line */
Emeric Brun50bcecc2013-04-22 13:05:23 +02001322 if (!arg)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001323 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001324
Emeric Brun50bcecc2013-04-22 13:05:23 +02001325 cfgerr = ssl_sock_load_cert_file(args[0], bind_conf, curproxy, &args[1], arg-1, err);
Willy Tarreauad1731d2013-04-02 17:35:58 +02001326 if (cfgerr) {
1327 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001328 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02001329 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001330 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01001331 fclose(f);
1332 return cfgerr;
1333}
1334
Emeric Brunfc0421f2012-09-07 17:30:07 +02001335#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 Tarreau7d588ee2012-11-26 18:47:31 +01001341#define SSL_renegotiate_pending(arg) 0
Emeric Brunfc0421f2012-09-07 17:30:07 +02001342#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02001343#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */
1344#define SSL_OP_SINGLE_ECDH_USE 0
1345#endif
Emeric Brun2d0c4822012-10-02 13:45:20 +02001346#ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */
1347#define SSL_OP_NO_TICKET 0
1348#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +02001349#ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */
1350#define SSL_OP_NO_COMPRESSION 0
1351#endif
Emeric Brunc0ff4922012-09-28 19:37:02 +02001352#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 Bruna4bcd9a2012-09-20 16:19:02 +02001358#ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */
1359#define SSL_OP_SINGLE_DH_USE 0
1360#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02001361#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */
1362#define SSL_OP_SINGLE_ECDH_USE 0
1363#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +02001364#ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */
1365#define SSL_MODE_RELEASE_BUFFERS 0
1366#endif
Remi Gacognef46cd6e2014-06-12 14:58:40 +02001367
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001368int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, SSL_CTX *ctx, struct proxy *curproxy)
Emeric Brunfc0421f2012-09-07 17:30:07 +02001369{
1370 int cfgerr = 0;
Emeric Brun850efd52014-01-29 12:24:34 +01001371 int verify = SSL_VERIFY_NONE;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02001372 long ssloptions =
Emeric Brunfc0421f2012-09-07 17:30:07 +02001373 SSL_OP_ALL | /* all known workarounds for bugs */
1374 SSL_OP_NO_SSLv2 |
1375 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02001376 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02001377 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02001378 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
1379 SSL_OP_CIPHER_SERVER_PREFERENCE;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02001380 long sslmode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02001381 SSL_MODE_ENABLE_PARTIAL_WRITE |
1382 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
1383 SSL_MODE_RELEASE_BUFFERS;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02001384 STACK_OF(SSL_CIPHER) * ciphers = NULL;
1385 SSL_CIPHER * cipher = NULL;
Remi Gacognec1eab8c2014-06-12 18:20:11 +02001386 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 Gacognef46cd6e2014-06-12 14:58:40 +02001392 int idx = 0;
1393 int dhe_found = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02001394
Thierry Fournier383085f2013-01-24 14:15:43 +01001395 /* 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 Brun89675492012-10-05 13:48:26 +02001401 if (bind_conf->ssl_options & BC_SSL_O_NO_SSLV3)
Emeric Brunfc0421f2012-09-07 17:30:07 +02001402 ssloptions |= SSL_OP_NO_SSLv3;
Emeric Brun89675492012-10-05 13:48:26 +02001403 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV10)
Emeric Brunfc0421f2012-09-07 17:30:07 +02001404 ssloptions |= SSL_OP_NO_TLSv1;
Emeric Brun89675492012-10-05 13:48:26 +02001405 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV11)
Emeric Brunc0ff4922012-09-28 19:37:02 +02001406 ssloptions |= SSL_OP_NO_TLSv1_1;
Emeric Brun89675492012-10-05 13:48:26 +02001407 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV12)
Emeric Brunc0ff4922012-09-28 19:37:02 +02001408 ssloptions |= SSL_OP_NO_TLSv1_2;
Emeric Brun89675492012-10-05 13:48:26 +02001409 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
Emeric Brun2d0c4822012-10-02 13:45:20 +02001410 ssloptions |= SSL_OP_NO_TICKET;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02001411 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 Brunfc0421f2012-09-07 17:30:07 +02001423
1424 SSL_CTX_set_options(ctx, ssloptions);
1425 SSL_CTX_set_mode(ctx, sslmode);
Emeric Brun850efd52014-01-29 12:24:34 +01001426 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 Brunfb510ea2012-10-05 12:00:26 +02001439 if (bind_conf->ca_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02001440 /* load CAfile to verify */
Emeric Brunfb510ea2012-10-05 12:00:26 +02001441 if (!SSL_CTX_load_verify_locations(ctx, bind_conf->ca_file, NULL)) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02001442 Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
Emeric Brunfb510ea2012-10-05 12:00:26 +02001443 curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02001444 cfgerr++;
1445 }
1446 /* set CA names fo client cert request, function returns void */
Emeric Brunfb510ea2012-10-05 12:00:26 +02001447 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(bind_conf->ca_file));
Emeric Brund94b3fe2012-09-20 18:23:56 +02001448 }
Emeric Brun850efd52014-01-29 12:24:34 +01001449 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 Brun051cdab2012-10-02 19:25:50 +02001454#ifdef X509_V_FLAG_CRL_CHECK
Emeric Brunfb510ea2012-10-05 12:00:26 +02001455 if (bind_conf->crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02001456 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
1457
Emeric Brunfb510ea2012-10-05 12:00:26 +02001458 if (!store || !X509_STORE_load_locations(store, bind_conf->crl_file, NULL)) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02001459 Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
Emeric Brunfb510ea2012-10-05 12:00:26 +02001460 curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02001461 cfgerr++;
1462 }
Emeric Brun561e5742012-10-02 15:20:55 +02001463 else {
1464 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
1465 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02001466 }
Emeric Brun051cdab2012-10-02 19:25:50 +02001467#endif
Emeric Brun644cde02012-12-14 11:21:13 +01001468 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02001469 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001470
Emeric Brun4f65bff2012-11-16 15:11:00 +01001471 if (global.tune.ssllifetime)
1472 SSL_CTX_set_timeout(ctx, global.tune.ssllifetime);
1473
Emeric Brunfc0421f2012-09-07 17:30:07 +02001474 shared_context_set_cache(ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001475 if (bind_conf->ciphers &&
1476 !SSL_CTX_set_cipher_list(ctx, bind_conf->ciphers)) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02001477 Alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001478 curproxy->id, bind_conf->ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001479 cfgerr++;
1480 }
1481
Remi Gacognef46cd6e2014-06-12 14:58:40 +02001482 /* 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 Gacognec1eab8c2014-06-12 18:20:11 +02001490 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 Gacognef46cd6e2014-06-12 14:58:40 +02001496 }
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 Brunfc0421f2012-09-07 17:30:07 +02001507 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02001508#if OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02001509 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02001510#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02001511
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001512#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 Bussink48f1c4e2014-02-13 12:29:42 +01001516#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001517 if (bind_conf->alpn_str)
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001518 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, bind_conf);
Willy Tarreauab861d32013-04-02 02:30:41 +02001519#endif
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001520
Emeric Brunfc0421f2012-09-07 17:30:07 +02001521#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1522 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001523 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001524#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02001525#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emeric Brun6924ef82013-03-06 14:08:53 +01001526 {
Emeric Brun2b58d042012-09-20 17:10:03 +02001527 int i;
1528 EC_KEY *ecdh;
1529
Emeric Brun6924ef82013-03-06 14:08:53 +01001530 i = OBJ_sn2nid(bind_conf->ecdhe ? bind_conf->ecdhe : ECDHE_DEFAULT_CURVE);
Emeric Brun2b58d042012-09-20 17:10:03 +02001531 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 Brun6924ef82013-03-06 14:08:53 +01001533 curproxy->id, bind_conf->ecdhe ? bind_conf->ecdhe : ECDHE_DEFAULT_CURVE,
1534 bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brun2b58d042012-09-20 17:10:03 +02001535 cfgerr++;
1536 }
1537 else {
1538 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
1539 EC_KEY_free(ecdh);
1540 }
1541 }
1542#endif
1543
Emeric Brunfc0421f2012-09-07 17:30:07 +02001544 return cfgerr;
1545}
1546
Evan Broderbe554312013-06-27 00:05:25 -07001547static 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 Broderbe554312013-06-27 00:05:25 -07001556 /* 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 Bruna848dae2013-10-08 11:27:28 +02001559 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 Broderbe554312013-06-27 00:05:25 -07001579 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 Brun369da852013-10-08 11:39:35 +02001589 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07001590 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 Bruna848dae2013-10-08 11:27:28 +02001596 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
1597 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07001598 return 0;
1599
1600 return 1;
1601}
1602
1603static 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 Bruna33410c2013-09-17 15:47:48 +02001644#if OPENSSL_VERSION_NUMBER < 0x00907000L
1645 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
1646#else
Evan Broderbe554312013-06-27 00:05:25 -07001647 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02001648#endif
Evan Broderbe554312013-06-27 00:05:25 -07001649 ok = ssl_sock_srv_hostcheck(str, servername);
1650 OPENSSL_free(str);
1651 }
1652 }
1653 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02001654 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07001655 }
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 Brun94324a42012-10-11 14:00:19 +02001670/* prepare ssl context from servers options. Returns an error count */
1671int ssl_sock_prepare_srv_ctx(struct server *srv, struct proxy *curproxy)
1672{
1673 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02001674 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02001675 SSL_OP_ALL | /* all known workarounds for bugs */
1676 SSL_OP_NO_SSLv2 |
1677 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02001678 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02001679 SSL_MODE_ENABLE_PARTIAL_WRITE |
1680 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
1681 SSL_MODE_RELEASE_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01001682 int verify = SSL_VERIFY_NONE;
Emeric Brun94324a42012-10-11 14:00:19 +02001683
Thierry Fournier383085f2013-01-24 14:15:43 +01001684 /* 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 Brun94324a42012-10-11 14:00:19 +02001690 /* 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 Horman66183002013-02-23 10:16:43 +09001695 srv->check_common.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02001696
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 Bruna7aa3092012-10-26 12:58:00 +02001705 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 Brun94324a42012-10-11 14:00:19 +02001725
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 Brunf9c5c472012-10-11 15:28:34 +02001734 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
1735 options |= SSL_OP_NO_TICKET;
Emeric Brun94324a42012-10-11 14:00:19 +02001736 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 Brun850efd52014-01-29 12:24:34 +01001751
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 Broderbe554312013-06-27 00:05:25 -07001763 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01001764 verify,
Evan Broderbe554312013-06-27 00:05:25 -07001765 srv->ssl_ctx.verify_host ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01001766 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02001767 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 Tarreau07ba08b2014-02-16 19:22:08 +01001770 Alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n",
Emeric Brunef42d922012-10-11 16:11:36 +02001771 curproxy->id, srv->id,
1772 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
1773 cfgerr++;
1774 }
1775 }
Emeric Brun850efd52014-01-29 12:24:34 +01001776 else {
1777 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Willy Tarreau07ba08b2014-02-16 19:22:08 +01001778 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 Brun850efd52014-01-29 12:24:34 +01001779 curproxy->id, srv->id,
1780 srv->conf.file, srv->conf.line);
1781 else
Willy Tarreau07ba08b2014-02-16 19:22:08 +01001782 Alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
Emeric Brun850efd52014-01-29 12:24:34 +01001783 curproxy->id, srv->id,
1784 srv->conf.file, srv->conf.line);
1785 cfgerr++;
1786 }
Emeric Brunef42d922012-10-11 16:11:36 +02001787#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 Tarreau07ba08b2014-02-16 19:22:08 +01001792 Alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
Emeric Brunef42d922012-10-11 16:11:36 +02001793 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 Brun4f65bff2012-11-16 15:11:00 +01001804 if (global.tune.ssllifetime)
1805 SSL_CTX_set_timeout(srv->ssl_ctx.ctx, global.tune.ssllifetime);
1806
Emeric Brun94324a42012-10-11 14:00:19 +02001807 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 Tarreau2a65ff02012-09-13 17:54:29 +02001819/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02001820 * be NULL, in which case nothing is done. Returns the number of errors
1821 * encountered.
1822 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001823int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf, struct proxy *px)
Emeric Brunfc0421f2012-09-07 17:30:07 +02001824{
1825 struct ebmb_node *node;
1826 struct sni_ctx *sni;
1827 int err = 0;
1828
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001829 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +02001830 return 0;
1831
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001832 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001833 while (node) {
1834 sni = ebmb_entry(node, struct sni_ctx, name);
1835 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001836 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001837 node = ebmb_next(node);
1838 }
1839
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001840 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001841 while (node) {
1842 sni = ebmb_entry(node, struct sni_ctx, name);
1843 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001844 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001845 node = ebmb_next(node);
1846 }
1847 return err;
1848}
1849
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001850/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02001851 * be NULL, in which case nothing is done. The default_ctx is nullified too.
1852 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001853void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02001854{
1855 struct ebmb_node *node, *back;
1856 struct sni_ctx *sni;
1857
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001858 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +02001859 return;
1860
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001861 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001862 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 Tarreau2a65ff02012-09-13 17:54:29 +02001872 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02001873 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 Tarreau2a65ff02012-09-13 17:54:29 +02001883 bind_conf->default_ctx = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02001884}
1885
Emeric Brun46591952012-05-18 15:47:34 +02001886/*
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 */
1892static int ssl_sock_init(struct connection *conn)
1893{
1894 /* already initialized */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001895 if (conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02001896 return 0;
1897
Willy Tarreau3c728722014-01-23 13:50:42 +01001898 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02001899 return 0;
1900
Willy Tarreau20879a02012-12-03 16:32:10 +01001901 if (global.maxsslconn && sslconns >= global.maxsslconn) {
1902 conn->err_code = CO_ER_SSL_TOO_MANY;
Willy Tarreau403edff2012-09-06 11:58:37 +02001903 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +01001904 }
Willy Tarreau403edff2012-09-06 11:58:37 +02001905
Emeric Brun46591952012-05-18 15:47:34 +02001906 /* If it is in client mode initiate SSL session
1907 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001908 if (objt_server(conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +02001909 /* Alloc a new SSL session ctx */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001910 conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx);
Willy Tarreau20879a02012-12-03 16:32:10 +01001911 if (!conn->xprt_ctx) {
1912 conn->err_code = CO_ER_SSL_NO_MEM;
Emeric Brun46591952012-05-18 15:47:34 +02001913 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +01001914 }
Emeric Brun46591952012-05-18 15:47:34 +02001915
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001916 SSL_set_connect_state(conn->xprt_ctx);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001917 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 Brun46591952012-05-18 15:47:34 +02001919
1920 /* set fd on SSL session context */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001921 SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +02001922
Evan Broderbe554312013-06-27 00:05:25 -07001923 /* set connection pointer */
1924 SSL_set_app_data(conn->xprt_ctx, conn);
1925
Emeric Brun46591952012-05-18 15:47:34 +02001926 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02001927 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02001928
1929 sslconns++;
Willy Tarreau71b734c2014-01-28 15:19:44 +01001930 totalsslconns++;
Emeric Brun46591952012-05-18 15:47:34 +02001931 return 0;
1932 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001933 else if (objt_listener(conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +02001934 /* Alloc a new SSL session ctx */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001935 conn->xprt_ctx = SSL_new(objt_listener(conn->target)->bind_conf->default_ctx);
Willy Tarreau20879a02012-12-03 16:32:10 +01001936 if (!conn->xprt_ctx) {
1937 conn->err_code = CO_ER_SSL_NO_MEM;
Emeric Brun46591952012-05-18 15:47:34 +02001938 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +01001939 }
Emeric Brun46591952012-05-18 15:47:34 +02001940
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001941 SSL_set_accept_state(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +02001942
1943 /* set fd on SSL session context */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001944 SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +02001945
Emeric Brune1f38db2012-09-03 20:36:47 +02001946 /* set connection pointer */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001947 SSL_set_app_data(conn->xprt_ctx, conn);
Emeric Brune1f38db2012-09-03 20:36:47 +02001948
Emeric Brun46591952012-05-18 15:47:34 +02001949 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02001950 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02001951
1952 sslconns++;
Willy Tarreau71b734c2014-01-28 15:19:44 +01001953 totalsslconns++;
Emeric Brun46591952012-05-18 15:47:34 +02001954 return 0;
1955 }
1956 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01001957 conn->err_code = CO_ER_SSL_NO_TARGET;
Emeric Brun46591952012-05-18 15:47:34 +02001958 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 */
1968int ssl_sock_handshake(struct connection *conn, unsigned int flag)
1969{
1970 int ret;
1971
Willy Tarreau3c728722014-01-23 13:50:42 +01001972 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02001973 return 0;
1974
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001975 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02001976 goto out_error;
1977
Emeric Brun674b7432012-11-08 19:21:55 +01001978 /* 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 Tarreaue1f50c42014-01-22 20:02:06 +01001994 __conn_sock_want_send(conn);
1995 fd_cant_send(conn->t.sock.fd);
Emeric Brun674b7432012-11-08 19:21:55 +01001996 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 Tarreaue1f50c42014-01-22 20:02:06 +01002010 __conn_sock_want_recv(conn);
2011 fd_cant_recv(conn->t.sock.fd);
Emeric Brun674b7432012-11-08 19:21:55 +01002012 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 Tarreau20879a02012-12-03 16:32:10 +01002018 if (!conn->err_code) {
Emeric Brun29f037d2014-04-25 19:05:36 +02002019 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 Tarreau20879a02012-12-03 16:32:10 +01002036 else
Emeric Brun29f037d2014-04-25 19:05:36 +02002037 conn->err_code = CO_ER_SSL_HANDSHAKE;
2038 }
Willy Tarreau20879a02012-12-03 16:32:10 +01002039 }
Emeric Brun674b7432012-11-08 19:21:55 +01002040 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 Tarreau46be2e52014-01-20 12:10:52 +01002049 conn_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01002050 if (!conn->err_code)
Willy Tarreauf51c6982014-04-25 20:02:39 +02002051 conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
2052 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01002053 goto out_error;
2054 }
2055 }
2056 /* read some data: consider handshake completed */
2057 goto reneg_ok;
2058 }
2059
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002060 ret = SSL_do_handshake(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +02002061 if (ret != 1) {
2062 /* handshake did not complete, let's find why */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002063 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +02002064
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 Tarreaue1f50c42014-01-22 20:02:06 +01002068 __conn_sock_want_send(conn);
2069 fd_cant_send(conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +02002070 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 Tarreaue1f50c42014-01-22 20:02:06 +01002077 __conn_sock_want_recv(conn);
2078 fd_cant_recv(conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +02002079 return 0;
2080 }
Willy Tarreau89230192012-09-28 20:22:13 +02002081 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 Tarreau20879a02012-12-03 16:32:10 +01002085
Emeric Brun29f037d2014-04-25 19:05:36 +02002086 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 Tarreau20879a02012-12-03 16:32:10 +01002103 else
Emeric Brun29f037d2014-04-25 19:05:36 +02002104 conn->err_code = CO_ER_SSL_HANDSHAKE;
2105 }
Willy Tarreau89230192012-09-28 20:22:13 +02002106 goto out_error;
2107 }
Emeric Brun46591952012-05-18 15:47:34 +02002108 else {
2109 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02002110 /* 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 Tarreau46be2e52014-01-20 12:10:52 +01002115 conn_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01002116 if (!conn->err_code)
Willy Tarreauf51c6982014-04-25 20:02:39 +02002117 conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
2118 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02002119 goto out_error;
2120 }
2121 }
2122
Emeric Brun674b7432012-11-08 19:21:55 +01002123reneg_ok:
2124
Emeric Brun46591952012-05-18 15:47:34 +02002125 /* Handshake succeeded */
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002126 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 Brun46591952012-05-18 15:47:34 +02002132 /* check if session was reused, if not store current session on server for reuse */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002133 if (objt_server(conn->target)->ssl_ctx.reused_sess)
2134 SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess);
Emeric Brun46591952012-05-18 15:47:34 +02002135
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002136 objt_server(conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +02002137 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002138 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 Brun46591952012-05-18 15:47:34 +02002143 }
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 Brun644cde02012-12-14 11:21:13 +01002150 /* Clear openssl global errors stack */
2151 ERR_clear_error();
2152
Emeric Brun9fa89732012-10-04 17:09:56 +02002153 /* free resumed session if exists */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002154 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 Brun9fa89732012-10-04 17:09:56 +02002157 }
2158
Emeric Brun46591952012-05-18 15:47:34 +02002159 /* Fail on all other handshake errors */
2160 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01002161 if (!conn->err_code)
2162 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02002163 return 0;
2164}
2165
2166/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01002167 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02002168 * 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 */
2174static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
2175{
2176 int ret, done = 0;
Willy Tarreauabf08d92014-01-14 11:31:27 +01002177 int try;
Emeric Brun46591952012-05-18 15:47:34 +02002178
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002179 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02002180 goto out_error;
2181
2182 if (conn->flags & CO_FL_HANDSHAKE)
2183 /* a handshake was requested */
2184 return 0;
2185
Willy Tarreauabf08d92014-01-14 11:31:27 +01002186 /* let's realign the buffer to optimize I/O */
2187 if (buffer_empty(buf))
Emeric Brun46591952012-05-18 15:47:34 +02002188 buf->p = buf->data;
Emeric Brun46591952012-05-18 15:47:34 +02002189
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 Tarreau00b0fb92014-01-17 11:09:40 +01002195 while (count > 0) {
Willy Tarreauabf08d92014-01-14 11:31:27 +01002196 /* 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 Tarreauf7bc57c2012-10-03 00:19:48 +02002207 ret = SSL_read(conn->xprt_ctx, bi_end(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +02002208 if (conn->flags & CO_FL_ERROR) {
2209 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01002210 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02002211 }
Emeric Brun46591952012-05-18 15:47:34 +02002212 if (ret > 0) {
2213 buf->i += ret;
2214 done += ret;
2215 if (ret < try)
2216 break;
2217 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02002218 }
2219 else if (ret == 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01002220 ret = SSL_get_error(conn->xprt_ctx, ret);
2221 if (ret != SSL_ERROR_ZERO_RETURN) {
Emeric Brun1c646862012-12-14 12:33:41 +01002222 /* error on protocol or underlying transport */
2223 if ((ret != SSL_ERROR_SYSCALL)
2224 || (errno && (errno != EAGAIN)))
2225 conn->flags |= CO_FL_ERROR;
2226
Emeric Brun644cde02012-12-14 11:21:13 +01002227 /* Clear openssl global errors stack */
2228 ERR_clear_error();
2229 }
Emeric Brun46591952012-05-18 15:47:34 +02002230 goto read0;
2231 }
2232 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002233 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +02002234 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01002235 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02002236 conn->flags |= CO_FL_SSL_WAIT_HS;
Emeric Brun8af8dd12012-11-08 17:56:20 +01002237 __conn_sock_want_send(conn);
Emeric Brun46591952012-05-18 15:47:34 +02002238 break;
2239 }
2240 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun282a76a2012-11-08 18:02:56 +01002241 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 Brun46591952012-05-18 15:47:34 +02002247 /* we need to poll for retry a read later */
Willy Tarreaue1f50c42014-01-22 20:02:06 +01002248 fd_cant_recv(conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +02002249 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 Brun644cde02012-12-14 11:21:13 +01002261 /* Clear openssl global errors stack */
2262 ERR_clear_error();
2263
Emeric Brun46591952012-05-18 15:47:34 +02002264 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 Tarreau1049b1f2014-02-02 01:51:17 +01002270 * <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 Brun46591952012-05-18 15:47:34 +02002272 * 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 */
2279static 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 Tarreauf7bc57c2012-10-03 00:19:48 +02002285 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02002286 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 Hestercad82342013-05-30 15:12:41 -07002297 try = bo_contig_data(buf);
Willy Tarreaubfd59462013-02-21 07:46:09 +01002298
Willy Tarreau7bed9452014-02-02 02:00:24 +01002299 if (!(flags & CO_SFL_STREAMER) &&
Willy Tarreau518cedd2014-02-17 15:43:01 +01002300 !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
2301 global.tune.ssl_max_record && try > global.tune.ssl_max_record) {
Willy Tarreaubfd59462013-02-21 07:46:09 +01002302 try = global.tune.ssl_max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01002303 }
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 Tarreaubfd59462013-02-21 07:46:09 +01002311
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002312 ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01002313
Emeric Brune1f38db2012-09-03 20:36:47 +02002314 if (conn->flags & CO_FL_ERROR) {
2315 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01002316 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02002317 }
Emeric Brun46591952012-05-18 15:47:34 +02002318 if (ret > 0) {
Willy Tarreau518cedd2014-02-17 15:43:01 +01002319 conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
2320
Emeric Brun46591952012-05-18 15:47:34 +02002321 buf->o -= ret;
2322 done += ret;
2323
Willy Tarreau5fb38032012-12-16 19:39:09 +01002324 if (likely(buffer_empty(buf)))
Emeric Brun46591952012-05-18 15:47:34 +02002325 /* 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 Tarreauf7bc57c2012-10-03 00:19:48 +02002333 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +02002334 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun282a76a2012-11-08 18:02:56 +01002335 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 Brun46591952012-05-18 15:47:34 +02002341 /* we need to poll to retry a write later */
Willy Tarreaue1f50c42014-01-22 20:02:06 +01002342 fd_cant_send(conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +02002343 break;
2344 }
2345 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01002346 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02002347 conn->flags |= CO_FL_SSL_WAIT_HS;
Emeric Brun8af8dd12012-11-08 17:56:20 +01002348 __conn_sock_want_recv(conn);
Emeric Brun46591952012-05-18 15:47:34 +02002349 break;
2350 }
2351 goto out_error;
2352 }
2353 }
2354 return done;
2355
2356 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01002357 /* Clear openssl global errors stack */
2358 ERR_clear_error();
2359
Emeric Brun46591952012-05-18 15:47:34 +02002360 conn->flags |= CO_FL_ERROR;
2361 return done;
2362}
2363
Emeric Brun46591952012-05-18 15:47:34 +02002364static void ssl_sock_close(struct connection *conn) {
2365
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002366 if (conn->xprt_ctx) {
2367 SSL_free(conn->xprt_ctx);
2368 conn->xprt_ctx = NULL;
Willy Tarreau403edff2012-09-06 11:58:37 +02002369 sslconns--;
Emeric Brun46591952012-05-18 15:47:34 +02002370 }
Emeric Brun46591952012-05-18 15:47:34 +02002371}
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 */
2376static 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 Brun644cde02012-12-14 11:21:13 +01002381 if (clean && (SSL_shutdown(conn->xprt_ctx) <= 0)) {
2382 /* Clear openssl global errors stack */
2383 ERR_clear_error();
2384 }
Emeric Brun46591952012-05-18 15:47:34 +02002385
2386 /* force flag on ssl to keep session in cache regardless shutdown result */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002387 SSL_set_shutdown(conn->xprt_ctx, SSL_SENT_SHUTDOWN);
Emeric Brun46591952012-05-18 15:47:34 +02002388}
2389
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002390/* used for logging, may be changed for a sample fetch later */
2391const 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 */
2399const 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 Tarreau8d598402012-10-22 17:58:39 +02002406/* 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 */
2410static int
2411ssl_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 Brunce5ad802012-10-22 14:11:22 +02002427
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 */
2432static int
2433ssl_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 Brun87855892012-10-17 17:39:35 +02002467/* 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 */
2470static int
2471ssl_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 */
2519static int
2520ssl_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 Safb76832014-05-08 23:42:08 -04002559char *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 */
2568char *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 */
2606int 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 */
2615unsigned 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 Tarreau7875d092012-09-10 08:20:03 +02002623/***** Below are some sample fetching functions for ACL/patterns *****/
2624
Emeric Brune64aef12012-09-21 13:15:06 +02002625/* boolean, returns true if client cert was present */
2626static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002627smp_fetch_ssl_fc_has_crt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002628 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brune64aef12012-09-21 13:15:06 +02002629{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002630 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 Brune64aef12012-09-21 13:15:06 +02002637 return 0;
2638
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002639 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02002640 smp->flags |= SMP_F_MAY_CHANGE;
2641 return 0;
2642 }
2643
2644 smp->flags = 0;
2645 smp->type = SMP_T_BOOL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002646 smp->data.uint = SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02002647
2648 return 1;
2649}
2650
Emeric Brunba841a12014-04-30 17:05:08 +02002651/* 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 Tarreau8d598402012-10-22 17:58:39 +02002655static int
Emeric Brunba841a12014-04-30 17:05:08 +02002656smp_fetch_ssl_x_serial(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002657 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8d598402012-10-22 17:58:39 +02002658{
Emeric Brunba841a12014-04-30 17:05:08 +02002659 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02002660 X509 *crt = NULL;
2661 int ret = 0;
2662 struct chunk *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002663 struct connection *conn;
2664
2665 if (!l4)
2666 return 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02002667
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002668 conn = objt_conn(l4->si[0].end);
2669 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02002670 return 0;
2671
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002672 if (!(conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02002673 smp->flags |= SMP_F_MAY_CHANGE;
2674 return 0;
2675 }
2676
Emeric Brunba841a12014-04-30 17:05:08 +02002677 if (cert_peer)
2678 crt = SSL_get_peer_certificate(conn->xprt_ctx);
2679 else
2680 crt = SSL_get_certificate(conn->xprt_ctx);
2681
Willy Tarreau8d598402012-10-22 17:58:39 +02002682 if (!crt)
2683 goto out;
2684
Willy Tarreau47ca5452012-12-23 20:22:19 +01002685 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02002686 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;
2692out:
Emeric Brunba841a12014-04-30 17:05:08 +02002693 /* SSL_get_peer_certificate, it increase X509 * ref count */
2694 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02002695 X509_free(crt);
2696 return ret;
2697}
Emeric Brune64aef12012-09-21 13:15:06 +02002698
Emeric Brunba841a12014-04-30 17:05:08 +02002699/* 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 Votha051b4a2013-05-14 20:37:59 +02002703static int
Emeric Brunba841a12014-04-30 17:05:08 +02002704smp_fetch_ssl_x_sha1(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002705 const struct arg *args, struct sample *smp, const char *kw)
James Votha051b4a2013-05-14 20:37:59 +02002706{
Emeric Brunba841a12014-04-30 17:05:08 +02002707 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02002708 X509 *crt = NULL;
2709 const EVP_MD *digest;
2710 int ret = 0;
2711 struct chunk *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002712 struct connection *conn;
James Votha051b4a2013-05-14 20:37:59 +02002713
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002714 if (!l4)
James Votha051b4a2013-05-14 20:37:59 +02002715 return 0;
2716
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002717 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 Votha051b4a2013-05-14 20:37:59 +02002722 smp->flags |= SMP_F_MAY_CHANGE;
2723 return 0;
2724 }
2725
Emeric Brunba841a12014-04-30 17:05:08 +02002726 if (cert_peer)
2727 crt = SSL_get_peer_certificate(conn->xprt_ctx);
2728 else
2729 crt = SSL_get_certificate(conn->xprt_ctx);
James Votha051b4a2013-05-14 20:37:59 +02002730 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;
2740out:
Emeric Brunba841a12014-04-30 17:05:08 +02002741 /* SSL_get_peer_certificate, it increase X509 * ref count */
2742 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02002743 X509_free(crt);
2744 return ret;
2745}
2746
Emeric Brunba841a12014-04-30 17:05:08 +02002747/* 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 Brunce5ad802012-10-22 14:11:22 +02002751static int
Emeric Brunba841a12014-04-30 17:05:08 +02002752smp_fetch_ssl_x_notafter(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002753 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brunce5ad802012-10-22 14:11:22 +02002754{
Emeric Brunba841a12014-04-30 17:05:08 +02002755 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02002756 X509 *crt = NULL;
2757 int ret = 0;
2758 struct chunk *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002759 struct connection *conn;
Emeric Brunce5ad802012-10-22 14:11:22 +02002760
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002761 if (!l4)
Emeric Brunce5ad802012-10-22 14:11:22 +02002762 return 0;
2763
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002764 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 Brunce5ad802012-10-22 14:11:22 +02002769 smp->flags |= SMP_F_MAY_CHANGE;
2770 return 0;
2771 }
2772
Emeric Brunba841a12014-04-30 17:05:08 +02002773 if (cert_peer)
2774 crt = SSL_get_peer_certificate(conn->xprt_ctx);
2775 else
2776 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Brunce5ad802012-10-22 14:11:22 +02002777 if (!crt)
2778 goto out;
2779
Willy Tarreau47ca5452012-12-23 20:22:19 +01002780 smp_trash = get_trash_chunk();
Emeric Brunce5ad802012-10-22 14:11:22 +02002781 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;
2787out:
Emeric Brunba841a12014-04-30 17:05:08 +02002788 /* SSL_get_peer_certificate, it increase X509 * ref count */
2789 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02002790 X509_free(crt);
2791 return ret;
2792}
2793
Emeric Brunba841a12014-04-30 17:05:08 +02002794/* 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 Brun87855892012-10-17 17:39:35 +02002798static int
Emeric Brunba841a12014-04-30 17:05:08 +02002799smp_fetch_ssl_x_i_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002800 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brun87855892012-10-17 17:39:35 +02002801{
Emeric Brunba841a12014-04-30 17:05:08 +02002802 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02002803 X509 *crt = NULL;
2804 X509_NAME *name;
2805 int ret = 0;
2806 struct chunk *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002807 struct connection *conn;
Emeric Brun87855892012-10-17 17:39:35 +02002808
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002809 if (!l4)
Emeric Brun87855892012-10-17 17:39:35 +02002810 return 0;
2811
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002812 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 Brun87855892012-10-17 17:39:35 +02002817 smp->flags |= SMP_F_MAY_CHANGE;
2818 return 0;
2819 }
2820
Emeric Brunba841a12014-04-30 17:05:08 +02002821 if (cert_peer)
2822 crt = SSL_get_peer_certificate(conn->xprt_ctx);
2823 else
2824 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Brun87855892012-10-17 17:39:35 +02002825 if (!crt)
2826 goto out;
2827
2828 name = X509_get_issuer_name(crt);
2829 if (!name)
2830 goto out;
2831
Willy Tarreau47ca5452012-12-23 20:22:19 +01002832 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02002833 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;
2850out:
Emeric Brunba841a12014-04-30 17:05:08 +02002851 /* SSL_get_peer_certificate, it increase X509 * ref count */
2852 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02002853 X509_free(crt);
2854 return ret;
2855}
2856
Emeric Brunba841a12014-04-30 17:05:08 +02002857/* 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 Brunce5ad802012-10-22 14:11:22 +02002861static int
Emeric Brunba841a12014-04-30 17:05:08 +02002862smp_fetch_ssl_x_notbefore(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002863 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brunce5ad802012-10-22 14:11:22 +02002864{
Emeric Brunba841a12014-04-30 17:05:08 +02002865 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02002866 X509 *crt = NULL;
2867 int ret = 0;
2868 struct chunk *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002869 struct connection *conn;
2870
2871 if (!l4)
2872 return 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02002873
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002874 conn = objt_conn(l4->si[0].end);
2875 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02002876 return 0;
2877
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002878 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02002879 smp->flags |= SMP_F_MAY_CHANGE;
2880 return 0;
2881 }
2882
Emeric Brunba841a12014-04-30 17:05:08 +02002883 if (cert_peer)
2884 crt = SSL_get_peer_certificate(conn->xprt_ctx);
2885 else
2886 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Brunce5ad802012-10-22 14:11:22 +02002887 if (!crt)
2888 goto out;
2889
Willy Tarreau47ca5452012-12-23 20:22:19 +01002890 smp_trash = get_trash_chunk();
Emeric Brunce5ad802012-10-22 14:11:22 +02002891 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;
2897out:
Emeric Brunba841a12014-04-30 17:05:08 +02002898 /* SSL_get_peer_certificate, it increase X509 * ref count */
2899 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02002900 X509_free(crt);
2901 return ret;
2902}
2903
Emeric Brunba841a12014-04-30 17:05:08 +02002904/* 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 Brun87855892012-10-17 17:39:35 +02002908static int
Emeric Brunba841a12014-04-30 17:05:08 +02002909smp_fetch_ssl_x_s_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002910 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brun87855892012-10-17 17:39:35 +02002911{
Emeric Brunba841a12014-04-30 17:05:08 +02002912 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02002913 X509 *crt = NULL;
2914 X509_NAME *name;
2915 int ret = 0;
2916 struct chunk *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002917 struct connection *conn;
Emeric Brun87855892012-10-17 17:39:35 +02002918
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002919 if (!l4)
Emeric Brun87855892012-10-17 17:39:35 +02002920 return 0;
2921
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002922 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 Brun87855892012-10-17 17:39:35 +02002927 smp->flags |= SMP_F_MAY_CHANGE;
2928 return 0;
2929 }
2930
Emeric Brunba841a12014-04-30 17:05:08 +02002931 if (cert_peer)
2932 crt = SSL_get_peer_certificate(conn->xprt_ctx);
2933 else
2934 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Brun87855892012-10-17 17:39:35 +02002935 if (!crt)
2936 goto out;
2937
2938 name = X509_get_subject_name(crt);
2939 if (!name)
2940 goto out;
2941
Willy Tarreau47ca5452012-12-23 20:22:19 +01002942 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02002943 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;
2960out:
Emeric Brunba841a12014-04-30 17:05:08 +02002961 /* SSL_get_peer_certificate, it increase X509 * ref count */
2962 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02002963 X509_free(crt);
2964 return ret;
2965}
Emeric Brun9143d372012-12-20 15:44:16 +01002966
2967/* integer, returns true if current session use a client certificate */
2968static int
2969smp_fetch_ssl_c_used(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002970 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brun9143d372012-12-20 15:44:16 +01002971{
2972 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002973 struct connection *conn;
Emeric Brun9143d372012-12-20 15:44:16 +01002974
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002975 if (!l4)
Emeric Brun9143d372012-12-20 15:44:16 +01002976 return 0;
2977
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002978 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 Brun9143d372012-12-20 15:44:16 +01002983 smp->flags |= SMP_F_MAY_CHANGE;
2984 return 0;
2985 }
2986
2987 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002988 crt = SSL_get_peer_certificate(conn->xprt_ctx);
Emeric Brun9143d372012-12-20 15:44:16 +01002989 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 Brunba841a12014-04-30 17:05:08 +02002998/* 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 Bruna7359fd2012-10-17 15:03:11 +02003002static int
Emeric Brunba841a12014-04-30 17:05:08 +02003003smp_fetch_ssl_x_version(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003004 const struct arg *args, struct sample *smp, const char *kw)
Emeric Bruna7359fd2012-10-17 15:03:11 +02003005{
Emeric Brunba841a12014-04-30 17:05:08 +02003006 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02003007 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003008 struct connection *conn;
3009
3010 if (!l4)
3011 return 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02003012
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003013 conn = objt_conn(l4->si[0].end);
3014 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02003015 return 0;
3016
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003017 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02003018 smp->flags |= SMP_F_MAY_CHANGE;
3019 return 0;
3020 }
3021
Emeric Brunba841a12014-04-30 17:05:08 +02003022 if (cert_peer)
3023 crt = SSL_get_peer_certificate(conn->xprt_ctx);
3024 else
3025 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Bruna7359fd2012-10-17 15:03:11 +02003026 if (!crt)
3027 return 0;
3028
3029 smp->data.uint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02003030 /* SSL_get_peer_certificate increase X509 * ref count */
3031 if (cert_peer)
3032 X509_free(crt);
Emeric Bruna7359fd2012-10-17 15:03:11 +02003033 smp->type = SMP_T_UINT;
3034
3035 return 1;
3036}
3037
Emeric Brunba841a12014-04-30 17:05:08 +02003038/* 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 Brun7f56e742012-10-19 18:15:40 +02003042static int
Emeric Brunba841a12014-04-30 17:05:08 +02003043smp_fetch_ssl_x_sig_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003044 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brun7f56e742012-10-19 18:15:40 +02003045{
Emeric Brunba841a12014-04-30 17:05:08 +02003046 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02003047 X509 *crt;
3048 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003049 struct connection *conn;
Emeric Brun7f56e742012-10-19 18:15:40 +02003050
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003051 if (!l4)
Emeric Brun7f56e742012-10-19 18:15:40 +02003052 return 0;
3053
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003054 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 Brun7f56e742012-10-19 18:15:40 +02003059 smp->flags |= SMP_F_MAY_CHANGE;
3060 return 0;
3061 }
3062
Emeric Brunba841a12014-04-30 17:05:08 +02003063 if (cert_peer)
3064 crt = SSL_get_peer_certificate(conn->xprt_ctx);
3065 else
3066 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Brun7f56e742012-10-19 18:15:40 +02003067 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 Brun9bf3ba22013-10-07 14:31:44 +02003073 if (!smp->data.str.str) {
Emeric Brunba841a12014-04-30 17:05:08 +02003074 /* SSL_get_peer_certificate increase X509 * ref count */
3075 if (cert_peer)
3076 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02003077 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02003078 }
Emeric Brun7f56e742012-10-19 18:15:40 +02003079
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003080 smp->type = SMP_T_STR;
3081 smp->flags |= SMP_F_CONST;
Emeric Brun7f56e742012-10-19 18:15:40 +02003082 smp->data.str.len = strlen(smp->data.str.str);
Emeric Brunba841a12014-04-30 17:05:08 +02003083 /* SSL_get_peer_certificate increase X509 * ref count */
3084 if (cert_peer)
3085 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02003086
3087 return 1;
3088}
3089
Emeric Brunba841a12014-04-30 17:05:08 +02003090/* 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 Brun521a0112012-10-22 12:22:55 +02003094static int
Emeric Brunba841a12014-04-30 17:05:08 +02003095smp_fetch_ssl_x_key_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003096 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brun521a0112012-10-22 12:22:55 +02003097{
Emeric Brunba841a12014-04-30 17:05:08 +02003098 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02003099 X509 *crt;
3100 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003101 struct connection *conn;
Emeric Brun521a0112012-10-22 12:22:55 +02003102
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003103 if (!l4)
Emeric Brun521a0112012-10-22 12:22:55 +02003104 return 0;
3105
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003106 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 Brun521a0112012-10-22 12:22:55 +02003111 smp->flags |= SMP_F_MAY_CHANGE;
3112 return 0;
3113 }
3114
Emeric Brunba841a12014-04-30 17:05:08 +02003115 if (cert_peer)
3116 crt = SSL_get_peer_certificate(conn->xprt_ctx);
3117 else
3118 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Brun521a0112012-10-22 12:22:55 +02003119 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 Brun9bf3ba22013-10-07 14:31:44 +02003125 if (!smp->data.str.str) {
Emeric Brunba841a12014-04-30 17:05:08 +02003126 /* SSL_get_peer_certificate increase X509 * ref count */
3127 if (cert_peer)
3128 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02003129 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02003130 }
Emeric Brun521a0112012-10-22 12:22:55 +02003131
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003132 smp->type = SMP_T_STR;
3133 smp->flags |= SMP_F_CONST;
Emeric Brun521a0112012-10-22 12:22:55 +02003134 smp->data.str.len = strlen(smp->data.str.str);
Emeric Brunba841a12014-04-30 17:05:08 +02003135 if (cert_peer)
3136 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02003137
3138 return 1;
3139}
3140
Emeric Brun645ae792014-04-30 14:21:06 +02003141/* 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 Tarreau7875d092012-09-10 08:20:03 +02003145static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02003146smp_fetch_ssl_fc(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003147 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7875d092012-09-10 08:20:03 +02003148{
Emeric Brun645ae792014-04-30 14:21:06 +02003149 int back_conn = (kw[4] == 'b') ? 1 : 0;
3150 struct connection *conn = objt_conn(l4->si[back_conn].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003151
Willy Tarreau7875d092012-09-10 08:20:03 +02003152 smp->type = SMP_T_BOOL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003153 smp->data.uint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02003154 return 1;
3155}
3156
Emeric Brun2525b6b2012-10-18 15:59:43 +02003157/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02003158static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02003159smp_fetch_ssl_fc_has_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003160 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7875d092012-09-10 08:20:03 +02003161{
3162#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003163 struct connection *conn = objt_conn(l4->si[0].end);
3164
Willy Tarreau7875d092012-09-10 08:20:03 +02003165 smp->type = SMP_T_BOOL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003166 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 Tarreau7875d092012-09-10 08:20:03 +02003169 return 1;
3170#else
3171 return 0;
3172#endif
3173}
3174
Emeric Brun645ae792014-04-30 14:21:06 +02003175/* 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 Brun589fcad2012-10-16 14:13:26 +02003179static int
3180smp_fetch_ssl_fc_cipher(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003181 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brun589fcad2012-10-16 14:13:26 +02003182{
Emeric Brun645ae792014-04-30 14:21:06 +02003183 int back_conn = (kw[4] == 'b') ? 1 : 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003184 struct connection *conn;
3185
Emeric Brun589fcad2012-10-16 14:13:26 +02003186 smp->flags = 0;
3187
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003188 if (!l4)
3189 return 0;
3190
Emeric Brun645ae792014-04-30 14:21:06 +02003191 conn = objt_conn(l4->si[back_conn].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003192 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02003193 return 0;
3194
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003195 smp->data.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx);
Emeric Brun589fcad2012-10-16 14:13:26 +02003196 if (!smp->data.str.str)
3197 return 0;
3198
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003199 smp->type = SMP_T_STR;
3200 smp->flags |= SMP_F_CONST;
Emeric Brun589fcad2012-10-16 14:13:26 +02003201 smp->data.str.len = strlen(smp->data.str.str);
3202
3203 return 1;
3204}
3205
Emeric Brun645ae792014-04-30 14:21:06 +02003206/* 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 Brun589fcad2012-10-16 14:13:26 +02003211static int
3212smp_fetch_ssl_fc_alg_keysize(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003213 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brun589fcad2012-10-16 14:13:26 +02003214{
Emeric Brun645ae792014-04-30 14:21:06 +02003215 int back_conn = (kw[4] == 'b') ? 1 : 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003216 struct connection *conn;
3217
Emeric Brun589fcad2012-10-16 14:13:26 +02003218 smp->flags = 0;
3219
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003220 if (!l4)
Emeric Brun589fcad2012-10-16 14:13:26 +02003221 return 0;
3222
Emeric Brun645ae792014-04-30 14:21:06 +02003223 conn = objt_conn(l4->si[back_conn].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003224 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02003225 return 0;
3226
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003227 if (!SSL_get_cipher_bits(conn->xprt_ctx, (int *)&smp->data.uint))
3228 return 0;
3229
Emeric Brun589fcad2012-10-16 14:13:26 +02003230 smp->type = SMP_T_UINT;
3231
3232 return 1;
3233}
3234
Emeric Brun645ae792014-04-30 14:21:06 +02003235/* 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 Brun589fcad2012-10-16 14:13:26 +02003239static int
3240smp_fetch_ssl_fc_use_keysize(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003241 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brun589fcad2012-10-16 14:13:26 +02003242{
Emeric Brun645ae792014-04-30 14:21:06 +02003243 int back_conn = (kw[4] == 'b') ? 1 : 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003244 struct connection *conn;
3245
Emeric Brun589fcad2012-10-16 14:13:26 +02003246 smp->flags = 0;
3247
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003248 if (!l4)
Emeric Brun589fcad2012-10-16 14:13:26 +02003249 return 0;
3250
Emeric Brun645ae792014-04-30 14:21:06 +02003251 conn = objt_conn(l4->si[back_conn].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003252 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 Brun589fcad2012-10-16 14:13:26 +02003256 if (!smp->data.uint)
3257 return 0;
3258
3259 smp->type = SMP_T_UINT;
3260
3261 return 1;
3262}
3263
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02003264#ifdef OPENSSL_NPN_NEGOTIATED
Willy Tarreau7875d092012-09-10 08:20:03 +02003265static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02003266smp_fetch_ssl_fc_npn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003267 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua33c6542012-10-15 13:19:06 +02003268{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003269 struct connection *conn;
3270
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003271 smp->flags = SMP_F_CONST;
3272 smp->type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02003273
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003274 if (!l4)
Willy Tarreaua33c6542012-10-15 13:19:06 +02003275 return 0;
3276
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003277 conn = objt_conn(l4->si[0].end);
3278 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
3279 return 0;
3280
Willy Tarreaua33c6542012-10-15 13:19:06 +02003281 smp->data.str.str = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003282 SSL_get0_next_proto_negotiated(conn->xprt_ctx,
Willy Tarreaua33c6542012-10-15 13:19:06 +02003283 (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 Tarreaua33c6542012-10-15 13:19:06 +02003289}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02003290#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02003291
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01003292#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02003293static int
3294smp_fetch_ssl_fc_alpn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003295 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab861d32013-04-02 02:30:41 +02003296{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003297 struct connection *conn;
3298
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003299 smp->flags = SMP_F_CONST;
3300 smp->type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02003301
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003302 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 Tarreauab861d32013-04-02 02:30:41 +02003307 return 0;
3308
3309 smp->data.str.str = NULL;
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01003310 SSL_get0_alpn_selected(conn->xprt_ctx,
Willy Tarreauab861d32013-04-02 02:30:41 +02003311 (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 Brun645ae792014-04-30 14:21:06 +02003320/* 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 Tarreaua33c6542012-10-15 13:19:06 +02003324static int
Emeric Brun589fcad2012-10-16 14:13:26 +02003325smp_fetch_ssl_fc_protocol(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003326 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brun589fcad2012-10-16 14:13:26 +02003327{
Emeric Brun645ae792014-04-30 14:21:06 +02003328 int back_conn = (kw[4] == 'b') ? 1 : 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003329 struct connection *conn;
3330
Emeric Brun589fcad2012-10-16 14:13:26 +02003331 smp->flags = 0;
3332
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003333 if (!l4)
Emeric Brun589fcad2012-10-16 14:13:26 +02003334 return 0;
3335
Emeric Brun645ae792014-04-30 14:21:06 +02003336 conn = objt_conn(l4->si[back_conn].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003337 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 Brun589fcad2012-10-16 14:13:26 +02003341 if (!smp->data.str.str)
3342 return 0;
3343
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003344 smp->type = SMP_T_STR;
3345 smp->flags = SMP_F_CONST;
Emeric Brun589fcad2012-10-16 14:13:26 +02003346 smp->data.str.len = strlen(smp->data.str.str);
3347
3348 return 1;
3349}
3350
Emeric Brun645ae792014-04-30 14:21:06 +02003351/* 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 Brun589fcad2012-10-16 14:13:26 +02003355static int
Emeric Brunfe68f682012-10-16 14:59:28 +02003356smp_fetch_ssl_fc_session_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003357 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brunfe68f682012-10-16 14:59:28 +02003358{
3359#if OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +02003360 int back_conn = (kw[4] == 'b') ? 1 : 0;
Emeric Brunfe68f682012-10-16 14:59:28 +02003361 SSL_SESSION *sess;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003362 struct connection *conn;
Emeric Brunfe68f682012-10-16 14:59:28 +02003363
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003364 smp->flags = SMP_F_CONST;
3365 smp->type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02003366
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003367 if (!l4)
Emeric Brunfe68f682012-10-16 14:59:28 +02003368 return 0;
3369
Emeric Brun645ae792014-04-30 14:21:06 +02003370 conn = objt_conn(l4->si[back_conn].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003371 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
3372 return 0;
3373
3374 sess = SSL_get_session(conn->xprt_ctx);
Emeric Brunfe68f682012-10-16 14:59:28 +02003375 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
3388static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02003389smp_fetch_ssl_fc_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003390 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7875d092012-09-10 08:20:03 +02003391{
3392#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003393 struct connection *conn;
3394
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003395 smp->flags = SMP_F_CONST;
3396 smp->type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02003397
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003398 if (!l4)
Willy Tarreau7875d092012-09-10 08:20:03 +02003399 return 0;
3400
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003401 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 Tarreau3e394c92012-09-14 23:56:58 +02003406 if (!smp->data.str.str)
3407 return 0;
3408
Willy Tarreau7875d092012-09-10 08:20:03 +02003409 smp->data.str.len = strlen(smp->data.str.str);
3410 return 1;
3411#else
3412 return 0;
3413#endif
3414}
3415
David Sc1ad52e2014-04-08 18:48:47 -04003416static int
3417smp_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 Brun645ae792014-04-30 14:21:06 +02003421 int back_conn = (kw[4] == 'b') ? 1 : 0;
David Sc1ad52e2014-04-08 18:48:47 -04003422 struct connection *conn;
3423 int finished_len;
David Sc1ad52e2014-04-08 18:48:47 -04003424 struct chunk *finished_trash;
David Sc1ad52e2014-04-08 18:48:47 -04003425
3426 smp->flags = 0;
3427
3428 if (!l4)
3429 return 0;
3430
Emeric Brun645ae792014-04-30 14:21:06 +02003431 conn = objt_conn(l4->si[back_conn].end);
David Sc1ad52e2014-04-08 18:48:47 -04003432 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 Brunb73a9b02014-04-30 18:49:19 +02003449 finished_trash->len = finished_len;
3450 smp->data.str = *finished_trash;
3451 smp->type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04003452
3453 return 1;
3454#else
3455 return 0;
3456#endif
3457}
3458
Emeric Brun2525b6b2012-10-18 15:59:43 +02003459/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02003460static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02003461smp_fetch_ssl_c_ca_err(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003462 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brunf282a812012-09-21 15:27:54 +02003463{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003464 struct connection *conn;
3465
3466 if (!l4)
Emeric Brunf282a812012-09-21 15:27:54 +02003467 return 0;
3468
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003469 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 Brunf282a812012-09-21 15:27:54 +02003474 smp->flags = SMP_F_MAY_CHANGE;
3475 return 0;
3476 }
3477
3478 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003479 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02003480 smp->flags = 0;
3481
3482 return 1;
3483}
3484
Emeric Brun2525b6b2012-10-18 15:59:43 +02003485/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02003486static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02003487smp_fetch_ssl_c_ca_err_depth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003488 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brunf282a812012-09-21 15:27:54 +02003489{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003490 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 Brunf282a812012-09-21 15:27:54 +02003497 return 0;
3498
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003499 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02003500 smp->flags = SMP_F_MAY_CHANGE;
3501 return 0;
3502 }
3503
3504 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003505 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CAEDEPTH(conn->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02003506 smp->flags = 0;
3507
3508 return 1;
3509}
3510
Emeric Brun2525b6b2012-10-18 15:59:43 +02003511/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02003512static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02003513smp_fetch_ssl_c_err(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003514 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brunf282a812012-09-21 15:27:54 +02003515{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003516 struct connection *conn;
3517
3518 if (!l4)
Emeric Brunf282a812012-09-21 15:27:54 +02003519 return 0;
3520
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003521 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 Brunf282a812012-09-21 15:27:54 +02003526 smp->flags = SMP_F_MAY_CHANGE;
3527 return 0;
3528 }
3529
3530 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003531 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02003532 smp->flags = 0;
3533
3534 return 1;
3535}
3536
Emeric Brun2525b6b2012-10-18 15:59:43 +02003537/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02003538static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02003539smp_fetch_ssl_c_verify(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003540 const struct arg *args, struct sample *smp, const char *kw)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02003541{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003542 struct connection *conn;
3543
3544 if (!l4)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02003545 return 0;
3546
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003547 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 Brunbaf8ffb2012-09-21 15:27:20 +02003552 smp->flags = SMP_F_MAY_CHANGE;
3553 return 0;
3554 }
3555
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003556 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02003557 return 0;
3558
3559 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003560 smp->data.uint = (unsigned int)SSL_get_verify_result(conn->xprt_ctx);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02003561 smp->flags = 0;
3562
3563 return 1;
3564}
3565
Emeric Brunfb510ea2012-10-05 12:00:26 +02003566/* parse the "ca-file" bind keyword */
3567static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Emeric Brund94b3fe2012-09-20 18:23:56 +02003568{
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 Brunef42d922012-10-11 16:11:36 +02003575 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 Brunc8e8d122012-10-02 18:42:10 +02003579
Emeric Brund94b3fe2012-09-20 18:23:56 +02003580 return 0;
3581}
3582
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003583/* parse the "ciphers" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02003584static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003585{
3586 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003587 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003588 return ERR_ALERT | ERR_FATAL;
3589 }
3590
Emeric Brun76d88952012-10-05 15:47:31 +02003591 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02003592 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003593 return 0;
3594}
3595
3596/* parse the "crt" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02003597static int bind_parse_crt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003598{
Willy Tarreau38011032013-08-13 16:59:39 +02003599 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02003600
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003601 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003602 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003603 return ERR_ALERT | ERR_FATAL;
3604 }
3605
Emeric Brunc8e8d122012-10-02 18:42:10 +02003606 if ((*args[cur_arg + 1] != '/' ) && global.crt_base) {
Willy Tarreau38011032013-08-13 16:59:39 +02003607 if ((strlen(global.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02003608 memprintf(err, "'%s' : path too long", args[cur_arg]);
3609 return ERR_ALERT | ERR_FATAL;
3610 }
Willy Tarreaub75d6922014-04-14 18:05:41 +02003611 snprintf(path, sizeof(path), "%s/%s", global.crt_base, args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02003612 if (ssl_sock_load_cert(path, conf, px, err) > 0)
3613 return ERR_ALERT | ERR_FATAL;
3614
3615 return 0;
3616 }
3617
Willy Tarreau4348fad2012-09-20 16:48:07 +02003618 if (ssl_sock_load_cert(args[cur_arg + 1], conf, px, err) > 0)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003619 return ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02003620
3621 return 0;
3622}
3623
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003624/* parse the "crt-list" bind keyword */
3625static 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 Tarreauad1731d2013-04-02 17:35:58 +02003632 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 Hocdetfe616562013-01-22 15:31:15 +01003634 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003635 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003636
3637 return 0;
3638}
3639
Emeric Brunfb510ea2012-10-05 12:00:26 +02003640/* parse the "crl-file" bind keyword */
3641static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Emeric Brund94b3fe2012-09-20 18:23:56 +02003642{
Emeric Brun051cdab2012-10-02 19:25:50 +02003643#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 Brund94b3fe2012-09-20 18:23:56 +02003648 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 Brun2b58d042012-09-20 17:10:03 +02003653
Emeric Brunef42d922012-10-11 16:11:36 +02003654 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 Brunc8e8d122012-10-02 18:42:10 +02003658
Emeric Brun2b58d042012-09-20 17:10:03 +02003659 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02003660#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02003661}
3662
3663/* parse the "ecdhe" bind keyword keywords */
3664static 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 Tarreau79eeafa2012-09-14 07:53:05 +02003682
3683 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02003684#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003685}
3686
Emeric Brun81c00f02012-09-21 14:31:21 +02003687/* parse the "crt_ignerr" and "ca_ignerr" bind keywords */
3688static 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 Brun2cb7ae52012-10-05 14:14:21 +02003722 return 0;
3723}
3724
3725/* parse the "force-sslv3" bind keyword */
3726static 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 */
3733static 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 Brun2d0c4822012-10-02 13:45:20 +02003736 return 0;
3737}
3738
Emeric Brun2cb7ae52012-10-05 14:14:21 +02003739/* parse the "force-tlsv11" bind keyword */
3740static 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 */
3753static 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 Brun2d0c4822012-10-02 13:45:20 +02003766/* parse the "no-tls-tickets" bind keyword */
3767static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
3768{
Emeric Brun89675492012-10-05 13:48:26 +02003769 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02003770 return 0;
3771}
3772
Emeric Brun2d0c4822012-10-02 13:45:20 +02003773
Emeric Brun9b3009b2012-10-05 11:55:06 +02003774/* parse the "no-sslv3" bind keyword */
3775static int bind_parse_no_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003776{
Emeric Brun89675492012-10-05 13:48:26 +02003777 conf->ssl_options |= BC_SSL_O_NO_SSLV3;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003778 return 0;
3779}
3780
Emeric Brun9b3009b2012-10-05 11:55:06 +02003781/* parse the "no-tlsv10" bind keyword */
3782static int bind_parse_no_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Emeric Brunc0ff4922012-09-28 19:37:02 +02003783{
Emeric Brun89675492012-10-05 13:48:26 +02003784 conf->ssl_options |= BC_SSL_O_NO_TLSV10;
Emeric Brunc0ff4922012-09-28 19:37:02 +02003785 return 0;
3786}
3787
Emeric Brun9b3009b2012-10-05 11:55:06 +02003788/* parse the "no-tlsv11" bind keyword */
3789static int bind_parse_no_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Emeric Brunc0ff4922012-09-28 19:37:02 +02003790{
Emeric Brun89675492012-10-05 13:48:26 +02003791 conf->ssl_options |= BC_SSL_O_NO_TLSV11;
Emeric Brunc0ff4922012-09-28 19:37:02 +02003792 return 0;
3793}
3794
Emeric Brun9b3009b2012-10-05 11:55:06 +02003795/* parse the "no-tlsv12" bind keyword */
3796static int bind_parse_no_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003797{
Emeric Brun89675492012-10-05 13:48:26 +02003798 conf->ssl_options |= BC_SSL_O_NO_TLSV12;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003799 return 0;
3800}
3801
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02003802/* parse the "npn" bind keyword */
3803static 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 Tarreauab861d32013-04-02 02:30:41 +02003850/* parse the "alpn" bind keyword */
3851static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
3852{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01003853#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02003854 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 Tarreau79eeafa2012-09-14 07:53:05 +02003898/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02003899static int bind_parse_ssl(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003900{
Willy Tarreau81796be2012-09-22 19:11:47 +02003901 struct listener *l;
3902
Willy Tarreau4348fad2012-09-20 16:48:07 +02003903 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02003904
3905 if (global.listen_default_ciphers && !conf->ciphers)
3906 conf->ciphers = strdup(global.listen_default_ciphers);
3907
Willy Tarreau81796be2012-09-22 19:11:47 +02003908 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003909 l->xprt = &ssl_sock;
Willy Tarreau81796be2012-09-22 19:11:47 +02003910
Willy Tarreau79eeafa2012-09-14 07:53:05 +02003911 return 0;
3912}
3913
Emmanuel Hocdet65623372013-01-24 17:17:15 +01003914/* parse the "strict-sni" bind keyword */
3915static 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 Brund94b3fe2012-09-20 18:23:56 +02003921/* parse the "verify" bind keyword */
3922static 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 Brun850efd52014-01-29 12:24:34 +01003931 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02003932 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01003933 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02003934 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01003935 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02003936 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 Tarreau92faadf2012-10-10 23:04:25 +02003946/************** "server" keywords ****************/
3947
Emeric Brunef42d922012-10-11 16:11:36 +02003948/* parse the "ca-file" server keyword */
3949static 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 Tarreau92faadf2012-10-10 23:04:25 +02003965/* parse the "check-ssl" server keyword */
3966static 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 */
3975static 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 Brunef42d922012-10-11 16:11:36 +02003987/* parse the "crl-file" server keyword */
3988static 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 Bruna7aa3092012-10-26 12:58:00 +02004010/* parse the "crt" server keyword */
4011static 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 Brunef42d922012-10-11 16:11:36 +02004026
Willy Tarreau92faadf2012-10-10 23:04:25 +02004027/* parse the "force-sslv3" server keyword */
4028static 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 */
4035static 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 */
4042static 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 */
4055static 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 */
4068static 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 */
4075static 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 */
4082static 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 */
4089static 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 Brunf9c5c472012-10-11 15:28:34 +02004095/* parse the "no-tls-tickets" server keyword */
4096static 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 Safb76832014-05-08 23:42:08 -04004101/* parse the "send-proxy-v2-ssl" server keyword */
4102static 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 */
4110static 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 Brunf9c5c472012-10-11 15:28:34 +02004117
Willy Tarreau92faadf2012-10-10 23:04:25 +02004118/* parse the "ssl" server keyword */
4119static 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 Brunef42d922012-10-11 16:11:36 +02004127/* parse the "verify" server keyword */
4128static 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 Brun850efd52014-01-29 12:24:34 +01004137 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02004138 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01004139 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02004140 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 Broderbe554312013-06-27 00:05:25 -07004147 return 0;
4148}
4149
4150/* parse the "verifyhost" server keyword */
4151static 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 Brunef42d922012-10-11 16:11:36 +02004161 return 0;
4162}
4163
Willy Tarreau7875d092012-09-10 08:20:03 +02004164/* Note: must not be declared <const> as its list will be overwritten.
4165 * Please take care of keeping this list alphabetically sorted.
4166 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02004167static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +02004168 { "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 Brunb73a9b02014-04-30 18:49:19 +02004172 { "ssl_bc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Emeric Brun645ae792014-04-30 14:21:06 +02004173 { "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 Tarreau80aca902013-01-07 15:42:20 +01004175 { "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 Brunba841a12014-04-30 17:05:08 +02004178 { "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 Tarreau80aca902013-01-07 15:42:20 +01004186 { "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 Brunba841a12014-04-30 17:05:08 +02004188 { "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 Brun55f4fa82014-04-30 17:11:25 +02004196 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +02004197 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01004198 { "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 FOURNIER7654c9f2013-12-17 00:20:33 +01004200 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01004201 { "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 Tarreaua33c6542012-10-15 13:19:06 +02004203#ifdef OPENSSL_NPN_NEGOTIATED
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004204 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +02004205#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01004206#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004207 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +02004208#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004209 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunb73a9b02014-04-30 18:49:19 +02004210 { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01004211 { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_UINT, SMP_USE_L5CLI },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004212 { "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 Tarreau7875d092012-09-10 08:20:03 +02004214 { 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 Tarreaudc13c112013-06-21 23:16:39 +02004220static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +01004221 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
4222 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +01004223 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +02004224}};
4225
Willy Tarreau79eeafa2012-09-14 07:53:05 +02004226/* 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 Tarreau51fb7652012-09-18 18:24:39 +02004233static struct bind_kw_list bind_kws = { "SSL", { }, {
Willy Tarreauab861d32013-04-02 02:30:41 +02004234 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emeric Brunfb510ea2012-10-05 12:00:26 +02004235 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
Emeric Brun2d0c4822012-10-02 13:45:20 +02004236 { "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 Brunfb510ea2012-10-05 12:00:26 +02004238 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
Emeric Brun2d0c4822012-10-02 13:45:20 +02004239 { "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 Hocdetfe616562013-01-22 15:31:15 +01004241 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
Emeric Brun2d0c4822012-10-02 13:45:20 +02004242 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emeric Brun2cb7ae52012-10-05 14:14:21 +02004243 { "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 Brun9b3009b2012-10-05 11:55:06 +02004247 { "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 Brun2d0c4822012-10-02 13:45:20 +02004251 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
Emeric Brun2d0c4822012-10-02 13:45:20 +02004252 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01004253 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
Emeric Brun2d0c4822012-10-02 13:45:20 +02004254 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02004255 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
Willy Tarreau79eeafa2012-09-14 07:53:05 +02004256 { NULL, NULL, 0 },
4257}};
Emeric Brun46591952012-05-18 15:47:34 +02004258
Willy Tarreau92faadf2012-10-10 23:04:25 +02004259/* 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 */
4266static struct srv_kw_list srv_kws = { "SSL", { }, {
Emeric Brunef42d922012-10-11 16:11:36 +02004267 { "ca-file", srv_parse_ca_file, 1, 0 }, /* set CAfile to process verify server cert */
Emeric Brunecc91fe2012-10-11 15:05:10 +02004268 { "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 Brunef42d922012-10-11 16:11:36 +02004270 { "crl-file", srv_parse_crl_file, 1, 0 }, /* set certificate revocation list file use on server cert verify */
Emeric Bruna7aa3092012-10-26 12:58:00 +02004271 { "crt", srv_parse_crt, 1, 0 }, /* set client certificate */
Emeric Brunecc91fe2012-10-11 15:05:10 +02004272 { "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 Brunf9c5c472012-10-11 15:28:34 +02004280 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 0 }, /* disable session resumption tickets */
David Safb76832014-05-08 23:42:08 -04004281 { "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 Brunecc91fe2012-10-11 15:05:10 +02004283 { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */
Emeric Brunef42d922012-10-11 16:11:36 +02004284 { "verify", srv_parse_verify, 1, 0 }, /* set SSL verify method */
Evan Broderbe554312013-06-27 00:05:25 -07004285 { "verifyhost", srv_parse_verifyhost, 1, 0 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +02004286 { NULL, NULL, 0, 0 },
4287}};
4288
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004289/* transport-layer operations for SSL sockets */
4290struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +02004291 .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 Tarreau92faadf2012-10-10 23:04:25 +02004302static void __ssl_sock_init(void)
4303{
Emeric Brun46591952012-05-18 15:47:34 +02004304 STACK_OF(SSL_COMP)* cm;
4305
Willy Tarreau610f04b2014-02-13 11:36:41 +01004306#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 Brun46591952012-05-18 15:47:34 +02004317 SSL_library_init();
4318 cm = SSL_COMP_get_compression_methods();
4319 sk_SSL_COMP_zero(cm);
Willy Tarreau7875d092012-09-10 08:20:03 +02004320 sample_register_fetches(&sample_fetch_keywords);
4321 acl_register_keywords(&acl_kws);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02004322 bind_register_keywords(&bind_kws);
Willy Tarreau92faadf2012-10-10 23:04:25 +02004323 srv_register_keywords(&srv_kws);
Emeric Brun46591952012-05-18 15:47:34 +02004324}
4325
4326/*
4327 * Local variables:
4328 * c-indent-level: 8
4329 * c-basic-offset: 8
4330 * End:
4331 */