blob: 18496d5e12fd2807181992c32f51ea3d4131de14 [file] [log] [blame]
Emeric Brun46591952012-05-18 15:47:34 +02001/*
2 * SSL data transfer functions between buffers and SOCK_STREAM sockets
3 *
4 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
Willy Tarreau69845df2012-09-10 09:43:09 +020011 * Acknowledgement:
12 * We'd like to specially thank the Stud project authors for a very clean
13 * and well documented code which helped us understand how the OpenSSL API
14 * ought to be used in non-blocking mode. This is one difficult part which
15 * is not easy to get from the OpenSSL doc, and reading the Stud code made
16 * it much more obvious than the examples in the OpenSSL package. Keep up
17 * the good works, guys !
18 *
19 * Stud is an extremely efficient and scalable SSL/TLS proxy which combines
20 * particularly well with haproxy. For more info about this project, visit :
21 * https://github.com/bumptech/stud
22 *
Emeric Brun46591952012-05-18 15:47:34 +020023 */
24
25#define _GNU_SOURCE
Emeric Brunfc0421f2012-09-07 17:30:07 +020026#include <ctype.h>
27#include <dirent.h>
Emeric Brun46591952012-05-18 15:47:34 +020028#include <errno.h>
29#include <fcntl.h>
30#include <stdio.h>
31#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020032#include <string.h>
33#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020034
35#include <sys/socket.h>
36#include <sys/stat.h>
37#include <sys/types.h>
38
39#include <netinet/tcp.h>
40
41#include <openssl/ssl.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020042#include <openssl/x509.h>
43#include <openssl/x509v3.h>
44#include <openssl/x509.h>
45#include <openssl/err.h>
Emeric Brun46591952012-05-18 15:47:34 +020046
47#include <common/buffer.h>
48#include <common/compat.h>
49#include <common/config.h>
50#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020051#include <common/errors.h>
Emeric Brun46591952012-05-18 15:47:34 +020052#include <common/standard.h>
53#include <common/ticks.h>
54#include <common/time.h>
55
Emeric Brunfc0421f2012-09-07 17:30:07 +020056#include <ebsttree.h>
57
58#include <types/global.h>
59#include <types/ssl_sock.h>
60
Willy Tarreau7875d092012-09-10 08:20:03 +020061#include <proto/acl.h>
62#include <proto/arg.h>
Emeric Brun46591952012-05-18 15:47:34 +020063#include <proto/connection.h>
64#include <proto/fd.h>
65#include <proto/freq_ctr.h>
66#include <proto/frontend.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020067#include <proto/listener.h>
Emeric Brun46591952012-05-18 15:47:34 +020068#include <proto/log.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020069#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020070#include <proto/ssl_sock.h>
71#include <proto/task.h>
72
Emeric Brune64aef12012-09-21 13:15:06 +020073#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brunf282a812012-09-21 15:27:54 +020074/* bits 0xFFFF0000 are reserved to store verify errors */
75
76/* Verify errors macros */
77#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
78#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
79#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
80
81#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
82#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
83#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +020084
Willy Tarreau403edff2012-09-06 11:58:37 +020085static int sslconns = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +020086
87void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
88{
89 struct connection *conn = (struct connection *)SSL_get_app_data(ssl);
90 (void)ret; /* shut gcc stupid warning */
91
92 if (where & SSL_CB_HANDSHAKE_START) {
93 /* Disable renegotiation (CVE-2009-3555) */
94 if (conn->flags & CO_FL_CONNECTED)
95 conn->flags |= CO_FL_ERROR;
96 }
Emeric Brunfc0421f2012-09-07 17:30:07 +020097}
98
Emeric Brune64aef12012-09-21 13:15:06 +020099/* Callback is called for each certificate of the chain during a verify
100 ok is set to 1 if preverify detect no error on current certificate.
101 Returns 0 to break the handshake, 1 otherwise. */
102int ssl_sock_verifycbk(int ok, X509_STORE_CTX *x_store)
103{
104 SSL *ssl;
105 struct connection *conn;
Emeric Brun81c00f02012-09-21 14:31:21 +0200106 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +0200107
108 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
109 conn = (struct connection *)SSL_get_app_data(ssl);
110
111 conn->data_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
112
Emeric Brun81c00f02012-09-21 14:31:21 +0200113 if (ok) /* no errors */
114 return ok;
115
116 depth = X509_STORE_CTX_get_error_depth(x_store);
117 err = X509_STORE_CTX_get_error(x_store);
118
119 /* check if CA error needs to be ignored */
120 if (depth > 0) {
Emeric Brunf282a812012-09-21 15:27:54 +0200121 if (!SSL_SOCK_ST_TO_CA_ERROR(conn->data_st)) {
122 conn->data_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
123 conn->data_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
124 }
125
Emeric Brun81c00f02012-09-21 14:31:21 +0200126 if (target_client(&conn->target)->bind_conf->ca_ignerr & (1ULL << err))
127 return 1;
128
129 return 0;
130 }
131
Emeric Brunf282a812012-09-21 15:27:54 +0200132 if (!SSL_SOCK_ST_TO_CRTERROR(conn->data_st))
133 conn->data_st |= SSL_SOCK_CRTERROR_TO_ST(err);
134
Emeric Brun81c00f02012-09-21 14:31:21 +0200135 /* check if certificate error needs to be ignored */
136 if (target_client(&conn->target)->bind_conf->crt_ignerr & (1ULL << err))
137 return 1;
138
139 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +0200140}
141
Emeric Brunfc0421f2012-09-07 17:30:07 +0200142#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
143/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
144 * warning when no match is found, which implies the default (first) cert
145 * will keep being used.
146 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200147static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200148{
149 const char *servername;
150 const char *wildp = NULL;
151 struct ebmb_node *node;
152 int i;
153 (void)al; /* shut gcc stupid warning */
154
155 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
156 if (!servername)
157 return SSL_TLSEXT_ERR_NOACK;
158
159 for (i = 0; i < trashlen; i++) {
160 if (!servername[i])
161 break;
162 trash[i] = tolower(servername[i]);
163 if (!wildp && (trash[i] == '.'))
164 wildp = &trash[i];
165 }
166 trash[i] = 0;
167
168 /* lookup in full qualified names */
169 node = ebst_lookup(&s->sni_ctx, trash);
170 if (!node) {
171 if (!wildp)
172 return SSL_TLSEXT_ERR_ALERT_WARNING;
173
174 /* lookup in full wildcards names */
175 node = ebst_lookup(&s->sni_w_ctx, wildp);
176 if (!node)
177 return SSL_TLSEXT_ERR_ALERT_WARNING;
178 }
179
180 /* switch ctx */
181 SSL_set_SSL_CTX(ssl, container_of(node, struct sni_ctx, name)->ctx);
182 return SSL_TLSEXT_ERR_OK;
183}
184#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
185
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200186#ifndef OPENSSL_NO_DH
187/* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1
188 if an error occured, and 0 if parameter not found. */
189int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file)
190{
191 int ret = -1;
192 BIO *in;
193 DH *dh = NULL;
194
195 in = BIO_new(BIO_s_file());
196 if (in == NULL)
197 goto end;
198
199 if (BIO_read_filename(in, file) <= 0)
200 goto end;
201
202 dh = PEM_read_bio_DHparams(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata);
203 if (dh) {
204 SSL_CTX_set_tmp_dh(ctx, dh);
205 ret = 1;
206 goto end;
207 }
208
209 ret = 0; /* DH params not found */
210end:
211 if (dh)
212 DH_free(dh);
213
214 if (in)
215 BIO_free(in);
216
217 return ret;
218}
219#endif
220
Emeric Brunfc0421f2012-09-07 17:30:07 +0200221/* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if
222 * an early error happens and the caller must call SSL_CTX_free() by itelf.
223 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200224int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200225{
226 BIO *in;
227 X509 *x = NULL, *ca;
228 int i, len, err;
229 int ret = -1;
230 int order = 0;
231 X509_NAME *xname;
232 char *str;
233 struct sni_ctx *sc;
234#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
235 STACK_OF(GENERAL_NAME) *names;
236#endif
237
238 in = BIO_new(BIO_s_file());
239 if (in == NULL)
240 goto end;
241
242 if (BIO_read_filename(in, file) <= 0)
243 goto end;
244
245 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata);
246 if (x == NULL)
247 goto end;
248
249#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
250 names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
251 if (names) {
252 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
253 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
254 if (name->type == GEN_DNS) {
255 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
256 if ((len = strlen(str))) {
257 int j;
258
259 if (*str != '*') {
260 sc = malloc(sizeof(struct sni_ctx) + len + 1);
261 for (j = 0; j < len; j++)
262 sc->name.key[j] = tolower(str[j]);
263 sc->name.key[len] = 0;
264 sc->order = order++;
265 sc->ctx = ctx;
266 ebst_insert(&s->sni_ctx, &sc->name);
267 }
268 else {
269 sc = malloc(sizeof(struct sni_ctx) + len);
270 for (j = 1; j < len; j++)
271 sc->name.key[j-1] = tolower(str[j]);
272 sc->name.key[len-1] = 0;
273 sc->order = order++;
274 sc->ctx = ctx;
275 ebst_insert(&s->sni_w_ctx, &sc->name);
276 }
277 }
278 OPENSSL_free(str);
279 }
280 }
281 }
282 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
283 }
284#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
285
286 xname = X509_get_subject_name(x);
287 i = -1;
288 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
289 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
290 if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) {
291 if ((len = strlen(str))) {
292 int j;
293
294 if (*str != '*') {
295 sc = malloc(sizeof(struct sni_ctx) + len + 1);
296 for (j = 0; j < len; j++)
297 sc->name.key[j] = tolower(str[j]);
298 sc->name.key[len] = 0;
299 sc->order = order++;
300 sc->ctx = ctx;
301 ebst_insert(&s->sni_ctx, &sc->name);
302 }
303 else {
304 sc = malloc(sizeof(struct sni_ctx) + len);
305 for (j = 1; j < len; j++)
306 sc->name.key[j-1] = tolower(str[j]);
307 sc->name.key[len-1] = 0;
308 sc->order = order++;
309 sc->ctx = ctx;
310 ebst_insert(&s->sni_w_ctx, &sc->name);
311 }
312 }
313 OPENSSL_free(str);
314 }
315 }
316
317 ret = 0; /* the caller must not free the SSL_CTX argument anymore */
318 if (!SSL_CTX_use_certificate(ctx, x))
319 goto end;
320
321 if (ctx->extra_certs != NULL) {
322 sk_X509_pop_free(ctx->extra_certs, X509_free);
323 ctx->extra_certs = NULL;
324 }
325
326 while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata))) {
327 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
328 X509_free(ca);
329 goto end;
330 }
331 }
332
333 err = ERR_get_error();
334 if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
335 /* we successfully reached the last cert in the file */
336 ret = 1;
337 }
338 ERR_clear_error();
339
340end:
341 if (x)
342 X509_free(x);
343
344 if (in)
345 BIO_free(in);
346
347 return ret;
348}
349
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200350static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200351{
352 int ret;
353 SSL_CTX *ctx;
354
355 ctx = SSL_CTX_new(SSLv23_server_method());
356 if (!ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200357 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
358 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200359 return 1;
360 }
361
362 if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200363 memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
364 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200365 SSL_CTX_free(ctx);
366 return 1;
367 }
368
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200369 ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200370 if (ret <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200371 memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
372 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200373 if (ret < 0) /* serious error, must do that ourselves */
374 SSL_CTX_free(ctx);
375 return 1;
376 }
377 /* we must not free the SSL_CTX anymore below, since it's already in
378 * the tree, so it will be discovered and cleaned in time.
379 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200380#ifndef OPENSSL_NO_DH
381 ret = ssl_sock_load_dh_params(ctx, path);
382 if (ret < 0) {
383 if (err)
384 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
385 *err ? *err : "", path);
386 return 1;
387 }
388#endif
389
Emeric Brunfc0421f2012-09-07 17:30:07 +0200390#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200391 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200392 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
393 err && *err ? *err : "");
Emeric Brunfc0421f2012-09-07 17:30:07 +0200394 return 1;
395 }
396#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200397 if (!bind_conf->default_ctx)
398 bind_conf->default_ctx = ctx;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200399
400 return 0;
401}
402
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200403int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200404{
405 struct dirent *de;
406 DIR *dir;
407 struct stat buf;
408 int pathlen = 0;
409 char *end, *fp;
410 int cfgerr = 0;
411
412 if (!(dir = opendir(path)))
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200413 return ssl_sock_load_cert_file(path, bind_conf, curproxy, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200414
415 /* strip trailing slashes, including first one */
416 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
417 *end = 0;
418
419 if (end >= path)
420 pathlen = end + 1 - path;
421 fp = malloc(pathlen + 1 + NAME_MAX + 1);
422
423 while ((de = readdir(dir))) {
424 snprintf(fp, pathlen + 1 + NAME_MAX + 1, "%s/%s", path, de->d_name);
425 if (stat(fp, &buf) != 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200426 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
427 err && *err ? *err : "", fp, strerror(errno));
Emeric Brunfc0421f2012-09-07 17:30:07 +0200428 cfgerr++;
429 continue;
430 }
431 if (!S_ISREG(buf.st_mode))
432 continue;
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200433 cfgerr += ssl_sock_load_cert_file(fp, bind_conf, curproxy, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200434 }
435 free(fp);
436 closedir(dir);
437 return cfgerr;
438}
439
440#ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */
441#define SSL_OP_CIPHER_SERVER_PREFERENCE 0
442#endif
443
444#ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */
445#define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0
446#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200447#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */
448#define SSL_OP_SINGLE_ECDH_USE 0
449#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200450#ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */
451#define SSL_OP_NO_COMPRESSION 0
452#endif
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200453#ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */
454#define SSL_OP_SINGLE_DH_USE 0
455#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200456#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */
457#define SSL_OP_SINGLE_ECDH_USE 0
458#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200459#ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */
460#define SSL_MODE_RELEASE_BUFFERS 0
461#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200462int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, SSL_CTX *ctx, struct proxy *curproxy)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200463{
464 int cfgerr = 0;
465 int ssloptions =
466 SSL_OP_ALL | /* all known workarounds for bugs */
467 SSL_OP_NO_SSLv2 |
468 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200469 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +0200470 SSL_OP_SINGLE_ECDH_USE |
Emeric Brunfc0421f2012-09-07 17:30:07 +0200471 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION;
472 int sslmode =
473 SSL_MODE_ENABLE_PARTIAL_WRITE |
474 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
475 SSL_MODE_RELEASE_BUFFERS;
476
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200477 if (bind_conf->nosslv3)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200478 ssloptions |= SSL_OP_NO_SSLv3;
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200479 if (bind_conf->notlsv1)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200480 ssloptions |= SSL_OP_NO_TLSv1;
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200481 if (bind_conf->prefer_server_ciphers)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200482 ssloptions |= SSL_OP_CIPHER_SERVER_PREFERENCE;
483
484 SSL_CTX_set_options(ctx, ssloptions);
485 SSL_CTX_set_mode(ctx, sslmode);
Emeric Brune64aef12012-09-21 13:15:06 +0200486 SSL_CTX_set_verify(ctx, bind_conf->verify ? bind_conf->verify : SSL_VERIFY_NONE, ssl_sock_verifycbk);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200487 if (bind_conf->verify & SSL_VERIFY_PEER) {
488 if (bind_conf->cafile) {
489 /* load CAfile to verify */
490 if (!SSL_CTX_load_verify_locations(ctx, bind_conf->cafile, NULL)) {
491 Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
492 curproxy->id, bind_conf->cafile, bind_conf->arg, bind_conf->file, bind_conf->line);
493 cfgerr++;
494 }
495 /* set CA names fo client cert request, function returns void */
496 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(bind_conf->cafile));
497 }
498
499 if (bind_conf->crlfile) {
500 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
501
502 if (!store || !X509_STORE_load_locations(store, bind_conf->crlfile, NULL)) {
503 Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
504 curproxy->id, bind_conf->cafile, bind_conf->arg, bind_conf->file, bind_conf->line);
505 cfgerr++;
506 }
507 }
508 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200509
510 shared_context_set_cache(ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200511 if (bind_conf->ciphers &&
512 !SSL_CTX_set_cipher_list(ctx, bind_conf->ciphers)) {
Emeric Brunfc0421f2012-09-07 17:30:07 +0200513 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 +0200514 curproxy->id, bind_conf->ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200515 cfgerr++;
516 }
517
518 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
519#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
520 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200521 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200522#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200523#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
524 if (bind_conf->ecdhe) {
525 int i;
526 EC_KEY *ecdh;
527
528 i = OBJ_sn2nid(bind_conf->ecdhe);
529 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
530 Alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
531 curproxy->id, bind_conf->ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
532 cfgerr++;
533 }
534 else {
535 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
536 EC_KEY_free(ecdh);
537 }
538 }
539#endif
540
Emeric Brunfc0421f2012-09-07 17:30:07 +0200541 return cfgerr;
542}
543
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200544/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +0200545 * be NULL, in which case nothing is done. Returns the number of errors
546 * encountered.
547 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200548int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf, struct proxy *px)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200549{
550 struct ebmb_node *node;
551 struct sni_ctx *sni;
552 int err = 0;
553
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200554 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200555 return 0;
556
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200557 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200558 while (node) {
559 sni = ebmb_entry(node, struct sni_ctx, name);
560 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200561 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200562 node = ebmb_next(node);
563 }
564
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200565 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200566 while (node) {
567 sni = ebmb_entry(node, struct sni_ctx, name);
568 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200569 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200570 node = ebmb_next(node);
571 }
572 return err;
573}
574
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200575/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +0200576 * be NULL, in which case nothing is done. The default_ctx is nullified too.
577 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200578void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200579{
580 struct ebmb_node *node, *back;
581 struct sni_ctx *sni;
582
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200583 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200584 return;
585
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200586 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200587 while (node) {
588 sni = ebmb_entry(node, struct sni_ctx, name);
589 back = ebmb_next(node);
590 ebmb_delete(node);
591 if (!sni->order) /* only free the CTX on its first occurrence */
592 SSL_CTX_free(sni->ctx);
593 free(sni);
594 node = back;
595 }
596
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200597 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200598 while (node) {
599 sni = ebmb_entry(node, struct sni_ctx, name);
600 back = ebmb_next(node);
601 ebmb_delete(node);
602 if (!sni->order) /* only free the CTX on its first occurrence */
603 SSL_CTX_free(sni->ctx);
604 free(sni);
605 node = back;
606 }
607
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200608 bind_conf->default_ctx = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +0200609}
610
Emeric Brun46591952012-05-18 15:47:34 +0200611/*
612 * This function is called if SSL * context is not yet allocated. The function
613 * is designed to be called before any other data-layer operation and sets the
614 * handshake flag on the connection. It is safe to call it multiple times.
615 * It returns 0 on success and -1 in error case.
616 */
617static int ssl_sock_init(struct connection *conn)
618{
619 /* already initialized */
620 if (conn->data_ctx)
621 return 0;
622
Willy Tarreau403edff2012-09-06 11:58:37 +0200623 if (global.maxsslconn && sslconns >= global.maxsslconn)
624 return -1;
625
Emeric Brun46591952012-05-18 15:47:34 +0200626 /* If it is in client mode initiate SSL session
627 in connect state otherwise accept state */
628 if (target_srv(&conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +0200629 /* Alloc a new SSL session ctx */
630 conn->data_ctx = SSL_new(target_srv(&conn->target)->ssl_ctx.ctx);
631 if (!conn->data_ctx)
632 return -1;
633
634 SSL_set_connect_state(conn->data_ctx);
635 if (target_srv(&conn->target)->ssl_ctx.reused_sess)
636 SSL_set_session(conn->data_ctx, target_srv(&conn->target)->ssl_ctx.reused_sess);
637
638 /* set fd on SSL session context */
639 SSL_set_fd(conn->data_ctx, conn->t.sock.fd);
640
641 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +0200642 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +0200643
644 sslconns++;
Emeric Brun46591952012-05-18 15:47:34 +0200645 return 0;
646 }
647 else if (target_client(&conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +0200648 /* Alloc a new SSL session ctx */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200649 conn->data_ctx = SSL_new(target_client(&conn->target)->bind_conf->default_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200650 if (!conn->data_ctx)
651 return -1;
652
653 SSL_set_accept_state(conn->data_ctx);
654
655 /* set fd on SSL session context */
656 SSL_set_fd(conn->data_ctx, conn->t.sock.fd);
657
Emeric Brune1f38db2012-09-03 20:36:47 +0200658 /* set connection pointer */
659 SSL_set_app_data(conn->data_ctx, conn);
660
Emeric Brun46591952012-05-18 15:47:34 +0200661 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +0200662 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +0200663
664 sslconns++;
Emeric Brun46591952012-05-18 15:47:34 +0200665 return 0;
666 }
667 /* don't know how to handle such a target */
668 return -1;
669}
670
671
672/* This is the callback which is used when an SSL handshake is pending. It
673 * updates the FD status if it wants some polling before being called again.
674 * It returns 0 if it fails in a fatal way or needs to poll to go further,
675 * otherwise it returns non-zero and removes itself from the connection's
676 * flags (the bit is provided in <flag> by the caller).
677 */
678int ssl_sock_handshake(struct connection *conn, unsigned int flag)
679{
680 int ret;
681
682 if (!conn->data_ctx)
683 goto out_error;
684
685 ret = SSL_do_handshake(conn->data_ctx);
686 if (ret != 1) {
687 /* handshake did not complete, let's find why */
688 ret = SSL_get_error(conn->data_ctx, ret);
689
690 if (ret == SSL_ERROR_WANT_WRITE) {
691 /* SSL handshake needs to write, L4 connection may not be ready */
692 __conn_sock_stop_recv(conn);
693 __conn_sock_poll_send(conn);
694 return 0;
695 }
696 else if (ret == SSL_ERROR_WANT_READ) {
697 /* SSL handshake needs to read, L4 connection is ready */
698 if (conn->flags & CO_FL_WAIT_L4_CONN)
699 conn->flags &= ~CO_FL_WAIT_L4_CONN;
700 __conn_sock_stop_send(conn);
701 __conn_sock_poll_recv(conn);
702 return 0;
703 }
704 else {
705 /* Fail on all other handshake errors */
706 goto out_error;
707 }
708 }
709
710 /* Handshake succeeded */
711 if (target_srv(&conn->target)) {
712 if (!SSL_session_reused(conn->data_ctx)) {
713 /* check if session was reused, if not store current session on server for reuse */
714 if (target_srv(&conn->target)->ssl_ctx.reused_sess)
715 SSL_SESSION_free(target_srv(&conn->target)->ssl_ctx.reused_sess);
716
717 target_srv(&conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->data_ctx);
718 }
719 }
720
721 /* The connection is now established at both layers, it's time to leave */
722 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
723 return 1;
724
725 out_error:
726 /* Fail on all other handshake errors */
727 conn->flags |= CO_FL_ERROR;
728 conn->flags &= ~flag;
729 return 0;
730}
731
732/* Receive up to <count> bytes from connection <conn>'s socket and store them
733 * into buffer <buf>. The caller must ensure that <count> is always smaller
734 * than the buffer's size. Only one call to recv() is performed, unless the
735 * buffer wraps, in which case a second call may be performed. The connection's
736 * flags are updated with whatever special event is detected (error, read0,
737 * empty). The caller is responsible for taking care of those events and
738 * avoiding the call if inappropriate. The function does not call the
739 * connection's polling update function, so the caller is responsible for this.
740 */
741static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
742{
743 int ret, done = 0;
744 int try = count;
745
746 if (!conn->data_ctx)
747 goto out_error;
748
749 if (conn->flags & CO_FL_HANDSHAKE)
750 /* a handshake was requested */
751 return 0;
752
753 /* compute the maximum block size we can read at once. */
754 if (buffer_empty(buf)) {
755 /* let's realign the buffer to optimize I/O */
756 buf->p = buf->data;
757 }
758 else if (buf->data + buf->o < buf->p &&
759 buf->p + buf->i < buf->data + buf->size) {
760 /* remaining space wraps at the end, with a moving limit */
761 if (try > buf->data + buf->size - (buf->p + buf->i))
762 try = buf->data + buf->size - (buf->p + buf->i);
763 }
764
765 /* read the largest possible block. For this, we perform only one call
766 * to recv() unless the buffer wraps and we exactly fill the first hunk,
767 * in which case we accept to do it once again. A new attempt is made on
768 * EINTR too.
769 */
770 while (try) {
771 ret = SSL_read(conn->data_ctx, bi_end(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +0200772 if (conn->flags & CO_FL_ERROR) {
773 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
774 break;
775 }
Emeric Brun46591952012-05-18 15:47:34 +0200776 if (ret > 0) {
777 buf->i += ret;
778 done += ret;
779 if (ret < try)
780 break;
781 count -= ret;
782 try = count;
783 }
784 else if (ret == 0) {
785 goto read0;
786 }
787 else {
788 ret = SSL_get_error(conn->data_ctx, ret);
789 if (ret == SSL_ERROR_WANT_WRITE) {
790 /* handshake is running, and it needs to poll for a write event */
791 conn->flags |= CO_FL_SSL_WAIT_HS;
792 __conn_sock_poll_send(conn);
793 break;
794 }
795 else if (ret == SSL_ERROR_WANT_READ) {
796 /* we need to poll for retry a read later */
797 __conn_data_poll_recv(conn);
798 break;
799 }
800 /* otherwise it's a real error */
801 goto out_error;
802 }
803 }
804 return done;
805
806 read0:
807 conn_sock_read0(conn);
808 return done;
809 out_error:
810 conn->flags |= CO_FL_ERROR;
811 return done;
812}
813
814
815/* Send all pending bytes from buffer <buf> to connection <conn>'s socket.
816 * <flags> may contain MSG_MORE to make the system hold on without sending
817 * data too fast, but this flag is ignored at the moment.
818 * Only one call to send() is performed, unless the buffer wraps, in which case
819 * a second call may be performed. The connection's flags are updated with
820 * whatever special event is detected (error, empty). The caller is responsible
821 * for taking care of those events and avoiding the call if inappropriate. The
822 * function does not call the connection's polling update function, so the caller
823 * is responsible for this.
824 */
825static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags)
826{
827 int ret, try, done;
828
829 done = 0;
830
831 if (!conn->data_ctx)
832 goto out_error;
833
834 if (conn->flags & CO_FL_HANDSHAKE)
835 /* a handshake was requested */
836 return 0;
837
838 /* send the largest possible block. For this we perform only one call
839 * to send() unless the buffer wraps and we exactly fill the first hunk,
840 * in which case we accept to do it once again.
841 */
842 while (buf->o) {
843 try = buf->o;
844 /* outgoing data may wrap at the end */
845 if (buf->data + try > buf->p)
846 try = buf->data + try - buf->p;
847
848 ret = SSL_write(conn->data_ctx, bo_ptr(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +0200849 if (conn->flags & CO_FL_ERROR) {
850 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
851 break;
852 }
Emeric Brun46591952012-05-18 15:47:34 +0200853 if (ret > 0) {
854 buf->o -= ret;
855 done += ret;
856
857 if (likely(!buffer_len(buf)))
858 /* optimize data alignment in the buffer */
859 buf->p = buf->data;
860
861 /* if the system buffer is full, don't insist */
862 if (ret < try)
863 break;
864 }
865 else {
866 ret = SSL_get_error(conn->data_ctx, ret);
867 if (ret == SSL_ERROR_WANT_WRITE) {
868 /* we need to poll to retry a write later */
869 __conn_data_poll_send(conn);
870 break;
871 }
872 else if (ret == SSL_ERROR_WANT_READ) {
873 /* handshake is running, and
874 it needs to poll for a read event,
875 write polling must be disabled cause
876 we are sure we can't write anything more
877 before handshake re-performed */
878 conn->flags |= CO_FL_SSL_WAIT_HS;
879 __conn_sock_poll_recv(conn);
880 break;
881 }
882 goto out_error;
883 }
884 }
885 return done;
886
887 out_error:
888 conn->flags |= CO_FL_ERROR;
889 return done;
890}
891
892
893static void ssl_sock_close(struct connection *conn) {
894
895 if (conn->data_ctx) {
896 SSL_free(conn->data_ctx);
897 conn->data_ctx = NULL;
Willy Tarreau403edff2012-09-06 11:58:37 +0200898 sslconns--;
Emeric Brun46591952012-05-18 15:47:34 +0200899 }
Emeric Brun46591952012-05-18 15:47:34 +0200900}
901
902/* This function tries to perform a clean shutdown on an SSL connection, and in
903 * any case, flags the connection as reusable if no handshake was in progress.
904 */
905static void ssl_sock_shutw(struct connection *conn, int clean)
906{
907 if (conn->flags & CO_FL_HANDSHAKE)
908 return;
909 /* no handshake was in progress, try a clean ssl shutdown */
910 if (clean)
911 SSL_shutdown(conn->data_ctx);
912
913 /* force flag on ssl to keep session in cache regardless shutdown result */
914 SSL_set_shutdown(conn->data_ctx, SSL_SENT_SHUTDOWN);
915}
916
Willy Tarreau7875d092012-09-10 08:20:03 +0200917/***** Below are some sample fetching functions for ACL/patterns *****/
918
Emeric Brune64aef12012-09-21 13:15:06 +0200919/* boolean, returns true if client cert was present */
920static int
921smp_fetch_client_crt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
922 const struct arg *args, struct sample *smp)
923{
924 if (!l4 || l4->si[0].conn.data != &ssl_sock)
925 return 0;
926
927 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
928 smp->flags |= SMP_F_MAY_CHANGE;
929 return 0;
930 }
931
932 smp->flags = 0;
933 smp->type = SMP_T_BOOL;
934 smp->data.uint = SSL_SOCK_ST_FL_VERIFY_DONE & l4->si[0].conn.data_st ? 1 : 0;
935
936 return 1;
937}
938
939
Willy Tarreau7875d092012-09-10 08:20:03 +0200940/* boolean, returns true if data layer is SSL */
941static int
942smp_fetch_is_ssl(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
943 const struct arg *args, struct sample *smp)
944{
945 smp->type = SMP_T_BOOL;
946 smp->data.uint = (l4->si[0].conn.data == &ssl_sock);
947 return 1;
948}
949
950/* boolean, returns true if data layer is SSL */
951static int
952smp_fetch_has_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
953 const struct arg *args, struct sample *smp)
954{
955#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
956 smp->type = SMP_T_BOOL;
957 smp->data.uint = (l4->si[0].conn.data == &ssl_sock) &&
Willy Tarreau3e394c92012-09-14 23:56:58 +0200958 l4->si[0].conn.data_ctx &&
Willy Tarreau7875d092012-09-10 08:20:03 +0200959 SSL_get_servername(l4->si[0].conn.data_ctx, TLSEXT_NAMETYPE_host_name) != NULL;
960 return 1;
961#else
962 return 0;
963#endif
964}
965
966static int
967smp_fetch_ssl_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
968 const struct arg *args, struct sample *smp)
969{
970#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
971 smp->flags = 0;
972 smp->type = SMP_T_CSTR;
973
Willy Tarreau3e394c92012-09-14 23:56:58 +0200974 if (!l4 || !l4->si[0].conn.data_ctx || l4->si[0].conn.data != &ssl_sock)
Willy Tarreau7875d092012-09-10 08:20:03 +0200975 return 0;
976
Willy Tarreau7875d092012-09-10 08:20:03 +0200977 smp->data.str.str = (char *)SSL_get_servername(l4->si[0].conn.data_ctx, TLSEXT_NAMETYPE_host_name);
Willy Tarreau3e394c92012-09-14 23:56:58 +0200978 if (!smp->data.str.str)
979 return 0;
980
Willy Tarreau7875d092012-09-10 08:20:03 +0200981 smp->data.str.len = strlen(smp->data.str.str);
982 return 1;
983#else
984 return 0;
985#endif
986}
987
Emeric Brunf282a812012-09-21 15:27:54 +0200988/* integer, returns the first verify error ID in CA */
989static int
990smp_fetch_verify_caerr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
991 const struct arg *args, struct sample *smp)
992{
993 if (!l4 || l4->si[0].conn.data != &ssl_sock)
994 return 0;
995
996 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
997 smp->flags = SMP_F_MAY_CHANGE;
998 return 0;
999 }
1000
1001 smp->type = SMP_T_UINT;
1002 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CA_ERROR(l4->si[0].conn.data_st);
1003 smp->flags = 0;
1004
1005 return 1;
1006}
1007
1008/* integer, returns the depth of the first verify error in CA */
1009static int
1010smp_fetch_verify_caerr_depth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1011 const struct arg *args, struct sample *smp)
1012{
1013 if (!l4 || l4->si[0].conn.data != &ssl_sock)
1014 return 0;
1015
1016 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1017 smp->flags = SMP_F_MAY_CHANGE;
1018 return 0;
1019 }
1020
1021 smp->type = SMP_T_UINT;
1022 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CAEDEPTH(l4->si[0].conn.data_st);
1023 smp->flags = 0;
1024
1025 return 1;
1026}
1027
1028/* integer, returns the depth of the first verify error in CA */
1029static int
1030smp_fetch_verify_crterr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1031 const struct arg *args, struct sample *smp)
1032{
1033 if (!l4 || l4->si[0].conn.data != &ssl_sock)
1034 return 0;
1035
1036 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1037 smp->flags = SMP_F_MAY_CHANGE;
1038 return 0;
1039 }
1040
1041 smp->type = SMP_T_UINT;
1042 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CRTERROR(l4->si[0].conn.data_st);
1043 smp->flags = 0;
1044
1045 return 1;
1046}
1047
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02001048/* integer, returns the verify result */
1049static int
1050smp_fetch_verify_result(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1051 const struct arg *args, struct sample *smp)
1052{
1053 if (!l4 || l4->si[0].conn.data != &ssl_sock)
1054 return 0;
1055
1056 if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) {
1057 smp->flags = SMP_F_MAY_CHANGE;
1058 return 0;
1059 }
1060
1061 if (!l4->si[0].conn.data_ctx)
1062 return 0;
1063
1064 smp->type = SMP_T_UINT;
1065 smp->data.uint = (unsigned int)SSL_get_verify_result(l4->si[0].conn.data_ctx);
1066 smp->flags = 0;
1067
1068 return 1;
1069}
1070
Emeric Brund94b3fe2012-09-20 18:23:56 +02001071/* parse the "cafile" bind keyword */
1072static int bind_parse_cafile(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1073{
1074 if (!*args[cur_arg + 1]) {
1075 if (err)
1076 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
1077 return ERR_ALERT | ERR_FATAL;
1078 }
1079
1080 conf->cafile = strdup(args[cur_arg + 1]);
1081 return 0;
1082}
1083
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001084/* parse the "ciphers" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001085static 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 +02001086{
1087 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001088 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001089 return ERR_ALERT | ERR_FATAL;
1090 }
1091
Willy Tarreau4348fad2012-09-20 16:48:07 +02001092 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001093 return 0;
1094}
1095
1096/* parse the "crt" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001097static 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 +02001098{
1099 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001100 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001101 return ERR_ALERT | ERR_FATAL;
1102 }
1103
Willy Tarreau4348fad2012-09-20 16:48:07 +02001104 if (ssl_sock_load_cert(args[cur_arg + 1], conf, px, err) > 0)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001105 return ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02001106
1107 return 0;
1108}
1109
1110/* parse the "crlfile" bind keyword */
1111static int bind_parse_crlfile(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1112{
1113 if (!*args[cur_arg + 1]) {
1114 if (err)
1115 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
1116 return ERR_ALERT | ERR_FATAL;
1117 }
Emeric Brun2b58d042012-09-20 17:10:03 +02001118
Emeric Brund94b3fe2012-09-20 18:23:56 +02001119 conf->crlfile = strdup(args[cur_arg + 1]);
Emeric Brun2b58d042012-09-20 17:10:03 +02001120 return 0;
1121}
1122
1123/* parse the "ecdhe" bind keyword keywords */
1124static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1125{
1126#if OPENSSL_VERSION_NUMBER < 0x0090800fL
1127 if (err)
1128 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
1129 return ERR_ALERT | ERR_FATAL;
1130#elif defined(OPENSSL_NO_ECDH)
1131 if (err)
1132 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
1133 return ERR_ALERT | ERR_FATAL;
1134#else
1135 if (!*args[cur_arg + 1]) {
1136 if (err)
1137 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
1138 return ERR_ALERT | ERR_FATAL;
1139 }
1140
1141 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001142
1143 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02001144#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001145}
1146
Emeric Brun81c00f02012-09-21 14:31:21 +02001147/* parse the "crt_ignerr" and "ca_ignerr" bind keywords */
1148static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1149{
1150 int code;
1151 char *p = args[cur_arg + 1];
1152 unsigned long long *ignerr = &conf->crt_ignerr;
1153
1154 if (!*p) {
1155 if (err)
1156 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
1157 return ERR_ALERT | ERR_FATAL;
1158 }
1159
1160 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
1161 ignerr = &conf->ca_ignerr;
1162
1163 if (strcmp(p, "all") == 0) {
1164 *ignerr = ~0ULL;
1165 return 0;
1166 }
1167
1168 while (p) {
1169 code = atoi(p);
1170 if ((code <= 0) || (code > 63)) {
1171 if (err)
1172 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
1173 args[cur_arg], code, args[cur_arg + 1]);
1174 return ERR_ALERT | ERR_FATAL;
1175 }
1176 *ignerr |= 1ULL << code;
1177 p = strchr(p, ',');
1178 if (p)
1179 p++;
1180 }
1181
1182 return 0;
1183}
1184
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001185/* parse the "nosslv3" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001186static int bind_parse_nosslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001187{
Willy Tarreau4348fad2012-09-20 16:48:07 +02001188 conf->nosslv3 = 1;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001189 return 0;
1190}
1191
1192/* parse the "notlsv1" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001193static int bind_parse_notlsv1(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001194{
Willy Tarreau4348fad2012-09-20 16:48:07 +02001195 conf->notlsv1 = 1;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001196 return 0;
1197}
1198
1199/* parse the "prefer-server-ciphers" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001200static int bind_parse_psc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001201{
Willy Tarreau4348fad2012-09-20 16:48:07 +02001202 conf->prefer_server_ciphers = 1;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001203 return 0;
1204}
1205
1206/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001207static 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 +02001208{
Willy Tarreau81796be2012-09-22 19:11:47 +02001209 struct listener *l;
1210
Willy Tarreau4348fad2012-09-20 16:48:07 +02001211 conf->is_ssl = 1;
Willy Tarreau81796be2012-09-22 19:11:47 +02001212 list_for_each_entry(l, &conf->listeners, by_bind)
1213 l->data = &ssl_sock;
1214
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001215 return 0;
1216}
1217
Emeric Brund94b3fe2012-09-20 18:23:56 +02001218/* parse the "verify" bind keyword */
1219static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1220{
1221 if (!*args[cur_arg + 1]) {
1222 if (err)
1223 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
1224 return ERR_ALERT | ERR_FATAL;
1225 }
1226
1227 if (strcmp(args[cur_arg + 1], "none") == 0)
1228 conf->verify = SSL_VERIFY_NONE;
1229 else if (strcmp(args[cur_arg + 1], "optional") == 0)
1230 conf->verify = SSL_VERIFY_PEER;
1231 else if (strcmp(args[cur_arg + 1], "required") == 0)
1232 conf->verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1233 else {
1234 if (err)
1235 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
1236 args[cur_arg], args[cur_arg + 1]);
1237 return ERR_ALERT | ERR_FATAL;
1238 }
1239
1240 return 0;
1241}
1242
Willy Tarreau7875d092012-09-10 08:20:03 +02001243/* Note: must not be declared <const> as its list will be overwritten.
1244 * Please take care of keeping this list alphabetically sorted.
1245 */
1246static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Emeric Brunf282a812012-09-21 15:27:54 +02001247 { "client_crt", smp_fetch_client_crt, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
1248 { "is_ssl", smp_fetch_is_ssl, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
1249 { "ssl_has_sni", smp_fetch_has_sni, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
1250 { "ssl_sni", smp_fetch_ssl_sni, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
1251 { "ssl_verify_caerr", smp_fetch_verify_caerr, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
1252 { "ssl_verify_caerr_depth", smp_fetch_verify_caerr_depth, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
1253 { "ssl_verify_crterr", smp_fetch_verify_crterr, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
1254 { "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 +02001255 { NULL, NULL, 0, 0, 0 },
1256}};
1257
1258/* Note: must not be declared <const> as its list will be overwritten.
1259 * Please take care of keeping this list alphabetically sorted.
1260 */
1261static struct acl_kw_list acl_kws = {{ },{
Emeric Brunf282a812012-09-21 15:27:54 +02001262 { "client_crt", acl_parse_int, smp_fetch_client_crt, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1263 { "is_ssl", acl_parse_int, smp_fetch_is_ssl, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1264 { "ssl_has_sni", acl_parse_int, smp_fetch_has_sni, acl_match_nothing, ACL_USE_L6REQ_PERMANENT, 0 },
1265 { "ssl_sni", acl_parse_str, smp_fetch_ssl_sni, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1266 { "ssl_sni_end", acl_parse_str, smp_fetch_ssl_sni, acl_match_end, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1267 { "ssl_sni_reg", acl_parse_str, smp_fetch_ssl_sni, acl_match_reg, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1268 { "ssl_verify_caerr", acl_parse_int, smp_fetch_verify_caerr, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1269 { "ssl_verify_caerr_depth", acl_parse_int, smp_fetch_verify_caerr_depth, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1270 { "ssl_verify_crterr", acl_parse_int, smp_fetch_verify_crterr, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
1271 { "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 +02001272 { NULL, NULL, NULL, NULL },
1273}};
1274
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001275/* Note: must not be declared <const> as its list will be overwritten.
1276 * Please take care of keeping this list alphabetically sorted, doing so helps
1277 * all code contributors.
1278 * Optional keywords are also declared with a NULL ->parse() function so that
1279 * the config parser can report an appropriate error when a known keyword was
1280 * not enabled.
1281 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001282static struct bind_kw_list bind_kws = { "SSL", { }, {
Emeric Brund94b3fe2012-09-20 18:23:56 +02001283 { "cafile", bind_parse_cafile, 1 }, /* set CAfile to process verify on client cert */
Emeric Brun81c00f02012-09-21 14:31:21 +02001284 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
Emeric Brun2b58d042012-09-20 17:10:03 +02001285 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emeric Brund94b3fe2012-09-20 18:23:56 +02001286 { "crlfile", bind_parse_crlfile, 1 }, /* set certificat revocation list file use on client cert verify */
Emeric Brun2b58d042012-09-20 17:10:03 +02001287 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
Emeric Brun81c00f02012-09-21 14:31:21 +02001288 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
Emeric Brun2b58d042012-09-20 17:10:03 +02001289 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
1290 { "nosslv3", bind_parse_nosslv3, 0 }, /* disable SSLv3 */
1291 { "notlsv1", bind_parse_notlsv1, 0 }, /* disable TLSv1 */
1292 { "prefer-server-ciphers", bind_parse_psc, 0 }, /* prefer server ciphers */
1293 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emeric Brund94b3fe2012-09-20 18:23:56 +02001294 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001295 { NULL, NULL, 0 },
1296}};
Emeric Brun46591952012-05-18 15:47:34 +02001297
1298/* data-layer operations for SSL sockets */
1299struct data_ops ssl_sock = {
1300 .snd_buf = ssl_sock_from_buf,
1301 .rcv_buf = ssl_sock_to_buf,
1302 .rcv_pipe = NULL,
1303 .snd_pipe = NULL,
1304 .shutr = NULL,
1305 .shutw = ssl_sock_shutw,
1306 .close = ssl_sock_close,
1307 .init = ssl_sock_init,
1308};
1309
1310__attribute__((constructor))
1311static void __ssl_sock_init(void) {
1312 STACK_OF(SSL_COMP)* cm;
1313
1314 SSL_library_init();
1315 cm = SSL_COMP_get_compression_methods();
1316 sk_SSL_COMP_zero(cm);
Willy Tarreau7875d092012-09-10 08:20:03 +02001317 sample_register_fetches(&sample_fetch_keywords);
1318 acl_register_keywords(&acl_kws);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02001319 bind_register_keywords(&bind_kws);
Emeric Brun46591952012-05-18 15:47:34 +02001320}
1321
1322/*
1323 * Local variables:
1324 * c-indent-level: 8
1325 * c-basic-offset: 8
1326 * End:
1327 */