Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 2 | * SSL/TLS transport layer over SOCK_STREAM sockets |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
Willy Tarreau | 69845df | 2012-09-10 09:43:09 +0200 | [diff] [blame] | 11 | * 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 Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #define _GNU_SOURCE |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 26 | #include <ctype.h> |
| 27 | #include <dirent.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 32 | #include <string.h> |
| 33 | #include <unistd.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 34 | |
| 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 Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 42 | #include <openssl/x509.h> |
| 43 | #include <openssl/x509v3.h> |
| 44 | #include <openssl/x509.h> |
| 45 | #include <openssl/err.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 46 | |
| 47 | #include <common/buffer.h> |
| 48 | #include <common/compat.h> |
| 49 | #include <common/config.h> |
| 50 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 51 | #include <common/errors.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 52 | #include <common/standard.h> |
| 53 | #include <common/ticks.h> |
| 54 | #include <common/time.h> |
| 55 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 56 | #include <ebsttree.h> |
| 57 | |
| 58 | #include <types/global.h> |
| 59 | #include <types/ssl_sock.h> |
| 60 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 61 | #include <proto/acl.h> |
| 62 | #include <proto/arg.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 63 | #include <proto/connection.h> |
| 64 | #include <proto/fd.h> |
| 65 | #include <proto/freq_ctr.h> |
| 66 | #include <proto/frontend.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 67 | #include <proto/listener.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 68 | #include <proto/server.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 69 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 70 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 71 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 72 | #include <proto/ssl_sock.h> |
| 73 | #include <proto/task.h> |
| 74 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 75 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 76 | /* 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 Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 86 | |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 87 | static int sslconns = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 88 | |
| 89 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 90 | { |
| 91 | struct connection *conn = (struct connection *)SSL_get_app_data(ssl); |
| 92 | (void)ret; /* shut gcc stupid warning */ |
| 93 | |
| 94 | if (where & SSL_CB_HANDSHAKE_START) { |
| 95 | /* Disable renegotiation (CVE-2009-3555) */ |
| 96 | if (conn->flags & CO_FL_CONNECTED) |
| 97 | conn->flags |= CO_FL_ERROR; |
| 98 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 101 | /* Callback is called for each certificate of the chain during a verify |
| 102 | ok is set to 1 if preverify detect no error on current certificate. |
| 103 | Returns 0 to break the handshake, 1 otherwise. */ |
| 104 | int ssl_sock_verifycbk(int ok, X509_STORE_CTX *x_store) |
| 105 | { |
| 106 | SSL *ssl; |
| 107 | struct connection *conn; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 108 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 109 | |
| 110 | ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 111 | conn = (struct connection *)SSL_get_app_data(ssl); |
| 112 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 113 | conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 114 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 115 | if (ok) /* no errors */ |
| 116 | return ok; |
| 117 | |
| 118 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 119 | err = X509_STORE_CTX_get_error(x_store); |
| 120 | |
| 121 | /* check if CA error needs to be ignored */ |
| 122 | if (depth > 0) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 123 | if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) { |
| 124 | conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 125 | conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 128 | if (target_client(&conn->target)->bind_conf->ca_ignerr & (1ULL << err)) |
| 129 | return 1; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 134 | if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st)) |
| 135 | conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 136 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 137 | /* check if certificate error needs to be ignored */ |
| 138 | if (target_client(&conn->target)->bind_conf->crt_ignerr & (1ULL << err)) |
| 139 | return 1; |
| 140 | |
| 141 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 144 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 145 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 146 | * warning when no match is found, which implies the default (first) cert |
| 147 | * will keep being used. |
| 148 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 149 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, struct bind_conf *s) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 150 | { |
| 151 | const char *servername; |
| 152 | const char *wildp = NULL; |
| 153 | struct ebmb_node *node; |
| 154 | int i; |
| 155 | (void)al; /* shut gcc stupid warning */ |
| 156 | |
| 157 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
| 158 | if (!servername) |
| 159 | return SSL_TLSEXT_ERR_NOACK; |
| 160 | |
| 161 | for (i = 0; i < trashlen; i++) { |
| 162 | if (!servername[i]) |
| 163 | break; |
| 164 | trash[i] = tolower(servername[i]); |
| 165 | if (!wildp && (trash[i] == '.')) |
| 166 | wildp = &trash[i]; |
| 167 | } |
| 168 | trash[i] = 0; |
| 169 | |
| 170 | /* lookup in full qualified names */ |
| 171 | node = ebst_lookup(&s->sni_ctx, trash); |
| 172 | if (!node) { |
| 173 | if (!wildp) |
| 174 | return SSL_TLSEXT_ERR_ALERT_WARNING; |
| 175 | |
| 176 | /* lookup in full wildcards names */ |
| 177 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 178 | if (!node) |
| 179 | return SSL_TLSEXT_ERR_ALERT_WARNING; |
| 180 | } |
| 181 | |
| 182 | /* switch ctx */ |
| 183 | SSL_set_SSL_CTX(ssl, container_of(node, struct sni_ctx, name)->ctx); |
| 184 | return SSL_TLSEXT_ERR_OK; |
| 185 | } |
| 186 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 187 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 188 | #ifndef OPENSSL_NO_DH |
| 189 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
| 190 | if an error occured, and 0 if parameter not found. */ |
| 191 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 192 | { |
| 193 | int ret = -1; |
| 194 | BIO *in; |
| 195 | DH *dh = NULL; |
| 196 | |
| 197 | in = BIO_new(BIO_s_file()); |
| 198 | if (in == NULL) |
| 199 | goto end; |
| 200 | |
| 201 | if (BIO_read_filename(in, file) <= 0) |
| 202 | goto end; |
| 203 | |
| 204 | dh = PEM_read_bio_DHparams(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata); |
| 205 | if (dh) { |
| 206 | SSL_CTX_set_tmp_dh(ctx, dh); |
| 207 | ret = 1; |
| 208 | goto end; |
| 209 | } |
| 210 | |
| 211 | ret = 0; /* DH params not found */ |
| 212 | end: |
| 213 | if (dh) |
| 214 | DH_free(dh); |
| 215 | |
| 216 | if (in) |
| 217 | BIO_free(in); |
| 218 | |
| 219 | return ret; |
| 220 | } |
| 221 | #endif |
| 222 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 223 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 224 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 225 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 226 | int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 227 | { |
| 228 | BIO *in; |
| 229 | X509 *x = NULL, *ca; |
| 230 | int i, len, err; |
| 231 | int ret = -1; |
| 232 | int order = 0; |
| 233 | X509_NAME *xname; |
| 234 | char *str; |
| 235 | struct sni_ctx *sc; |
| 236 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 237 | STACK_OF(GENERAL_NAME) *names; |
| 238 | #endif |
| 239 | |
| 240 | in = BIO_new(BIO_s_file()); |
| 241 | if (in == NULL) |
| 242 | goto end; |
| 243 | |
| 244 | if (BIO_read_filename(in, file) <= 0) |
| 245 | goto end; |
| 246 | |
| 247 | x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata); |
| 248 | if (x == NULL) |
| 249 | goto end; |
| 250 | |
| 251 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 252 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 253 | if (names) { |
| 254 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 255 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 256 | if (name->type == GEN_DNS) { |
| 257 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 258 | if ((len = strlen(str))) { |
| 259 | int j; |
| 260 | |
| 261 | if (*str != '*') { |
| 262 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
| 263 | for (j = 0; j < len; j++) |
| 264 | sc->name.key[j] = tolower(str[j]); |
| 265 | sc->name.key[len] = 0; |
| 266 | sc->order = order++; |
| 267 | sc->ctx = ctx; |
| 268 | ebst_insert(&s->sni_ctx, &sc->name); |
| 269 | } |
| 270 | else { |
| 271 | sc = malloc(sizeof(struct sni_ctx) + len); |
| 272 | for (j = 1; j < len; j++) |
| 273 | sc->name.key[j-1] = tolower(str[j]); |
| 274 | sc->name.key[len-1] = 0; |
| 275 | sc->order = order++; |
| 276 | sc->ctx = ctx; |
| 277 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 278 | } |
| 279 | } |
| 280 | OPENSSL_free(str); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 285 | } |
| 286 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 287 | |
| 288 | xname = X509_get_subject_name(x); |
| 289 | i = -1; |
| 290 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 291 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
| 292 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) { |
| 293 | if ((len = strlen(str))) { |
| 294 | int j; |
| 295 | |
| 296 | if (*str != '*') { |
| 297 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
| 298 | for (j = 0; j < len; j++) |
| 299 | sc->name.key[j] = tolower(str[j]); |
| 300 | sc->name.key[len] = 0; |
| 301 | sc->order = order++; |
| 302 | sc->ctx = ctx; |
| 303 | ebst_insert(&s->sni_ctx, &sc->name); |
| 304 | } |
| 305 | else { |
| 306 | sc = malloc(sizeof(struct sni_ctx) + len); |
| 307 | for (j = 1; j < len; j++) |
| 308 | sc->name.key[j-1] = tolower(str[j]); |
| 309 | sc->name.key[len-1] = 0; |
| 310 | sc->order = order++; |
| 311 | sc->ctx = ctx; |
| 312 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 313 | } |
| 314 | } |
| 315 | OPENSSL_free(str); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 320 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 321 | goto end; |
| 322 | |
| 323 | if (ctx->extra_certs != NULL) { |
| 324 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 325 | ctx->extra_certs = NULL; |
| 326 | } |
| 327 | |
| 328 | while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata))) { |
| 329 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 330 | X509_free(ca); |
| 331 | goto end; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | err = ERR_get_error(); |
| 336 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 337 | /* we successfully reached the last cert in the file */ |
| 338 | ret = 1; |
| 339 | } |
| 340 | ERR_clear_error(); |
| 341 | |
| 342 | end: |
| 343 | if (x) |
| 344 | X509_free(x); |
| 345 | |
| 346 | if (in) |
| 347 | BIO_free(in); |
| 348 | |
| 349 | return ret; |
| 350 | } |
| 351 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 352 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 353 | { |
| 354 | int ret; |
| 355 | SSL_CTX *ctx; |
| 356 | |
| 357 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 358 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 359 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 360 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 365 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 366 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 367 | SSL_CTX_free(ctx); |
| 368 | return 1; |
| 369 | } |
| 370 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 371 | ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 372 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 373 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 374 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 375 | if (ret < 0) /* serious error, must do that ourselves */ |
| 376 | SSL_CTX_free(ctx); |
| 377 | return 1; |
| 378 | } |
| 379 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 380 | * the tree, so it will be discovered and cleaned in time. |
| 381 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 382 | #ifndef OPENSSL_NO_DH |
| 383 | ret = ssl_sock_load_dh_params(ctx, path); |
| 384 | if (ret < 0) { |
| 385 | if (err) |
| 386 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 387 | *err ? *err : "", path); |
| 388 | return 1; |
| 389 | } |
| 390 | #endif |
| 391 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 392 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 393 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 394 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 395 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 396 | return 1; |
| 397 | } |
| 398 | #endif |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 399 | if (!bind_conf->default_ctx) |
| 400 | bind_conf->default_ctx = ctx; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 405 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 406 | { |
| 407 | struct dirent *de; |
| 408 | DIR *dir; |
| 409 | struct stat buf; |
| 410 | int pathlen = 0; |
| 411 | char *end, *fp; |
| 412 | int cfgerr = 0; |
| 413 | |
| 414 | if (!(dir = opendir(path))) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 415 | return ssl_sock_load_cert_file(path, bind_conf, curproxy, err); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 416 | |
| 417 | /* strip trailing slashes, including first one */ |
| 418 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 419 | *end = 0; |
| 420 | |
| 421 | if (end >= path) |
| 422 | pathlen = end + 1 - path; |
| 423 | fp = malloc(pathlen + 1 + NAME_MAX + 1); |
| 424 | |
| 425 | while ((de = readdir(dir))) { |
| 426 | snprintf(fp, pathlen + 1 + NAME_MAX + 1, "%s/%s", path, de->d_name); |
| 427 | if (stat(fp, &buf) != 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 428 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 429 | err && *err ? *err : "", fp, strerror(errno)); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 430 | cfgerr++; |
| 431 | continue; |
| 432 | } |
| 433 | if (!S_ISREG(buf.st_mode)) |
| 434 | continue; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 435 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, curproxy, err); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 436 | } |
| 437 | free(fp); |
| 438 | closedir(dir); |
| 439 | return cfgerr; |
| 440 | } |
| 441 | |
| 442 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 443 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 444 | #endif |
| 445 | |
| 446 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 447 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
| 448 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 449 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 450 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 451 | #endif |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 452 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 453 | #define SSL_OP_NO_TICKET 0 |
| 454 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 455 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 456 | #define SSL_OP_NO_COMPRESSION 0 |
| 457 | #endif |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 458 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 459 | #define SSL_OP_NO_TLSv1_1 0 |
| 460 | #endif |
| 461 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 462 | #define SSL_OP_NO_TLSv1_2 0 |
| 463 | #endif |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 464 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 465 | #define SSL_OP_SINGLE_DH_USE 0 |
| 466 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 467 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 468 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 469 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 470 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 471 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 472 | #endif |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 473 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, SSL_CTX *ctx, struct proxy *curproxy) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 474 | { |
| 475 | int cfgerr = 0; |
| 476 | int ssloptions = |
| 477 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 478 | SSL_OP_NO_SSLv2 | |
| 479 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 480 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 481 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 482 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
| 483 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 484 | int sslmode = |
| 485 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 486 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 487 | SSL_MODE_RELEASE_BUFFERS; |
| 488 | |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 489 | if (bind_conf->ssl_options & BC_SSL_O_NO_SSLV3) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 490 | ssloptions |= SSL_OP_NO_SSLv3; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 491 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV10) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 492 | ssloptions |= SSL_OP_NO_TLSv1; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 493 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV11) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 494 | ssloptions |= SSL_OP_NO_TLSv1_1; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 495 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLSV12) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 496 | ssloptions |= SSL_OP_NO_TLSv1_2; |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 497 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 498 | ssloptions |= SSL_OP_NO_TICKET; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 499 | if (bind_conf->ssl_options & BC_SSL_O_USE_SSLV3) |
| 500 | SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()); |
| 501 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV10) |
| 502 | SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()); |
| 503 | #if SSL_OP_NO_TLSv1_1 |
| 504 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV11) |
| 505 | SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method()); |
| 506 | #endif |
| 507 | #if SSL_OP_NO_TLSv1_2 |
| 508 | if (bind_conf->ssl_options & BC_SSL_O_USE_TLSV12) |
| 509 | SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method()); |
| 510 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 511 | |
| 512 | SSL_CTX_set_options(ctx, ssloptions); |
| 513 | SSL_CTX_set_mode(ctx, sslmode); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 514 | SSL_CTX_set_verify(ctx, bind_conf->verify ? bind_conf->verify : SSL_VERIFY_NONE, ssl_sock_verifycbk); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 515 | if (bind_conf->verify & SSL_VERIFY_PEER) { |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 516 | if (bind_conf->ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 517 | /* load CAfile to verify */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 518 | if (!SSL_CTX_load_verify_locations(ctx, bind_conf->ca_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 519 | Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 520 | curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 521 | cfgerr++; |
| 522 | } |
| 523 | /* set CA names fo client cert request, function returns void */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 524 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(bind_conf->ca_file)); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 525 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 526 | #ifdef X509_V_FLAG_CRL_CHECK |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 527 | if (bind_conf->crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 528 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 529 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 530 | if (!store || !X509_STORE_load_locations(store, bind_conf->crl_file, NULL)) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 531 | Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 532 | curproxy->id, bind_conf->ca_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 533 | cfgerr++; |
| 534 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 535 | else { |
| 536 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 537 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 538 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 539 | #endif |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 540 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 541 | |
| 542 | shared_context_set_cache(ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 543 | if (bind_conf->ciphers && |
| 544 | !SSL_CTX_set_cipher_list(ctx, bind_conf->ciphers)) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 545 | Alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 546 | curproxy->id, bind_conf->ciphers, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 547 | cfgerr++; |
| 548 | } |
| 549 | |
| 550 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
| 551 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 552 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 553 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 554 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 555 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 556 | if (bind_conf->ecdhe) { |
| 557 | int i; |
| 558 | EC_KEY *ecdh; |
| 559 | |
| 560 | i = OBJ_sn2nid(bind_conf->ecdhe); |
| 561 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
| 562 | Alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 563 | curproxy->id, bind_conf->ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 564 | cfgerr++; |
| 565 | } |
| 566 | else { |
| 567 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 568 | EC_KEY_free(ecdh); |
| 569 | } |
| 570 | } |
| 571 | #endif |
| 572 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 573 | return cfgerr; |
| 574 | } |
| 575 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 576 | /* prepare ssl context from servers options. Returns an error count */ |
| 577 | int ssl_sock_prepare_srv_ctx(struct server *srv, struct proxy *curproxy) |
| 578 | { |
| 579 | int cfgerr = 0; |
| 580 | int options = |
| 581 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 582 | SSL_OP_NO_SSLv2 | |
| 583 | SSL_OP_NO_COMPRESSION; |
| 584 | int mode = |
| 585 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 586 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 587 | SSL_MODE_RELEASE_BUFFERS; |
| 588 | |
| 589 | /* Initiate SSL context for current server */ |
| 590 | srv->ssl_ctx.reused_sess = NULL; |
| 591 | if (srv->use_ssl) |
| 592 | srv->xprt = &ssl_sock; |
| 593 | if (srv->check.use_ssl) |
| 594 | srv->check.xprt = &ssl_sock; |
| 595 | |
| 596 | srv->ssl_ctx.ctx = SSL_CTX_new(SSLv23_client_method()); |
| 597 | if (!srv->ssl_ctx.ctx) { |
| 598 | Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 599 | proxy_type_str(curproxy), curproxy->id, |
| 600 | srv->id); |
| 601 | cfgerr++; |
| 602 | return cfgerr; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_SSLV3) |
| 607 | options |= SSL_OP_NO_SSLv3; |
| 608 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV10) |
| 609 | options |= SSL_OP_NO_TLSv1; |
| 610 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV11) |
| 611 | options |= SSL_OP_NO_TLSv1_1; |
| 612 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLSV12) |
| 613 | options |= SSL_OP_NO_TLSv1_2; |
| 614 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_SSLV3) |
| 615 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, SSLv3_client_method()); |
| 616 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV10) |
| 617 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_client_method()); |
| 618 | #if SSL_OP_NO_TLSv1_1 |
| 619 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV11) |
| 620 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_1_client_method()); |
| 621 | #endif |
| 622 | #if SSL_OP_NO_TLSv1_2 |
| 623 | if (srv->ssl_ctx.options & SRV_SSL_O_USE_TLSV12) |
| 624 | SSL_CTX_set_ssl_version(srv->ssl_ctx.ctx, TLSv1_2_client_method()); |
| 625 | #endif |
| 626 | |
| 627 | SSL_CTX_set_options(srv->ssl_ctx.ctx, options); |
| 628 | SSL_CTX_set_mode(srv->ssl_ctx.ctx, mode); |
| 629 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, SSL_VERIFY_NONE, NULL); |
| 630 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF); |
| 631 | if (srv->ssl_ctx.ciphers && |
| 632 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
| 633 | Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 634 | curproxy->id, srv->id, |
| 635 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
| 636 | cfgerr++; |
| 637 | } |
| 638 | |
| 639 | return cfgerr; |
| 640 | } |
| 641 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 642 | /* Walks down the two trees in bind_conf and prepares all certs. The pointer may |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 643 | * be NULL, in which case nothing is done. Returns the number of errors |
| 644 | * encountered. |
| 645 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 646 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf, struct proxy *px) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 647 | { |
| 648 | struct ebmb_node *node; |
| 649 | struct sni_ctx *sni; |
| 650 | int err = 0; |
| 651 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 652 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 653 | return 0; |
| 654 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 655 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 656 | while (node) { |
| 657 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 658 | if (!sni->order) /* only initialize the CTX on its first occurrence */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 659 | err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 660 | node = ebmb_next(node); |
| 661 | } |
| 662 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 663 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 664 | while (node) { |
| 665 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 666 | if (!sni->order) /* only initialize the CTX on its first occurrence */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 667 | err += ssl_sock_prepare_ctx(bind_conf, sni->ctx, px); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 668 | node = ebmb_next(node); |
| 669 | } |
| 670 | return err; |
| 671 | } |
| 672 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 673 | /* Walks down the two trees in bind_conf and frees all the certs. The pointer may |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 674 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 675 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 676 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 677 | { |
| 678 | struct ebmb_node *node, *back; |
| 679 | struct sni_ctx *sni; |
| 680 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 681 | if (!bind_conf || !bind_conf->is_ssl) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 682 | return; |
| 683 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 684 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 685 | while (node) { |
| 686 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 687 | back = ebmb_next(node); |
| 688 | ebmb_delete(node); |
| 689 | if (!sni->order) /* only free the CTX on its first occurrence */ |
| 690 | SSL_CTX_free(sni->ctx); |
| 691 | free(sni); |
| 692 | node = back; |
| 693 | } |
| 694 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 695 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 696 | while (node) { |
| 697 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 698 | back = ebmb_next(node); |
| 699 | ebmb_delete(node); |
| 700 | if (!sni->order) /* only free the CTX on its first occurrence */ |
| 701 | SSL_CTX_free(sni->ctx); |
| 702 | free(sni); |
| 703 | node = back; |
| 704 | } |
| 705 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 706 | bind_conf->default_ctx = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 707 | } |
| 708 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 709 | /* |
| 710 | * This function is called if SSL * context is not yet allocated. The function |
| 711 | * is designed to be called before any other data-layer operation and sets the |
| 712 | * handshake flag on the connection. It is safe to call it multiple times. |
| 713 | * It returns 0 on success and -1 in error case. |
| 714 | */ |
| 715 | static int ssl_sock_init(struct connection *conn) |
| 716 | { |
| 717 | /* already initialized */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 718 | if (conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 719 | return 0; |
| 720 | |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 721 | if (global.maxsslconn && sslconns >= global.maxsslconn) |
| 722 | return -1; |
| 723 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 724 | /* If it is in client mode initiate SSL session |
| 725 | in connect state otherwise accept state */ |
| 726 | if (target_srv(&conn->target)) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 727 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 728 | conn->xprt_ctx = SSL_new(target_srv(&conn->target)->ssl_ctx.ctx); |
| 729 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 730 | return -1; |
| 731 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 732 | SSL_set_connect_state(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 733 | if (target_srv(&conn->target)->ssl_ctx.reused_sess) |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 734 | SSL_set_session(conn->xprt_ctx, target_srv(&conn->target)->ssl_ctx.reused_sess); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 735 | |
| 736 | /* set fd on SSL session context */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 737 | SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 738 | |
| 739 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 740 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 741 | |
| 742 | sslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 743 | return 0; |
| 744 | } |
| 745 | else if (target_client(&conn->target)) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 746 | /* Alloc a new SSL session ctx */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 747 | conn->xprt_ctx = SSL_new(target_client(&conn->target)->bind_conf->default_ctx); |
| 748 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 749 | return -1; |
| 750 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 751 | SSL_set_accept_state(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 752 | |
| 753 | /* set fd on SSL session context */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 754 | SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 755 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 756 | /* set connection pointer */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 757 | SSL_set_app_data(conn->xprt_ctx, conn); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 758 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 759 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 760 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 761 | |
| 762 | sslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 763 | return 0; |
| 764 | } |
| 765 | /* don't know how to handle such a target */ |
| 766 | return -1; |
| 767 | } |
| 768 | |
| 769 | |
| 770 | /* This is the callback which is used when an SSL handshake is pending. It |
| 771 | * updates the FD status if it wants some polling before being called again. |
| 772 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 773 | * otherwise it returns non-zero and removes itself from the connection's |
| 774 | * flags (the bit is provided in <flag> by the caller). |
| 775 | */ |
| 776 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 777 | { |
| 778 | int ret; |
| 779 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 780 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 781 | goto out_error; |
| 782 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 783 | ret = SSL_do_handshake(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 784 | if (ret != 1) { |
| 785 | /* handshake did not complete, let's find why */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 786 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 787 | |
| 788 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 789 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 790 | __conn_sock_stop_recv(conn); |
| 791 | __conn_sock_poll_send(conn); |
| 792 | return 0; |
| 793 | } |
| 794 | else if (ret == SSL_ERROR_WANT_READ) { |
| 795 | /* SSL handshake needs to read, L4 connection is ready */ |
| 796 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 797 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 798 | __conn_sock_stop_send(conn); |
| 799 | __conn_sock_poll_recv(conn); |
| 800 | return 0; |
| 801 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 802 | else if (ret == SSL_ERROR_SYSCALL) { |
| 803 | /* if errno is null, then connection was successfully established */ |
| 804 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 805 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 806 | goto out_error; |
| 807 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 808 | else { |
| 809 | /* Fail on all other handshake errors */ |
| 810 | goto out_error; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /* Handshake succeeded */ |
| 815 | if (target_srv(&conn->target)) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 816 | if (!SSL_session_reused(conn->xprt_ctx)) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 817 | /* check if session was reused, if not store current session on server for reuse */ |
| 818 | if (target_srv(&conn->target)->ssl_ctx.reused_sess) |
| 819 | SSL_SESSION_free(target_srv(&conn->target)->ssl_ctx.reused_sess); |
| 820 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 821 | target_srv(&conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
| 825 | /* The connection is now established at both layers, it's time to leave */ |
| 826 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 827 | return 1; |
| 828 | |
| 829 | out_error: |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 830 | /* free resumed session if exists */ |
| 831 | if (target_srv(&conn->target) && target_srv(&conn->target)->ssl_ctx.reused_sess) { |
| 832 | SSL_SESSION_free(target_srv(&conn->target)->ssl_ctx.reused_sess); |
| 833 | target_srv(&conn->target)->ssl_ctx.reused_sess = NULL; |
| 834 | } |
| 835 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 836 | /* Fail on all other handshake errors */ |
| 837 | conn->flags |= CO_FL_ERROR; |
| 838 | conn->flags &= ~flag; |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | /* Receive up to <count> bytes from connection <conn>'s socket and store them |
| 843 | * into buffer <buf>. The caller must ensure that <count> is always smaller |
| 844 | * than the buffer's size. Only one call to recv() is performed, unless the |
| 845 | * buffer wraps, in which case a second call may be performed. The connection's |
| 846 | * flags are updated with whatever special event is detected (error, read0, |
| 847 | * empty). The caller is responsible for taking care of those events and |
| 848 | * avoiding the call if inappropriate. The function does not call the |
| 849 | * connection's polling update function, so the caller is responsible for this. |
| 850 | */ |
| 851 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 852 | { |
| 853 | int ret, done = 0; |
| 854 | int try = count; |
| 855 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 856 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 857 | goto out_error; |
| 858 | |
| 859 | if (conn->flags & CO_FL_HANDSHAKE) |
| 860 | /* a handshake was requested */ |
| 861 | return 0; |
| 862 | |
| 863 | /* compute the maximum block size we can read at once. */ |
| 864 | if (buffer_empty(buf)) { |
| 865 | /* let's realign the buffer to optimize I/O */ |
| 866 | buf->p = buf->data; |
| 867 | } |
| 868 | else if (buf->data + buf->o < buf->p && |
| 869 | buf->p + buf->i < buf->data + buf->size) { |
| 870 | /* remaining space wraps at the end, with a moving limit */ |
| 871 | if (try > buf->data + buf->size - (buf->p + buf->i)) |
| 872 | try = buf->data + buf->size - (buf->p + buf->i); |
| 873 | } |
| 874 | |
| 875 | /* read the largest possible block. For this, we perform only one call |
| 876 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 877 | * in which case we accept to do it once again. A new attempt is made on |
| 878 | * EINTR too. |
| 879 | */ |
| 880 | while (try) { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 881 | ret = SSL_read(conn->xprt_ctx, bi_end(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 882 | if (conn->flags & CO_FL_ERROR) { |
| 883 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
| 884 | break; |
| 885 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 886 | if (ret > 0) { |
| 887 | buf->i += ret; |
| 888 | done += ret; |
| 889 | if (ret < try) |
| 890 | break; |
| 891 | count -= ret; |
| 892 | try = count; |
| 893 | } |
| 894 | else if (ret == 0) { |
| 895 | goto read0; |
| 896 | } |
| 897 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 898 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 899 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 900 | /* handshake is running, and it needs to poll for a write event */ |
| 901 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 902 | __conn_sock_poll_send(conn); |
| 903 | break; |
| 904 | } |
| 905 | else if (ret == SSL_ERROR_WANT_READ) { |
| 906 | /* we need to poll for retry a read later */ |
| 907 | __conn_data_poll_recv(conn); |
| 908 | break; |
| 909 | } |
| 910 | /* otherwise it's a real error */ |
| 911 | goto out_error; |
| 912 | } |
| 913 | } |
| 914 | return done; |
| 915 | |
| 916 | read0: |
| 917 | conn_sock_read0(conn); |
| 918 | return done; |
| 919 | out_error: |
| 920 | conn->flags |= CO_FL_ERROR; |
| 921 | return done; |
| 922 | } |
| 923 | |
| 924 | |
| 925 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
| 926 | * <flags> may contain MSG_MORE to make the system hold on without sending |
| 927 | * data too fast, but this flag is ignored at the moment. |
| 928 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 929 | * a second call may be performed. The connection's flags are updated with |
| 930 | * whatever special event is detected (error, empty). The caller is responsible |
| 931 | * for taking care of those events and avoiding the call if inappropriate. The |
| 932 | * function does not call the connection's polling update function, so the caller |
| 933 | * is responsible for this. |
| 934 | */ |
| 935 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 936 | { |
| 937 | int ret, try, done; |
| 938 | |
| 939 | done = 0; |
| 940 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 941 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 942 | goto out_error; |
| 943 | |
| 944 | if (conn->flags & CO_FL_HANDSHAKE) |
| 945 | /* a handshake was requested */ |
| 946 | return 0; |
| 947 | |
| 948 | /* send the largest possible block. For this we perform only one call |
| 949 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 950 | * in which case we accept to do it once again. |
| 951 | */ |
| 952 | while (buf->o) { |
| 953 | try = buf->o; |
| 954 | /* outgoing data may wrap at the end */ |
| 955 | if (buf->data + try > buf->p) |
| 956 | try = buf->data + try - buf->p; |
| 957 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 958 | ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 959 | if (conn->flags & CO_FL_ERROR) { |
| 960 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
| 961 | break; |
| 962 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 963 | if (ret > 0) { |
| 964 | buf->o -= ret; |
| 965 | done += ret; |
| 966 | |
| 967 | if (likely(!buffer_len(buf))) |
| 968 | /* optimize data alignment in the buffer */ |
| 969 | buf->p = buf->data; |
| 970 | |
| 971 | /* if the system buffer is full, don't insist */ |
| 972 | if (ret < try) |
| 973 | break; |
| 974 | } |
| 975 | else { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 976 | ret = SSL_get_error(conn->xprt_ctx, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 977 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 978 | /* we need to poll to retry a write later */ |
| 979 | __conn_data_poll_send(conn); |
| 980 | break; |
| 981 | } |
| 982 | else if (ret == SSL_ERROR_WANT_READ) { |
| 983 | /* handshake is running, and |
| 984 | it needs to poll for a read event, |
| 985 | write polling must be disabled cause |
| 986 | we are sure we can't write anything more |
| 987 | before handshake re-performed */ |
| 988 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 989 | __conn_sock_poll_recv(conn); |
| 990 | break; |
| 991 | } |
| 992 | goto out_error; |
| 993 | } |
| 994 | } |
| 995 | return done; |
| 996 | |
| 997 | out_error: |
| 998 | conn->flags |= CO_FL_ERROR; |
| 999 | return done; |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | static void ssl_sock_close(struct connection *conn) { |
| 1004 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1005 | if (conn->xprt_ctx) { |
| 1006 | SSL_free(conn->xprt_ctx); |
| 1007 | conn->xprt_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 1008 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1009 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 1013 | * any case, flags the connection as reusable if no handshake was in progress. |
| 1014 | */ |
| 1015 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 1016 | { |
| 1017 | if (conn->flags & CO_FL_HANDSHAKE) |
| 1018 | return; |
| 1019 | /* no handshake was in progress, try a clean ssl shutdown */ |
| 1020 | if (clean) |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1021 | SSL_shutdown(conn->xprt_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1022 | |
| 1023 | /* force flag on ssl to keep session in cache regardless shutdown result */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1024 | SSL_set_shutdown(conn->xprt_ctx, SSL_SENT_SHUTDOWN); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1025 | } |
| 1026 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1027 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 1028 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1029 | /* boolean, returns true if client cert was present */ |
| 1030 | static int |
| 1031 | smp_fetch_client_crt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 1032 | const struct arg *args, struct sample *smp) |
| 1033 | { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1034 | if (!l4 || l4->si[0].conn.xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1035 | return 0; |
| 1036 | |
| 1037 | if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) { |
| 1038 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | smp->flags = 0; |
| 1043 | smp->type = SMP_T_BOOL; |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1044 | smp->data.uint = SSL_SOCK_ST_FL_VERIFY_DONE & l4->si[0].conn.xprt_st ? 1 : 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1045 | |
| 1046 | return 1; |
| 1047 | } |
| 1048 | |
| 1049 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1050 | /* boolean, returns true if transport layer is SSL */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1051 | static int |
| 1052 | smp_fetch_is_ssl(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 1053 | const struct arg *args, struct sample *smp) |
| 1054 | { |
| 1055 | smp->type = SMP_T_BOOL; |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1056 | smp->data.uint = (l4->si[0].conn.xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1057 | return 1; |
| 1058 | } |
| 1059 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1060 | /* boolean, returns true if transport layer is SSL */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1061 | static int |
| 1062 | smp_fetch_has_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 1063 | const struct arg *args, struct sample *smp) |
| 1064 | { |
| 1065 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1066 | smp->type = SMP_T_BOOL; |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1067 | smp->data.uint = (l4->si[0].conn.xprt == &ssl_sock) && |
| 1068 | l4->si[0].conn.xprt_ctx && |
| 1069 | SSL_get_servername(l4->si[0].conn.xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1070 | return 1; |
| 1071 | #else |
| 1072 | return 0; |
| 1073 | #endif |
| 1074 | } |
| 1075 | |
| 1076 | static int |
| 1077 | smp_fetch_ssl_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 1078 | const struct arg *args, struct sample *smp) |
| 1079 | { |
| 1080 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1081 | smp->flags = 0; |
| 1082 | smp->type = SMP_T_CSTR; |
| 1083 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1084 | if (!l4 || !l4->si[0].conn.xprt_ctx || l4->si[0].conn.xprt != &ssl_sock) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1085 | return 0; |
| 1086 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1087 | smp->data.str.str = (char *)SSL_get_servername(l4->si[0].conn.xprt_ctx, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 1088 | if (!smp->data.str.str) |
| 1089 | return 0; |
| 1090 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1091 | smp->data.str.len = strlen(smp->data.str.str); |
| 1092 | return 1; |
| 1093 | #else |
| 1094 | return 0; |
| 1095 | #endif |
| 1096 | } |
| 1097 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1098 | /* integer, returns the first verify error ID in CA */ |
| 1099 | static int |
| 1100 | smp_fetch_verify_caerr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 1101 | const struct arg *args, struct sample *smp) |
| 1102 | { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1103 | if (!l4 || l4->si[0].conn.xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1104 | return 0; |
| 1105 | |
| 1106 | if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) { |
| 1107 | smp->flags = SMP_F_MAY_CHANGE; |
| 1108 | return 0; |
| 1109 | } |
| 1110 | |
| 1111 | smp->type = SMP_T_UINT; |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1112 | smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CA_ERROR(l4->si[0].conn.xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1113 | smp->flags = 0; |
| 1114 | |
| 1115 | return 1; |
| 1116 | } |
| 1117 | |
| 1118 | /* integer, returns the depth of the first verify error in CA */ |
| 1119 | static int |
| 1120 | smp_fetch_verify_caerr_depth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 1121 | const struct arg *args, struct sample *smp) |
| 1122 | { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1123 | if (!l4 || l4->si[0].conn.xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1124 | return 0; |
| 1125 | |
| 1126 | if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) { |
| 1127 | smp->flags = SMP_F_MAY_CHANGE; |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | smp->type = SMP_T_UINT; |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1132 | smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CAEDEPTH(l4->si[0].conn.xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1133 | smp->flags = 0; |
| 1134 | |
| 1135 | return 1; |
| 1136 | } |
| 1137 | |
| 1138 | /* integer, returns the depth of the first verify error in CA */ |
| 1139 | static int |
| 1140 | smp_fetch_verify_crterr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 1141 | const struct arg *args, struct sample *smp) |
| 1142 | { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1143 | if (!l4 || l4->si[0].conn.xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1144 | return 0; |
| 1145 | |
| 1146 | if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) { |
| 1147 | smp->flags = SMP_F_MAY_CHANGE; |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | smp->type = SMP_T_UINT; |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1152 | smp->data.uint = (unsigned int)SSL_SOCK_ST_TO_CRTERROR(l4->si[0].conn.xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1153 | smp->flags = 0; |
| 1154 | |
| 1155 | return 1; |
| 1156 | } |
| 1157 | |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 1158 | /* integer, returns the verify result */ |
| 1159 | static int |
| 1160 | smp_fetch_verify_result(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 1161 | const struct arg *args, struct sample *smp) |
| 1162 | { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1163 | if (!l4 || l4->si[0].conn.xprt != &ssl_sock) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 1164 | return 0; |
| 1165 | |
| 1166 | if (!(l4->si[0].conn.flags & CO_FL_CONNECTED)) { |
| 1167 | smp->flags = SMP_F_MAY_CHANGE; |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1171 | if (!l4->si[0].conn.xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 1172 | return 0; |
| 1173 | |
| 1174 | smp->type = SMP_T_UINT; |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1175 | smp->data.uint = (unsigned int)SSL_get_verify_result(l4->si[0].conn.xprt_ctx); |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 1176 | smp->flags = 0; |
| 1177 | |
| 1178 | return 1; |
| 1179 | } |
| 1180 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1181 | /* parse the "ca-file" bind keyword */ |
| 1182 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1183 | { |
| 1184 | if (!*args[cur_arg + 1]) { |
| 1185 | if (err) |
| 1186 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 1187 | return ERR_ALERT | ERR_FATAL; |
| 1188 | } |
| 1189 | |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 1190 | if ((*args[cur_arg + 1] != '/') && global.ca_base) { |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1191 | conf->ca_file = malloc(strlen(global.ca_base) + 1 + strlen(args[cur_arg + 1]) + 1); |
| 1192 | if (conf->ca_file) |
| 1193 | sprintf(conf->ca_file, "%s/%s", global.ca_base, args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 1194 | return 0; |
| 1195 | } |
| 1196 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1197 | conf->ca_file = strdup(args[cur_arg + 1]); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1198 | return 0; |
| 1199 | } |
| 1200 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1201 | /* parse the "ciphers" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1202 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1203 | { |
| 1204 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1205 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1206 | return ERR_ALERT | ERR_FATAL; |
| 1207 | } |
| 1208 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 1209 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1210 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1211 | return 0; |
| 1212 | } |
| 1213 | |
| 1214 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1215 | static int bind_parse_crt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1216 | { |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 1217 | char path[PATH_MAX]; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1218 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1219 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1220 | return ERR_ALERT | ERR_FATAL; |
| 1221 | } |
| 1222 | |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 1223 | if ((*args[cur_arg + 1] != '/' ) && global.crt_base) { |
| 1224 | if ((strlen(global.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > PATH_MAX) { |
| 1225 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 1226 | return ERR_ALERT | ERR_FATAL; |
| 1227 | } |
| 1228 | sprintf(path, "%s/%s", global.crt_base, args[cur_arg + 1]); |
| 1229 | if (ssl_sock_load_cert(path, conf, px, err) > 0) |
| 1230 | return ERR_ALERT | ERR_FATAL; |
| 1231 | |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1235 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, px, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1236 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1237 | |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1241 | /* parse the "crl-file" bind keyword */ |
| 1242 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1243 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 1244 | #ifndef X509_V_FLAG_CRL_CHECK |
| 1245 | if (err) |
| 1246 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 1247 | return ERR_ALERT | ERR_FATAL; |
| 1248 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1249 | if (!*args[cur_arg + 1]) { |
| 1250 | if (err) |
| 1251 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 1252 | return ERR_ALERT | ERR_FATAL; |
| 1253 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1254 | |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 1255 | if ((*args[cur_arg + 1] != '/') && global.ca_base) { |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1256 | conf->crl_file = malloc(strlen(global.ca_base) + 1 + strlen(args[cur_arg + 1]) + 1); |
| 1257 | if (conf->crl_file) |
| 1258 | sprintf(conf->crl_file, "%s/%s", global.ca_base, args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 1259 | return 0; |
| 1260 | } |
| 1261 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1262 | conf->crl_file = strdup(args[cur_arg + 1]); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1263 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 1264 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | /* parse the "ecdhe" bind keyword keywords */ |
| 1268 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1269 | { |
| 1270 | #if OPENSSL_VERSION_NUMBER < 0x0090800fL |
| 1271 | if (err) |
| 1272 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 1273 | return ERR_ALERT | ERR_FATAL; |
| 1274 | #elif defined(OPENSSL_NO_ECDH) |
| 1275 | if (err) |
| 1276 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 1277 | return ERR_ALERT | ERR_FATAL; |
| 1278 | #else |
| 1279 | if (!*args[cur_arg + 1]) { |
| 1280 | if (err) |
| 1281 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 1282 | return ERR_ALERT | ERR_FATAL; |
| 1283 | } |
| 1284 | |
| 1285 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1286 | |
| 1287 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 1288 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1289 | } |
| 1290 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1291 | /* parse the "crt_ignerr" and "ca_ignerr" bind keywords */ |
| 1292 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1293 | { |
| 1294 | int code; |
| 1295 | char *p = args[cur_arg + 1]; |
| 1296 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 1297 | |
| 1298 | if (!*p) { |
| 1299 | if (err) |
| 1300 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 1301 | return ERR_ALERT | ERR_FATAL; |
| 1302 | } |
| 1303 | |
| 1304 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 1305 | ignerr = &conf->ca_ignerr; |
| 1306 | |
| 1307 | if (strcmp(p, "all") == 0) { |
| 1308 | *ignerr = ~0ULL; |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | while (p) { |
| 1313 | code = atoi(p); |
| 1314 | if ((code <= 0) || (code > 63)) { |
| 1315 | if (err) |
| 1316 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 1317 | args[cur_arg], code, args[cur_arg + 1]); |
| 1318 | return ERR_ALERT | ERR_FATAL; |
| 1319 | } |
| 1320 | *ignerr |= 1ULL << code; |
| 1321 | p = strchr(p, ','); |
| 1322 | if (p) |
| 1323 | p++; |
| 1324 | } |
| 1325 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 1326 | return 0; |
| 1327 | } |
| 1328 | |
| 1329 | /* parse the "force-sslv3" bind keyword */ |
| 1330 | static int bind_parse_force_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1331 | { |
| 1332 | conf->ssl_options |= BC_SSL_O_USE_SSLV3; |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
| 1336 | /* parse the "force-tlsv10" bind keyword */ |
| 1337 | static int bind_parse_force_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1338 | { |
| 1339 | conf->ssl_options |= BC_SSL_O_USE_TLSV10; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 1340 | return 0; |
| 1341 | } |
| 1342 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 1343 | /* parse the "force-tlsv11" bind keyword */ |
| 1344 | static int bind_parse_force_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1345 | { |
| 1346 | #if SSL_OP_NO_TLSv1_1 |
| 1347 | conf->ssl_options |= BC_SSL_O_USE_TLSV11; |
| 1348 | return 0; |
| 1349 | #else |
| 1350 | if (err) |
| 1351 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[cur_arg]); |
| 1352 | return ERR_ALERT | ERR_FATAL; |
| 1353 | #endif |
| 1354 | } |
| 1355 | |
| 1356 | /* parse the "force-tlsv12" bind keyword */ |
| 1357 | static int bind_parse_force_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1358 | { |
| 1359 | #if SSL_OP_NO_TLSv1_2 |
| 1360 | conf->ssl_options |= BC_SSL_O_USE_TLSV12; |
| 1361 | return 0; |
| 1362 | #else |
| 1363 | if (err) |
| 1364 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[cur_arg]); |
| 1365 | return ERR_ALERT | ERR_FATAL; |
| 1366 | #endif |
| 1367 | } |
| 1368 | |
| 1369 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 1370 | /* parse the "no-tls-tickets" bind keyword */ |
| 1371 | static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1372 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1373 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1374 | return 0; |
| 1375 | } |
| 1376 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 1377 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 1378 | /* parse the "no-sslv3" bind keyword */ |
| 1379 | static int bind_parse_no_sslv3(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1380 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1381 | conf->ssl_options |= BC_SSL_O_NO_SSLV3; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1382 | return 0; |
| 1383 | } |
| 1384 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 1385 | /* parse the "no-tlsv10" bind keyword */ |
| 1386 | static int bind_parse_no_tlsv10(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 1387 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1388 | conf->ssl_options |= BC_SSL_O_NO_TLSV10; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 1389 | return 0; |
| 1390 | } |
| 1391 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 1392 | /* parse the "no-tlsv11" bind keyword */ |
| 1393 | static int bind_parse_no_tlsv11(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 1394 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1395 | conf->ssl_options |= BC_SSL_O_NO_TLSV11; |
Emeric Brun | c0ff492 | 2012-09-28 19:37:02 +0200 | [diff] [blame] | 1396 | return 0; |
| 1397 | } |
| 1398 | |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 1399 | /* parse the "no-tlsv12" bind keyword */ |
| 1400 | static int bind_parse_no_tlsv12(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1401 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 1402 | conf->ssl_options |= BC_SSL_O_NO_TLSV12; |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1403 | return 0; |
| 1404 | } |
| 1405 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1406 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1407 | static int bind_parse_ssl(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1408 | { |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 1409 | struct listener *l; |
| 1410 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1411 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 1412 | |
| 1413 | if (global.listen_default_ciphers && !conf->ciphers) |
| 1414 | conf->ciphers = strdup(global.listen_default_ciphers); |
| 1415 | |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 1416 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1417 | l->xprt = &ssl_sock; |
Willy Tarreau | 81796be | 2012-09-22 19:11:47 +0200 | [diff] [blame] | 1418 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1419 | return 0; |
| 1420 | } |
| 1421 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 1422 | /* parse the "verify" bind keyword */ |
| 1423 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1424 | { |
| 1425 | if (!*args[cur_arg + 1]) { |
| 1426 | if (err) |
| 1427 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 1428 | return ERR_ALERT | ERR_FATAL; |
| 1429 | } |
| 1430 | |
| 1431 | if (strcmp(args[cur_arg + 1], "none") == 0) |
| 1432 | conf->verify = SSL_VERIFY_NONE; |
| 1433 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
| 1434 | conf->verify = SSL_VERIFY_PEER; |
| 1435 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
| 1436 | conf->verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1437 | else { |
| 1438 | if (err) |
| 1439 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 1440 | args[cur_arg], args[cur_arg + 1]); |
| 1441 | return ERR_ALERT | ERR_FATAL; |
| 1442 | } |
| 1443 | |
| 1444 | return 0; |
| 1445 | } |
| 1446 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 1447 | /************** "server" keywords ****************/ |
| 1448 | |
| 1449 | /* parse the "check-ssl" server keyword */ |
| 1450 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1451 | { |
| 1452 | newsrv->check.use_ssl = 1; |
| 1453 | if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 1454 | newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers); |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | /* parse the "ciphers" server keyword */ |
| 1459 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1460 | { |
| 1461 | if (!*args[*cur_arg + 1]) { |
| 1462 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 1463 | return ERR_ALERT | ERR_FATAL; |
| 1464 | } |
| 1465 | |
| 1466 | free(newsrv->ssl_ctx.ciphers); |
| 1467 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 1468 | return 0; |
| 1469 | } |
| 1470 | |
| 1471 | /* parse the "force-sslv3" server keyword */ |
| 1472 | static int srv_parse_force_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1473 | { |
| 1474 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_SSLV3; |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | /* parse the "force-tlsv10" server keyword */ |
| 1479 | static int srv_parse_force_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1480 | { |
| 1481 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV10; |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | /* parse the "force-tlsv11" server keyword */ |
| 1486 | static int srv_parse_force_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1487 | { |
| 1488 | #if SSL_OP_NO_TLSv1_1 |
| 1489 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV11; |
| 1490 | return 0; |
| 1491 | #else |
| 1492 | if (err) |
| 1493 | memprintf(err, "'%s' : library does not support protocol TLSv1.1", args[*cur_arg]); |
| 1494 | return ERR_ALERT | ERR_FATAL; |
| 1495 | #endif |
| 1496 | } |
| 1497 | |
| 1498 | /* parse the "force-tlsv12" server keyword */ |
| 1499 | static int srv_parse_force_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1500 | { |
| 1501 | #if SSL_OP_NO_TLSv1_2 |
| 1502 | newsrv->ssl_ctx.options |= SRV_SSL_O_USE_TLSV12; |
| 1503 | return 0; |
| 1504 | #else |
| 1505 | if (err) |
| 1506 | memprintf(err, "'%s' : library does not support protocol TLSv1.2", args[*cur_arg]); |
| 1507 | return ERR_ALERT | ERR_FATAL; |
| 1508 | #endif |
| 1509 | } |
| 1510 | |
| 1511 | /* parse the "no-sslv3" server keyword */ |
| 1512 | static int srv_parse_no_sslv3(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1513 | { |
| 1514 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_SSLV3; |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
| 1518 | /* parse the "no-tlsv10" server keyword */ |
| 1519 | static int srv_parse_no_tlsv10(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1520 | { |
| 1521 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV10; |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
| 1525 | /* parse the "no-tlsv11" server keyword */ |
| 1526 | static int srv_parse_no_tlsv11(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1527 | { |
| 1528 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV11; |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | /* parse the "no-tlsv12" server keyword */ |
| 1533 | static int srv_parse_no_tlsv12(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1534 | { |
| 1535 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLSV12; |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | /* parse the "ssl" server keyword */ |
| 1540 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1541 | { |
| 1542 | newsrv->use_ssl = 1; |
| 1543 | if (global.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 1544 | newsrv->ssl_ctx.ciphers = strdup(global.connect_default_ciphers); |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1548 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1549 | * Please take care of keeping this list alphabetically sorted. |
| 1550 | */ |
| 1551 | static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1552 | { "client_crt", smp_fetch_client_crt, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES }, |
| 1553 | { "is_ssl", smp_fetch_is_ssl, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES }, |
| 1554 | { "ssl_has_sni", smp_fetch_has_sni, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES }, |
| 1555 | { "ssl_sni", smp_fetch_ssl_sni, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES }, |
| 1556 | { "ssl_verify_caerr", smp_fetch_verify_caerr, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES }, |
| 1557 | { "ssl_verify_caerr_depth", smp_fetch_verify_caerr_depth, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES }, |
| 1558 | { "ssl_verify_crterr", smp_fetch_verify_crterr, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES }, |
| 1559 | { "ssl_verify_result", smp_fetch_verify_result, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1560 | { NULL, NULL, 0, 0, 0 }, |
| 1561 | }}; |
| 1562 | |
| 1563 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1564 | * Please take care of keeping this list alphabetically sorted. |
| 1565 | */ |
| 1566 | static struct acl_kw_list acl_kws = {{ },{ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1567 | { "client_crt", acl_parse_int, smp_fetch_client_crt, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
| 1568 | { "is_ssl", acl_parse_int, smp_fetch_is_ssl, acl_match_nothing, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
| 1569 | { "ssl_has_sni", acl_parse_int, smp_fetch_has_sni, acl_match_nothing, ACL_USE_L6REQ_PERMANENT, 0 }, |
| 1570 | { "ssl_sni", acl_parse_str, smp_fetch_ssl_sni, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
| 1571 | { "ssl_sni_end", acl_parse_str, smp_fetch_ssl_sni, acl_match_end, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
| 1572 | { "ssl_sni_reg", acl_parse_str, smp_fetch_ssl_sni, acl_match_reg, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
| 1573 | { "ssl_verify_caerr", acl_parse_int, smp_fetch_verify_caerr, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
| 1574 | { "ssl_verify_caerr_depth", acl_parse_int, smp_fetch_verify_caerr_depth, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
| 1575 | { "ssl_verify_crterr", acl_parse_int, smp_fetch_verify_crterr, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
| 1576 | { "ssl_verify_result", acl_parse_int, smp_fetch_verify_result, acl_match_int, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1577 | { NULL, NULL, NULL, NULL }, |
| 1578 | }}; |
| 1579 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1580 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1581 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1582 | * all code contributors. |
| 1583 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1584 | * the config parser can report an appropriate error when a known keyword was |
| 1585 | * not enabled. |
| 1586 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 1587 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1588 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 1589 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 1590 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 1591 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 1592 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 1593 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 1594 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 1595 | { "force-sslv3", bind_parse_force_sslv3, 0 }, /* force SSLv3 */ |
| 1596 | { "force-tlsv10", bind_parse_force_tlsv10, 0 }, /* force TLSv10 */ |
| 1597 | { "force-tlsv11", bind_parse_force_tlsv11, 0 }, /* force TLSv11 */ |
| 1598 | { "force-tlsv12", bind_parse_force_tlsv12, 0 }, /* force TLSv12 */ |
Emeric Brun | 9b3009b | 2012-10-05 11:55:06 +0200 | [diff] [blame] | 1599 | { "no-sslv3", bind_parse_no_sslv3, 0 }, /* disable SSLv3 */ |
| 1600 | { "no-tlsv10", bind_parse_no_tlsv10, 0 }, /* disable TLSv10 */ |
| 1601 | { "no-tlsv11", bind_parse_no_tlsv11, 0 }, /* disable TLSv11 */ |
| 1602 | { "no-tlsv12", bind_parse_no_tlsv12, 0 }, /* disable TLSv12 */ |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 1603 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 1604 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
| 1605 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1606 | { NULL, NULL, 0 }, |
| 1607 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1608 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 1609 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1610 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1611 | * all code contributors. |
| 1612 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1613 | * the config parser can report an appropriate error when a known keyword was |
| 1614 | * not enabled. |
| 1615 | */ |
| 1616 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Emeric Brun | ecc91fe | 2012-10-11 15:05:10 +0200 | [diff] [blame^] | 1617 | { "check-ssl", srv_parse_check_ssl, 0, 0 }, /* enable SSL for health checks */ |
| 1618 | { "ciphers", srv_parse_ciphers, 1, 0 }, /* select the cipher suite */ |
| 1619 | { "force-sslv3", srv_parse_force_sslv3, 0, 0 }, /* force SSLv3 */ |
| 1620 | { "force-tlsv10", srv_parse_force_tlsv10, 0, 0 }, /* force TLSv10 */ |
| 1621 | { "force-tlsv11", srv_parse_force_tlsv11, 0, 0 }, /* force TLSv11 */ |
| 1622 | { "force-tlsv12", srv_parse_force_tlsv12, 0, 0 }, /* force TLSv12 */ |
| 1623 | { "no-sslv3", srv_parse_no_sslv3, 0, 0 }, /* disable SSLv3 */ |
| 1624 | { "no-tlsv10", srv_parse_no_tlsv10, 0, 0 }, /* disable TLSv10 */ |
| 1625 | { "no-tlsv11", srv_parse_no_tlsv11, 0, 0 }, /* disable TLSv11 */ |
| 1626 | { "no-tlsv12", srv_parse_no_tlsv12, 0, 0 }, /* disable TLSv12 */ |
| 1627 | { "ssl", srv_parse_ssl, 0, 0 }, /* enable SSL processing */ |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 1628 | { NULL, NULL, 0, 0 }, |
| 1629 | }}; |
| 1630 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1631 | /* transport-layer operations for SSL sockets */ |
| 1632 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1633 | .snd_buf = ssl_sock_from_buf, |
| 1634 | .rcv_buf = ssl_sock_to_buf, |
| 1635 | .rcv_pipe = NULL, |
| 1636 | .snd_pipe = NULL, |
| 1637 | .shutr = NULL, |
| 1638 | .shutw = ssl_sock_shutw, |
| 1639 | .close = ssl_sock_close, |
| 1640 | .init = ssl_sock_init, |
| 1641 | }; |
| 1642 | |
| 1643 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 1644 | static void __ssl_sock_init(void) |
| 1645 | { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1646 | STACK_OF(SSL_COMP)* cm; |
| 1647 | |
| 1648 | SSL_library_init(); |
| 1649 | cm = SSL_COMP_get_compression_methods(); |
| 1650 | sk_SSL_COMP_zero(cm); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 1651 | sample_register_fetches(&sample_fetch_keywords); |
| 1652 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 1653 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 1654 | srv_register_keywords(&srv_kws); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | /* |
| 1658 | * Local variables: |
| 1659 | * c-indent-level: 8 |
| 1660 | * c-basic-offset: 8 |
| 1661 | * End: |
| 1662 | */ |