blob: 3aeba744a7c322a1cc5f09fe7e74b0887bf7e959 [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 Brun46591952012-05-18 15:47:34 +020047
48#include <common/buffer.h>
49#include <common/compat.h>
50#include <common/config.h>
51#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020052#include <common/errors.h>
Emeric Brun46591952012-05-18 15:47:34 +020053#include <common/standard.h>
54#include <common/ticks.h>
55#include <common/time.h>
56
Emeric Brunfc0421f2012-09-07 17:30:07 +020057#include <ebsttree.h>
58
59#include <types/global.h>
60#include <types/ssl_sock.h>
61
Willy Tarreau7875d092012-09-10 08:20:03 +020062#include <proto/acl.h>
63#include <proto/arg.h>
Emeric Brun46591952012-05-18 15:47:34 +020064#include <proto/connection.h>
65#include <proto/fd.h>
66#include <proto/freq_ctr.h>
67#include <proto/frontend.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020068#include <proto/listener.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020069#include <proto/server.h>
Emeric Brun46591952012-05-18 15:47:34 +020070#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020071#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020072#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020073#include <proto/ssl_sock.h>
74#include <proto/task.h>
75
Emeric Brune64aef12012-09-21 13:15:06 +020076#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brunf282a812012-09-21 15:27:54 +020077/* bits 0xFFFF0000 are reserved to store verify errors */
78
79/* Verify errors macros */
80#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
81#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
82#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
83
84#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
85#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
86#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +020087
Willy Tarreau403edff2012-09-06 11:58:37 +020088static int sslconns = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +020089
90void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
91{
92 struct connection *conn = (struct connection *)SSL_get_app_data(ssl);
93 (void)ret; /* shut gcc stupid warning */
94
95 if (where & SSL_CB_HANDSHAKE_START) {
96 /* Disable renegotiation (CVE-2009-3555) */
Willy Tarreau20879a02012-12-03 16:32:10 +010097 if (conn->flags & CO_FL_CONNECTED) {
Emeric Brune1f38db2012-09-03 20:36:47 +020098 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +010099 conn->err_code = CO_ER_SSL_RENEG;
100 }
Emeric Brune1f38db2012-09-03 20:36:47 +0200101 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200102}
103
Emeric Brune64aef12012-09-21 13:15:06 +0200104/* Callback is called for each certificate of the chain during a verify
105 ok is set to 1 if preverify detect no error on current certificate.
106 Returns 0 to break the handshake, 1 otherwise. */
107int ssl_sock_verifycbk(int ok, X509_STORE_CTX *x_store)
108{
109 SSL *ssl;
110 struct connection *conn;
Emeric Brun81c00f02012-09-21 14:31:21 +0200111 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +0200112
113 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
114 conn = (struct connection *)SSL_get_app_data(ssl);
115
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200116 conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +0200117
Emeric Brun81c00f02012-09-21 14:31:21 +0200118 if (ok) /* no errors */
119 return ok;
120
121 depth = X509_STORE_CTX_get_error_depth(x_store);
122 err = X509_STORE_CTX_get_error(x_store);
123
124 /* check if CA error needs to be ignored */
125 if (depth > 0) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200126 if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) {
127 conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
128 conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +0200129 }
130
Emeric Brun1eb20ef2012-12-03 13:24:29 +0100131 if (objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
132 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +0200133 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +0100134 }
Emeric Brun81c00f02012-09-21 14:31:21 +0200135
Willy Tarreau20879a02012-12-03 16:32:10 +0100136 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +0200137 return 0;
138 }
139
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200140 if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st))
141 conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +0200142
Emeric Brun81c00f02012-09-21 14:31:21 +0200143 /* check if certificate error needs to be ignored */
Emeric Brun1eb20ef2012-12-03 13:24:29 +0100144 if (objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
145 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +0200146 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +0100147 }
Emeric Brun81c00f02012-09-21 14:31:21 +0200148
Willy Tarreau20879a02012-12-03 16:32:10 +0100149 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +0200150 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +0200151}
152
Willy Tarreau6c9a3d52012-10-18 18:57:14 +0200153#ifdef OPENSSL_NPN_NEGOTIATED
154/* This callback is used so that the server advertises the list of
155 * negociable protocols for NPN.
156 */
157static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
158 unsigned int *len, void *arg)
159{
160 struct bind_conf *conf = arg;
161
162 *data = (const unsigned char *)conf->npn_str;
163 *len = conf->npn_len;
164 return SSL_TLSEXT_ERR_OK;
165}
166#endif
167
Emeric Brunfc0421f2012-09-07 17:30:07 +0200168#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
169/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
170 * warning when no match is found, which implies the default (first) cert
171 * will keep being used.
172 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200173static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200174{
175 const char *servername;
176 const char *wildp = NULL;
177 struct ebmb_node *node;
178 int i;
179 (void)al; /* shut gcc stupid warning */
180
181 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +0100182 if (!servername) {
183 if (s->strict_sni)
184 return SSL_TLSEXT_ERR_ALERT_FATAL;
185 else
186 return SSL_TLSEXT_ERR_NOACK;
187 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200188
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100189 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +0200190 if (!servername[i])
191 break;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100192 trash.str[i] = tolower(servername[i]);
193 if (!wildp && (trash.str[i] == '.'))
194 wildp = &trash.str[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +0200195 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100196 trash.str[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200197
198 /* lookup in full qualified names */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100199 node = ebst_lookup(&s->sni_ctx, trash.str);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200200 if (!node) {
Emmanuel Hocdet65623372013-01-24 17:17:15 +0100201 if (!wildp) {
202 if (s->strict_sni)
203 return SSL_TLSEXT_ERR_ALERT_FATAL;
204 else
205 return SSL_TLSEXT_ERR_ALERT_WARNING;
206 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200207 /* lookup in full wildcards names */
208 node = ebst_lookup(&s->sni_w_ctx, wildp);
Emmanuel Hocdet65623372013-01-24 17:17:15 +0100209 if (!node) {
210 if (s->strict_sni)
211 return SSL_TLSEXT_ERR_ALERT_FATAL;
212 else
213 return SSL_TLSEXT_ERR_ALERT_WARNING;
214 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200215 }
216
217 /* switch ctx */
218 SSL_set_SSL_CTX(ssl, container_of(node, struct sni_ctx, name)->ctx);
219 return SSL_TLSEXT_ERR_OK;
220}
221#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
222
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200223#ifndef OPENSSL_NO_DH
224/* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1
225 if an error occured, and 0 if parameter not found. */
226int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file)
227{
228 int ret = -1;
229 BIO *in;
230 DH *dh = NULL;
231
232 in = BIO_new(BIO_s_file());
233 if (in == NULL)
234 goto end;
235
236 if (BIO_read_filename(in, file) <= 0)
237 goto end;
238
239 dh = PEM_read_bio_DHparams(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata);
240 if (dh) {
241 SSL_CTX_set_tmp_dh(ctx, dh);
242 ret = 1;
243 goto end;
244 }
245
246 ret = 0; /* DH params not found */
Emeric Brun644cde02012-12-14 11:21:13 +0100247
248 /* Clear openssl global errors stack */
249 ERR_clear_error();
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200250end:
251 if (dh)
252 DH_free(dh);
253
254 if (in)
255 BIO_free(in);
256
257 return ret;
258}
259#endif
260
Emeric Brunfc0421f2012-09-07 17:30:07 +0200261/* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if
262 * an early error happens and the caller must call SSL_CTX_free() by itelf.
263 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200264int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200265{
266 BIO *in;
267 X509 *x = NULL, *ca;
268 int i, len, err;
269 int ret = -1;
270 int order = 0;
271 X509_NAME *xname;
272 char *str;
273 struct sni_ctx *sc;
274#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
275 STACK_OF(GENERAL_NAME) *names;
276#endif
277
278 in = BIO_new(BIO_s_file());
279 if (in == NULL)
280 goto end;
281
282 if (BIO_read_filename(in, file) <= 0)
283 goto end;
284
285 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata);
286 if (x == NULL)
287 goto end;
288
289#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
290 names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
291 if (names) {
292 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
293 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
294 if (name->type == GEN_DNS) {
295 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
296 if ((len = strlen(str))) {
297 int j;
298
299 if (*str != '*') {
300 sc = malloc(sizeof(struct sni_ctx) + len + 1);
301 for (j = 0; j < len; j++)
302 sc->name.key[j] = tolower(str[j]);
303 sc->name.key[len] = 0;
304 sc->order = order++;
305 sc->ctx = ctx;
306 ebst_insert(&s->sni_ctx, &sc->name);
307 }
308 else {
309 sc = malloc(sizeof(struct sni_ctx) + len);
310 for (j = 1; j < len; j++)
311 sc->name.key[j-1] = tolower(str[j]);
312 sc->name.key[len-1] = 0;
313 sc->order = order++;
314 sc->ctx = ctx;
315 ebst_insert(&s->sni_w_ctx, &sc->name);
316 }
317 }
318 OPENSSL_free(str);
319 }
320 }
321 }
322 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
323 }
324#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
325
326 xname = X509_get_subject_name(x);
327 i = -1;
328 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
329 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
330 if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) {
331 if ((len = strlen(str))) {
332 int j;
333
334 if (*str != '*') {
335 sc = malloc(sizeof(struct sni_ctx) + len + 1);
336 for (j = 0; j < len; j++)
337 sc->name.key[j] = tolower(str[j]);
338 sc->name.key[len] = 0;
339 sc->order = order++;
340 sc->ctx = ctx;
341 ebst_insert(&s->sni_ctx, &sc->name);
342 }
343 else {
344 sc = malloc(sizeof(struct sni_ctx) + len);
345 for (j = 1; j < len; j++)
346 sc->name.key[j-1] = tolower(str[j]);
347 sc->name.key[len-1] = 0;
348 sc->order = order++;
349 sc->ctx = ctx;
350 ebst_insert(&s->sni_w_ctx, &sc->name);
351 }
352 }
353 OPENSSL_free(str);
354 }
355 }
356
357 ret = 0; /* the caller must not free the SSL_CTX argument anymore */
358 if (!SSL_CTX_use_certificate(ctx, x))
359 goto end;
360
361 if (ctx->extra_certs != NULL) {
362 sk_X509_pop_free(ctx->extra_certs, X509_free);
363 ctx->extra_certs = NULL;
364 }
365
366 while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata))) {
367 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
368 X509_free(ca);
369 goto end;
370 }
371 }
372
373 err = ERR_get_error();
374 if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
375 /* we successfully reached the last cert in the file */
376 ret = 1;
377 }
378 ERR_clear_error();
379
380end:
381 if (x)
382 X509_free(x);
383
384 if (in)
385 BIO_free(in);
386
387 return ret;
388}
389
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200390static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200391{
392 int ret;
393 SSL_CTX *ctx;
394
395 ctx = SSL_CTX_new(SSLv23_server_method());
396 if (!ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200397 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
398 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200399 return 1;
400 }
401
402 if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200403 memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
404 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200405 SSL_CTX_free(ctx);
406 return 1;
407 }
408
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200409 ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200410 if (ret <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200411 memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
412 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200413 if (ret < 0) /* serious error, must do that ourselves */
414 SSL_CTX_free(ctx);
415 return 1;
416 }
Emeric Brun61694ab2012-10-26 13:35:33 +0200417
418 if (SSL_CTX_check_private_key(ctx) <= 0) {
419 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
420 err && *err ? *err : "", path);
421 return 1;
422 }
423
Emeric Brunfc0421f2012-09-07 17:30:07 +0200424 /* we must not free the SSL_CTX anymore below, since it's already in
425 * the tree, so it will be discovered and cleaned in time.
426 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200427#ifndef OPENSSL_NO_DH
428 ret = ssl_sock_load_dh_params(ctx, path);
429 if (ret < 0) {
430 if (err)
431 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
432 *err ? *err : "", path);
433 return 1;
434 }
435#endif
436
Emeric Brunfc0421f2012-09-07 17:30:07 +0200437#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200438 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200439 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
440 err && *err ? *err : "");
Emeric Brunfc0421f2012-09-07 17:30:07 +0200441 return 1;
442 }
443#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200444 if (!bind_conf->default_ctx)
445 bind_conf->default_ctx = ctx;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200446
447 return 0;
448}
449
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200450int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200451{
452 struct dirent *de;
453 DIR *dir;
454 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +0100455 char *end;
456 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +0200457 int cfgerr = 0;
458
459 if (!(dir = opendir(path)))
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200460 return ssl_sock_load_cert_file(path, bind_conf, curproxy, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200461
462 /* strip trailing slashes, including first one */
463 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
464 *end = 0;
465
Emeric Brunfc0421f2012-09-07 17:30:07 +0200466 while ((de = readdir(dir))) {
Willy Tarreauee2663b2012-12-06 11:36:59 +0100467 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200468 if (stat(fp, &buf) != 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200469 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
470 err && *err ? *err : "", fp, strerror(errno));
Emeric Brunfc0421f2012-09-07 17:30:07 +0200471 cfgerr++;
472 continue;
473 }
474 if (!S_ISREG(buf.st_mode))
475 continue;
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200476 cfgerr += ssl_sock_load_cert_file(fp, bind_conf, curproxy, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200477 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200478 closedir(dir);
479 return cfgerr;
480}
481
Thierry Fournier383085f2013-01-24 14:15:43 +0100482/* Make sure openssl opens /dev/urandom before the chroot. The work is only
483 * done once. Zero is returned if the operation fails. No error is returned
484 * if the random is said as not implemented, because we expect that openssl
485 * will use another method once needed.
486 */
487static int ssl_initialize_random()
488{
489 unsigned char random;
490 static int random_initialized = 0;
491
492 if (!random_initialized && RAND_bytes(&random, 1) != 0)
493 random_initialized = 1;
494
495 return random_initialized;
496}
497
Emeric Brunfc0421f2012-09-07 17:30:07 +0200498#ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */
499#define SSL_OP_CIPHER_SERVER_PREFERENCE 0
500#endif
501
502#ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */
503#define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0
Willy Tarreau7d588ee2012-11-26 18:47:31 +0100504#define SSL_renegotiate_pending(arg) 0
Emeric Brunfc0421f2012-09-07 17:30:07 +0200505#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200506#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */
507#define SSL_OP_SINGLE_ECDH_USE 0
508#endif
Emeric Brun2d0c4822012-10-02 13:45:20 +0200509#ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */
510#define SSL_OP_NO_TICKET 0
511#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200512#ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */
513#define SSL_OP_NO_COMPRESSION 0
514#endif
Emeric Brunc0ff4922012-09-28 19:37:02 +0200515#ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */
516#define SSL_OP_NO_TLSv1_1 0
517#endif
518#ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */
519#define SSL_OP_NO_TLSv1_2 0
520#endif
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200521#ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */
522#define SSL_OP_SINGLE_DH_USE 0
523#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200524#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */
525#define SSL_OP_SINGLE_ECDH_USE 0
526#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200527#ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */
528#define SSL_MODE_RELEASE_BUFFERS 0
529#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200530int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, SSL_CTX *ctx, struct proxy *curproxy)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200531{
532 int cfgerr = 0;
533 int ssloptions =
534 SSL_OP_ALL | /* all known workarounds for bugs */
535 SSL_OP_NO_SSLv2 |
536 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200537 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +0200538 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +0200539 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
540 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200541 int sslmode =
542 SSL_MODE_ENABLE_PARTIAL_WRITE |
543 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
544 SSL_MODE_RELEASE_BUFFERS;
545
Thierry Fournier383085f2013-01-24 14:15:43 +0100546 /* Make sure openssl opens /dev/urandom before the chroot */
547 if (!ssl_initialize_random()) {
548 Alert("OpenSSL random data generator initialization failed.\n");
549 cfgerr++;
550 }
551
Emeric Brun89675492012-10-05 13:48:26 +0200552 if (bind_conf->ssl_options & BC_SSL_O_NO_SSLV3)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200553 ssloptions |= SSL_OP_NO_SSLv3;
Emeric Brun89675492012-10-05 13:48:26 +0200554 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV10)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200555 ssloptions |= SSL_OP_NO_TLSv1;
Emeric Brun89675492012-10-05 13:48:26 +0200556 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV11)
Emeric Brunc0ff4922012-09-28 19:37:02 +0200557 ssloptions |= SSL_OP_NO_TLSv1_1;
Emeric Brun89675492012-10-05 13:48:26 +0200558 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV12)
Emeric Brunc0ff4922012-09-28 19:37:02 +0200559 ssloptions |= SSL_OP_NO_TLSv1_2;
Emeric Brun89675492012-10-05 13:48:26 +0200560 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
Emeric Brun2d0c4822012-10-02 13:45:20 +0200561 ssloptions |= SSL_OP_NO_TICKET;
Emeric Brun2cb7ae52012-10-05 14:14:21 +0200562 if (bind_conf->ssl_options & BC_SSL_O_USE_SSLV3)
563 SSL_CTX_set_ssl_version(ctx, SSLv3_server_method());
564 if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV10)
565 SSL_CTX_set_ssl_version(ctx, TLSv1_server_method());
566#if SSL_OP_NO_TLSv1_1
567 if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV11)
568 SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method());
569#endif
570#if SSL_OP_NO_TLSv1_2
571 if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV12)
572 SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method());
573#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200574
575 SSL_CTX_set_options(ctx, ssloptions);
576 SSL_CTX_set_mode(ctx, sslmode);
Emeric Brune64aef12012-09-21 13:15:06 +0200577 SSL_CTX_set_verify(ctx, bind_conf->verify ? bind_conf->verify : SSL_VERIFY_NONE, ssl_sock_verifycbk);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200578 if (bind_conf->verify & SSL_VERIFY_PEER) {
Emeric Brunfb510ea2012-10-05 12:00:26 +0200579 if (bind_conf->ca_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200580 /* load CAfile to verify */
Emeric Brunfb510ea2012-10-05 12:00:26 +0200581 if (!SSL_CTX_load_verify_locations(ctx, bind_conf->ca_file, NULL)) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200582 Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
Emeric Brunfb510ea2012-10-05 12:00:26 +0200583 curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200584 cfgerr++;
585 }
586 /* set CA names fo client cert request, function returns void */
Emeric Brunfb510ea2012-10-05 12:00:26 +0200587 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(bind_conf->ca_file));
Emeric Brund94b3fe2012-09-20 18:23:56 +0200588 }
Emeric Brun051cdab2012-10-02 19:25:50 +0200589#ifdef X509_V_FLAG_CRL_CHECK
Emeric Brunfb510ea2012-10-05 12:00:26 +0200590 if (bind_conf->crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200591 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
592
Emeric Brunfb510ea2012-10-05 12:00:26 +0200593 if (!store || !X509_STORE_load_locations(store, bind_conf->crl_file, NULL)) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200594 Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
Emeric Brunfb510ea2012-10-05 12:00:26 +0200595 curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200596 cfgerr++;
597 }
Emeric Brun561e5742012-10-02 15:20:55 +0200598 else {
599 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
600 }
Emeric Brund94b3fe2012-09-20 18:23:56 +0200601 }
Emeric Brun051cdab2012-10-02 19:25:50 +0200602#endif
Emeric Brun644cde02012-12-14 11:21:13 +0100603 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +0200604 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200605
Emeric Brun4f65bff2012-11-16 15:11:00 +0100606 if (global.tune.ssllifetime)
607 SSL_CTX_set_timeout(ctx, global.tune.ssllifetime);
608
Emeric Brunfc0421f2012-09-07 17:30:07 +0200609 shared_context_set_cache(ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200610 if (bind_conf->ciphers &&
611 !SSL_CTX_set_cipher_list(ctx, bind_conf->ciphers)) {
Emeric Brunfc0421f2012-09-07 17:30:07 +0200612 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 +0200613 curproxy->id, bind_conf->ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200614 cfgerr++;
615 }
616
617 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +0200618#ifdef OPENSSL_NPN_NEGOTIATED
619 if (bind_conf->npn_str)
620 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, bind_conf);
621#endif
622
Emeric Brunfc0421f2012-09-07 17:30:07 +0200623#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
624 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200625 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200626#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200627#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
628 if (bind_conf->ecdhe) {
629 int i;
630 EC_KEY *ecdh;
631
632 i = OBJ_sn2nid(bind_conf->ecdhe);
633 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
634 Alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
635 curproxy->id, bind_conf->ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
636 cfgerr++;
637 }
638 else {
639 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
640 EC_KEY_free(ecdh);
641 }
642 }
643#endif
644
Emeric Brunfc0421f2012-09-07 17:30:07 +0200645 return cfgerr;
646}
647
Emeric Brun94324a42012-10-11 14:00:19 +0200648/* prepare ssl context from servers options. Returns an error count */
649int ssl_sock_prepare_srv_ctx(struct server *srv, struct proxy *curproxy)
650{
651 int cfgerr = 0;
652 int options =
653 SSL_OP_ALL | /* all known workarounds for bugs */
654 SSL_OP_NO_SSLv2 |
655 SSL_OP_NO_COMPRESSION;
656 int mode =
657 SSL_MODE_ENABLE_PARTIAL_WRITE |
658 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
659 SSL_MODE_RELEASE_BUFFERS;
660
Thierry Fournier383085f2013-01-24 14:15:43 +0100661 /* Make sure openssl opens /dev/urandom before the chroot */
662 if (!ssl_initialize_random()) {
663 Alert("OpenSSL random data generator initialization failed.\n");
664 cfgerr++;
665 }
666
Emeric Brun94324a42012-10-11 14:00:19 +0200667 /* Initiate SSL context for current server */
668 srv->ssl_ctx.reused_sess = NULL;
669 if (srv->use_ssl)
670 srv->xprt = &ssl_sock;
671 if (srv->check.use_ssl)
672 srv->check.xprt = &ssl_sock;
673
674 srv->ssl_ctx.ctx = SSL_CTX_new(SSLv23_client_method());
675 if (!srv->ssl_ctx.ctx) {
676 Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
677 proxy_type_str(curproxy), curproxy->id,
678 srv->id);
679 cfgerr++;
680 return cfgerr;
681 }
Emeric Bruna7aa3092012-10-26 12:58:00 +0200682 if (srv->ssl_ctx.client_crt) {
683 if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) {
684 Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
685 proxy_type_str(curproxy), curproxy->id,
686 srv->id, srv->ssl_ctx.client_crt);
687 cfgerr++;
688 }
689 else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) {
690 Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
691 proxy_type_str(curproxy), curproxy->id,
692 srv->id, srv->ssl_ctx.client_crt);
693 cfgerr++;
694 }
695 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
696 Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
697 proxy_type_str(curproxy), curproxy->id,
698 srv->id, srv->ssl_ctx.client_crt);
699 cfgerr++;
700 }
701 }
Emeric Brun94324a42012-10-11 14:00:19 +0200702
703 if (srv->ssl_ctx.options & SRV_SSL_O_NO_SSLV3)
704 options |= SSL_OP_NO_SSLv3;
705 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV10)
706 options |= SSL_OP_NO_TLSv1;
707 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV11)
708 options |= SSL_OP_NO_TLSv1_1;
709 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV12)
710 options |= SSL_OP_NO_TLSv1_2;
Emeric Brunf9c5c472012-10-11 15:28:34 +0200711 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
712 options |= SSL_OP_NO_TICKET;
Emeric Brun94324a42012-10-11 14:00:19 +0200713 if (srv->ssl_ctx.options & SRV_SSL_O_USE_SSLV3)
714 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, SSLv3_client_method());
715 if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV10)
716 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_client_method());
717#if SSL_OP_NO_TLSv1_1
718 if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV11)
719 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_1_client_method());
720#endif
721#if SSL_OP_NO_TLSv1_2
722 if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV12)
723 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_2_client_method());
724#endif
725
726 SSL_CTX_set_options(srv->ssl_ctx.ctx, options);
727 SSL_CTX_set_mode(srv->ssl_ctx.ctx, mode);
Emeric Brunef42d922012-10-11 16:11:36 +0200728 SSL_CTX_set_verify(srv->ssl_ctx.ctx, srv->ssl_ctx.verify ? srv->ssl_ctx.verify : SSL_VERIFY_NONE, NULL);
729 if (srv->ssl_ctx.verify & SSL_VERIFY_PEER) {
730 if (srv->ssl_ctx.ca_file) {
731 /* load CAfile to verify */
732 if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) {
733 Alert("Proxy '%s', server '%s' |%s:%d] unable to load CA file '%s'.\n",
734 curproxy->id, srv->id,
735 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
736 cfgerr++;
737 }
738 }
739#ifdef X509_V_FLAG_CRL_CHECK
740 if (srv->ssl_ctx.crl_file) {
741 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
742
743 if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) {
744 Alert("Proxy '%s', server '%s' |%s:%d] unable to configure CRL file '%s'.\n",
745 curproxy->id, srv->id,
746 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
747 cfgerr++;
748 }
749 else {
750 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
751 }
752 }
753#endif
754 }
755
Emeric Brun4f65bff2012-11-16 15:11:00 +0100756 if (global.tune.ssllifetime)
757 SSL_CTX_set_timeout(srv->ssl_ctx.ctx, global.tune.ssllifetime);
758
Emeric Brun94324a42012-10-11 14:00:19 +0200759 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF);
760 if (srv->ssl_ctx.ciphers &&
761 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
762 Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
763 curproxy->id, srv->id,
764 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
765 cfgerr++;
766 }
767
768 return cfgerr;
769}
770
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200771/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +0200772 * be NULL, in which case nothing is done. Returns the number of errors
773 * encountered.
774 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200775int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf, struct proxy *px)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200776{
777 struct ebmb_node *node;
778 struct sni_ctx *sni;
779 int err = 0;
780
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200781 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200782 return 0;
783
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200784 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200785 while (node) {
786 sni = ebmb_entry(node, struct sni_ctx, name);
787 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200788 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200789 node = ebmb_next(node);
790 }
791
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200792 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200793 while (node) {
794 sni = ebmb_entry(node, struct sni_ctx, name);
795 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200796 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200797 node = ebmb_next(node);
798 }
799 return err;
800}
801
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200802/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +0200803 * be NULL, in which case nothing is done. The default_ctx is nullified too.
804 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200805void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200806{
807 struct ebmb_node *node, *back;
808 struct sni_ctx *sni;
809
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200810 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200811 return;
812
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200813 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200814 while (node) {
815 sni = ebmb_entry(node, struct sni_ctx, name);
816 back = ebmb_next(node);
817 ebmb_delete(node);
818 if (!sni->order) /* only free the CTX on its first occurrence */
819 SSL_CTX_free(sni->ctx);
820 free(sni);
821 node = back;
822 }
823
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200824 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200825 while (node) {
826 sni = ebmb_entry(node, struct sni_ctx, name);
827 back = ebmb_next(node);
828 ebmb_delete(node);
829 if (!sni->order) /* only free the CTX on its first occurrence */
830 SSL_CTX_free(sni->ctx);
831 free(sni);
832 node = back;
833 }
834
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200835 bind_conf->default_ctx = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +0200836}
837
Emeric Brun46591952012-05-18 15:47:34 +0200838/*
839 * This function is called if SSL * context is not yet allocated. The function
840 * is designed to be called before any other data-layer operation and sets the
841 * handshake flag on the connection. It is safe to call it multiple times.
842 * It returns 0 on success and -1 in error case.
843 */
844static int ssl_sock_init(struct connection *conn)
845{
846 /* already initialized */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200847 if (conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200848 return 0;
849
Willy Tarreau20879a02012-12-03 16:32:10 +0100850 if (global.maxsslconn && sslconns >= global.maxsslconn) {
851 conn->err_code = CO_ER_SSL_TOO_MANY;
Willy Tarreau403edff2012-09-06 11:58:37 +0200852 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +0100853 }
Willy Tarreau403edff2012-09-06 11:58:37 +0200854
Emeric Brun46591952012-05-18 15:47:34 +0200855 /* If it is in client mode initiate SSL session
856 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100857 if (objt_server(conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +0200858 /* Alloc a new SSL session ctx */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100859 conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx);
Willy Tarreau20879a02012-12-03 16:32:10 +0100860 if (!conn->xprt_ctx) {
861 conn->err_code = CO_ER_SSL_NO_MEM;
Emeric Brun46591952012-05-18 15:47:34 +0200862 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +0100863 }
Emeric Brun46591952012-05-18 15:47:34 +0200864
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200865 SSL_set_connect_state(conn->xprt_ctx);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100866 if (objt_server(conn->target)->ssl_ctx.reused_sess)
867 SSL_set_session(conn->xprt_ctx, objt_server(conn->target)->ssl_ctx.reused_sess);
Emeric Brun46591952012-05-18 15:47:34 +0200868
869 /* set fd on SSL session context */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200870 SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +0200871
872 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +0200873 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +0200874
875 sslconns++;
Emeric Brun46591952012-05-18 15:47:34 +0200876 return 0;
877 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100878 else if (objt_listener(conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +0200879 /* Alloc a new SSL session ctx */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100880 conn->xprt_ctx = SSL_new(objt_listener(conn->target)->bind_conf->default_ctx);
Willy Tarreau20879a02012-12-03 16:32:10 +0100881 if (!conn->xprt_ctx) {
882 conn->err_code = CO_ER_SSL_NO_MEM;
Emeric Brun46591952012-05-18 15:47:34 +0200883 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +0100884 }
Emeric Brun46591952012-05-18 15:47:34 +0200885
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200886 SSL_set_accept_state(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200887
888 /* set fd on SSL session context */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200889 SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +0200890
Emeric Brune1f38db2012-09-03 20:36:47 +0200891 /* set connection pointer */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200892 SSL_set_app_data(conn->xprt_ctx, conn);
Emeric Brune1f38db2012-09-03 20:36:47 +0200893
Emeric Brun46591952012-05-18 15:47:34 +0200894 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +0200895 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +0200896
897 sslconns++;
Emeric Brun46591952012-05-18 15:47:34 +0200898 return 0;
899 }
900 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +0100901 conn->err_code = CO_ER_SSL_NO_TARGET;
Emeric Brun46591952012-05-18 15:47:34 +0200902 return -1;
903}
904
905
906/* This is the callback which is used when an SSL handshake is pending. It
907 * updates the FD status if it wants some polling before being called again.
908 * It returns 0 if it fails in a fatal way or needs to poll to go further,
909 * otherwise it returns non-zero and removes itself from the connection's
910 * flags (the bit is provided in <flag> by the caller).
911 */
912int ssl_sock_handshake(struct connection *conn, unsigned int flag)
913{
914 int ret;
915
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200916 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200917 goto out_error;
918
Emeric Brun674b7432012-11-08 19:21:55 +0100919 /* If we use SSL_do_handshake to process a reneg initiated by
920 * the remote peer, it sometimes returns SSL_ERROR_SSL.
921 * Usually SSL_write and SSL_read are used and process implicitly
922 * the reneg handshake.
923 * Here we use SSL_peek as a workaround for reneg.
924 */
925 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) {
926 char c;
927
928 ret = SSL_peek(conn->xprt_ctx, &c, 1);
929 if (ret <= 0) {
930 /* handshake may have not been completed, let's find why */
931 ret = SSL_get_error(conn->xprt_ctx, ret);
932 if (ret == SSL_ERROR_WANT_WRITE) {
933 /* SSL handshake needs to write, L4 connection may not be ready */
934 __conn_sock_stop_recv(conn);
935 __conn_sock_poll_send(conn);
936 return 0;
937 }
938 else if (ret == SSL_ERROR_WANT_READ) {
939 /* handshake may have been completed but we have
940 * no more data to read.
941 */
942 if (!SSL_renegotiate_pending(conn->xprt_ctx)) {
943 ret = 1;
944 goto reneg_ok;
945 }
946 /* SSL handshake needs to read, L4 connection is ready */
947 if (conn->flags & CO_FL_WAIT_L4_CONN)
948 conn->flags &= ~CO_FL_WAIT_L4_CONN;
949 __conn_sock_stop_send(conn);
950 __conn_sock_poll_recv(conn);
951 return 0;
952 }
953 else if (ret == SSL_ERROR_SYSCALL) {
954 /* if errno is null, then connection was successfully established */
955 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
956 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +0100957 if (!conn->err_code) {
958 if (!((SSL *)conn->xprt_ctx)->packet_length)
959 if (!errno)
960 conn->err_code = CO_ER_SSL_EMPTY;
961 else
962 conn->err_code = CO_ER_SSL_ABORT;
963 else
964 conn->err_code = CO_ER_SSL_HANDSHAKE;
965 }
Emeric Brun674b7432012-11-08 19:21:55 +0100966 goto out_error;
967 }
968 else {
969 /* Fail on all other handshake errors */
970 /* Note: OpenSSL may leave unread bytes in the socket's
971 * buffer, causing an RST to be emitted upon close() on
972 * TCP sockets. We first try to drain possibly pending
973 * data to avoid this as much as possible.
974 */
975 ret = recv(conn->t.sock.fd, trash.str, trash.size, MSG_NOSIGNAL|MSG_DONTWAIT);
Willy Tarreau20879a02012-12-03 16:32:10 +0100976 if (!conn->err_code)
977 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +0100978 goto out_error;
979 }
980 }
981 /* read some data: consider handshake completed */
982 goto reneg_ok;
983 }
984
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200985 ret = SSL_do_handshake(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200986 if (ret != 1) {
987 /* handshake did not complete, let's find why */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200988 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +0200989
990 if (ret == SSL_ERROR_WANT_WRITE) {
991 /* SSL handshake needs to write, L4 connection may not be ready */
992 __conn_sock_stop_recv(conn);
993 __conn_sock_poll_send(conn);
994 return 0;
995 }
996 else if (ret == SSL_ERROR_WANT_READ) {
997 /* SSL handshake needs to read, L4 connection is ready */
998 if (conn->flags & CO_FL_WAIT_L4_CONN)
999 conn->flags &= ~CO_FL_WAIT_L4_CONN;
1000 __conn_sock_stop_send(conn);
1001 __conn_sock_poll_recv(conn);
1002 return 0;
1003 }
Willy Tarreau89230192012-09-28 20:22:13 +02001004 else if (ret == SSL_ERROR_SYSCALL) {
1005 /* if errno is null, then connection was successfully established */
1006 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
1007 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01001008
1009 if (!((SSL *)conn->xprt_ctx)->packet_length)
1010 if (!errno)
1011 conn->err_code = CO_ER_SSL_EMPTY;
1012 else
1013 conn->err_code = CO_ER_SSL_ABORT;
1014 else
1015 conn->err_code = CO_ER_SSL_HANDSHAKE;
Willy Tarreau89230192012-09-28 20:22:13 +02001016 goto out_error;
1017 }
Emeric Brun46591952012-05-18 15:47:34 +02001018 else {
1019 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02001020 /* Note: OpenSSL may leave unread bytes in the socket's
1021 * buffer, causing an RST to be emitted upon close() on
1022 * TCP sockets. We first try to drain possibly pending
1023 * data to avoid this as much as possible.
1024 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001025 ret = recv(conn->t.sock.fd, trash.str, trash.size, MSG_NOSIGNAL|MSG_DONTWAIT);
Willy Tarreau20879a02012-12-03 16:32:10 +01001026 if (!conn->err_code)
1027 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02001028 goto out_error;
1029 }
1030 }
1031
Emeric Brun674b7432012-11-08 19:21:55 +01001032reneg_ok:
1033
Emeric Brun46591952012-05-18 15:47:34 +02001034 /* Handshake succeeded */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001035 if (objt_server(conn->target)) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001036 if (!SSL_session_reused(conn->xprt_ctx)) {
Emeric Brun46591952012-05-18 15:47:34 +02001037 /* check if session was reused, if not store current session on server for reuse */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001038 if (objt_server(conn->target)->ssl_ctx.reused_sess)
1039 SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess);
Emeric Brun46591952012-05-18 15:47:34 +02001040
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001041 objt_server(conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +02001042 }
1043 }
1044
1045 /* The connection is now established at both layers, it's time to leave */
1046 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
1047 return 1;
1048
1049 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01001050 /* Clear openssl global errors stack */
1051 ERR_clear_error();
1052
Emeric Brun9fa89732012-10-04 17:09:56 +02001053 /* free resumed session if exists */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001054 if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) {
1055 SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess);
1056 objt_server(conn->target)->ssl_ctx.reused_sess = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02001057 }
1058
Emeric Brun46591952012-05-18 15:47:34 +02001059 /* Fail on all other handshake errors */
1060 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001061 if (!conn->err_code)
1062 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02001063 return 0;
1064}
1065
1066/* Receive up to <count> bytes from connection <conn>'s socket and store them
1067 * into buffer <buf>. The caller must ensure that <count> is always smaller
1068 * than the buffer's size. Only one call to recv() is performed, unless the
1069 * buffer wraps, in which case a second call may be performed. The connection's
1070 * flags are updated with whatever special event is detected (error, read0,
1071 * empty). The caller is responsible for taking care of those events and
1072 * avoiding the call if inappropriate. The function does not call the
1073 * connection's polling update function, so the caller is responsible for this.
1074 */
1075static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
1076{
1077 int ret, done = 0;
1078 int try = count;
1079
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001080 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02001081 goto out_error;
1082
1083 if (conn->flags & CO_FL_HANDSHAKE)
1084 /* a handshake was requested */
1085 return 0;
1086
1087 /* compute the maximum block size we can read at once. */
1088 if (buffer_empty(buf)) {
1089 /* let's realign the buffer to optimize I/O */
1090 buf->p = buf->data;
1091 }
1092 else if (buf->data + buf->o < buf->p &&
1093 buf->p + buf->i < buf->data + buf->size) {
1094 /* remaining space wraps at the end, with a moving limit */
1095 if (try > buf->data + buf->size - (buf->p + buf->i))
1096 try = buf->data + buf->size - (buf->p + buf->i);
1097 }
1098
1099 /* read the largest possible block. For this, we perform only one call
1100 * to recv() unless the buffer wraps and we exactly fill the first hunk,
1101 * in which case we accept to do it once again. A new attempt is made on
1102 * EINTR too.
1103 */
1104 while (try) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001105 ret = SSL_read(conn->xprt_ctx, bi_end(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +02001106 if (conn->flags & CO_FL_ERROR) {
1107 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01001108 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02001109 }
Emeric Brun46591952012-05-18 15:47:34 +02001110 if (ret > 0) {
1111 buf->i += ret;
1112 done += ret;
1113 if (ret < try)
1114 break;
1115 count -= ret;
1116 try = count;
1117 }
1118 else if (ret == 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01001119 ret = SSL_get_error(conn->xprt_ctx, ret);
1120 if (ret != SSL_ERROR_ZERO_RETURN) {
Emeric Brun1c646862012-12-14 12:33:41 +01001121 /* error on protocol or underlying transport */
1122 if ((ret != SSL_ERROR_SYSCALL)
1123 || (errno && (errno != EAGAIN)))
1124 conn->flags |= CO_FL_ERROR;
1125
Emeric Brun644cde02012-12-14 11:21:13 +01001126 /* Clear openssl global errors stack */
1127 ERR_clear_error();
1128 }
Emeric Brun46591952012-05-18 15:47:34 +02001129 goto read0;
1130 }
1131 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001132 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +02001133 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01001134 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02001135 conn->flags |= CO_FL_SSL_WAIT_HS;
Emeric Brun8af8dd12012-11-08 17:56:20 +01001136 __conn_sock_want_send(conn);
Emeric Brun46591952012-05-18 15:47:34 +02001137 break;
1138 }
1139 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun282a76a2012-11-08 18:02:56 +01001140 if (SSL_renegotiate_pending(conn->xprt_ctx)) {
1141 /* handshake is running, and it may need to re-enable read */
1142 conn->flags |= CO_FL_SSL_WAIT_HS;
1143 __conn_sock_want_recv(conn);
1144 break;
1145 }
Emeric Brun46591952012-05-18 15:47:34 +02001146 /* we need to poll for retry a read later */
1147 __conn_data_poll_recv(conn);
1148 break;
1149 }
1150 /* otherwise it's a real error */
1151 goto out_error;
1152 }
1153 }
1154 return done;
1155
1156 read0:
1157 conn_sock_read0(conn);
1158 return done;
1159 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01001160 /* Clear openssl global errors stack */
1161 ERR_clear_error();
1162
Emeric Brun46591952012-05-18 15:47:34 +02001163 conn->flags |= CO_FL_ERROR;
1164 return done;
1165}
1166
1167
1168/* Send all pending bytes from buffer <buf> to connection <conn>'s socket.
1169 * <flags> may contain MSG_MORE to make the system hold on without sending
1170 * data too fast, but this flag is ignored at the moment.
1171 * Only one call to send() is performed, unless the buffer wraps, in which case
1172 * a second call may be performed. The connection's flags are updated with
1173 * whatever special event is detected (error, empty). The caller is responsible
1174 * for taking care of those events and avoiding the call if inappropriate. The
1175 * function does not call the connection's polling update function, so the caller
1176 * is responsible for this.
1177 */
1178static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags)
1179{
1180 int ret, try, done;
1181
1182 done = 0;
1183
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001184 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02001185 goto out_error;
1186
1187 if (conn->flags & CO_FL_HANDSHAKE)
1188 /* a handshake was requested */
1189 return 0;
1190
1191 /* send the largest possible block. For this we perform only one call
1192 * to send() unless the buffer wraps and we exactly fill the first hunk,
1193 * in which case we accept to do it once again.
1194 */
1195 while (buf->o) {
1196 try = buf->o;
1197 /* outgoing data may wrap at the end */
1198 if (buf->data + try > buf->p)
1199 try = buf->data + try - buf->p;
1200
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001201 ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +02001202 if (conn->flags & CO_FL_ERROR) {
1203 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01001204 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02001205 }
Emeric Brun46591952012-05-18 15:47:34 +02001206 if (ret > 0) {
1207 buf->o -= ret;
1208 done += ret;
1209
Willy Tarreau5fb38032012-12-16 19:39:09 +01001210 if (likely(buffer_empty(buf)))
Emeric Brun46591952012-05-18 15:47:34 +02001211 /* optimize data alignment in the buffer */
1212 buf->p = buf->data;
1213
1214 /* if the system buffer is full, don't insist */
1215 if (ret < try)
1216 break;
1217 }
1218 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001219 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +02001220 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun282a76a2012-11-08 18:02:56 +01001221 if (SSL_renegotiate_pending(conn->xprt_ctx)) {
1222 /* handshake is running, and it may need to re-enable write */
1223 conn->flags |= CO_FL_SSL_WAIT_HS;
1224 __conn_sock_want_send(conn);
1225 break;
1226 }
Emeric Brun46591952012-05-18 15:47:34 +02001227 /* we need to poll to retry a write later */
1228 __conn_data_poll_send(conn);
1229 break;
1230 }
1231 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01001232 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02001233 conn->flags |= CO_FL_SSL_WAIT_HS;
Emeric Brun8af8dd12012-11-08 17:56:20 +01001234 __conn_sock_want_recv(conn);
Emeric Brun46591952012-05-18 15:47:34 +02001235 break;
1236 }
1237 goto out_error;
1238 }
1239 }
1240 return done;
1241
1242 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01001243 /* Clear openssl global errors stack */
1244 ERR_clear_error();
1245
Emeric Brun46591952012-05-18 15:47:34 +02001246 conn->flags |= CO_FL_ERROR;
1247 return done;
1248}
1249
Emeric Brun46591952012-05-18 15:47:34 +02001250static void ssl_sock_close(struct connection *conn) {
1251
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001252 if (conn->xprt_ctx) {
1253 SSL_free(conn->xprt_ctx);
1254 conn->xprt_ctx = NULL;
Willy Tarreau403edff2012-09-06 11:58:37 +02001255 sslconns--;
Emeric Brun46591952012-05-18 15:47:34 +02001256 }
Emeric Brun46591952012-05-18 15:47:34 +02001257}
1258
1259/* This function tries to perform a clean shutdown on an SSL connection, and in
1260 * any case, flags the connection as reusable if no handshake was in progress.
1261 */
1262static void ssl_sock_shutw(struct connection *conn, int clean)
1263{
1264 if (conn->flags & CO_FL_HANDSHAKE)
1265 return;
1266 /* no handshake was in progress, try a clean ssl shutdown */
Emeric Brun644cde02012-12-14 11:21:13 +01001267 if (clean && (SSL_shutdown(conn->xprt_ctx) <= 0)) {
1268 /* Clear openssl global errors stack */
1269 ERR_clear_error();
1270 }
Emeric Brun46591952012-05-18 15:47:34 +02001271
1272 /* force flag on ssl to keep session in cache regardless shutdown result */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001273 SSL_set_shutdown(conn->xprt_ctx, SSL_SENT_SHUTDOWN);
Emeric Brun46591952012-05-18 15:47:34 +02001274}
1275
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001276/* used for logging, may be changed for a sample fetch later */
1277const char *ssl_sock_get_cipher_name(struct connection *conn)
1278{
1279 if (!conn->xprt && !conn->xprt_ctx)
1280 return NULL;
1281 return SSL_get_cipher_name(conn->xprt_ctx);
1282}
1283
1284/* used for logging, may be changed for a sample fetch later */
1285const char *ssl_sock_get_proto_version(struct connection *conn)
1286{
1287 if (!conn->xprt && !conn->xprt_ctx)
1288 return NULL;
1289 return SSL_get_version(conn->xprt_ctx);
1290}
1291
Willy Tarreau8d598402012-10-22 17:58:39 +02001292/* Extract a serial from a cert, and copy it to a chunk.
1293 * Returns 1 if serial is found and copied, 0 if no serial found and
1294 * -1 if output is not large enough.
1295 */
1296static int
1297ssl_sock_get_serial(X509 *crt, struct chunk *out)
1298{
1299 ASN1_INTEGER *serial;
1300
1301 serial = X509_get_serialNumber(crt);
1302 if (!serial)
1303 return 0;
1304
1305 if (out->size < serial->length)
1306 return -1;
1307
1308 memcpy(out->str, serial->data, serial->length);
1309 out->len = serial->length;
1310 return 1;
1311}
1312
Emeric Brunce5ad802012-10-22 14:11:22 +02001313
1314/* Copy Date in ASN1_UTCTIME format in struct chunk out.
1315 * Returns 1 if serial is found and copied, 0 if no valid time found
1316 * and -1 if output is not large enough.
1317 */
1318static int
1319ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out)
1320{
1321 if (tm->type == V_ASN1_GENERALIZEDTIME) {
1322 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
1323
1324 if (gentm->length < 12)
1325 return 0;
1326 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
1327 return 0;
1328 if (out->size < gentm->length-2)
1329 return -1;
1330
1331 memcpy(out->str, gentm->data+2, gentm->length-2);
1332 out->len = gentm->length-2;
1333 return 1;
1334 }
1335 else if (tm->type == V_ASN1_UTCTIME) {
1336 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
1337
1338 if (utctm->length < 10)
1339 return 0;
1340 if (utctm->data[0] >= 0x35)
1341 return 0;
1342 if (out->size < utctm->length)
1343 return -1;
1344
1345 memcpy(out->str, utctm->data, utctm->length);
1346 out->len = utctm->length;
1347 return 1;
1348 }
1349
1350 return 0;
1351}
1352
Emeric Brun87855892012-10-17 17:39:35 +02001353/* Extract an entry from a X509_NAME and copy its value to an output chunk.
1354 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
1355 */
1356static int
1357ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out)
1358{
1359 X509_NAME_ENTRY *ne;
1360 int i, j, n;
1361 int cur = 0;
1362 const char *s;
1363 char tmp[128];
1364
1365 out->len = 0;
1366 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
1367 if (pos < 0)
1368 j = (sk_X509_NAME_ENTRY_num(a->entries)-1) - i;
1369 else
1370 j = i;
1371
1372 ne = sk_X509_NAME_ENTRY_value(a->entries, j);
1373 n = OBJ_obj2nid(ne->object);
1374 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
1375 i2t_ASN1_OBJECT(tmp, sizeof(tmp), ne->object);
1376 s = tmp;
1377 }
1378
1379 if (chunk_strcasecmp(entry, s) != 0)
1380 continue;
1381
1382 if (pos < 0)
1383 cur--;
1384 else
1385 cur++;
1386
1387 if (cur != pos)
1388 continue;
1389
1390 if (ne->value->length > out->size)
1391 return -1;
1392
1393 memcpy(out->str, ne->value->data, ne->value->length);
1394 out->len = ne->value->length;
1395 return 1;
1396 }
1397
1398 return 0;
1399
1400}
1401
1402/* Extract and format full DN from a X509_NAME and copy result into a chunk
1403 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
1404 */
1405static int
1406ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out)
1407{
1408 X509_NAME_ENTRY *ne;
1409 int i, n, ln;
1410 int l = 0;
1411 const char *s;
1412 char *p;
1413 char tmp[128];
1414
1415 out->len = 0;
1416 p = out->str;
1417 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
1418 ne = sk_X509_NAME_ENTRY_value(a->entries, i);
1419 n = OBJ_obj2nid(ne->object);
1420 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
1421 i2t_ASN1_OBJECT(tmp, sizeof(tmp), ne->object);
1422 s = tmp;
1423 }
1424 ln = strlen(s);
1425
1426 l += 1 + ln + 1 + ne->value->length;
1427 if (l > out->size)
1428 return -1;
1429 out->len = l;
1430
1431 *(p++)='/';
1432 memcpy(p, s, ln);
1433 p += ln;
1434 *(p++)='=';
1435 memcpy(p, ne->value->data, ne->value->length);
1436 p += ne->value->length;
1437 }
1438
1439 if (!out->len)
1440 return 0;
1441
1442 return 1;
1443}
1444
Willy Tarreau7875d092012-09-10 08:20:03 +02001445/***** Below are some sample fetching functions for ACL/patterns *****/
1446
Emeric Brune64aef12012-09-21 13:15:06 +02001447/* boolean, returns true if client cert was present */
1448static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02001449smp_fetch_ssl_fc_has_crt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1450 const struct arg *args, struct sample *smp)
Emeric Brune64aef12012-09-21 13:15:06 +02001451{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001452 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02001453 return 0;
1454
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001455 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02001456 smp->flags |= SMP_F_MAY_CHANGE;
1457 return 0;
1458 }
1459
1460 smp->flags = 0;
1461 smp->type = SMP_T_BOOL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001462 smp->data.uint = SSL_SOCK_ST_FL_VERIFY_DONE & l4->si[0].conn->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001463
1464 return 1;
1465}
1466
Willy Tarreau8d598402012-10-22 17:58:39 +02001467/* bin, returns serial in a binary chunk */
1468static int
1469smp_fetch_ssl_c_serial(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1470 const struct arg *args, struct sample *smp)
1471{
1472 X509 *crt = NULL;
1473 int ret = 0;
1474 struct chunk *smp_trash;
1475
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001476 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02001477 return 0;
1478
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001479 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02001480 smp->flags |= SMP_F_MAY_CHANGE;
1481 return 0;
1482 }
1483
1484 /* SSL_get_peer_certificate, it increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001485 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Willy Tarreau8d598402012-10-22 17:58:39 +02001486 if (!crt)
1487 goto out;
1488
Willy Tarreau47ca5452012-12-23 20:22:19 +01001489 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02001490 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
1491 goto out;
1492
1493 smp->data.str = *smp_trash;
1494 smp->type = SMP_T_BIN;
1495 ret = 1;
1496out:
1497 if (crt)
1498 X509_free(crt);
1499 return ret;
1500}
Emeric Brune64aef12012-09-21 13:15:06 +02001501
Emeric Brunce5ad802012-10-22 14:11:22 +02001502/*str, returns notafter date in ASN1_UTCTIME format */
1503static int
1504smp_fetch_ssl_c_notafter(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1505 const struct arg *args, struct sample *smp)
1506{
1507 X509 *crt = NULL;
1508 int ret = 0;
1509 struct chunk *smp_trash;
1510
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001511 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02001512 return 0;
1513
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001514 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02001515 smp->flags |= SMP_F_MAY_CHANGE;
1516 return 0;
1517 }
1518
1519 /* SSL_get_peer_certificate, it increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001520 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brunce5ad802012-10-22 14:11:22 +02001521 if (!crt)
1522 goto out;
1523
Willy Tarreau47ca5452012-12-23 20:22:19 +01001524 smp_trash = get_trash_chunk();
Emeric Brunce5ad802012-10-22 14:11:22 +02001525 if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0)
1526 goto out;
1527
1528 smp->data.str = *smp_trash;
1529 smp->type = SMP_T_STR;
1530 ret = 1;
1531out:
1532 if (crt)
1533 X509_free(crt);
1534 return ret;
1535}
1536
Emeric Brun87855892012-10-17 17:39:35 +02001537/* str, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. */
1538static int
1539smp_fetch_ssl_c_i_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1540 const struct arg *args, struct sample *smp)
1541{
1542 X509 *crt = NULL;
1543 X509_NAME *name;
1544 int ret = 0;
1545 struct chunk *smp_trash;
1546
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001547 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun87855892012-10-17 17:39:35 +02001548 return 0;
1549
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001550 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02001551 smp->flags |= SMP_F_MAY_CHANGE;
1552 return 0;
1553 }
1554
1555 /* SSL_get_peer_certificate, it increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001556 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun87855892012-10-17 17:39:35 +02001557 if (!crt)
1558 goto out;
1559
1560 name = X509_get_issuer_name(crt);
1561 if (!name)
1562 goto out;
1563
Willy Tarreau47ca5452012-12-23 20:22:19 +01001564 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02001565 if (args && args[0].type == ARGT_STR) {
1566 int pos = 1;
1567
1568 if (args[1].type == ARGT_SINT)
1569 pos = args[1].data.sint;
1570 else if (args[1].type == ARGT_UINT)
1571 pos =(int)args[1].data.uint;
1572
1573 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
1574 goto out;
1575 }
1576 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
1577 goto out;
1578
1579 smp->type = SMP_T_STR;
1580 smp->data.str = *smp_trash;
1581 ret = 1;
1582out:
1583 if (crt)
1584 X509_free(crt);
1585 return ret;
1586}
1587
Emeric Brunce5ad802012-10-22 14:11:22 +02001588/*str, returns notbefore date in ASN1_UTCTIME format */
1589static int
1590smp_fetch_ssl_c_notbefore(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1591 const struct arg *args, struct sample *smp)
1592{
1593 X509 *crt = NULL;
1594 int ret = 0;
1595 struct chunk *smp_trash;
1596
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001597 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02001598 return 0;
1599
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001600 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02001601 smp->flags |= SMP_F_MAY_CHANGE;
1602 return 0;
1603 }
1604
1605 /* SSL_get_peer_certificate, it increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001606 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brunce5ad802012-10-22 14:11:22 +02001607 if (!crt)
1608 goto out;
1609
Willy Tarreau47ca5452012-12-23 20:22:19 +01001610 smp_trash = get_trash_chunk();
Emeric Brunce5ad802012-10-22 14:11:22 +02001611 if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0)
1612 goto out;
1613
1614 smp->data.str = *smp_trash;
1615 smp->type = SMP_T_STR;
1616 ret = 1;
1617out:
1618 if (crt)
1619 X509_free(crt);
1620 return ret;
1621}
1622
Emeric Brun87855892012-10-17 17:39:35 +02001623/* str, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. */
1624static int
1625smp_fetch_ssl_c_s_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1626 const struct arg *args, struct sample *smp)
1627{
1628 X509 *crt = NULL;
1629 X509_NAME *name;
1630 int ret = 0;
1631 struct chunk *smp_trash;
1632
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001633 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun87855892012-10-17 17:39:35 +02001634 return 0;
1635
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001636 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02001637 smp->flags |= SMP_F_MAY_CHANGE;
1638 return 0;
1639 }
1640
1641 /* SSL_get_peer_certificate, it increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001642 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun87855892012-10-17 17:39:35 +02001643 if (!crt)
1644 goto out;
1645
1646 name = X509_get_subject_name(crt);
1647 if (!name)
1648 goto out;
1649
Willy Tarreau47ca5452012-12-23 20:22:19 +01001650 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02001651 if (args && args[0].type == ARGT_STR) {
1652 int pos = 1;
1653
1654 if (args[1].type == ARGT_SINT)
1655 pos = args[1].data.sint;
1656 else if (args[1].type == ARGT_UINT)
1657 pos =(int)args[1].data.uint;
1658
1659 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
1660 goto out;
1661 }
1662 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
1663 goto out;
1664
1665 smp->type = SMP_T_STR;
1666 smp->data.str = *smp_trash;
1667 ret = 1;
1668out:
1669 if (crt)
1670 X509_free(crt);
1671 return ret;
1672}
Emeric Brun9143d372012-12-20 15:44:16 +01001673
1674/* integer, returns true if current session use a client certificate */
1675static int
1676smp_fetch_ssl_c_used(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1677 const struct arg *args, struct sample *smp)
1678{
1679 X509 *crt;
1680
1681 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
1682 return 0;
1683
1684 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
1685 smp->flags |= SMP_F_MAY_CHANGE;
1686 return 0;
1687 }
1688
1689 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
1690 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
1691 if (crt) {
1692 X509_free(crt);
1693 }
1694
1695 smp->type = SMP_T_BOOL;
1696 smp->data.uint = (crt != NULL);
1697 return 1;
1698}
1699
Emeric Bruna7359fd2012-10-17 15:03:11 +02001700/* integer, returns the client certificate version */
1701static int
1702smp_fetch_ssl_c_version(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1703 const struct arg *args, struct sample *smp)
1704{
1705 X509 *crt;
1706
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001707 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02001708 return 0;
1709
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001710 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02001711 smp->flags |= SMP_F_MAY_CHANGE;
1712 return 0;
1713 }
1714
1715 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001716 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Bruna7359fd2012-10-17 15:03:11 +02001717 if (!crt)
1718 return 0;
1719
1720 smp->data.uint = (unsigned int)(1 + X509_get_version(crt));
1721 X509_free(crt);
1722 smp->type = SMP_T_UINT;
1723
1724 return 1;
1725}
1726
Emeric Brun7f56e742012-10-19 18:15:40 +02001727/* str, returns the client certificate sig alg */
1728static int
1729smp_fetch_ssl_c_sig_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1730 const struct arg *args, struct sample *smp)
1731{
1732 X509 *crt;
1733 int nid;
1734
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001735 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun7f56e742012-10-19 18:15:40 +02001736 return 0;
1737
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001738 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02001739 smp->flags |= SMP_F_MAY_CHANGE;
1740 return 0;
1741 }
1742
1743 /* SSL_get_peer_certificate increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001744 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun7f56e742012-10-19 18:15:40 +02001745 if (!crt)
1746 return 0;
1747
1748 nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->signature->algorithm));
1749
1750 smp->data.str.str = (char *)OBJ_nid2sn(nid);
1751 if (!smp->data.str.str)
1752 return 0;
1753
1754 smp->type = SMP_T_CSTR;
1755 smp->data.str.len = strlen(smp->data.str.str);
1756 X509_free(crt);
1757
1758 return 1;
1759}
1760
Emeric Brun521a0112012-10-22 12:22:55 +02001761/* str, returns the client certificate key alg */
1762static int
1763smp_fetch_ssl_c_key_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1764 const struct arg *args, struct sample *smp)
1765{
1766 X509 *crt;
1767 int nid;
1768
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001769 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun521a0112012-10-22 12:22:55 +02001770 return 0;
1771
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001772 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02001773 smp->flags |= SMP_F_MAY_CHANGE;
1774 return 0;
1775 }
1776
1777 /* SSL_get_peer_certificate increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001778 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun521a0112012-10-22 12:22:55 +02001779 if (!crt)
1780 return 0;
1781
1782 nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->key->algor->algorithm));
1783
1784 smp->data.str.str = (char *)OBJ_nid2sn(nid);
1785 if (!smp->data.str.str)
1786 return 0;
1787
1788 smp->type = SMP_T_CSTR;
1789 smp->data.str.len = strlen(smp->data.str.str);
1790 X509_free(crt);
1791
1792 return 1;
1793}
1794
Emeric Brun2525b6b2012-10-18 15:59:43 +02001795/* boolean, returns true if front conn. transport layer is SSL */
Willy Tarreau7875d092012-09-10 08:20:03 +02001796static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02001797smp_fetch_ssl_fc(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau7875d092012-09-10 08:20:03 +02001798 const struct arg *args, struct sample *smp)
1799{
1800 smp->type = SMP_T_BOOL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001801 smp->data.uint = (l4->si[0].conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02001802 return 1;
1803}
1804
Emeric Brun2525b6b2012-10-18 15:59:43 +02001805/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02001806static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02001807smp_fetch_ssl_fc_has_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1808 const struct arg *args, struct sample *smp)
Willy Tarreau7875d092012-09-10 08:20:03 +02001809{
1810#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1811 smp->type = SMP_T_BOOL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001812 smp->data.uint = (l4->si[0].conn->xprt == &ssl_sock) &&
1813 l4->si[0].conn->xprt_ctx &&
1814 SSL_get_servername(l4->si[0].conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02001815 return 1;
1816#else
1817 return 0;
1818#endif
1819}
1820
Willy Tarreau8d598402012-10-22 17:58:39 +02001821/* bin, returns serial in a binary chunk */
1822static int
1823smp_fetch_ssl_f_serial(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1824 const struct arg *args, struct sample *smp)
1825{
1826 X509 *crt = NULL;
1827 int ret = 0;
1828 struct chunk *smp_trash;
1829
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001830 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02001831 return 0;
1832
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001833 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02001834 smp->flags |= SMP_F_MAY_CHANGE;
1835 return 0;
1836 }
1837
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001838 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Willy Tarreau8d598402012-10-22 17:58:39 +02001839 if (!crt)
1840 goto out;
1841
Willy Tarreau47ca5452012-12-23 20:22:19 +01001842 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02001843 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
1844 goto out;
1845
1846 smp->data.str = *smp_trash;
1847 smp->type = SMP_T_BIN;
1848 ret = 1;
1849out:
1850 return ret;
1851}
Emeric Brunce5ad802012-10-22 14:11:22 +02001852/*str, returns notafter date in ASN1_UTCTIME format */
1853static int
1854smp_fetch_ssl_f_notafter(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1855 const struct arg *args, struct sample *smp)
1856{
1857 X509 *crt = NULL;
1858 int ret = 0;
1859 struct chunk *smp_trash;
1860
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001861 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02001862 return 0;
1863
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001864 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02001865 smp->flags |= SMP_F_MAY_CHANGE;
1866 return 0;
1867 }
1868
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001869 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brunce5ad802012-10-22 14:11:22 +02001870 if (!crt)
1871 goto out;
1872
Willy Tarreau47ca5452012-12-23 20:22:19 +01001873 smp_trash = get_trash_chunk();
Emeric Brunce5ad802012-10-22 14:11:22 +02001874 if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0)
1875 goto out;
1876
1877 smp->data.str = *smp_trash;
1878 smp->type = SMP_T_STR;
1879 ret = 1;
1880out:
1881 return ret;
1882}
1883
1884/*str, returns notbefore date in ASN1_UTCTIME format */
1885static int
1886smp_fetch_ssl_f_notbefore(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1887 const struct arg *args, struct sample *smp)
1888{
1889 X509 *crt = NULL;
1890 int ret = 0;
1891 struct chunk *smp_trash;
1892
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001893 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02001894 return 0;
1895
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001896 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02001897 smp->flags |= SMP_F_MAY_CHANGE;
1898 return 0;
1899 }
1900
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001901 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brunce5ad802012-10-22 14:11:22 +02001902 if (!crt)
1903 goto out;
1904
Willy Tarreau47ca5452012-12-23 20:22:19 +01001905 smp_trash = get_trash_chunk();
Emeric Brunce5ad802012-10-22 14:11:22 +02001906 if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0)
1907 goto out;
1908
1909 smp->data.str = *smp_trash;
1910 smp->type = SMP_T_STR;
1911 ret = 1;
1912out:
1913 return ret;
1914}
Willy Tarreau8d598402012-10-22 17:58:39 +02001915
Emeric Bruna7359fd2012-10-17 15:03:11 +02001916/* integer, returns the frontend certificate version */
1917static int
1918smp_fetch_ssl_f_version(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1919 const struct arg *args, struct sample *smp)
1920{
1921 X509 *crt;
1922
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001923 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02001924 return 0;
1925
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001926 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02001927 smp->flags |= SMP_F_MAY_CHANGE;
1928 return 0;
1929 }
1930
1931 /* SSL_get_certificate returns a ptr on an SSL * internal sub struct */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001932 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Bruna7359fd2012-10-17 15:03:11 +02001933 if (!crt)
1934 return 0;
1935
1936 smp->data.uint = (unsigned int)(1 + X509_get_version(crt));
1937 smp->type = SMP_T_UINT;
1938
1939 return 1;
1940}
1941
Emeric Brun7f56e742012-10-19 18:15:40 +02001942/* str, returns the client certificate sig alg */
1943static int
1944smp_fetch_ssl_f_sig_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1945 const struct arg *args, struct sample *smp)
1946{
1947 X509 *crt;
1948 int nid;
1949
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001950 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun7f56e742012-10-19 18:15:40 +02001951 return 0;
1952
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001953 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02001954 smp->flags |= SMP_F_MAY_CHANGE;
1955 return 0;
1956 }
1957
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001958 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun7f56e742012-10-19 18:15:40 +02001959 if (!crt)
1960 return 0;
1961
1962 nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->signature->algorithm));
1963
1964 smp->data.str.str = (char *)OBJ_nid2sn(nid);
1965 if (!smp->data.str.str)
1966 return 0;
1967
1968 smp->type = SMP_T_CSTR;
1969 smp->data.str.len = strlen(smp->data.str.str);
1970
1971 return 1;
1972}
1973
Emeric Brun521a0112012-10-22 12:22:55 +02001974/* str, returns the client certificate key alg */
1975static int
1976smp_fetch_ssl_f_key_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1977 const struct arg *args, struct sample *smp)
1978{
1979 X509 *crt;
1980 int nid;
1981
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001982 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun521a0112012-10-22 12:22:55 +02001983 return 0;
1984
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001985 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02001986 smp->flags |= SMP_F_MAY_CHANGE;
1987 return 0;
1988 }
1989
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001990 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun521a0112012-10-22 12:22:55 +02001991 if (!crt)
1992 return 0;
1993
1994 nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->key->algor->algorithm));
1995
1996 smp->data.str.str = (char *)OBJ_nid2sn(nid);
1997 if (!smp->data.str.str)
1998 return 0;
1999
2000 smp->type = SMP_T_CSTR;
2001 smp->data.str.len = strlen(smp->data.str.str);
2002
2003 return 1;
2004}
2005
Emeric Brun87855892012-10-17 17:39:35 +02002006/* str, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. */
2007static int
2008smp_fetch_ssl_f_i_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2009 const struct arg *args, struct sample *smp)
2010{
2011 X509 *crt = NULL;
2012 X509_NAME *name;
2013 int ret = 0;
2014 struct chunk *smp_trash;
2015
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002016 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun87855892012-10-17 17:39:35 +02002017 return 0;
2018
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002019 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02002020 smp->flags |= SMP_F_MAY_CHANGE;
2021 return 0;
2022 }
2023
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002024 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun87855892012-10-17 17:39:35 +02002025 if (!crt)
2026 goto out;
2027
2028 name = X509_get_issuer_name(crt);
2029 if (!name)
2030 goto out;
2031
Willy Tarreau47ca5452012-12-23 20:22:19 +01002032 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02002033 if (args && args[0].type == ARGT_STR) {
2034 int pos = 1;
2035
2036 if (args[1].type == ARGT_SINT)
2037 pos = args[1].data.sint;
2038 else if (args[1].type == ARGT_UINT)
2039 pos =(int)args[1].data.uint;
2040
2041 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
2042 goto out;
2043 }
2044 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
2045 goto out;
2046
2047 smp->type = SMP_T_STR;
2048 smp->data.str = *smp_trash;
2049 ret = 1;
2050out:
2051 return ret;
2052}
2053
2054/* str, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. */
2055static int
2056smp_fetch_ssl_f_s_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2057 const struct arg *args, struct sample *smp)
2058{
2059 X509 *crt = NULL;
2060 X509_NAME *name;
2061 int ret = 0;
2062 struct chunk *smp_trash;
2063
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002064 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun87855892012-10-17 17:39:35 +02002065 return 0;
2066
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002067 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02002068 smp->flags |= SMP_F_MAY_CHANGE;
2069 return 0;
2070 }
2071
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002072 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun87855892012-10-17 17:39:35 +02002073 if (!crt)
2074 goto out;
2075
2076 name = X509_get_subject_name(crt);
2077 if (!name)
2078 goto out;
2079
Willy Tarreau47ca5452012-12-23 20:22:19 +01002080 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02002081 if (args && args[0].type == ARGT_STR) {
2082 int pos = 1;
2083
2084 if (args[1].type == ARGT_SINT)
2085 pos = args[1].data.sint;
2086 else if (args[1].type == ARGT_UINT)
2087 pos =(int)args[1].data.uint;
2088
2089 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
2090 goto out;
2091 }
2092 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
2093 goto out;
2094
2095 smp->type = SMP_T_STR;
2096 smp->data.str = *smp_trash;
2097 ret = 1;
2098out:
2099 return ret;
2100}
2101
Emeric Brun589fcad2012-10-16 14:13:26 +02002102static int
2103smp_fetch_ssl_fc_cipher(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2104 const struct arg *args, struct sample *smp)
2105{
2106 smp->flags = 0;
2107
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002108 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02002109 return 0;
2110
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002111 smp->data.str.str = (char *)SSL_get_cipher_name(l4->si[0].conn->xprt_ctx);
Emeric Brun589fcad2012-10-16 14:13:26 +02002112 if (!smp->data.str.str)
2113 return 0;
2114
2115 smp->type = SMP_T_CSTR;
2116 smp->data.str.len = strlen(smp->data.str.str);
2117
2118 return 1;
2119}
2120
2121static int
2122smp_fetch_ssl_fc_alg_keysize(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2123 const struct arg *args, struct sample *smp)
2124{
2125 smp->flags = 0;
2126
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002127 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02002128 return 0;
2129
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002130 if (!SSL_get_cipher_bits(l4->si[0].conn->xprt_ctx, (int *)&smp->data.uint))
Emeric Brun589fcad2012-10-16 14:13:26 +02002131 return 0;
2132
2133 smp->type = SMP_T_UINT;
2134
2135 return 1;
2136}
2137
2138static int
2139smp_fetch_ssl_fc_use_keysize(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2140 const struct arg *args, struct sample *smp)
2141{
2142 smp->flags = 0;
2143
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002144 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02002145 return 0;
2146
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002147 smp->data.uint = (unsigned int)SSL_get_cipher_bits(l4->si[0].conn->xprt_ctx, NULL);
Emeric Brun589fcad2012-10-16 14:13:26 +02002148 if (!smp->data.uint)
2149 return 0;
2150
2151 smp->type = SMP_T_UINT;
2152
2153 return 1;
2154}
2155
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02002156#ifdef OPENSSL_NPN_NEGOTIATED
Willy Tarreau7875d092012-09-10 08:20:03 +02002157static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002158smp_fetch_ssl_fc_npn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2159 const struct arg *args, struct sample *smp)
Willy Tarreaua33c6542012-10-15 13:19:06 +02002160{
Willy Tarreaua33c6542012-10-15 13:19:06 +02002161 smp->flags = 0;
2162 smp->type = SMP_T_CSTR;
2163
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002164 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Willy Tarreaua33c6542012-10-15 13:19:06 +02002165 return 0;
2166
2167 smp->data.str.str = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002168 SSL_get0_next_proto_negotiated(l4->si[0].conn->xprt_ctx,
Willy Tarreaua33c6542012-10-15 13:19:06 +02002169 (const unsigned char **)&smp->data.str.str, (unsigned *)&smp->data.str.len);
2170
2171 if (!smp->data.str.str)
2172 return 0;
2173
2174 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02002175}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02002176#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02002177
2178static int
Emeric Brun589fcad2012-10-16 14:13:26 +02002179smp_fetch_ssl_fc_protocol(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2180 const struct arg *args, struct sample *smp)
2181{
2182 smp->flags = 0;
2183
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002184 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02002185 return 0;
2186
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002187 smp->data.str.str = (char *)SSL_get_version(l4->si[0].conn->xprt_ctx);
Emeric Brun589fcad2012-10-16 14:13:26 +02002188 if (!smp->data.str.str)
2189 return 0;
2190
2191 smp->type = SMP_T_CSTR;
2192 smp->data.str.len = strlen(smp->data.str.str);
2193
2194 return 1;
2195}
2196
2197static int
Emeric Brunfe68f682012-10-16 14:59:28 +02002198smp_fetch_ssl_fc_session_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2199 const struct arg *args, struct sample *smp)
2200{
2201#if OPENSSL_VERSION_NUMBER > 0x0090800fL
2202 SSL_SESSION *sess;
2203
2204 smp->flags = 0;
2205 smp->type = SMP_T_CBIN;
2206
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002207 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunfe68f682012-10-16 14:59:28 +02002208 return 0;
2209
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002210 sess = SSL_get_session(l4->si[0].conn->xprt_ctx);
Emeric Brunfe68f682012-10-16 14:59:28 +02002211 if (!sess)
2212 return 0;
2213
2214 smp->data.str.str = (char *)SSL_SESSION_get_id(sess, (unsigned int *)&smp->data.str.len);
2215 if (!smp->data.str.str || !&smp->data.str.len)
2216 return 0;
2217
2218 return 1;
2219#else
2220 return 0;
2221#endif
2222}
2223
2224static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002225smp_fetch_ssl_fc_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2226 const struct arg *args, struct sample *smp)
Willy Tarreau7875d092012-09-10 08:20:03 +02002227{
2228#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2229 smp->flags = 0;
2230 smp->type = SMP_T_CSTR;
2231
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002232 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Willy Tarreau7875d092012-09-10 08:20:03 +02002233 return 0;
2234
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002235 smp->data.str.str = (char *)SSL_get_servername(l4->si[0].conn->xprt_ctx, TLSEXT_NAMETYPE_host_name);
Willy Tarreau3e394c92012-09-14 23:56:58 +02002236 if (!smp->data.str.str)
2237 return 0;
2238
Willy Tarreau7875d092012-09-10 08:20:03 +02002239 smp->data.str.len = strlen(smp->data.str.str);
2240 return 1;
2241#else
2242 return 0;
2243#endif
2244}
2245
Emeric Brun2525b6b2012-10-18 15:59:43 +02002246/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02002247static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002248smp_fetch_ssl_c_ca_err(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Emeric Brunf282a812012-09-21 15:27:54 +02002249 const struct arg *args, struct sample *smp)
2250{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002251 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02002252 return 0;
2253
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002254 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02002255 smp->flags = SMP_F_MAY_CHANGE;
2256 return 0;
2257 }
2258
2259 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002260 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CA_ERROR(l4->si[0].conn->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02002261 smp->flags = 0;
2262
2263 return 1;
2264}
2265
Emeric Brun2525b6b2012-10-18 15:59:43 +02002266/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02002267static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002268smp_fetch_ssl_c_ca_err_depth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Emeric Brunf282a812012-09-21 15:27:54 +02002269 const struct arg *args, struct sample *smp)
2270{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002271 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02002272 return 0;
2273
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002274 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02002275 smp->flags = SMP_F_MAY_CHANGE;
2276 return 0;
2277 }
2278
2279 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002280 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CAEDEPTH(l4->si[0].conn->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02002281 smp->flags = 0;
2282
2283 return 1;
2284}
2285
Emeric Brun2525b6b2012-10-18 15:59:43 +02002286/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02002287static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002288smp_fetch_ssl_c_err(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2289 const struct arg *args, struct sample *smp)
Emeric Brunf282a812012-09-21 15:27:54 +02002290{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002291 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02002292 return 0;
2293
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002294 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02002295 smp->flags = SMP_F_MAY_CHANGE;
2296 return 0;
2297 }
2298
2299 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002300 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CRTERROR(l4->si[0].conn->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02002301 smp->flags = 0;
2302
2303 return 1;
2304}
2305
Emeric Brun2525b6b2012-10-18 15:59:43 +02002306/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02002307static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002308smp_fetch_ssl_c_verify(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2309 const struct arg *args, struct sample *smp)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02002310{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002311 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02002312 return 0;
2313
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002314 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02002315 smp->flags = SMP_F_MAY_CHANGE;
2316 return 0;
2317 }
2318
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002319 if (!l4->si[0].conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02002320 return 0;
2321
2322 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002323 smp->data.uint = (unsigned int)SSL_get_verify_result(l4->si[0].conn->xprt_ctx);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02002324 smp->flags = 0;
2325
2326 return 1;
2327}
2328
Emeric Brunfb510ea2012-10-05 12:00:26 +02002329/* parse the "ca-file" bind keyword */
2330static 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 +02002331{
2332 if (!*args[cur_arg + 1]) {
2333 if (err)
2334 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
2335 return ERR_ALERT | ERR_FATAL;
2336 }
2337
Emeric Brunef42d922012-10-11 16:11:36 +02002338 if ((*args[cur_arg + 1] != '/') && global.ca_base)
2339 memprintf(&conf->ca_file, "%s/%s", global.ca_base, args[cur_arg + 1]);
2340 else
2341 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02002342
Emeric Brund94b3fe2012-09-20 18:23:56 +02002343 return 0;
2344}
2345
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002346/* parse the "ciphers" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002347static 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 +02002348{
2349 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002350 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002351 return ERR_ALERT | ERR_FATAL;
2352 }
2353
Emeric Brun76d88952012-10-05 15:47:31 +02002354 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002355 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002356 return 0;
2357}
2358
2359/* parse the "crt" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002360static 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 +02002361{
Emeric Brunc8e8d122012-10-02 18:42:10 +02002362 char path[PATH_MAX];
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002363 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002364 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002365 return ERR_ALERT | ERR_FATAL;
2366 }
2367
Emeric Brunc8e8d122012-10-02 18:42:10 +02002368 if ((*args[cur_arg + 1] != '/' ) && global.crt_base) {
2369 if ((strlen(global.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > PATH_MAX) {
2370 memprintf(err, "'%s' : path too long", args[cur_arg]);
2371 return ERR_ALERT | ERR_FATAL;
2372 }
2373 sprintf(path, "%s/%s", global.crt_base, args[cur_arg + 1]);
2374 if (ssl_sock_load_cert(path, conf, px, err) > 0)
2375 return ERR_ALERT | ERR_FATAL;
2376
2377 return 0;
2378 }
2379
Willy Tarreau4348fad2012-09-20 16:48:07 +02002380 if (ssl_sock_load_cert(args[cur_arg + 1], conf, px, err) > 0)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002381 return ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02002382
2383 return 0;
2384}
2385
Emeric Brunfb510ea2012-10-05 12:00:26 +02002386/* parse the "crl-file" bind keyword */
2387static 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 +02002388{
Emeric Brun051cdab2012-10-02 19:25:50 +02002389#ifndef X509_V_FLAG_CRL_CHECK
2390 if (err)
2391 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
2392 return ERR_ALERT | ERR_FATAL;
2393#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02002394 if (!*args[cur_arg + 1]) {
2395 if (err)
2396 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
2397 return ERR_ALERT | ERR_FATAL;
2398 }
Emeric Brun2b58d042012-09-20 17:10:03 +02002399
Emeric Brunef42d922012-10-11 16:11:36 +02002400 if ((*args[cur_arg + 1] != '/') && global.ca_base)
2401 memprintf(&conf->crl_file, "%s/%s", global.ca_base, args[cur_arg + 1]);
2402 else
2403 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02002404
Emeric Brun2b58d042012-09-20 17:10:03 +02002405 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02002406#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02002407}
2408
2409/* parse the "ecdhe" bind keyword keywords */
2410static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2411{
2412#if OPENSSL_VERSION_NUMBER < 0x0090800fL
2413 if (err)
2414 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
2415 return ERR_ALERT | ERR_FATAL;
2416#elif defined(OPENSSL_NO_ECDH)
2417 if (err)
2418 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
2419 return ERR_ALERT | ERR_FATAL;
2420#else
2421 if (!*args[cur_arg + 1]) {
2422 if (err)
2423 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
2424 return ERR_ALERT | ERR_FATAL;
2425 }
2426
2427 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002428
2429 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02002430#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002431}
2432
Emeric Brun81c00f02012-09-21 14:31:21 +02002433/* parse the "crt_ignerr" and "ca_ignerr" bind keywords */
2434static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2435{
2436 int code;
2437 char *p = args[cur_arg + 1];
2438 unsigned long long *ignerr = &conf->crt_ignerr;
2439
2440 if (!*p) {
2441 if (err)
2442 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
2443 return ERR_ALERT | ERR_FATAL;
2444 }
2445
2446 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
2447 ignerr = &conf->ca_ignerr;
2448
2449 if (strcmp(p, "all") == 0) {
2450 *ignerr = ~0ULL;
2451 return 0;
2452 }
2453
2454 while (p) {
2455 code = atoi(p);
2456 if ((code <= 0) || (code > 63)) {
2457 if (err)
2458 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
2459 args[cur_arg], code, args[cur_arg + 1]);
2460 return ERR_ALERT | ERR_FATAL;
2461 }
2462 *ignerr |= 1ULL << code;
2463 p = strchr(p, ',');
2464 if (p)
2465 p++;
2466 }
2467
Emeric Brun2cb7ae52012-10-05 14:14:21 +02002468 return 0;
2469}
2470
2471/* parse the "force-sslv3" bind keyword */
2472static int bind_parse_force_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2473{
2474 conf->ssl_options |= BC_SSL_O_USE_SSLV3;
2475 return 0;
2476}
2477
2478/* parse the "force-tlsv10" bind keyword */
2479static int bind_parse_force_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2480{
2481 conf->ssl_options |= BC_SSL_O_USE_TLSV10;
Emeric Brun2d0c4822012-10-02 13:45:20 +02002482 return 0;
2483}
2484
Emeric Brun2cb7ae52012-10-05 14:14:21 +02002485/* parse the "force-tlsv11" bind keyword */
2486static int bind_parse_force_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2487{
2488#if SSL_OP_NO_TLSv1_1
2489 conf->ssl_options |= BC_SSL_O_USE_TLSV11;
2490 return 0;
2491#else
2492 if (err)
2493 memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[cur_arg]);
2494 return ERR_ALERT | ERR_FATAL;
2495#endif
2496}
2497
2498/* parse the "force-tlsv12" bind keyword */
2499static int bind_parse_force_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2500{
2501#if SSL_OP_NO_TLSv1_2
2502 conf->ssl_options |= BC_SSL_O_USE_TLSV12;
2503 return 0;
2504#else
2505 if (err)
2506 memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[cur_arg]);
2507 return ERR_ALERT | ERR_FATAL;
2508#endif
2509}
2510
2511
Emeric Brun2d0c4822012-10-02 13:45:20 +02002512/* parse the "no-tls-tickets" bind keyword */
2513static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2514{
Emeric Brun89675492012-10-05 13:48:26 +02002515 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02002516 return 0;
2517}
2518
Emeric Brun2d0c4822012-10-02 13:45:20 +02002519
Emeric Brun9b3009b2012-10-05 11:55:06 +02002520/* parse the "no-sslv3" bind keyword */
2521static 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 +02002522{
Emeric Brun89675492012-10-05 13:48:26 +02002523 conf->ssl_options |= BC_SSL_O_NO_SSLV3;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002524 return 0;
2525}
2526
Emeric Brun9b3009b2012-10-05 11:55:06 +02002527/* parse the "no-tlsv10" bind keyword */
2528static 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 +02002529{
Emeric Brun89675492012-10-05 13:48:26 +02002530 conf->ssl_options |= BC_SSL_O_NO_TLSV10;
Emeric Brunc0ff4922012-09-28 19:37:02 +02002531 return 0;
2532}
2533
Emeric Brun9b3009b2012-10-05 11:55:06 +02002534/* parse the "no-tlsv11" bind keyword */
2535static 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 +02002536{
Emeric Brun89675492012-10-05 13:48:26 +02002537 conf->ssl_options |= BC_SSL_O_NO_TLSV11;
Emeric Brunc0ff4922012-09-28 19:37:02 +02002538 return 0;
2539}
2540
Emeric Brun9b3009b2012-10-05 11:55:06 +02002541/* parse the "no-tlsv12" bind keyword */
2542static 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 +02002543{
Emeric Brun89675492012-10-05 13:48:26 +02002544 conf->ssl_options |= BC_SSL_O_NO_TLSV12;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002545 return 0;
2546}
2547
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02002548/* parse the "npn" bind keyword */
2549static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2550{
2551#ifdef OPENSSL_NPN_NEGOTIATED
2552 char *p1, *p2;
2553
2554 if (!*args[cur_arg + 1]) {
2555 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
2556 return ERR_ALERT | ERR_FATAL;
2557 }
2558
2559 free(conf->npn_str);
2560
2561 /* the NPN string is built as a suite of (<len> <name>)* */
2562 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
2563 conf->npn_str = calloc(1, conf->npn_len);
2564 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
2565
2566 /* replace commas with the name length */
2567 p1 = conf->npn_str;
2568 p2 = p1 + 1;
2569 while (1) {
2570 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
2571 if (!p2)
2572 p2 = p1 + 1 + strlen(p1 + 1);
2573
2574 if (p2 - (p1 + 1) > 255) {
2575 *p2 = '\0';
2576 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
2577 return ERR_ALERT | ERR_FATAL;
2578 }
2579
2580 *p1 = p2 - (p1 + 1);
2581 p1 = p2;
2582
2583 if (!*p2)
2584 break;
2585
2586 *(p2++) = '\0';
2587 }
2588 return 0;
2589#else
2590 if (err)
2591 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
2592 return ERR_ALERT | ERR_FATAL;
2593#endif
2594}
2595
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002596/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002597static 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 +02002598{
Willy Tarreau81796be2012-09-22 19:11:47 +02002599 struct listener *l;
2600
Willy Tarreau4348fad2012-09-20 16:48:07 +02002601 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02002602
2603 if (global.listen_default_ciphers && !conf->ciphers)
2604 conf->ciphers = strdup(global.listen_default_ciphers);
2605
Willy Tarreau81796be2012-09-22 19:11:47 +02002606 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002607 l->xprt = &ssl_sock;
Willy Tarreau81796be2012-09-22 19:11:47 +02002608
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002609 return 0;
2610}
2611
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002612/* parse the "strict-sni" bind keyword */
2613static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2614{
2615 conf->strict_sni = 1;
2616 return 0;
2617}
2618
Emeric Brund94b3fe2012-09-20 18:23:56 +02002619/* parse the "verify" bind keyword */
2620static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2621{
2622 if (!*args[cur_arg + 1]) {
2623 if (err)
2624 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
2625 return ERR_ALERT | ERR_FATAL;
2626 }
2627
2628 if (strcmp(args[cur_arg + 1], "none") == 0)
2629 conf->verify = SSL_VERIFY_NONE;
2630 else if (strcmp(args[cur_arg + 1], "optional") == 0)
2631 conf->verify = SSL_VERIFY_PEER;
2632 else if (strcmp(args[cur_arg + 1], "required") == 0)
2633 conf->verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
2634 else {
2635 if (err)
2636 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
2637 args[cur_arg], args[cur_arg + 1]);
2638 return ERR_ALERT | ERR_FATAL;
2639 }
2640
2641 return 0;
2642}
2643
Willy Tarreau92faadf2012-10-10 23:04:25 +02002644/************** "server" keywords ****************/
2645
Emeric Brunef42d922012-10-11 16:11:36 +02002646/* parse the "ca-file" server keyword */
2647static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2648{
2649 if (!*args[*cur_arg + 1]) {
2650 if (err)
2651 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
2652 return ERR_ALERT | ERR_FATAL;
2653 }
2654
2655 if ((*args[*cur_arg + 1] != '/') && global.ca_base)
2656 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global.ca_base, args[*cur_arg + 1]);
2657 else
2658 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
2659
2660 return 0;
2661}
2662
Willy Tarreau92faadf2012-10-10 23:04:25 +02002663/* parse the "check-ssl" server keyword */
2664static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2665{
2666 newsrv->check.use_ssl = 1;
2667 if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
2668 newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers);
2669 return 0;
2670}
2671
2672/* parse the "ciphers" server keyword */
2673static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2674{
2675 if (!*args[*cur_arg + 1]) {
2676 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
2677 return ERR_ALERT | ERR_FATAL;
2678 }
2679
2680 free(newsrv->ssl_ctx.ciphers);
2681 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
2682 return 0;
2683}
2684
Emeric Brunef42d922012-10-11 16:11:36 +02002685/* parse the "crl-file" server keyword */
2686static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2687{
2688#ifndef X509_V_FLAG_CRL_CHECK
2689 if (err)
2690 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
2691 return ERR_ALERT | ERR_FATAL;
2692#else
2693 if (!*args[*cur_arg + 1]) {
2694 if (err)
2695 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
2696 return ERR_ALERT | ERR_FATAL;
2697 }
2698
2699 if ((*args[*cur_arg + 1] != '/') && global.ca_base)
2700 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global.ca_base, args[*cur_arg + 1]);
2701 else
2702 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
2703
2704 return 0;
2705#endif
2706}
2707
Emeric Bruna7aa3092012-10-26 12:58:00 +02002708/* parse the "crt" server keyword */
2709static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2710{
2711 if (!*args[*cur_arg + 1]) {
2712 if (err)
2713 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
2714 return ERR_ALERT | ERR_FATAL;
2715 }
2716
2717 if ((*args[*cur_arg + 1] != '/') && global.crt_base)
2718 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global.ca_base, args[*cur_arg + 1]);
2719 else
2720 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
2721
2722 return 0;
2723}
Emeric Brunef42d922012-10-11 16:11:36 +02002724
Willy Tarreau92faadf2012-10-10 23:04:25 +02002725/* parse the "force-sslv3" server keyword */
2726static int srv_parse_force_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2727{
2728 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_SSLV3;
2729 return 0;
2730}
2731
2732/* parse the "force-tlsv10" server keyword */
2733static int srv_parse_force_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2734{
2735 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV10;
2736 return 0;
2737}
2738
2739/* parse the "force-tlsv11" server keyword */
2740static int srv_parse_force_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2741{
2742#if SSL_OP_NO_TLSv1_1
2743 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV11;
2744 return 0;
2745#else
2746 if (err)
2747 memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[*cur_arg]);
2748 return ERR_ALERT | ERR_FATAL;
2749#endif
2750}
2751
2752/* parse the "force-tlsv12" server keyword */
2753static int srv_parse_force_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2754{
2755#if SSL_OP_NO_TLSv1_2
2756 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV12;
2757 return 0;
2758#else
2759 if (err)
2760 memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[*cur_arg]);
2761 return ERR_ALERT | ERR_FATAL;
2762#endif
2763}
2764
2765/* parse the "no-sslv3" server keyword */
2766static int srv_parse_no_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2767{
2768 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_SSLV3;
2769 return 0;
2770}
2771
2772/* parse the "no-tlsv10" server keyword */
2773static int srv_parse_no_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2774{
2775 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV10;
2776 return 0;
2777}
2778
2779/* parse the "no-tlsv11" server keyword */
2780static int srv_parse_no_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2781{
2782 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV11;
2783 return 0;
2784}
2785
2786/* parse the "no-tlsv12" server keyword */
2787static int srv_parse_no_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2788{
2789 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV12;
2790 return 0;
2791}
2792
Emeric Brunf9c5c472012-10-11 15:28:34 +02002793/* parse the "no-tls-tickets" server keyword */
2794static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2795{
2796 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
2797 return 0;
2798}
2799
Willy Tarreau92faadf2012-10-10 23:04:25 +02002800/* parse the "ssl" server keyword */
2801static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2802{
2803 newsrv->use_ssl = 1;
2804 if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
2805 newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers);
2806 return 0;
2807}
2808
Emeric Brunef42d922012-10-11 16:11:36 +02002809/* parse the "verify" server keyword */
2810static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2811{
2812 if (!*args[*cur_arg + 1]) {
2813 if (err)
2814 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
2815 return ERR_ALERT | ERR_FATAL;
2816 }
2817
2818 if (strcmp(args[*cur_arg + 1], "none") == 0)
2819 newsrv->ssl_ctx.verify = SSL_VERIFY_NONE;
2820 else if (strcmp(args[*cur_arg + 1], "required") == 0)
2821 newsrv->ssl_ctx.verify = SSL_VERIFY_PEER;
2822 else {
2823 if (err)
2824 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
2825 args[*cur_arg], args[*cur_arg + 1]);
2826 return ERR_ALERT | ERR_FATAL;
2827 }
2828
2829 return 0;
2830}
2831
Willy Tarreau7875d092012-09-10 08:20:03 +02002832/* Note: must not be declared <const> as its list will be overwritten.
2833 * Please take care of keeping this list alphabetically sorted.
2834 */
2835static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Emeric Brun2525b6b2012-10-18 15:59:43 +02002836 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
2837 { "ssl_c_ca_err_depth", smp_fetch_ssl_c_ca_err_depth, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
2838 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun87855892012-10-17 17:39:35 +02002839 { "ssl_c_i_dn", smp_fetch_ssl_c_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun521a0112012-10-22 12:22:55 +02002840 { "ssl_c_key_alg", smp_fetch_ssl_c_key_alg, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brunce5ad802012-10-22 14:11:22 +02002841 { "ssl_c_notafter", smp_fetch_ssl_c_notafter, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
2842 { "ssl_c_notbefore", smp_fetch_ssl_c_notbefore, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun7f56e742012-10-19 18:15:40 +02002843 { "ssl_c_sig_alg", smp_fetch_ssl_c_sig_alg, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun87855892012-10-17 17:39:35 +02002844 { "ssl_c_s_dn", smp_fetch_ssl_c_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau8d598402012-10-22 17:58:39 +02002845 { "ssl_c_serial", smp_fetch_ssl_c_serial, 0, NULL, SMP_T_BIN, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun9143d372012-12-20 15:44:16 +01002846 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002847 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Bruna7359fd2012-10-17 15:03:11 +02002848 { "ssl_c_version", smp_fetch_ssl_c_version, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun87855892012-10-17 17:39:35 +02002849 { "ssl_f_i_dn", smp_fetch_ssl_f_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun521a0112012-10-22 12:22:55 +02002850 { "ssl_f_key_alg", smp_fetch_ssl_f_key_alg, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brunce5ad802012-10-22 14:11:22 +02002851 { "ssl_f_notafter", smp_fetch_ssl_f_notafter, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
2852 { "ssl_f_notbefore", smp_fetch_ssl_f_notbefore, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun7f56e742012-10-19 18:15:40 +02002853 { "ssl_f_sig_alg", smp_fetch_ssl_f_sig_alg, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun87855892012-10-17 17:39:35 +02002854 { "ssl_f_s_dn", smp_fetch_ssl_f_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau8d598402012-10-22 17:58:39 +02002855 { "ssl_f_serial", smp_fetch_ssl_f_serial, 0, NULL, SMP_T_BIN, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Bruna7359fd2012-10-17 15:03:11 +02002856 { "ssl_f_version", smp_fetch_ssl_f_version, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002857 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun589fcad2012-10-16 14:13:26 +02002858 { "ssl_fc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
2859 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002860 { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
2861 { "ssl_fc_has_sni", smp_fetch_ssl_fc_has_sni, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreaua33c6542012-10-15 13:19:06 +02002862#ifdef OPENSSL_NPN_NEGOTIATED
Emeric Brun2525b6b2012-10-18 15:59:43 +02002863 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreaua33c6542012-10-15 13:19:06 +02002864#endif
Emeric Brun589fcad2012-10-16 14:13:26 +02002865 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
2866 { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brunfe68f682012-10-16 14:59:28 +02002867 { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002868 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau7875d092012-09-10 08:20:03 +02002869 { NULL, NULL, 0, 0, 0 },
2870}};
2871
2872/* Note: must not be declared <const> as its list will be overwritten.
2873 * Please take care of keeping this list alphabetically sorted.
2874 */
2875static struct acl_kw_list acl_kws = {{ },{
Emeric Brun2525b6b2012-10-18 15:59:43 +02002876 { "ssl_c_ca_err", acl_parse_int, smp_fetch_ssl_c_ca_err, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2877 { "ssl_c_ca_err_depth", acl_parse_int, smp_fetch_ssl_c_ca_err_depth, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2878 { "ssl_c_err", acl_parse_int, smp_fetch_ssl_c_err, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun87855892012-10-17 17:39:35 +02002879 { "ssl_c_i_dn", acl_parse_str, smp_fetch_ssl_c_i_dn, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, ARG2(0,STR,SINT) },
Emeric Brun521a0112012-10-22 12:22:55 +02002880 { "ssl_c_key_alg", acl_parse_str, smp_fetch_ssl_c_key_alg, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brunce5ad802012-10-22 14:11:22 +02002881 { "ssl_c_notafter", acl_parse_str, smp_fetch_ssl_c_notafter, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2882 { "ssl_c_notbefore", acl_parse_str, smp_fetch_ssl_c_notbefore, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun7f56e742012-10-19 18:15:40 +02002883 { "ssl_c_sig_alg", acl_parse_str, smp_fetch_ssl_c_sig_alg, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun87855892012-10-17 17:39:35 +02002884 { "ssl_c_s_dn", acl_parse_str, smp_fetch_ssl_c_s_dn, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, ARG2(0,STR,SINT) },
Willy Tarreau8d598402012-10-22 17:58:39 +02002885 { "ssl_c_serial", acl_parse_bin, smp_fetch_ssl_c_serial, acl_match_bin, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun9143d372012-12-20 15:44:16 +01002886 { "ssl_c_used", acl_parse_int, smp_fetch_ssl_c_used, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002887 { "ssl_c_verify", acl_parse_int, smp_fetch_ssl_c_verify, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Bruna7359fd2012-10-17 15:03:11 +02002888 { "ssl_c_version", acl_parse_int, smp_fetch_ssl_c_version, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun87855892012-10-17 17:39:35 +02002889 { "ssl_f_i_dn", acl_parse_str, smp_fetch_ssl_f_i_dn, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, ARG2(0,STR,SINT) },
Emeric Brun521a0112012-10-22 12:22:55 +02002890 { "ssl_f_key_alg", acl_parse_str, smp_fetch_ssl_f_key_alg, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brunce5ad802012-10-22 14:11:22 +02002891 { "ssl_f_notafter", acl_parse_str, smp_fetch_ssl_f_notafter, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2892 { "ssl_f_notbefore", acl_parse_str, smp_fetch_ssl_f_notbefore, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun7f56e742012-10-19 18:15:40 +02002893 { "ssl_f_sig_alg", acl_parse_str, smp_fetch_ssl_f_sig_alg, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun87855892012-10-17 17:39:35 +02002894 { "ssl_f_s_dn", acl_parse_str, smp_fetch_ssl_f_s_dn, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, ARG2(0,STR,SINT) },
Willy Tarreau8d598402012-10-22 17:58:39 +02002895 { "ssl_f_serial", acl_parse_bin, smp_fetch_ssl_f_serial, acl_match_bin, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Bruna7359fd2012-10-17 15:03:11 +02002896 { "ssl_f_version", acl_parse_int, smp_fetch_ssl_f_version, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002897 { "ssl_fc", acl_parse_int, smp_fetch_ssl_fc, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun589fcad2012-10-16 14:13:26 +02002898 { "ssl_fc_alg_keysize", acl_parse_str, smp_fetch_ssl_fc_alg_keysize, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2899 { "ssl_fc_cipher", acl_parse_str, smp_fetch_ssl_fc_cipher, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002900 { "ssl_fc_has_crt", acl_parse_int, smp_fetch_ssl_fc_has_crt, acl_match_nothing, ACL_USE_L6REQ_PERMANENT, 0 },
2901 { "ssl_fc_has_sni", acl_parse_int, smp_fetch_ssl_fc_has_sni, acl_match_nothing, ACL_USE_L6REQ_PERMANENT, 0 },
Willy Tarreaua33c6542012-10-15 13:19:06 +02002902#ifdef OPENSSL_NPN_NEGOTIATED
Emeric Brun2525b6b2012-10-18 15:59:43 +02002903 { "ssl_fc_npn", acl_parse_str, smp_fetch_ssl_fc_npn, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Willy Tarreaua33c6542012-10-15 13:19:06 +02002904#endif
Emeric Brun589fcad2012-10-16 14:13:26 +02002905 { "ssl_fc_protocol", acl_parse_str, smp_fetch_ssl_fc_protocol, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2906 { "ssl_fc_use_keysize", acl_parse_str, smp_fetch_ssl_fc_use_keysize, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002907 { "ssl_fc_sni", acl_parse_str, smp_fetch_ssl_fc_sni, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2908 { "ssl_fc_sni_end", acl_parse_str, smp_fetch_ssl_fc_sni, acl_match_end, ACL_USE_L6REQ_PERMANENT, 0 },
2909 { "ssl_fc_sni_reg", acl_parse_reg, smp_fetch_ssl_fc_sni, acl_match_reg, ACL_USE_L6REQ_PERMANENT, 0 },
Willy Tarreau7875d092012-09-10 08:20:03 +02002910 { NULL, NULL, NULL, NULL },
2911}};
2912
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002913/* Note: must not be declared <const> as its list will be overwritten.
2914 * Please take care of keeping this list alphabetically sorted, doing so helps
2915 * all code contributors.
2916 * Optional keywords are also declared with a NULL ->parse() function so that
2917 * the config parser can report an appropriate error when a known keyword was
2918 * not enabled.
2919 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002920static struct bind_kw_list bind_kws = { "SSL", { }, {
Emeric Brunfb510ea2012-10-05 12:00:26 +02002921 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
Emeric Brun2d0c4822012-10-02 13:45:20 +02002922 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
2923 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emeric Brunfb510ea2012-10-05 12:00:26 +02002924 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
Emeric Brun2d0c4822012-10-02 13:45:20 +02002925 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
2926 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
2927 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emeric Brun2cb7ae52012-10-05 14:14:21 +02002928 { "force-sslv3", bind_parse_force_sslv3, 0 }, /* force SSLv3 */
2929 { "force-tlsv10", bind_parse_force_tlsv10, 0 }, /* force TLSv10 */
2930 { "force-tlsv11", bind_parse_force_tlsv11, 0 }, /* force TLSv11 */
2931 { "force-tlsv12", bind_parse_force_tlsv12, 0 }, /* force TLSv12 */
Emeric Brun9b3009b2012-10-05 11:55:06 +02002932 { "no-sslv3", bind_parse_no_sslv3, 0 }, /* disable SSLv3 */
2933 { "no-tlsv10", bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */
2934 { "no-tlsv11", bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */
2935 { "no-tlsv12", bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */
Emeric Brun2d0c4822012-10-02 13:45:20 +02002936 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
Emeric Brun2d0c4822012-10-02 13:45:20 +02002937 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002938 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
Emeric Brun2d0c4822012-10-02 13:45:20 +02002939 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02002940 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002941 { NULL, NULL, 0 },
2942}};
Emeric Brun46591952012-05-18 15:47:34 +02002943
Willy Tarreau92faadf2012-10-10 23:04:25 +02002944/* Note: must not be declared <const> as its list will be overwritten.
2945 * Please take care of keeping this list alphabetically sorted, doing so helps
2946 * all code contributors.
2947 * Optional keywords are also declared with a NULL ->parse() function so that
2948 * the config parser can report an appropriate error when a known keyword was
2949 * not enabled.
2950 */
2951static struct srv_kw_list srv_kws = { "SSL", { }, {
Emeric Brunef42d922012-10-11 16:11:36 +02002952 { "ca-file", srv_parse_ca_file, 1, 0 }, /* set CAfile to process verify server cert */
Emeric Brunecc91fe2012-10-11 15:05:10 +02002953 { "check-ssl", srv_parse_check_ssl, 0, 0 }, /* enable SSL for health checks */
2954 { "ciphers", srv_parse_ciphers, 1, 0 }, /* select the cipher suite */
Emeric Brunef42d922012-10-11 16:11:36 +02002955 { "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 +02002956 { "crt", srv_parse_crt, 1, 0 }, /* set client certificate */
Emeric Brunecc91fe2012-10-11 15:05:10 +02002957 { "force-sslv3", srv_parse_force_sslv3, 0, 0 }, /* force SSLv3 */
2958 { "force-tlsv10", srv_parse_force_tlsv10, 0, 0 }, /* force TLSv10 */
2959 { "force-tlsv11", srv_parse_force_tlsv11, 0, 0 }, /* force TLSv11 */
2960 { "force-tlsv12", srv_parse_force_tlsv12, 0, 0 }, /* force TLSv12 */
2961 { "no-sslv3", srv_parse_no_sslv3, 0, 0 }, /* disable SSLv3 */
2962 { "no-tlsv10", srv_parse_no_tlsv10, 0, 0 }, /* disable TLSv10 */
2963 { "no-tlsv11", srv_parse_no_tlsv11, 0, 0 }, /* disable TLSv11 */
2964 { "no-tlsv12", srv_parse_no_tlsv12, 0, 0 }, /* disable TLSv12 */
Emeric Brunf9c5c472012-10-11 15:28:34 +02002965 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 0 }, /* disable session resumption tickets */
Emeric Brunecc91fe2012-10-11 15:05:10 +02002966 { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */
Emeric Brunef42d922012-10-11 16:11:36 +02002967 { "verify", srv_parse_verify, 1, 0 }, /* set SSL verify method */
Willy Tarreau92faadf2012-10-10 23:04:25 +02002968 { NULL, NULL, 0, 0 },
2969}};
2970
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002971/* transport-layer operations for SSL sockets */
2972struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +02002973 .snd_buf = ssl_sock_from_buf,
2974 .rcv_buf = ssl_sock_to_buf,
2975 .rcv_pipe = NULL,
2976 .snd_pipe = NULL,
2977 .shutr = NULL,
2978 .shutw = ssl_sock_shutw,
2979 .close = ssl_sock_close,
2980 .init = ssl_sock_init,
2981};
2982
2983__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +02002984static void __ssl_sock_init(void)
2985{
Emeric Brun46591952012-05-18 15:47:34 +02002986 STACK_OF(SSL_COMP)* cm;
2987
2988 SSL_library_init();
2989 cm = SSL_COMP_get_compression_methods();
2990 sk_SSL_COMP_zero(cm);
Willy Tarreau7875d092012-09-10 08:20:03 +02002991 sample_register_fetches(&sample_fetch_keywords);
2992 acl_register_keywords(&acl_kws);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002993 bind_register_keywords(&bind_kws);
Willy Tarreau92faadf2012-10-10 23:04:25 +02002994 srv_register_keywords(&srv_kws);
Emeric Brun46591952012-05-18 15:47:34 +02002995}
2996
2997/*
2998 * Local variables:
2999 * c-indent-level: 8
3000 * c-basic-offset: 8
3001 * End:
3002 */