Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SSL data transfer functions between buffers and SOCK_STREAM sockets |
| 3 | * |
| 4 | * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
Willy 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> |
| 51 | #include <common/standard.h> |
| 52 | #include <common/ticks.h> |
| 53 | #include <common/time.h> |
| 54 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 55 | #include <ebsttree.h> |
| 56 | |
| 57 | #include <types/global.h> |
| 58 | #include <types/ssl_sock.h> |
| 59 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 60 | #include <proto/acl.h> |
| 61 | #include <proto/arg.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 62 | #include <proto/connection.h> |
| 63 | #include <proto/fd.h> |
| 64 | #include <proto/freq_ctr.h> |
| 65 | #include <proto/frontend.h> |
| 66 | #include <proto/log.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 67 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 68 | #include <proto/ssl_sock.h> |
| 69 | #include <proto/task.h> |
| 70 | |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 71 | static int sslconns = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 72 | |
| 73 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 74 | { |
| 75 | struct connection *conn = (struct connection *)SSL_get_app_data(ssl); |
| 76 | (void)ret; /* shut gcc stupid warning */ |
| 77 | |
| 78 | if (where & SSL_CB_HANDSHAKE_START) { |
| 79 | /* Disable renegotiation (CVE-2009-3555) */ |
| 80 | if (conn->flags & CO_FL_CONNECTED) |
| 81 | conn->flags |= CO_FL_ERROR; |
| 82 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 86 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 87 | * warning when no match is found, which implies the default (first) cert |
| 88 | * will keep being used. |
| 89 | */ |
| 90 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, struct ssl_conf *s) |
| 91 | { |
| 92 | const char *servername; |
| 93 | const char *wildp = NULL; |
| 94 | struct ebmb_node *node; |
| 95 | int i; |
| 96 | (void)al; /* shut gcc stupid warning */ |
| 97 | |
| 98 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
| 99 | if (!servername) |
| 100 | return SSL_TLSEXT_ERR_NOACK; |
| 101 | |
| 102 | for (i = 0; i < trashlen; i++) { |
| 103 | if (!servername[i]) |
| 104 | break; |
| 105 | trash[i] = tolower(servername[i]); |
| 106 | if (!wildp && (trash[i] == '.')) |
| 107 | wildp = &trash[i]; |
| 108 | } |
| 109 | trash[i] = 0; |
| 110 | |
| 111 | /* lookup in full qualified names */ |
| 112 | node = ebst_lookup(&s->sni_ctx, trash); |
| 113 | if (!node) { |
| 114 | if (!wildp) |
| 115 | return SSL_TLSEXT_ERR_ALERT_WARNING; |
| 116 | |
| 117 | /* lookup in full wildcards names */ |
| 118 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 119 | if (!node) |
| 120 | return SSL_TLSEXT_ERR_ALERT_WARNING; |
| 121 | } |
| 122 | |
| 123 | /* switch ctx */ |
| 124 | SSL_set_SSL_CTX(ssl, container_of(node, struct sni_ctx, name)->ctx); |
| 125 | return SSL_TLSEXT_ERR_OK; |
| 126 | } |
| 127 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 128 | |
| 129 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 130 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 131 | */ |
| 132 | int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct ssl_conf *s) |
| 133 | { |
| 134 | BIO *in; |
| 135 | X509 *x = NULL, *ca; |
| 136 | int i, len, err; |
| 137 | int ret = -1; |
| 138 | int order = 0; |
| 139 | X509_NAME *xname; |
| 140 | char *str; |
| 141 | struct sni_ctx *sc; |
| 142 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 143 | STACK_OF(GENERAL_NAME) *names; |
| 144 | #endif |
| 145 | |
| 146 | in = BIO_new(BIO_s_file()); |
| 147 | if (in == NULL) |
| 148 | goto end; |
| 149 | |
| 150 | if (BIO_read_filename(in, file) <= 0) |
| 151 | goto end; |
| 152 | |
| 153 | x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata); |
| 154 | if (x == NULL) |
| 155 | goto end; |
| 156 | |
| 157 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 158 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 159 | if (names) { |
| 160 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 161 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 162 | if (name->type == GEN_DNS) { |
| 163 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 164 | if ((len = strlen(str))) { |
| 165 | int j; |
| 166 | |
| 167 | if (*str != '*') { |
| 168 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
| 169 | for (j = 0; j < len; j++) |
| 170 | sc->name.key[j] = tolower(str[j]); |
| 171 | sc->name.key[len] = 0; |
| 172 | sc->order = order++; |
| 173 | sc->ctx = ctx; |
| 174 | ebst_insert(&s->sni_ctx, &sc->name); |
| 175 | } |
| 176 | else { |
| 177 | sc = malloc(sizeof(struct sni_ctx) + len); |
| 178 | for (j = 1; j < len; j++) |
| 179 | sc->name.key[j-1] = tolower(str[j]); |
| 180 | sc->name.key[len-1] = 0; |
| 181 | sc->order = order++; |
| 182 | sc->ctx = ctx; |
| 183 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 184 | } |
| 185 | } |
| 186 | OPENSSL_free(str); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 191 | } |
| 192 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 193 | |
| 194 | xname = X509_get_subject_name(x); |
| 195 | i = -1; |
| 196 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 197 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
| 198 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, entry->value) >= 0) { |
| 199 | if ((len = strlen(str))) { |
| 200 | int j; |
| 201 | |
| 202 | if (*str != '*') { |
| 203 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
| 204 | for (j = 0; j < len; j++) |
| 205 | sc->name.key[j] = tolower(str[j]); |
| 206 | sc->name.key[len] = 0; |
| 207 | sc->order = order++; |
| 208 | sc->ctx = ctx; |
| 209 | ebst_insert(&s->sni_ctx, &sc->name); |
| 210 | } |
| 211 | else { |
| 212 | sc = malloc(sizeof(struct sni_ctx) + len); |
| 213 | for (j = 1; j < len; j++) |
| 214 | sc->name.key[j-1] = tolower(str[j]); |
| 215 | sc->name.key[len-1] = 0; |
| 216 | sc->order = order++; |
| 217 | sc->ctx = ctx; |
| 218 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 219 | } |
| 220 | } |
| 221 | OPENSSL_free(str); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 226 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 227 | goto end; |
| 228 | |
| 229 | if (ctx->extra_certs != NULL) { |
| 230 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 231 | ctx->extra_certs = NULL; |
| 232 | } |
| 233 | |
| 234 | while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, ctx->default_passwd_callback_userdata))) { |
| 235 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 236 | X509_free(ca); |
| 237 | goto end; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | err = ERR_get_error(); |
| 242 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 243 | /* we successfully reached the last cert in the file */ |
| 244 | ret = 1; |
| 245 | } |
| 246 | ERR_clear_error(); |
| 247 | |
| 248 | end: |
| 249 | if (x) |
| 250 | X509_free(x); |
| 251 | |
| 252 | if (in) |
| 253 | BIO_free(in); |
| 254 | |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | int ssl_sock_load_cert_file(const char *path, struct ssl_conf *ssl_conf, struct proxy *curproxy) |
| 259 | { |
| 260 | int ret; |
| 261 | SSL_CTX *ctx; |
| 262 | |
| 263 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 264 | if (!ctx) { |
| 265 | Alert("Proxy '%s': unable to allocate SSL context for bind '%s' at [%s:%d] using cert '%s'.\n", |
| 266 | curproxy->id, ssl_conf->arg, ssl_conf->file, ssl_conf->line, path); |
| 267 | return 1; |
| 268 | } |
| 269 | |
| 270 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
| 271 | Alert("Proxy '%s': unable to load SSL private key from file '%s' in bind '%s' at [%s:%d].\n", |
| 272 | curproxy->id, path, ssl_conf->arg, ssl_conf->file, ssl_conf->line); |
| 273 | SSL_CTX_free(ctx); |
| 274 | return 1; |
| 275 | } |
| 276 | |
| 277 | ret = ssl_sock_load_cert_chain_file(ctx, path, ssl_conf); |
| 278 | if (ret <= 0) { |
| 279 | Alert("Proxy '%s': unable to load SSL certificate from file '%s' in bind '%s' at [%s:%d].\n", |
| 280 | curproxy->id, path, ssl_conf->arg, ssl_conf->file, ssl_conf->line); |
| 281 | if (ret < 0) /* serious error, must do that ourselves */ |
| 282 | SSL_CTX_free(ctx); |
| 283 | return 1; |
| 284 | } |
| 285 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 286 | * the tree, so it will be discovered and cleaned in time. |
| 287 | */ |
| 288 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 289 | if (ssl_conf->default_ctx) { |
| 290 | Alert("Proxy '%s': file '%s' : this version of openssl cannot load multiple SSL certificates in bind '%s' at [%s:%d].\n", |
| 291 | curproxy->id, path, ssl_conf->arg, ssl_conf->file, ssl_conf->line); |
| 292 | return 1; |
| 293 | } |
| 294 | #endif |
| 295 | if (!ssl_conf->default_ctx) |
| 296 | ssl_conf->default_ctx = ctx; |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | int ssl_sock_load_cert(char *path, struct ssl_conf *ssl_conf, struct proxy *curproxy) |
| 302 | { |
| 303 | struct dirent *de; |
| 304 | DIR *dir; |
| 305 | struct stat buf; |
| 306 | int pathlen = 0; |
| 307 | char *end, *fp; |
| 308 | int cfgerr = 0; |
| 309 | |
| 310 | if (!(dir = opendir(path))) |
| 311 | return ssl_sock_load_cert_file(path, ssl_conf, curproxy); |
| 312 | |
| 313 | /* strip trailing slashes, including first one */ |
| 314 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 315 | *end = 0; |
| 316 | |
| 317 | if (end >= path) |
| 318 | pathlen = end + 1 - path; |
| 319 | fp = malloc(pathlen + 1 + NAME_MAX + 1); |
| 320 | |
| 321 | while ((de = readdir(dir))) { |
| 322 | snprintf(fp, pathlen + 1 + NAME_MAX + 1, "%s/%s", path, de->d_name); |
| 323 | if (stat(fp, &buf) != 0) { |
| 324 | Alert("Proxy '%s': unable to stat SSL certificate from file '%s' in bind '%s' at [%s:%d] : %s.\n", |
| 325 | curproxy->id, fp, ssl_conf->arg, ssl_conf->file, ssl_conf->line, strerror(errno)); |
| 326 | cfgerr++; |
| 327 | continue; |
| 328 | } |
| 329 | if (!S_ISREG(buf.st_mode)) |
| 330 | continue; |
| 331 | cfgerr += ssl_sock_load_cert_file(fp, ssl_conf, curproxy); |
| 332 | } |
| 333 | free(fp); |
| 334 | closedir(dir); |
| 335 | return cfgerr; |
| 336 | } |
| 337 | |
| 338 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 339 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 340 | #endif |
| 341 | |
| 342 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 343 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
| 344 | #endif |
| 345 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 346 | #define SSL_OP_NO_COMPRESSION 0 |
| 347 | #endif |
| 348 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 349 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 350 | #endif |
| 351 | int ssl_sock_prepare_ctx(struct ssl_conf *ssl_conf, SSL_CTX *ctx, struct proxy *curproxy) |
| 352 | { |
| 353 | int cfgerr = 0; |
| 354 | int ssloptions = |
| 355 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 356 | SSL_OP_NO_SSLv2 | |
| 357 | SSL_OP_NO_COMPRESSION | |
| 358 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION; |
| 359 | int sslmode = |
| 360 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 361 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 362 | SSL_MODE_RELEASE_BUFFERS; |
| 363 | |
| 364 | if (ssl_conf->nosslv3) |
| 365 | ssloptions |= SSL_OP_NO_SSLv3; |
| 366 | if (ssl_conf->notlsv1) |
| 367 | ssloptions |= SSL_OP_NO_TLSv1; |
| 368 | if (ssl_conf->prefer_server_ciphers) |
| 369 | ssloptions |= SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 370 | |
| 371 | SSL_CTX_set_options(ctx, ssloptions); |
| 372 | SSL_CTX_set_mode(ctx, sslmode); |
| 373 | SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL); |
| 374 | |
| 375 | shared_context_set_cache(ctx); |
| 376 | if (ssl_conf->ciphers && |
| 377 | !SSL_CTX_set_cipher_list(ctx, ssl_conf->ciphers)) { |
| 378 | Alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 379 | curproxy->id, ssl_conf->ciphers, ssl_conf->arg, ssl_conf->file, ssl_conf->line); |
| 380 | cfgerr++; |
| 381 | } |
| 382 | |
| 383 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
| 384 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 385 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
| 386 | SSL_CTX_set_tlsext_servername_arg(ctx, ssl_conf); |
| 387 | #endif |
| 388 | return cfgerr; |
| 389 | } |
| 390 | |
| 391 | /* Walks down the two trees in ssl_conf and prepares all certs. The pointer may |
| 392 | * be NULL, in which case nothing is done. Returns the number of errors |
| 393 | * encountered. |
| 394 | */ |
| 395 | int ssl_sock_prepare_all_ctx(struct ssl_conf *ssl_conf, struct proxy *px) |
| 396 | { |
| 397 | struct ebmb_node *node; |
| 398 | struct sni_ctx *sni; |
| 399 | int err = 0; |
| 400 | |
| 401 | if (!ssl_conf) |
| 402 | return 0; |
| 403 | |
| 404 | node = ebmb_first(&ssl_conf->sni_ctx); |
| 405 | while (node) { |
| 406 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 407 | if (!sni->order) /* only initialize the CTX on its first occurrence */ |
| 408 | err += ssl_sock_prepare_ctx(ssl_conf, sni->ctx, px); |
| 409 | node = ebmb_next(node); |
| 410 | } |
| 411 | |
| 412 | node = ebmb_first(&ssl_conf->sni_w_ctx); |
| 413 | while (node) { |
| 414 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 415 | if (!sni->order) /* only initialize the CTX on its first occurrence */ |
| 416 | err += ssl_sock_prepare_ctx(ssl_conf, sni->ctx, px); |
| 417 | node = ebmb_next(node); |
| 418 | } |
| 419 | return err; |
| 420 | } |
| 421 | |
| 422 | /* Walks down the two trees in ssl_conf and frees all the certs. The pointer may |
| 423 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 424 | */ |
| 425 | void ssl_sock_free_all_ctx(struct ssl_conf *ssl_conf) |
| 426 | { |
| 427 | struct ebmb_node *node, *back; |
| 428 | struct sni_ctx *sni; |
| 429 | |
| 430 | if (!ssl_conf) |
| 431 | return; |
| 432 | |
| 433 | node = ebmb_first(&ssl_conf->sni_ctx); |
| 434 | while (node) { |
| 435 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 436 | back = ebmb_next(node); |
| 437 | ebmb_delete(node); |
| 438 | if (!sni->order) /* only free the CTX on its first occurrence */ |
| 439 | SSL_CTX_free(sni->ctx); |
| 440 | free(sni); |
| 441 | node = back; |
| 442 | } |
| 443 | |
| 444 | node = ebmb_first(&ssl_conf->sni_w_ctx); |
| 445 | while (node) { |
| 446 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 447 | back = ebmb_next(node); |
| 448 | ebmb_delete(node); |
| 449 | if (!sni->order) /* only free the CTX on its first occurrence */ |
| 450 | SSL_CTX_free(sni->ctx); |
| 451 | free(sni); |
| 452 | node = back; |
| 453 | } |
| 454 | |
| 455 | ssl_conf->default_ctx = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 458 | /* |
| 459 | * This function is called if SSL * context is not yet allocated. The function |
| 460 | * is designed to be called before any other data-layer operation and sets the |
| 461 | * handshake flag on the connection. It is safe to call it multiple times. |
| 462 | * It returns 0 on success and -1 in error case. |
| 463 | */ |
| 464 | static int ssl_sock_init(struct connection *conn) |
| 465 | { |
| 466 | /* already initialized */ |
| 467 | if (conn->data_ctx) |
| 468 | return 0; |
| 469 | |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 470 | if (global.maxsslconn && sslconns >= global.maxsslconn) |
| 471 | return -1; |
| 472 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 473 | /* If it is in client mode initiate SSL session |
| 474 | in connect state otherwise accept state */ |
| 475 | if (target_srv(&conn->target)) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 476 | /* Alloc a new SSL session ctx */ |
| 477 | conn->data_ctx = SSL_new(target_srv(&conn->target)->ssl_ctx.ctx); |
| 478 | if (!conn->data_ctx) |
| 479 | return -1; |
| 480 | |
| 481 | SSL_set_connect_state(conn->data_ctx); |
| 482 | if (target_srv(&conn->target)->ssl_ctx.reused_sess) |
| 483 | SSL_set_session(conn->data_ctx, target_srv(&conn->target)->ssl_ctx.reused_sess); |
| 484 | |
| 485 | /* set fd on SSL session context */ |
| 486 | SSL_set_fd(conn->data_ctx, conn->t.sock.fd); |
| 487 | |
| 488 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 489 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 490 | |
| 491 | sslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | else if (target_client(&conn->target)) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 495 | /* Alloc a new SSL session ctx */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 496 | conn->data_ctx = SSL_new(target_client(&conn->target)->ssl_conf->default_ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 497 | if (!conn->data_ctx) |
| 498 | return -1; |
| 499 | |
| 500 | SSL_set_accept_state(conn->data_ctx); |
| 501 | |
| 502 | /* set fd on SSL session context */ |
| 503 | SSL_set_fd(conn->data_ctx, conn->t.sock.fd); |
| 504 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 505 | /* set connection pointer */ |
| 506 | SSL_set_app_data(conn->data_ctx, conn); |
| 507 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 508 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 509 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 510 | |
| 511 | sslconns++; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | /* don't know how to handle such a target */ |
| 515 | return -1; |
| 516 | } |
| 517 | |
| 518 | |
| 519 | /* This is the callback which is used when an SSL handshake is pending. It |
| 520 | * updates the FD status if it wants some polling before being called again. |
| 521 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 522 | * otherwise it returns non-zero and removes itself from the connection's |
| 523 | * flags (the bit is provided in <flag> by the caller). |
| 524 | */ |
| 525 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 526 | { |
| 527 | int ret; |
| 528 | |
| 529 | if (!conn->data_ctx) |
| 530 | goto out_error; |
| 531 | |
| 532 | ret = SSL_do_handshake(conn->data_ctx); |
| 533 | if (ret != 1) { |
| 534 | /* handshake did not complete, let's find why */ |
| 535 | ret = SSL_get_error(conn->data_ctx, ret); |
| 536 | |
| 537 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 538 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 539 | __conn_sock_stop_recv(conn); |
| 540 | __conn_sock_poll_send(conn); |
| 541 | return 0; |
| 542 | } |
| 543 | else if (ret == SSL_ERROR_WANT_READ) { |
| 544 | /* SSL handshake needs to read, L4 connection is ready */ |
| 545 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 546 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 547 | __conn_sock_stop_send(conn); |
| 548 | __conn_sock_poll_recv(conn); |
| 549 | return 0; |
| 550 | } |
| 551 | else { |
| 552 | /* Fail on all other handshake errors */ |
| 553 | goto out_error; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /* Handshake succeeded */ |
| 558 | if (target_srv(&conn->target)) { |
| 559 | if (!SSL_session_reused(conn->data_ctx)) { |
| 560 | /* check if session was reused, if not store current session on server for reuse */ |
| 561 | if (target_srv(&conn->target)->ssl_ctx.reused_sess) |
| 562 | SSL_SESSION_free(target_srv(&conn->target)->ssl_ctx.reused_sess); |
| 563 | |
| 564 | target_srv(&conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->data_ctx); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /* The connection is now established at both layers, it's time to leave */ |
| 569 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 570 | return 1; |
| 571 | |
| 572 | out_error: |
| 573 | /* Fail on all other handshake errors */ |
| 574 | conn->flags |= CO_FL_ERROR; |
| 575 | conn->flags &= ~flag; |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | /* Receive up to <count> bytes from connection <conn>'s socket and store them |
| 580 | * into buffer <buf>. The caller must ensure that <count> is always smaller |
| 581 | * than the buffer's size. Only one call to recv() is performed, unless the |
| 582 | * buffer wraps, in which case a second call may be performed. The connection's |
| 583 | * flags are updated with whatever special event is detected (error, read0, |
| 584 | * empty). The caller is responsible for taking care of those events and |
| 585 | * avoiding the call if inappropriate. The function does not call the |
| 586 | * connection's polling update function, so the caller is responsible for this. |
| 587 | */ |
| 588 | static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count) |
| 589 | { |
| 590 | int ret, done = 0; |
| 591 | int try = count; |
| 592 | |
| 593 | if (!conn->data_ctx) |
| 594 | goto out_error; |
| 595 | |
| 596 | if (conn->flags & CO_FL_HANDSHAKE) |
| 597 | /* a handshake was requested */ |
| 598 | return 0; |
| 599 | |
| 600 | /* compute the maximum block size we can read at once. */ |
| 601 | if (buffer_empty(buf)) { |
| 602 | /* let's realign the buffer to optimize I/O */ |
| 603 | buf->p = buf->data; |
| 604 | } |
| 605 | else if (buf->data + buf->o < buf->p && |
| 606 | buf->p + buf->i < buf->data + buf->size) { |
| 607 | /* remaining space wraps at the end, with a moving limit */ |
| 608 | if (try > buf->data + buf->size - (buf->p + buf->i)) |
| 609 | try = buf->data + buf->size - (buf->p + buf->i); |
| 610 | } |
| 611 | |
| 612 | /* read the largest possible block. For this, we perform only one call |
| 613 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 614 | * in which case we accept to do it once again. A new attempt is made on |
| 615 | * EINTR too. |
| 616 | */ |
| 617 | while (try) { |
| 618 | ret = SSL_read(conn->data_ctx, bi_end(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 619 | if (conn->flags & CO_FL_ERROR) { |
| 620 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
| 621 | break; |
| 622 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 623 | if (ret > 0) { |
| 624 | buf->i += ret; |
| 625 | done += ret; |
| 626 | if (ret < try) |
| 627 | break; |
| 628 | count -= ret; |
| 629 | try = count; |
| 630 | } |
| 631 | else if (ret == 0) { |
| 632 | goto read0; |
| 633 | } |
| 634 | else { |
| 635 | ret = SSL_get_error(conn->data_ctx, ret); |
| 636 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 637 | /* handshake is running, and it needs to poll for a write event */ |
| 638 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 639 | __conn_sock_poll_send(conn); |
| 640 | break; |
| 641 | } |
| 642 | else if (ret == SSL_ERROR_WANT_READ) { |
| 643 | /* we need to poll for retry a read later */ |
| 644 | __conn_data_poll_recv(conn); |
| 645 | break; |
| 646 | } |
| 647 | /* otherwise it's a real error */ |
| 648 | goto out_error; |
| 649 | } |
| 650 | } |
| 651 | return done; |
| 652 | |
| 653 | read0: |
| 654 | conn_sock_read0(conn); |
| 655 | return done; |
| 656 | out_error: |
| 657 | conn->flags |= CO_FL_ERROR; |
| 658 | return done; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | /* Send all pending bytes from buffer <buf> to connection <conn>'s socket. |
| 663 | * <flags> may contain MSG_MORE to make the system hold on without sending |
| 664 | * data too fast, but this flag is ignored at the moment. |
| 665 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 666 | * a second call may be performed. The connection's flags are updated with |
| 667 | * whatever special event is detected (error, empty). The caller is responsible |
| 668 | * for taking care of those events and avoiding the call if inappropriate. The |
| 669 | * function does not call the connection's polling update function, so the caller |
| 670 | * is responsible for this. |
| 671 | */ |
| 672 | static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags) |
| 673 | { |
| 674 | int ret, try, done; |
| 675 | |
| 676 | done = 0; |
| 677 | |
| 678 | if (!conn->data_ctx) |
| 679 | goto out_error; |
| 680 | |
| 681 | if (conn->flags & CO_FL_HANDSHAKE) |
| 682 | /* a handshake was requested */ |
| 683 | return 0; |
| 684 | |
| 685 | /* send the largest possible block. For this we perform only one call |
| 686 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 687 | * in which case we accept to do it once again. |
| 688 | */ |
| 689 | while (buf->o) { |
| 690 | try = buf->o; |
| 691 | /* outgoing data may wrap at the end */ |
| 692 | if (buf->data + try > buf->p) |
| 693 | try = buf->data + try - buf->p; |
| 694 | |
| 695 | ret = SSL_write(conn->data_ctx, bo_ptr(buf), try); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 696 | if (conn->flags & CO_FL_ERROR) { |
| 697 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
| 698 | break; |
| 699 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 700 | if (ret > 0) { |
| 701 | buf->o -= ret; |
| 702 | done += ret; |
| 703 | |
| 704 | if (likely(!buffer_len(buf))) |
| 705 | /* optimize data alignment in the buffer */ |
| 706 | buf->p = buf->data; |
| 707 | |
| 708 | /* if the system buffer is full, don't insist */ |
| 709 | if (ret < try) |
| 710 | break; |
| 711 | } |
| 712 | else { |
| 713 | ret = SSL_get_error(conn->data_ctx, ret); |
| 714 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 715 | /* we need to poll to retry a write later */ |
| 716 | __conn_data_poll_send(conn); |
| 717 | break; |
| 718 | } |
| 719 | else if (ret == SSL_ERROR_WANT_READ) { |
| 720 | /* handshake is running, and |
| 721 | it needs to poll for a read event, |
| 722 | write polling must be disabled cause |
| 723 | we are sure we can't write anything more |
| 724 | before handshake re-performed */ |
| 725 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 726 | __conn_sock_poll_recv(conn); |
| 727 | break; |
| 728 | } |
| 729 | goto out_error; |
| 730 | } |
| 731 | } |
| 732 | return done; |
| 733 | |
| 734 | out_error: |
| 735 | conn->flags |= CO_FL_ERROR; |
| 736 | return done; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | static void ssl_sock_close(struct connection *conn) { |
| 741 | |
| 742 | if (conn->data_ctx) { |
| 743 | SSL_free(conn->data_ctx); |
| 744 | conn->data_ctx = NULL; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 745 | sslconns--; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 746 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 750 | * any case, flags the connection as reusable if no handshake was in progress. |
| 751 | */ |
| 752 | static void ssl_sock_shutw(struct connection *conn, int clean) |
| 753 | { |
| 754 | if (conn->flags & CO_FL_HANDSHAKE) |
| 755 | return; |
| 756 | /* no handshake was in progress, try a clean ssl shutdown */ |
| 757 | if (clean) |
| 758 | SSL_shutdown(conn->data_ctx); |
| 759 | |
| 760 | /* force flag on ssl to keep session in cache regardless shutdown result */ |
| 761 | SSL_set_shutdown(conn->data_ctx, SSL_SENT_SHUTDOWN); |
| 762 | } |
| 763 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 764 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 765 | |
| 766 | /* boolean, returns true if data layer is SSL */ |
| 767 | static int |
| 768 | smp_fetch_is_ssl(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 769 | const struct arg *args, struct sample *smp) |
| 770 | { |
| 771 | smp->type = SMP_T_BOOL; |
| 772 | smp->data.uint = (l4->si[0].conn.data == &ssl_sock); |
| 773 | return 1; |
| 774 | } |
| 775 | |
| 776 | /* boolean, returns true if data layer is SSL */ |
| 777 | static int |
| 778 | smp_fetch_has_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 779 | const struct arg *args, struct sample *smp) |
| 780 | { |
| 781 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 782 | smp->type = SMP_T_BOOL; |
| 783 | smp->data.uint = (l4->si[0].conn.data == &ssl_sock) && |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 784 | l4->si[0].conn.data_ctx && |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 785 | SSL_get_servername(l4->si[0].conn.data_ctx, TLSEXT_NAMETYPE_host_name) != NULL; |
| 786 | return 1; |
| 787 | #else |
| 788 | return 0; |
| 789 | #endif |
| 790 | } |
| 791 | |
| 792 | static int |
| 793 | smp_fetch_ssl_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 794 | const struct arg *args, struct sample *smp) |
| 795 | { |
| 796 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 797 | smp->flags = 0; |
| 798 | smp->type = SMP_T_CSTR; |
| 799 | |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 800 | if (!l4 || !l4->si[0].conn.data_ctx || l4->si[0].conn.data != &ssl_sock) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 801 | return 0; |
| 802 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 803 | smp->data.str.str = (char *)SSL_get_servername(l4->si[0].conn.data_ctx, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 804 | if (!smp->data.str.str) |
| 805 | return 0; |
| 806 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 807 | smp->data.str.len = strlen(smp->data.str.str); |
| 808 | return 1; |
| 809 | #else |
| 810 | return 0; |
| 811 | #endif |
| 812 | } |
| 813 | |
| 814 | /* Note: must not be declared <const> as its list will be overwritten. |
| 815 | * Please take care of keeping this list alphabetically sorted. |
| 816 | */ |
| 817 | static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{ |
| 818 | { "is_ssl", smp_fetch_is_ssl, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES }, |
| 819 | { "ssl_has_sni", smp_fetch_has_sni, 0, NULL, SMP_T_BOOL, SMP_CAP_REQ|SMP_CAP_RES }, |
| 820 | { "ssl_sni", smp_fetch_ssl_sni, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES }, |
| 821 | { NULL, NULL, 0, 0, 0 }, |
| 822 | }}; |
| 823 | |
| 824 | /* Note: must not be declared <const> as its list will be overwritten. |
| 825 | * Please take care of keeping this list alphabetically sorted. |
| 826 | */ |
| 827 | static struct acl_kw_list acl_kws = {{ },{ |
| 828 | { "is_ssl", acl_parse_int, smp_fetch_is_ssl, acl_match_nothing, ACL_USE_L6REQ_PERMANENT, 0 }, |
| 829 | { "ssl_has_sni", acl_parse_int, smp_fetch_has_sni, acl_match_nothing, ACL_USE_L6REQ_PERMANENT, 0 }, |
| 830 | { "ssl_sni", acl_parse_str, smp_fetch_ssl_sni, acl_match_str, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
| 831 | { "ssl_sni_end", acl_parse_str, smp_fetch_ssl_sni, acl_match_end, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
| 832 | { "ssl_sni_reg", acl_parse_str, smp_fetch_ssl_sni, acl_match_reg, ACL_USE_L6REQ_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
| 833 | { NULL, NULL, NULL, NULL }, |
| 834 | }}; |
| 835 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 836 | |
| 837 | /* data-layer operations for SSL sockets */ |
| 838 | struct data_ops ssl_sock = { |
| 839 | .snd_buf = ssl_sock_from_buf, |
| 840 | .rcv_buf = ssl_sock_to_buf, |
| 841 | .rcv_pipe = NULL, |
| 842 | .snd_pipe = NULL, |
| 843 | .shutr = NULL, |
| 844 | .shutw = ssl_sock_shutw, |
| 845 | .close = ssl_sock_close, |
| 846 | .init = ssl_sock_init, |
| 847 | }; |
| 848 | |
| 849 | __attribute__((constructor)) |
| 850 | static void __ssl_sock_init(void) { |
| 851 | STACK_OF(SSL_COMP)* cm; |
| 852 | |
| 853 | SSL_library_init(); |
| 854 | cm = SSL_COMP_get_compression_methods(); |
| 855 | sk_SSL_COMP_zero(cm); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 856 | sample_register_fetches(&sample_fetch_keywords); |
| 857 | acl_register_keywords(&acl_kws); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /* |
| 861 | * Local variables: |
| 862 | * c-indent-level: 8 |
| 863 | * c-basic-offset: 8 |
| 864 | * End: |
| 865 | */ |