1 #include "first.h" 2 3 #include <errno.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <unistd.h> 7 8 #ifndef USE_OPENSSL_KERBEROS 9 #ifndef OPENSSL_NO_KRB5 10 #define OPENSSL_NO_KRB5 11 #endif 12 #endif 13 14 #include "sys-crypto.h" 15 16 #include <openssl/ssl.h> 17 #include <openssl/bio.h> 18 #include <openssl/bn.h> 19 #include <openssl/err.h> 20 #include <openssl/objects.h> 21 #include <openssl/pem.h> 22 #include <openssl/rand.h> 23 #ifndef OPENSSL_NO_DH 24 #include <openssl/dh.h> 25 #endif 26 27 #if ! defined OPENSSL_NO_TLSEXT && ! defined SSL_CTRL_SET_TLSEXT_HOSTNAME 28 #define OPENSSL_NO_TLSEXT 29 #endif 30 31 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL 32 #ifndef OPENSSL_NO_ECDH 33 #include <openssl/ecdh.h> 34 #endif 35 #endif 36 37 #include "base.h" 38 #include "http_header.h" 39 #include "log.h" 40 #include "plugin.h" 41 42 typedef struct { 43 /* SNI per host: with COMP_SERVER_SOCKET, COMP_HTTP_SCHEME, COMP_HTTP_HOST */ 44 EVP_PKEY *ssl_pemfile_pkey; 45 X509 *ssl_pemfile_x509; 46 const buffer *ssl_pemfile; 47 const buffer *ssl_privkey; 48 } plugin_cert; 49 50 typedef struct { 51 SSL_CTX *ssl_ctx; 52 EVP_PKEY *ssl_pemfile_pkey; 53 } plugin_ssl_ctx; 54 55 typedef struct { 56 SSL_CTX *ssl_ctx; /* output from network_init_ssl() */ 57 58 /*(used only during startup; not patched)*/ 59 unsigned char ssl_enabled; /* only interesting for setting up listening sockets. don't use at runtime */ 60 unsigned char ssl_honor_cipher_order; /* determine SSL cipher in server-preferred order, not client-order */ 61 unsigned char ssl_empty_fragments; /* whether to not set SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS */ 62 unsigned char ssl_use_sslv2; 63 unsigned char ssl_use_sslv3; 64 const buffer *ssl_cipher_list; 65 const buffer *ssl_dh_file; 66 const buffer *ssl_ec_curve; 67 array *ssl_conf_cmd; 68 69 /*(copied from plugin_data for socket ssl_ctx config)*/ 70 EVP_PKEY *ssl_pemfile_pkey; 71 X509 *ssl_pemfile_x509; 72 const buffer *ssl_pemfile; 73 const buffer *ssl_privkey; 74 STACK_OF(X509_NAME) *ssl_ca_file; 75 STACK_OF(X509_NAME) *ssl_ca_dn_file; 76 const buffer *ssl_ca_crl_file; 77 unsigned char ssl_verifyclient; 78 unsigned char ssl_verifyclient_enforce; 79 unsigned char ssl_verifyclient_depth; 80 unsigned char ssl_read_ahead; 81 } plugin_config_socket; /*(used at startup during configuration)*/ 82 83 typedef struct { 84 /* SNI per host: w/ COMP_SERVER_SOCKET, COMP_HTTP_SCHEME, COMP_HTTP_HOST */ 85 EVP_PKEY *ssl_pemfile_pkey; 86 X509 *ssl_pemfile_x509; 87 const buffer *ssl_pemfile; 88 STACK_OF(X509_NAME) *ssl_ca_file; 89 STACK_OF(X509_NAME) *ssl_ca_dn_file; 90 const buffer *ssl_ca_crl_file; 91 92 unsigned char ssl_verifyclient; 93 unsigned char ssl_verifyclient_enforce; 94 unsigned char ssl_verifyclient_depth; 95 unsigned char ssl_verifyclient_export_cert; 96 unsigned char ssl_read_ahead; 97 unsigned char ssl_log_noise; 98 unsigned char ssl_disable_client_renegotiation; 99 const buffer *ssl_verifyclient_username; 100 const buffer *ssl_acme_tls_1; 101 } plugin_config; 102 103 typedef struct { 104 PLUGIN_DATA; 105 plugin_ssl_ctx *ssl_ctxs; 106 plugin_config defaults; 107 server *srv; 108 array *cafiles; 109 } plugin_data; 110 111 static int ssl_is_init; 112 /* need assigned p->id for deep access of module handler_ctx for connection 113 * i.e. handler_ctx *hctx = r->plugin_ctx[plugin_data_singleton->id]; */ 114 static plugin_data *plugin_data_singleton; 115 #define LOCAL_SEND_BUFSIZE (16 * 1024) 116 static char *local_send_buffer; 117 118 typedef struct { 119 SSL *ssl; 120 request_st *r; 121 connection *con; 122 short renegotiations; /* count of SSL_CB_HANDSHAKE_START */ 123 short close_notify; 124 unsigned short request_env_patched; 125 unsigned short alpn; 126 plugin_config conf; 127 buffer *tmp_buf; 128 } handler_ctx; 129 130 131 static handler_ctx * 132 handler_ctx_init (void) 133 { 134 handler_ctx *hctx = calloc(1, sizeof(*hctx)); 135 force_assert(hctx); 136 return hctx; 137 } 138 139 140 static void 141 handler_ctx_free (handler_ctx *hctx) 142 { 143 if (hctx->ssl) SSL_free(hctx->ssl); 144 free(hctx); 145 } 146 147 148 INIT_FUNC(mod_openssl_init) 149 { 150 plugin_data_singleton = (plugin_data *)calloc(1, sizeof(plugin_data)); 151 #ifdef DEBUG_WOLFSSL 152 wolfSSL_Debugging_ON(); 153 #endif 154 return plugin_data_singleton; 155 } 156 157 158 static int mod_openssl_init_once_openssl (server *srv) 159 { 160 if (ssl_is_init) return 1; 161 162 #if OPENSSL_VERSION_NUMBER >= 0x10100000L \ 163 && !defined(LIBRESSL_VERSION_NUMBER) 164 OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS 165 |OPENSSL_INIT_LOAD_CRYPTO_STRINGS,NULL); 166 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS 167 |OPENSSL_INIT_ADD_ALL_DIGESTS 168 |OPENSSL_INIT_LOAD_CONFIG, NULL); 169 #else 170 SSL_load_error_strings(); 171 SSL_library_init(); 172 OpenSSL_add_all_algorithms(); 173 #endif 174 ssl_is_init = 1; 175 176 if (0 == RAND_status()) { 177 log_error(srv->errh, __FILE__, __LINE__, 178 "SSL: not enough entropy in the pool"); 179 return 0; 180 } 181 182 local_send_buffer = malloc(LOCAL_SEND_BUFSIZE); 183 force_assert(NULL != local_send_buffer); 184 185 return 1; 186 } 187 188 189 static void mod_openssl_free_openssl (void) 190 { 191 if (!ssl_is_init) return; 192 193 #if OPENSSL_VERSION_NUMBER >= 0x10100000L \ 194 && !defined(LIBRESSL_VERSION_NUMBER) 195 /*(OpenSSL libraries handle thread init and deinit) 196 * https://github.com/openssl/openssl/pull/1048 */ 197 #else 198 CRYPTO_cleanup_all_ex_data(); 199 ERR_free_strings(); 200 #if OPENSSL_VERSION_NUMBER >= 0x10000000L 201 ERR_remove_thread_state(NULL); 202 #else 203 ERR_remove_state(0); 204 #endif 205 EVP_cleanup(); 206 #endif 207 208 free(local_send_buffer); 209 ssl_is_init = 0; 210 } 211 212 213 static void 214 mod_openssl_free_config (server *srv, plugin_data * const p) 215 { 216 array_free(p->cafiles); 217 218 if (NULL != p->ssl_ctxs) { 219 SSL_CTX * const ssl_ctx_global_scope = p->ssl_ctxs->ssl_ctx; 220 /* free ssl_ctx from $SERVER["socket"] (if not copy of global scope) */ 221 for (uint32_t i = 1; i < srv->config_context->used; ++i) { 222 plugin_ssl_ctx * const s = p->ssl_ctxs + i; 223 if (s->ssl_ctx && s->ssl_ctx != ssl_ctx_global_scope) 224 SSL_CTX_free(s->ssl_ctx); 225 } 226 /* free ssl_ctx from global scope */ 227 if (ssl_ctx_global_scope) 228 SSL_CTX_free(ssl_ctx_global_scope); 229 free(p->ssl_ctxs); 230 } 231 232 if (NULL == p->cvlist) return; 233 /* (init i to 0 if global context; to 1 to skip empty global context) */ 234 for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 235 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 236 for (; -1 != cpv->k_id; ++cpv) { 237 switch (cpv->k_id) { 238 case 0: /* ssl.pemfile */ 239 if (cpv->vtype == T_CONFIG_LOCAL) { 240 plugin_cert *pc = cpv->v.v; 241 EVP_PKEY_free(pc->ssl_pemfile_pkey); 242 X509_free(pc->ssl_pemfile_x509); 243 } 244 break; 245 case 2: /* ssl.ca-file */ 246 case 3: /* ssl.ca-dn-file */ 247 if (cpv->vtype == T_CONFIG_LOCAL) 248 sk_X509_NAME_pop_free(cpv->v.v, X509_NAME_free); 249 break; 250 default: 251 break; 252 } 253 } 254 } 255 } 256 257 258 static int 259 mod_openssl_load_verify_locn (SSL_CTX *ssl_ctx, const buffer *b, server *srv) 260 { 261 const char *fn = b->ptr; 262 if (1 == SSL_CTX_load_verify_locations(ssl_ctx, fn, NULL)) 263 return 1; 264 265 log_error(srv->errh, __FILE__, __LINE__, 266 "SSL: %s %s", ERR_error_string(ERR_get_error(), NULL), fn); 267 return 0; 268 } 269 270 271 static int 272 mod_openssl_load_ca_files (SSL_CTX *ssl_ctx, plugin_data *p, server *srv) 273 { 274 /* load all ssl.ca-files specified in the config 275 * into each SSL_CTX to be prepared for SNI */ 276 277 for (uint32_t i = 0, used = p->cafiles->used; i < used; ++i) { 278 const buffer *b = &((data_string *)p->cafiles->data[i])->value; 279 if (!mod_openssl_load_verify_locn(ssl_ctx, b, srv)) 280 return 0; 281 } 282 return 1; 283 } 284 285 286 FREE_FUNC(mod_openssl_free) 287 { 288 plugin_data *p = p_d; 289 if (NULL == p->srv) return; 290 mod_openssl_free_config(p->srv, p); 291 mod_openssl_free_openssl(); 292 } 293 294 295 static void 296 mod_openssl_merge_config_cpv (plugin_config * const pconf, const config_plugin_value_t * const cpv) 297 { 298 switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ 299 case 0: /* ssl.pemfile */ 300 if (cpv->vtype == T_CONFIG_LOCAL) { 301 plugin_cert *pc = cpv->v.v; 302 pconf->ssl_pemfile_pkey = pc->ssl_pemfile_pkey; 303 pconf->ssl_pemfile_x509 = pc->ssl_pemfile_x509; 304 pconf->ssl_pemfile = pc->ssl_pemfile; 305 } 306 break; 307 case 1: /* ssl.privkey */ 308 break; 309 case 2: /* ssl.ca-file */ 310 if (cpv->vtype == T_CONFIG_LOCAL) 311 pconf->ssl_ca_file = cpv->v.v; 312 break; 313 case 3: /* ssl.ca-dn-file */ 314 if (cpv->vtype == T_CONFIG_LOCAL) 315 pconf->ssl_ca_dn_file = cpv->v.v; 316 break; 317 case 4: /* ssl.ca-crl-file */ 318 pconf->ssl_ca_crl_file = cpv->v.b; 319 break; 320 case 5: /* ssl.read-ahead */ 321 pconf->ssl_read_ahead = (0 != cpv->v.u); 322 break; 323 case 6: /* ssl.disable-client-renegotiation */ 324 pconf->ssl_disable_client_renegotiation = (0 != cpv->v.u); 325 break; 326 case 7: /* ssl.verifyclient.activate */ 327 pconf->ssl_verifyclient = (0 != cpv->v.u); 328 break; 329 case 8: /* ssl.verifyclient.enforce */ 330 pconf->ssl_verifyclient_enforce = (0 != cpv->v.u); 331 break; 332 case 9: /* ssl.verifyclient.depth */ 333 pconf->ssl_verifyclient_depth = (unsigned char)cpv->v.shrt; 334 break; 335 case 10:/* ssl.verifyclient.username */ 336 pconf->ssl_verifyclient_username = cpv->v.b; 337 break; 338 case 11:/* ssl.verifyclient.exportcert */ 339 pconf->ssl_verifyclient_export_cert = (0 != cpv->v.u); 340 break; 341 case 12:/* ssl.acme-tls-1 */ 342 pconf->ssl_acme_tls_1 = cpv->v.b; 343 break; 344 case 13:/* debug.log-ssl-noise */ 345 pconf->ssl_log_noise = (0 != cpv->v.u); 346 break; 347 default:/* should not happen */ 348 return; 349 } 350 } 351 352 353 static void 354 mod_openssl_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) 355 { 356 do { 357 mod_openssl_merge_config_cpv(pconf, cpv); 358 } while ((++cpv)->k_id != -1); 359 } 360 361 362 static void 363 mod_openssl_patch_config (request_st * const r, plugin_config * const pconf) 364 { 365 plugin_data * const p = plugin_data_singleton; 366 memcpy(pconf, &p->defaults, sizeof(plugin_config)); 367 for (int i = 1, used = p->nconfig; i < used; ++i) { 368 if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id)) 369 mod_openssl_merge_config(pconf, p->cvlist + p->cvlist[i].v.u2[0]); 370 } 371 } 372 373 374 static int 375 safer_X509_NAME_oneline(X509_NAME *name, char *buf, size_t sz) 376 { 377 BIO *bio = BIO_new(BIO_s_mem()); 378 if (bio) { 379 int len = X509_NAME_print_ex(bio, name, 0, XN_FLAG_ONELINE); 380 BIO_gets(bio, buf, (int)sz); /*(may be truncated if len >= sz)*/ 381 BIO_free(bio); 382 return len; /*return value has similar semantics to that of snprintf()*/ 383 } 384 else { 385 buf[0] = '\0'; 386 return -1; 387 } 388 } 389 390 391 static void 392 ssl_info_callback (const SSL *ssl, int where, int ret) 393 { 394 UNUSED(ret); 395 396 if (0 != (where & SSL_CB_HANDSHAKE_START)) { 397 handler_ctx *hctx = (handler_ctx *) SSL_get_app_data(ssl); 398 if (hctx->renegotiations >= 0) ++hctx->renegotiations; 399 } 400 #ifdef TLS1_3_VERSION 401 /* https://github.com/openssl/openssl/issues/5721 402 * "TLSv1.3 unexpected InfoCallback after handshake completed" */ 403 if (0 != (where & SSL_CB_HANDSHAKE_DONE)) { 404 /* SSL_version() is valid after initial handshake completed */ 405 if (SSL_version(ssl) >= TLS1_3_VERSION) { 406 /* https://wiki.openssl.org/index.php/TLS1.3 407 * "Renegotiation is not possible in a TLSv1.3 connection" */ 408 handler_ctx *hctx = (handler_ctx *) SSL_get_app_data(ssl); 409 hctx->renegotiations = -1; 410 } 411 } 412 #endif 413 } 414 415 /* https://wiki.openssl.org/index.php/Manual:SSL_CTX_set_verify(3)#EXAMPLES */ 416 static int 417 verify_callback(int preverify_ok, X509_STORE_CTX *ctx) 418 { 419 char buf[256]; 420 X509 *err_cert; 421 int err, depth; 422 SSL *ssl; 423 handler_ctx *hctx; 424 425 err = X509_STORE_CTX_get_error(ctx); 426 depth = X509_STORE_CTX_get_error_depth(ctx); 427 428 /* 429 * Retrieve the pointer to the SSL of the connection currently treated 430 * and the application specific data stored into the SSL object. 431 */ 432 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); 433 hctx = (handler_ctx *) SSL_get_app_data(ssl); 434 435 /* 436 * Catch a too long certificate chain. The depth limit set using 437 * SSL_CTX_set_verify_depth() is by purpose set to "limit+1" so 438 * that whenever the "depth>verify_depth" condition is met, we 439 * have violated the limit and want to log this error condition. 440 * We must do it here, because the CHAIN_TOO_LONG error would not 441 * be found explicitly; only errors introduced by cutting off the 442 * additional certificates would be logged. 443 */ 444 if (depth > hctx->conf.ssl_verifyclient_depth) { 445 preverify_ok = 0; 446 err = X509_V_ERR_CERT_CHAIN_TOO_LONG; 447 X509_STORE_CTX_set_error(ctx, err); 448 } 449 450 if (preverify_ok && 0 == depth 451 && NULL != hctx->conf.ssl_ca_dn_file 452 && NULL != hctx->conf.ssl_ca_file) { 453 /* verify that client cert is issued by CA in ssl.ca-dn-file 454 * if both ssl.ca-dn-file and ssl.ca-file were configured */ 455 STACK_OF(X509_NAME) * const cert_names = hctx->conf.ssl_ca_dn_file; 456 X509_NAME *issuer; 457 #if OPENSSL_VERSION_NUMBER >= 0x10002000L 458 err_cert = X509_STORE_CTX_get_current_cert(ctx); 459 #else 460 err_cert = ctx->current_cert; 461 #endif 462 if (NULL == err_cert) return !hctx->conf.ssl_verifyclient_enforce; 463 issuer = X509_get_issuer_name(err_cert); 464 #if 0 /*(?desirable/undesirable to have cert_names sorted?)*/ 465 if (-1 != sk_X509_NAME_find(cert_names, issuer)) 466 return preverify_ok; /* match */ 467 #else 468 for (int i = 0, len = sk_X509_NAME_num(cert_names); i < len; ++i) { 469 if (0 == X509_NAME_cmp(sk_X509_NAME_value(cert_names, i), issuer)) 470 return preverify_ok; /* match */ 471 } 472 #endif 473 474 preverify_ok = 0; 475 err = X509_V_ERR_CERT_REJECTED; 476 X509_STORE_CTX_set_error(ctx, err); 477 } 478 479 if (preverify_ok) { 480 return preverify_ok; 481 } 482 483 #if OPENSSL_VERSION_NUMBER >= 0x10002000L 484 err_cert = X509_STORE_CTX_get_current_cert(ctx); 485 #else 486 err_cert = ctx->current_cert; 487 #endif 488 if (NULL == err_cert) return !hctx->conf.ssl_verifyclient_enforce; 489 safer_X509_NAME_oneline(X509_get_subject_name(err_cert),buf,sizeof(buf)); 490 log_error_st *errh = hctx->r->conf.errh; 491 log_error(errh, __FILE__, __LINE__, 492 "SSL: verify error:num=%d:%s:depth=%d:subject=%s", 493 err, X509_verify_cert_error_string(err), depth, buf); 494 495 /* 496 * At this point, err contains the last verification error. We can use 497 * it for something special 498 */ 499 if (!preverify_ok && (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY || 500 err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT)) { 501 safer_X509_NAME_oneline(X509_get_issuer_name(err_cert),buf,sizeof(buf)); 502 log_error(errh, __FILE__, __LINE__, "SSL: issuer=%s", buf); 503 } 504 505 return !hctx->conf.ssl_verifyclient_enforce; 506 } 507 508 #ifndef OPENSSL_NO_TLSEXT 509 static int 510 mod_openssl_SNI (SSL *ssl, handler_ctx *hctx, const char *servername, size_t len) 511 { 512 request_st * const r = hctx->r; 513 if (len >= 1024) { /*(expecting < 256; TLSEXT_MAXLEN_host_name is 255)*/ 514 log_error(r->conf.errh, __FILE__, __LINE__, 515 "SSL: SNI name too long %.*s", (int)len, servername); 516 return SSL_TLSEXT_ERR_ALERT_FATAL; 517 } 518 519 /* use SNI to patch mod_openssl config and then reset COMP_HTTP_HOST */ 520 buffer_copy_string_len(&r->uri.authority, servername, len); 521 buffer_to_lower(&r->uri.authority); 522 #if 0 523 /*(r->uri.authority used below for configuration before request read; 524 * revisit for h2)*/ 525 if (0 != http_request_host_policy(&r->uri.authority, 526 r->conf.http_parseopts, 443)) 527 return SSL_TLSEXT_ERR_ALERT_FATAL; 528 #endif 529 530 const buffer * const ssl_pemfile = hctx->conf.ssl_pemfile; 531 532 r->conditional_is_valid |= (1 << COMP_HTTP_SCHEME) 533 | (1 << COMP_HTTP_HOST); 534 mod_openssl_patch_config(r, &hctx->conf); 535 /* reset COMP_HTTP_HOST so that conditions re-run after request hdrs read */ 536 /*(done in response.c:config_cond_cache_reset() after request hdrs read)*/ 537 /*config_cond_cache_reset_item(r, COMP_HTTP_HOST);*/ 538 /*buffer_clear(&r->uri.authority);*/ 539 540 if (!buffer_is_equal(hctx->conf.ssl_pemfile, ssl_pemfile)) { 541 /* reconfigure to use SNI-specific cert if SNI-specific cert provided */ 542 543 if (NULL == hctx->conf.ssl_pemfile_x509 544 || NULL == hctx->conf.ssl_pemfile_pkey) { 545 /* x509/pkey available <=> pemfile was set <=> pemfile got patched: 546 * so this should never happen, unless you nest $SERVER["socket"] */ 547 log_error(r->conf.errh, __FILE__, __LINE__, 548 "SSL: no certificate/private key for TLS server name %s", 549 r->uri.authority.ptr); 550 return SSL_TLSEXT_ERR_ALERT_FATAL; 551 } 552 553 /* first set certificate! 554 * setting private key checks whether certificate matches it */ 555 if (1 != SSL_use_certificate(ssl, hctx->conf.ssl_pemfile_x509)) { 556 log_error(r->conf.errh, __FILE__, __LINE__, 557 "SSL: failed to set certificate for TLS server name %s: %s", 558 r->uri.authority.ptr, ERR_error_string(ERR_get_error(), NULL)); 559 return SSL_TLSEXT_ERR_ALERT_FATAL; 560 } 561 562 if (1 != SSL_use_PrivateKey(ssl, hctx->conf.ssl_pemfile_pkey)) { 563 log_error(r->conf.errh, __FILE__, __LINE__, 564 "SSL: failed to set private key for TLS server name %s: %s", 565 r->uri.authority.ptr, ERR_error_string(ERR_get_error(), NULL)); 566 return SSL_TLSEXT_ERR_ALERT_FATAL; 567 } 568 } 569 570 if (hctx->conf.ssl_verifyclient) { 571 int mode; 572 STACK_OF(X509_NAME) * const cert_names = hctx->conf.ssl_ca_dn_file 573 ? hctx->conf.ssl_ca_dn_file 574 : hctx->conf.ssl_ca_file; 575 if (NULL == cert_names) { 576 log_error(r->conf.errh, __FILE__, __LINE__, 577 "SSL: can't verify client without ssl.ca-file " 578 "or ssl.ca-dn-file for TLS server name %s: %s", 579 r->uri.authority.ptr, ERR_error_string(ERR_get_error(), NULL)); 580 return SSL_TLSEXT_ERR_ALERT_FATAL; 581 } 582 583 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(cert_names)); 584 /* forcing verification here is really not that useful 585 * -- a client could just connect without SNI */ 586 mode = SSL_VERIFY_PEER; 587 if (hctx->conf.ssl_verifyclient_enforce) { 588 mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; 589 } 590 SSL_set_verify(ssl, mode, verify_callback); 591 SSL_set_verify_depth(ssl, hctx->conf.ssl_verifyclient_depth + 1); 592 } else { 593 SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL); 594 } 595 596 return SSL_TLSEXT_ERR_OK; 597 } 598 599 #ifdef SSL_CLIENT_HELLO_SUCCESS 600 static int 601 mod_openssl_client_hello_cb (SSL *ssl, int *al, void *srv) 602 { 603 handler_ctx *hctx = (handler_ctx *) SSL_get_app_data(ssl); 604 buffer_copy_string(&hctx->r->uri.scheme, "https"); 605 UNUSED(srv); 606 607 const unsigned char *name; 608 size_t len, slen; 609 if (!SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &name, &len)) { 610 return SSL_CLIENT_HELLO_SUCCESS; /* client did not provide SNI */ 611 } 612 613 /* expecting single element in the server_name extension; parse first one */ 614 if (len > 5 615 && (size_t)((name[0] << 8) + name[1]) == len-2 616 && name[2] == TLSEXT_TYPE_server_name 617 && (slen = (name[3] << 8) + name[4]) <= len-5) { /*(first)*/ 618 int rc = mod_openssl_SNI(ssl, hctx, (const char *)name+5, slen); 619 if (rc == SSL_TLSEXT_ERR_OK) 620 return SSL_CLIENT_HELLO_SUCCESS; 621 } 622 623 *al = TLS1_AD_UNRECOGNIZED_NAME; 624 return SSL_CLIENT_HELLO_ERROR; 625 } 626 #else 627 static int 628 network_ssl_servername_callback (SSL *ssl, int *al, server *srv) 629 { 630 handler_ctx *hctx = (handler_ctx *) SSL_get_app_data(ssl); 631 buffer_copy_string(&hctx->r->uri.scheme, "https"); 632 UNUSED(al); 633 UNUSED(srv); 634 635 const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); 636 return (NULL != servername) 637 ? mod_openssl_SNI(ssl, hctx, servername, strlen(servername)) 638 : SSL_TLSEXT_ERR_NOACK; /* client did not provide SNI */ 639 } 640 #endif 641 #endif 642 643 644 static X509 * 645 x509_load_pem_file (const char *file, log_error_st *errh) 646 { 647 BIO *in; 648 X509 *x = NULL; 649 650 in = BIO_new(BIO_s_file()); 651 if (NULL == in) { 652 log_error(errh, __FILE__, __LINE__, 653 "SSL: BIO_new(BIO_s_file()) failed"); 654 goto error; 655 } 656 657 if (BIO_read_filename(in,file) <= 0) { 658 log_error(errh, __FILE__, __LINE__, 659 "SSL: BIO_read_filename('%s') failed", file); 660 goto error; 661 } 662 663 x = PEM_read_bio_X509(in, NULL, NULL, NULL); 664 if (NULL == x) { 665 log_error(errh, __FILE__, __LINE__, 666 "SSL: couldn't read X509 certificate from '%s'", file); 667 goto error; 668 } 669 670 BIO_free(in); 671 return x; 672 673 error: 674 if (NULL != in) BIO_free(in); 675 return NULL; 676 } 677 678 679 static EVP_PKEY * 680 evp_pkey_load_pem_file (const char *file, log_error_st *errh) 681 { 682 BIO *in; 683 EVP_PKEY *x = NULL; 684 685 in = BIO_new(BIO_s_file()); 686 if (NULL == in) { 687 log_error(errh, __FILE__, __LINE__, 688 "SSL: BIO_new(BIO_s_file()) failed"); 689 goto error; 690 } 691 692 if (BIO_read_filename(in,file) <= 0) { 693 log_error(errh, __FILE__, __LINE__, 694 "SSL: BIO_read_filename('%s') failed", file); 695 goto error; 696 } 697 698 x = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); 699 if (NULL == x) { 700 log_error(errh, __FILE__, __LINE__, 701 "SSL: couldn't read private key from '%s'", file); 702 goto error; 703 } 704 705 BIO_free(in); 706 return x; 707 708 error: 709 if (NULL != in) BIO_free(in); 710 return NULL; 711 } 712 713 714 static void * 715 network_openssl_load_pemfile (server *srv, const buffer *pemfile, const buffer *privkey) 716 { 717 if (!mod_openssl_init_once_openssl(srv)) return NULL; 718 719 X509 *ssl_pemfile_x509 = x509_load_pem_file(pemfile->ptr, srv->errh); 720 if (NULL == ssl_pemfile_x509) 721 return NULL; 722 723 EVP_PKEY *ssl_pemfile_pkey = evp_pkey_load_pem_file(privkey->ptr, srv->errh); 724 if (NULL == ssl_pemfile_pkey) { 725 X509_free(ssl_pemfile_x509); 726 return NULL; 727 } 728 729 if (!X509_check_private_key(ssl_pemfile_x509, ssl_pemfile_pkey)) { 730 log_error(srv->errh, __FILE__, __LINE__, "SSL:" 731 "Private key does not match the certificate public key, " 732 "reason: %s %s %s", ERR_error_string(ERR_get_error(), NULL), 733 pemfile->ptr, privkey->ptr); 734 EVP_PKEY_free(ssl_pemfile_pkey); 735 X509_free(ssl_pemfile_x509); 736 return NULL; 737 } 738 739 plugin_cert *pc = malloc(sizeof(plugin_cert)); 740 force_assert(pc); 741 pc->ssl_pemfile_pkey = ssl_pemfile_pkey; 742 pc->ssl_pemfile_x509 = ssl_pemfile_x509; 743 pc->ssl_pemfile = pemfile; 744 pc->ssl_privkey = privkey; 745 return pc; 746 } 747 748 749 #ifndef OPENSSL_NO_TLSEXT 750 751 #if OPENSSL_VERSION_NUMBER >= 0x10002000 752 753 static int 754 mod_openssl_acme_tls_1 (SSL *ssl, handler_ctx *hctx) 755 { 756 buffer * const b = hctx->tmp_buf; 757 const buffer * const name = &hctx->r->uri.authority; 758 log_error_st * const errh = hctx->r->conf.errh; 759 X509 *ssl_pemfile_x509 = NULL; 760 EVP_PKEY *ssl_pemfile_pkey = NULL; 761 size_t len; 762 int rc = SSL_TLSEXT_ERR_ALERT_FATAL; 763 764 /* check if acme-tls/1 protocol is enabled (path to dir of cert(s) is set)*/ 765 if (buffer_string_is_empty(hctx->conf.ssl_acme_tls_1)) 766 return SSL_TLSEXT_ERR_NOACK; /*(reuse value here for not-configured)*/ 767 buffer_copy_buffer(b, hctx->conf.ssl_acme_tls_1); 768 buffer_append_slash(b); 769 770 /* check if SNI set server name (required for acme-tls/1 protocol) 771 * and perform simple path checks for no '/' 772 * and no leading '.' (e.g. ignore "." or ".." or anything beginning '.') */ 773 if (buffer_string_is_empty(name)) return rc; 774 if (NULL != strchr(name->ptr, '/')) return rc; 775 if (name->ptr[0] == '.') return rc; 776 #if 0 777 if (0 != http_request_host_policy(name,hctx->r->conf.http_parseopts,443)) 778 return rc; 779 #endif 780 buffer_append_string_buffer(b, name); 781 len = buffer_string_length(b); 782 783 do { 784 buffer_append_string_len(b, CONST_STR_LEN(".crt.pem")); 785 ssl_pemfile_x509 = x509_load_pem_file(b->ptr, errh); 786 if (NULL == ssl_pemfile_x509) { 787 log_error(errh, __FILE__, __LINE__, 788 "SSL: Failed to load acme-tls/1 pemfile: %s", b->ptr); 789 break; 790 } 791 792 buffer_string_set_length(b, len); /*(remove ".crt.pem")*/ 793 buffer_append_string_len(b, CONST_STR_LEN(".key.pem")); 794 ssl_pemfile_pkey = evp_pkey_load_pem_file(b->ptr, errh); 795 if (NULL == ssl_pemfile_pkey) { 796 log_error(errh, __FILE__, __LINE__, 797 "SSL: Failed to load acme-tls/1 pemfile: %s", b->ptr); 798 break; 799 } 800 801 #if 0 /* redundant with below? */ 802 if (!X509_check_private_key(ssl_pemfile_x509, ssl_pemfile_pkey)) { 803 log_error(errh, __FILE__, __LINE__, 804 "SSL: Private key does not match acme-tls/1 " 805 "certificate public key, reason: %s %s" 806 ERR_error_string(ERR_get_error(), NULL), b->ptr); 807 break; 808 } 809 #endif 810 811 /* first set certificate! 812 * setting private key checks whether certificate matches it */ 813 if (1 != SSL_use_certificate(ssl, ssl_pemfile_x509)) { 814 log_error(errh, __FILE__, __LINE__, 815 "SSL: failed to set acme-tls/1 certificate for TLS server " 816 "name %s: %s", name->ptr, ERR_error_string(ERR_get_error(),NULL)); 817 break; 818 } 819 820 if (1 != SSL_use_PrivateKey(ssl, ssl_pemfile_pkey)) { 821 log_error(errh, __FILE__, __LINE__, 822 "SSL: failed to set acme-tls/1 private key for TLS server " 823 "name %s: %s", name->ptr, ERR_error_string(ERR_get_error(),NULL)); 824 break; 825 } 826 827 rc = SSL_TLSEXT_ERR_OK; 828 } while (0); 829 830 if (ssl_pemfile_pkey) EVP_PKEY_free(ssl_pemfile_pkey); 831 if (ssl_pemfile_x509) X509_free(ssl_pemfile_x509); 832 833 return rc; 834 } 835 836 enum { 837 MOD_OPENSSL_ALPN_HTTP11 = 1 838 ,MOD_OPENSSL_ALPN_HTTP10 = 2 839 ,MOD_OPENSSL_ALPN_H2 = 3 840 ,MOD_OPENSSL_ALPN_ACME_TLS_1 = 4 841 }; 842 843 /* https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids */ 844 static int 845 mod_openssl_alpn_select_cb (SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) 846 { 847 handler_ctx *hctx = (handler_ctx *) SSL_get_app_data(ssl); 848 unsigned short proto; 849 UNUSED(arg); 850 851 for (unsigned int i = 0, n; i < inlen; i += n) { 852 n = in[i++]; 853 if (i+n > inlen || 0 == n) break; 854 switch (n) { 855 #if 0 856 case 2: /* "h2" */ 857 if (in[i] == 'h' && in[i+1] == '2') { 858 proto = MOD_OPENSSL_ALPN_H2; 859 break; 860 } 861 continue; 862 #endif 863 case 8: /* "http/1.1" "http/1.0" */ 864 if (0 == memcmp(in+i, "http/1.", 7)) { 865 if (in[i+7] == '1') { 866 proto = MOD_OPENSSL_ALPN_HTTP11; 867 break; 868 } 869 if (in[i+7] == '0') { 870 proto = MOD_OPENSSL_ALPN_HTTP10; 871 break; 872 } 873 } 874 continue; 875 case 10: /* "acme-tls/1" */ 876 if (0 == memcmp(in+i, "acme-tls/1", 10)) { 877 int rc = mod_openssl_acme_tls_1(ssl, hctx); 878 if (rc == SSL_TLSEXT_ERR_OK) { 879 proto = MOD_OPENSSL_ALPN_ACME_TLS_1; 880 break; 881 } 882 /* (use SSL_TLSEXT_ERR_NOACK for not-configured) */ 883 if (rc == SSL_TLSEXT_ERR_NOACK) continue; 884 return rc; 885 } 886 continue; 887 default: 888 continue; 889 } 890 891 hctx->alpn = proto; 892 *out = in+i; 893 *outlen = n; 894 return SSL_TLSEXT_ERR_OK; 895 } 896 897 #if OPENSSL_VERSION_NUMBER < 0x10100000L 898 return SSL_TLSEXT_ERR_NOACK; 899 #else 900 return SSL_TLSEXT_ERR_ALERT_FATAL; 901 #endif 902 } 903 904 #endif /* OPENSSL_VERSION_NUMBER >= 0x10002000 */ 905 906 #endif /* OPENSSL_NO_TLSEXT */ 907 908 909 static int 910 network_openssl_ssl_conf_cmd (server *srv, plugin_config_socket *s) 911 { 912 #ifdef SSL_CONF_FLAG_CMDLINE 913 914 int rc = 0; 915 const data_string *ds; 916 SSL_CONF_CTX * const cctx = SSL_CONF_CTX_new(); 917 SSL_CONF_CTX_set_ssl_ctx(cctx, s->ssl_ctx); 918 SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE 919 | SSL_CONF_FLAG_SERVER 920 | SSL_CONF_FLAG_SHOW_ERRORS 921 | SSL_CONF_FLAG_CERTIFICATE); 922 923 /* always disable null and export ciphers */ 924 ds = (const data_string *) 925 array_get_element_klen(s->ssl_conf_cmd, 926 CONST_STR_LEN("CipherString")); 927 if (NULL != ds) { 928 buffer *cipher_string = 929 array_get_buf_ptr(s->ssl_conf_cmd, CONST_STR_LEN("CipherString")); 930 buffer_append_string_len(cipher_string, 931 CONST_STR_LEN(":!aNULL:!eNULL:!EXP")); 932 } 933 934 for (size_t i = 0; i < s->ssl_conf_cmd->used; ++i) { 935 ds = (data_string *)s->ssl_conf_cmd->data[i]; 936 ERR_clear_error(); 937 if (SSL_CONF_cmd(cctx, ds->key.ptr, ds->value.ptr) <= 0) { 938 log_error(srv->errh, __FILE__, __LINE__, 939 "SSL: SSL_CONF_cmd %s %s: %s", ds->key.ptr, ds->value.ptr, 940 ERR_error_string(ERR_get_error(), NULL)); 941 rc = -1; 942 break; 943 } 944 } 945 946 if (0 == rc && 1 != SSL_CONF_CTX_finish(cctx)) { 947 log_error(srv->errh, __FILE__, __LINE__, 948 "SSL: SSL_CONF_CTX_finish(): %s", 949 ERR_error_string(ERR_get_error(), NULL)); 950 rc = -1; 951 } 952 953 SSL_CONF_CTX_free(cctx); 954 return rc; 955 956 #else 957 958 UNUSED(s); 959 log_error(srv->errh, __FILE__, __LINE__, 960 "SSL: ssl.openssl.ssl-conf-cmd not available; ignored"); 961 return 0; 962 963 #endif 964 } 965 966 967 static int 968 network_init_ssl (server *srv, plugin_config_socket *s, plugin_data *p) 969 { 970 #ifndef OPENSSL_NO_DH 971 /* 1024-bit MODP Group with 160-bit prime order subgroup (RFC5114) 972 * -----BEGIN DH PARAMETERS----- 973 * MIIBDAKBgQCxC4+WoIDgHd6S3l6uXVTsUsmfvPsGo8aaap3KUtI7YWBz4oZ1oj0Y 974 * mDjvHi7mUsAT7LSuqQYRIySXXDzUm4O/rMvdfZDEvXCYSI6cIZpzck7/1vrlZEc4 975 * +qMaT/VbzMChUa9fDci0vUW/N982XBpl5oz9p21NpwjfH7K8LkpDcQKBgQCk0cvV 976 * w/00EmdlpELvuZkF+BBN0lisUH/WQGz/FCZtMSZv6h5cQVZLd35pD1UE8hMWAhe0 977 * sBuIal6RVH+eJ0n01/vX07mpLuGQnQ0iY/gKdqaiTAh6CR9THb8KAWm2oorWYqTR 978 * jnOvoy13nVkY0IvIhY9Nzvl8KiSFXm7rIrOy5QICAKA= 979 * -----END DH PARAMETERS----- 980 */ 981 982 static const unsigned char dh1024_p[]={ 983 0xB1,0x0B,0x8F,0x96,0xA0,0x80,0xE0,0x1D,0xDE,0x92,0xDE,0x5E, 984 0xAE,0x5D,0x54,0xEC,0x52,0xC9,0x9F,0xBC,0xFB,0x06,0xA3,0xC6, 985 0x9A,0x6A,0x9D,0xCA,0x52,0xD2,0x3B,0x61,0x60,0x73,0xE2,0x86, 986 0x75,0xA2,0x3D,0x18,0x98,0x38,0xEF,0x1E,0x2E,0xE6,0x52,0xC0, 987 0x13,0xEC,0xB4,0xAE,0xA9,0x06,0x11,0x23,0x24,0x97,0x5C,0x3C, 988 0xD4,0x9B,0x83,0xBF,0xAC,0xCB,0xDD,0x7D,0x90,0xC4,0xBD,0x70, 989 0x98,0x48,0x8E,0x9C,0x21,0x9A,0x73,0x72,0x4E,0xFF,0xD6,0xFA, 990 0xE5,0x64,0x47,0x38,0xFA,0xA3,0x1A,0x4F,0xF5,0x5B,0xCC,0xC0, 991 0xA1,0x51,0xAF,0x5F,0x0D,0xC8,0xB4,0xBD,0x45,0xBF,0x37,0xDF, 992 0x36,0x5C,0x1A,0x65,0xE6,0x8C,0xFD,0xA7,0x6D,0x4D,0xA7,0x08, 993 0xDF,0x1F,0xB2,0xBC,0x2E,0x4A,0x43,0x71, 994 }; 995 996 static const unsigned char dh1024_g[]={ 997 0xA4,0xD1,0xCB,0xD5,0xC3,0xFD,0x34,0x12,0x67,0x65,0xA4,0x42, 998 0xEF,0xB9,0x99,0x05,0xF8,0x10,0x4D,0xD2,0x58,0xAC,0x50,0x7F, 999 0xD6,0x40,0x6C,0xFF,0x14,0x26,0x6D,0x31,0x26,0x6F,0xEA,0x1E, 1000 0x5C,0x41,0x56,0x4B,0x77,0x7E,0x69,0x0F,0x55,0x04,0xF2,0x13, 1001 0x16,0x02,0x17,0xB4,0xB0,0x1B,0x88,0x6A,0x5E,0x91,0x54,0x7F, 1002 0x9E,0x27,0x49,0xF4,0xD7,0xFB,0xD7,0xD3,0xB9,0xA9,0x2E,0xE1, 1003 0x90,0x9D,0x0D,0x22,0x63,0xF8,0x0A,0x76,0xA6,0xA2,0x4C,0x08, 1004 0x7A,0x09,0x1F,0x53,0x1D,0xBF,0x0A,0x01,0x69,0xB6,0xA2,0x8A, 1005 0xD6,0x62,0xA4,0xD1,0x8E,0x73,0xAF,0xA3,0x2D,0x77,0x9D,0x59, 1006 0x18,0xD0,0x8B,0xC8,0x85,0x8F,0x4D,0xCE,0xF9,0x7C,0x2A,0x24, 1007 0x85,0x5E,0x6E,0xEB,0x22,0xB3,0xB2,0xE5, 1008 }; 1009 #endif 1010 1011 /* load SSL certificates */ 1012 1013 #ifndef SSL_OP_NO_COMPRESSION 1014 #define SSL_OP_NO_COMPRESSION 0 1015 #endif 1016 #ifndef SSL_MODE_RELEASE_BUFFERS /* OpenSSL >= 1.0.0 */ 1017 #define SSL_MODE_RELEASE_BUFFERS 0 1018 #endif 1019 long ssloptions = SSL_OP_ALL 1020 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 1021 | SSL_OP_NO_COMPRESSION; 1022 1023 #if OPENSSL_VERSION_NUMBER >= 0x10100000L 1024 s->ssl_ctx = (!s->ssl_use_sslv2 && !s->ssl_use_sslv3) 1025 ? SSL_CTX_new(TLS_server_method()) 1026 : SSL_CTX_new(SSLv23_server_method()); 1027 #else 1028 s->ssl_ctx = SSL_CTX_new(SSLv23_server_method()); 1029 #endif 1030 if (NULL == s->ssl_ctx) { 1031 log_error(srv->errh, __FILE__, __LINE__, 1032 "SSL: %s", ERR_error_string(ERR_get_error(), NULL)); 1033 return -1; 1034 } 1035 1036 /* completely useless identifier; 1037 * required for client cert verification to work with sessions */ 1038 if (0 == SSL_CTX_set_session_id_context( 1039 s->ssl_ctx,(const unsigned char*)CONST_STR_LEN("lighttpd"))){ 1040 log_error(srv->errh, __FILE__, __LINE__, 1041 "SSL: failed to set session context: %s", 1042 ERR_error_string(ERR_get_error(), NULL)); 1043 return -1; 1044 } 1045 1046 if (s->ssl_empty_fragments) { 1047 #ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS 1048 ssloptions &= ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; 1049 #else 1050 ssloptions &= ~0x00000800L; /* hardcode constant */ 1051 log_error(srv->errh, __FILE__, __LINE__, 1052 "WARNING: SSL: 'insert empty fragments' not supported by the " 1053 "openssl version used to compile lighttpd with"); 1054 #endif 1055 } 1056 1057 SSL_CTX_set_options(s->ssl_ctx, ssloptions); 1058 SSL_CTX_set_info_callback(s->ssl_ctx, ssl_info_callback); 1059 1060 #ifndef HAVE_WOLFSSL_SSL_H /*(wolfSSL does not support SSLv2)*/ 1061 if (!s->ssl_use_sslv2 && 0 != SSL_OP_NO_SSLv2) { 1062 /* disable SSLv2 */ 1063 if ((SSL_OP_NO_SSLv2 1064 & SSL_CTX_set_options(s->ssl_ctx, SSL_OP_NO_SSLv2)) 1065 != SSL_OP_NO_SSLv2) { 1066 log_error(srv->errh, __FILE__, __LINE__, 1067 "SSL: %s", ERR_error_string(ERR_get_error(), NULL)); 1068 return -1; 1069 } 1070 } 1071 #endif 1072 1073 if (!s->ssl_use_sslv3 && 0 != SSL_OP_NO_SSLv3) { 1074 /* disable SSLv3 */ 1075 if ((SSL_OP_NO_SSLv3 1076 & SSL_CTX_set_options(s->ssl_ctx, SSL_OP_NO_SSLv3)) 1077 != SSL_OP_NO_SSLv3) { 1078 log_error(srv->errh, __FILE__, __LINE__, 1079 "SSL: %s", ERR_error_string(ERR_get_error(), NULL)); 1080 return -1; 1081 } 1082 } 1083 1084 if (!buffer_string_is_empty(s->ssl_cipher_list)) { 1085 /* Disable support for low encryption ciphers */ 1086 if (SSL_CTX_set_cipher_list(s->ssl_ctx,s->ssl_cipher_list->ptr)!=1){ 1087 log_error(srv->errh, __FILE__, __LINE__, 1088 "SSL: %s", ERR_error_string(ERR_get_error(), NULL)); 1089 return -1; 1090 } 1091 1092 if (s->ssl_honor_cipher_order) { 1093 SSL_CTX_set_options(s->ssl_ctx,SSL_OP_CIPHER_SERVER_PREFERENCE); 1094 } 1095 } 1096 1097 #ifndef OPENSSL_NO_DH 1098 { 1099 DH *dh; 1100 /* Support for Diffie-Hellman key exchange */ 1101 if (!buffer_string_is_empty(s->ssl_dh_file)) { 1102 /* DH parameters from file */ 1103 BIO *bio; 1104 bio = BIO_new_file((char *) s->ssl_dh_file->ptr, "r"); 1105 if (bio == NULL) { 1106 log_error(srv->errh, __FILE__, __LINE__, 1107 "SSL: Unable to open file %s", s->ssl_dh_file->ptr); 1108 return -1; 1109 } 1110 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); 1111 BIO_free(bio); 1112 if (dh == NULL) { 1113 log_error(srv->errh, __FILE__, __LINE__, 1114 "SSL: PEM_read_bio_DHparams failed %s", s->ssl_dh_file->ptr); 1115 return -1; 1116 } 1117 } else { 1118 BIGNUM *dh_p, *dh_g; 1119 /* Default DH parameters from RFC5114 */ 1120 dh = DH_new(); 1121 if (dh == NULL) { 1122 log_error(srv->errh, __FILE__, __LINE__, 1123 "SSL: DH_new () failed"); 1124 return -1; 1125 } 1126 dh_p = BN_bin2bn(dh1024_p,sizeof(dh1024_p), NULL); 1127 dh_g = BN_bin2bn(dh1024_g,sizeof(dh1024_g), NULL); 1128 if ((dh_p == NULL) || (dh_g == NULL)) { 1129 DH_free(dh); 1130 log_error(srv->errh, __FILE__, __LINE__, 1131 "SSL: BN_bin2bn () failed"); 1132 return -1; 1133 } 1134 #if OPENSSL_VERSION_NUMBER < 0x10100000L \ 1135 || defined(LIBRESSL_VERSION_NUMBER) 1136 dh->p = dh_p; 1137 dh->g = dh_g; 1138 dh->length = 160; 1139 #else 1140 DH_set0_pqg(dh, dh_p, NULL, dh_g); 1141 DH_set_length(dh, 160); 1142 #endif 1143 } 1144 SSL_CTX_set_tmp_dh(s->ssl_ctx,dh); 1145 SSL_CTX_set_options(s->ssl_ctx,SSL_OP_SINGLE_DH_USE); 1146 DH_free(dh); 1147 } 1148 #else 1149 if (!buffer_string_is_empty(s->ssl_dh_file)) { 1150 log_error(srv->errh, __FILE__, __LINE__, 1151 "SSL: openssl compiled without DH support, " 1152 "can't load parameters from %s", s->ssl_dh_file->ptr); 1153 } 1154 #endif 1155 1156 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL 1157 #ifndef OPENSSL_NO_ECDH 1158 { 1159 int nid = 0; 1160 /* Support for Elliptic-Curve Diffie-Hellman key exchange */ 1161 if (!buffer_string_is_empty(s->ssl_ec_curve)) { 1162 /* OpenSSL only supports the "named curves" 1163 * from RFC 4492, section 5.1.1. */ 1164 nid = OBJ_sn2nid((char *) s->ssl_ec_curve->ptr); 1165 if (nid == 0) { 1166 log_error(srv->errh, __FILE__, __LINE__, 1167 "SSL: Unknown curve name %s", s->ssl_ec_curve->ptr); 1168 return -1; 1169 } 1170 } else { 1171 #if OPENSSL_VERSION_NUMBER < 0x10002000 1172 /* Default curve */ 1173 nid = OBJ_sn2nid("prime256v1"); 1174 #elif OPENSSL_VERSION_NUMBER < 0x10100000L \ 1175 || defined(LIBRESSL_VERSION_NUMBER) 1176 if (!SSL_CTX_set_ecdh_auto(s->ssl_ctx, 1)) { 1177 log_error(srv->errh, __FILE__, __LINE__, 1178 "SSL: SSL_CTX_set_ecdh_auto() failed"); 1179 } 1180 #endif 1181 } 1182 if (nid) { 1183 EC_KEY *ecdh; 1184 ecdh = EC_KEY_new_by_curve_name(nid); 1185 if (ecdh == NULL) { 1186 log_error(srv->errh, __FILE__, __LINE__, 1187 "SSL: Unable to create curve %s", s->ssl_ec_curve->ptr); 1188 return -1; 1189 } 1190 SSL_CTX_set_tmp_ecdh(s->ssl_ctx,ecdh); 1191 SSL_CTX_set_options(s->ssl_ctx,SSL_OP_SINGLE_ECDH_USE); 1192 EC_KEY_free(ecdh); 1193 } 1194 } 1195 #endif 1196 #endif 1197 1198 /* load all ssl.ca-files specified in the config into each SSL_CTX 1199 * to be prepared for SNI */ 1200 if (!mod_openssl_load_ca_files(s->ssl_ctx, p, srv)) 1201 return -1; 1202 1203 if (s->ssl_verifyclient) { 1204 STACK_OF(X509_NAME) * const cert_names = s->ssl_ca_dn_file 1205 ? s->ssl_ca_dn_file 1206 : s->ssl_ca_file; 1207 if (NULL == cert_names) { 1208 log_error(srv->errh, __FILE__, __LINE__, 1209 "SSL: You specified ssl.verifyclient.activate " 1210 "but no ssl.ca-file or ssl.ca-dn-file"); 1211 return -1; 1212 } 1213 SSL_CTX_set_client_CA_list(s->ssl_ctx, SSL_dup_CA_list(cert_names)); 1214 int mode = SSL_VERIFY_PEER; 1215 if (s->ssl_verifyclient_enforce) { 1216 mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; 1217 } 1218 SSL_CTX_set_verify(s->ssl_ctx, mode, verify_callback); 1219 SSL_CTX_set_verify_depth(s->ssl_ctx, s->ssl_verifyclient_depth + 1); 1220 if (!buffer_string_is_empty(s->ssl_ca_crl_file)) { 1221 X509_STORE *store = SSL_CTX_get_cert_store(s->ssl_ctx); 1222 if (1 != X509_STORE_load_locations(store, s->ssl_ca_crl_file->ptr, NULL)) { 1223 log_error(srv->errh, __FILE__, __LINE__, 1224 "SSL: %s %s", ERR_error_string(ERR_get_error(), NULL), 1225 s->ssl_ca_crl_file->ptr); 1226 return -1; 1227 } 1228 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL); 1229 } 1230 } 1231 1232 if (1 != SSL_CTX_use_certificate_chain_file(s->ssl_ctx, 1233 s->ssl_pemfile->ptr)) { 1234 log_error(srv->errh, __FILE__, __LINE__, 1235 "SSL: %s %s", ERR_error_string(ERR_get_error(), NULL), 1236 s->ssl_pemfile->ptr); 1237 return -1; 1238 } 1239 1240 if (1 != SSL_CTX_use_PrivateKey(s->ssl_ctx, s->ssl_pemfile_pkey)) { 1241 log_error(srv->errh, __FILE__, __LINE__, 1242 "SSL: %s %s %s", ERR_error_string(ERR_get_error(), NULL), 1243 s->ssl_pemfile->ptr, s->ssl_privkey->ptr); 1244 return -1; 1245 } 1246 1247 if (SSL_CTX_check_private_key(s->ssl_ctx) != 1) { 1248 log_error(srv->errh, __FILE__, __LINE__, 1249 "SSL: Private key does not match the certificate public key, " 1250 "reason: %s %s %s", ERR_error_string(ERR_get_error(), NULL), 1251 s->ssl_pemfile->ptr, s->ssl_privkey->ptr); 1252 return -1; 1253 } 1254 SSL_CTX_set_default_read_ahead(s->ssl_ctx, s->ssl_read_ahead); 1255 SSL_CTX_set_mode(s->ssl_ctx, SSL_CTX_get_mode(s->ssl_ctx) 1256 | SSL_MODE_ENABLE_PARTIAL_WRITE 1257 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER 1258 | SSL_MODE_RELEASE_BUFFERS); 1259 1260 #ifndef OPENSSL_NO_TLSEXT 1261 #ifdef SSL_CLIENT_HELLO_SUCCESS 1262 SSL_CTX_set_client_hello_cb(s->ssl_ctx,mod_openssl_client_hello_cb,srv); 1263 #else 1264 if (!SSL_CTX_set_tlsext_servername_callback( 1265 s->ssl_ctx, network_ssl_servername_callback) || 1266 !SSL_CTX_set_tlsext_servername_arg(s->ssl_ctx, srv)) { 1267 log_error(srv->errh, __FILE__, __LINE__, 1268 "SSL: failed to initialize TLS servername callback, " 1269 "openssl library does not support TLS servername extension"); 1270 return -1; 1271 } 1272 #endif 1273 1274 #if OPENSSL_VERSION_NUMBER >= 0x10002000 1275 SSL_CTX_set_alpn_select_cb(s->ssl_ctx,mod_openssl_alpn_select_cb,NULL); 1276 #endif 1277 #endif 1278 1279 if (s->ssl_conf_cmd && s->ssl_conf_cmd->used) { 1280 if (0 != network_openssl_ssl_conf_cmd(srv, s)) return -1; 1281 } 1282 1283 return 0; 1284 } 1285 1286 1287 static int 1288 mod_openssl_set_defaults_sockets(server *srv, plugin_data *p) 1289 { 1290 static const config_plugin_keys_t cpk[] = { 1291 { CONST_STR_LEN("ssl.engine"), 1292 T_CONFIG_BOOL, 1293 T_CONFIG_SCOPE_CONNECTION } 1294 ,{ CONST_STR_LEN("ssl.cipher-list"), 1295 T_CONFIG_STRING, 1296 T_CONFIG_SCOPE_CONNECTION } 1297 ,{ CONST_STR_LEN("ssl.honor-cipher-order"), 1298 T_CONFIG_BOOL, 1299 T_CONFIG_SCOPE_CONNECTION } 1300 ,{ CONST_STR_LEN("ssl.dh-file"), 1301 T_CONFIG_STRING, 1302 T_CONFIG_SCOPE_CONNECTION } 1303 ,{ CONST_STR_LEN("ssl.ec-curve"), 1304 T_CONFIG_STRING, 1305 T_CONFIG_SCOPE_CONNECTION } 1306 ,{ CONST_STR_LEN("ssl.openssl.ssl-conf-cmd"), 1307 T_CONFIG_ARRAY_KVSTRING, 1308 T_CONFIG_SCOPE_CONNECTION } 1309 ,{ CONST_STR_LEN("ssl.pemfile"), /* included to process global scope */ 1310 T_CONFIG_STRING, 1311 T_CONFIG_SCOPE_CONNECTION } 1312 ,{ CONST_STR_LEN("ssl.empty-fragments"), 1313 T_CONFIG_BOOL, 1314 T_CONFIG_SCOPE_CONNECTION } 1315 ,{ CONST_STR_LEN("ssl.use-sslv2"), 1316 T_CONFIG_BOOL, 1317 T_CONFIG_SCOPE_CONNECTION } 1318 ,{ CONST_STR_LEN("ssl.use-sslv3"), 1319 T_CONFIG_BOOL, 1320 T_CONFIG_SCOPE_CONNECTION } 1321 ,{ NULL, 0, 1322 T_CONFIG_UNSET, 1323 T_CONFIG_SCOPE_UNSET } 1324 }; 1325 static const buffer default_ssl_cipher_list = { CONST_STR_LEN("HIGH"), 0 }; 1326 1327 p->ssl_ctxs = calloc(srv->config_context->used, sizeof(plugin_ssl_ctx)); 1328 force_assert(p->ssl_ctxs); 1329 1330 int rc = HANDLER_GO_ON; 1331 plugin_data_base srvplug; 1332 memset(&srvplug, 0, sizeof(srvplug)); 1333 plugin_data_base * const ps = &srvplug; 1334 if (!config_plugin_values_init(srv, ps, cpk, "mod_openssl")) 1335 return HANDLER_ERROR; 1336 1337 plugin_config_socket defaults; 1338 memset(&defaults, 0, sizeof(defaults)); 1339 defaults.ssl_honor_cipher_order = 1; 1340 defaults.ssl_cipher_list = &default_ssl_cipher_list; 1341 1342 /* process and validate config directives for global and $SERVER["socket"] 1343 * (init i to 0 if global context; to 1 to skip empty global context) */ 1344 for (int i = !ps->cvlist[0].v.u2[1]; i < ps->nconfig; ++i) { 1345 config_cond_info cfginfo; 1346 config_get_config_cond_info(&cfginfo, (uint32_t)ps->cvlist[i].k_id); 1347 int is_socket_scope = (0 == i || cfginfo.comp == COMP_SERVER_SOCKET); 1348 int count_not_engine = 0; 1349 1350 plugin_config_socket conf; 1351 memcpy(&conf, &defaults, sizeof(conf)); 1352 1353 /*(preserve prior behavior; not inherited)*/ 1354 /*(forcing inheritance might break existing configs where SSL is enabled 1355 * by default in the global scope, but not $SERVER["socket"]=="*:80") */ 1356 conf.ssl_enabled = 0; 1357 1358 config_plugin_value_t *cpv = ps->cvlist + ps->cvlist[i].v.u2[0]; 1359 for (; -1 != cpv->k_id; ++cpv) { 1360 /* ignore ssl.pemfile (k_id=6); included to process global scope */ 1361 if (!is_socket_scope && cpv->k_id != 6) { 1362 log_error(srv->errh, __FILE__, __LINE__, 1363 "%s is valid only in global scope or " 1364 "$SERVER[\"socket\"] condition", cpk[cpv->k_id].k); 1365 continue; 1366 } 1367 ++count_not_engine; 1368 switch (cpv->k_id) { 1369 case 0: /* ssl.engine */ 1370 conf.ssl_enabled = (0 != cpv->v.u); 1371 --count_not_engine; 1372 break; 1373 case 1: /* ssl.cipher-list */ 1374 conf.ssl_cipher_list = cpv->v.b; 1375 break; 1376 case 2: /* ssl.honor-cipher-order */ 1377 conf.ssl_honor_cipher_order = (0 != cpv->v.u); 1378 break; 1379 case 3: /* ssl.dh-file */ 1380 conf.ssl_dh_file = cpv->v.b; 1381 break; 1382 case 4: /* ssl.ec-curve */ 1383 conf.ssl_ec_curve = cpv->v.b; 1384 break; 1385 case 5: /* ssl.openssl.ssl-conf-cmd */ 1386 *(const array **)&conf.ssl_conf_cmd = cpv->v.a; 1387 break; 1388 case 6: /* ssl.pemfile */ 1389 /* ignore here; included to process global scope when 1390 * ssl.pemfile is set, but ssl.engine is not "enable" */ 1391 break; 1392 case 7: /* ssl.empty-fragments */ 1393 conf.ssl_empty_fragments = (0 != cpv->v.u); 1394 break; 1395 case 8: /* ssl.use-sslv2 */ 1396 conf.ssl_use_sslv2 = (0 != cpv->v.u); 1397 break; 1398 case 9: /* ssl.use-sslv3 */ 1399 conf.ssl_use_sslv3 = (0 != cpv->v.u); 1400 break; 1401 default:/* should not happen */ 1402 break; 1403 } 1404 } 1405 if (HANDLER_GO_ON != rc) break; 1406 if (0 == i) memcpy(&defaults, &conf, sizeof(conf)); 1407 1408 if (0 != i && !conf.ssl_enabled) continue; 1409 1410 /* fill plugin_config_socket with global context then $SERVER["socket"] 1411 * only for directives directly in current $SERVER["socket"] condition*/ 1412 1413 /*conf.ssl_pemfile_pkey = p->defaults.ssl_pemfile_pkey;*/ 1414 /*conf.ssl_pemfile_x509 = p->defaults.ssl_pemfile_x509;*/ 1415 conf.ssl_ca_file = p->defaults.ssl_ca_file; 1416 conf.ssl_ca_dn_file = p->defaults.ssl_ca_dn_file; 1417 conf.ssl_ca_crl_file = p->defaults.ssl_ca_crl_file; 1418 conf.ssl_verifyclient = p->defaults.ssl_verifyclient; 1419 conf.ssl_verifyclient_enforce = p->defaults.ssl_verifyclient_enforce; 1420 conf.ssl_verifyclient_depth = p->defaults.ssl_verifyclient_depth; 1421 conf.ssl_read_ahead = p->defaults.ssl_read_ahead; 1422 1423 int sidx = ps->cvlist[i].k_id; 1424 for (int j = !p->cvlist[0].v.u2[1]; j < p->nconfig; ++j) { 1425 if (p->cvlist[j].k_id != sidx) continue; 1426 /*if (0 == sidx) break;*//*(repeat to get ssl_pemfile,ssl_privkey)*/ 1427 cpv = p->cvlist + p->cvlist[j].v.u2[0]; 1428 for (; -1 != cpv->k_id; ++cpv) { 1429 ++count_not_engine; 1430 switch (cpv->k_id) { 1431 case 0: /* ssl.pemfile */ 1432 if (cpv->vtype == T_CONFIG_LOCAL) { 1433 plugin_cert *pc = cpv->v.v; 1434 conf.ssl_pemfile_pkey = pc->ssl_pemfile_pkey; 1435 conf.ssl_pemfile_x509 = pc->ssl_pemfile_x509; 1436 conf.ssl_pemfile = pc->ssl_pemfile; 1437 conf.ssl_privkey = pc->ssl_privkey; 1438 } 1439 break; 1440 case 2: /* ssl.ca-file */ 1441 if (cpv->vtype == T_CONFIG_LOCAL) 1442 conf.ssl_ca_file = cpv->v.v; 1443 break; 1444 case 3: /* ssl.ca-dn-file */ 1445 if (cpv->vtype == T_CONFIG_LOCAL) 1446 conf.ssl_ca_dn_file = cpv->v.v; 1447 break; 1448 case 4: /* ssl.ca-crl-file */ 1449 conf.ssl_ca_crl_file = cpv->v.b; 1450 break; 1451 case 5: /* ssl.read-ahead */ 1452 conf.ssl_read_ahead = (0 != cpv->v.u); 1453 break; 1454 case 7: /* ssl.verifyclient.activate */ 1455 conf.ssl_verifyclient = (0 != cpv->v.u); 1456 break; 1457 case 8: /* ssl.verifyclient.enforce */ 1458 conf.ssl_verifyclient_enforce = (0 != cpv->v.u); 1459 break; 1460 case 9: /* ssl.verifyclient.depth */ 1461 conf.ssl_verifyclient_depth = (unsigned char)cpv->v.shrt; 1462 break; 1463 default: 1464 break; 1465 } 1466 } 1467 break; 1468 } 1469 1470 if (NULL == conf.ssl_pemfile_x509) { 1471 if (0 == i && !conf.ssl_enabled) continue; 1472 if (0 != i) { 1473 /* inherit ssl settings from global scope 1474 * (if only ssl.engine = "enable" and no other ssl.* settings) 1475 * (This is for convenience when defining both IPv4 and IPv6 1476 * and desiring to inherit the ssl config from global context 1477 * without having to duplicate the directives)*/ 1478 if (count_not_engine) { 1479 log_error(srv->errh, __FILE__, __LINE__, 1480 "ssl.pemfile has to be set in same $SERVER[\"socket\"] scope " 1481 "as other ssl.* directives, unless only ssl.engine is set, " 1482 "inheriting ssl.* from global scope"); 1483 rc = HANDLER_ERROR; 1484 continue; 1485 } 1486 plugin_ssl_ctx * const s = p->ssl_ctxs + sidx; 1487 *s = *p->ssl_ctxs;/*(copy struct of ssl_ctx from global scope)*/ 1488 continue; 1489 } 1490 /* PEM file is required */ 1491 log_error(srv->errh, __FILE__, __LINE__, 1492 "ssl.pemfile has to be set when ssl.engine = \"enable\""); 1493 rc = HANDLER_ERROR; 1494 continue; 1495 } 1496 1497 /* configure ssl_ctx for socket */ 1498 1499 /*conf.ssl_ctx = NULL;*//*(filled by network_init_ssl() even on error)*/ 1500 if (0 == network_init_ssl(srv, &conf, p)) { 1501 plugin_ssl_ctx * const s = p->ssl_ctxs + sidx; 1502 s->ssl_ctx = conf.ssl_ctx; 1503 /*(used to match and avoid work changing keys 1504 * during SNI when pkey has not changed)*/ 1505 s->ssl_pemfile_pkey = conf.ssl_pemfile_pkey; 1506 } 1507 else { 1508 SSL_CTX_free(conf.ssl_ctx); 1509 rc = HANDLER_ERROR; 1510 } 1511 } 1512 1513 free(srvplug.cvlist); 1514 return rc; 1515 } 1516 1517 1518 SETDEFAULTS_FUNC(mod_openssl_set_defaults) 1519 { 1520 static const config_plugin_keys_t cpk[] = { 1521 { CONST_STR_LEN("ssl.pemfile"), 1522 T_CONFIG_STRING, 1523 T_CONFIG_SCOPE_CONNECTION } 1524 ,{ CONST_STR_LEN("ssl.privkey"), 1525 T_CONFIG_STRING, 1526 T_CONFIG_SCOPE_CONNECTION } 1527 ,{ CONST_STR_LEN("ssl.ca-file"), 1528 T_CONFIG_STRING, 1529 T_CONFIG_SCOPE_CONNECTION } 1530 ,{ CONST_STR_LEN("ssl.ca-dn-file"), 1531 T_CONFIG_STRING, 1532 T_CONFIG_SCOPE_CONNECTION } 1533 ,{ CONST_STR_LEN("ssl.ca-crl-file"), 1534 T_CONFIG_STRING, 1535 T_CONFIG_SCOPE_CONNECTION } 1536 ,{ CONST_STR_LEN("ssl.read-ahead"), 1537 T_CONFIG_BOOL, 1538 T_CONFIG_SCOPE_CONNECTION } 1539 ,{ CONST_STR_LEN("ssl.disable-client-renegotiation"), 1540 T_CONFIG_BOOL, 1541 T_CONFIG_SCOPE_CONNECTION } 1542 ,{ CONST_STR_LEN("ssl.verifyclient.activate"), 1543 T_CONFIG_BOOL, 1544 T_CONFIG_SCOPE_CONNECTION } 1545 ,{ CONST_STR_LEN("ssl.verifyclient.enforce"), 1546 T_CONFIG_BOOL, 1547 T_CONFIG_SCOPE_CONNECTION } 1548 ,{ CONST_STR_LEN("ssl.verifyclient.depth"), 1549 T_CONFIG_SHORT, 1550 T_CONFIG_SCOPE_CONNECTION } 1551 ,{ CONST_STR_LEN("ssl.verifyclient.username"), 1552 T_CONFIG_STRING, 1553 T_CONFIG_SCOPE_CONNECTION } 1554 ,{ CONST_STR_LEN("ssl.verifyclient.exportcert"), 1555 T_CONFIG_BOOL, 1556 T_CONFIG_SCOPE_CONNECTION } 1557 ,{ CONST_STR_LEN("ssl.acme-tls-1"), 1558 T_CONFIG_STRING, 1559 T_CONFIG_SCOPE_CONNECTION } 1560 ,{ CONST_STR_LEN("debug.log-ssl-noise"), 1561 T_CONFIG_BOOL, 1562 T_CONFIG_SCOPE_CONNECTION } 1563 ,{ NULL, 0, 1564 T_CONFIG_UNSET, 1565 T_CONFIG_SCOPE_UNSET } 1566 }; 1567 1568 plugin_data * const p = p_d; 1569 p->srv = srv; 1570 p->cafiles = array_init(0); 1571 if (!config_plugin_values_init(srv, p, cpk, "mod_openssl")) 1572 return HANDLER_ERROR; 1573 1574 /* process and validate config directives 1575 * (init i to 0 if global context; to 1 to skip empty global context) */ 1576 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 1577 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 1578 config_plugin_value_t *pemfile = NULL; 1579 config_plugin_value_t *privkey = NULL; 1580 const buffer *ssl_ca_file = NULL; 1581 const buffer *ssl_ca_dn_file = NULL; 1582 for (; -1 != cpv->k_id; ++cpv) { 1583 switch (cpv->k_id) { 1584 case 0: /* ssl.pemfile */ 1585 if (!buffer_string_is_empty(cpv->v.b)) pemfile = cpv; 1586 break; 1587 case 1: /* ssl.privkey */ 1588 if (!buffer_string_is_empty(cpv->v.b)) privkey = cpv; 1589 break; 1590 case 2: /* ssl.ca-file */ 1591 case 3: /* ssl.ca-dn-file */ 1592 if (!mod_openssl_init_once_openssl(srv)) return HANDLER_ERROR; 1593 if (!buffer_string_is_empty(cpv->v.b)) { 1594 const char * const fn = cpv->v.b->ptr; 1595 if (cpv->k_id == 2) 1596 ssl_ca_file = cpv->v.b; 1597 else 1598 ssl_ca_dn_file = cpv->v.b; 1599 cpv->v.v = SSL_load_client_CA_file(fn); 1600 if (NULL != cpv->v.v) { 1601 cpv->vtype = T_CONFIG_LOCAL; 1602 } 1603 else { 1604 log_error(srv->errh, __FILE__, __LINE__, "SSL: %s %s", 1605 ERR_error_string(ERR_get_error(), NULL), fn); 1606 return HANDLER_ERROR; 1607 } 1608 } 1609 break; 1610 case 4: /* ssl.ca-crl-file */ 1611 case 5: /* ssl.read-ahead */ 1612 case 6: /* ssl.disable-client-renegotiation */ 1613 case 7: /* ssl.verifyclient.activate */ 1614 case 8: /* ssl.verifyclient.enforce */ 1615 break; 1616 case 9: /* ssl.verifyclient.depth */ 1617 if (cpv->v.shrt > 255) { 1618 log_error(srv->errh, __FILE__, __LINE__, 1619 "%s is absurdly large (%hu); limiting to 255", 1620 cpk[cpv->k_id].k, cpv->v.shrt); 1621 cpv->v.shrt = 255; 1622 } 1623 break; 1624 case 10:/* ssl.verifyclient.username */ 1625 case 11:/* ssl.verifyclient.exportcert */ 1626 case 12:/* ssl.acme-tls-1 */ 1627 case 13:/* debug.log-ssl-noise */ 1628 break; 1629 default:/* should not happen */ 1630 break; 1631 } 1632 } 1633 1634 /* load all ssl.ca-files into a single chain to be prepared for SNI */ 1635 /*(certificate load order might matter)*/ 1636 if (ssl_ca_dn_file) 1637 array_insert_value(p->cafiles, CONST_BUF_LEN(ssl_ca_dn_file)); 1638 if (ssl_ca_file) 1639 array_insert_value(p->cafiles, CONST_BUF_LEN(ssl_ca_file)); 1640 1641 if (pemfile) { 1642 #ifdef OPENSSL_NO_TLSEXT 1643 config_cond_info cfginfo; 1644 uint32_t j = (uint32_t)p->cvlist[i].k_id; 1645 config_get_config_cond_info(&cfginfo, j); 1646 if (j > 0 && (COMP_SERVER_SOCKET != cfginfo.comp 1647 || cfginfo.cond != CONFIG_COND_EQ)) { 1648 if (COMP_HTTP_HOST == cfginfo.comp) 1649 log_error(srv->errh, __FILE__, __LINE__, "SSL:" 1650 "can't use ssl.pemfile with $HTTP[\"host\"], " 1651 "as openssl version does not support TLS extensions"); 1652 else 1653 log_error(srv->errh, __FILE__, __LINE__, "SSL:" 1654 "ssl.pemfile only works in SSL socket binding context " 1655 "as openssl version does not support TLS extensions"); 1656 return HANDLER_ERROR; 1657 } 1658 #endif 1659 if (NULL == privkey) privkey = pemfile; 1660 pemfile->v.v = 1661 network_openssl_load_pemfile(srv, pemfile->v.b, privkey->v.b); 1662 if (pemfile->v.v) 1663 pemfile->vtype = T_CONFIG_LOCAL; 1664 else 1665 return HANDLER_ERROR; 1666 } 1667 } 1668 1669 p->defaults.ssl_verifyclient = 0; 1670 p->defaults.ssl_verifyclient_enforce = 1; 1671 p->defaults.ssl_verifyclient_depth = 9; 1672 p->defaults.ssl_verifyclient_export_cert = 0; 1673 p->defaults.ssl_disable_client_renegotiation = 1; 1674 p->defaults.ssl_read_ahead = 0; 1675 1676 /* initialize p->defaults from global config context */ 1677 if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 1678 const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 1679 if (-1 != cpv->k_id) 1680 mod_openssl_merge_config(&p->defaults, cpv); 1681 } 1682 1683 return mod_openssl_set_defaults_sockets(srv, p); 1684 } 1685 1686 1687 static int 1688 load_next_chunk (request_st * const r, chunkqueue * const cq, off_t max_bytes, 1689 const char ** const data, size_t * const data_len) 1690 { 1691 chunk *c = cq->first; 1692 1693 /* local_send_buffer is a static buffer of size (LOCAL_SEND_BUFSIZE) 1694 * 1695 * it has to stay at the same location all the time to satisfy the needs 1696 * of SSL_write to pass the SAME parameter in case of a _WANT_WRITE 1697 * 1698 * buffer is allocated once, is NOT realloced (note: not thread-safe) 1699 * 1700 * (Note: above restriction no longer true since SSL_CTX_set_mode() is 1701 * called with SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) 1702 * */ 1703 1704 force_assert(NULL != c); 1705 1706 switch (c->type) { 1707 case MEM_CHUNK: 1708 *data = NULL; 1709 *data_len = 0; 1710 do { 1711 size_t have; 1712 1713 force_assert(c->offset >= 0 1714 && c->offset <= (off_t)buffer_string_length(c->mem)); 1715 1716 have = buffer_string_length(c->mem) - c->offset; 1717 1718 /* copy small mem chunks into single large buffer before SSL_write() 1719 * to reduce number times write() called underneath SSL_write() and 1720 * potentially reduce number of packets generated if TCP_NODELAY */ 1721 if (*data_len) { 1722 size_t space = LOCAL_SEND_BUFSIZE - *data_len; 1723 if (have > space) 1724 have = space; 1725 if (have > (size_t)max_bytes - *data_len) 1726 have = (size_t)max_bytes - *data_len; 1727 if (*data != local_send_buffer) { 1728 memcpy(local_send_buffer, *data, *data_len); 1729 *data = local_send_buffer; 1730 } 1731 memcpy(local_send_buffer+*data_len,c->mem->ptr+c->offset,have); 1732 *data_len += have; 1733 continue; 1734 } 1735 1736 if ((off_t) have > max_bytes) have = max_bytes; 1737 1738 *data = c->mem->ptr + c->offset; 1739 *data_len = have; 1740 } while ((c = c->next) && c->type == MEM_CHUNK 1741 && *data_len < LOCAL_SEND_BUFSIZE 1742 && (off_t) *data_len < max_bytes); 1743 return 0; 1744 1745 case FILE_CHUNK: 1746 if (0 != chunkqueue_open_file_chunk(cq, r->conf.errh)) return -1; 1747 1748 { 1749 off_t offset, toSend; 1750 1751 force_assert(c->offset >= 0 && c->offset <= c->file.length); 1752 offset = c->file.start + c->offset; 1753 toSend = c->file.length - c->offset; 1754 1755 if (toSend > LOCAL_SEND_BUFSIZE) toSend = LOCAL_SEND_BUFSIZE; 1756 if (toSend > max_bytes) toSend = max_bytes; 1757 1758 if (-1 == lseek(c->file.fd, offset, SEEK_SET)) { 1759 log_perror(r->conf.errh, __FILE__, __LINE__, "lseek"); 1760 return -1; 1761 } 1762 if (-1 == (toSend = read(c->file.fd, local_send_buffer, toSend))) { 1763 log_perror(r->conf.errh, __FILE__, __LINE__, "read"); 1764 return -1; 1765 } 1766 1767 *data = local_send_buffer; 1768 *data_len = toSend; 1769 } 1770 return 0; 1771 } 1772 1773 return -1; 1774 } 1775 1776 1777 static int 1778 mod_openssl_close_notify(handler_ctx *hctx); 1779 1780 1781 static int 1782 connection_write_cq_ssl (connection *con, chunkqueue *cq, off_t max_bytes) 1783 { 1784 request_st * const r = &con->request; 1785 handler_ctx *hctx = r->plugin_ctx[plugin_data_singleton->id]; 1786 SSL *ssl = hctx->ssl; 1787 1788 if (0 != hctx->close_notify) return mod_openssl_close_notify(hctx); 1789 1790 chunkqueue_remove_finished_chunks(cq); 1791 1792 while (max_bytes > 0 && NULL != cq->first) { 1793 const char *data; 1794 size_t data_len; 1795 int wr; 1796 1797 if (0 != load_next_chunk(r, cq, max_bytes, &data, &data_len)) return -1; 1798 1799 /** 1800 * SSL_write man-page 1801 * 1802 * WARNING 1803 * When an SSL_write() operation has to be repeated because of 1804 * SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE, it must be 1805 * repeated with the same arguments. 1806 */ 1807 1808 ERR_clear_error(); 1809 wr = SSL_write(ssl, data, data_len); 1810 1811 if (hctx->renegotiations > 1 1812 && hctx->conf.ssl_disable_client_renegotiation) { 1813 log_error(r->conf.errh, __FILE__, __LINE__, 1814 "SSL: renegotiation initiated by client, killing connection"); 1815 return -1; 1816 } 1817 1818 if (wr <= 0) { 1819 int ssl_r; 1820 unsigned long err; 1821 1822 switch ((ssl_r = SSL_get_error(ssl, wr))) { 1823 case SSL_ERROR_WANT_READ: 1824 con->is_readable = -1; 1825 return 0; /* try again later */ 1826 case SSL_ERROR_WANT_WRITE: 1827 con->is_writable = -1; 1828 return 0; /* try again later */ 1829 case SSL_ERROR_SYSCALL: 1830 /* perhaps we have error waiting in our error-queue */ 1831 if (0 != (err = ERR_get_error())) { 1832 do { 1833 log_error(r->conf.errh, __FILE__, __LINE__, 1834 "SSL: %d %d %s",ssl_r,wr,ERR_error_string(err,NULL)); 1835 } while((err = ERR_get_error())); 1836 } else if (wr == -1) { 1837 /* no, but we have errno */ 1838 switch(errno) { 1839 case EPIPE: 1840 case ECONNRESET: 1841 return -2; 1842 default: 1843 log_perror(r->conf.errh, __FILE__, __LINE__, 1844 "SSL: %d %d", ssl_r, wr); 1845 break; 1846 } 1847 } else { 1848 /* neither error-queue nor errno ? */ 1849 log_perror(r->conf.errh, __FILE__, __LINE__, 1850 "SSL (error): %d %d", ssl_r, wr); 1851 } 1852 break; 1853 1854 case SSL_ERROR_ZERO_RETURN: 1855 /* clean shutdown on the remote side */ 1856 1857 if (wr == 0) return -2; 1858 1859 /* fall through */ 1860 default: 1861 while((err = ERR_get_error())) { 1862 log_error(r->conf.errh, __FILE__, __LINE__, 1863 "SSL: %d %d %s", ssl_r, wr, ERR_error_string(err, NULL)); 1864 } 1865 break; 1866 } 1867 return -1; 1868 } 1869 1870 chunkqueue_mark_written(cq, wr); 1871 max_bytes -= wr; 1872 1873 if ((size_t) wr < data_len) break; /* try again later */ 1874 } 1875 1876 return 0; 1877 } 1878 1879 1880 static int 1881 connection_read_cq_ssl (connection *con, chunkqueue *cq, off_t max_bytes) 1882 { 1883 request_st * const r = &con->request; 1884 handler_ctx *hctx = r->plugin_ctx[plugin_data_singleton->id]; 1885 int len; 1886 char *mem = NULL; 1887 size_t mem_len = 0; 1888 1889 UNUSED(max_bytes); 1890 1891 if (0 != hctx->close_notify) return mod_openssl_close_notify(hctx); 1892 1893 ERR_clear_error(); 1894 do { 1895 len = SSL_pending(hctx->ssl); 1896 mem_len = len < 2048 ? 2048 : (size_t)len; 1897 chunk * const ckpt = cq->last; 1898 mem = chunkqueue_get_memory(cq, &mem_len); 1899 1900 len = SSL_read(hctx->ssl, mem, mem_len); 1901 if (len > 0) { 1902 chunkqueue_use_memory(cq, ckpt, len); 1903 con->bytes_read += len; 1904 } else { 1905 chunkqueue_use_memory(cq, ckpt, 0); 1906 } 1907 1908 if (hctx->renegotiations > 1 1909 && hctx->conf.ssl_disable_client_renegotiation) { 1910 log_error(r->conf.errh, __FILE__, __LINE__, 1911 "SSL: renegotiation initiated by client, killing connection"); 1912 return -1; 1913 } 1914 1915 #if OPENSSL_VERSION_NUMBER >= 0x10002000 1916 if (hctx->alpn) { 1917 if (hctx->alpn == MOD_OPENSSL_ALPN_ACME_TLS_1) { 1918 chunkqueue_reset(cq); 1919 /* initiate handshake in order to send ServerHello. 1920 * Once TLS handshake is complete, return -1 to result in 1921 * CON_STATE_ERROR so that socket connection is quickly closed*/ 1922 if (1 == SSL_do_handshake(hctx->ssl)) return -1; 1923 len = -1; 1924 break; 1925 } 1926 hctx->alpn = 0; 1927 } 1928 #endif 1929 } while (len > 0 1930 && (hctx->conf.ssl_read_ahead || SSL_pending(hctx->ssl) > 0)); 1931 1932 if (len < 0) { 1933 int oerrno = errno; 1934 int rc, ssl_err; 1935 switch ((rc = SSL_get_error(hctx->ssl, len))) { 1936 case SSL_ERROR_WANT_WRITE: 1937 con->is_writable = -1; 1938 /* fall through */ 1939 case SSL_ERROR_WANT_READ: 1940 con->is_readable = 0; 1941 1942 /* the manual says we have to call SSL_read with the same arguments 1943 * next time. we ignore this restriction; no one has complained 1944 * about it in 1.5 yet, so it probably works anyway. 1945 */ 1946 1947 return 0; 1948 case SSL_ERROR_SYSCALL: 1949 /** 1950 * man SSL_get_error() 1951 * 1952 * SSL_ERROR_SYSCALL 1953 * Some I/O error occurred. The OpenSSL error queue may contain 1954 * more information on the error. If the error queue is empty 1955 * (i.e. ERR_get_error() returns 0), ret can be used to find out 1956 * more about the error: If ret == 0, an EOF was observed that 1957 * violates the protocol. If ret == -1, the underlying BIO 1958 * reported an I/O error (for socket I/O on Unix systems, consult 1959 * errno for details). 1960 * 1961 */ 1962 while((ssl_err = ERR_get_error())) { 1963 /* get all errors from the error-queue */ 1964 log_error(r->conf.errh, __FILE__, __LINE__, 1965 "SSL: %d %s", rc, ERR_error_string(ssl_err, NULL)); 1966 } 1967 1968 switch(oerrno) { 1969 default: 1970 /* (oerrno should be something like ECONNABORTED not 0 1971 * if client disconnected before anything was sent 1972 * (e.g. TCP connection probe), but it does not appear 1973 * that openssl provides such notification, not even 1974 * something like SSL_R_SSL_HANDSHAKE_FAILURE) */ 1975 if (0==oerrno && 0==cq->bytes_in && !hctx->conf.ssl_log_noise) 1976 break; 1977 1978 log_error(r->conf.errh, __FILE__, __LINE__, 1979 "SSL: %d %d %d %s", len, rc, oerrno, strerror(oerrno)); 1980 break; 1981 } 1982 1983 break; 1984 case SSL_ERROR_ZERO_RETURN: 1985 /* clean shutdown on the remote side */ 1986 1987 if (rc == 0) { 1988 /* FIXME: later */ 1989 } 1990 1991 /* fall through */ 1992 default: 1993 while((ssl_err = ERR_get_error())) { 1994 switch (ERR_GET_REASON(ssl_err)) { 1995 case SSL_R_SSL_HANDSHAKE_FAILURE: 1996 #ifdef SSL_R_TLSV1_ALERT_UNKNOWN_CA 1997 case SSL_R_TLSV1_ALERT_UNKNOWN_CA: 1998 #endif 1999 #ifdef SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN 2000 case SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN: 2001 #endif 2002 #ifdef SSL_R_SSLV3_ALERT_BAD_CERTIFICATE 2003 case SSL_R_SSLV3_ALERT_BAD_CERTIFICATE: 2004 #endif 2005 if (!hctx->conf.ssl_log_noise) continue; 2006 break; 2007 default: 2008 break; 2009 } 2010 /* get all errors from the error-queue */ 2011 log_error(r->conf.errh, __FILE__, __LINE__, 2012 "SSL: %d %s", rc, ERR_error_string(ssl_err, NULL)); 2013 } 2014 break; 2015 } 2016 return -1; 2017 } else if (len == 0) { 2018 con->is_readable = 0; 2019 /* the other end close the connection -> KEEP-ALIVE */ 2020 2021 return -2; 2022 } else { 2023 return 0; 2024 } 2025 } 2026 2027 2028 CONNECTION_FUNC(mod_openssl_handle_con_accept) 2029 { 2030 server_socket *srv_sock = con->srv_socket; 2031 if (!srv_sock->is_ssl) return HANDLER_GO_ON; 2032 2033 plugin_data *p = p_d; 2034 handler_ctx * const hctx = handler_ctx_init(); 2035 request_st * const r = &con->request; 2036 hctx->r = r; 2037 hctx->con = con; 2038 hctx->tmp_buf = con->srv->tmp_buf; 2039 r->plugin_ctx[p->id] = hctx; 2040 2041 plugin_ssl_ctx * const s = p->ssl_ctxs + srv_sock->sidx; 2042 hctx->conf.ssl_pemfile_pkey = s->ssl_pemfile_pkey; 2043 hctx->ssl = SSL_new(s->ssl_ctx); 2044 if (NULL != hctx->ssl 2045 && SSL_set_app_data(hctx->ssl, hctx) 2046 && SSL_set_fd(hctx->ssl, con->fd)) { 2047 SSL_set_accept_state(hctx->ssl); 2048 con->network_read = connection_read_cq_ssl; 2049 con->network_write = connection_write_cq_ssl; 2050 con->proto_default_port = 443; /* "https" */ 2051 mod_openssl_patch_config(r, &hctx->conf); 2052 return HANDLER_GO_ON; 2053 } 2054 else { 2055 log_error(r->conf.errh, __FILE__, __LINE__, 2056 "SSL: %s", ERR_error_string(ERR_get_error(), NULL)); 2057 return HANDLER_ERROR; 2058 } 2059 } 2060 2061 2062 static void 2063 mod_openssl_detach(handler_ctx *hctx) 2064 { 2065 /* step aside from futher SSL processing 2066 * (used after handle_connection_shut_wr hook) */ 2067 /* future: might restore prior network_read and network_write fn ptrs */ 2068 hctx->con->is_ssl_sock = 0; 2069 /* if called after handle_connection_shut_wr hook, shutdown SHUT_WR */ 2070 if (-1 == hctx->close_notify) shutdown(hctx->con->fd, SHUT_WR); 2071 hctx->close_notify = 1; 2072 } 2073 2074 2075 CONNECTION_FUNC(mod_openssl_handle_con_shut_wr) 2076 { 2077 request_st * const r = &con->request; 2078 plugin_data *p = p_d; 2079 handler_ctx *hctx = r->plugin_ctx[p->id]; 2080 if (NULL == hctx) return HANDLER_GO_ON; 2081 2082 hctx->close_notify = -2; 2083 if (SSL_is_init_finished(hctx->ssl)) { 2084 mod_openssl_close_notify(hctx); 2085 } 2086 else { 2087 mod_openssl_detach(hctx); 2088 } 2089 2090 return HANDLER_GO_ON; 2091 } 2092 2093 2094 static int 2095 mod_openssl_close_notify(handler_ctx *hctx) 2096 { 2097 int ret, ssl_r; 2098 unsigned long err; 2099 log_error_st *errh; 2100 2101 if (1 == hctx->close_notify) return -2; 2102 2103 ERR_clear_error(); 2104 switch ((ret = SSL_shutdown(hctx->ssl))) { 2105 case 1: 2106 mod_openssl_detach(hctx); 2107 return -2; 2108 case 0: 2109 /* Drain SSL read buffers in case pending records need processing. 2110 * Limit to reading next record to avoid denial of service when CPU 2111 * processing TLS is slower than arrival speed of TLS data packets. 2112 * (unless hctx->conf.ssl_read_ahead is set) 2113 * 2114 * references: 2115 * 2116 * "New session ticket breaks bidirectional shutdown of TLS 1.3 connection" 2117 * https://github.com/openssl/openssl/issues/6262 2118 * 2119 * The peer is still allowed to send data after receiving the 2120 * "close notify" event. If the peer did send data it need to be 2121 * processed by calling SSL_read() before calling SSL_shutdown() a 2122 * second time. SSL_read() will indicate the end of the peer data by 2123 * returning <= 0 and SSL_get_error() returning 2124 * SSL_ERROR_ZERO_RETURN. It is recommended to call SSL_read() 2125 * between SSL_shutdown() calls. 2126 * 2127 * Additional discussion in "Auto retry in shutdown" 2128 * https://github.com/openssl/openssl/pull/6340 2129 */ 2130 ssl_r = SSL_pending(hctx->ssl); 2131 if (ssl_r) { 2132 do { 2133 char buf[4096]; 2134 ret = SSL_read(hctx->ssl, buf, (int)sizeof(buf)); 2135 } while (ret > 0 && (hctx->conf.ssl_read_ahead||(ssl_r-=ret))); 2136 } 2137 2138 ERR_clear_error(); 2139 switch ((ret = SSL_shutdown(hctx->ssl))) { 2140 case 1: 2141 mod_openssl_detach(hctx); 2142 return -2; 2143 case 0: 2144 hctx->close_notify = -1; 2145 return 0; 2146 default: 2147 break; 2148 } 2149 2150 /* fall through */ 2151 default: 2152 2153 if (!SSL_is_init_finished(hctx->ssl)) { 2154 mod_openssl_detach(hctx); 2155 return -2; 2156 } 2157 2158 switch ((ssl_r = SSL_get_error(hctx->ssl, ret))) { 2159 case SSL_ERROR_ZERO_RETURN: 2160 case SSL_ERROR_WANT_WRITE: 2161 case SSL_ERROR_WANT_READ: 2162 hctx->close_notify = -1; 2163 return 0; /* try again later */ 2164 case SSL_ERROR_SYSCALL: 2165 /* perhaps we have error waiting in our error-queue */ 2166 errh = hctx->r->conf.errh; 2167 if (0 != (err = ERR_get_error())) { 2168 do { 2169 log_error(errh, __FILE__, __LINE__, 2170 "SSL: %d %d %s",ssl_r,ret,ERR_error_string(err,NULL)); 2171 } while((err = ERR_get_error())); 2172 } else if (errno != 0) { 2173 /*ssl bug (see lighttpd ticket #2213): sometimes errno==0*/ 2174 switch(errno) { 2175 case EPIPE: 2176 case ECONNRESET: 2177 break; 2178 default: 2179 log_perror(errh, __FILE__, __LINE__, 2180 "SSL (error): %d %d", ssl_r, ret); 2181 break; 2182 } 2183 } 2184 2185 break; 2186 default: 2187 errh = hctx->r->conf.errh; 2188 while((err = ERR_get_error())) { 2189 log_error(errh, __FILE__, __LINE__, 2190 "SSL: %d %d %s", ssl_r, ret, ERR_error_string(err, NULL)); 2191 } 2192 2193 break; 2194 } 2195 } 2196 ERR_clear_error(); 2197 hctx->close_notify = -1; 2198 return ret; 2199 } 2200 2201 2202 CONNECTION_FUNC(mod_openssl_handle_con_close) 2203 { 2204 request_st * const r = &con->request; 2205 plugin_data *p = p_d; 2206 handler_ctx *hctx = r->plugin_ctx[p->id]; 2207 if (NULL != hctx) { 2208 handler_ctx_free(hctx); 2209 r->plugin_ctx[p->id] = NULL; 2210 } 2211 2212 return HANDLER_GO_ON; 2213 } 2214 2215 2216 static void 2217 https_add_ssl_client_entries (request_st * const r, handler_ctx * const hctx) 2218 { 2219 buffer * const tb = r->tmp_buf; 2220 X509 *xs; 2221 X509_NAME *xn; 2222 int i, nentries; 2223 2224 long vr = SSL_get_verify_result(hctx->ssl); 2225 if (vr != X509_V_OK) { 2226 char errstr[256]; 2227 ERR_error_string_n(vr, errstr, sizeof(errstr)); 2228 buffer_copy_string_len(tb, CONST_STR_LEN("FAILED:")); 2229 buffer_append_string(tb, errstr); 2230 http_header_env_set(r, 2231 CONST_STR_LEN("SSL_CLIENT_VERIFY"), 2232 CONST_BUF_LEN(tb)); 2233 return; 2234 } else if (!(xs = SSL_get_peer_certificate(hctx->ssl))) { 2235 http_header_env_set(r, 2236 CONST_STR_LEN("SSL_CLIENT_VERIFY"), 2237 CONST_STR_LEN("NONE")); 2238 return; 2239 } else { 2240 http_header_env_set(r, 2241 CONST_STR_LEN("SSL_CLIENT_VERIFY"), 2242 CONST_STR_LEN("SUCCESS")); 2243 } 2244 2245 xn = X509_get_subject_name(xs); 2246 { 2247 char buf[256]; 2248 int len = safer_X509_NAME_oneline(xn, buf, sizeof(buf)); 2249 if (len > 0) { 2250 if (len >= (int)sizeof(buf)) len = (int)sizeof(buf)-1; 2251 http_header_env_set(r, 2252 CONST_STR_LEN("SSL_CLIENT_S_DN"), 2253 buf, (size_t)len); 2254 } 2255 } 2256 buffer_copy_string_len(tb, CONST_STR_LEN("SSL_CLIENT_S_DN_")); 2257 for (i = 0, nentries = X509_NAME_entry_count(xn); i < nentries; ++i) { 2258 int xobjnid; 2259 const char * xobjsn; 2260 X509_NAME_ENTRY *xe; 2261 2262 if (!(xe = X509_NAME_get_entry(xn, i))) { 2263 continue; 2264 } 2265 xobjnid = OBJ_obj2nid((ASN1_OBJECT*)X509_NAME_ENTRY_get_object(xe)); 2266 xobjsn = OBJ_nid2sn(xobjnid); 2267 if (xobjsn) { 2268 buffer_string_set_length(tb, sizeof("SSL_CLIENT_S_DN_")-1); 2269 buffer_append_string(tb, xobjsn); 2270 http_header_env_set(r, 2271 CONST_BUF_LEN(tb), 2272 (const char*)X509_NAME_ENTRY_get_data(xe)->data, 2273 X509_NAME_ENTRY_get_data(xe)->length); 2274 } 2275 } 2276 2277 { 2278 ASN1_INTEGER *xsn = X509_get_serialNumber(xs); 2279 BIGNUM *serialBN = ASN1_INTEGER_to_BN(xsn, NULL); 2280 char *serialHex = BN_bn2hex(serialBN); 2281 http_header_env_set(r, 2282 CONST_STR_LEN("SSL_CLIENT_M_SERIAL"), 2283 serialHex, strlen(serialHex)); 2284 OPENSSL_free(serialHex); 2285 BN_free(serialBN); 2286 } 2287 2288 if (!buffer_string_is_empty(hctx->conf.ssl_verifyclient_username)) { 2289 /* pick one of the exported values as "REMOTE_USER", for example 2290 * ssl.verifyclient.username = "SSL_CLIENT_S_DN_UID" 2291 * or 2292 * ssl.verifyclient.username = "SSL_CLIENT_S_DN_emailAddress" 2293 */ 2294 const buffer *varname = hctx->conf.ssl_verifyclient_username; 2295 const buffer *vb = http_header_env_get(r, CONST_BUF_LEN(varname)); 2296 if (vb) { /* same as http_auth.c:http_auth_setenv() */ 2297 http_header_env_set(r, 2298 CONST_STR_LEN("REMOTE_USER"), 2299 CONST_BUF_LEN(vb)); 2300 http_header_env_set(r, 2301 CONST_STR_LEN("AUTH_TYPE"), 2302 CONST_STR_LEN("SSL_CLIENT_VERIFY")); 2303 } 2304 } 2305 2306 if (hctx->conf.ssl_verifyclient_export_cert) { 2307 BIO *bio; 2308 if (NULL != (bio = BIO_new(BIO_s_mem()))) { 2309 PEM_write_bio_X509(bio, xs); 2310 const int n = BIO_pending(bio); 2311 2312 buffer_string_prepare_copy(tb, n); 2313 BIO_read(bio, tb->ptr, n); 2314 BIO_free(bio); 2315 buffer_commit(tb, n); 2316 http_header_env_set(r, 2317 CONST_STR_LEN("SSL_CLIENT_CERT"), 2318 CONST_BUF_LEN(tb)); 2319 } 2320 } 2321 X509_free(xs); 2322 } 2323 2324 2325 static void 2326 http_cgi_ssl_env (request_st * const r, handler_ctx * const hctx) 2327 { 2328 const char *s; 2329 const SSL_CIPHER *cipher; 2330 2331 s = SSL_get_version(hctx->ssl); 2332 http_header_env_set(r, CONST_STR_LEN("SSL_PROTOCOL"), s, strlen(s)); 2333 2334 if ((cipher = SSL_get_current_cipher(hctx->ssl))) { 2335 int usekeysize, algkeysize; 2336 char buf[LI_ITOSTRING_LENGTH]; 2337 s = SSL_CIPHER_get_name(cipher); 2338 http_header_env_set(r, CONST_STR_LEN("SSL_CIPHER"), s, strlen(s)); 2339 usekeysize = SSL_CIPHER_get_bits(cipher, &algkeysize); 2340 http_header_env_set(r, CONST_STR_LEN("SSL_CIPHER_USEKEYSIZE"), 2341 buf, li_itostrn(buf, sizeof(buf), usekeysize)); 2342 http_header_env_set(r, CONST_STR_LEN("SSL_CIPHER_ALGKEYSIZE"), 2343 buf, li_itostrn(buf, sizeof(buf), algkeysize)); 2344 } 2345 } 2346 2347 2348 REQUEST_FUNC(mod_openssl_handle_request_env) 2349 { 2350 plugin_data *p = p_d; 2351 handler_ctx *hctx = r->plugin_ctx[p->id]; 2352 if (NULL == hctx) return HANDLER_GO_ON; 2353 if (hctx->request_env_patched) return HANDLER_GO_ON; 2354 hctx->request_env_patched = 1; 2355 2356 http_cgi_ssl_env(r, hctx); 2357 if (hctx->conf.ssl_verifyclient) { 2358 https_add_ssl_client_entries(r, hctx); 2359 } 2360 2361 return HANDLER_GO_ON; 2362 } 2363 2364 2365 REQUEST_FUNC(mod_openssl_handle_uri_raw) 2366 { 2367 /* mod_openssl must be loaded prior to mod_auth 2368 * if mod_openssl is configured to set REMOTE_USER based on client cert */ 2369 /* mod_openssl must be loaded after mod_extforward 2370 * if mod_openssl config is based on lighttpd.conf remote IP conditional 2371 * using remote IP address set by mod_extforward, *unless* PROXY protocol 2372 * is enabled with extforward.hap-PROXY = "enable", in which case the 2373 * reverse is true: mod_extforward must be loaded after mod_openssl */ 2374 plugin_data *p = p_d; 2375 handler_ctx *hctx = r->plugin_ctx[p->id]; 2376 if (NULL == hctx) return HANDLER_GO_ON; 2377 2378 mod_openssl_patch_config(r, &hctx->conf); 2379 if (hctx->conf.ssl_verifyclient) { 2380 mod_openssl_handle_request_env(r, p); 2381 } 2382 2383 return HANDLER_GO_ON; 2384 } 2385 2386 2387 REQUEST_FUNC(mod_openssl_handle_request_reset) 2388 { 2389 plugin_data *p = p_d; 2390 handler_ctx *hctx = r->plugin_ctx[p->id]; 2391 if (NULL == hctx) return HANDLER_GO_ON; 2392 2393 hctx->request_env_patched = 0; 2394 return HANDLER_GO_ON; 2395 } 2396 2397 2398 int mod_openssl_plugin_init (plugin *p); 2399 int mod_openssl_plugin_init (plugin *p) 2400 { 2401 p->version = LIGHTTPD_VERSION_ID; 2402 p->name = "openssl"; 2403 p->init = mod_openssl_init; 2404 p->cleanup = mod_openssl_free; 2405 p->priv_defaults= mod_openssl_set_defaults; 2406 2407 p->handle_connection_accept = mod_openssl_handle_con_accept; 2408 p->handle_connection_shut_wr = mod_openssl_handle_con_shut_wr; 2409 p->handle_connection_close = mod_openssl_handle_con_close; 2410 p->handle_uri_raw = mod_openssl_handle_uri_raw; 2411 p->handle_request_env = mod_openssl_handle_request_env; 2412 p->connection_reset = mod_openssl_handle_request_reset; 2413 2414 return 0; 2415 } 2416