blob: 9c60679cb1c3cfe05d76e6f9b7a018223e22215c [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>
Willy Tarreau92faadf2012-10-10 23:04:25 +020068#include <proto/server.h>
Emeric Brun46591952012-05-18 15:47:34 +020069#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020070#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020071#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020072#include <proto/ssl_sock.h>
73#include <proto/task.h>
74
Emeric Brune64aef12012-09-21 13:15:06 +020075#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brunf282a812012-09-21 15:27:54 +020076/* bits 0xFFFF0000 are reserved to store verify errors */
77
78/* Verify errors macros */
79#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
80#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
81#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
82
83#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
84#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
85#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +020086
Willy Tarreau403edff2012-09-06 11:58:37 +020087static int sslconns = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +020088
89void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
90{
91 struct connection *conn = (struct connection *)SSL_get_app_data(ssl);
92 (void)ret; /* shut gcc stupid warning */
93
94 if (where & SSL_CB_HANDSHAKE_START) {
95 /* Disable renegotiation (CVE-2009-3555) */
96 if (conn->flags & CO_FL_CONNECTED)
97 conn->flags |= CO_FL_ERROR;
98 }
Emeric Brunfc0421f2012-09-07 17:30:07 +020099}
100
Emeric Brune64aef12012-09-21 13:15:06 +0200101/* Callback is called for each certificate of the chain during a verify
102 ok is set to 1 if preverify detect no error on current certificate.
103 Returns 0 to break the handshake, 1 otherwise. */
104int ssl_sock_verifycbk(int ok, X509_STORE_CTX *x_store)
105{
106 SSL *ssl;
107 struct connection *conn;
Emeric Brun81c00f02012-09-21 14:31:21 +0200108 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +0200109
110 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
111 conn = (struct connection *)SSL_get_app_data(ssl);
112
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200113 conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +0200114
Emeric Brun81c00f02012-09-21 14:31:21 +0200115 if (ok) /* no errors */
116 return ok;
117
118 depth = X509_STORE_CTX_get_error_depth(x_store);
119 err = X509_STORE_CTX_get_error(x_store);
120
121 /* check if CA error needs to be ignored */
122 if (depth > 0) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200123 if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) {
124 conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
125 conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +0200126 }
127
Emeric Brun81c00f02012-09-21 14:31:21 +0200128 if (target_client(&conn->target)->bind_conf->ca_ignerr & (1ULL << err))
129 return 1;
130
131 return 0;
132 }
133
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200134 if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st))
135 conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +0200136
Emeric Brun81c00f02012-09-21 14:31:21 +0200137 /* check if certificate error needs to be ignored */
138 if (target_client(&conn->target)->bind_conf->crt_ignerr & (1ULL << err))
139 return 1;
140
141 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +0200142}
143
Willy Tarreau6c9a3d52012-10-18 18:57:14 +0200144#ifdef OPENSSL_NPN_NEGOTIATED
145/* This callback is used so that the server advertises the list of
146 * negociable protocols for NPN.
147 */
148static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
149 unsigned int *len, void *arg)
150{
151 struct bind_conf *conf = arg;
152
153 *data = (const unsigned char *)conf->npn_str;
154 *len = conf->npn_len;
155 return SSL_TLSEXT_ERR_OK;
156}
157#endif
158
Emeric Brunfc0421f2012-09-07 17:30:07 +0200159#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
160/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
161 * warning when no match is found, which implies the default (first) cert
162 * will keep being used.
163 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200164static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200165{
166 const char *servername;
167 const char *wildp = NULL;
168 struct ebmb_node *node;
169 int i;
170 (void)al; /* shut gcc stupid warning */
171
172 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
173 if (!servername)
174 return SSL_TLSEXT_ERR_NOACK;
175
176 for (i = 0; i < trashlen; i++) {
177 if (!servername[i])
178 break;
179 trash[i] = tolower(servername[i]);
180 if (!wildp && (trash[i] == '.'))
181 wildp = &trash[i];
182 }
183 trash[i] = 0;
184
185 /* lookup in full qualified names */
186 node = ebst_lookup(&s->sni_ctx, trash);
187 if (!node) {
188 if (!wildp)
189 return SSL_TLSEXT_ERR_ALERT_WARNING;
190
191 /* lookup in full wildcards names */
192 node = ebst_lookup(&s->sni_w_ctx, wildp);
193 if (!node)
194 return SSL_TLSEXT_ERR_ALERT_WARNING;
195 }
196
197 /* switch ctx */
198 SSL_set_SSL_CTX(ssl, container_of(node, struct sni_ctx, name)->ctx);
199 return SSL_TLSEXT_ERR_OK;
200}
201#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
202
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200203#ifndef OPENSSL_NO_DH
204/* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1
205 if an error occured, and 0 if parameter not found. */
206int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file)
207{
208 int ret = -1;
209 BIO *in;
210 DH *dh = NULL;
211
212 in = BIO_new(BIO_s_file());
213 if (in == NULL)
214 goto end;
215
216 if (BIO_read_filename(in, file) <= 0)
217 goto end;
218
219 dh = PEM_read_bio_DHparams(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata);
220 if (dh) {
221 SSL_CTX_set_tmp_dh(ctx, dh);
222 ret = 1;
223 goto end;
224 }
225
226 ret = 0; /* DH params not found */
227end:
228 if (dh)
229 DH_free(dh);
230
231 if (in)
232 BIO_free(in);
233
234 return ret;
235}
236#endif
237
Emeric Brunfc0421f2012-09-07 17:30:07 +0200238/* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if
239 * an early error happens and the caller must call SSL_CTX_free() by itelf.
240 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200241int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200242{
243 BIO *in;
244 X509 *x = NULL, *ca;
245 int i, len, err;
246 int ret = -1;
247 int order = 0;
248 X509_NAME *xname;
249 char *str;
250 struct sni_ctx *sc;
251#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
252 STACK_OF(GENERAL_NAME) *names;
253#endif
254
255 in = BIO_new(BIO_s_file());
256 if (in == NULL)
257 goto end;
258
259 if (BIO_read_filename(in, file) <= 0)
260 goto end;
261
262 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata);
263 if (x == NULL)
264 goto end;
265
266#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
267 names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
268 if (names) {
269 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
270 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
271 if (name->type == GEN_DNS) {
272 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
273 if ((len = strlen(str))) {
274 int j;
275
276 if (*str != '*') {
277 sc = malloc(sizeof(struct sni_ctx) + len + 1);
278 for (j = 0; j < len; j++)
279 sc->name.key[j] = tolower(str[j]);
280 sc->name.key[len] = 0;
281 sc->order = order++;
282 sc->ctx = ctx;
283 ebst_insert(&s->sni_ctx, &sc->name);
284 }
285 else {
286 sc = malloc(sizeof(struct sni_ctx) + len);
287 for (j = 1; j < len; j++)
288 sc->name.key[j-1] = tolower(str[j]);
289 sc->name.key[len-1] = 0;
290 sc->order = order++;
291 sc->ctx = ctx;
292 ebst_insert(&s->sni_w_ctx, &sc->name);
293 }
294 }
295 OPENSSL_free(str);
296 }
297 }
298 }
299 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
300 }
301#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
302
303 xname = X509_get_subject_name(x);
304 i = -1;
305 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
306 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
307 if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) {
308 if ((len = strlen(str))) {
309 int j;
310
311 if (*str != '*') {
312 sc = malloc(sizeof(struct sni_ctx) + len + 1);
313 for (j = 0; j < len; j++)
314 sc->name.key[j] = tolower(str[j]);
315 sc->name.key[len] = 0;
316 sc->order = order++;
317 sc->ctx = ctx;
318 ebst_insert(&s->sni_ctx, &sc->name);
319 }
320 else {
321 sc = malloc(sizeof(struct sni_ctx) + len);
322 for (j = 1; j < len; j++)
323 sc->name.key[j-1] = tolower(str[j]);
324 sc->name.key[len-1] = 0;
325 sc->order = order++;
326 sc->ctx = ctx;
327 ebst_insert(&s->sni_w_ctx, &sc->name);
328 }
329 }
330 OPENSSL_free(str);
331 }
332 }
333
334 ret = 0; /* the caller must not free the SSL_CTX argument anymore */
335 if (!SSL_CTX_use_certificate(ctx, x))
336 goto end;
337
338 if (ctx->extra_certs != NULL) {
339 sk_X509_pop_free(ctx->extra_certs, X509_free);
340 ctx->extra_certs = NULL;
341 }
342
343 while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata))) {
344 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
345 X509_free(ca);
346 goto end;
347 }
348 }
349
350 err = ERR_get_error();
351 if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
352 /* we successfully reached the last cert in the file */
353 ret = 1;
354 }
355 ERR_clear_error();
356
357end:
358 if (x)
359 X509_free(x);
360
361 if (in)
362 BIO_free(in);
363
364 return ret;
365}
366
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200367static 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 +0200368{
369 int ret;
370 SSL_CTX *ctx;
371
372 ctx = SSL_CTX_new(SSLv23_server_method());
373 if (!ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200374 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
375 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200376 return 1;
377 }
378
379 if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200380 memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
381 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200382 SSL_CTX_free(ctx);
383 return 1;
384 }
385
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200386 ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200387 if (ret <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200388 memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
389 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200390 if (ret < 0) /* serious error, must do that ourselves */
391 SSL_CTX_free(ctx);
392 return 1;
393 }
394 /* we must not free the SSL_CTX anymore below, since it's already in
395 * the tree, so it will be discovered and cleaned in time.
396 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200397#ifndef OPENSSL_NO_DH
398 ret = ssl_sock_load_dh_params(ctx, path);
399 if (ret < 0) {
400 if (err)
401 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
402 *err ? *err : "", path);
403 return 1;
404 }
405#endif
406
Emeric Brunfc0421f2012-09-07 17:30:07 +0200407#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200408 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200409 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
410 err && *err ? *err : "");
Emeric Brunfc0421f2012-09-07 17:30:07 +0200411 return 1;
412 }
413#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200414 if (!bind_conf->default_ctx)
415 bind_conf->default_ctx = ctx;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200416
417 return 0;
418}
419
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200420int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200421{
422 struct dirent *de;
423 DIR *dir;
424 struct stat buf;
425 int pathlen = 0;
426 char *end, *fp;
427 int cfgerr = 0;
428
429 if (!(dir = opendir(path)))
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200430 return ssl_sock_load_cert_file(path, bind_conf, curproxy, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200431
432 /* strip trailing slashes, including first one */
433 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
434 *end = 0;
435
436 if (end >= path)
437 pathlen = end + 1 - path;
438 fp = malloc(pathlen + 1 + NAME_MAX + 1);
439
440 while ((de = readdir(dir))) {
441 snprintf(fp, pathlen + 1 + NAME_MAX + 1, "%s/%s", path, de->d_name);
442 if (stat(fp, &buf) != 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200443 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
444 err && *err ? *err : "", fp, strerror(errno));
Emeric Brunfc0421f2012-09-07 17:30:07 +0200445 cfgerr++;
446 continue;
447 }
448 if (!S_ISREG(buf.st_mode))
449 continue;
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200450 cfgerr += ssl_sock_load_cert_file(fp, bind_conf, curproxy, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200451 }
452 free(fp);
453 closedir(dir);
454 return cfgerr;
455}
456
457#ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */
458#define SSL_OP_CIPHER_SERVER_PREFERENCE 0
459#endif
460
461#ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */
462#define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0
463#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200464#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */
465#define SSL_OP_SINGLE_ECDH_USE 0
466#endif
Emeric Brun2d0c4822012-10-02 13:45:20 +0200467#ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */
468#define SSL_OP_NO_TICKET 0
469#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200470#ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */
471#define SSL_OP_NO_COMPRESSION 0
472#endif
Emeric Brunc0ff4922012-09-28 19:37:02 +0200473#ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */
474#define SSL_OP_NO_TLSv1_1 0
475#endif
476#ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */
477#define SSL_OP_NO_TLSv1_2 0
478#endif
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200479#ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */
480#define SSL_OP_SINGLE_DH_USE 0
481#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200482#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */
483#define SSL_OP_SINGLE_ECDH_USE 0
484#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200485#ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */
486#define SSL_MODE_RELEASE_BUFFERS 0
487#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200488int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, SSL_CTX *ctx, struct proxy *curproxy)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200489{
490 int cfgerr = 0;
491 int ssloptions =
492 SSL_OP_ALL | /* all known workarounds for bugs */
493 SSL_OP_NO_SSLv2 |
494 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200495 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +0200496 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +0200497 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
498 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200499 int sslmode =
500 SSL_MODE_ENABLE_PARTIAL_WRITE |
501 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
502 SSL_MODE_RELEASE_BUFFERS;
503
Emeric Brun89675492012-10-05 13:48:26 +0200504 if (bind_conf->ssl_options & BC_SSL_O_NO_SSLV3)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200505 ssloptions |= SSL_OP_NO_SSLv3;
Emeric Brun89675492012-10-05 13:48:26 +0200506 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV10)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200507 ssloptions |= SSL_OP_NO_TLSv1;
Emeric Brun89675492012-10-05 13:48:26 +0200508 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV11)
Emeric Brunc0ff4922012-09-28 19:37:02 +0200509 ssloptions |= SSL_OP_NO_TLSv1_1;
Emeric Brun89675492012-10-05 13:48:26 +0200510 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV12)
Emeric Brunc0ff4922012-09-28 19:37:02 +0200511 ssloptions |= SSL_OP_NO_TLSv1_2;
Emeric Brun89675492012-10-05 13:48:26 +0200512 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
Emeric Brun2d0c4822012-10-02 13:45:20 +0200513 ssloptions |= SSL_OP_NO_TICKET;
Emeric Brun2cb7ae52012-10-05 14:14:21 +0200514 if (bind_conf->ssl_options & BC_SSL_O_USE_SSLV3)
515 SSL_CTX_set_ssl_version(ctx, SSLv3_server_method());
516 if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV10)
517 SSL_CTX_set_ssl_version(ctx, TLSv1_server_method());
518#if SSL_OP_NO_TLSv1_1
519 if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV11)
520 SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method());
521#endif
522#if SSL_OP_NO_TLSv1_2
523 if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV12)
524 SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method());
525#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200526
527 SSL_CTX_set_options(ctx, ssloptions);
528 SSL_CTX_set_mode(ctx, sslmode);
Emeric Brune64aef12012-09-21 13:15:06 +0200529 SSL_CTX_set_verify(ctx, bind_conf->verify ? bind_conf->verify : SSL_VERIFY_NONE, ssl_sock_verifycbk);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200530 if (bind_conf->verify & SSL_VERIFY_PEER) {
Emeric Brunfb510ea2012-10-05 12:00:26 +0200531 if (bind_conf->ca_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200532 /* load CAfile to verify */
Emeric Brunfb510ea2012-10-05 12:00:26 +0200533 if (!SSL_CTX_load_verify_locations(ctx, bind_conf->ca_file, NULL)) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200534 Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
Emeric Brunfb510ea2012-10-05 12:00:26 +0200535 curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200536 cfgerr++;
537 }
538 /* set CA names fo client cert request, function returns void */
Emeric Brunfb510ea2012-10-05 12:00:26 +0200539 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(bind_conf->ca_file));
Emeric Brund94b3fe2012-09-20 18:23:56 +0200540 }
Emeric Brun051cdab2012-10-02 19:25:50 +0200541#ifdef X509_V_FLAG_CRL_CHECK
Emeric Brunfb510ea2012-10-05 12:00:26 +0200542 if (bind_conf->crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200543 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
544
Emeric Brunfb510ea2012-10-05 12:00:26 +0200545 if (!store || !X509_STORE_load_locations(store, bind_conf->crl_file, NULL)) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200546 Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
Emeric Brunfb510ea2012-10-05 12:00:26 +0200547 curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200548 cfgerr++;
549 }
Emeric Brun561e5742012-10-02 15:20:55 +0200550 else {
551 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
552 }
Emeric Brund94b3fe2012-09-20 18:23:56 +0200553 }
Emeric Brun051cdab2012-10-02 19:25:50 +0200554#endif
Emeric Brund94b3fe2012-09-20 18:23:56 +0200555 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200556
557 shared_context_set_cache(ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200558 if (bind_conf->ciphers &&
559 !SSL_CTX_set_cipher_list(ctx, bind_conf->ciphers)) {
Emeric Brunfc0421f2012-09-07 17:30:07 +0200560 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 +0200561 curproxy->id, bind_conf->ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200562 cfgerr++;
563 }
564
565 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +0200566#ifdef OPENSSL_NPN_NEGOTIATED
567 if (bind_conf->npn_str)
568 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, bind_conf);
569#endif
570
Emeric Brunfc0421f2012-09-07 17:30:07 +0200571#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
572 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200573 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200574#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200575#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
576 if (bind_conf->ecdhe) {
577 int i;
578 EC_KEY *ecdh;
579
580 i = OBJ_sn2nid(bind_conf->ecdhe);
581 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
582 Alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
583 curproxy->id, bind_conf->ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
584 cfgerr++;
585 }
586 else {
587 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
588 EC_KEY_free(ecdh);
589 }
590 }
591#endif
592
Emeric Brunfc0421f2012-09-07 17:30:07 +0200593 return cfgerr;
594}
595
Emeric Brun94324a42012-10-11 14:00:19 +0200596/* prepare ssl context from servers options. Returns an error count */
597int ssl_sock_prepare_srv_ctx(struct server *srv, struct proxy *curproxy)
598{
599 int cfgerr = 0;
600 int options =
601 SSL_OP_ALL | /* all known workarounds for bugs */
602 SSL_OP_NO_SSLv2 |
603 SSL_OP_NO_COMPRESSION;
604 int mode =
605 SSL_MODE_ENABLE_PARTIAL_WRITE |
606 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
607 SSL_MODE_RELEASE_BUFFERS;
608
609 /* Initiate SSL context for current server */
610 srv->ssl_ctx.reused_sess = NULL;
611 if (srv->use_ssl)
612 srv->xprt = &ssl_sock;
613 if (srv->check.use_ssl)
614 srv->check.xprt = &ssl_sock;
615
616 srv->ssl_ctx.ctx = SSL_CTX_new(SSLv23_client_method());
617 if (!srv->ssl_ctx.ctx) {
618 Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
619 proxy_type_str(curproxy), curproxy->id,
620 srv->id);
621 cfgerr++;
622 return cfgerr;
623 }
624
625
626 if (srv->ssl_ctx.options & SRV_SSL_O_NO_SSLV3)
627 options |= SSL_OP_NO_SSLv3;
628 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV10)
629 options |= SSL_OP_NO_TLSv1;
630 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV11)
631 options |= SSL_OP_NO_TLSv1_1;
632 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV12)
633 options |= SSL_OP_NO_TLSv1_2;
Emeric Brunf9c5c472012-10-11 15:28:34 +0200634 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
635 options |= SSL_OP_NO_TICKET;
Emeric Brun94324a42012-10-11 14:00:19 +0200636 if (srv->ssl_ctx.options & SRV_SSL_O_USE_SSLV3)
637 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, SSLv3_client_method());
638 if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV10)
639 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_client_method());
640#if SSL_OP_NO_TLSv1_1
641 if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV11)
642 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_1_client_method());
643#endif
644#if SSL_OP_NO_TLSv1_2
645 if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV12)
646 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_2_client_method());
647#endif
648
649 SSL_CTX_set_options(srv->ssl_ctx.ctx, options);
650 SSL_CTX_set_mode(srv->ssl_ctx.ctx, mode);
Emeric Brunef42d922012-10-11 16:11:36 +0200651 SSL_CTX_set_verify(srv->ssl_ctx.ctx, srv->ssl_ctx.verify ? srv->ssl_ctx.verify : SSL_VERIFY_NONE, NULL);
652 if (srv->ssl_ctx.verify & SSL_VERIFY_PEER) {
653 if (srv->ssl_ctx.ca_file) {
654 /* load CAfile to verify */
655 if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) {
656 Alert("Proxy '%s', server '%s' |%s:%d] unable to load CA file '%s'.\n",
657 curproxy->id, srv->id,
658 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
659 cfgerr++;
660 }
661 }
662#ifdef X509_V_FLAG_CRL_CHECK
663 if (srv->ssl_ctx.crl_file) {
664 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
665
666 if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) {
667 Alert("Proxy '%s', server '%s' |%s:%d] unable to configure CRL file '%s'.\n",
668 curproxy->id, srv->id,
669 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
670 cfgerr++;
671 }
672 else {
673 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
674 }
675 }
676#endif
677 }
678
Emeric Brun94324a42012-10-11 14:00:19 +0200679 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF);
680 if (srv->ssl_ctx.ciphers &&
681 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
682 Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
683 curproxy->id, srv->id,
684 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
685 cfgerr++;
686 }
687
688 return cfgerr;
689}
690
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200691/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +0200692 * be NULL, in which case nothing is done. Returns the number of errors
693 * encountered.
694 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200695int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf, struct proxy *px)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200696{
697 struct ebmb_node *node;
698 struct sni_ctx *sni;
699 int err = 0;
700
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200701 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200702 return 0;
703
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200704 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200705 while (node) {
706 sni = ebmb_entry(node, struct sni_ctx, name);
707 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200708 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200709 node = ebmb_next(node);
710 }
711
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200712 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200713 while (node) {
714 sni = ebmb_entry(node, struct sni_ctx, name);
715 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200716 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200717 node = ebmb_next(node);
718 }
719 return err;
720}
721
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200722/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +0200723 * be NULL, in which case nothing is done. The default_ctx is nullified too.
724 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200725void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200726{
727 struct ebmb_node *node, *back;
728 struct sni_ctx *sni;
729
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200730 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200731 return;
732
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200733 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200734 while (node) {
735 sni = ebmb_entry(node, struct sni_ctx, name);
736 back = ebmb_next(node);
737 ebmb_delete(node);
738 if (!sni->order) /* only free the CTX on its first occurrence */
739 SSL_CTX_free(sni->ctx);
740 free(sni);
741 node = back;
742 }
743
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200744 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200745 while (node) {
746 sni = ebmb_entry(node, struct sni_ctx, name);
747 back = ebmb_next(node);
748 ebmb_delete(node);
749 if (!sni->order) /* only free the CTX on its first occurrence */
750 SSL_CTX_free(sni->ctx);
751 free(sni);
752 node = back;
753 }
754
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200755 bind_conf->default_ctx = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +0200756}
757
Emeric Brun46591952012-05-18 15:47:34 +0200758/*
759 * This function is called if SSL * context is not yet allocated. The function
760 * is designed to be called before any other data-layer operation and sets the
761 * handshake flag on the connection. It is safe to call it multiple times.
762 * It returns 0 on success and -1 in error case.
763 */
764static int ssl_sock_init(struct connection *conn)
765{
766 /* already initialized */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200767 if (conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200768 return 0;
769
Willy Tarreau403edff2012-09-06 11:58:37 +0200770 if (global.maxsslconn && sslconns >= global.maxsslconn)
771 return -1;
772
Emeric Brun46591952012-05-18 15:47:34 +0200773 /* If it is in client mode initiate SSL session
774 in connect state otherwise accept state */
775 if (target_srv(&conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +0200776 /* Alloc a new SSL session ctx */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200777 conn->xprt_ctx = SSL_new(target_srv(&conn->target)->ssl_ctx.ctx);
778 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200779 return -1;
780
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200781 SSL_set_connect_state(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200782 if (target_srv(&conn->target)->ssl_ctx.reused_sess)
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200783 SSL_set_session(conn->xprt_ctx, target_srv(&conn->target)->ssl_ctx.reused_sess);
Emeric Brun46591952012-05-18 15:47:34 +0200784
785 /* set fd on SSL session context */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200786 SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +0200787
788 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +0200789 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +0200790
791 sslconns++;
Emeric Brun46591952012-05-18 15:47:34 +0200792 return 0;
793 }
794 else if (target_client(&conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +0200795 /* Alloc a new SSL session ctx */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200796 conn->xprt_ctx = SSL_new(target_client(&conn->target)->bind_conf->default_ctx);
797 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200798 return -1;
799
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200800 SSL_set_accept_state(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200801
802 /* set fd on SSL session context */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200803 SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +0200804
Emeric Brune1f38db2012-09-03 20:36:47 +0200805 /* set connection pointer */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200806 SSL_set_app_data(conn->xprt_ctx, conn);
Emeric Brune1f38db2012-09-03 20:36:47 +0200807
Emeric Brun46591952012-05-18 15:47:34 +0200808 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +0200809 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +0200810
811 sslconns++;
Emeric Brun46591952012-05-18 15:47:34 +0200812 return 0;
813 }
814 /* don't know how to handle such a target */
815 return -1;
816}
817
818
819/* This is the callback which is used when an SSL handshake is pending. It
820 * updates the FD status if it wants some polling before being called again.
821 * It returns 0 if it fails in a fatal way or needs to poll to go further,
822 * otherwise it returns non-zero and removes itself from the connection's
823 * flags (the bit is provided in <flag> by the caller).
824 */
825int ssl_sock_handshake(struct connection *conn, unsigned int flag)
826{
827 int ret;
828
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200829 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200830 goto out_error;
831
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200832 ret = SSL_do_handshake(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200833 if (ret != 1) {
834 /* handshake did not complete, let's find why */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200835 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +0200836
837 if (ret == SSL_ERROR_WANT_WRITE) {
838 /* SSL handshake needs to write, L4 connection may not be ready */
839 __conn_sock_stop_recv(conn);
840 __conn_sock_poll_send(conn);
841 return 0;
842 }
843 else if (ret == SSL_ERROR_WANT_READ) {
844 /* SSL handshake needs to read, L4 connection is ready */
845 if (conn->flags & CO_FL_WAIT_L4_CONN)
846 conn->flags &= ~CO_FL_WAIT_L4_CONN;
847 __conn_sock_stop_send(conn);
848 __conn_sock_poll_recv(conn);
849 return 0;
850 }
Willy Tarreau89230192012-09-28 20:22:13 +0200851 else if (ret == SSL_ERROR_SYSCALL) {
852 /* if errno is null, then connection was successfully established */
853 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
854 conn->flags &= ~CO_FL_WAIT_L4_CONN;
855 goto out_error;
856 }
Emeric Brun46591952012-05-18 15:47:34 +0200857 else {
858 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +0200859 /* Note: OpenSSL may leave unread bytes in the socket's
860 * buffer, causing an RST to be emitted upon close() on
861 * TCP sockets. We first try to drain possibly pending
862 * data to avoid this as much as possible.
863 */
864 ret = recv(conn->t.sock.fd, trash, trashlen, MSG_NOSIGNAL|MSG_DONTWAIT);
Emeric Brun46591952012-05-18 15:47:34 +0200865 goto out_error;
866 }
867 }
868
869 /* Handshake succeeded */
870 if (target_srv(&conn->target)) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200871 if (!SSL_session_reused(conn->xprt_ctx)) {
Emeric Brun46591952012-05-18 15:47:34 +0200872 /* check if session was reused, if not store current session on server for reuse */
873 if (target_srv(&conn->target)->ssl_ctx.reused_sess)
874 SSL_SESSION_free(target_srv(&conn->target)->ssl_ctx.reused_sess);
875
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200876 target_srv(&conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200877 }
878 }
879
880 /* The connection is now established at both layers, it's time to leave */
881 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
882 return 1;
883
884 out_error:
Emeric Brun9fa89732012-10-04 17:09:56 +0200885 /* free resumed session if exists */
886 if (target_srv(&conn->target) && target_srv(&conn->target)->ssl_ctx.reused_sess) {
887 SSL_SESSION_free(target_srv(&conn->target)->ssl_ctx.reused_sess);
888 target_srv(&conn->target)->ssl_ctx.reused_sess = NULL;
889 }
890
Emeric Brun46591952012-05-18 15:47:34 +0200891 /* Fail on all other handshake errors */
892 conn->flags |= CO_FL_ERROR;
893 conn->flags &= ~flag;
894 return 0;
895}
896
897/* Receive up to <count> bytes from connection <conn>'s socket and store them
898 * into buffer <buf>. The caller must ensure that <count> is always smaller
899 * than the buffer's size. Only one call to recv() is performed, unless the
900 * buffer wraps, in which case a second call may be performed. The connection's
901 * flags are updated with whatever special event is detected (error, read0,
902 * empty). The caller is responsible for taking care of those events and
903 * avoiding the call if inappropriate. The function does not call the
904 * connection's polling update function, so the caller is responsible for this.
905 */
906static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
907{
908 int ret, done = 0;
909 int try = count;
910
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200911 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200912 goto out_error;
913
914 if (conn->flags & CO_FL_HANDSHAKE)
915 /* a handshake was requested */
916 return 0;
917
918 /* compute the maximum block size we can read at once. */
919 if (buffer_empty(buf)) {
920 /* let's realign the buffer to optimize I/O */
921 buf->p = buf->data;
922 }
923 else if (buf->data + buf->o < buf->p &&
924 buf->p + buf->i < buf->data + buf->size) {
925 /* remaining space wraps at the end, with a moving limit */
926 if (try > buf->data + buf->size - (buf->p + buf->i))
927 try = buf->data + buf->size - (buf->p + buf->i);
928 }
929
930 /* read the largest possible block. For this, we perform only one call
931 * to recv() unless the buffer wraps and we exactly fill the first hunk,
932 * in which case we accept to do it once again. A new attempt is made on
933 * EINTR too.
934 */
935 while (try) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200936 ret = SSL_read(conn->xprt_ctx, bi_end(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +0200937 if (conn->flags & CO_FL_ERROR) {
938 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
939 break;
940 }
Emeric Brun46591952012-05-18 15:47:34 +0200941 if (ret > 0) {
942 buf->i += ret;
943 done += ret;
944 if (ret < try)
945 break;
946 count -= ret;
947 try = count;
948 }
949 else if (ret == 0) {
950 goto read0;
951 }
952 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200953 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +0200954 if (ret == SSL_ERROR_WANT_WRITE) {
955 /* handshake is running, and it needs to poll for a write event */
956 conn->flags |= CO_FL_SSL_WAIT_HS;
957 __conn_sock_poll_send(conn);
958 break;
959 }
960 else if (ret == SSL_ERROR_WANT_READ) {
961 /* we need to poll for retry a read later */
962 __conn_data_poll_recv(conn);
963 break;
964 }
965 /* otherwise it's a real error */
966 goto out_error;
967 }
968 }
969 return done;
970
971 read0:
972 conn_sock_read0(conn);
973 return done;
974 out_error:
975 conn->flags |= CO_FL_ERROR;
976 return done;
977}
978
979
980/* Send all pending bytes from buffer <buf> to connection <conn>'s socket.
981 * <flags> may contain MSG_MORE to make the system hold on without sending
982 * data too fast, but this flag is ignored at the moment.
983 * Only one call to send() is performed, unless the buffer wraps, in which case
984 * a second call may be performed. The connection's flags are updated with
985 * whatever special event is detected (error, empty). The caller is responsible
986 * for taking care of those events and avoiding the call if inappropriate. The
987 * function does not call the connection's polling update function, so the caller
988 * is responsible for this.
989 */
990static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags)
991{
992 int ret, try, done;
993
994 done = 0;
995
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200996 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200997 goto out_error;
998
999 if (conn->flags & CO_FL_HANDSHAKE)
1000 /* a handshake was requested */
1001 return 0;
1002
1003 /* send the largest possible block. For this we perform only one call
1004 * to send() unless the buffer wraps and we exactly fill the first hunk,
1005 * in which case we accept to do it once again.
1006 */
1007 while (buf->o) {
1008 try = buf->o;
1009 /* outgoing data may wrap at the end */
1010 if (buf->data + try > buf->p)
1011 try = buf->data + try - buf->p;
1012
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001013 ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +02001014 if (conn->flags & CO_FL_ERROR) {
1015 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
1016 break;
1017 }
Emeric Brun46591952012-05-18 15:47:34 +02001018 if (ret > 0) {
1019 buf->o -= ret;
1020 done += ret;
1021
1022 if (likely(!buffer_len(buf)))
1023 /* optimize data alignment in the buffer */
1024 buf->p = buf->data;
1025
1026 /* if the system buffer is full, don't insist */
1027 if (ret < try)
1028 break;
1029 }
1030 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001031 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +02001032 if (ret == SSL_ERROR_WANT_WRITE) {
1033 /* we need to poll to retry a write later */
1034 __conn_data_poll_send(conn);
1035 break;
1036 }
1037 else if (ret == SSL_ERROR_WANT_READ) {
1038 /* handshake is running, and
1039 it needs to poll for a read event,
1040 write polling must be disabled cause
1041 we are sure we can't write anything more
1042 before handshake re-performed */
1043 conn->flags |= CO_FL_SSL_WAIT_HS;
1044 __conn_sock_poll_recv(conn);
1045 break;
1046 }
1047 goto out_error;
1048 }
1049 }
1050 return done;
1051
1052 out_error:
1053 conn->flags |= CO_FL_ERROR;
1054 return done;
1055}
1056
1057
1058static void ssl_sock_close(struct connection *conn) {
1059
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001060 if (conn->xprt_ctx) {
1061 SSL_free(conn->xprt_ctx);
1062 conn->xprt_ctx = NULL;
Willy Tarreau403edff2012-09-06 11:58:37 +02001063 sslconns--;
Emeric Brun46591952012-05-18 15:47:34 +02001064 }
Emeric Brun46591952012-05-18 15:47:34 +02001065}
1066
1067/* This function tries to perform a clean shutdown on an SSL connection, and in
1068 * any case, flags the connection as reusable if no handshake was in progress.
1069 */
1070static void ssl_sock_shutw(struct connection *conn, int clean)
1071{
1072 if (conn->flags & CO_FL_HANDSHAKE)
1073 return;
1074 /* no handshake was in progress, try a clean ssl shutdown */
1075 if (clean)
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001076 SSL_shutdown(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +02001077
1078 /* force flag on ssl to keep session in cache regardless shutdown result */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001079 SSL_set_shutdown(conn->xprt_ctx, SSL_SENT_SHUTDOWN);
Emeric Brun46591952012-05-18 15:47:34 +02001080}
1081
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001082/* used for logging, may be changed for a sample fetch later */
1083const char *ssl_sock_get_cipher_name(struct connection *conn)
1084{
1085 if (!conn->xprt && !conn->xprt_ctx)
1086 return NULL;
1087 return SSL_get_cipher_name(conn->xprt_ctx);
1088}
1089
1090/* used for logging, may be changed for a sample fetch later */
1091const char *ssl_sock_get_proto_version(struct connection *conn)
1092{
1093 if (!conn->xprt && !conn->xprt_ctx)
1094 return NULL;
1095 return SSL_get_version(conn->xprt_ctx);
1096}
1097
Willy Tarreau7875d092012-09-10 08:20:03 +02001098/***** Below are some sample fetching functions for ACL/patterns *****/
1099
Emeric Brune64aef12012-09-21 13:15:06 +02001100/* boolean, returns true if client cert was present */
1101static int
1102smp_fetch_client_crt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1103 const struct arg *args, struct sample *smp)
1104{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001105 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02001106 return 0;
1107
1108 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1109 smp->flags |= SMP_F_MAY_CHANGE;
1110 return 0;
1111 }
1112
1113 smp->flags = 0;
1114 smp->type = SMP_T_BOOL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001115 smp->data.uint = SSL_SOCK_ST_FL_VERIFY_DONE & l4->si[0].conn.xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001116
1117 return 1;
1118}
1119
1120
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001121/* boolean, returns true if transport layer is SSL */
Willy Tarreau7875d092012-09-10 08:20:03 +02001122static int
1123smp_fetch_is_ssl(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1124 const struct arg *args, struct sample *smp)
1125{
1126 smp->type = SMP_T_BOOL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001127 smp->data.uint = (l4->si[0].conn.xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02001128 return 1;
1129}
1130
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001131/* boolean, returns true if transport layer is SSL */
Willy Tarreau7875d092012-09-10 08:20:03 +02001132static int
1133smp_fetch_has_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1134 const struct arg *args, struct sample *smp)
1135{
1136#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1137 smp->type = SMP_T_BOOL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001138 smp->data.uint = (l4->si[0].conn.xprt == &ssl_sock) &&
1139 l4->si[0].conn.xprt_ctx &&
1140 SSL_get_servername(l4->si[0].conn.xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02001141 return 1;
1142#else
1143 return 0;
1144#endif
1145}
1146
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001147#ifdef OPENSSL_NPN_NEGOTIATED
Willy Tarreau7875d092012-09-10 08:20:03 +02001148static int
Willy Tarreaua33c6542012-10-15 13:19:06 +02001149smp_fetch_ssl_npn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1150 const struct arg *args, struct sample *smp)
1151{
Willy Tarreaua33c6542012-10-15 13:19:06 +02001152 smp->flags = 0;
1153 smp->type = SMP_T_CSTR;
1154
1155 if (!l4 || !l4->si[0].conn.xprt_ctx || l4->si[0].conn.xprt != &ssl_sock)
1156 return 0;
1157
1158 smp->data.str.str = NULL;
1159 SSL_get0_next_proto_negotiated(l4->si[0].conn.xprt_ctx,
1160 (const unsigned char **)&smp->data.str.str, (unsigned *)&smp->data.str.len);
1161
1162 if (!smp->data.str.str)
1163 return 0;
1164
1165 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02001166}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001167#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02001168
1169static int
Willy Tarreau7875d092012-09-10 08:20:03 +02001170smp_fetch_ssl_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1171 const struct arg *args, struct sample *smp)
1172{
1173#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1174 smp->flags = 0;
1175 smp->type = SMP_T_CSTR;
1176
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001177 if (!l4 || !l4->si[0].conn.xprt_ctx || l4->si[0].conn.xprt != &ssl_sock)
Willy Tarreau7875d092012-09-10 08:20:03 +02001178 return 0;
1179
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001180 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 +02001181 if (!smp->data.str.str)
1182 return 0;
1183
Willy Tarreau7875d092012-09-10 08:20:03 +02001184 smp->data.str.len = strlen(smp->data.str.str);
1185 return 1;
1186#else
1187 return 0;
1188#endif
1189}
1190
Emeric Brunf282a812012-09-21 15:27:54 +02001191/* integer, returns the first verify error ID in CA */
1192static int
1193smp_fetch_verify_caerr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1194 const struct arg *args, struct sample *smp)
1195{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001196 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02001197 return 0;
1198
1199 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1200 smp->flags = SMP_F_MAY_CHANGE;
1201 return 0;
1202 }
1203
1204 smp->type = SMP_T_UINT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001205 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CA_ERROR(l4->si[0].conn.xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02001206 smp->flags = 0;
1207
1208 return 1;
1209}
1210
1211/* integer, returns the depth of the first verify error in CA */
1212static int
1213smp_fetch_verify_caerr_depth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1214 const struct arg *args, struct sample *smp)
1215{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001216 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02001217 return 0;
1218
1219 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1220 smp->flags = SMP_F_MAY_CHANGE;
1221 return 0;
1222 }
1223
1224 smp->type = SMP_T_UINT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001225 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CAEDEPTH(l4->si[0].conn.xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02001226 smp->flags = 0;
1227
1228 return 1;
1229}
1230
1231/* integer, returns the depth of the first verify error in CA */
1232static int
1233smp_fetch_verify_crterr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1234 const struct arg *args, struct sample *smp)
1235{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001236 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02001237 return 0;
1238
1239 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1240 smp->flags = SMP_F_MAY_CHANGE;
1241 return 0;
1242 }
1243
1244 smp->type = SMP_T_UINT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001245 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CRTERROR(l4->si[0].conn.xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02001246 smp->flags = 0;
1247
1248 return 1;
1249}
1250
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001251/* integer, returns the verify result */
1252static int
1253smp_fetch_verify_result(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1254 const struct arg *args, struct sample *smp)
1255{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001256 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001257 return 0;
1258
1259 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1260 smp->flags = SMP_F_MAY_CHANGE;
1261 return 0;
1262 }
1263
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001264 if (!l4->si[0].conn.xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001265 return 0;
1266
1267 smp->type = SMP_T_UINT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001268 smp->data.uint = (unsigned int)SSL_get_verify_result(l4->si[0].conn.xprt_ctx);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001269 smp->flags = 0;
1270
1271 return 1;
1272}
1273
Emeric Brunfb510ea2012-10-05 12:00:26 +02001274/* parse the "ca-file" bind keyword */
1275static 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 +02001276{
1277 if (!*args[cur_arg + 1]) {
1278 if (err)
1279 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
1280 return ERR_ALERT | ERR_FATAL;
1281 }
1282
Emeric Brunef42d922012-10-11 16:11:36 +02001283 if ((*args[cur_arg + 1] != '/') && global.ca_base)
1284 memprintf(&conf->ca_file, "%s/%s", global.ca_base, args[cur_arg + 1]);
1285 else
1286 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02001287
Emeric Brund94b3fe2012-09-20 18:23:56 +02001288 return 0;
1289}
1290
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001291/* parse the "ciphers" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001292static 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 +02001293{
1294 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001295 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001296 return ERR_ALERT | ERR_FATAL;
1297 }
1298
Emeric Brun76d88952012-10-05 15:47:31 +02001299 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02001300 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001301 return 0;
1302}
1303
1304/* parse the "crt" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001305static 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 +02001306{
Emeric Brunc8e8d122012-10-02 18:42:10 +02001307 char path[PATH_MAX];
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001308 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001309 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001310 return ERR_ALERT | ERR_FATAL;
1311 }
1312
Emeric Brunc8e8d122012-10-02 18:42:10 +02001313 if ((*args[cur_arg + 1] != '/' ) && global.crt_base) {
1314 if ((strlen(global.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > PATH_MAX) {
1315 memprintf(err, "'%s' : path too long", args[cur_arg]);
1316 return ERR_ALERT | ERR_FATAL;
1317 }
1318 sprintf(path, "%s/%s", global.crt_base, args[cur_arg + 1]);
1319 if (ssl_sock_load_cert(path, conf, px, err) > 0)
1320 return ERR_ALERT | ERR_FATAL;
1321
1322 return 0;
1323 }
1324
Willy Tarreau4348fad2012-09-20 16:48:07 +02001325 if (ssl_sock_load_cert(args[cur_arg + 1], conf, px, err) > 0)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001326 return ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02001327
1328 return 0;
1329}
1330
Emeric Brunfb510ea2012-10-05 12:00:26 +02001331/* parse the "crl-file" bind keyword */
1332static 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 +02001333{
Emeric Brun051cdab2012-10-02 19:25:50 +02001334#ifndef X509_V_FLAG_CRL_CHECK
1335 if (err)
1336 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
1337 return ERR_ALERT | ERR_FATAL;
1338#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02001339 if (!*args[cur_arg + 1]) {
1340 if (err)
1341 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
1342 return ERR_ALERT | ERR_FATAL;
1343 }
Emeric Brun2b58d042012-09-20 17:10:03 +02001344
Emeric Brunef42d922012-10-11 16:11:36 +02001345 if ((*args[cur_arg + 1] != '/') && global.ca_base)
1346 memprintf(&conf->crl_file, "%s/%s", global.ca_base, args[cur_arg + 1]);
1347 else
1348 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02001349
Emeric Brun2b58d042012-09-20 17:10:03 +02001350 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02001351#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02001352}
1353
1354/* parse the "ecdhe" bind keyword keywords */
1355static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1356{
1357#if OPENSSL_VERSION_NUMBER < 0x0090800fL
1358 if (err)
1359 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
1360 return ERR_ALERT | ERR_FATAL;
1361#elif defined(OPENSSL_NO_ECDH)
1362 if (err)
1363 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
1364 return ERR_ALERT | ERR_FATAL;
1365#else
1366 if (!*args[cur_arg + 1]) {
1367 if (err)
1368 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
1369 return ERR_ALERT | ERR_FATAL;
1370 }
1371
1372 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001373
1374 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02001375#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001376}
1377
Emeric Brun81c00f02012-09-21 14:31:21 +02001378/* parse the "crt_ignerr" and "ca_ignerr" bind keywords */
1379static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1380{
1381 int code;
1382 char *p = args[cur_arg + 1];
1383 unsigned long long *ignerr = &conf->crt_ignerr;
1384
1385 if (!*p) {
1386 if (err)
1387 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
1388 return ERR_ALERT | ERR_FATAL;
1389 }
1390
1391 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
1392 ignerr = &conf->ca_ignerr;
1393
1394 if (strcmp(p, "all") == 0) {
1395 *ignerr = ~0ULL;
1396 return 0;
1397 }
1398
1399 while (p) {
1400 code = atoi(p);
1401 if ((code <= 0) || (code > 63)) {
1402 if (err)
1403 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
1404 args[cur_arg], code, args[cur_arg + 1]);
1405 return ERR_ALERT | ERR_FATAL;
1406 }
1407 *ignerr |= 1ULL << code;
1408 p = strchr(p, ',');
1409 if (p)
1410 p++;
1411 }
1412
Emeric Brun2cb7ae52012-10-05 14:14:21 +02001413 return 0;
1414}
1415
1416/* parse the "force-sslv3" bind keyword */
1417static int bind_parse_force_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1418{
1419 conf->ssl_options |= BC_SSL_O_USE_SSLV3;
1420 return 0;
1421}
1422
1423/* parse the "force-tlsv10" bind keyword */
1424static int bind_parse_force_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1425{
1426 conf->ssl_options |= BC_SSL_O_USE_TLSV10;
Emeric Brun2d0c4822012-10-02 13:45:20 +02001427 return 0;
1428}
1429
Emeric Brun2cb7ae52012-10-05 14:14:21 +02001430/* parse the "force-tlsv11" bind keyword */
1431static int bind_parse_force_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1432{
1433#if SSL_OP_NO_TLSv1_1
1434 conf->ssl_options |= BC_SSL_O_USE_TLSV11;
1435 return 0;
1436#else
1437 if (err)
1438 memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[cur_arg]);
1439 return ERR_ALERT | ERR_FATAL;
1440#endif
1441}
1442
1443/* parse the "force-tlsv12" bind keyword */
1444static int bind_parse_force_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1445{
1446#if SSL_OP_NO_TLSv1_2
1447 conf->ssl_options |= BC_SSL_O_USE_TLSV12;
1448 return 0;
1449#else
1450 if (err)
1451 memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[cur_arg]);
1452 return ERR_ALERT | ERR_FATAL;
1453#endif
1454}
1455
1456
Emeric Brun2d0c4822012-10-02 13:45:20 +02001457/* parse the "no-tls-tickets" bind keyword */
1458static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1459{
Emeric Brun89675492012-10-05 13:48:26 +02001460 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02001461 return 0;
1462}
1463
Emeric Brun2d0c4822012-10-02 13:45:20 +02001464
Emeric Brun9b3009b2012-10-05 11:55:06 +02001465/* parse the "no-sslv3" bind keyword */
1466static 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 +02001467{
Emeric Brun89675492012-10-05 13:48:26 +02001468 conf->ssl_options |= BC_SSL_O_NO_SSLV3;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001469 return 0;
1470}
1471
Emeric Brun9b3009b2012-10-05 11:55:06 +02001472/* parse the "no-tlsv10" bind keyword */
1473static 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 +02001474{
Emeric Brun89675492012-10-05 13:48:26 +02001475 conf->ssl_options |= BC_SSL_O_NO_TLSV10;
Emeric Brunc0ff4922012-09-28 19:37:02 +02001476 return 0;
1477}
1478
Emeric Brun9b3009b2012-10-05 11:55:06 +02001479/* parse the "no-tlsv11" bind keyword */
1480static 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 +02001481{
Emeric Brun89675492012-10-05 13:48:26 +02001482 conf->ssl_options |= BC_SSL_O_NO_TLSV11;
Emeric Brunc0ff4922012-09-28 19:37:02 +02001483 return 0;
1484}
1485
Emeric Brun9b3009b2012-10-05 11:55:06 +02001486/* parse the "no-tlsv12" bind keyword */
1487static 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 +02001488{
Emeric Brun89675492012-10-05 13:48:26 +02001489 conf->ssl_options |= BC_SSL_O_NO_TLSV12;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001490 return 0;
1491}
1492
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001493/* parse the "npn" bind keyword */
1494static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1495{
1496#ifdef OPENSSL_NPN_NEGOTIATED
1497 char *p1, *p2;
1498
1499 if (!*args[cur_arg + 1]) {
1500 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
1501 return ERR_ALERT | ERR_FATAL;
1502 }
1503
1504 free(conf->npn_str);
1505
1506 /* the NPN string is built as a suite of (<len> <name>)* */
1507 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
1508 conf->npn_str = calloc(1, conf->npn_len);
1509 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
1510
1511 /* replace commas with the name length */
1512 p1 = conf->npn_str;
1513 p2 = p1 + 1;
1514 while (1) {
1515 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
1516 if (!p2)
1517 p2 = p1 + 1 + strlen(p1 + 1);
1518
1519 if (p2 - (p1 + 1) > 255) {
1520 *p2 = '\0';
1521 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
1522 return ERR_ALERT | ERR_FATAL;
1523 }
1524
1525 *p1 = p2 - (p1 + 1);
1526 p1 = p2;
1527
1528 if (!*p2)
1529 break;
1530
1531 *(p2++) = '\0';
1532 }
1533 return 0;
1534#else
1535 if (err)
1536 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
1537 return ERR_ALERT | ERR_FATAL;
1538#endif
1539}
1540
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001541/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001542static 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 +02001543{
Willy Tarreau81796be2012-09-22 19:11:47 +02001544 struct listener *l;
1545
Willy Tarreau4348fad2012-09-20 16:48:07 +02001546 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02001547
1548 if (global.listen_default_ciphers && !conf->ciphers)
1549 conf->ciphers = strdup(global.listen_default_ciphers);
1550
Willy Tarreau81796be2012-09-22 19:11:47 +02001551 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001552 l->xprt = &ssl_sock;
Willy Tarreau81796be2012-09-22 19:11:47 +02001553
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001554 return 0;
1555}
1556
Emeric Brund94b3fe2012-09-20 18:23:56 +02001557/* parse the "verify" bind keyword */
1558static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1559{
1560 if (!*args[cur_arg + 1]) {
1561 if (err)
1562 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
1563 return ERR_ALERT | ERR_FATAL;
1564 }
1565
1566 if (strcmp(args[cur_arg + 1], "none") == 0)
1567 conf->verify = SSL_VERIFY_NONE;
1568 else if (strcmp(args[cur_arg + 1], "optional") == 0)
1569 conf->verify = SSL_VERIFY_PEER;
1570 else if (strcmp(args[cur_arg + 1], "required") == 0)
1571 conf->verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1572 else {
1573 if (err)
1574 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
1575 args[cur_arg], args[cur_arg + 1]);
1576 return ERR_ALERT | ERR_FATAL;
1577 }
1578
1579 return 0;
1580}
1581
Willy Tarreau92faadf2012-10-10 23:04:25 +02001582/************** "server" keywords ****************/
1583
Emeric Brunef42d922012-10-11 16:11:36 +02001584/* parse the "ca-file" server keyword */
1585static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1586{
1587 if (!*args[*cur_arg + 1]) {
1588 if (err)
1589 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
1590 return ERR_ALERT | ERR_FATAL;
1591 }
1592
1593 if ((*args[*cur_arg + 1] != '/') && global.ca_base)
1594 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global.ca_base, args[*cur_arg + 1]);
1595 else
1596 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
1597
1598 return 0;
1599}
1600
Willy Tarreau92faadf2012-10-10 23:04:25 +02001601/* parse the "check-ssl" server keyword */
1602static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1603{
1604 newsrv->check.use_ssl = 1;
1605 if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
1606 newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers);
1607 return 0;
1608}
1609
1610/* parse the "ciphers" server keyword */
1611static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1612{
1613 if (!*args[*cur_arg + 1]) {
1614 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
1615 return ERR_ALERT | ERR_FATAL;
1616 }
1617
1618 free(newsrv->ssl_ctx.ciphers);
1619 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
1620 return 0;
1621}
1622
Emeric Brunef42d922012-10-11 16:11:36 +02001623/* parse the "crl-file" server keyword */
1624static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1625{
1626#ifndef X509_V_FLAG_CRL_CHECK
1627 if (err)
1628 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
1629 return ERR_ALERT | ERR_FATAL;
1630#else
1631 if (!*args[*cur_arg + 1]) {
1632 if (err)
1633 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
1634 return ERR_ALERT | ERR_FATAL;
1635 }
1636
1637 if ((*args[*cur_arg + 1] != '/') && global.ca_base)
1638 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global.ca_base, args[*cur_arg + 1]);
1639 else
1640 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
1641
1642 return 0;
1643#endif
1644}
1645
1646
Willy Tarreau92faadf2012-10-10 23:04:25 +02001647/* parse the "force-sslv3" server keyword */
1648static int srv_parse_force_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1649{
1650 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_SSLV3;
1651 return 0;
1652}
1653
1654/* parse the "force-tlsv10" server keyword */
1655static int srv_parse_force_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1656{
1657 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV10;
1658 return 0;
1659}
1660
1661/* parse the "force-tlsv11" server keyword */
1662static int srv_parse_force_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1663{
1664#if SSL_OP_NO_TLSv1_1
1665 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV11;
1666 return 0;
1667#else
1668 if (err)
1669 memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[*cur_arg]);
1670 return ERR_ALERT | ERR_FATAL;
1671#endif
1672}
1673
1674/* parse the "force-tlsv12" server keyword */
1675static int srv_parse_force_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1676{
1677#if SSL_OP_NO_TLSv1_2
1678 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV12;
1679 return 0;
1680#else
1681 if (err)
1682 memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[*cur_arg]);
1683 return ERR_ALERT | ERR_FATAL;
1684#endif
1685}
1686
1687/* parse the "no-sslv3" server keyword */
1688static int srv_parse_no_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1689{
1690 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_SSLV3;
1691 return 0;
1692}
1693
1694/* parse the "no-tlsv10" server keyword */
1695static int srv_parse_no_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1696{
1697 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV10;
1698 return 0;
1699}
1700
1701/* parse the "no-tlsv11" server keyword */
1702static int srv_parse_no_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1703{
1704 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV11;
1705 return 0;
1706}
1707
1708/* parse the "no-tlsv12" server keyword */
1709static int srv_parse_no_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1710{
1711 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV12;
1712 return 0;
1713}
1714
Emeric Brunf9c5c472012-10-11 15:28:34 +02001715/* parse the "no-tls-tickets" server keyword */
1716static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1717{
1718 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
1719 return 0;
1720}
1721
Willy Tarreau92faadf2012-10-10 23:04:25 +02001722/* parse the "ssl" server keyword */
1723static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1724{
1725 newsrv->use_ssl = 1;
1726 if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
1727 newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers);
1728 return 0;
1729}
1730
Emeric Brunef42d922012-10-11 16:11:36 +02001731/* parse the "verify" server keyword */
1732static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1733{
1734 if (!*args[*cur_arg + 1]) {
1735 if (err)
1736 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
1737 return ERR_ALERT | ERR_FATAL;
1738 }
1739
1740 if (strcmp(args[*cur_arg + 1], "none") == 0)
1741 newsrv->ssl_ctx.verify = SSL_VERIFY_NONE;
1742 else if (strcmp(args[*cur_arg + 1], "required") == 0)
1743 newsrv->ssl_ctx.verify = SSL_VERIFY_PEER;
1744 else {
1745 if (err)
1746 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
1747 args[*cur_arg], args[*cur_arg + 1]);
1748 return ERR_ALERT | ERR_FATAL;
1749 }
1750
1751 return 0;
1752}
1753
Willy Tarreau7875d092012-09-10 08:20:03 +02001754/* Note: must not be declared <const> as its list will be overwritten.
1755 * Please take care of keeping this list alphabetically sorted.
1756 */
1757static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Emeric Brunf282a812012-09-21 15:27:54 +02001758 { "client_crt", smp_fetch_client_crt, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
1759 { "is_ssl", smp_fetch_is_ssl, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
1760 { "ssl_has_sni", smp_fetch_has_sni, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreaua33c6542012-10-15 13:19:06 +02001761#ifdef OPENSSL_NPN_NEGOTIATED
1762 { "ssl_npn", smp_fetch_ssl_npn, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
1763#endif
Emeric Brunf282a812012-09-21 15:27:54 +02001764 { "ssl_sni", smp_fetch_ssl_sni, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
1765 { "ssl_verify_caerr", smp_fetch_verify_caerr, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
1766 { "ssl_verify_caerr_depth", smp_fetch_verify_caerr_depth, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
1767 { "ssl_verify_crterr", smp_fetch_verify_crterr, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
1768 { "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 +02001769 { NULL, NULL, 0, 0, 0 },
1770}};
1771
1772/* Note: must not be declared <const> as its list will be overwritten.
1773 * Please take care of keeping this list alphabetically sorted.
1774 */
1775static struct acl_kw_list acl_kws = {{ },{
Emeric Brunf282a812012-09-21 15:27:54 +02001776 { "client_crt", acl_parse_int, smp_fetch_client_crt, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1777 { "is_ssl", acl_parse_int, smp_fetch_is_ssl, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1778 { "ssl_has_sni", acl_parse_int, smp_fetch_has_sni, acl_match_nothing, ACL_USE_L6REQ_PERMANENT, 0 },
Willy Tarreaua33c6542012-10-15 13:19:06 +02001779#ifdef OPENSSL_NPN_NEGOTIATED
1780 { "ssl_npn", acl_parse_str, smp_fetch_ssl_npn, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1781#endif
Emeric Brunf282a812012-09-21 15:27:54 +02001782 { "ssl_sni", acl_parse_str, smp_fetch_ssl_sni, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Willy Tarreau8c866a32012-10-19 14:34:30 +02001783 { "ssl_sni_end", acl_parse_str, smp_fetch_ssl_sni, acl_match_end, ACL_USE_L6REQ_PERMANENT, 0 },
1784 { "ssl_sni_reg", acl_parse_reg, smp_fetch_ssl_sni, acl_match_reg, ACL_USE_L6REQ_PERMANENT, 0 },
Emeric Brunf282a812012-09-21 15:27:54 +02001785 { "ssl_verify_caerr", acl_parse_int, smp_fetch_verify_caerr, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1786 { "ssl_verify_caerr_depth", acl_parse_int, smp_fetch_verify_caerr_depth, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1787 { "ssl_verify_crterr", acl_parse_int, smp_fetch_verify_crterr, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1788 { "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 +02001789 { NULL, NULL, NULL, NULL },
1790}};
1791
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001792/* Note: must not be declared <const> as its list will be overwritten.
1793 * Please take care of keeping this list alphabetically sorted, doing so helps
1794 * all code contributors.
1795 * Optional keywords are also declared with a NULL ->parse() function so that
1796 * the config parser can report an appropriate error when a known keyword was
1797 * not enabled.
1798 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001799static struct bind_kw_list bind_kws = { "SSL", { }, {
Emeric Brunfb510ea2012-10-05 12:00:26 +02001800 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
Emeric Brun2d0c4822012-10-02 13:45:20 +02001801 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
1802 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emeric Brunfb510ea2012-10-05 12:00:26 +02001803 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
Emeric Brun2d0c4822012-10-02 13:45:20 +02001804 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
1805 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
1806 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emeric Brun2cb7ae52012-10-05 14:14:21 +02001807 { "force-sslv3", bind_parse_force_sslv3, 0 }, /* force SSLv3 */
1808 { "force-tlsv10", bind_parse_force_tlsv10, 0 }, /* force TLSv10 */
1809 { "force-tlsv11", bind_parse_force_tlsv11, 0 }, /* force TLSv11 */
1810 { "force-tlsv12", bind_parse_force_tlsv12, 0 }, /* force TLSv12 */
Emeric Brun9b3009b2012-10-05 11:55:06 +02001811 { "no-sslv3", bind_parse_no_sslv3, 0 }, /* disable SSLv3 */
1812 { "no-tlsv10", bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */
1813 { "no-tlsv11", bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */
1814 { "no-tlsv12", bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */
Emeric Brun2d0c4822012-10-02 13:45:20 +02001815 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
Emeric Brun2d0c4822012-10-02 13:45:20 +02001816 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
1817 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001818 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001819 { NULL, NULL, 0 },
1820}};
Emeric Brun46591952012-05-18 15:47:34 +02001821
Willy Tarreau92faadf2012-10-10 23:04:25 +02001822/* Note: must not be declared <const> as its list will be overwritten.
1823 * Please take care of keeping this list alphabetically sorted, doing so helps
1824 * all code contributors.
1825 * Optional keywords are also declared with a NULL ->parse() function so that
1826 * the config parser can report an appropriate error when a known keyword was
1827 * not enabled.
1828 */
1829static struct srv_kw_list srv_kws = { "SSL", { }, {
Emeric Brunef42d922012-10-11 16:11:36 +02001830 { "ca-file", srv_parse_ca_file, 1, 0 }, /* set CAfile to process verify server cert */
Emeric Brunecc91fe2012-10-11 15:05:10 +02001831 { "check-ssl", srv_parse_check_ssl, 0, 0 }, /* enable SSL for health checks */
1832 { "ciphers", srv_parse_ciphers, 1, 0 }, /* select the cipher suite */
Emeric Brunef42d922012-10-11 16:11:36 +02001833 { "crl-file", srv_parse_crl_file, 1, 0 }, /* set certificate revocation list file use on server cert verify */
Emeric Brunecc91fe2012-10-11 15:05:10 +02001834 { "force-sslv3", srv_parse_force_sslv3, 0, 0 }, /* force SSLv3 */
1835 { "force-tlsv10", srv_parse_force_tlsv10, 0, 0 }, /* force TLSv10 */
1836 { "force-tlsv11", srv_parse_force_tlsv11, 0, 0 }, /* force TLSv11 */
1837 { "force-tlsv12", srv_parse_force_tlsv12, 0, 0 }, /* force TLSv12 */
1838 { "no-sslv3", srv_parse_no_sslv3, 0, 0 }, /* disable SSLv3 */
1839 { "no-tlsv10", srv_parse_no_tlsv10, 0, 0 }, /* disable TLSv10 */
1840 { "no-tlsv11", srv_parse_no_tlsv11, 0, 0 }, /* disable TLSv11 */
1841 { "no-tlsv12", srv_parse_no_tlsv12, 0, 0 }, /* disable TLSv12 */
Emeric Brunf9c5c472012-10-11 15:28:34 +02001842 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 0 }, /* disable session resumption tickets */
Emeric Brunecc91fe2012-10-11 15:05:10 +02001843 { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */
Emeric Brunef42d922012-10-11 16:11:36 +02001844 { "verify", srv_parse_verify, 1, 0 }, /* set SSL verify method */
Willy Tarreau92faadf2012-10-10 23:04:25 +02001845 { NULL, NULL, 0, 0 },
1846}};
1847
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001848/* transport-layer operations for SSL sockets */
1849struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +02001850 .snd_buf = ssl_sock_from_buf,
1851 .rcv_buf = ssl_sock_to_buf,
1852 .rcv_pipe = NULL,
1853 .snd_pipe = NULL,
1854 .shutr = NULL,
1855 .shutw = ssl_sock_shutw,
1856 .close = ssl_sock_close,
1857 .init = ssl_sock_init,
1858};
1859
1860__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +02001861static void __ssl_sock_init(void)
1862{
Emeric Brun46591952012-05-18 15:47:34 +02001863 STACK_OF(SSL_COMP)* cm;
1864
1865 SSL_library_init();
1866 cm = SSL_COMP_get_compression_methods();
1867 sk_SSL_COMP_zero(cm);
Willy Tarreau7875d092012-09-10 08:20:03 +02001868 sample_register_fetches(&sample_fetch_keywords);
1869 acl_register_keywords(&acl_kws);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001870 bind_register_keywords(&bind_kws);
Willy Tarreau92faadf2012-10-10 23:04:25 +02001871 srv_register_keywords(&srv_kws);
Emeric Brun46591952012-05-18 15:47:34 +02001872}
1873
1874/*
1875 * Local variables:
1876 * c-indent-level: 8
1877 * c-basic-offset: 8
1878 * End:
1879 */