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