blob: 7415b754a5131cab1e01e3e3bf6f6ed23b000c04 [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) */
Willy Tarreau20879a02012-12-03 16:32:10 +010096 if (conn->flags & CO_FL_CONNECTED) {
Emeric Brune1f38db2012-09-03 20:36:47 +020097 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +010098 conn->err_code = CO_ER_SSL_RENEG;
99 }
Emeric Brune1f38db2012-09-03 20:36:47 +0200100 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200101}
102
Emeric Brune64aef12012-09-21 13:15:06 +0200103/* Callback is called for each certificate of the chain during a verify
104 ok is set to 1 if preverify detect no error on current certificate.
105 Returns 0 to break the handshake, 1 otherwise. */
106int ssl_sock_verifycbk(int ok, X509_STORE_CTX *x_store)
107{
108 SSL *ssl;
109 struct connection *conn;
Emeric Brun81c00f02012-09-21 14:31:21 +0200110 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +0200111
112 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
113 conn = (struct connection *)SSL_get_app_data(ssl);
114
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200115 conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +0200116
Emeric Brun81c00f02012-09-21 14:31:21 +0200117 if (ok) /* no errors */
118 return ok;
119
120 depth = X509_STORE_CTX_get_error_depth(x_store);
121 err = X509_STORE_CTX_get_error(x_store);
122
123 /* check if CA error needs to be ignored */
124 if (depth > 0) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200125 if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) {
126 conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
127 conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +0200128 }
129
Emeric Brun1eb20ef2012-12-03 13:24:29 +0100130 if (objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
131 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +0200132 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +0100133 }
Emeric Brun81c00f02012-09-21 14:31:21 +0200134
Willy Tarreau20879a02012-12-03 16:32:10 +0100135 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +0200136 return 0;
137 }
138
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200139 if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st))
140 conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +0200141
Emeric Brun81c00f02012-09-21 14:31:21 +0200142 /* check if certificate error needs to be ignored */
Emeric Brun1eb20ef2012-12-03 13:24:29 +0100143 if (objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
144 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +0200145 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +0100146 }
Emeric Brun81c00f02012-09-21 14:31:21 +0200147
Willy Tarreau20879a02012-12-03 16:32:10 +0100148 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +0200149 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +0200150}
151
Willy Tarreau6c9a3d52012-10-18 18:57:14 +0200152#ifdef OPENSSL_NPN_NEGOTIATED
153/* This callback is used so that the server advertises the list of
154 * negociable protocols for NPN.
155 */
156static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
157 unsigned int *len, void *arg)
158{
159 struct bind_conf *conf = arg;
160
161 *data = (const unsigned char *)conf->npn_str;
162 *len = conf->npn_len;
163 return SSL_TLSEXT_ERR_OK;
164}
165#endif
166
Emeric Brunfc0421f2012-09-07 17:30:07 +0200167#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
168/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
169 * warning when no match is found, which implies the default (first) cert
170 * will keep being used.
171 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200172static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200173{
174 const char *servername;
175 const char *wildp = NULL;
176 struct ebmb_node *node;
177 int i;
178 (void)al; /* shut gcc stupid warning */
179
180 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
181 if (!servername)
182 return SSL_TLSEXT_ERR_NOACK;
183
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100184 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +0200185 if (!servername[i])
186 break;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100187 trash.str[i] = tolower(servername[i]);
188 if (!wildp && (trash.str[i] == '.'))
189 wildp = &trash.str[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +0200190 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100191 trash.str[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200192
193 /* lookup in full qualified names */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100194 node = ebst_lookup(&s->sni_ctx, trash.str);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200195 if (!node) {
196 if (!wildp)
197 return SSL_TLSEXT_ERR_ALERT_WARNING;
198
199 /* lookup in full wildcards names */
200 node = ebst_lookup(&s->sni_w_ctx, wildp);
201 if (!node)
202 return SSL_TLSEXT_ERR_ALERT_WARNING;
203 }
204
205 /* switch ctx */
206 SSL_set_SSL_CTX(ssl, container_of(node, struct sni_ctx, name)->ctx);
207 return SSL_TLSEXT_ERR_OK;
208}
209#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
210
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200211#ifndef OPENSSL_NO_DH
212/* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1
213 if an error occured, and 0 if parameter not found. */
214int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file)
215{
216 int ret = -1;
217 BIO *in;
218 DH *dh = NULL;
219
220 in = BIO_new(BIO_s_file());
221 if (in == NULL)
222 goto end;
223
224 if (BIO_read_filename(in, file) <= 0)
225 goto end;
226
227 dh = PEM_read_bio_DHparams(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata);
228 if (dh) {
229 SSL_CTX_set_tmp_dh(ctx, dh);
230 ret = 1;
231 goto end;
232 }
233
234 ret = 0; /* DH params not found */
Emeric Brun644cde02012-12-14 11:21:13 +0100235
236 /* Clear openssl global errors stack */
237 ERR_clear_error();
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200238end:
239 if (dh)
240 DH_free(dh);
241
242 if (in)
243 BIO_free(in);
244
245 return ret;
246}
247#endif
248
Emeric Brunfc0421f2012-09-07 17:30:07 +0200249/* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if
250 * an early error happens and the caller must call SSL_CTX_free() by itelf.
251 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200252int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200253{
254 BIO *in;
255 X509 *x = NULL, *ca;
256 int i, len, err;
257 int ret = -1;
258 int order = 0;
259 X509_NAME *xname;
260 char *str;
261 struct sni_ctx *sc;
262#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
263 STACK_OF(GENERAL_NAME) *names;
264#endif
265
266 in = BIO_new(BIO_s_file());
267 if (in == NULL)
268 goto end;
269
270 if (BIO_read_filename(in, file) <= 0)
271 goto end;
272
273 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata);
274 if (x == NULL)
275 goto end;
276
277#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
278 names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
279 if (names) {
280 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
281 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
282 if (name->type == GEN_DNS) {
283 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
284 if ((len = strlen(str))) {
285 int j;
286
287 if (*str != '*') {
288 sc = malloc(sizeof(struct sni_ctx) + len + 1);
289 for (j = 0; j < len; j++)
290 sc->name.key[j] = tolower(str[j]);
291 sc->name.key[len] = 0;
292 sc->order = order++;
293 sc->ctx = ctx;
294 ebst_insert(&s->sni_ctx, &sc->name);
295 }
296 else {
297 sc = malloc(sizeof(struct sni_ctx) + len);
298 for (j = 1; j < len; j++)
299 sc->name.key[j-1] = tolower(str[j]);
300 sc->name.key[len-1] = 0;
301 sc->order = order++;
302 sc->ctx = ctx;
303 ebst_insert(&s->sni_w_ctx, &sc->name);
304 }
305 }
306 OPENSSL_free(str);
307 }
308 }
309 }
310 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
311 }
312#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
313
314 xname = X509_get_subject_name(x);
315 i = -1;
316 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
317 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
318 if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) {
319 if ((len = strlen(str))) {
320 int j;
321
322 if (*str != '*') {
323 sc = malloc(sizeof(struct sni_ctx) + len + 1);
324 for (j = 0; j < len; j++)
325 sc->name.key[j] = tolower(str[j]);
326 sc->name.key[len] = 0;
327 sc->order = order++;
328 sc->ctx = ctx;
329 ebst_insert(&s->sni_ctx, &sc->name);
330 }
331 else {
332 sc = malloc(sizeof(struct sni_ctx) + len);
333 for (j = 1; j < len; j++)
334 sc->name.key[j-1] = tolower(str[j]);
335 sc->name.key[len-1] = 0;
336 sc->order = order++;
337 sc->ctx = ctx;
338 ebst_insert(&s->sni_w_ctx, &sc->name);
339 }
340 }
341 OPENSSL_free(str);
342 }
343 }
344
345 ret = 0; /* the caller must not free the SSL_CTX argument anymore */
346 if (!SSL_CTX_use_certificate(ctx, x))
347 goto end;
348
349 if (ctx->extra_certs != NULL) {
350 sk_X509_pop_free(ctx->extra_certs, X509_free);
351 ctx->extra_certs = NULL;
352 }
353
354 while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata))) {
355 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
356 X509_free(ca);
357 goto end;
358 }
359 }
360
361 err = ERR_get_error();
362 if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
363 /* we successfully reached the last cert in the file */
364 ret = 1;
365 }
366 ERR_clear_error();
367
368end:
369 if (x)
370 X509_free(x);
371
372 if (in)
373 BIO_free(in);
374
375 return ret;
376}
377
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200378static 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 +0200379{
380 int ret;
381 SSL_CTX *ctx;
382
383 ctx = SSL_CTX_new(SSLv23_server_method());
384 if (!ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200385 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
386 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200387 return 1;
388 }
389
390 if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200391 memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
392 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200393 SSL_CTX_free(ctx);
394 return 1;
395 }
396
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200397 ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200398 if (ret <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200399 memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
400 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200401 if (ret < 0) /* serious error, must do that ourselves */
402 SSL_CTX_free(ctx);
403 return 1;
404 }
Emeric Brun61694ab2012-10-26 13:35:33 +0200405
406 if (SSL_CTX_check_private_key(ctx) <= 0) {
407 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
408 err && *err ? *err : "", path);
409 return 1;
410 }
411
Emeric Brunfc0421f2012-09-07 17:30:07 +0200412 /* we must not free the SSL_CTX anymore below, since it's already in
413 * the tree, so it will be discovered and cleaned in time.
414 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200415#ifndef OPENSSL_NO_DH
416 ret = ssl_sock_load_dh_params(ctx, path);
417 if (ret < 0) {
418 if (err)
419 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
420 *err ? *err : "", path);
421 return 1;
422 }
423#endif
424
Emeric Brunfc0421f2012-09-07 17:30:07 +0200425#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200426 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200427 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
428 err && *err ? *err : "");
Emeric Brunfc0421f2012-09-07 17:30:07 +0200429 return 1;
430 }
431#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200432 if (!bind_conf->default_ctx)
433 bind_conf->default_ctx = ctx;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200434
435 return 0;
436}
437
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200438int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200439{
440 struct dirent *de;
441 DIR *dir;
442 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +0100443 char *end;
444 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +0200445 int cfgerr = 0;
446
447 if (!(dir = opendir(path)))
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200448 return ssl_sock_load_cert_file(path, bind_conf, curproxy, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200449
450 /* strip trailing slashes, including first one */
451 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
452 *end = 0;
453
Emeric Brunfc0421f2012-09-07 17:30:07 +0200454 while ((de = readdir(dir))) {
Willy Tarreauee2663b2012-12-06 11:36:59 +0100455 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200456 if (stat(fp, &buf) != 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200457 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
458 err && *err ? *err : "", fp, strerror(errno));
Emeric Brunfc0421f2012-09-07 17:30:07 +0200459 cfgerr++;
460 continue;
461 }
462 if (!S_ISREG(buf.st_mode))
463 continue;
Willy Tarreau79eeafa2012-09-14 07:53:05 +0200464 cfgerr += ssl_sock_load_cert_file(fp, bind_conf, curproxy, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200465 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200466 closedir(dir);
467 return cfgerr;
468}
469
470#ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */
471#define SSL_OP_CIPHER_SERVER_PREFERENCE 0
472#endif
473
474#ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */
475#define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0
Willy Tarreau7d588ee2012-11-26 18:47:31 +0100476#define SSL_renegotiate_pending(arg) 0
Emeric Brunfc0421f2012-09-07 17:30:07 +0200477#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200478#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */
479#define SSL_OP_SINGLE_ECDH_USE 0
480#endif
Emeric Brun2d0c4822012-10-02 13:45:20 +0200481#ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */
482#define SSL_OP_NO_TICKET 0
483#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200484#ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */
485#define SSL_OP_NO_COMPRESSION 0
486#endif
Emeric Brunc0ff4922012-09-28 19:37:02 +0200487#ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */
488#define SSL_OP_NO_TLSv1_1 0
489#endif
490#ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */
491#define SSL_OP_NO_TLSv1_2 0
492#endif
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200493#ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */
494#define SSL_OP_SINGLE_DH_USE 0
495#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200496#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */
497#define SSL_OP_SINGLE_ECDH_USE 0
498#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200499#ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */
500#define SSL_MODE_RELEASE_BUFFERS 0
501#endif
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200502int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, SSL_CTX *ctx, struct proxy *curproxy)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200503{
504 int cfgerr = 0;
505 int ssloptions =
506 SSL_OP_ALL | /* all known workarounds for bugs */
507 SSL_OP_NO_SSLv2 |
508 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +0200509 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +0200510 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +0200511 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
512 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emeric Brunfc0421f2012-09-07 17:30:07 +0200513 int sslmode =
514 SSL_MODE_ENABLE_PARTIAL_WRITE |
515 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
516 SSL_MODE_RELEASE_BUFFERS;
517
Emeric Brun89675492012-10-05 13:48:26 +0200518 if (bind_conf->ssl_options & BC_SSL_O_NO_SSLV3)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200519 ssloptions |= SSL_OP_NO_SSLv3;
Emeric Brun89675492012-10-05 13:48:26 +0200520 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV10)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200521 ssloptions |= SSL_OP_NO_TLSv1;
Emeric Brun89675492012-10-05 13:48:26 +0200522 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV11)
Emeric Brunc0ff4922012-09-28 19:37:02 +0200523 ssloptions |= SSL_OP_NO_TLSv1_1;
Emeric Brun89675492012-10-05 13:48:26 +0200524 if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV12)
Emeric Brunc0ff4922012-09-28 19:37:02 +0200525 ssloptions |= SSL_OP_NO_TLSv1_2;
Emeric Brun89675492012-10-05 13:48:26 +0200526 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
Emeric Brun2d0c4822012-10-02 13:45:20 +0200527 ssloptions |= SSL_OP_NO_TICKET;
Emeric Brun2cb7ae52012-10-05 14:14:21 +0200528 if (bind_conf->ssl_options & BC_SSL_O_USE_SSLV3)
529 SSL_CTX_set_ssl_version(ctx, SSLv3_server_method());
530 if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV10)
531 SSL_CTX_set_ssl_version(ctx, TLSv1_server_method());
532#if SSL_OP_NO_TLSv1_1
533 if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV11)
534 SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method());
535#endif
536#if SSL_OP_NO_TLSv1_2
537 if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV12)
538 SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method());
539#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +0200540
541 SSL_CTX_set_options(ctx, ssloptions);
542 SSL_CTX_set_mode(ctx, sslmode);
Emeric Brune64aef12012-09-21 13:15:06 +0200543 SSL_CTX_set_verify(ctx, bind_conf->verify ? bind_conf->verify : SSL_VERIFY_NONE, ssl_sock_verifycbk);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200544 if (bind_conf->verify & SSL_VERIFY_PEER) {
Emeric Brunfb510ea2012-10-05 12:00:26 +0200545 if (bind_conf->ca_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200546 /* load CAfile to verify */
Emeric Brunfb510ea2012-10-05 12:00:26 +0200547 if (!SSL_CTX_load_verify_locations(ctx, bind_conf->ca_file, NULL)) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200548 Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
Emeric Brunfb510ea2012-10-05 12:00:26 +0200549 curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200550 cfgerr++;
551 }
552 /* set CA names fo client cert request, function returns void */
Emeric Brunfb510ea2012-10-05 12:00:26 +0200553 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(bind_conf->ca_file));
Emeric Brund94b3fe2012-09-20 18:23:56 +0200554 }
Emeric Brun051cdab2012-10-02 19:25:50 +0200555#ifdef X509_V_FLAG_CRL_CHECK
Emeric Brunfb510ea2012-10-05 12:00:26 +0200556 if (bind_conf->crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200557 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
558
Emeric Brunfb510ea2012-10-05 12:00:26 +0200559 if (!store || !X509_STORE_load_locations(store, bind_conf->crl_file, NULL)) {
Emeric Brund94b3fe2012-09-20 18:23:56 +0200560 Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
Emeric Brunfb510ea2012-10-05 12:00:26 +0200561 curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +0200562 cfgerr++;
563 }
Emeric Brun561e5742012-10-02 15:20:55 +0200564 else {
565 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
566 }
Emeric Brund94b3fe2012-09-20 18:23:56 +0200567 }
Emeric Brun051cdab2012-10-02 19:25:50 +0200568#endif
Emeric Brun644cde02012-12-14 11:21:13 +0100569 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +0200570 }
Emeric Brunfc0421f2012-09-07 17:30:07 +0200571
Emeric Brun4f65bff2012-11-16 15:11:00 +0100572 if (global.tune.ssllifetime)
573 SSL_CTX_set_timeout(ctx, global.tune.ssllifetime);
574
Emeric Brunfc0421f2012-09-07 17:30:07 +0200575 shared_context_set_cache(ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200576 if (bind_conf->ciphers &&
577 !SSL_CTX_set_cipher_list(ctx, bind_conf->ciphers)) {
Emeric Brunfc0421f2012-09-07 17:30:07 +0200578 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 +0200579 curproxy->id, bind_conf->ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200580 cfgerr++;
581 }
582
583 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +0200584#ifdef OPENSSL_NPN_NEGOTIATED
585 if (bind_conf->npn_str)
586 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, bind_conf);
587#endif
588
Emeric Brunfc0421f2012-09-07 17:30:07 +0200589#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
590 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200591 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200592#endif
Emeric Brun2b58d042012-09-20 17:10:03 +0200593#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
594 if (bind_conf->ecdhe) {
595 int i;
596 EC_KEY *ecdh;
597
598 i = OBJ_sn2nid(bind_conf->ecdhe);
599 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
600 Alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
601 curproxy->id, bind_conf->ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
602 cfgerr++;
603 }
604 else {
605 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
606 EC_KEY_free(ecdh);
607 }
608 }
609#endif
610
Emeric Brunfc0421f2012-09-07 17:30:07 +0200611 return cfgerr;
612}
613
Emeric Brun94324a42012-10-11 14:00:19 +0200614/* prepare ssl context from servers options. Returns an error count */
615int ssl_sock_prepare_srv_ctx(struct server *srv, struct proxy *curproxy)
616{
617 int cfgerr = 0;
618 int options =
619 SSL_OP_ALL | /* all known workarounds for bugs */
620 SSL_OP_NO_SSLv2 |
621 SSL_OP_NO_COMPRESSION;
622 int mode =
623 SSL_MODE_ENABLE_PARTIAL_WRITE |
624 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
625 SSL_MODE_RELEASE_BUFFERS;
626
627 /* Initiate SSL context for current server */
628 srv->ssl_ctx.reused_sess = NULL;
629 if (srv->use_ssl)
630 srv->xprt = &ssl_sock;
631 if (srv->check.use_ssl)
632 srv->check.xprt = &ssl_sock;
633
634 srv->ssl_ctx.ctx = SSL_CTX_new(SSLv23_client_method());
635 if (!srv->ssl_ctx.ctx) {
636 Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
637 proxy_type_str(curproxy), curproxy->id,
638 srv->id);
639 cfgerr++;
640 return cfgerr;
641 }
Emeric Bruna7aa3092012-10-26 12:58:00 +0200642 if (srv->ssl_ctx.client_crt) {
643 if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) {
644 Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
645 proxy_type_str(curproxy), curproxy->id,
646 srv->id, srv->ssl_ctx.client_crt);
647 cfgerr++;
648 }
649 else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) {
650 Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
651 proxy_type_str(curproxy), curproxy->id,
652 srv->id, srv->ssl_ctx.client_crt);
653 cfgerr++;
654 }
655 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
656 Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
657 proxy_type_str(curproxy), curproxy->id,
658 srv->id, srv->ssl_ctx.client_crt);
659 cfgerr++;
660 }
661 }
Emeric Brun94324a42012-10-11 14:00:19 +0200662
663 if (srv->ssl_ctx.options & SRV_SSL_O_NO_SSLV3)
664 options |= SSL_OP_NO_SSLv3;
665 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV10)
666 options |= SSL_OP_NO_TLSv1;
667 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV11)
668 options |= SSL_OP_NO_TLSv1_1;
669 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV12)
670 options |= SSL_OP_NO_TLSv1_2;
Emeric Brunf9c5c472012-10-11 15:28:34 +0200671 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
672 options |= SSL_OP_NO_TICKET;
Emeric Brun94324a42012-10-11 14:00:19 +0200673 if (srv->ssl_ctx.options & SRV_SSL_O_USE_SSLV3)
674 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, SSLv3_client_method());
675 if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV10)
676 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_client_method());
677#if SSL_OP_NO_TLSv1_1
678 if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV11)
679 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_1_client_method());
680#endif
681#if SSL_OP_NO_TLSv1_2
682 if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV12)
683 SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_2_client_method());
684#endif
685
686 SSL_CTX_set_options(srv->ssl_ctx.ctx, options);
687 SSL_CTX_set_mode(srv->ssl_ctx.ctx, mode);
Emeric Brunef42d922012-10-11 16:11:36 +0200688 SSL_CTX_set_verify(srv->ssl_ctx.ctx, srv->ssl_ctx.verify ? srv->ssl_ctx.verify : SSL_VERIFY_NONE, NULL);
689 if (srv->ssl_ctx.verify & SSL_VERIFY_PEER) {
690 if (srv->ssl_ctx.ca_file) {
691 /* load CAfile to verify */
692 if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) {
693 Alert("Proxy '%s', server '%s' |%s:%d] unable to load CA file '%s'.\n",
694 curproxy->id, srv->id,
695 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
696 cfgerr++;
697 }
698 }
699#ifdef X509_V_FLAG_CRL_CHECK
700 if (srv->ssl_ctx.crl_file) {
701 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
702
703 if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) {
704 Alert("Proxy '%s', server '%s' |%s:%d] unable to configure CRL file '%s'.\n",
705 curproxy->id, srv->id,
706 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
707 cfgerr++;
708 }
709 else {
710 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
711 }
712 }
713#endif
714 }
715
Emeric Brun4f65bff2012-11-16 15:11:00 +0100716 if (global.tune.ssllifetime)
717 SSL_CTX_set_timeout(srv->ssl_ctx.ctx, global.tune.ssllifetime);
718
Emeric Brun94324a42012-10-11 14:00:19 +0200719 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF);
720 if (srv->ssl_ctx.ciphers &&
721 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
722 Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
723 curproxy->id, srv->id,
724 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
725 cfgerr++;
726 }
727
728 return cfgerr;
729}
730
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200731/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +0200732 * be NULL, in which case nothing is done. Returns the number of errors
733 * encountered.
734 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200735int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf, struct proxy *px)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200736{
737 struct ebmb_node *node;
738 struct sni_ctx *sni;
739 int err = 0;
740
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200741 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200742 return 0;
743
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200744 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200745 while (node) {
746 sni = ebmb_entry(node, struct sni_ctx, name);
747 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200748 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200749 node = ebmb_next(node);
750 }
751
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200752 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200753 while (node) {
754 sni = ebmb_entry(node, struct sni_ctx, name);
755 if (!sni->order) /* only initialize the CTX on its first occurrence */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200756 err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200757 node = ebmb_next(node);
758 }
759 return err;
760}
761
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200762/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +0200763 * be NULL, in which case nothing is done. The default_ctx is nullified too.
764 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200765void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200766{
767 struct ebmb_node *node, *back;
768 struct sni_ctx *sni;
769
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200770 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +0200771 return;
772
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200773 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200774 while (node) {
775 sni = ebmb_entry(node, struct sni_ctx, name);
776 back = ebmb_next(node);
777 ebmb_delete(node);
778 if (!sni->order) /* only free the CTX on its first occurrence */
779 SSL_CTX_free(sni->ctx);
780 free(sni);
781 node = back;
782 }
783
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200784 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +0200785 while (node) {
786 sni = ebmb_entry(node, struct sni_ctx, name);
787 back = ebmb_next(node);
788 ebmb_delete(node);
789 if (!sni->order) /* only free the CTX on its first occurrence */
790 SSL_CTX_free(sni->ctx);
791 free(sni);
792 node = back;
793 }
794
Willy Tarreau2a65ff02012-09-13 17:54:29 +0200795 bind_conf->default_ctx = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +0200796}
797
Emeric Brun46591952012-05-18 15:47:34 +0200798/*
799 * This function is called if SSL * context is not yet allocated. The function
800 * is designed to be called before any other data-layer operation and sets the
801 * handshake flag on the connection. It is safe to call it multiple times.
802 * It returns 0 on success and -1 in error case.
803 */
804static int ssl_sock_init(struct connection *conn)
805{
806 /* already initialized */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200807 if (conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200808 return 0;
809
Willy Tarreau20879a02012-12-03 16:32:10 +0100810 if (global.maxsslconn && sslconns >= global.maxsslconn) {
811 conn->err_code = CO_ER_SSL_TOO_MANY;
Willy Tarreau403edff2012-09-06 11:58:37 +0200812 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +0100813 }
Willy Tarreau403edff2012-09-06 11:58:37 +0200814
Emeric Brun46591952012-05-18 15:47:34 +0200815 /* If it is in client mode initiate SSL session
816 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100817 if (objt_server(conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +0200818 /* Alloc a new SSL session ctx */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100819 conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx);
Willy Tarreau20879a02012-12-03 16:32:10 +0100820 if (!conn->xprt_ctx) {
821 conn->err_code = CO_ER_SSL_NO_MEM;
Emeric Brun46591952012-05-18 15:47:34 +0200822 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +0100823 }
Emeric Brun46591952012-05-18 15:47:34 +0200824
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200825 SSL_set_connect_state(conn->xprt_ctx);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100826 if (objt_server(conn->target)->ssl_ctx.reused_sess)
827 SSL_set_session(conn->xprt_ctx, objt_server(conn->target)->ssl_ctx.reused_sess);
Emeric Brun46591952012-05-18 15:47:34 +0200828
829 /* set fd on SSL session context */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200830 SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +0200831
832 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +0200833 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +0200834
835 sslconns++;
Emeric Brun46591952012-05-18 15:47:34 +0200836 return 0;
837 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100838 else if (objt_listener(conn->target)) {
Emeric Brun46591952012-05-18 15:47:34 +0200839 /* Alloc a new SSL session ctx */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100840 conn->xprt_ctx = SSL_new(objt_listener(conn->target)->bind_conf->default_ctx);
Willy Tarreau20879a02012-12-03 16:32:10 +0100841 if (!conn->xprt_ctx) {
842 conn->err_code = CO_ER_SSL_NO_MEM;
Emeric Brun46591952012-05-18 15:47:34 +0200843 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +0100844 }
Emeric Brun46591952012-05-18 15:47:34 +0200845
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200846 SSL_set_accept_state(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200847
848 /* set fd on SSL session context */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200849 SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +0200850
Emeric Brune1f38db2012-09-03 20:36:47 +0200851 /* set connection pointer */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200852 SSL_set_app_data(conn->xprt_ctx, conn);
Emeric Brune1f38db2012-09-03 20:36:47 +0200853
Emeric Brun46591952012-05-18 15:47:34 +0200854 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +0200855 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +0200856
857 sslconns++;
Emeric Brun46591952012-05-18 15:47:34 +0200858 return 0;
859 }
860 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +0100861 conn->err_code = CO_ER_SSL_NO_TARGET;
Emeric Brun46591952012-05-18 15:47:34 +0200862 return -1;
863}
864
865
866/* This is the callback which is used when an SSL handshake is pending. It
867 * updates the FD status if it wants some polling before being called again.
868 * It returns 0 if it fails in a fatal way or needs to poll to go further,
869 * otherwise it returns non-zero and removes itself from the connection's
870 * flags (the bit is provided in <flag> by the caller).
871 */
872int ssl_sock_handshake(struct connection *conn, unsigned int flag)
873{
874 int ret;
875
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200876 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +0200877 goto out_error;
878
Emeric Brun674b7432012-11-08 19:21:55 +0100879 /* If we use SSL_do_handshake to process a reneg initiated by
880 * the remote peer, it sometimes returns SSL_ERROR_SSL.
881 * Usually SSL_write and SSL_read are used and process implicitly
882 * the reneg handshake.
883 * Here we use SSL_peek as a workaround for reneg.
884 */
885 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) {
886 char c;
887
888 ret = SSL_peek(conn->xprt_ctx, &c, 1);
889 if (ret <= 0) {
890 /* handshake may have not been completed, let's find why */
891 ret = SSL_get_error(conn->xprt_ctx, ret);
892 if (ret == SSL_ERROR_WANT_WRITE) {
893 /* SSL handshake needs to write, L4 connection may not be ready */
894 __conn_sock_stop_recv(conn);
895 __conn_sock_poll_send(conn);
896 return 0;
897 }
898 else if (ret == SSL_ERROR_WANT_READ) {
899 /* handshake may have been completed but we have
900 * no more data to read.
901 */
902 if (!SSL_renegotiate_pending(conn->xprt_ctx)) {
903 ret = 1;
904 goto reneg_ok;
905 }
906 /* SSL handshake needs to read, L4 connection is ready */
907 if (conn->flags & CO_FL_WAIT_L4_CONN)
908 conn->flags &= ~CO_FL_WAIT_L4_CONN;
909 __conn_sock_stop_send(conn);
910 __conn_sock_poll_recv(conn);
911 return 0;
912 }
913 else if (ret == SSL_ERROR_SYSCALL) {
914 /* if errno is null, then connection was successfully established */
915 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
916 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +0100917 if (!conn->err_code) {
918 if (!((SSL *)conn->xprt_ctx)->packet_length)
919 if (!errno)
920 conn->err_code = CO_ER_SSL_EMPTY;
921 else
922 conn->err_code = CO_ER_SSL_ABORT;
923 else
924 conn->err_code = CO_ER_SSL_HANDSHAKE;
925 }
Emeric Brun674b7432012-11-08 19:21:55 +0100926 goto out_error;
927 }
928 else {
929 /* Fail on all other handshake errors */
930 /* Note: OpenSSL may leave unread bytes in the socket's
931 * buffer, causing an RST to be emitted upon close() on
932 * TCP sockets. We first try to drain possibly pending
933 * data to avoid this as much as possible.
934 */
935 ret = recv(conn->t.sock.fd, trash.str, trash.size, MSG_NOSIGNAL|MSG_DONTWAIT);
Willy Tarreau20879a02012-12-03 16:32:10 +0100936 if (!conn->err_code)
937 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +0100938 goto out_error;
939 }
940 }
941 /* read some data: consider handshake completed */
942 goto reneg_ok;
943 }
944
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200945 ret = SSL_do_handshake(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +0200946 if (ret != 1) {
947 /* handshake did not complete, let's find why */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200948 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +0200949
950 if (ret == SSL_ERROR_WANT_WRITE) {
951 /* SSL handshake needs to write, L4 connection may not be ready */
952 __conn_sock_stop_recv(conn);
953 __conn_sock_poll_send(conn);
954 return 0;
955 }
956 else if (ret == SSL_ERROR_WANT_READ) {
957 /* SSL handshake needs to read, L4 connection is ready */
958 if (conn->flags & CO_FL_WAIT_L4_CONN)
959 conn->flags &= ~CO_FL_WAIT_L4_CONN;
960 __conn_sock_stop_send(conn);
961 __conn_sock_poll_recv(conn);
962 return 0;
963 }
Willy Tarreau89230192012-09-28 20:22:13 +0200964 else if (ret == SSL_ERROR_SYSCALL) {
965 /* if errno is null, then connection was successfully established */
966 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
967 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +0100968
969 if (!((SSL *)conn->xprt_ctx)->packet_length)
970 if (!errno)
971 conn->err_code = CO_ER_SSL_EMPTY;
972 else
973 conn->err_code = CO_ER_SSL_ABORT;
974 else
975 conn->err_code = CO_ER_SSL_HANDSHAKE;
Willy Tarreau89230192012-09-28 20:22:13 +0200976 goto out_error;
977 }
Emeric Brun46591952012-05-18 15:47:34 +0200978 else {
979 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +0200980 /* Note: OpenSSL may leave unread bytes in the socket's
981 * buffer, causing an RST to be emitted upon close() on
982 * TCP sockets. We first try to drain possibly pending
983 * data to avoid this as much as possible.
984 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100985 ret = recv(conn->t.sock.fd, trash.str, trash.size, MSG_NOSIGNAL|MSG_DONTWAIT);
Willy Tarreau20879a02012-12-03 16:32:10 +0100986 if (!conn->err_code)
987 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +0200988 goto out_error;
989 }
990 }
991
Emeric Brun674b7432012-11-08 19:21:55 +0100992reneg_ok:
993
Emeric Brun46591952012-05-18 15:47:34 +0200994 /* Handshake succeeded */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100995 if (objt_server(conn->target)) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200996 if (!SSL_session_reused(conn->xprt_ctx)) {
Emeric Brun46591952012-05-18 15:47:34 +0200997 /* check if session was reused, if not store current session on server for reuse */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100998 if (objt_server(conn->target)->ssl_ctx.reused_sess)
999 SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess);
Emeric Brun46591952012-05-18 15:47:34 +02001000
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001001 objt_server(conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +02001002 }
1003 }
1004
1005 /* The connection is now established at both layers, it's time to leave */
1006 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
1007 return 1;
1008
1009 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01001010 /* Clear openssl global errors stack */
1011 ERR_clear_error();
1012
Emeric Brun9fa89732012-10-04 17:09:56 +02001013 /* free resumed session if exists */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001014 if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) {
1015 SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess);
1016 objt_server(conn->target)->ssl_ctx.reused_sess = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02001017 }
1018
Emeric Brun46591952012-05-18 15:47:34 +02001019 /* Fail on all other handshake errors */
1020 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001021 if (!conn->err_code)
1022 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02001023 return 0;
1024}
1025
1026/* Receive up to <count> bytes from connection <conn>'s socket and store them
1027 * into buffer <buf>. The caller must ensure that <count> is always smaller
1028 * than the buffer's size. Only one call to recv() is performed, unless the
1029 * buffer wraps, in which case a second call may be performed. The connection's
1030 * flags are updated with whatever special event is detected (error, read0,
1031 * empty). The caller is responsible for taking care of those events and
1032 * avoiding the call if inappropriate. The function does not call the
1033 * connection's polling update function, so the caller is responsible for this.
1034 */
1035static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
1036{
1037 int ret, done = 0;
1038 int try = count;
1039
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001040 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02001041 goto out_error;
1042
1043 if (conn->flags & CO_FL_HANDSHAKE)
1044 /* a handshake was requested */
1045 return 0;
1046
1047 /* compute the maximum block size we can read at once. */
1048 if (buffer_empty(buf)) {
1049 /* let's realign the buffer to optimize I/O */
1050 buf->p = buf->data;
1051 }
1052 else if (buf->data + buf->o < buf->p &&
1053 buf->p + buf->i < buf->data + buf->size) {
1054 /* remaining space wraps at the end, with a moving limit */
1055 if (try > buf->data + buf->size - (buf->p + buf->i))
1056 try = buf->data + buf->size - (buf->p + buf->i);
1057 }
1058
1059 /* read the largest possible block. For this, we perform only one call
1060 * to recv() unless the buffer wraps and we exactly fill the first hunk,
1061 * in which case we accept to do it once again. A new attempt is made on
1062 * EINTR too.
1063 */
1064 while (try) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001065 ret = SSL_read(conn->xprt_ctx, bi_end(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +02001066 if (conn->flags & CO_FL_ERROR) {
1067 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01001068 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02001069 }
Emeric Brun46591952012-05-18 15:47:34 +02001070 if (ret > 0) {
1071 buf->i += ret;
1072 done += ret;
1073 if (ret < try)
1074 break;
1075 count -= ret;
1076 try = count;
1077 }
1078 else if (ret == 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01001079 ret = SSL_get_error(conn->xprt_ctx, ret);
1080 if (ret != SSL_ERROR_ZERO_RETURN) {
1081 /* Clear openssl global errors stack */
1082 ERR_clear_error();
1083 }
Emeric Brun46591952012-05-18 15:47:34 +02001084 goto read0;
1085 }
1086 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001087 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +02001088 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01001089 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02001090 conn->flags |= CO_FL_SSL_WAIT_HS;
Emeric Brun8af8dd12012-11-08 17:56:20 +01001091 __conn_sock_want_send(conn);
Emeric Brun46591952012-05-18 15:47:34 +02001092 break;
1093 }
1094 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun282a76a2012-11-08 18:02:56 +01001095 if (SSL_renegotiate_pending(conn->xprt_ctx)) {
1096 /* handshake is running, and it may need to re-enable read */
1097 conn->flags |= CO_FL_SSL_WAIT_HS;
1098 __conn_sock_want_recv(conn);
1099 break;
1100 }
Emeric Brun46591952012-05-18 15:47:34 +02001101 /* we need to poll for retry a read later */
1102 __conn_data_poll_recv(conn);
1103 break;
1104 }
1105 /* otherwise it's a real error */
1106 goto out_error;
1107 }
1108 }
1109 return done;
1110
1111 read0:
1112 conn_sock_read0(conn);
1113 return done;
1114 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01001115 /* Clear openssl global errors stack */
1116 ERR_clear_error();
1117
Emeric Brun46591952012-05-18 15:47:34 +02001118 conn->flags |= CO_FL_ERROR;
1119 return done;
1120}
1121
1122
1123/* Send all pending bytes from buffer <buf> to connection <conn>'s socket.
1124 * <flags> may contain MSG_MORE to make the system hold on without sending
1125 * data too fast, but this flag is ignored at the moment.
1126 * Only one call to send() is performed, unless the buffer wraps, in which case
1127 * a second call may be performed. The connection's flags are updated with
1128 * whatever special event is detected (error, empty). The caller is responsible
1129 * for taking care of those events and avoiding the call if inappropriate. The
1130 * function does not call the connection's polling update function, so the caller
1131 * is responsible for this.
1132 */
1133static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags)
1134{
1135 int ret, try, done;
1136
1137 done = 0;
1138
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001139 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02001140 goto out_error;
1141
1142 if (conn->flags & CO_FL_HANDSHAKE)
1143 /* a handshake was requested */
1144 return 0;
1145
1146 /* send the largest possible block. For this we perform only one call
1147 * to send() unless the buffer wraps and we exactly fill the first hunk,
1148 * in which case we accept to do it once again.
1149 */
1150 while (buf->o) {
1151 try = buf->o;
1152 /* outgoing data may wrap at the end */
1153 if (buf->data + try > buf->p)
1154 try = buf->data + try - buf->p;
1155
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001156 ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +02001157 if (conn->flags & CO_FL_ERROR) {
1158 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01001159 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02001160 }
Emeric Brun46591952012-05-18 15:47:34 +02001161 if (ret > 0) {
1162 buf->o -= ret;
1163 done += ret;
1164
1165 if (likely(!buffer_len(buf)))
1166 /* optimize data alignment in the buffer */
1167 buf->p = buf->data;
1168
1169 /* if the system buffer is full, don't insist */
1170 if (ret < try)
1171 break;
1172 }
1173 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001174 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +02001175 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun282a76a2012-11-08 18:02:56 +01001176 if (SSL_renegotiate_pending(conn->xprt_ctx)) {
1177 /* handshake is running, and it may need to re-enable write */
1178 conn->flags |= CO_FL_SSL_WAIT_HS;
1179 __conn_sock_want_send(conn);
1180 break;
1181 }
Emeric Brun46591952012-05-18 15:47:34 +02001182 /* we need to poll to retry a write later */
1183 __conn_data_poll_send(conn);
1184 break;
1185 }
1186 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01001187 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02001188 conn->flags |= CO_FL_SSL_WAIT_HS;
Emeric Brun8af8dd12012-11-08 17:56:20 +01001189 __conn_sock_want_recv(conn);
Emeric Brun46591952012-05-18 15:47:34 +02001190 break;
1191 }
1192 goto out_error;
1193 }
1194 }
1195 return done;
1196
1197 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01001198 /* Clear openssl global errors stack */
1199 ERR_clear_error();
1200
Emeric Brun46591952012-05-18 15:47:34 +02001201 conn->flags |= CO_FL_ERROR;
1202 return done;
1203}
1204
Emeric Brun46591952012-05-18 15:47:34 +02001205static void ssl_sock_close(struct connection *conn) {
1206
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001207 if (conn->xprt_ctx) {
1208 SSL_free(conn->xprt_ctx);
1209 conn->xprt_ctx = NULL;
Willy Tarreau403edff2012-09-06 11:58:37 +02001210 sslconns--;
Emeric Brun46591952012-05-18 15:47:34 +02001211 }
Emeric Brun46591952012-05-18 15:47:34 +02001212}
1213
1214/* This function tries to perform a clean shutdown on an SSL connection, and in
1215 * any case, flags the connection as reusable if no handshake was in progress.
1216 */
1217static void ssl_sock_shutw(struct connection *conn, int clean)
1218{
1219 if (conn->flags & CO_FL_HANDSHAKE)
1220 return;
1221 /* no handshake was in progress, try a clean ssl shutdown */
Emeric Brun644cde02012-12-14 11:21:13 +01001222 if (clean && (SSL_shutdown(conn->xprt_ctx) <= 0)) {
1223 /* Clear openssl global errors stack */
1224 ERR_clear_error();
1225 }
Emeric Brun46591952012-05-18 15:47:34 +02001226
1227 /* force flag on ssl to keep session in cache regardless shutdown result */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001228 SSL_set_shutdown(conn->xprt_ctx, SSL_SENT_SHUTDOWN);
Emeric Brun46591952012-05-18 15:47:34 +02001229}
1230
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001231/* used for logging, may be changed for a sample fetch later */
1232const char *ssl_sock_get_cipher_name(struct connection *conn)
1233{
1234 if (!conn->xprt && !conn->xprt_ctx)
1235 return NULL;
1236 return SSL_get_cipher_name(conn->xprt_ctx);
1237}
1238
1239/* used for logging, may be changed for a sample fetch later */
1240const char *ssl_sock_get_proto_version(struct connection *conn)
1241{
1242 if (!conn->xprt && !conn->xprt_ctx)
1243 return NULL;
1244 return SSL_get_version(conn->xprt_ctx);
1245}
1246
Willy Tarreau8d598402012-10-22 17:58:39 +02001247/* Extract a serial from a cert, and copy it to a chunk.
1248 * Returns 1 if serial is found and copied, 0 if no serial found and
1249 * -1 if output is not large enough.
1250 */
1251static int
1252ssl_sock_get_serial(X509 *crt, struct chunk *out)
1253{
1254 ASN1_INTEGER *serial;
1255
1256 serial = X509_get_serialNumber(crt);
1257 if (!serial)
1258 return 0;
1259
1260 if (out->size < serial->length)
1261 return -1;
1262
1263 memcpy(out->str, serial->data, serial->length);
1264 out->len = serial->length;
1265 return 1;
1266}
1267
Emeric Brunce5ad802012-10-22 14:11:22 +02001268
1269/* Copy Date in ASN1_UTCTIME format in struct chunk out.
1270 * Returns 1 if serial is found and copied, 0 if no valid time found
1271 * and -1 if output is not large enough.
1272 */
1273static int
1274ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out)
1275{
1276 if (tm->type == V_ASN1_GENERALIZEDTIME) {
1277 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
1278
1279 if (gentm->length < 12)
1280 return 0;
1281 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
1282 return 0;
1283 if (out->size < gentm->length-2)
1284 return -1;
1285
1286 memcpy(out->str, gentm->data+2, gentm->length-2);
1287 out->len = gentm->length-2;
1288 return 1;
1289 }
1290 else if (tm->type == V_ASN1_UTCTIME) {
1291 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
1292
1293 if (utctm->length < 10)
1294 return 0;
1295 if (utctm->data[0] >= 0x35)
1296 return 0;
1297 if (out->size < utctm->length)
1298 return -1;
1299
1300 memcpy(out->str, utctm->data, utctm->length);
1301 out->len = utctm->length;
1302 return 1;
1303 }
1304
1305 return 0;
1306}
1307
Emeric Brun87855892012-10-17 17:39:35 +02001308/* Extract an entry from a X509_NAME and copy its value to an output chunk.
1309 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
1310 */
1311static int
1312ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out)
1313{
1314 X509_NAME_ENTRY *ne;
1315 int i, j, n;
1316 int cur = 0;
1317 const char *s;
1318 char tmp[128];
1319
1320 out->len = 0;
1321 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
1322 if (pos < 0)
1323 j = (sk_X509_NAME_ENTRY_num(a->entries)-1) - i;
1324 else
1325 j = i;
1326
1327 ne = sk_X509_NAME_ENTRY_value(a->entries, j);
1328 n = OBJ_obj2nid(ne->object);
1329 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
1330 i2t_ASN1_OBJECT(tmp, sizeof(tmp), ne->object);
1331 s = tmp;
1332 }
1333
1334 if (chunk_strcasecmp(entry, s) != 0)
1335 continue;
1336
1337 if (pos < 0)
1338 cur--;
1339 else
1340 cur++;
1341
1342 if (cur != pos)
1343 continue;
1344
1345 if (ne->value->length > out->size)
1346 return -1;
1347
1348 memcpy(out->str, ne->value->data, ne->value->length);
1349 out->len = ne->value->length;
1350 return 1;
1351 }
1352
1353 return 0;
1354
1355}
1356
1357/* Extract and format full DN from a X509_NAME and copy result into a chunk
1358 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
1359 */
1360static int
1361ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out)
1362{
1363 X509_NAME_ENTRY *ne;
1364 int i, n, ln;
1365 int l = 0;
1366 const char *s;
1367 char *p;
1368 char tmp[128];
1369
1370 out->len = 0;
1371 p = out->str;
1372 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
1373 ne = sk_X509_NAME_ENTRY_value(a->entries, i);
1374 n = OBJ_obj2nid(ne->object);
1375 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
1376 i2t_ASN1_OBJECT(tmp, sizeof(tmp), ne->object);
1377 s = tmp;
1378 }
1379 ln = strlen(s);
1380
1381 l += 1 + ln + 1 + ne->value->length;
1382 if (l > out->size)
1383 return -1;
1384 out->len = l;
1385
1386 *(p++)='/';
1387 memcpy(p, s, ln);
1388 p += ln;
1389 *(p++)='=';
1390 memcpy(p, ne->value->data, ne->value->length);
1391 p += ne->value->length;
1392 }
1393
1394 if (!out->len)
1395 return 0;
1396
1397 return 1;
1398}
1399
Willy Tarreau7875d092012-09-10 08:20:03 +02001400/***** Below are some sample fetching functions for ACL/patterns *****/
1401
Emeric Brune64aef12012-09-21 13:15:06 +02001402/* boolean, returns true if client cert was present */
1403static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02001404smp_fetch_ssl_fc_has_crt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1405 const struct arg *args, struct sample *smp)
Emeric Brune64aef12012-09-21 13:15:06 +02001406{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001407 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02001408 return 0;
1409
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001410 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02001411 smp->flags |= SMP_F_MAY_CHANGE;
1412 return 0;
1413 }
1414
1415 smp->flags = 0;
1416 smp->type = SMP_T_BOOL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001417 smp->data.uint = SSL_SOCK_ST_FL_VERIFY_DONE & l4->si[0].conn->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001418
1419 return 1;
1420}
1421
Willy Tarreau8d598402012-10-22 17:58:39 +02001422/* bin, returns serial in a binary chunk */
1423static int
1424smp_fetch_ssl_c_serial(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1425 const struct arg *args, struct sample *smp)
1426{
1427 X509 *crt = NULL;
1428 int ret = 0;
1429 struct chunk *smp_trash;
1430
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001431 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02001432 return 0;
1433
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001434 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02001435 smp->flags |= SMP_F_MAY_CHANGE;
1436 return 0;
1437 }
1438
1439 /* SSL_get_peer_certificate, it increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001440 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Willy Tarreau8d598402012-10-22 17:58:39 +02001441 if (!crt)
1442 goto out;
1443
1444 smp_trash = sample_get_trash_chunk();
1445 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
1446 goto out;
1447
1448 smp->data.str = *smp_trash;
1449 smp->type = SMP_T_BIN;
1450 ret = 1;
1451out:
1452 if (crt)
1453 X509_free(crt);
1454 return ret;
1455}
Emeric Brune64aef12012-09-21 13:15:06 +02001456
Emeric Brunce5ad802012-10-22 14:11:22 +02001457/*str, returns notafter date in ASN1_UTCTIME format */
1458static int
1459smp_fetch_ssl_c_notafter(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1460 const struct arg *args, struct sample *smp)
1461{
1462 X509 *crt = NULL;
1463 int ret = 0;
1464 struct chunk *smp_trash;
1465
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001466 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02001467 return 0;
1468
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001469 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02001470 smp->flags |= SMP_F_MAY_CHANGE;
1471 return 0;
1472 }
1473
1474 /* SSL_get_peer_certificate, it increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001475 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brunce5ad802012-10-22 14:11:22 +02001476 if (!crt)
1477 goto out;
1478
1479 smp_trash = sample_get_trash_chunk();
1480 if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0)
1481 goto out;
1482
1483 smp->data.str = *smp_trash;
1484 smp->type = SMP_T_STR;
1485 ret = 1;
1486out:
1487 if (crt)
1488 X509_free(crt);
1489 return ret;
1490}
1491
Emeric Brun87855892012-10-17 17:39:35 +02001492/* str, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. */
1493static int
1494smp_fetch_ssl_c_i_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1495 const struct arg *args, struct sample *smp)
1496{
1497 X509 *crt = NULL;
1498 X509_NAME *name;
1499 int ret = 0;
1500 struct chunk *smp_trash;
1501
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001502 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun87855892012-10-17 17:39:35 +02001503 return 0;
1504
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001505 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02001506 smp->flags |= SMP_F_MAY_CHANGE;
1507 return 0;
1508 }
1509
1510 /* SSL_get_peer_certificate, it increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001511 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun87855892012-10-17 17:39:35 +02001512 if (!crt)
1513 goto out;
1514
1515 name = X509_get_issuer_name(crt);
1516 if (!name)
1517 goto out;
1518
1519 smp_trash = sample_get_trash_chunk();
1520 if (args && args[0].type == ARGT_STR) {
1521 int pos = 1;
1522
1523 if (args[1].type == ARGT_SINT)
1524 pos = args[1].data.sint;
1525 else if (args[1].type == ARGT_UINT)
1526 pos =(int)args[1].data.uint;
1527
1528 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
1529 goto out;
1530 }
1531 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
1532 goto out;
1533
1534 smp->type = SMP_T_STR;
1535 smp->data.str = *smp_trash;
1536 ret = 1;
1537out:
1538 if (crt)
1539 X509_free(crt);
1540 return ret;
1541}
1542
Emeric Brunce5ad802012-10-22 14:11:22 +02001543/*str, returns notbefore date in ASN1_UTCTIME format */
1544static int
1545smp_fetch_ssl_c_notbefore(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1546 const struct arg *args, struct sample *smp)
1547{
1548 X509 *crt = NULL;
1549 int ret = 0;
1550 struct chunk *smp_trash;
1551
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001552 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02001553 return 0;
1554
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001555 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02001556 smp->flags |= SMP_F_MAY_CHANGE;
1557 return 0;
1558 }
1559
1560 /* SSL_get_peer_certificate, it increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001561 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brunce5ad802012-10-22 14:11:22 +02001562 if (!crt)
1563 goto out;
1564
1565 smp_trash = sample_get_trash_chunk();
1566 if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0)
1567 goto out;
1568
1569 smp->data.str = *smp_trash;
1570 smp->type = SMP_T_STR;
1571 ret = 1;
1572out:
1573 if (crt)
1574 X509_free(crt);
1575 return ret;
1576}
1577
Emeric Brun87855892012-10-17 17:39:35 +02001578/* str, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. */
1579static int
1580smp_fetch_ssl_c_s_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1581 const struct arg *args, struct sample *smp)
1582{
1583 X509 *crt = NULL;
1584 X509_NAME *name;
1585 int ret = 0;
1586 struct chunk *smp_trash;
1587
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001588 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun87855892012-10-17 17:39:35 +02001589 return 0;
1590
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001591 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02001592 smp->flags |= SMP_F_MAY_CHANGE;
1593 return 0;
1594 }
1595
1596 /* SSL_get_peer_certificate, it increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001597 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun87855892012-10-17 17:39:35 +02001598 if (!crt)
1599 goto out;
1600
1601 name = X509_get_subject_name(crt);
1602 if (!name)
1603 goto out;
1604
1605 smp_trash = sample_get_trash_chunk();
1606 if (args && args[0].type == ARGT_STR) {
1607 int pos = 1;
1608
1609 if (args[1].type == ARGT_SINT)
1610 pos = args[1].data.sint;
1611 else if (args[1].type == ARGT_UINT)
1612 pos =(int)args[1].data.uint;
1613
1614 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
1615 goto out;
1616 }
1617 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
1618 goto out;
1619
1620 smp->type = SMP_T_STR;
1621 smp->data.str = *smp_trash;
1622 ret = 1;
1623out:
1624 if (crt)
1625 X509_free(crt);
1626 return ret;
1627}
Emeric Bruna7359fd2012-10-17 15:03:11 +02001628/* integer, returns the client certificate version */
1629static int
1630smp_fetch_ssl_c_version(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1631 const struct arg *args, struct sample *smp)
1632{
1633 X509 *crt;
1634
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001635 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02001636 return 0;
1637
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001638 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02001639 smp->flags |= SMP_F_MAY_CHANGE;
1640 return 0;
1641 }
1642
1643 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001644 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Bruna7359fd2012-10-17 15:03:11 +02001645 if (!crt)
1646 return 0;
1647
1648 smp->data.uint = (unsigned int)(1 + X509_get_version(crt));
1649 X509_free(crt);
1650 smp->type = SMP_T_UINT;
1651
1652 return 1;
1653}
1654
Emeric Brun7f56e742012-10-19 18:15:40 +02001655/* str, returns the client certificate sig alg */
1656static int
1657smp_fetch_ssl_c_sig_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1658 const struct arg *args, struct sample *smp)
1659{
1660 X509 *crt;
1661 int nid;
1662
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001663 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun7f56e742012-10-19 18:15:40 +02001664 return 0;
1665
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001666 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02001667 smp->flags |= SMP_F_MAY_CHANGE;
1668 return 0;
1669 }
1670
1671 /* SSL_get_peer_certificate increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001672 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun7f56e742012-10-19 18:15:40 +02001673 if (!crt)
1674 return 0;
1675
1676 nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->signature->algorithm));
1677
1678 smp->data.str.str = (char *)OBJ_nid2sn(nid);
1679 if (!smp->data.str.str)
1680 return 0;
1681
1682 smp->type = SMP_T_CSTR;
1683 smp->data.str.len = strlen(smp->data.str.str);
1684 X509_free(crt);
1685
1686 return 1;
1687}
1688
Emeric Brun521a0112012-10-22 12:22:55 +02001689/* str, returns the client certificate key alg */
1690static int
1691smp_fetch_ssl_c_key_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1692 const struct arg *args, struct sample *smp)
1693{
1694 X509 *crt;
1695 int nid;
1696
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001697 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun521a0112012-10-22 12:22:55 +02001698 return 0;
1699
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001700 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02001701 smp->flags |= SMP_F_MAY_CHANGE;
1702 return 0;
1703 }
1704
1705 /* SSL_get_peer_certificate increase X509 * ref count */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001706 crt = SSL_get_peer_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun521a0112012-10-22 12:22:55 +02001707 if (!crt)
1708 return 0;
1709
1710 nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->key->algor->algorithm));
1711
1712 smp->data.str.str = (char *)OBJ_nid2sn(nid);
1713 if (!smp->data.str.str)
1714 return 0;
1715
1716 smp->type = SMP_T_CSTR;
1717 smp->data.str.len = strlen(smp->data.str.str);
1718 X509_free(crt);
1719
1720 return 1;
1721}
1722
Emeric Brun2525b6b2012-10-18 15:59:43 +02001723/* boolean, returns true if front conn. transport layer is SSL */
Willy Tarreau7875d092012-09-10 08:20:03 +02001724static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02001725smp_fetch_ssl_fc(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau7875d092012-09-10 08:20:03 +02001726 const struct arg *args, struct sample *smp)
1727{
1728 smp->type = SMP_T_BOOL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001729 smp->data.uint = (l4->si[0].conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02001730 return 1;
1731}
1732
Emeric Brun2525b6b2012-10-18 15:59:43 +02001733/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02001734static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02001735smp_fetch_ssl_fc_has_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1736 const struct arg *args, struct sample *smp)
Willy Tarreau7875d092012-09-10 08:20:03 +02001737{
1738#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1739 smp->type = SMP_T_BOOL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001740 smp->data.uint = (l4->si[0].conn->xprt == &ssl_sock) &&
1741 l4->si[0].conn->xprt_ctx &&
1742 SSL_get_servername(l4->si[0].conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02001743 return 1;
1744#else
1745 return 0;
1746#endif
1747}
1748
Willy Tarreau8d598402012-10-22 17:58:39 +02001749/* bin, returns serial in a binary chunk */
1750static int
1751smp_fetch_ssl_f_serial(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1752 const struct arg *args, struct sample *smp)
1753{
1754 X509 *crt = NULL;
1755 int ret = 0;
1756 struct chunk *smp_trash;
1757
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001758 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02001759 return 0;
1760
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001761 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02001762 smp->flags |= SMP_F_MAY_CHANGE;
1763 return 0;
1764 }
1765
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001766 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Willy Tarreau8d598402012-10-22 17:58:39 +02001767 if (!crt)
1768 goto out;
1769
1770 smp_trash = sample_get_trash_chunk();
1771 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
1772 goto out;
1773
1774 smp->data.str = *smp_trash;
1775 smp->type = SMP_T_BIN;
1776 ret = 1;
1777out:
1778 return ret;
1779}
Emeric Brunce5ad802012-10-22 14:11:22 +02001780/*str, returns notafter date in ASN1_UTCTIME format */
1781static int
1782smp_fetch_ssl_f_notafter(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1783 const struct arg *args, struct sample *smp)
1784{
1785 X509 *crt = NULL;
1786 int ret = 0;
1787 struct chunk *smp_trash;
1788
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001789 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02001790 return 0;
1791
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001792 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02001793 smp->flags |= SMP_F_MAY_CHANGE;
1794 return 0;
1795 }
1796
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001797 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brunce5ad802012-10-22 14:11:22 +02001798 if (!crt)
1799 goto out;
1800
1801 smp_trash = sample_get_trash_chunk();
1802 if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0)
1803 goto out;
1804
1805 smp->data.str = *smp_trash;
1806 smp->type = SMP_T_STR;
1807 ret = 1;
1808out:
1809 return ret;
1810}
1811
1812/*str, returns notbefore date in ASN1_UTCTIME format */
1813static int
1814smp_fetch_ssl_f_notbefore(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1815 const struct arg *args, struct sample *smp)
1816{
1817 X509 *crt = NULL;
1818 int ret = 0;
1819 struct chunk *smp_trash;
1820
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001821 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02001822 return 0;
1823
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001824 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02001825 smp->flags |= SMP_F_MAY_CHANGE;
1826 return 0;
1827 }
1828
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001829 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brunce5ad802012-10-22 14:11:22 +02001830 if (!crt)
1831 goto out;
1832
1833 smp_trash = sample_get_trash_chunk();
1834 if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0)
1835 goto out;
1836
1837 smp->data.str = *smp_trash;
1838 smp->type = SMP_T_STR;
1839 ret = 1;
1840out:
1841 return ret;
1842}
Willy Tarreau8d598402012-10-22 17:58:39 +02001843
Emeric Bruna7359fd2012-10-17 15:03:11 +02001844/* integer, returns the frontend certificate version */
1845static int
1846smp_fetch_ssl_f_version(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1847 const struct arg *args, struct sample *smp)
1848{
1849 X509 *crt;
1850
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001851 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02001852 return 0;
1853
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001854 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02001855 smp->flags |= SMP_F_MAY_CHANGE;
1856 return 0;
1857 }
1858
1859 /* SSL_get_certificate returns a ptr on an SSL * internal sub struct */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001860 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Bruna7359fd2012-10-17 15:03:11 +02001861 if (!crt)
1862 return 0;
1863
1864 smp->data.uint = (unsigned int)(1 + X509_get_version(crt));
1865 smp->type = SMP_T_UINT;
1866
1867 return 1;
1868}
1869
Emeric Brun7f56e742012-10-19 18:15:40 +02001870/* str, returns the client certificate sig alg */
1871static int
1872smp_fetch_ssl_f_sig_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1873 const struct arg *args, struct sample *smp)
1874{
1875 X509 *crt;
1876 int nid;
1877
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001878 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun7f56e742012-10-19 18:15:40 +02001879 return 0;
1880
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001881 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02001882 smp->flags |= SMP_F_MAY_CHANGE;
1883 return 0;
1884 }
1885
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001886 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun7f56e742012-10-19 18:15:40 +02001887 if (!crt)
1888 return 0;
1889
1890 nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->signature->algorithm));
1891
1892 smp->data.str.str = (char *)OBJ_nid2sn(nid);
1893 if (!smp->data.str.str)
1894 return 0;
1895
1896 smp->type = SMP_T_CSTR;
1897 smp->data.str.len = strlen(smp->data.str.str);
1898
1899 return 1;
1900}
1901
Emeric Brun521a0112012-10-22 12:22:55 +02001902/* str, returns the client certificate key alg */
1903static int
1904smp_fetch_ssl_f_key_alg(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1905 const struct arg *args, struct sample *smp)
1906{
1907 X509 *crt;
1908 int nid;
1909
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001910 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun521a0112012-10-22 12:22:55 +02001911 return 0;
1912
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001913 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02001914 smp->flags |= SMP_F_MAY_CHANGE;
1915 return 0;
1916 }
1917
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001918 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun521a0112012-10-22 12:22:55 +02001919 if (!crt)
1920 return 0;
1921
1922 nid = OBJ_obj2nid((ASN1_OBJECT *)(crt->cert_info->key->algor->algorithm));
1923
1924 smp->data.str.str = (char *)OBJ_nid2sn(nid);
1925 if (!smp->data.str.str)
1926 return 0;
1927
1928 smp->type = SMP_T_CSTR;
1929 smp->data.str.len = strlen(smp->data.str.str);
1930
1931 return 1;
1932}
1933
Emeric Brun87855892012-10-17 17:39:35 +02001934/* str, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. */
1935static int
1936smp_fetch_ssl_f_i_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1937 const struct arg *args, struct sample *smp)
1938{
1939 X509 *crt = NULL;
1940 X509_NAME *name;
1941 int ret = 0;
1942 struct chunk *smp_trash;
1943
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001944 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun87855892012-10-17 17:39:35 +02001945 return 0;
1946
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001947 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02001948 smp->flags |= SMP_F_MAY_CHANGE;
1949 return 0;
1950 }
1951
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001952 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun87855892012-10-17 17:39:35 +02001953 if (!crt)
1954 goto out;
1955
1956 name = X509_get_issuer_name(crt);
1957 if (!name)
1958 goto out;
1959
1960 smp_trash = sample_get_trash_chunk();
1961 if (args && args[0].type == ARGT_STR) {
1962 int pos = 1;
1963
1964 if (args[1].type == ARGT_SINT)
1965 pos = args[1].data.sint;
1966 else if (args[1].type == ARGT_UINT)
1967 pos =(int)args[1].data.uint;
1968
1969 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
1970 goto out;
1971 }
1972 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
1973 goto out;
1974
1975 smp->type = SMP_T_STR;
1976 smp->data.str = *smp_trash;
1977 ret = 1;
1978out:
1979 return ret;
1980}
1981
1982/* str, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. */
1983static int
1984smp_fetch_ssl_f_s_dn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1985 const struct arg *args, struct sample *smp)
1986{
1987 X509 *crt = NULL;
1988 X509_NAME *name;
1989 int ret = 0;
1990 struct chunk *smp_trash;
1991
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001992 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun87855892012-10-17 17:39:35 +02001993 return 0;
1994
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001995 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02001996 smp->flags |= SMP_F_MAY_CHANGE;
1997 return 0;
1998 }
1999
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002000 crt = SSL_get_certificate(l4->si[0].conn->xprt_ctx);
Emeric Brun87855892012-10-17 17:39:35 +02002001 if (!crt)
2002 goto out;
2003
2004 name = X509_get_subject_name(crt);
2005 if (!name)
2006 goto out;
2007
2008 smp_trash = sample_get_trash_chunk();
2009 if (args && args[0].type == ARGT_STR) {
2010 int pos = 1;
2011
2012 if (args[1].type == ARGT_SINT)
2013 pos = args[1].data.sint;
2014 else if (args[1].type == ARGT_UINT)
2015 pos =(int)args[1].data.uint;
2016
2017 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
2018 goto out;
2019 }
2020 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
2021 goto out;
2022
2023 smp->type = SMP_T_STR;
2024 smp->data.str = *smp_trash;
2025 ret = 1;
2026out:
2027 return ret;
2028}
2029
Emeric Brun589fcad2012-10-16 14:13:26 +02002030static int
2031smp_fetch_ssl_fc_cipher(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2032 const struct arg *args, struct sample *smp)
2033{
2034 smp->flags = 0;
2035
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002036 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02002037 return 0;
2038
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002039 smp->data.str.str = (char *)SSL_get_cipher_name(l4->si[0].conn->xprt_ctx);
Emeric Brun589fcad2012-10-16 14:13:26 +02002040 if (!smp->data.str.str)
2041 return 0;
2042
2043 smp->type = SMP_T_CSTR;
2044 smp->data.str.len = strlen(smp->data.str.str);
2045
2046 return 1;
2047}
2048
2049static int
2050smp_fetch_ssl_fc_alg_keysize(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2051 const struct arg *args, struct sample *smp)
2052{
2053 smp->flags = 0;
2054
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002055 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02002056 return 0;
2057
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002058 if (!SSL_get_cipher_bits(l4->si[0].conn->xprt_ctx, (int *)&smp->data.uint))
Emeric Brun589fcad2012-10-16 14:13:26 +02002059 return 0;
2060
2061 smp->type = SMP_T_UINT;
2062
2063 return 1;
2064}
2065
2066static int
2067smp_fetch_ssl_fc_use_keysize(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2068 const struct arg *args, struct sample *smp)
2069{
2070 smp->flags = 0;
2071
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002072 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02002073 return 0;
2074
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002075 smp->data.uint = (unsigned int)SSL_get_cipher_bits(l4->si[0].conn->xprt_ctx, NULL);
Emeric Brun589fcad2012-10-16 14:13:26 +02002076 if (!smp->data.uint)
2077 return 0;
2078
2079 smp->type = SMP_T_UINT;
2080
2081 return 1;
2082}
2083
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02002084#ifdef OPENSSL_NPN_NEGOTIATED
Willy Tarreau7875d092012-09-10 08:20:03 +02002085static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002086smp_fetch_ssl_fc_npn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2087 const struct arg *args, struct sample *smp)
Willy Tarreaua33c6542012-10-15 13:19:06 +02002088{
Willy Tarreaua33c6542012-10-15 13:19:06 +02002089 smp->flags = 0;
2090 smp->type = SMP_T_CSTR;
2091
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002092 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Willy Tarreaua33c6542012-10-15 13:19:06 +02002093 return 0;
2094
2095 smp->data.str.str = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002096 SSL_get0_next_proto_negotiated(l4->si[0].conn->xprt_ctx,
Willy Tarreaua33c6542012-10-15 13:19:06 +02002097 (const unsigned char **)&smp->data.str.str, (unsigned *)&smp->data.str.len);
2098
2099 if (!smp->data.str.str)
2100 return 0;
2101
2102 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02002103}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02002104#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02002105
2106static int
Emeric Brun589fcad2012-10-16 14:13:26 +02002107smp_fetch_ssl_fc_protocol(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2108 const struct arg *args, struct sample *smp)
2109{
2110 smp->flags = 0;
2111
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002112 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02002113 return 0;
2114
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002115 smp->data.str.str = (char *)SSL_get_version(l4->si[0].conn->xprt_ctx);
Emeric Brun589fcad2012-10-16 14:13:26 +02002116 if (!smp->data.str.str)
2117 return 0;
2118
2119 smp->type = SMP_T_CSTR;
2120 smp->data.str.len = strlen(smp->data.str.str);
2121
2122 return 1;
2123}
2124
2125static int
Emeric Brunfe68f682012-10-16 14:59:28 +02002126smp_fetch_ssl_fc_session_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2127 const struct arg *args, struct sample *smp)
2128{
2129#if OPENSSL_VERSION_NUMBER > 0x0090800fL
2130 SSL_SESSION *sess;
2131
2132 smp->flags = 0;
2133 smp->type = SMP_T_CBIN;
2134
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002135 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunfe68f682012-10-16 14:59:28 +02002136 return 0;
2137
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002138 sess = SSL_get_session(l4->si[0].conn->xprt_ctx);
Emeric Brunfe68f682012-10-16 14:59:28 +02002139 if (!sess)
2140 return 0;
2141
2142 smp->data.str.str = (char *)SSL_SESSION_get_id(sess, (unsigned int *)&smp->data.str.len);
2143 if (!smp->data.str.str || !&smp->data.str.len)
2144 return 0;
2145
2146 return 1;
2147#else
2148 return 0;
2149#endif
2150}
2151
2152static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002153smp_fetch_ssl_fc_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2154 const struct arg *args, struct sample *smp)
Willy Tarreau7875d092012-09-10 08:20:03 +02002155{
2156#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2157 smp->flags = 0;
2158 smp->type = SMP_T_CSTR;
2159
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002160 if (!l4 || !l4->si[0].conn->xprt_ctx || l4->si[0].conn->xprt != &ssl_sock)
Willy Tarreau7875d092012-09-10 08:20:03 +02002161 return 0;
2162
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002163 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 +02002164 if (!smp->data.str.str)
2165 return 0;
2166
Willy Tarreau7875d092012-09-10 08:20:03 +02002167 smp->data.str.len = strlen(smp->data.str.str);
2168 return 1;
2169#else
2170 return 0;
2171#endif
2172}
2173
Emeric Brun2525b6b2012-10-18 15:59:43 +02002174/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02002175static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002176smp_fetch_ssl_c_ca_err(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Emeric Brunf282a812012-09-21 15:27:54 +02002177 const struct arg *args, struct sample *smp)
2178{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002179 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02002180 return 0;
2181
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002182 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02002183 smp->flags = SMP_F_MAY_CHANGE;
2184 return 0;
2185 }
2186
2187 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002188 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CA_ERROR(l4->si[0].conn->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02002189 smp->flags = 0;
2190
2191 return 1;
2192}
2193
Emeric Brun2525b6b2012-10-18 15:59:43 +02002194/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02002195static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002196smp_fetch_ssl_c_ca_err_depth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Emeric Brunf282a812012-09-21 15:27:54 +02002197 const struct arg *args, struct sample *smp)
2198{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002199 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02002200 return 0;
2201
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002202 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02002203 smp->flags = SMP_F_MAY_CHANGE;
2204 return 0;
2205 }
2206
2207 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002208 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CAEDEPTH(l4->si[0].conn->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02002209 smp->flags = 0;
2210
2211 return 1;
2212}
2213
Emeric Brun2525b6b2012-10-18 15:59:43 +02002214/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02002215static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002216smp_fetch_ssl_c_err(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2217 const struct arg *args, struct sample *smp)
Emeric Brunf282a812012-09-21 15:27:54 +02002218{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002219 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02002220 return 0;
2221
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002222 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02002223 smp->flags = SMP_F_MAY_CHANGE;
2224 return 0;
2225 }
2226
2227 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002228 smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CRTERROR(l4->si[0].conn->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02002229 smp->flags = 0;
2230
2231 return 1;
2232}
2233
Emeric Brun2525b6b2012-10-18 15:59:43 +02002234/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02002235static int
Emeric Brun2525b6b2012-10-18 15:59:43 +02002236smp_fetch_ssl_c_verify(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2237 const struct arg *args, struct sample *smp)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02002238{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002239 if (!l4 || l4->si[0].conn->xprt != &ssl_sock)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02002240 return 0;
2241
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002242 if (!(l4->si[0].conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02002243 smp->flags = SMP_F_MAY_CHANGE;
2244 return 0;
2245 }
2246
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002247 if (!l4->si[0].conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02002248 return 0;
2249
2250 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002251 smp->data.uint = (unsigned int)SSL_get_verify_result(l4->si[0].conn->xprt_ctx);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02002252 smp->flags = 0;
2253
2254 return 1;
2255}
2256
Emeric Brunfb510ea2012-10-05 12:00:26 +02002257/* parse the "ca-file" bind keyword */
2258static 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 +02002259{
2260 if (!*args[cur_arg + 1]) {
2261 if (err)
2262 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
2263 return ERR_ALERT | ERR_FATAL;
2264 }
2265
Emeric Brunef42d922012-10-11 16:11:36 +02002266 if ((*args[cur_arg + 1] != '/') && global.ca_base)
2267 memprintf(&conf->ca_file, "%s/%s", global.ca_base, args[cur_arg + 1]);
2268 else
2269 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02002270
Emeric Brund94b3fe2012-09-20 18:23:56 +02002271 return 0;
2272}
2273
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002274/* parse the "ciphers" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002275static 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 +02002276{
2277 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002278 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002279 return ERR_ALERT | ERR_FATAL;
2280 }
2281
Emeric Brun76d88952012-10-05 15:47:31 +02002282 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02002283 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002284 return 0;
2285}
2286
2287/* parse the "crt" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002288static 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 +02002289{
Emeric Brunc8e8d122012-10-02 18:42:10 +02002290 char path[PATH_MAX];
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002291 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002292 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002293 return ERR_ALERT | ERR_FATAL;
2294 }
2295
Emeric Brunc8e8d122012-10-02 18:42:10 +02002296 if ((*args[cur_arg + 1] != '/' ) && global.crt_base) {
2297 if ((strlen(global.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > PATH_MAX) {
2298 memprintf(err, "'%s' : path too long", args[cur_arg]);
2299 return ERR_ALERT | ERR_FATAL;
2300 }
2301 sprintf(path, "%s/%s", global.crt_base, args[cur_arg + 1]);
2302 if (ssl_sock_load_cert(path, conf, px, err) > 0)
2303 return ERR_ALERT | ERR_FATAL;
2304
2305 return 0;
2306 }
2307
Willy Tarreau4348fad2012-09-20 16:48:07 +02002308 if (ssl_sock_load_cert(args[cur_arg + 1], conf, px, err) > 0)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002309 return ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02002310
2311 return 0;
2312}
2313
Emeric Brunfb510ea2012-10-05 12:00:26 +02002314/* parse the "crl-file" bind keyword */
2315static 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 +02002316{
Emeric Brun051cdab2012-10-02 19:25:50 +02002317#ifndef X509_V_FLAG_CRL_CHECK
2318 if (err)
2319 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
2320 return ERR_ALERT | ERR_FATAL;
2321#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02002322 if (!*args[cur_arg + 1]) {
2323 if (err)
2324 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
2325 return ERR_ALERT | ERR_FATAL;
2326 }
Emeric Brun2b58d042012-09-20 17:10:03 +02002327
Emeric Brunef42d922012-10-11 16:11:36 +02002328 if ((*args[cur_arg + 1] != '/') && global.ca_base)
2329 memprintf(&conf->crl_file, "%s/%s", global.ca_base, args[cur_arg + 1]);
2330 else
2331 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02002332
Emeric Brun2b58d042012-09-20 17:10:03 +02002333 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02002334#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02002335}
2336
2337/* parse the "ecdhe" bind keyword keywords */
2338static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2339{
2340#if OPENSSL_VERSION_NUMBER < 0x0090800fL
2341 if (err)
2342 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
2343 return ERR_ALERT | ERR_FATAL;
2344#elif defined(OPENSSL_NO_ECDH)
2345 if (err)
2346 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
2347 return ERR_ALERT | ERR_FATAL;
2348#else
2349 if (!*args[cur_arg + 1]) {
2350 if (err)
2351 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
2352 return ERR_ALERT | ERR_FATAL;
2353 }
2354
2355 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002356
2357 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02002358#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002359}
2360
Emeric Brun81c00f02012-09-21 14:31:21 +02002361/* parse the "crt_ignerr" and "ca_ignerr" bind keywords */
2362static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2363{
2364 int code;
2365 char *p = args[cur_arg + 1];
2366 unsigned long long *ignerr = &conf->crt_ignerr;
2367
2368 if (!*p) {
2369 if (err)
2370 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
2371 return ERR_ALERT | ERR_FATAL;
2372 }
2373
2374 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
2375 ignerr = &conf->ca_ignerr;
2376
2377 if (strcmp(p, "all") == 0) {
2378 *ignerr = ~0ULL;
2379 return 0;
2380 }
2381
2382 while (p) {
2383 code = atoi(p);
2384 if ((code <= 0) || (code > 63)) {
2385 if (err)
2386 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
2387 args[cur_arg], code, args[cur_arg + 1]);
2388 return ERR_ALERT | ERR_FATAL;
2389 }
2390 *ignerr |= 1ULL << code;
2391 p = strchr(p, ',');
2392 if (p)
2393 p++;
2394 }
2395
Emeric Brun2cb7ae52012-10-05 14:14:21 +02002396 return 0;
2397}
2398
2399/* parse the "force-sslv3" bind keyword */
2400static int bind_parse_force_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2401{
2402 conf->ssl_options |= BC_SSL_O_USE_SSLV3;
2403 return 0;
2404}
2405
2406/* parse the "force-tlsv10" bind keyword */
2407static int bind_parse_force_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2408{
2409 conf->ssl_options |= BC_SSL_O_USE_TLSV10;
Emeric Brun2d0c4822012-10-02 13:45:20 +02002410 return 0;
2411}
2412
Emeric Brun2cb7ae52012-10-05 14:14:21 +02002413/* parse the "force-tlsv11" bind keyword */
2414static int bind_parse_force_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2415{
2416#if SSL_OP_NO_TLSv1_1
2417 conf->ssl_options |= BC_SSL_O_USE_TLSV11;
2418 return 0;
2419#else
2420 if (err)
2421 memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[cur_arg]);
2422 return ERR_ALERT | ERR_FATAL;
2423#endif
2424}
2425
2426/* parse the "force-tlsv12" bind keyword */
2427static int bind_parse_force_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2428{
2429#if SSL_OP_NO_TLSv1_2
2430 conf->ssl_options |= BC_SSL_O_USE_TLSV12;
2431 return 0;
2432#else
2433 if (err)
2434 memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[cur_arg]);
2435 return ERR_ALERT | ERR_FATAL;
2436#endif
2437}
2438
2439
Emeric Brun2d0c4822012-10-02 13:45:20 +02002440/* parse the "no-tls-tickets" bind keyword */
2441static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2442{
Emeric Brun89675492012-10-05 13:48:26 +02002443 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02002444 return 0;
2445}
2446
Emeric Brun2d0c4822012-10-02 13:45:20 +02002447
Emeric Brun9b3009b2012-10-05 11:55:06 +02002448/* parse the "no-sslv3" bind keyword */
2449static 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 +02002450{
Emeric Brun89675492012-10-05 13:48:26 +02002451 conf->ssl_options |= BC_SSL_O_NO_SSLV3;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002452 return 0;
2453}
2454
Emeric Brun9b3009b2012-10-05 11:55:06 +02002455/* parse the "no-tlsv10" bind keyword */
2456static 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 +02002457{
Emeric Brun89675492012-10-05 13:48:26 +02002458 conf->ssl_options |= BC_SSL_O_NO_TLSV10;
Emeric Brunc0ff4922012-09-28 19:37:02 +02002459 return 0;
2460}
2461
Emeric Brun9b3009b2012-10-05 11:55:06 +02002462/* parse the "no-tlsv11" bind keyword */
2463static 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 +02002464{
Emeric Brun89675492012-10-05 13:48:26 +02002465 conf->ssl_options |= BC_SSL_O_NO_TLSV11;
Emeric Brunc0ff4922012-09-28 19:37:02 +02002466 return 0;
2467}
2468
Emeric Brun9b3009b2012-10-05 11:55:06 +02002469/* parse the "no-tlsv12" bind keyword */
2470static 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 +02002471{
Emeric Brun89675492012-10-05 13:48:26 +02002472 conf->ssl_options |= BC_SSL_O_NO_TLSV12;
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002473 return 0;
2474}
2475
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02002476/* parse the "npn" bind keyword */
2477static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2478{
2479#ifdef OPENSSL_NPN_NEGOTIATED
2480 char *p1, *p2;
2481
2482 if (!*args[cur_arg + 1]) {
2483 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
2484 return ERR_ALERT | ERR_FATAL;
2485 }
2486
2487 free(conf->npn_str);
2488
2489 /* the NPN string is built as a suite of (<len> <name>)* */
2490 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
2491 conf->npn_str = calloc(1, conf->npn_len);
2492 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
2493
2494 /* replace commas with the name length */
2495 p1 = conf->npn_str;
2496 p2 = p1 + 1;
2497 while (1) {
2498 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
2499 if (!p2)
2500 p2 = p1 + 1 + strlen(p1 + 1);
2501
2502 if (p2 - (p1 + 1) > 255) {
2503 *p2 = '\0';
2504 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
2505 return ERR_ALERT | ERR_FATAL;
2506 }
2507
2508 *p1 = p2 - (p1 + 1);
2509 p1 = p2;
2510
2511 if (!*p2)
2512 break;
2513
2514 *(p2++) = '\0';
2515 }
2516 return 0;
2517#else
2518 if (err)
2519 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
2520 return ERR_ALERT | ERR_FATAL;
2521#endif
2522}
2523
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002524/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002525static 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 +02002526{
Willy Tarreau81796be2012-09-22 19:11:47 +02002527 struct listener *l;
2528
Willy Tarreau4348fad2012-09-20 16:48:07 +02002529 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02002530
2531 if (global.listen_default_ciphers && !conf->ciphers)
2532 conf->ciphers = strdup(global.listen_default_ciphers);
2533
Willy Tarreau81796be2012-09-22 19:11:47 +02002534 list_for_each_entry(l, &conf->listeners, by_bind)
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002535 l->xprt = &ssl_sock;
Willy Tarreau81796be2012-09-22 19:11:47 +02002536
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002537 return 0;
2538}
2539
Emeric Brund94b3fe2012-09-20 18:23:56 +02002540/* parse the "verify" bind keyword */
2541static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2542{
2543 if (!*args[cur_arg + 1]) {
2544 if (err)
2545 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
2546 return ERR_ALERT | ERR_FATAL;
2547 }
2548
2549 if (strcmp(args[cur_arg + 1], "none") == 0)
2550 conf->verify = SSL_VERIFY_NONE;
2551 else if (strcmp(args[cur_arg + 1], "optional") == 0)
2552 conf->verify = SSL_VERIFY_PEER;
2553 else if (strcmp(args[cur_arg + 1], "required") == 0)
2554 conf->verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
2555 else {
2556 if (err)
2557 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
2558 args[cur_arg], args[cur_arg + 1]);
2559 return ERR_ALERT | ERR_FATAL;
2560 }
2561
2562 return 0;
2563}
2564
Willy Tarreau92faadf2012-10-10 23:04:25 +02002565/************** "server" keywords ****************/
2566
Emeric Brunef42d922012-10-11 16:11:36 +02002567/* parse the "ca-file" server keyword */
2568static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2569{
2570 if (!*args[*cur_arg + 1]) {
2571 if (err)
2572 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
2573 return ERR_ALERT | ERR_FATAL;
2574 }
2575
2576 if ((*args[*cur_arg + 1] != '/') && global.ca_base)
2577 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global.ca_base, args[*cur_arg + 1]);
2578 else
2579 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
2580
2581 return 0;
2582}
2583
Willy Tarreau92faadf2012-10-10 23:04:25 +02002584/* parse the "check-ssl" server keyword */
2585static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2586{
2587 newsrv->check.use_ssl = 1;
2588 if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
2589 newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers);
2590 return 0;
2591}
2592
2593/* parse the "ciphers" server keyword */
2594static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2595{
2596 if (!*args[*cur_arg + 1]) {
2597 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
2598 return ERR_ALERT | ERR_FATAL;
2599 }
2600
2601 free(newsrv->ssl_ctx.ciphers);
2602 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
2603 return 0;
2604}
2605
Emeric Brunef42d922012-10-11 16:11:36 +02002606/* parse the "crl-file" server keyword */
2607static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2608{
2609#ifndef X509_V_FLAG_CRL_CHECK
2610 if (err)
2611 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
2612 return ERR_ALERT | ERR_FATAL;
2613#else
2614 if (!*args[*cur_arg + 1]) {
2615 if (err)
2616 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
2617 return ERR_ALERT | ERR_FATAL;
2618 }
2619
2620 if ((*args[*cur_arg + 1] != '/') && global.ca_base)
2621 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global.ca_base, args[*cur_arg + 1]);
2622 else
2623 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
2624
2625 return 0;
2626#endif
2627}
2628
Emeric Bruna7aa3092012-10-26 12:58:00 +02002629/* parse the "crt" server keyword */
2630static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2631{
2632 if (!*args[*cur_arg + 1]) {
2633 if (err)
2634 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
2635 return ERR_ALERT | ERR_FATAL;
2636 }
2637
2638 if ((*args[*cur_arg + 1] != '/') && global.crt_base)
2639 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global.ca_base, args[*cur_arg + 1]);
2640 else
2641 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
2642
2643 return 0;
2644}
Emeric Brunef42d922012-10-11 16:11:36 +02002645
Willy Tarreau92faadf2012-10-10 23:04:25 +02002646/* parse the "force-sslv3" server keyword */
2647static int srv_parse_force_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2648{
2649 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_SSLV3;
2650 return 0;
2651}
2652
2653/* parse the "force-tlsv10" server keyword */
2654static int srv_parse_force_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2655{
2656 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV10;
2657 return 0;
2658}
2659
2660/* parse the "force-tlsv11" server keyword */
2661static int srv_parse_force_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2662{
2663#if SSL_OP_NO_TLSv1_1
2664 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV11;
2665 return 0;
2666#else
2667 if (err)
2668 memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[*cur_arg]);
2669 return ERR_ALERT | ERR_FATAL;
2670#endif
2671}
2672
2673/* parse the "force-tlsv12" server keyword */
2674static int srv_parse_force_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2675{
2676#if SSL_OP_NO_TLSv1_2
2677 newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV12;
2678 return 0;
2679#else
2680 if (err)
2681 memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[*cur_arg]);
2682 return ERR_ALERT | ERR_FATAL;
2683#endif
2684}
2685
2686/* parse the "no-sslv3" server keyword */
2687static int srv_parse_no_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2688{
2689 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_SSLV3;
2690 return 0;
2691}
2692
2693/* parse the "no-tlsv10" server keyword */
2694static int srv_parse_no_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2695{
2696 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV10;
2697 return 0;
2698}
2699
2700/* parse the "no-tlsv11" server keyword */
2701static int srv_parse_no_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2702{
2703 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV11;
2704 return 0;
2705}
2706
2707/* parse the "no-tlsv12" server keyword */
2708static int srv_parse_no_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2709{
2710 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV12;
2711 return 0;
2712}
2713
Emeric Brunf9c5c472012-10-11 15:28:34 +02002714/* parse the "no-tls-tickets" server keyword */
2715static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2716{
2717 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
2718 return 0;
2719}
2720
Willy Tarreau92faadf2012-10-10 23:04:25 +02002721/* parse the "ssl" server keyword */
2722static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2723{
2724 newsrv->use_ssl = 1;
2725 if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
2726 newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers);
2727 return 0;
2728}
2729
Emeric Brunef42d922012-10-11 16:11:36 +02002730/* parse the "verify" server keyword */
2731static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2732{
2733 if (!*args[*cur_arg + 1]) {
2734 if (err)
2735 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
2736 return ERR_ALERT | ERR_FATAL;
2737 }
2738
2739 if (strcmp(args[*cur_arg + 1], "none") == 0)
2740 newsrv->ssl_ctx.verify = SSL_VERIFY_NONE;
2741 else if (strcmp(args[*cur_arg + 1], "required") == 0)
2742 newsrv->ssl_ctx.verify = SSL_VERIFY_PEER;
2743 else {
2744 if (err)
2745 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
2746 args[*cur_arg], args[*cur_arg + 1]);
2747 return ERR_ALERT | ERR_FATAL;
2748 }
2749
2750 return 0;
2751}
2752
Willy Tarreau7875d092012-09-10 08:20:03 +02002753/* Note: must not be declared <const> as its list will be overwritten.
2754 * Please take care of keeping this list alphabetically sorted.
2755 */
2756static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Emeric Brun2525b6b2012-10-18 15:59:43 +02002757 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
2758 { "ssl_c_ca_err_depth", smp_fetch_ssl_c_ca_err_depth, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
2759 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun87855892012-10-17 17:39:35 +02002760 { "ssl_c_i_dn", smp_fetch_ssl_c_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun521a0112012-10-22 12:22:55 +02002761 { "ssl_c_key_alg", smp_fetch_ssl_c_key_alg, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brunce5ad802012-10-22 14:11:22 +02002762 { "ssl_c_notafter", smp_fetch_ssl_c_notafter, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
2763 { "ssl_c_notbefore", smp_fetch_ssl_c_notbefore, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun7f56e742012-10-19 18:15:40 +02002764 { "ssl_c_sig_alg", smp_fetch_ssl_c_sig_alg, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun87855892012-10-17 17:39:35 +02002765 { "ssl_c_s_dn", smp_fetch_ssl_c_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau8d598402012-10-22 17:58:39 +02002766 { "ssl_c_serial", smp_fetch_ssl_c_serial, 0, NULL, SMP_T_BIN, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002767 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Bruna7359fd2012-10-17 15:03:11 +02002768 { "ssl_c_version", smp_fetch_ssl_c_version, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun87855892012-10-17 17:39:35 +02002769 { "ssl_f_i_dn", smp_fetch_ssl_f_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun521a0112012-10-22 12:22:55 +02002770 { "ssl_f_key_alg", smp_fetch_ssl_f_key_alg, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brunce5ad802012-10-22 14:11:22 +02002771 { "ssl_f_notafter", smp_fetch_ssl_f_notafter, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
2772 { "ssl_f_notbefore", smp_fetch_ssl_f_notbefore, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun7f56e742012-10-19 18:15:40 +02002773 { "ssl_f_sig_alg", smp_fetch_ssl_f_sig_alg, 0, NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun87855892012-10-17 17:39:35 +02002774 { "ssl_f_s_dn", smp_fetch_ssl_f_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau8d598402012-10-22 17:58:39 +02002775 { "ssl_f_serial", smp_fetch_ssl_f_serial, 0, NULL, SMP_T_BIN, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Bruna7359fd2012-10-17 15:03:11 +02002776 { "ssl_f_version", smp_fetch_ssl_f_version, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002777 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun589fcad2012-10-16 14:13:26 +02002778 { "ssl_fc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
2779 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002780 { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
2781 { "ssl_fc_has_sni", smp_fetch_ssl_fc_has_sni, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreaua33c6542012-10-15 13:19:06 +02002782#ifdef OPENSSL_NPN_NEGOTIATED
Emeric Brun2525b6b2012-10-18 15:59:43 +02002783 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreaua33c6542012-10-15 13:19:06 +02002784#endif
Emeric Brun589fcad2012-10-16 14:13:26 +02002785 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
2786 { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brunfe68f682012-10-16 14:59:28 +02002787 { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002788 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau7875d092012-09-10 08:20:03 +02002789 { NULL, NULL, 0, 0, 0 },
2790}};
2791
2792/* Note: must not be declared <const> as its list will be overwritten.
2793 * Please take care of keeping this list alphabetically sorted.
2794 */
2795static struct acl_kw_list acl_kws = {{ },{
Emeric Brun2525b6b2012-10-18 15:59:43 +02002796 { "ssl_c_ca_err", acl_parse_int, smp_fetch_ssl_c_ca_err, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2797 { "ssl_c_ca_err_depth", acl_parse_int, smp_fetch_ssl_c_ca_err_depth, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2798 { "ssl_c_err", acl_parse_int, smp_fetch_ssl_c_err, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun87855892012-10-17 17:39:35 +02002799 { "ssl_c_i_dn", acl_parse_str, smp_fetch_ssl_c_i_dn, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, ARG2(0,STR,SINT) },
Emeric Brun521a0112012-10-22 12:22:55 +02002800 { "ssl_c_key_alg", acl_parse_str, smp_fetch_ssl_c_key_alg, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brunce5ad802012-10-22 14:11:22 +02002801 { "ssl_c_notafter", acl_parse_str, smp_fetch_ssl_c_notafter, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2802 { "ssl_c_notbefore", acl_parse_str, smp_fetch_ssl_c_notbefore, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun7f56e742012-10-19 18:15:40 +02002803 { "ssl_c_sig_alg", acl_parse_str, smp_fetch_ssl_c_sig_alg, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun87855892012-10-17 17:39:35 +02002804 { "ssl_c_s_dn", acl_parse_str, smp_fetch_ssl_c_s_dn, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, ARG2(0,STR,SINT) },
Willy Tarreau8d598402012-10-22 17:58:39 +02002805 { "ssl_c_serial", acl_parse_bin, smp_fetch_ssl_c_serial, acl_match_bin, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002806 { "ssl_c_verify", acl_parse_int, smp_fetch_ssl_c_verify, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Bruna7359fd2012-10-17 15:03:11 +02002807 { "ssl_c_version", acl_parse_int, smp_fetch_ssl_c_version, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun87855892012-10-17 17:39:35 +02002808 { "ssl_f_i_dn", acl_parse_str, smp_fetch_ssl_f_i_dn, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, ARG2(0,STR,SINT) },
Emeric Brun521a0112012-10-22 12:22:55 +02002809 { "ssl_f_key_alg", acl_parse_str, smp_fetch_ssl_f_key_alg, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brunce5ad802012-10-22 14:11:22 +02002810 { "ssl_f_notafter", acl_parse_str, smp_fetch_ssl_f_notafter, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2811 { "ssl_f_notbefore", acl_parse_str, smp_fetch_ssl_f_notbefore, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun7f56e742012-10-19 18:15:40 +02002812 { "ssl_f_sig_alg", acl_parse_str, smp_fetch_ssl_f_sig_alg, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun87855892012-10-17 17:39:35 +02002813 { "ssl_f_s_dn", acl_parse_str, smp_fetch_ssl_f_s_dn, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, ARG2(0,STR,SINT) },
Willy Tarreau8d598402012-10-22 17:58:39 +02002814 { "ssl_f_serial", acl_parse_bin, smp_fetch_ssl_f_serial, acl_match_bin, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Bruna7359fd2012-10-17 15:03:11 +02002815 { "ssl_f_version", acl_parse_int, smp_fetch_ssl_f_version, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002816 { "ssl_fc", acl_parse_int, smp_fetch_ssl_fc, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun589fcad2012-10-16 14:13:26 +02002817 { "ssl_fc_alg_keysize", acl_parse_str, smp_fetch_ssl_fc_alg_keysize, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2818 { "ssl_fc_cipher", acl_parse_str, smp_fetch_ssl_fc_cipher, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002819 { "ssl_fc_has_crt", acl_parse_int, smp_fetch_ssl_fc_has_crt, acl_match_nothing, ACL_USE_L6REQ_PERMANENT, 0 },
2820 { "ssl_fc_has_sni", acl_parse_int, smp_fetch_ssl_fc_has_sni, acl_match_nothing, ACL_USE_L6REQ_PERMANENT, 0 },
Willy Tarreaua33c6542012-10-15 13:19:06 +02002821#ifdef OPENSSL_NPN_NEGOTIATED
Emeric Brun2525b6b2012-10-18 15:59:43 +02002822 { "ssl_fc_npn", acl_parse_str, smp_fetch_ssl_fc_npn, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Willy Tarreaua33c6542012-10-15 13:19:06 +02002823#endif
Emeric Brun589fcad2012-10-16 14:13:26 +02002824 { "ssl_fc_protocol", acl_parse_str, smp_fetch_ssl_fc_protocol, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2825 { "ssl_fc_use_keysize", acl_parse_str, smp_fetch_ssl_fc_use_keysize, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
Emeric Brun2525b6b2012-10-18 15:59:43 +02002826 { "ssl_fc_sni", acl_parse_str, smp_fetch_ssl_fc_sni, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 },
2827 { "ssl_fc_sni_end", acl_parse_str, smp_fetch_ssl_fc_sni, acl_match_end, ACL_USE_L6REQ_PERMANENT, 0 },
2828 { "ssl_fc_sni_reg", acl_parse_reg, smp_fetch_ssl_fc_sni, acl_match_reg, ACL_USE_L6REQ_PERMANENT, 0 },
Willy Tarreau7875d092012-09-10 08:20:03 +02002829 { NULL, NULL, NULL, NULL },
2830}};
2831
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002832/* Note: must not be declared <const> as its list will be overwritten.
2833 * Please take care of keeping this list alphabetically sorted, doing so helps
2834 * all code contributors.
2835 * Optional keywords are also declared with a NULL ->parse() function so that
2836 * the config parser can report an appropriate error when a known keyword was
2837 * not enabled.
2838 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002839static struct bind_kw_list bind_kws = { "SSL", { }, {
Emeric Brunfb510ea2012-10-05 12:00:26 +02002840 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
Emeric Brun2d0c4822012-10-02 13:45:20 +02002841 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
2842 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emeric Brunfb510ea2012-10-05 12:00:26 +02002843 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
Emeric Brun2d0c4822012-10-02 13:45:20 +02002844 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
2845 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
2846 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emeric Brun2cb7ae52012-10-05 14:14:21 +02002847 { "force-sslv3", bind_parse_force_sslv3, 0 }, /* force SSLv3 */
2848 { "force-tlsv10", bind_parse_force_tlsv10, 0 }, /* force TLSv10 */
2849 { "force-tlsv11", bind_parse_force_tlsv11, 0 }, /* force TLSv11 */
2850 { "force-tlsv12", bind_parse_force_tlsv12, 0 }, /* force TLSv12 */
Emeric Brun9b3009b2012-10-05 11:55:06 +02002851 { "no-sslv3", bind_parse_no_sslv3, 0 }, /* disable SSLv3 */
2852 { "no-tlsv10", bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */
2853 { "no-tlsv11", bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */
2854 { "no-tlsv12", bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */
Emeric Brun2d0c4822012-10-02 13:45:20 +02002855 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
Emeric Brun2d0c4822012-10-02 13:45:20 +02002856 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
2857 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02002858 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002859 { NULL, NULL, 0 },
2860}};
Emeric Brun46591952012-05-18 15:47:34 +02002861
Willy Tarreau92faadf2012-10-10 23:04:25 +02002862/* Note: must not be declared <const> as its list will be overwritten.
2863 * Please take care of keeping this list alphabetically sorted, doing so helps
2864 * all code contributors.
2865 * Optional keywords are also declared with a NULL ->parse() function so that
2866 * the config parser can report an appropriate error when a known keyword was
2867 * not enabled.
2868 */
2869static struct srv_kw_list srv_kws = { "SSL", { }, {
Emeric Brunef42d922012-10-11 16:11:36 +02002870 { "ca-file", srv_parse_ca_file, 1, 0 }, /* set CAfile to process verify server cert */
Emeric Brunecc91fe2012-10-11 15:05:10 +02002871 { "check-ssl", srv_parse_check_ssl, 0, 0 }, /* enable SSL for health checks */
2872 { "ciphers", srv_parse_ciphers, 1, 0 }, /* select the cipher suite */
Emeric Brunef42d922012-10-11 16:11:36 +02002873 { "crl-file", srv_parse_crl_file, 1, 0 }, /* set certificate revocation list file use on server cert verify */
Emeric Bruna7aa3092012-10-26 12:58:00 +02002874 { "crt", srv_parse_crt, 1, 0 }, /* set client certificate */
Emeric Brunecc91fe2012-10-11 15:05:10 +02002875 { "force-sslv3", srv_parse_force_sslv3, 0, 0 }, /* force SSLv3 */
2876 { "force-tlsv10", srv_parse_force_tlsv10, 0, 0 }, /* force TLSv10 */
2877 { "force-tlsv11", srv_parse_force_tlsv11, 0, 0 }, /* force TLSv11 */
2878 { "force-tlsv12", srv_parse_force_tlsv12, 0, 0 }, /* force TLSv12 */
2879 { "no-sslv3", srv_parse_no_sslv3, 0, 0 }, /* disable SSLv3 */
2880 { "no-tlsv10", srv_parse_no_tlsv10, 0, 0 }, /* disable TLSv10 */
2881 { "no-tlsv11", srv_parse_no_tlsv11, 0, 0 }, /* disable TLSv11 */
2882 { "no-tlsv12", srv_parse_no_tlsv12, 0, 0 }, /* disable TLSv12 */
Emeric Brunf9c5c472012-10-11 15:28:34 +02002883 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 0 }, /* disable session resumption tickets */
Emeric Brunecc91fe2012-10-11 15:05:10 +02002884 { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */
Emeric Brunef42d922012-10-11 16:11:36 +02002885 { "verify", srv_parse_verify, 1, 0 }, /* set SSL verify method */
Willy Tarreau92faadf2012-10-10 23:04:25 +02002886 { NULL, NULL, 0, 0 },
2887}};
2888
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002889/* transport-layer operations for SSL sockets */
2890struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +02002891 .snd_buf = ssl_sock_from_buf,
2892 .rcv_buf = ssl_sock_to_buf,
2893 .rcv_pipe = NULL,
2894 .snd_pipe = NULL,
2895 .shutr = NULL,
2896 .shutw = ssl_sock_shutw,
2897 .close = ssl_sock_close,
2898 .init = ssl_sock_init,
2899};
2900
2901__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +02002902static void __ssl_sock_init(void)
2903{
Emeric Brun46591952012-05-18 15:47:34 +02002904 STACK_OF(SSL_COMP)* cm;
2905
2906 SSL_library_init();
2907 cm = SSL_COMP_get_compression_methods();
2908 sk_SSL_COMP_zero(cm);
Willy Tarreau7875d092012-09-10 08:20:03 +02002909 sample_register_fetches(&sample_fetch_keywords);
2910 acl_register_keywords(&acl_kws);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02002911 bind_register_keywords(&bind_kws);
Willy Tarreau92faadf2012-10-10 23:04:25 +02002912 srv_register_keywords(&srv_kws);
Emeric Brun46591952012-05-18 15:47:34 +02002913}
2914
2915/*
2916 * Local variables:
2917 * c-indent-level: 8
2918 * c-basic-offset: 8
2919 * End:
2920 */