blob: df09f9ad5d0a5ae349b2fbd5c06635167053c54e [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>
Emeric Brun46591952012-05-18 15:47:34 +020046
47#include <common/buffer.h>
48#include <common/compat.h>
49#include <common/config.h>
50#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020051#include <common/errors.h>
Emeric Brun46591952012-05-18 15:47:34 +020052#include <common/standard.h>
53#include <common/ticks.h>
54#include <common/time.h>
55
Emeric Brunfc0421f2012-09-07 17:30:07 +020056#include <ebsttree.h>
57
58#include <types/global.h>
59#include <types/ssl_sock.h>
60
Willy Tarreau7875d092012-09-10 08:20:03 +020061#include <proto/acl.h>
62#include <proto/arg.h>
Emeric Brun46591952012-05-18 15:47:34 +020063#include <proto/connection.h>
64#include <proto/fd.h>
65#include <proto/freq_ctr.h>
66#include <proto/frontend.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020067#include <proto/listener.h>
Emeric Brun46591952012-05-18 15:47:34 +020068#include <proto/log.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020069#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020070#include <proto/ssl_sock.h>
71#include <proto/task.h>
72
Emeric Brune64aef12012-09-21 13:15:06 +020073#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brunf282a812012-09-21 15:27:54 +020074/* bits 0xFFFF0000 are reserved to store verify errors */
75
76/* Verify errors macros */
77#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
78#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
79#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
80
81#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
82#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
83#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +020084
Willy Tarreau403edff2012-09-06 11:58:37 +020085static int sslconns = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +020086
87void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
88{
89 struct connection *conn = (struct connection *)SSL_get_app_data(ssl);
90 (void)ret; /* shut gcc stupid warning */
91
92 if (where & SSL_CB_HANDSHAKE_START) {
93 /* Disable renegotiation (CVE-2009-3555) */
94 if (conn->flags & CO_FL_CONNECTED)
95 conn->flags |= CO_FL_ERROR;
96 }
Emeric Brunfc0421f2012-09-07 17:30:07 +020097}
98
Emeric Brune64aef12012-09-21 13:15:06 +020099/* Callback is called for each certificate of the chain during a verify
100 ok is set to 1 if preverify detect no error on current certificate.
101 Returns 0 to break the handshake, 1 otherwise. */
102int ssl_sock_verifycbk(int ok, X509_STORE_CTX *x_store)
103{
104 SSL *ssl;
105 struct connection *conn;
Emeric Brun81c00f02012-09-21 14:31:21 +0200106 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +0200107
108 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
109 conn = (struct connection *)SSL_get_app_data(ssl);
110
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200111 conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +0200112
Emeric Brun81c00f02012-09-21 14:31:21 +0200113 if (ok) /* no errors */
114 return ok;
115
116 depth = X509_STORE_CTX_get_error_depth(x_store);
117 err = X509_STORE_CTX_get_error(x_store);
118
119 /* check if CA error needs to be ignored */
120 if (depth > 0) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200121 if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) {
122 conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
123 conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +0200124 }
125
Emeric Brun81c00f02012-09-21 14:31:21 +0200126 if (target_client(&conn->target)->bind_conf->ca_ignerr & (1ULL << err))
127 return 1;
128
129 return 0;
130 }
131
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200132 if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st))
133 conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +0200134
Emeric Brun81c00f02012-09-21 14:31:21 +0200135 /* check if certificate error needs to be ignored */
136 if (target_client(&conn->target)->bind_conf->crt_ignerr & (1ULL << err))
137 return 1;
138
139 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +0200140}
141
Emeric Brunfc0421f2012-09-07 17:30:07 +0200142#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
143/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
144 * warning when no match is found, which implies the default (first) cert
145 * will keep being used.
146 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200147static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200148{
149 const char *servername;
150 const char *wildp = NULL;
151 struct ebmb_node *node;
152 int i;
153 (void)al; /* shut gcc stupid warning */
154
155 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
156 if (!servername)
157 return SSL_TLSEXT_ERR_NOACK;
158
159 for (i = 0; i < trashlen; i++) {
160 if (!servername[i])
161 break;
162 trash[i] = tolower(servername[i]);
163 if (!wildp && (trash[i] == '.'))
164 wildp = &trash[i];
165 }
166 trash[i] = 0;
167
168 /* lookup in full qualified names */
169 node = ebst_lookup(&s->sni_ctx, trash);
170 if (!node) {
171 if (!wildp)
172 return SSL_TLSEXT_ERR_ALERT_WARNING;
173
174 /* lookup in full wildcards names */
175 node = ebst_lookup(&s->sni_w_ctx, wildp);
176 if (!node)
177 return SSL_TLSEXT_ERR_ALERT_WARNING;
178 }
179
180 /* switch ctx */
181 SSL_set_SSL_CTX(ssl, container_of(node, struct sni_ctx, name)->ctx);
182 return SSL_TLSEXT_ERR_OK;
183}
184#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
185
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200186#ifndef OPENSSL_NO_DH
187/* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1
188 if an error occured, and 0 if parameter not found. */
189int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file)
190{
191 int ret = -1;
192 BIO *in;
193 DH *dh = NULL;
194
195 in = BIO_new(BIO_s_file());
196 if (in == NULL)
197 goto end;
198
199 if (BIO_read_filename(in, file) <= 0)
200 goto end;
201
202 dh = PEM_read_bio_DHparams(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata);
203 if (dh) {
204 SSL_CTX_set_tmp_dh(ctx, dh);
205 ret = 1;
206 goto end;
207 }
208
209 ret = 0; /* DH params not found */
210end:
211 if (dh)
212 DH_free(dh);
213
214 if (in)
215 BIO_free(in);
216
217 return ret;
218}
219#endif
220
Emeric Brunfc0421f2012-09-07 17:30:07 +0200221/* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if
222 * an early error happens and the caller must call SSL_CTX_free() by itelf.
223 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200224int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200225{
226 BIO *in;
227 X509 *x = NULL, *ca;
228 int i, len, err;
229 int ret = -1;
230 int order = 0;
231 X509_NAME *xname;
232 char *str;
233 struct sni_ctx *sc;
234#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
235 STACK_OF(GENERAL_NAME) *names;
236#endif
237
238 in = BIO_new(BIO_s_file());
239 if (in == NULL)
240 goto end;
241
242 if (BIO_read_filename(in, file) <= 0)
243 goto end;
244
245 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata);
246 if (x == NULL)
247 goto end;
248
249#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
250 names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
251 if (names) {
252 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
253 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
254 if (name->type == GEN_DNS) {
255 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
256 if ((len = strlen(str))) {
257 int j;
258
259 if (*str != '*') {
260 sc = malloc(sizeof(struct sni_ctx) + len + 1);
261 for (j = 0; j < len; j++)
262 sc->name.key[j] = tolower(str[j]);
263 sc->name.key[len] = 0;
264 sc->order = order++;
265 sc->ctx = ctx;
266 ebst_insert(&s->sni_ctx, &sc->name);
267 }
268 else {
269 sc = malloc(sizeof(struct sni_ctx) + len);
270 for (j = 1; j < len; j++)
271 sc->name.key[j-1] = tolower(str[j]);
272 sc->name.key[len-1] = 0;
273 sc->order = order++;
274 sc->ctx = ctx;
275 ebst_insert(&s->sni_w_ctx, &sc->name);
276 }
277 }
278 OPENSSL_free(str);
279 }
280 }
281 }
282 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
283 }
284#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
285
286 xname = X509_get_subject_name(x);
287 i = -1;
288 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
289 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
290 if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) {
291 if ((len = strlen(str))) {
292 int j;
293
294 if (*str != '*') {
295 sc = malloc(sizeof(struct sni_ctx) + len + 1);
296 for (j = 0; j < len; j++)
297 sc->name.key[j] = tolower(str[j]);
298 sc->name.key[len] = 0;
299 sc->order = order++;
300 sc->ctx = ctx;
301 ebst_insert(&s->sni_ctx, &sc->name);
302 }
303 else {
304 sc = malloc(sizeof(struct sni_ctx) + len);
305 for (j = 1; j < len; j++)
306 sc->name.key[j-1] = tolower(str[j]);
307 sc->name.key[len-1] = 0;
308 sc->order = order++;
309 sc->ctx = ctx;
310 ebst_insert(&s->sni_w_ctx, &sc->name);
311 }
312 }
313 OPENSSL_free(str);
314 }
315 }
316
317 ret = 0; /* the caller must not free the SSL_CTX argument anymore */
318 if (!SSL_CTX_use_certificate(ctx, x))
319 goto end;
320
321 if (ctx->extra_certs != NULL) {
322 sk_X509_pop_free(ctx->extra_certs, X509_free);
323 ctx->extra_certs = NULL;
324 }
325
326 while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata))) {
327 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
328 X509_free(ca);
329 goto end;
330 }
331 }
332
333 err = ERR_get_error();
334 if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
335 /* we successfully reached the last cert in the file */
336 ret = 1;
337 }
338 ERR_clear_error();
339
340end:
341 if (x)
342 X509_free(x);
343
344 if (in)
345 BIO_free(in);
346
347 return ret;
348}
349
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200350static 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 +0200351{
352 int ret;
353 SSL_CTX *ctx;
354
355 ctx = SSL_CTX_new(SSLv23_server_method());
356 if (!ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200357 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
358 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200359 return 1;
360 }
361
362 if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200363 memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
364 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200365 SSL_CTX_free(ctx);
366 return 1;
367 }
368
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200369 ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200370 if (ret <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200371 memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
372 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200373 if (ret < 0) /* serious error, must do that ourselves */
374 SSL_CTX_free(ctx);
375 return 1;
376 }
377 /* we must not free the SSL_CTX anymore below, since it's already in
378 * the tree, so it will be discovered and cleaned in time.
379 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200380#ifndef OPENSSL_NO_DH
381 ret = ssl_sock_load_dh_params(ctx, path);
382 if (ret < 0) {
383 if (err)
384 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
385 *err ? *err : "", path);
386 return 1;
387 }
388#endif
389
Emeric Brunfc0421f2012-09-07 17:30:07 +0200390#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200391 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200392 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
393 err && *err ? *err : "");
Emeric Brunfc0421f2012-09-07 17:30:07 +0200394 return 1;
395 }
396#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200397 if (!bind_conf->default_ctx)
398 bind_conf->default_ctx = ctx;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200399
400 return 0;
401}
402
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200403int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200404{
405 struct dirent *de;
406 DIR *dir;
407 struct stat buf;
408 int pathlen = 0;
409 char *end, *fp;
410 int cfgerr = 0;
411
412 if (!(dir = opendir(path)))
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200413 return ssl_sock_load_cert_file(path, bind_conf, curproxy, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200414
415 /* strip trailing slashes, including first one */
416 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
417 *end = 0;
418
419 if (end >= path)
420 pathlen = end + 1 - path;
421 fp = malloc(pathlen + 1 + NAME_MAX + 1);
422
423 while ((de = readdir(dir))) {
424 snprintf(fp, pathlen + 1 + NAME_MAX + 1, "%s/%s", path, de->d_name);
425 if (stat(fp, &buf) != 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200426 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
427 err && *err ? *err : "", fp, strerror(errno));
Emeric Brunfc0421f2012-09-07 17:30:07 +0200428 cfgerr++;
429 continue;
430 }
431 if (!S_ISREG(buf.st_mode))
432 continue;
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200433 cfgerr += ssl_sock_load_cert_file(fp, bind_conf, curproxy, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200434 }
435 free(fp);
436 closedir(dir);
437 return cfgerr;
438}
439
440#ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */
441#define SSL_OP_CIPHER_SERVER_PREFERENCE 0
442#endif
443
444#ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */
445#define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0
446#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200447#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */
448#define SSL_OP_SINGLE_ECDH_USE 0
449#endif
Emeric Brun2d0c4822012-10-02 13:45:20 +0200450#ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */
451#define SSL_OP_NO_TICKET 0
452#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200453#ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */
454#define SSL_OP_NO_COMPRESSION 0
455#endif
Emeric Brunc0ff4922012-09-28 19:37:02 +0200456#ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */
457#define SSL_OP_NO_TLSv1_1 0
458#endif
459#ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */
460#define SSL_OP_NO_TLSv1_2 0
461#endif
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200462#ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */
463#define SSL_OP_SINGLE_DH_USE 0
464#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200465#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */
466#define SSL_OP_SINGLE_ECDH_USE 0
467#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200468#ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */
469#define SSL_MODE_RELEASE_BUFFERS 0
470#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200471int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, SSL_CTX *ctx, struct proxy *curproxy)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200472{
473 int cfgerr = 0;
474 int ssloptions =
475 SSL_OP_ALL | /* all known workarounds for bugs */
476 SSL_OP_NO_SSLv2 |
477 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200478 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +0200479 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +0200480 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
481 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200482 int sslmode =
483 SSL_MODE_ENABLE_PARTIAL_WRITE |
484 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
485 SSL_MODE_RELEASE_BUFFERS;
486
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200487 if (bind_conf->nosslv3)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200488 ssloptions |= SSL_OP_NO_SSLv3;
Emeric Brunc0ff4922012-09-28 19:37:02 +0200489 if (bind_conf->notlsv10)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200490 ssloptions |= SSL_OP_NO_TLSv1;
Emeric Brunc0ff4922012-09-28 19:37:02 +0200491 if (bind_conf->notlsv11)
492 ssloptions |= SSL_OP_NO_TLSv1_1;
493 if (bind_conf->notlsv12)
494 ssloptions |= SSL_OP_NO_TLSv1_2;
Emeric Brun2d0c4822012-10-02 13:45:20 +0200495 if (bind_conf->no_tls_tickets)
496 ssloptions |= SSL_OP_NO_TICKET;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200497
498 SSL_CTX_set_options(ctx, ssloptions);
499 SSL_CTX_set_mode(ctx, sslmode);
Emeric Brune64aef12012-09-21 13:15:06 +0200500 SSL_CTX_set_verify(ctx, bind_conf->verify ? bind_conf->verify : SSL_VERIFY_NONE, ssl_sock_verifycbk);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200501 if (bind_conf->verify & SSL_VERIFY_PEER) {
502 if (bind_conf->cafile) {
503 /* load CAfile to verify */
504 if (!SSL_CTX_load_verify_locations(ctx, bind_conf->cafile, NULL)) {
505 Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
506 curproxy->id, bind_conf->cafile, bind_conf->arg, bind_conf->file, bind_conf->line);
507 cfgerr++;
508 }
509 /* set CA names fo client cert request, function returns void */
510 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(bind_conf->cafile));
511 }
Emeric Brun051cdab2012-10-02 19:25:50 +0200512#ifdef X509_V_FLAG_CRL_CHECK
Emeric Brund94b3fe2012-09-20 18:23:56 +0200513 if (bind_conf->crlfile) {
514 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
515
516 if (!store || !X509_STORE_load_locations(store, bind_conf->crlfile, NULL)) {
517 Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
518 curproxy->id, bind_conf->cafile, bind_conf->arg, bind_conf->file, bind_conf->line);
519 cfgerr++;
520 }
Emeric Brun561e5742012-10-02 15:20:55 +0200521 else {
522 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
523 }
Emeric Brund94b3fe2012-09-20 18:23:56 +0200524 }
Emeric Brun051cdab2012-10-02 19:25:50 +0200525#endif
Emeric Brund94b3fe2012-09-20 18:23:56 +0200526 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200527
528 shared_context_set_cache(ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200529 if (bind_conf->ciphers &&
530 !SSL_CTX_set_cipher_list(ctx, bind_conf->ciphers)) {
Emeric Brunfc0421f2012-09-07 17:30:07 +0200531 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 +0200532 curproxy->id, bind_conf->ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200533 cfgerr++;
534 }
535
536 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
537#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
538 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200539 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200540#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200541#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
542 if (bind_conf->ecdhe) {
543 int i;
544 EC_KEY *ecdh;
545
546 i = OBJ_sn2nid(bind_conf->ecdhe);
547 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
548 Alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
549 curproxy->id, bind_conf->ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
550 cfgerr++;
551 }
552 else {
553 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
554 EC_KEY_free(ecdh);
555 }
556 }
557#endif
558
Emeric Brunfc0421f2012-09-07 17:30:07 +0200559 return cfgerr;
560}
561
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200562/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +0200563 * be NULL, in which case nothing is done. Returns the number of errors
564 * encountered.
565 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200566int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf, struct proxy *px)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200567{
568 struct ebmb_node *node;
569 struct sni_ctx *sni;
570 int err = 0;
571
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200572 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200573 return 0;
574
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200575 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200576 while (node) {
577 sni = ebmb_entry(node, struct sni_ctx, name);
578 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200579 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200580 node = ebmb_next(node);
581 }
582
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200583 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200584 while (node) {
585 sni = ebmb_entry(node, struct sni_ctx, name);
586 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200587 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200588 node = ebmb_next(node);
589 }
590 return err;
591}
592
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200593/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +0200594 * be NULL, in which case nothing is done. The default_ctx is nullified too.
595 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200596void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200597{
598 struct ebmb_node *node, *back;
599 struct sni_ctx *sni;
600
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200601 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200602 return;
603
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200604 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200605 while (node) {
606 sni = ebmb_entry(node, struct sni_ctx, name);
607 back = ebmb_next(node);
608 ebmb_delete(node);
609 if (!sni->order) /* only free the CTX on its first occurrence */
610 SSL_CTX_free(sni->ctx);
611 free(sni);
612 node = back;
613 }
614
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200615 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200616 while (node) {
617 sni = ebmb_entry(node, struct sni_ctx, name);
618 back = ebmb_next(node);
619 ebmb_delete(node);
620 if (!sni->order) /* only free the CTX on its first occurrence */
621 SSL_CTX_free(sni->ctx);
622 free(sni);
623 node = back;
624 }
625
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200626 bind_conf->default_ctx = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +0200627}
628
Emeric Brun46591952012-05-18 15:47:34 +0200629/*
630 * This function is called if SSL * context is not yet allocated. The function
631 * is designed to be called before any other data-layer operation and sets the
632 * handshake flag on the connection. It is safe to call it multiple times.
633 * It returns 0 on success and -1 in error case.
634 */
635static int ssl_sock_init(struct connection *conn)
636{
637 /* already initialized */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200638 if (conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200639 return 0;
640
Willy Tarreau403edff2012-09-06 11:58:37 +0200641 if (global.maxsslconn && sslconns >= global.maxsslconn)
642 return -1;
643
Emeric Brun46591952012-05-18 15:47:34 +0200644 /* If it is in client mode initiate SSL session
645 in connect state otherwise accept state */
646 if (target_srv(&conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +0200647 /* Alloc a new SSL session ctx */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200648 conn->xprt_ctx = SSL_new(target_srv(&conn->target)->ssl_ctx.ctx);
649 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200650 return -1;
651
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200652 SSL_set_connect_state(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200653 if (target_srv(&conn->target)->ssl_ctx.reused_sess)
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200654 SSL_set_session(conn->xprt_ctx, target_srv(&conn->target)->ssl_ctx.reused_sess);
Emeric Brun46591952012-05-18 15:47:34 +0200655
656 /* set fd on SSL session context */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200657 SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +0200658
659 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +0200660 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +0200661
662 sslconns++;
Emeric Brun46591952012-05-18 15:47:34 +0200663 return 0;
664 }
665 else if (target_client(&conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +0200666 /* Alloc a new SSL session ctx */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200667 conn->xprt_ctx = SSL_new(target_client(&conn->target)->bind_conf->default_ctx);
668 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200669 return -1;
670
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200671 SSL_set_accept_state(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200672
673 /* set fd on SSL session context */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200674 SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +0200675
Emeric Brune1f38db2012-09-03 20:36:47 +0200676 /* set connection pointer */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200677 SSL_set_app_data(conn->xprt_ctx, conn);
Emeric Brune1f38db2012-09-03 20:36:47 +0200678
Emeric Brun46591952012-05-18 15:47:34 +0200679 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +0200680 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +0200681
682 sslconns++;
Emeric Brun46591952012-05-18 15:47:34 +0200683 return 0;
684 }
685 /* don't know how to handle such a target */
686 return -1;
687}
688
689
690/* This is the callback which is used when an SSL handshake is pending. It
691 * updates the FD status if it wants some polling before being called again.
692 * It returns 0 if it fails in a fatal way or needs to poll to go further,
693 * otherwise it returns non-zero and removes itself from the connection's
694 * flags (the bit is provided in <flag> by the caller).
695 */
696int ssl_sock_handshake(struct connection *conn, unsigned int flag)
697{
698 int ret;
699
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200700 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200701 goto out_error;
702
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200703 ret = SSL_do_handshake(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200704 if (ret != 1) {
705 /* handshake did not complete, let's find why */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200706 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +0200707
708 if (ret == SSL_ERROR_WANT_WRITE) {
709 /* SSL handshake needs to write, L4 connection may not be ready */
710 __conn_sock_stop_recv(conn);
711 __conn_sock_poll_send(conn);
712 return 0;
713 }
714 else if (ret == SSL_ERROR_WANT_READ) {
715 /* SSL handshake needs to read, L4 connection is ready */
716 if (conn->flags & CO_FL_WAIT_L4_CONN)
717 conn->flags &= ~CO_FL_WAIT_L4_CONN;
718 __conn_sock_stop_send(conn);
719 __conn_sock_poll_recv(conn);
720 return 0;
721 }
Willy Tarreau89230192012-09-28 20:22:13 +0200722 else if (ret == SSL_ERROR_SYSCALL) {
723 /* if errno is null, then connection was successfully established */
724 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
725 conn->flags &= ~CO_FL_WAIT_L4_CONN;
726 goto out_error;
727 }
Emeric Brun46591952012-05-18 15:47:34 +0200728 else {
729 /* Fail on all other handshake errors */
730 goto out_error;
731 }
732 }
733
734 /* Handshake succeeded */
735 if (target_srv(&conn->target)) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200736 if (!SSL_session_reused(conn->xprt_ctx)) {
Emeric Brun46591952012-05-18 15:47:34 +0200737 /* check if session was reused, if not store current session on server for reuse */
738 if (target_srv(&conn->target)->ssl_ctx.reused_sess)
739 SSL_SESSION_free(target_srv(&conn->target)->ssl_ctx.reused_sess);
740
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200741 target_srv(&conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200742 }
743 }
744
745 /* The connection is now established at both layers, it's time to leave */
746 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
747 return 1;
748
749 out_error:
Emeric Brun9fa89732012-10-04 17:09:56 +0200750 /* free resumed session if exists */
751 if (target_srv(&conn->target) && target_srv(&conn->target)->ssl_ctx.reused_sess) {
752 SSL_SESSION_free(target_srv(&conn->target)->ssl_ctx.reused_sess);
753 target_srv(&conn->target)->ssl_ctx.reused_sess = NULL;
754 }
755
Emeric Brun46591952012-05-18 15:47:34 +0200756 /* Fail on all other handshake errors */
757 conn->flags |= CO_FL_ERROR;
758 conn->flags &= ~flag;
759 return 0;
760}
761
762/* Receive up to <count> bytes from connection <conn>'s socket and store them
763 * into buffer <buf>. The caller must ensure that <count> is always smaller
764 * than the buffer's size. Only one call to recv() is performed, unless the
765 * buffer wraps, in which case a second call may be performed. The connection's
766 * flags are updated with whatever special event is detected (error, read0,
767 * empty). The caller is responsible for taking care of those events and
768 * avoiding the call if inappropriate. The function does not call the
769 * connection's polling update function, so the caller is responsible for this.
770 */
771static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
772{
773 int ret, done = 0;
774 int try = count;
775
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200776 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200777 goto out_error;
778
779 if (conn->flags & CO_FL_HANDSHAKE)
780 /* a handshake was requested */
781 return 0;
782
783 /* compute the maximum block size we can read at once. */
784 if (buffer_empty(buf)) {
785 /* let's realign the buffer to optimize I/O */
786 buf->p = buf->data;
787 }
788 else if (buf->data + buf->o < buf->p &&
789 buf->p + buf->i < buf->data + buf->size) {
790 /* remaining space wraps at the end, with a moving limit */
791 if (try > buf->data + buf->size - (buf->p + buf->i))
792 try = buf->data + buf->size - (buf->p + buf->i);
793 }
794
795 /* read the largest possible block. For this, we perform only one call
796 * to recv() unless the buffer wraps and we exactly fill the first hunk,
797 * in which case we accept to do it once again. A new attempt is made on
798 * EINTR too.
799 */
800 while (try) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200801 ret = SSL_read(conn->xprt_ctx, bi_end(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +0200802 if (conn->flags & CO_FL_ERROR) {
803 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
804 break;
805 }
Emeric Brun46591952012-05-18 15:47:34 +0200806 if (ret > 0) {
807 buf->i += ret;
808 done += ret;
809 if (ret < try)
810 break;
811 count -= ret;
812 try = count;
813 }
814 else if (ret == 0) {
815 goto read0;
816 }
817 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200818 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +0200819 if (ret == SSL_ERROR_WANT_WRITE) {
820 /* handshake is running, and it needs to poll for a write event */
821 conn->flags |= CO_FL_SSL_WAIT_HS;
822 __conn_sock_poll_send(conn);
823 break;
824 }
825 else if (ret == SSL_ERROR_WANT_READ) {
826 /* we need to poll for retry a read later */
827 __conn_data_poll_recv(conn);
828 break;
829 }
830 /* otherwise it's a real error */
831 goto out_error;
832 }
833 }
834 return done;
835
836 read0:
837 conn_sock_read0(conn);
838 return done;
839 out_error:
840 conn->flags |= CO_FL_ERROR;
841 return done;
842}
843
844
845/* Send all pending bytes from buffer <buf> to connection <conn>'s socket.
846 * <flags> may contain MSG_MORE to make the system hold on without sending
847 * data too fast, but this flag is ignored at the moment.
848 * Only one call to send() is performed, unless the buffer wraps, in which case
849 * a second call may be performed. The connection's flags are updated with
850 * whatever special event is detected (error, empty). The caller is responsible
851 * for taking care of those events and avoiding the call if inappropriate. The
852 * function does not call the connection's polling update function, so the caller
853 * is responsible for this.
854 */
855static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags)
856{
857 int ret, try, done;
858
859 done = 0;
860
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200861 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200862 goto out_error;
863
864 if (conn->flags & CO_FL_HANDSHAKE)
865 /* a handshake was requested */
866 return 0;
867
868 /* send the largest possible block. For this we perform only one call
869 * to send() unless the buffer wraps and we exactly fill the first hunk,
870 * in which case we accept to do it once again.
871 */
872 while (buf->o) {
873 try = buf->o;
874 /* outgoing data may wrap at the end */
875 if (buf->data + try > buf->p)
876 try = buf->data + try - buf->p;
877
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200878 ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +0200879 if (conn->flags & CO_FL_ERROR) {
880 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
881 break;
882 }
Emeric Brun46591952012-05-18 15:47:34 +0200883 if (ret > 0) {
884 buf->o -= ret;
885 done += ret;
886
887 if (likely(!buffer_len(buf)))
888 /* optimize data alignment in the buffer */
889 buf->p = buf->data;
890
891 /* if the system buffer is full, don't insist */
892 if (ret < try)
893 break;
894 }
895 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200896 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +0200897 if (ret == SSL_ERROR_WANT_WRITE) {
898 /* we need to poll to retry a write later */
899 __conn_data_poll_send(conn);
900 break;
901 }
902 else if (ret == SSL_ERROR_WANT_READ) {
903 /* handshake is running, and
904 it needs to poll for a read event,
905 write polling must be disabled cause
906 we are sure we can't write anything more
907 before handshake re-performed */
908 conn->flags |= CO_FL_SSL_WAIT_HS;
909 __conn_sock_poll_recv(conn);
910 break;
911 }
912 goto out_error;
913 }
914 }
915 return done;
916
917 out_error:
918 conn->flags |= CO_FL_ERROR;
919 return done;
920}
921
922
923static void ssl_sock_close(struct connection *conn) {
924
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200925 if (conn->xprt_ctx) {
926 SSL_free(conn->xprt_ctx);
927 conn->xprt_ctx = NULL;
Willy Tarreau403edff2012-09-06 11:58:37 +0200928 sslconns--;
Emeric Brun46591952012-05-18 15:47:34 +0200929 }
Emeric Brun46591952012-05-18 15:47:34 +0200930}
931
932/* This function tries to perform a clean shutdown on an SSL connection, and in
933 * any case, flags the connection as reusable if no handshake was in progress.
934 */
935static void ssl_sock_shutw(struct connection *conn, int clean)
936{
937 if (conn->flags & CO_FL_HANDSHAKE)
938 return;
939 /* no handshake was in progress, try a clean ssl shutdown */
940 if (clean)
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200941 SSL_shutdown(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200942
943 /* force flag on ssl to keep session in cache regardless shutdown result */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200944 SSL_set_shutdown(conn->xprt_ctx, SSL_SENT_SHUTDOWN);
Emeric Brun46591952012-05-18 15:47:34 +0200945}
946
Willy Tarreau7875d092012-09-10 08:20:03 +0200947/***** Below are some sample fetching functions for ACL/patterns *****/
948
Emeric Brune64aef12012-09-21 13:15:06 +0200949/* boolean, returns true if client cert was present */
950static int
951smp_fetch_client_crt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
952 const struct arg *args, struct sample *smp)
953{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200954 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +0200955 return 0;
956
957 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
958 smp->flags |= SMP_F_MAY_CHANGE;
959 return 0;
960 }
961
962 smp->flags = 0;
963 smp->type = SMP_T_BOOL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200964 smp->data.uint = SSL_SOCK_ST_FL_VERIFY_DONE & l4->si[0].conn.xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +0200965
966 return 1;
967}
968
969
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200970/* boolean, returns true if transport layer is SSL */
Willy Tarreau7875d092012-09-10 08:20:03 +0200971static int
972smp_fetch_is_ssl(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
973 const struct arg *args, struct sample *smp)
974{
975 smp->type = SMP_T_BOOL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200976 smp->data.uint = (l4->si[0].conn.xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +0200977 return 1;
978}
979
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200980/* boolean, returns true if transport layer is SSL */
Willy Tarreau7875d092012-09-10 08:20:03 +0200981static int
982smp_fetch_has_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
983 const struct arg *args, struct sample *smp)
984{
985#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
986 smp->type = SMP_T_BOOL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200987 smp->data.uint = (l4->si[0].conn.xprt == &ssl_sock) &&
988 l4->si[0].conn.xprt_ctx &&
989 SSL_get_servername(l4->si[0].conn.xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +0200990 return 1;
991#else
992 return 0;
993#endif
994}
995
996static int
997smp_fetch_ssl_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
998 const struct arg *args, struct sample *smp)
999{
1000#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1001 smp->flags = 0;
1002 smp->type = SMP_T_CSTR;
1003
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001004 if (!l4 || !l4->si[0].conn.xprt_ctx || l4->si[0].conn.xprt != &ssl_sock)
Willy Tarreau7875d092012-09-10 08:20:03 +02001005 return 0;
1006
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001007 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 +02001008 if (!smp->data.str.str)
1009 return 0;
1010
Willy Tarreau7875d092012-09-10 08:20:03 +02001011 smp->data.str.len = strlen(smp->data.str.str);
1012 return 1;
1013#else
1014 return 0;
1015#endif
1016}
1017
Emeric Brunf282a812012-09-21 15:27:54 +02001018/* integer, returns the first verify error ID in CA */
1019static int
1020smp_fetch_verify_caerr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1021 const struct arg *args, struct sample *smp)
1022{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001023 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02001024 return 0;
1025
1026 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1027 smp->flags = SMP_F_MAY_CHANGE;
1028 return 0;
1029 }
1030
1031 smp->type = SMP_T_UINT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001032 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CA_ERROR(l4->si[0].conn.xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02001033 smp->flags = 0;
1034
1035 return 1;
1036}
1037
1038/* integer, returns the depth of the first verify error in CA */
1039static int
1040smp_fetch_verify_caerr_depth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1041 const struct arg *args, struct sample *smp)
1042{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001043 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02001044 return 0;
1045
1046 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1047 smp->flags = SMP_F_MAY_CHANGE;
1048 return 0;
1049 }
1050
1051 smp->type = SMP_T_UINT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001052 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CAEDEPTH(l4->si[0].conn.xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02001053 smp->flags = 0;
1054
1055 return 1;
1056}
1057
1058/* integer, returns the depth of the first verify error in CA */
1059static int
1060smp_fetch_verify_crterr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1061 const struct arg *args, struct sample *smp)
1062{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001063 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02001064 return 0;
1065
1066 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1067 smp->flags = SMP_F_MAY_CHANGE;
1068 return 0;
1069 }
1070
1071 smp->type = SMP_T_UINT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001072 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CRTERROR(l4->si[0].conn.xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02001073 smp->flags = 0;
1074
1075 return 1;
1076}
1077
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001078/* integer, returns the verify result */
1079static int
1080smp_fetch_verify_result(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1081 const struct arg *args, struct sample *smp)
1082{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001083 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001084 return 0;
1085
1086 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1087 smp->flags = SMP_F_MAY_CHANGE;
1088 return 0;
1089 }
1090
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001091 if (!l4->si[0].conn.xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001092 return 0;
1093
1094 smp->type = SMP_T_UINT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001095 smp->data.uint = (unsigned int)SSL_get_verify_result(l4->si[0].conn.xprt_ctx);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001096 smp->flags = 0;
1097
1098 return 1;
1099}
1100
Emeric Brund94b3fe2012-09-20 18:23:56 +02001101/* parse the "cafile" bind keyword */
1102static int bind_parse_cafile(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1103{
1104 if (!*args[cur_arg + 1]) {
1105 if (err)
1106 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
1107 return ERR_ALERT | ERR_FATAL;
1108 }
1109
Emeric Brunc8e8d122012-10-02 18:42:10 +02001110 if ((*args[cur_arg + 1] != '/') && global.ca_base) {
1111 conf->cafile = malloc(strlen(global.ca_base) + 1 + strlen(args[cur_arg + 1]) + 1);
1112 if (conf->cafile)
1113 sprintf(conf->cafile, "%s/%s", global.ca_base, args[cur_arg + 1]);
1114 return 0;
1115 }
1116
Emeric Brund94b3fe2012-09-20 18:23:56 +02001117 conf->cafile = strdup(args[cur_arg + 1]);
1118 return 0;
1119}
1120
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001121/* parse the "ciphers" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001122static 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 +02001123{
1124 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001125 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001126 return ERR_ALERT | ERR_FATAL;
1127 }
1128
Willy Tarreau4348fad2012-09-20 16:48:07 +02001129 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001130 return 0;
1131}
1132
1133/* parse the "crt" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001134static 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 +02001135{
Emeric Brunc8e8d122012-10-02 18:42:10 +02001136 char path[PATH_MAX];
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001137 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001138 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001139 return ERR_ALERT | ERR_FATAL;
1140 }
1141
Emeric Brunc8e8d122012-10-02 18:42:10 +02001142 if ((*args[cur_arg + 1] != '/' ) && global.crt_base) {
1143 if ((strlen(global.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > PATH_MAX) {
1144 memprintf(err, "'%s' : path too long", args[cur_arg]);
1145 return ERR_ALERT | ERR_FATAL;
1146 }
1147 sprintf(path, "%s/%s", global.crt_base, args[cur_arg + 1]);
1148 if (ssl_sock_load_cert(path, conf, px, err) > 0)
1149 return ERR_ALERT | ERR_FATAL;
1150
1151 return 0;
1152 }
1153
Willy Tarreau4348fad2012-09-20 16:48:07 +02001154 if (ssl_sock_load_cert(args[cur_arg + 1], conf, px, err) > 0)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001155 return ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02001156
1157 return 0;
1158}
1159
1160/* parse the "crlfile" bind keyword */
1161static int bind_parse_crlfile(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1162{
Emeric Brun051cdab2012-10-02 19:25:50 +02001163#ifndef X509_V_FLAG_CRL_CHECK
1164 if (err)
1165 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
1166 return ERR_ALERT | ERR_FATAL;
1167#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02001168 if (!*args[cur_arg + 1]) {
1169 if (err)
1170 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
1171 return ERR_ALERT | ERR_FATAL;
1172 }
Emeric Brun2b58d042012-09-20 17:10:03 +02001173
Emeric Brunc8e8d122012-10-02 18:42:10 +02001174 if ((*args[cur_arg + 1] != '/') && global.ca_base) {
1175 conf->crlfile = malloc(strlen(global.ca_base) + 1 + strlen(args[cur_arg + 1]) + 1);
1176 if (conf->crlfile)
1177 sprintf(conf->crlfile, "%s/%s", global.ca_base, args[cur_arg + 1]);
1178 return 0;
1179 }
1180
Emeric Brund94b3fe2012-09-20 18:23:56 +02001181 conf->crlfile = strdup(args[cur_arg + 1]);
Emeric Brun2b58d042012-09-20 17:10:03 +02001182 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02001183#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02001184}
1185
1186/* parse the "ecdhe" bind keyword keywords */
1187static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1188{
1189#if OPENSSL_VERSION_NUMBER < 0x0090800fL
1190 if (err)
1191 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
1192 return ERR_ALERT | ERR_FATAL;
1193#elif defined(OPENSSL_NO_ECDH)
1194 if (err)
1195 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
1196 return ERR_ALERT | ERR_FATAL;
1197#else
1198 if (!*args[cur_arg + 1]) {
1199 if (err)
1200 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
1201 return ERR_ALERT | ERR_FATAL;
1202 }
1203
1204 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001205
1206 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02001207#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001208}
1209
Emeric Brun81c00f02012-09-21 14:31:21 +02001210/* parse the "crt_ignerr" and "ca_ignerr" bind keywords */
1211static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1212{
1213 int code;
1214 char *p = args[cur_arg + 1];
1215 unsigned long long *ignerr = &conf->crt_ignerr;
1216
1217 if (!*p) {
1218 if (err)
1219 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
1220 return ERR_ALERT | ERR_FATAL;
1221 }
1222
1223 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
1224 ignerr = &conf->ca_ignerr;
1225
1226 if (strcmp(p, "all") == 0) {
1227 *ignerr = ~0ULL;
1228 return 0;
1229 }
1230
1231 while (p) {
1232 code = atoi(p);
1233 if ((code <= 0) || (code > 63)) {
1234 if (err)
1235 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
1236 args[cur_arg], code, args[cur_arg + 1]);
1237 return ERR_ALERT | ERR_FATAL;
1238 }
1239 *ignerr |= 1ULL << code;
1240 p = strchr(p, ',');
1241 if (p)
1242 p++;
1243 }
1244
Emeric Brun2d0c4822012-10-02 13:45:20 +02001245 return 0;
1246}
1247
1248/* parse the "no-tls-tickets" bind keyword */
1249static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1250{
1251 conf->no_tls_tickets = 1;
Emeric Brun81c00f02012-09-21 14:31:21 +02001252 return 0;
1253}
1254
Emeric Brun2d0c4822012-10-02 13:45:20 +02001255
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001256/* parse the "nosslv3" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001257static int bind_parse_nosslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001258{
Willy Tarreau4348fad2012-09-20 16:48:07 +02001259 conf->nosslv3 = 1;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001260 return 0;
1261}
1262
1263/* parse the "notlsv1" bind keyword */
Emeric Brunc0ff4922012-09-28 19:37:02 +02001264static int bind_parse_notlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1265{
1266 conf->notlsv10 = 1;
1267 return 0;
1268}
1269
1270/* parse the "notlsv11" bind keyword */
1271static int bind_parse_notlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1272{
1273 conf->notlsv11 = 1;
1274 return 0;
1275}
1276
1277/* parse the "notlsv12" bind keyword */
1278static int bind_parse_notlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001279{
Emeric Brunc0ff4922012-09-28 19:37:02 +02001280 conf->notlsv12 = 1;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001281 return 0;
1282}
1283
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001284/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001285static 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 +02001286{
Willy Tarreau81796be2012-09-22 19:11:47 +02001287 struct listener *l;
1288
Willy Tarreau4348fad2012-09-20 16:48:07 +02001289 conf->is_ssl = 1;
Willy Tarreau81796be2012-09-22 19:11:47 +02001290 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001291 l->xprt = &ssl_sock;
Willy Tarreau81796be2012-09-22 19:11:47 +02001292
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001293 return 0;
1294}
1295
Emeric Brund94b3fe2012-09-20 18:23:56 +02001296/* parse the "verify" bind keyword */
1297static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1298{
1299 if (!*args[cur_arg + 1]) {
1300 if (err)
1301 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
1302 return ERR_ALERT | ERR_FATAL;
1303 }
1304
1305 if (strcmp(args[cur_arg + 1], "none") == 0)
1306 conf->verify = SSL_VERIFY_NONE;
1307 else if (strcmp(args[cur_arg + 1], "optional") == 0)
1308 conf->verify = SSL_VERIFY_PEER;
1309 else if (strcmp(args[cur_arg + 1], "required") == 0)
1310 conf->verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1311 else {
1312 if (err)
1313 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
1314 args[cur_arg], args[cur_arg + 1]);
1315 return ERR_ALERT | ERR_FATAL;
1316 }
1317
1318 return 0;
1319}
1320
Willy Tarreau7875d092012-09-10 08:20:03 +02001321/* Note: must not be declared <const> as its list will be overwritten.
1322 * Please take care of keeping this list alphabetically sorted.
1323 */
1324static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Emeric Brunf282a812012-09-21 15:27:54 +02001325 { "client_crt", smp_fetch_client_crt, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
1326 { "is_ssl", smp_fetch_is_ssl, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
1327 { "ssl_has_sni", smp_fetch_has_sni, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
1328 { "ssl_sni", smp_fetch_ssl_sni, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
1329 { "ssl_verify_caerr", smp_fetch_verify_caerr, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
1330 { "ssl_verify_caerr_depth", smp_fetch_verify_caerr_depth, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
1331 { "ssl_verify_crterr", smp_fetch_verify_crterr, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
1332 { "ssl_verify_result", smp_fetch_verify_result, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau7875d092012-09-10 08:20:03 +02001333 { NULL, NULL, 0, 0, 0 },
1334}};
1335
1336/* Note: must not be declared <const> as its list will be overwritten.
1337 * Please take care of keeping this list alphabetically sorted.
1338 */
1339static struct acl_kw_list acl_kws = {{ },{
Emeric Brunf282a812012-09-21 15:27:54 +02001340 { "client_crt", acl_parse_int, smp_fetch_client_crt, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1341 { "is_ssl", acl_parse_int, smp_fetch_is_ssl, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1342 { "ssl_has_sni", acl_parse_int, smp_fetch_has_sni, acl_match_nothing, ACL_USE_L6REQ_PERMANENT, 0 },
1343 { "ssl_sni", acl_parse_str, smp_fetch_ssl_sni, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1344 { "ssl_sni_end", acl_parse_str, smp_fetch_ssl_sni, acl_match_end, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1345 { "ssl_sni_reg", acl_parse_str, smp_fetch_ssl_sni, acl_match_reg, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1346 { "ssl_verify_caerr", acl_parse_int, smp_fetch_verify_caerr, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1347 { "ssl_verify_caerr_depth", acl_parse_int, smp_fetch_verify_caerr_depth, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1348 { "ssl_verify_crterr", acl_parse_int, smp_fetch_verify_crterr, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1349 { "ssl_verify_result", acl_parse_int, smp_fetch_verify_result, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Willy Tarreau7875d092012-09-10 08:20:03 +02001350 { NULL, NULL, NULL, NULL },
1351}};
1352
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001353/* Note: must not be declared <const> as its list will be overwritten.
1354 * Please take care of keeping this list alphabetically sorted, doing so helps
1355 * all code contributors.
1356 * Optional keywords are also declared with a NULL ->parse() function so that
1357 * the config parser can report an appropriate error when a known keyword was
1358 * not enabled.
1359 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001360static struct bind_kw_list bind_kws = { "SSL", { }, {
Emeric Brun2d0c4822012-10-02 13:45:20 +02001361 { "cafile", bind_parse_cafile, 1 }, /* set CAfile to process verify on client cert */
1362 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
1363 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
1364 { "crlfile", bind_parse_crlfile, 1 }, /* set certificat revocation list file use on client cert verify */
1365 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
1366 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
1367 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
1368 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
1369 { "nosslv3", bind_parse_nosslv3, 0 }, /* disable SSLv3 */
1370 { "notlsv10", bind_parse_notlsv10, 0 }, /* disable TLSv10 */
1371 { "notlsv11", bind_parse_notlsv11, 0 }, /* disable TLSv11 */
1372 { "notlsv12", bind_parse_notlsv12, 0 }, /* disable TLSv12 */
Emeric Brun2d0c4822012-10-02 13:45:20 +02001373 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
1374 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001375 { NULL, NULL, 0 },
1376}};
Emeric Brun46591952012-05-18 15:47:34 +02001377
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001378/* transport-layer operations for SSL sockets */
1379struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +02001380 .snd_buf = ssl_sock_from_buf,
1381 .rcv_buf = ssl_sock_to_buf,
1382 .rcv_pipe = NULL,
1383 .snd_pipe = NULL,
1384 .shutr = NULL,
1385 .shutw = ssl_sock_shutw,
1386 .close = ssl_sock_close,
1387 .init = ssl_sock_init,
1388};
1389
1390__attribute__((constructor))
1391static void __ssl_sock_init(void) {
1392 STACK_OF(SSL_COMP)* cm;
1393
1394 SSL_library_init();
1395 cm = SSL_COMP_get_compression_methods();
1396 sk_SSL_COMP_zero(cm);
Willy Tarreau7875d092012-09-10 08:20:03 +02001397 sample_register_fetches(&sample_fetch_keywords);
1398 acl_register_keywords(&acl_kws);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001399 bind_register_keywords(&bind_kws);
Emeric Brun46591952012-05-18 15:47:34 +02001400}
1401
1402/*
1403 * Local variables:
1404 * c-indent-level: 8
1405 * c-basic-offset: 8
1406 * End:
1407 */