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