blob: fb59515af6a87dba643aac5bbc660d98d9571355 [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
Emeric Brunfc0421f2012-09-07 17:30:07 +0200144#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
145/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
146 * warning when no match is found, which implies the default (first) cert
147 * will keep being used.
148 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200149static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200150{
151 const char *servername;
152 const char *wildp = NULL;
153 struct ebmb_node *node;
154 int i;
155 (void)al; /* shut gcc stupid warning */
156
157 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
158 if (!servername)
159 return SSL_TLSEXT_ERR_NOACK;
160
161 for (i = 0; i < trashlen; i++) {
162 if (!servername[i])
163 break;
164 trash[i] = tolower(servername[i]);
165 if (!wildp && (trash[i] == '.'))
166 wildp = &trash[i];
167 }
168 trash[i] = 0;
169
170 /* lookup in full qualified names */
171 node = ebst_lookup(&s->sni_ctx, trash);
172 if (!node) {
173 if (!wildp)
174 return SSL_TLSEXT_ERR_ALERT_WARNING;
175
176 /* lookup in full wildcards names */
177 node = ebst_lookup(&s->sni_w_ctx, wildp);
178 if (!node)
179 return SSL_TLSEXT_ERR_ALERT_WARNING;
180 }
181
182 /* switch ctx */
183 SSL_set_SSL_CTX(ssl, container_of(node, struct sni_ctx, name)->ctx);
184 return SSL_TLSEXT_ERR_OK;
185}
186#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
187
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200188#ifndef OPENSSL_NO_DH
189/* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1
190 if an error occured, and 0 if parameter not found. */
191int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file)
192{
193 int ret = -1;
194 BIO *in;
195 DH *dh = NULL;
196
197 in = BIO_new(BIO_s_file());
198 if (in == NULL)
199 goto end;
200
201 if (BIO_read_filename(in, file) <= 0)
202 goto end;
203
204 dh = PEM_read_bio_DHparams(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata);
205 if (dh) {
206 SSL_CTX_set_tmp_dh(ctx, dh);
207 ret = 1;
208 goto end;
209 }
210
211 ret = 0; /* DH params not found */
212end:
213 if (dh)
214 DH_free(dh);
215
216 if (in)
217 BIO_free(in);
218
219 return ret;
220}
221#endif
222
Emeric Brunfc0421f2012-09-07 17:30:07 +0200223/* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if
224 * an early error happens and the caller must call SSL_CTX_free() by itelf.
225 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200226int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200227{
228 BIO *in;
229 X509 *x = NULL, *ca;
230 int i, len, err;
231 int ret = -1;
232 int order = 0;
233 X509_NAME *xname;
234 char *str;
235 struct sni_ctx *sc;
236#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
237 STACK_OF(GENERAL_NAME) *names;
238#endif
239
240 in = BIO_new(BIO_s_file());
241 if (in == NULL)
242 goto end;
243
244 if (BIO_read_filename(in, file) <= 0)
245 goto end;
246
247 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata);
248 if (x == NULL)
249 goto end;
250
251#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
252 names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
253 if (names) {
254 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
255 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
256 if (name->type == GEN_DNS) {
257 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
258 if ((len = strlen(str))) {
259 int j;
260
261 if (*str != '*') {
262 sc = malloc(sizeof(struct sni_ctx) + len + 1);
263 for (j = 0; j < len; j++)
264 sc->name.key[j] = tolower(str[j]);
265 sc->name.key[len] = 0;
266 sc->order = order++;
267 sc->ctx = ctx;
268 ebst_insert(&s->sni_ctx, &sc->name);
269 }
270 else {
271 sc = malloc(sizeof(struct sni_ctx) + len);
272 for (j = 1; j < len; j++)
273 sc->name.key[j-1] = tolower(str[j]);
274 sc->name.key[len-1] = 0;
275 sc->order = order++;
276 sc->ctx = ctx;
277 ebst_insert(&s->sni_w_ctx, &sc->name);
278 }
279 }
280 OPENSSL_free(str);
281 }
282 }
283 }
284 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
285 }
286#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
287
288 xname = X509_get_subject_name(x);
289 i = -1;
290 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
291 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
292 if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) {
293 if ((len = strlen(str))) {
294 int j;
295
296 if (*str != '*') {
297 sc = malloc(sizeof(struct sni_ctx) + len + 1);
298 for (j = 0; j < len; j++)
299 sc->name.key[j] = tolower(str[j]);
300 sc->name.key[len] = 0;
301 sc->order = order++;
302 sc->ctx = ctx;
303 ebst_insert(&s->sni_ctx, &sc->name);
304 }
305 else {
306 sc = malloc(sizeof(struct sni_ctx) + len);
307 for (j = 1; j < len; j++)
308 sc->name.key[j-1] = tolower(str[j]);
309 sc->name.key[len-1] = 0;
310 sc->order = order++;
311 sc->ctx = ctx;
312 ebst_insert(&s->sni_w_ctx, &sc->name);
313 }
314 }
315 OPENSSL_free(str);
316 }
317 }
318
319 ret = 0; /* the caller must not free the SSL_CTX argument anymore */
320 if (!SSL_CTX_use_certificate(ctx, x))
321 goto end;
322
323 if (ctx->extra_certs != NULL) {
324 sk_X509_pop_free(ctx->extra_certs, X509_free);
325 ctx->extra_certs = NULL;
326 }
327
328 while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata))) {
329 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
330 X509_free(ca);
331 goto end;
332 }
333 }
334
335 err = ERR_get_error();
336 if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
337 /* we successfully reached the last cert in the file */
338 ret = 1;
339 }
340 ERR_clear_error();
341
342end:
343 if (x)
344 X509_free(x);
345
346 if (in)
347 BIO_free(in);
348
349 return ret;
350}
351
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200352static 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 +0200353{
354 int ret;
355 SSL_CTX *ctx;
356
357 ctx = SSL_CTX_new(SSLv23_server_method());
358 if (!ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200359 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
360 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200361 return 1;
362 }
363
364 if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200365 memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
366 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200367 SSL_CTX_free(ctx);
368 return 1;
369 }
370
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200371 ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200372 if (ret <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200373 memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
374 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200375 if (ret < 0) /* serious error, must do that ourselves */
376 SSL_CTX_free(ctx);
377 return 1;
378 }
379 /* we must not free the SSL_CTX anymore below, since it's already in
380 * the tree, so it will be discovered and cleaned in time.
381 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200382#ifndef OPENSSL_NO_DH
383 ret = ssl_sock_load_dh_params(ctx, path);
384 if (ret < 0) {
385 if (err)
386 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
387 *err ? *err : "", path);
388 return 1;
389 }
390#endif
391
Emeric Brunfc0421f2012-09-07 17:30:07 +0200392#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200393 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200394 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
395 err && *err ? *err : "");
Emeric Brunfc0421f2012-09-07 17:30:07 +0200396 return 1;
397 }
398#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200399 if (!bind_conf->default_ctx)
400 bind_conf->default_ctx = ctx;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200401
402 return 0;
403}
404
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200405int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200406{
407 struct dirent *de;
408 DIR *dir;
409 struct stat buf;
410 int pathlen = 0;
411 char *end, *fp;
412 int cfgerr = 0;
413
414 if (!(dir = opendir(path)))
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200415 return ssl_sock_load_cert_file(path, bind_conf, curproxy, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200416
417 /* strip trailing slashes, including first one */
418 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
419 *end = 0;
420
421 if (end >= path)
422 pathlen = end + 1 - path;
423 fp = malloc(pathlen + 1 + NAME_MAX + 1);
424
425 while ((de = readdir(dir))) {
426 snprintf(fp, pathlen + 1 + NAME_MAX + 1, "%s/%s", path, de->d_name);
427 if (stat(fp, &buf) != 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200428 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
429 err && *err ? *err : "", fp, strerror(errno));
Emeric Brunfc0421f2012-09-07 17:30:07 +0200430 cfgerr++;
431 continue;
432 }
433 if (!S_ISREG(buf.st_mode))
434 continue;
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200435 cfgerr += ssl_sock_load_cert_file(fp, bind_conf, curproxy, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200436 }
437 free(fp);
438 closedir(dir);
439 return cfgerr;
440}
441
442#ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */
443#define SSL_OP_CIPHER_SERVER_PREFERENCE 0
444#endif
445
446#ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */
447#define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0
448#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200449#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */
450#define SSL_OP_SINGLE_ECDH_USE 0
451#endif
Emeric Brun2d0c4822012-10-02 13:45:20 +0200452#ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */
453#define SSL_OP_NO_TICKET 0
454#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200455#ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */
456#define SSL_OP_NO_COMPRESSION 0
457#endif
Emeric Brunc0ff4922012-09-28 19:37:02 +0200458#ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */
459#define SSL_OP_NO_TLSv1_1 0
460#endif
461#ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */
462#define SSL_OP_NO_TLSv1_2 0
463#endif
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200464#ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */
465#define SSL_OP_SINGLE_DH_USE 0
466#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200467#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */
468#define SSL_OP_SINGLE_ECDH_USE 0
469#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200470#ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */
471#define SSL_MODE_RELEASE_BUFFERS 0
472#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200473int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, SSL_CTX *ctx, struct proxy *curproxy)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200474{
475 int cfgerr = 0;
476 int ssloptions =
477 SSL_OP_ALL | /* all known workarounds for bugs */
478 SSL_OP_NO_SSLv2 |
479 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200480 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +0200481 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +0200482 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
483 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200484 int sslmode =
485 SSL_MODE_ENABLE_PARTIAL_WRITE |
486 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
487 SSL_MODE_RELEASE_BUFFERS;
488
Emeric Brun89675492012-10-05 13:48:26 +0200489 if (bind_conf->ssl_options & BC_SSL_O_NO_SSLV3)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200490 ssloptions |= SSL_OP_NO_SSLv3;
Emeric Brun89675492012-10-05 13:48:26 +0200491 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV10)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200492 ssloptions |= SSL_OP_NO_TLSv1;
Emeric Brun89675492012-10-05 13:48:26 +0200493 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV11)
Emeric Brunc0ff4922012-09-28 19:37:02 +0200494 ssloptions |= SSL_OP_NO_TLSv1_1;
Emeric Brun89675492012-10-05 13:48:26 +0200495 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV12)
Emeric Brunc0ff4922012-09-28 19:37:02 +0200496 ssloptions |= SSL_OP_NO_TLSv1_2;
Emeric Brun89675492012-10-05 13:48:26 +0200497 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
Emeric Brun2d0c4822012-10-02 13:45:20 +0200498 ssloptions |= SSL_OP_NO_TICKET;
Emeric Brun2cb7ae52012-10-05 14:14:21 +0200499 if (bind_conf->ssl_options & BC_SSL_O_USE_SSLV3)
500 SSL_CTX_set_ssl_version(ctx, SSLv3_server_method());
501 if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV10)
502 SSL_CTX_set_ssl_version(ctx, TLSv1_server_method());
503#if SSL_OP_NO_TLSv1_1
504 if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV11)
505 SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method());
506#endif
507#if SSL_OP_NO_TLSv1_2
508 if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV12)
509 SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method());
510#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200511
512 SSL_CTX_set_options(ctx, ssloptions);
513 SSL_CTX_set_mode(ctx, sslmode);
Emeric Brune64aef12012-09-21 13:15:06 +0200514 SSL_CTX_set_verify(ctx, bind_conf->verify ? bind_conf->verify : SSL_VERIFY_NONE, ssl_sock_verifycbk);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200515 if (bind_conf->verify & SSL_VERIFY_PEER) {
Emeric Brunfb510ea2012-10-05 12:00:26 +0200516 if (bind_conf->ca_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200517 /* load CAfile to verify */
Emeric Brunfb510ea2012-10-05 12:00:26 +0200518 if (!SSL_CTX_load_verify_locations(ctx, bind_conf->ca_file, NULL)) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200519 Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
Emeric Brunfb510ea2012-10-05 12:00:26 +0200520 curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200521 cfgerr++;
522 }
523 /* set CA names fo client cert request, function returns void */
Emeric Brunfb510ea2012-10-05 12:00:26 +0200524 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(bind_conf->ca_file));
Emeric Brund94b3fe2012-09-20 18:23:56 +0200525 }
Emeric Brun051cdab2012-10-02 19:25:50 +0200526#ifdef X509_V_FLAG_CRL_CHECK
Emeric Brunfb510ea2012-10-05 12:00:26 +0200527 if (bind_conf->crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200528 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
529
Emeric Brunfb510ea2012-10-05 12:00:26 +0200530 if (!store || !X509_STORE_load_locations(store, bind_conf->crl_file, NULL)) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200531 Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
Emeric Brunfb510ea2012-10-05 12:00:26 +0200532 curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200533 cfgerr++;
534 }
Emeric Brun561e5742012-10-02 15:20:55 +0200535 else {
536 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
537 }
Emeric Brund94b3fe2012-09-20 18:23:56 +0200538 }
Emeric Brun051cdab2012-10-02 19:25:50 +0200539#endif
Emeric Brund94b3fe2012-09-20 18:23:56 +0200540 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200541
542 shared_context_set_cache(ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200543 if (bind_conf->ciphers &&
544 !SSL_CTX_set_cipher_list(ctx, bind_conf->ciphers)) {
Emeric Brunfc0421f2012-09-07 17:30:07 +0200545 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 +0200546 curproxy->id, bind_conf->ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200547 cfgerr++;
548 }
549
550 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
551#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
552 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200553 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200554#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200555#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
556 if (bind_conf->ecdhe) {
557 int i;
558 EC_KEY *ecdh;
559
560 i = OBJ_sn2nid(bind_conf->ecdhe);
561 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
562 Alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
563 curproxy->id, bind_conf->ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
564 cfgerr++;
565 }
566 else {
567 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
568 EC_KEY_free(ecdh);
569 }
570 }
571#endif
572
Emeric Brunfc0421f2012-09-07 17:30:07 +0200573 return cfgerr;
574}
575
Emeric Brun94324a42012-10-11 14:00:19 +0200576/* prepare ssl context from servers options. Returns an error count */
577int ssl_sock_prepare_srv_ctx(struct server *srv, struct proxy *curproxy)
578{
579 int cfgerr = 0;
580 int options =
581 SSL_OP_ALL | /* all known workarounds for bugs */
582 SSL_OP_NO_SSLv2 |
583 SSL_OP_NO_COMPRESSION;
584 int mode =
585 SSL_MODE_ENABLE_PARTIAL_WRITE |
586 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
587 SSL_MODE_RELEASE_BUFFERS;
588
589 /* Initiate SSL context for current server */
590 srv->ssl_ctx.reused_sess = NULL;
591 if (srv->use_ssl)
592 srv->xprt = &ssl_sock;
593 if (srv->check.use_ssl)
594 srv->check.xprt = &ssl_sock;
595
596 srv->ssl_ctx.ctx = SSL_CTX_new(SSLv23_client_method());
597 if (!srv->ssl_ctx.ctx) {
598 Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
599 proxy_type_str(curproxy), curproxy->id,
600 srv->id);
601 cfgerr++;
602 return cfgerr;
603 }
604
605
606 if (srv->ssl_ctx.options & SRV_SSL_O_NO_SSLV3)
607 options |= SSL_OP_NO_SSLv3;
608 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV10)
609 options |= SSL_OP_NO_TLSv1;
610 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV11)
611 options |= SSL_OP_NO_TLSv1_1;
612 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV12)
613 options |= SSL_OP_NO_TLSv1_2;
Emeric Brunf9c5c472012-10-11 15:28:34 +0200614 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
615 options |= SSL_OP_NO_TICKET;
Emeric Brun94324a42012-10-11 14:00:19 +0200616 if (srv->ssl_ctx.options & SRV_SSL_O_USE_SSLV3)
617 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, SSLv3_client_method());
618 if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV10)
619 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_client_method());
620#if SSL_OP_NO_TLSv1_1
621 if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV11)
622 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_1_client_method());
623#endif
624#if SSL_OP_NO_TLSv1_2
625 if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV12)
626 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_2_client_method());
627#endif
628
629 SSL_CTX_set_options(srv->ssl_ctx.ctx, options);
630 SSL_CTX_set_mode(srv->ssl_ctx.ctx, mode);
631 SSL_CTX_set_verify(srv->ssl_ctx.ctx, SSL_VERIFY_NONE, NULL);
632 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF);
633 if (srv->ssl_ctx.ciphers &&
634 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
635 Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
636 curproxy->id, srv->id,
637 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
638 cfgerr++;
639 }
640
641 return cfgerr;
642}
643
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200644/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +0200645 * be NULL, in which case nothing is done. Returns the number of errors
646 * encountered.
647 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200648int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf, struct proxy *px)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200649{
650 struct ebmb_node *node;
651 struct sni_ctx *sni;
652 int err = 0;
653
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200654 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200655 return 0;
656
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200657 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200658 while (node) {
659 sni = ebmb_entry(node, struct sni_ctx, name);
660 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200661 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200662 node = ebmb_next(node);
663 }
664
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200665 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200666 while (node) {
667 sni = ebmb_entry(node, struct sni_ctx, name);
668 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200669 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200670 node = ebmb_next(node);
671 }
672 return err;
673}
674
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200675/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +0200676 * be NULL, in which case nothing is done. The default_ctx is nullified too.
677 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200678void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200679{
680 struct ebmb_node *node, *back;
681 struct sni_ctx *sni;
682
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200683 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200684 return;
685
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200686 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200687 while (node) {
688 sni = ebmb_entry(node, struct sni_ctx, name);
689 back = ebmb_next(node);
690 ebmb_delete(node);
691 if (!sni->order) /* only free the CTX on its first occurrence */
692 SSL_CTX_free(sni->ctx);
693 free(sni);
694 node = back;
695 }
696
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200697 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200698 while (node) {
699 sni = ebmb_entry(node, struct sni_ctx, name);
700 back = ebmb_next(node);
701 ebmb_delete(node);
702 if (!sni->order) /* only free the CTX on its first occurrence */
703 SSL_CTX_free(sni->ctx);
704 free(sni);
705 node = back;
706 }
707
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200708 bind_conf->default_ctx = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +0200709}
710
Emeric Brun46591952012-05-18 15:47:34 +0200711/*
712 * This function is called if SSL * context is not yet allocated. The function
713 * is designed to be called before any other data-layer operation and sets the
714 * handshake flag on the connection. It is safe to call it multiple times.
715 * It returns 0 on success and -1 in error case.
716 */
717static int ssl_sock_init(struct connection *conn)
718{
719 /* already initialized */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200720 if (conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200721 return 0;
722
Willy Tarreau403edff2012-09-06 11:58:37 +0200723 if (global.maxsslconn && sslconns >= global.maxsslconn)
724 return -1;
725
Emeric Brun46591952012-05-18 15:47:34 +0200726 /* If it is in client mode initiate SSL session
727 in connect state otherwise accept state */
728 if (target_srv(&conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +0200729 /* Alloc a new SSL session ctx */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200730 conn->xprt_ctx = SSL_new(target_srv(&conn->target)->ssl_ctx.ctx);
731 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200732 return -1;
733
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200734 SSL_set_connect_state(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200735 if (target_srv(&conn->target)->ssl_ctx.reused_sess)
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200736 SSL_set_session(conn->xprt_ctx, target_srv(&conn->target)->ssl_ctx.reused_sess);
Emeric Brun46591952012-05-18 15:47:34 +0200737
738 /* set fd on SSL session context */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200739 SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +0200740
741 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +0200742 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +0200743
744 sslconns++;
Emeric Brun46591952012-05-18 15:47:34 +0200745 return 0;
746 }
747 else if (target_client(&conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +0200748 /* Alloc a new SSL session ctx */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200749 conn->xprt_ctx = SSL_new(target_client(&conn->target)->bind_conf->default_ctx);
750 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200751 return -1;
752
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200753 SSL_set_accept_state(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200754
755 /* set fd on SSL session context */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200756 SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +0200757
Emeric Brune1f38db2012-09-03 20:36:47 +0200758 /* set connection pointer */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200759 SSL_set_app_data(conn->xprt_ctx, conn);
Emeric Brune1f38db2012-09-03 20:36:47 +0200760
Emeric Brun46591952012-05-18 15:47:34 +0200761 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +0200762 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +0200763
764 sslconns++;
Emeric Brun46591952012-05-18 15:47:34 +0200765 return 0;
766 }
767 /* don't know how to handle such a target */
768 return -1;
769}
770
771
772/* This is the callback which is used when an SSL handshake is pending. It
773 * updates the FD status if it wants some polling before being called again.
774 * It returns 0 if it fails in a fatal way or needs to poll to go further,
775 * otherwise it returns non-zero and removes itself from the connection's
776 * flags (the bit is provided in <flag> by the caller).
777 */
778int ssl_sock_handshake(struct connection *conn, unsigned int flag)
779{
780 int ret;
781
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200782 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200783 goto out_error;
784
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200785 ret = SSL_do_handshake(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200786 if (ret != 1) {
787 /* handshake did not complete, let's find why */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200788 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +0200789
790 if (ret == SSL_ERROR_WANT_WRITE) {
791 /* SSL handshake needs to write, L4 connection may not be ready */
792 __conn_sock_stop_recv(conn);
793 __conn_sock_poll_send(conn);
794 return 0;
795 }
796 else if (ret == SSL_ERROR_WANT_READ) {
797 /* SSL handshake needs to read, L4 connection is ready */
798 if (conn->flags & CO_FL_WAIT_L4_CONN)
799 conn->flags &= ~CO_FL_WAIT_L4_CONN;
800 __conn_sock_stop_send(conn);
801 __conn_sock_poll_recv(conn);
802 return 0;
803 }
Willy Tarreau89230192012-09-28 20:22:13 +0200804 else if (ret == SSL_ERROR_SYSCALL) {
805 /* if errno is null, then connection was successfully established */
806 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
807 conn->flags &= ~CO_FL_WAIT_L4_CONN;
808 goto out_error;
809 }
Emeric Brun46591952012-05-18 15:47:34 +0200810 else {
811 /* Fail on all other handshake errors */
812 goto out_error;
813 }
814 }
815
816 /* Handshake succeeded */
817 if (target_srv(&conn->target)) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200818 if (!SSL_session_reused(conn->xprt_ctx)) {
Emeric Brun46591952012-05-18 15:47:34 +0200819 /* check if session was reused, if not store current session on server for reuse */
820 if (target_srv(&conn->target)->ssl_ctx.reused_sess)
821 SSL_SESSION_free(target_srv(&conn->target)->ssl_ctx.reused_sess);
822
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200823 target_srv(&conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200824 }
825 }
826
827 /* The connection is now established at both layers, it's time to leave */
828 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
829 return 1;
830
831 out_error:
Emeric Brun9fa89732012-10-04 17:09:56 +0200832 /* free resumed session if exists */
833 if (target_srv(&conn->target) && target_srv(&conn->target)->ssl_ctx.reused_sess) {
834 SSL_SESSION_free(target_srv(&conn->target)->ssl_ctx.reused_sess);
835 target_srv(&conn->target)->ssl_ctx.reused_sess = NULL;
836 }
837
Emeric Brun46591952012-05-18 15:47:34 +0200838 /* Fail on all other handshake errors */
839 conn->flags |= CO_FL_ERROR;
840 conn->flags &= ~flag;
841 return 0;
842}
843
844/* Receive up to <count> bytes from connection <conn>'s socket and store them
845 * into buffer <buf>. The caller must ensure that <count> is always smaller
846 * than the buffer's size. Only one call to recv() is performed, unless the
847 * buffer wraps, in which case a second call may be performed. The connection's
848 * flags are updated with whatever special event is detected (error, read0,
849 * empty). The caller is responsible for taking care of those events and
850 * avoiding the call if inappropriate. The function does not call the
851 * connection's polling update function, so the caller is responsible for this.
852 */
853static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
854{
855 int ret, done = 0;
856 int try = count;
857
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200858 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200859 goto out_error;
860
861 if (conn->flags & CO_FL_HANDSHAKE)
862 /* a handshake was requested */
863 return 0;
864
865 /* compute the maximum block size we can read at once. */
866 if (buffer_empty(buf)) {
867 /* let's realign the buffer to optimize I/O */
868 buf->p = buf->data;
869 }
870 else if (buf->data + buf->o < buf->p &&
871 buf->p + buf->i < buf->data + buf->size) {
872 /* remaining space wraps at the end, with a moving limit */
873 if (try > buf->data + buf->size - (buf->p + buf->i))
874 try = buf->data + buf->size - (buf->p + buf->i);
875 }
876
877 /* read the largest possible block. For this, we perform only one call
878 * to recv() unless the buffer wraps and we exactly fill the first hunk,
879 * in which case we accept to do it once again. A new attempt is made on
880 * EINTR too.
881 */
882 while (try) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200883 ret = SSL_read(conn->xprt_ctx, bi_end(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +0200884 if (conn->flags & CO_FL_ERROR) {
885 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
886 break;
887 }
Emeric Brun46591952012-05-18 15:47:34 +0200888 if (ret > 0) {
889 buf->i += ret;
890 done += ret;
891 if (ret < try)
892 break;
893 count -= ret;
894 try = count;
895 }
896 else if (ret == 0) {
897 goto read0;
898 }
899 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200900 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +0200901 if (ret == SSL_ERROR_WANT_WRITE) {
902 /* handshake is running, and it needs to poll for a write event */
903 conn->flags |= CO_FL_SSL_WAIT_HS;
904 __conn_sock_poll_send(conn);
905 break;
906 }
907 else if (ret == SSL_ERROR_WANT_READ) {
908 /* we need to poll for retry a read later */
909 __conn_data_poll_recv(conn);
910 break;
911 }
912 /* otherwise it's a real error */
913 goto out_error;
914 }
915 }
916 return done;
917
918 read0:
919 conn_sock_read0(conn);
920 return done;
921 out_error:
922 conn->flags |= CO_FL_ERROR;
923 return done;
924}
925
926
927/* Send all pending bytes from buffer <buf> to connection <conn>'s socket.
928 * <flags> may contain MSG_MORE to make the system hold on without sending
929 * data too fast, but this flag is ignored at the moment.
930 * Only one call to send() is performed, unless the buffer wraps, in which case
931 * a second call may be performed. The connection's flags are updated with
932 * whatever special event is detected (error, empty). The caller is responsible
933 * for taking care of those events and avoiding the call if inappropriate. The
934 * function does not call the connection's polling update function, so the caller
935 * is responsible for this.
936 */
937static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags)
938{
939 int ret, try, done;
940
941 done = 0;
942
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200943 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200944 goto out_error;
945
946 if (conn->flags & CO_FL_HANDSHAKE)
947 /* a handshake was requested */
948 return 0;
949
950 /* send the largest possible block. For this we perform only one call
951 * to send() unless the buffer wraps and we exactly fill the first hunk,
952 * in which case we accept to do it once again.
953 */
954 while (buf->o) {
955 try = buf->o;
956 /* outgoing data may wrap at the end */
957 if (buf->data + try > buf->p)
958 try = buf->data + try - buf->p;
959
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200960 ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +0200961 if (conn->flags & CO_FL_ERROR) {
962 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
963 break;
964 }
Emeric Brun46591952012-05-18 15:47:34 +0200965 if (ret > 0) {
966 buf->o -= ret;
967 done += ret;
968
969 if (likely(!buffer_len(buf)))
970 /* optimize data alignment in the buffer */
971 buf->p = buf->data;
972
973 /* if the system buffer is full, don't insist */
974 if (ret < try)
975 break;
976 }
977 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200978 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +0200979 if (ret == SSL_ERROR_WANT_WRITE) {
980 /* we need to poll to retry a write later */
981 __conn_data_poll_send(conn);
982 break;
983 }
984 else if (ret == SSL_ERROR_WANT_READ) {
985 /* handshake is running, and
986 it needs to poll for a read event,
987 write polling must be disabled cause
988 we are sure we can't write anything more
989 before handshake re-performed */
990 conn->flags |= CO_FL_SSL_WAIT_HS;
991 __conn_sock_poll_recv(conn);
992 break;
993 }
994 goto out_error;
995 }
996 }
997 return done;
998
999 out_error:
1000 conn->flags |= CO_FL_ERROR;
1001 return done;
1002}
1003
1004
1005static void ssl_sock_close(struct connection *conn) {
1006
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001007 if (conn->xprt_ctx) {
1008 SSL_free(conn->xprt_ctx);
1009 conn->xprt_ctx = NULL;
Willy Tarreau403edff2012-09-06 11:58:37 +02001010 sslconns--;
Emeric Brun46591952012-05-18 15:47:34 +02001011 }
Emeric Brun46591952012-05-18 15:47:34 +02001012}
1013
1014/* This function tries to perform a clean shutdown on an SSL connection, and in
1015 * any case, flags the connection as reusable if no handshake was in progress.
1016 */
1017static void ssl_sock_shutw(struct connection *conn, int clean)
1018{
1019 if (conn->flags & CO_FL_HANDSHAKE)
1020 return;
1021 /* no handshake was in progress, try a clean ssl shutdown */
1022 if (clean)
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001023 SSL_shutdown(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +02001024
1025 /* force flag on ssl to keep session in cache regardless shutdown result */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001026 SSL_set_shutdown(conn->xprt_ctx, SSL_SENT_SHUTDOWN);
Emeric Brun46591952012-05-18 15:47:34 +02001027}
1028
Willy Tarreau7875d092012-09-10 08:20:03 +02001029/***** Below are some sample fetching functions for ACL/patterns *****/
1030
Emeric Brune64aef12012-09-21 13:15:06 +02001031/* boolean, returns true if client cert was present */
1032static int
1033smp_fetch_client_crt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1034 const struct arg *args, struct sample *smp)
1035{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001036 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02001037 return 0;
1038
1039 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1040 smp->flags |= SMP_F_MAY_CHANGE;
1041 return 0;
1042 }
1043
1044 smp->flags = 0;
1045 smp->type = SMP_T_BOOL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001046 smp->data.uint = SSL_SOCK_ST_FL_VERIFY_DONE & l4->si[0].conn.xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001047
1048 return 1;
1049}
1050
1051
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001052/* boolean, returns true if transport layer is SSL */
Willy Tarreau7875d092012-09-10 08:20:03 +02001053static int
1054smp_fetch_is_ssl(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1055 const struct arg *args, struct sample *smp)
1056{
1057 smp->type = SMP_T_BOOL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001058 smp->data.uint = (l4->si[0].conn.xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02001059 return 1;
1060}
1061
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001062/* boolean, returns true if transport layer is SSL */
Willy Tarreau7875d092012-09-10 08:20:03 +02001063static int
1064smp_fetch_has_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1065 const struct arg *args, struct sample *smp)
1066{
1067#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1068 smp->type = SMP_T_BOOL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001069 smp->data.uint = (l4->si[0].conn.xprt == &ssl_sock) &&
1070 l4->si[0].conn.xprt_ctx &&
1071 SSL_get_servername(l4->si[0].conn.xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02001072 return 1;
1073#else
1074 return 0;
1075#endif
1076}
1077
1078static int
1079smp_fetch_ssl_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1080 const struct arg *args, struct sample *smp)
1081{
1082#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1083 smp->flags = 0;
1084 smp->type = SMP_T_CSTR;
1085
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001086 if (!l4 || !l4->si[0].conn.xprt_ctx || l4->si[0].conn.xprt != &ssl_sock)
Willy Tarreau7875d092012-09-10 08:20:03 +02001087 return 0;
1088
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001089 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 +02001090 if (!smp->data.str.str)
1091 return 0;
1092
Willy Tarreau7875d092012-09-10 08:20:03 +02001093 smp->data.str.len = strlen(smp->data.str.str);
1094 return 1;
1095#else
1096 return 0;
1097#endif
1098}
1099
Emeric Brunf282a812012-09-21 15:27:54 +02001100/* integer, returns the first verify error ID in CA */
1101static int
1102smp_fetch_verify_caerr(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 Brunf282a812012-09-21 15:27:54 +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->type = SMP_T_UINT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001114 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CA_ERROR(l4->si[0].conn.xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02001115 smp->flags = 0;
1116
1117 return 1;
1118}
1119
1120/* integer, returns the depth of the first verify error in CA */
1121static int
1122smp_fetch_verify_caerr_depth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1123 const struct arg *args, struct sample *smp)
1124{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001125 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02001126 return 0;
1127
1128 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1129 smp->flags = SMP_F_MAY_CHANGE;
1130 return 0;
1131 }
1132
1133 smp->type = SMP_T_UINT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001134 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CAEDEPTH(l4->si[0].conn.xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02001135 smp->flags = 0;
1136
1137 return 1;
1138}
1139
1140/* integer, returns the depth of the first verify error in CA */
1141static int
1142smp_fetch_verify_crterr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1143 const struct arg *args, struct sample *smp)
1144{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001145 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02001146 return 0;
1147
1148 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1149 smp->flags = SMP_F_MAY_CHANGE;
1150 return 0;
1151 }
1152
1153 smp->type = SMP_T_UINT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001154 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CRTERROR(l4->si[0].conn.xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02001155 smp->flags = 0;
1156
1157 return 1;
1158}
1159
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001160/* integer, returns the verify result */
1161static int
1162smp_fetch_verify_result(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1163 const struct arg *args, struct sample *smp)
1164{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001165 if (!l4 || l4->si[0].conn.xprt != &ssl_sock)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001166 return 0;
1167
1168 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1169 smp->flags = SMP_F_MAY_CHANGE;
1170 return 0;
1171 }
1172
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001173 if (!l4->si[0].conn.xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001174 return 0;
1175
1176 smp->type = SMP_T_UINT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001177 smp->data.uint = (unsigned int)SSL_get_verify_result(l4->si[0].conn.xprt_ctx);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001178 smp->flags = 0;
1179
1180 return 1;
1181}
1182
Emeric Brunfb510ea2012-10-05 12:00:26 +02001183/* parse the "ca-file" bind keyword */
1184static 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 +02001185{
1186 if (!*args[cur_arg + 1]) {
1187 if (err)
1188 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
1189 return ERR_ALERT | ERR_FATAL;
1190 }
1191
Emeric Brunc8e8d122012-10-02 18:42:10 +02001192 if ((*args[cur_arg + 1] != '/') && global.ca_base) {
Emeric Brunfb510ea2012-10-05 12:00:26 +02001193 conf->ca_file = malloc(strlen(global.ca_base) + 1 + strlen(args[cur_arg + 1]) + 1);
1194 if (conf->ca_file)
1195 sprintf(conf->ca_file, "%s/%s", global.ca_base, args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02001196 return 0;
1197 }
1198
Emeric Brunfb510ea2012-10-05 12:00:26 +02001199 conf->ca_file = strdup(args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02001200 return 0;
1201}
1202
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001203/* parse the "ciphers" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001204static 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 +02001205{
1206 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001207 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001208 return ERR_ALERT | ERR_FATAL;
1209 }
1210
Emeric Brun76d88952012-10-05 15:47:31 +02001211 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02001212 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001213 return 0;
1214}
1215
1216/* parse the "crt" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001217static 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 +02001218{
Emeric Brunc8e8d122012-10-02 18:42:10 +02001219 char path[PATH_MAX];
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001220 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001221 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001222 return ERR_ALERT | ERR_FATAL;
1223 }
1224
Emeric Brunc8e8d122012-10-02 18:42:10 +02001225 if ((*args[cur_arg + 1] != '/' ) && global.crt_base) {
1226 if ((strlen(global.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > PATH_MAX) {
1227 memprintf(err, "'%s' : path too long", args[cur_arg]);
1228 return ERR_ALERT | ERR_FATAL;
1229 }
1230 sprintf(path, "%s/%s", global.crt_base, args[cur_arg + 1]);
1231 if (ssl_sock_load_cert(path, conf, px, err) > 0)
1232 return ERR_ALERT | ERR_FATAL;
1233
1234 return 0;
1235 }
1236
Willy Tarreau4348fad2012-09-20 16:48:07 +02001237 if (ssl_sock_load_cert(args[cur_arg + 1], conf, px, err) > 0)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001238 return ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02001239
1240 return 0;
1241}
1242
Emeric Brunfb510ea2012-10-05 12:00:26 +02001243/* parse the "crl-file" bind keyword */
1244static 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 +02001245{
Emeric Brun051cdab2012-10-02 19:25:50 +02001246#ifndef X509_V_FLAG_CRL_CHECK
1247 if (err)
1248 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
1249 return ERR_ALERT | ERR_FATAL;
1250#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02001251 if (!*args[cur_arg + 1]) {
1252 if (err)
1253 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
1254 return ERR_ALERT | ERR_FATAL;
1255 }
Emeric Brun2b58d042012-09-20 17:10:03 +02001256
Emeric Brunc8e8d122012-10-02 18:42:10 +02001257 if ((*args[cur_arg + 1] != '/') && global.ca_base) {
Emeric Brunfb510ea2012-10-05 12:00:26 +02001258 conf->crl_file = malloc(strlen(global.ca_base) + 1 + strlen(args[cur_arg + 1]) + 1);
1259 if (conf->crl_file)
1260 sprintf(conf->crl_file, "%s/%s", global.ca_base, args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02001261 return 0;
1262 }
1263
Emeric Brunfb510ea2012-10-05 12:00:26 +02001264 conf->crl_file = strdup(args[cur_arg + 1]);
Emeric Brun2b58d042012-09-20 17:10:03 +02001265 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02001266#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02001267}
1268
1269/* parse the "ecdhe" bind keyword keywords */
1270static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1271{
1272#if OPENSSL_VERSION_NUMBER < 0x0090800fL
1273 if (err)
1274 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
1275 return ERR_ALERT | ERR_FATAL;
1276#elif defined(OPENSSL_NO_ECDH)
1277 if (err)
1278 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
1279 return ERR_ALERT | ERR_FATAL;
1280#else
1281 if (!*args[cur_arg + 1]) {
1282 if (err)
1283 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
1284 return ERR_ALERT | ERR_FATAL;
1285 }
1286
1287 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001288
1289 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02001290#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001291}
1292
Emeric Brun81c00f02012-09-21 14:31:21 +02001293/* parse the "crt_ignerr" and "ca_ignerr" bind keywords */
1294static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1295{
1296 int code;
1297 char *p = args[cur_arg + 1];
1298 unsigned long long *ignerr = &conf->crt_ignerr;
1299
1300 if (!*p) {
1301 if (err)
1302 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
1303 return ERR_ALERT | ERR_FATAL;
1304 }
1305
1306 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
1307 ignerr = &conf->ca_ignerr;
1308
1309 if (strcmp(p, "all") == 0) {
1310 *ignerr = ~0ULL;
1311 return 0;
1312 }
1313
1314 while (p) {
1315 code = atoi(p);
1316 if ((code <= 0) || (code > 63)) {
1317 if (err)
1318 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
1319 args[cur_arg], code, args[cur_arg + 1]);
1320 return ERR_ALERT | ERR_FATAL;
1321 }
1322 *ignerr |= 1ULL << code;
1323 p = strchr(p, ',');
1324 if (p)
1325 p++;
1326 }
1327
Emeric Brun2cb7ae52012-10-05 14:14:21 +02001328 return 0;
1329}
1330
1331/* parse the "force-sslv3" bind keyword */
1332static int bind_parse_force_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1333{
1334 conf->ssl_options |= BC_SSL_O_USE_SSLV3;
1335 return 0;
1336}
1337
1338/* parse the "force-tlsv10" bind keyword */
1339static int bind_parse_force_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1340{
1341 conf->ssl_options |= BC_SSL_O_USE_TLSV10;
Emeric Brun2d0c4822012-10-02 13:45:20 +02001342 return 0;
1343}
1344
Emeric Brun2cb7ae52012-10-05 14:14:21 +02001345/* parse the "force-tlsv11" bind keyword */
1346static int bind_parse_force_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1347{
1348#if SSL_OP_NO_TLSv1_1
1349 conf->ssl_options |= BC_SSL_O_USE_TLSV11;
1350 return 0;
1351#else
1352 if (err)
1353 memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[cur_arg]);
1354 return ERR_ALERT | ERR_FATAL;
1355#endif
1356}
1357
1358/* parse the "force-tlsv12" bind keyword */
1359static int bind_parse_force_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1360{
1361#if SSL_OP_NO_TLSv1_2
1362 conf->ssl_options |= BC_SSL_O_USE_TLSV12;
1363 return 0;
1364#else
1365 if (err)
1366 memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[cur_arg]);
1367 return ERR_ALERT | ERR_FATAL;
1368#endif
1369}
1370
1371
Emeric Brun2d0c4822012-10-02 13:45:20 +02001372/* parse the "no-tls-tickets" bind keyword */
1373static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1374{
Emeric Brun89675492012-10-05 13:48:26 +02001375 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02001376 return 0;
1377}
1378
Emeric Brun2d0c4822012-10-02 13:45:20 +02001379
Emeric Brun9b3009b2012-10-05 11:55:06 +02001380/* parse the "no-sslv3" bind keyword */
1381static 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 +02001382{
Emeric Brun89675492012-10-05 13:48:26 +02001383 conf->ssl_options |= BC_SSL_O_NO_SSLV3;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001384 return 0;
1385}
1386
Emeric Brun9b3009b2012-10-05 11:55:06 +02001387/* parse the "no-tlsv10" bind keyword */
1388static 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 +02001389{
Emeric Brun89675492012-10-05 13:48:26 +02001390 conf->ssl_options |= BC_SSL_O_NO_TLSV10;
Emeric Brunc0ff4922012-09-28 19:37:02 +02001391 return 0;
1392}
1393
Emeric Brun9b3009b2012-10-05 11:55:06 +02001394/* parse the "no-tlsv11" bind keyword */
1395static 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 +02001396{
Emeric Brun89675492012-10-05 13:48:26 +02001397 conf->ssl_options |= BC_SSL_O_NO_TLSV11;
Emeric Brunc0ff4922012-09-28 19:37:02 +02001398 return 0;
1399}
1400
Emeric Brun9b3009b2012-10-05 11:55:06 +02001401/* parse the "no-tlsv12" bind keyword */
1402static 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 +02001403{
Emeric Brun89675492012-10-05 13:48:26 +02001404 conf->ssl_options |= BC_SSL_O_NO_TLSV12;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001405 return 0;
1406}
1407
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001408/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001409static 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 +02001410{
Willy Tarreau81796be2012-09-22 19:11:47 +02001411 struct listener *l;
1412
Willy Tarreau4348fad2012-09-20 16:48:07 +02001413 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02001414
1415 if (global.listen_default_ciphers && !conf->ciphers)
1416 conf->ciphers = strdup(global.listen_default_ciphers);
1417
Willy Tarreau81796be2012-09-22 19:11:47 +02001418 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001419 l->xprt = &ssl_sock;
Willy Tarreau81796be2012-09-22 19:11:47 +02001420
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001421 return 0;
1422}
1423
Emeric Brund94b3fe2012-09-20 18:23:56 +02001424/* parse the "verify" bind keyword */
1425static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1426{
1427 if (!*args[cur_arg + 1]) {
1428 if (err)
1429 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
1430 return ERR_ALERT | ERR_FATAL;
1431 }
1432
1433 if (strcmp(args[cur_arg + 1], "none") == 0)
1434 conf->verify = SSL_VERIFY_NONE;
1435 else if (strcmp(args[cur_arg + 1], "optional") == 0)
1436 conf->verify = SSL_VERIFY_PEER;
1437 else if (strcmp(args[cur_arg + 1], "required") == 0)
1438 conf->verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1439 else {
1440 if (err)
1441 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
1442 args[cur_arg], args[cur_arg + 1]);
1443 return ERR_ALERT | ERR_FATAL;
1444 }
1445
1446 return 0;
1447}
1448
Willy Tarreau92faadf2012-10-10 23:04:25 +02001449/************** "server" keywords ****************/
1450
1451/* parse the "check-ssl" server keyword */
1452static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1453{
1454 newsrv->check.use_ssl = 1;
1455 if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
1456 newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers);
1457 return 0;
1458}
1459
1460/* parse the "ciphers" server keyword */
1461static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1462{
1463 if (!*args[*cur_arg + 1]) {
1464 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
1465 return ERR_ALERT | ERR_FATAL;
1466 }
1467
1468 free(newsrv->ssl_ctx.ciphers);
1469 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
1470 return 0;
1471}
1472
1473/* parse the "force-sslv3" server keyword */
1474static int srv_parse_force_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1475{
1476 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_SSLV3;
1477 return 0;
1478}
1479
1480/* parse the "force-tlsv10" server keyword */
1481static int srv_parse_force_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1482{
1483 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV10;
1484 return 0;
1485}
1486
1487/* parse the "force-tlsv11" server keyword */
1488static int srv_parse_force_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1489{
1490#if SSL_OP_NO_TLSv1_1
1491 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV11;
1492 return 0;
1493#else
1494 if (err)
1495 memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[*cur_arg]);
1496 return ERR_ALERT | ERR_FATAL;
1497#endif
1498}
1499
1500/* parse the "force-tlsv12" server keyword */
1501static int srv_parse_force_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1502{
1503#if SSL_OP_NO_TLSv1_2
1504 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV12;
1505 return 0;
1506#else
1507 if (err)
1508 memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[*cur_arg]);
1509 return ERR_ALERT | ERR_FATAL;
1510#endif
1511}
1512
1513/* parse the "no-sslv3" server keyword */
1514static int srv_parse_no_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1515{
1516 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_SSLV3;
1517 return 0;
1518}
1519
1520/* parse the "no-tlsv10" server keyword */
1521static int srv_parse_no_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1522{
1523 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV10;
1524 return 0;
1525}
1526
1527/* parse the "no-tlsv11" server keyword */
1528static int srv_parse_no_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1529{
1530 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV11;
1531 return 0;
1532}
1533
1534/* parse the "no-tlsv12" server keyword */
1535static int srv_parse_no_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1536{
1537 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV12;
1538 return 0;
1539}
1540
Emeric Brunf9c5c472012-10-11 15:28:34 +02001541/* parse the "no-tls-tickets" server keyword */
1542static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1543{
1544 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
1545 return 0;
1546}
1547
Willy Tarreau92faadf2012-10-10 23:04:25 +02001548/* parse the "ssl" server keyword */
1549static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1550{
1551 newsrv->use_ssl = 1;
1552 if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
1553 newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers);
1554 return 0;
1555}
1556
Willy Tarreau7875d092012-09-10 08:20:03 +02001557/* Note: must not be declared <const> as its list will be overwritten.
1558 * Please take care of keeping this list alphabetically sorted.
1559 */
1560static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Emeric Brunf282a812012-09-21 15:27:54 +02001561 { "client_crt", smp_fetch_client_crt, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
1562 { "is_ssl", smp_fetch_is_ssl, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
1563 { "ssl_has_sni", smp_fetch_has_sni, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
1564 { "ssl_sni", smp_fetch_ssl_sni, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
1565 { "ssl_verify_caerr", smp_fetch_verify_caerr, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
1566 { "ssl_verify_caerr_depth", smp_fetch_verify_caerr_depth, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
1567 { "ssl_verify_crterr", smp_fetch_verify_crterr, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
1568 { "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 +02001569 { NULL, NULL, 0, 0, 0 },
1570}};
1571
1572/* Note: must not be declared <const> as its list will be overwritten.
1573 * Please take care of keeping this list alphabetically sorted.
1574 */
1575static struct acl_kw_list acl_kws = {{ },{
Emeric Brunf282a812012-09-21 15:27:54 +02001576 { "client_crt", acl_parse_int, smp_fetch_client_crt, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1577 { "is_ssl", acl_parse_int, smp_fetch_is_ssl, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1578 { "ssl_has_sni", acl_parse_int, smp_fetch_has_sni, acl_match_nothing, ACL_USE_L6REQ_PERMANENT, 0 },
1579 { "ssl_sni", acl_parse_str, smp_fetch_ssl_sni, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1580 { "ssl_sni_end", acl_parse_str, smp_fetch_ssl_sni, acl_match_end, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1581 { "ssl_sni_reg", acl_parse_str, smp_fetch_ssl_sni, acl_match_reg, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1582 { "ssl_verify_caerr", acl_parse_int, smp_fetch_verify_caerr, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1583 { "ssl_verify_caerr_depth", acl_parse_int, smp_fetch_verify_caerr_depth, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1584 { "ssl_verify_crterr", acl_parse_int, smp_fetch_verify_crterr, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1585 { "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 +02001586 { NULL, NULL, NULL, NULL },
1587}};
1588
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001589/* Note: must not be declared <const> as its list will be overwritten.
1590 * Please take care of keeping this list alphabetically sorted, doing so helps
1591 * all code contributors.
1592 * Optional keywords are also declared with a NULL ->parse() function so that
1593 * the config parser can report an appropriate error when a known keyword was
1594 * not enabled.
1595 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001596static struct bind_kw_list bind_kws = { "SSL", { }, {
Emeric Brunfb510ea2012-10-05 12:00:26 +02001597 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
Emeric Brun2d0c4822012-10-02 13:45:20 +02001598 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
1599 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emeric Brunfb510ea2012-10-05 12:00:26 +02001600 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
Emeric Brun2d0c4822012-10-02 13:45:20 +02001601 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
1602 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
1603 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emeric Brun2cb7ae52012-10-05 14:14:21 +02001604 { "force-sslv3", bind_parse_force_sslv3, 0 }, /* force SSLv3 */
1605 { "force-tlsv10", bind_parse_force_tlsv10, 0 }, /* force TLSv10 */
1606 { "force-tlsv11", bind_parse_force_tlsv11, 0 }, /* force TLSv11 */
1607 { "force-tlsv12", bind_parse_force_tlsv12, 0 }, /* force TLSv12 */
Emeric Brun9b3009b2012-10-05 11:55:06 +02001608 { "no-sslv3", bind_parse_no_sslv3, 0 }, /* disable SSLv3 */
1609 { "no-tlsv10", bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */
1610 { "no-tlsv11", bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */
1611 { "no-tlsv12", bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */
Emeric Brun2d0c4822012-10-02 13:45:20 +02001612 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
Emeric Brun2d0c4822012-10-02 13:45:20 +02001613 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
1614 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001615 { NULL, NULL, 0 },
1616}};
Emeric Brun46591952012-05-18 15:47:34 +02001617
Willy Tarreau92faadf2012-10-10 23:04:25 +02001618/* Note: must not be declared <const> as its list will be overwritten.
1619 * Please take care of keeping this list alphabetically sorted, doing so helps
1620 * all code contributors.
1621 * Optional keywords are also declared with a NULL ->parse() function so that
1622 * the config parser can report an appropriate error when a known keyword was
1623 * not enabled.
1624 */
1625static struct srv_kw_list srv_kws = { "SSL", { }, {
Emeric Brunecc91fe2012-10-11 15:05:10 +02001626 { "check-ssl", srv_parse_check_ssl, 0, 0 }, /* enable SSL for health checks */
1627 { "ciphers", srv_parse_ciphers, 1, 0 }, /* select the cipher suite */
1628 { "force-sslv3", srv_parse_force_sslv3, 0, 0 }, /* force SSLv3 */
1629 { "force-tlsv10", srv_parse_force_tlsv10, 0, 0 }, /* force TLSv10 */
1630 { "force-tlsv11", srv_parse_force_tlsv11, 0, 0 }, /* force TLSv11 */
1631 { "force-tlsv12", srv_parse_force_tlsv12, 0, 0 }, /* force TLSv12 */
1632 { "no-sslv3", srv_parse_no_sslv3, 0, 0 }, /* disable SSLv3 */
1633 { "no-tlsv10", srv_parse_no_tlsv10, 0, 0 }, /* disable TLSv10 */
1634 { "no-tlsv11", srv_parse_no_tlsv11, 0, 0 }, /* disable TLSv11 */
1635 { "no-tlsv12", srv_parse_no_tlsv12, 0, 0 }, /* disable TLSv12 */
Emeric Brunf9c5c472012-10-11 15:28:34 +02001636 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 0 }, /* disable session resumption tickets */
Emeric Brunecc91fe2012-10-11 15:05:10 +02001637 { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */
Willy Tarreau92faadf2012-10-10 23:04:25 +02001638 { NULL, NULL, 0, 0 },
1639}};
1640
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001641/* transport-layer operations for SSL sockets */
1642struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +02001643 .snd_buf = ssl_sock_from_buf,
1644 .rcv_buf = ssl_sock_to_buf,
1645 .rcv_pipe = NULL,
1646 .snd_pipe = NULL,
1647 .shutr = NULL,
1648 .shutw = ssl_sock_shutw,
1649 .close = ssl_sock_close,
1650 .init = ssl_sock_init,
1651};
1652
1653__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +02001654static void __ssl_sock_init(void)
1655{
Emeric Brun46591952012-05-18 15:47:34 +02001656 STACK_OF(SSL_COMP)* cm;
1657
1658 SSL_library_init();
1659 cm = SSL_COMP_get_compression_methods();
1660 sk_SSL_COMP_zero(cm);
Willy Tarreau7875d092012-09-10 08:20:03 +02001661 sample_register_fetches(&sample_fetch_keywords);
1662 acl_register_keywords(&acl_kws);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001663 bind_register_keywords(&bind_kws);
Willy Tarreau92faadf2012-10-10 23:04:25 +02001664 srv_register_keywords(&srv_kws);
Emeric Brun46591952012-05-18 15:47:34 +02001665}
1666
1667/*
1668 * Local variables:
1669 * c-indent-level: 8
1670 * c-basic-offset: 8
1671 * End:
1672 */