Revision 2913

View differences:

branches/lighttpd-1.4.x/src/base.h
320 320
	off_t *global_bytes_per_second_cnt_ptr; /*  */
321 321

  
322 322
#ifdef USE_OPENSSL
323
	SSL_CTX *ssl_ctx;
323
	SSL_CTX *ssl_ctx; /* not patched */
324
	/* SNI per host: with COMP_SERVER_SOCKET, COMP_HTTP_SCHEME, COMP_HTTP_HOST */
325
	EVP_PKEY *ssl_pemfile_pkey;
326
	X509 *ssl_pemfile_x509;
327
	STACK_OF(X509_NAME) *ssl_ca_file_cert_names;
324 328
#endif
325 329
} specific_config;
326 330

  
branches/lighttpd-1.4.x/src/server.c
314 314
			buffer_free(s->ssl_verifyclient_username);
315 315
#ifdef USE_OPENSSL
316 316
			SSL_CTX_free(s->ssl_ctx);
317
			EVP_PKEY_free(s->ssl_pemfile_pkey);
318
			X509_free(s->ssl_pemfile_x509);
319
			if (NULL != s->ssl_ca_file_cert_names) sk_X509_NAME_pop_free(s->ssl_ca_file_cert_names, X509_NAME_free);
317 320
#endif
318 321
			free(s);
319 322
		}
branches/lighttpd-1.4.x/src/configfile.c
339 339

  
340 340
	PATCH(ssl_pemfile);
341 341
#ifdef USE_OPENSSL
342
	PATCH(ssl_ctx);
342
	PATCH(ssl_pemfile_x509);
343
	PATCH(ssl_pemfile_pkey);
343 344
#endif
344 345
	PATCH(ssl_ca_file);
346
#ifdef USE_OPENSSL
347
	PATCH(ssl_ca_file_cert_names);
348
#endif
345 349
	PATCH(ssl_cipher_list);
346 350
	PATCH(ssl_dh_file);
347 351
	PATCH(ssl_ec_curve);
......
409 413
			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.pemfile"))) {
410 414
				PATCH(ssl_pemfile);
411 415
#ifdef USE_OPENSSL
412
				PATCH(ssl_ctx);
416
				PATCH(ssl_pemfile_x509);
417
				PATCH(ssl_pemfile_pkey);
413 418
#endif
414 419
			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ca-file"))) {
415 420
				PATCH(ssl_ca_file);
421
#ifdef USE_OPENSSL
422
				PATCH(ssl_ca_file_cert_names);
423
#endif
416 424
			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.honor-cipher-order"))) {
417 425
				PATCH(ssl_honor_cipher_order);
418 426
			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.empty-fragments"))) {
branches/lighttpd-1.4.x/src/network.c
112 112
	config_patch_connection(srv, con, COMP_HTTP_SCHEME);
113 113
	config_patch_connection(srv, con, COMP_HTTP_HOST);
114 114

  
115
	if (NULL == con->conf.ssl_ctx) {
116
		/* ssl_ctx <=> pemfile was set <=> ssl_ctx got patched: so this should never happen */
115
	if (NULL == con->conf.ssl_pemfile_x509 || NULL == con->conf.ssl_pemfile_pkey) {
116
		/* x509/pkey available <=> pemfile was set <=> pemfile got patched: so this should never happen, unless you nest $SERVER["socket"] */
117 117
		log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
118
			"null SSL_CTX for TLS server name", con->tlsext_server_name);
118
			"no certificate/private key for TLS server name", con->tlsext_server_name);
119 119
		return SSL_TLSEXT_ERR_ALERT_FATAL;
120 120
	}
121 121

  
122
	/* switch to new SSL_CTX in reaction to a client's server_name extension */
123
	if (con->conf.ssl_ctx != SSL_set_SSL_CTX(ssl, con->conf.ssl_ctx)) {
124
		log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
125
			"failed to set SSL_CTX for TLS server name", con->tlsext_server_name);
122
	/* first set certificate! setting private key checks whether certificate matches it */
123
	if (!SSL_use_certificate(ssl, con->conf.ssl_pemfile_x509)) {
124
		log_error_write(srv, __FILE__, __LINE__, "ssb:s", "SSL:",
125
			"failed to set certificate for TLS server name", con->tlsext_server_name,
126
			ERR_error_string(ERR_get_error(), NULL));
126 127
		return SSL_TLSEXT_ERR_ALERT_FATAL;
127 128
	}
128 129

  
130
	if (!SSL_use_PrivateKey(ssl, con->conf.ssl_pemfile_pkey)) {
131
		log_error_write(srv, __FILE__, __LINE__, "ssb:s", "SSL:",
132
			"failed to set private key for TLS server name", con->tlsext_server_name,
133
			ERR_error_string(ERR_get_error(), NULL));
134
		return SSL_TLSEXT_ERR_ALERT_FATAL;
135
	}
136

  
137
	if (con->conf.ssl_verifyclient) {
138
		if (NULL == con->conf.ssl_ca_file_cert_names) {
139
			log_error_write(srv, __FILE__, __LINE__, "ssb:s", "SSL:",
140
				"can't verify client without ssl.ca-file for TLS server name", con->tlsext_server_name,
141
				ERR_error_string(ERR_get_error(), NULL));
142
			return SSL_TLSEXT_ERR_ALERT_FATAL;
143
		}
144

  
145
		SSL_set_client_CA_list(ssl, SSL_dup_CA_list(con->conf.ssl_ca_file_cert_names));
146
		/* forcing verification here is really not that useful - a client could just connect without SNI */
147
		SSL_set_verify(
148
			ssl,
149
			SSL_VERIFY_PEER | (con->conf.ssl_verifyclient_enforce ? SSL_VERIFY_FAIL_IF_NO_PEER_CERT : 0),
150
			NULL
151
		);
152
		SSL_set_verify_depth(ssl, con->conf.ssl_verifyclient_depth);
153
	}
154

  
129 155
	return SSL_TLSEXT_ERR_OK;
130 156
}
131 157
#endif
......
491 517
	NETWORK_BACKEND_SOLARIS_SENDFILEV
492 518
} network_backend_t;
493 519

  
520
#ifdef USE_OPENSSL
521
static X509* x509_load_pem_file(server *srv, const char *file) {
522
	BIO *in;
523
	X509 *x = NULL;
524

  
525
	in = BIO_new(BIO_s_file());
526
	if (NULL == in) {
527
		log_error_write(srv, __FILE__, __LINE__, "S", "SSL: BIO_new(BIO_s_file()) failed");
528
		goto error;
529
	}
530

  
531
	if (BIO_read_filename(in,file) <= 0) {
532
		log_error_write(srv, __FILE__, __LINE__, "SSS", "SSL: BIO_read_filename('", file,"') failed");
533
		goto error;
534
	}
535
	x = PEM_read_bio_X509(in, NULL, NULL, NULL);
536

  
537
	if (NULL == x) {
538
		log_error_write(srv, __FILE__, __LINE__, "SSS", "SSL: couldn't read X509 certificate from '", file,"'");
539
		goto error;
540
	}
541

  
542
	BIO_free(in);
543
	return x;
544

  
545
error:
546
	if (NULL != x) X509_free(x);
547
	if (NULL != in) BIO_free(in);
548
	return NULL;
549
}
550

  
551
static EVP_PKEY* evp_pkey_load_pem_file(server *srv, const char *file) {
552
	BIO *in;
553
	EVP_PKEY *x = NULL;
554

  
555
	in=BIO_new(BIO_s_file());
556
	if (NULL == in) {
557
		log_error_write(srv, __FILE__, __LINE__, "s", "SSL: BIO_new(BIO_s_file()) failed");
558
		goto error;
559
	}
560

  
561
	if (BIO_read_filename(in,file) <= 0) {
562
		log_error_write(srv, __FILE__, __LINE__, "SSS", "SSL: BIO_read_filename('", file,"') failed");
563
		goto error;
564
	}
565
	x = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
566

  
567
	if (NULL == x) {
568
		log_error_write(srv, __FILE__, __LINE__, "SSS", "SSL: couldn't read private key from '", file,"'");
569
		goto error;
570
	}
571

  
572
	BIO_free(in);
573
	return x;
574

  
575
error:
576
	if (NULL != x) EVP_PKEY_free(x);
577
	if (NULL != in) BIO_free(in);
578
	return NULL;
579
}
580

  
581
static int network_openssl_load_pemfile(server *srv, size_t ndx) {
582
	specific_config *s = srv->config_storage[ndx];
583

  
584
#ifdef OPENSSL_NO_TLSEXT
585
	{
586
		data_config *dc = (data_config *)srv->config_context->data[i];
587
		if ((ndx > 0 && (COMP_SERVER_SOCKET != dc->comp || dc->cond != CONFIG_COND_EQ))
588
			|| !s->ssl_enabled) {
589
			log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
590
					"ssl.pemfile only works in SSL socket binding context as openssl version does not support TLS extensions");
591
			return -1;
592
		}
593
	}
594
#endif
595

  
596
	if (NULL == (s->ssl_pemfile_x509 = x509_load_pem_file(srv, s->ssl_pemfile->ptr))) return -1;
597
	if (NULL == (s->ssl_pemfile_pkey = evp_pkey_load_pem_file(srv, s->ssl_pemfile->ptr))) return -1;
598

  
599
	if (!X509_check_private_key(s->ssl_pemfile_x509, s->ssl_pemfile_pkey)) {
600
		log_error_write(srv, __FILE__, __LINE__, "sssb", "SSL:",
601
				"Private key does not match the certificate public key, reason:",
602
				ERR_error_string(ERR_get_error(), NULL),
603
				s->ssl_pemfile);
604
		return -1;
605
	}
606

  
607
	return 0;
608
}
609
#endif
610

  
494 611
int network_init(server *srv) {
495 612
	buffer *b;
496
	size_t i;
613
	size_t i, j;
497 614
	network_backend_t backend;
498 615

  
499 616
#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
......
580 697
		long ssloptions =
581 698
			SSL_OP_ALL | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | SSL_OP_NO_COMPRESSION;
582 699

  
583
		if (buffer_is_empty(s->ssl_pemfile)) continue;
700
		if (buffer_is_empty(s->ssl_pemfile) && buffer_is_empty(s->ssl_ca_file)) continue;
584 701

  
585
#ifdef OPENSSL_NO_TLSEXT
586
		{
587
			data_config *dc = (data_config *)srv->config_context->data[i];
588
			if (COMP_HTTP_HOST == dc->comp) {
589
			    log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
590
					    "can't use ssl.pemfile with $HTTP[\"host\"], openssl version does not support TLS extensions");
591
			    return -1;
592
			}
593
		}
594
#endif
595

  
596 702
		if (srv->ssl_is_init == 0) {
597 703
			SSL_load_error_strings();
598 704
			SSL_library_init();
......
606 712
			}
607 713
		}
608 714

  
715
		if (!buffer_is_empty(s->ssl_pemfile)) {
716
#ifdef OPENSSL_NO_TLSEXT
717
			data_config *dc = (data_config *)srv->config_context->data[i];
718
			if (COMP_HTTP_HOST == dc->comp) {
719
				log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
720
						"can't use ssl.pemfile with $HTTP[\"host\"], openssl version does not support TLS extensions");
721
				return -1;
722
			}
723
#endif
724
			if (network_openssl_load_pemfile(srv, i)) return -1;
725
		}
726

  
727

  
728
		if (!buffer_is_empty(s->ssl_ca_file)) {
729
			s->ssl_ca_file_cert_names = SSL_load_client_CA_file(s->ssl_ca_file->ptr);
730
			if (NULL == s->ssl_ca_file_cert_names) {
731
				log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
732
						ERR_error_string(ERR_get_error(), NULL), s->ssl_ca_file);
733
			}
734
		}
735

  
736
		if (buffer_is_empty(s->ssl_pemfile) || !s->ssl_enabled) continue;
737

  
609 738
		if (NULL == (s->ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) {
610 739
			log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
611 740
					ERR_error_string(ERR_get_error(), NULL));
......
721 850
#endif
722 851
#endif
723 852

  
724
		if (!buffer_is_empty(s->ssl_ca_file)) {
725
			if (1 != SSL_CTX_load_verify_locations(s->ssl_ctx, s->ssl_ca_file->ptr, NULL)) {
726
				log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
727
						ERR_error_string(ERR_get_error(), NULL), s->ssl_ca_file);
728
				return -1;
729
			}
730
			if (s->ssl_verifyclient) {
731
				STACK_OF(X509_NAME) *certs = SSL_load_client_CA_file(s->ssl_ca_file->ptr);
732
				if (!certs) {
853
		/* load all ssl.ca-files specified in the config into each SSL_CTX to be prepared for SNI */
854
		for (j = 0; j < srv->config_context->used; j++) {
855
			specific_config *s1 = srv->config_storage[j];
856

  
857
			if (!buffer_is_empty(s1->ssl_ca_file)) {
858
				if (1 != SSL_CTX_load_verify_locations(s->ssl_ctx, s1->ssl_ca_file->ptr, NULL)) {
733 859
					log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
734
							ERR_error_string(ERR_get_error(), NULL), s->ssl_ca_file);
735
				}
736
				if (SSL_CTX_set_session_id_context(s->ssl_ctx, (void*) &srv, sizeof(srv)) != 1) {
737
					log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
738
						ERR_error_string(ERR_get_error(), NULL));
860
							ERR_error_string(ERR_get_error(), NULL), s1->ssl_ca_file);
739 861
					return -1;
740 862
				}
741
				SSL_CTX_set_client_CA_list(s->ssl_ctx, certs);
742
				SSL_CTX_set_verify(
743
					s->ssl_ctx,
744
					SSL_VERIFY_PEER | (s->ssl_verifyclient_enforce ? SSL_VERIFY_FAIL_IF_NO_PEER_CERT : 0),
745
					NULL
863
			}
864
		}
865

  
866
		if (s->ssl_verifyclient) {
867
			if (NULL == s->ssl_ca_file_cert_names) {
868
				log_error_write(srv, __FILE__, __LINE__, "s",
869
					"SSL: You specified ssl.verifyclient.activate but no ca_file"
746 870
				);
747
				SSL_CTX_set_verify_depth(s->ssl_ctx, s->ssl_verifyclient_depth);
871
				return -1;
748 872
			}
749
		} else if (s->ssl_verifyclient) {
750
			log_error_write(
751
				srv, __FILE__, __LINE__, "s",
752
				"SSL: You specified ssl.verifyclient.activate but no ca_file"
873
			SSL_CTX_set_client_CA_list(s->ssl_ctx, SSL_dup_CA_list(s->ssl_ca_file_cert_names));
874
			SSL_CTX_set_verify(
875
				s->ssl_ctx,
876
				SSL_VERIFY_PEER | (s->ssl_verifyclient_enforce ? SSL_VERIFY_FAIL_IF_NO_PEER_CERT : 0),
877
				NULL
753 878
			);
879
			SSL_CTX_set_verify_depth(s->ssl_ctx, s->ssl_verifyclient_depth);
754 880
		}
755 881

  
756
		if (SSL_CTX_use_certificate_file(s->ssl_ctx, s->ssl_pemfile->ptr, SSL_FILETYPE_PEM) < 0) {
882
		if (SSL_CTX_use_certificate(s->ssl_ctx, s->ssl_pemfile_x509) < 0) {
757 883
			log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
758 884
					ERR_error_string(ERR_get_error(), NULL), s->ssl_pemfile);
759 885
			return -1;
760 886
		}
761 887

  
762
		if (SSL_CTX_use_PrivateKey_file (s->ssl_ctx, s->ssl_pemfile->ptr, SSL_FILETYPE_PEM) < 0) {
888
		if (SSL_CTX_use_PrivateKey(s->ssl_ctx, s->ssl_pemfile_pkey) < 0) {
763 889
			log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
764 890
					ERR_error_string(ERR_get_error(), NULL), s->ssl_pemfile);
765 891
			return -1;
......
856 982
	for (i = 1; i < srv->config_context->used; i++) {
857 983
		data_config *dc = (data_config *)srv->config_context->data[i];
858 984
		specific_config *s = srv->config_storage[i];
859
		size_t j;
860 985

  
861 986
		/* not our stage */
862 987
		if (COMP_SERVER_SOCKET != dc->comp) continue;
branches/lighttpd-1.4.x/NEWS
6 6
- 1.4.34
7 7
  * [mod_auth] explicitly link ssl for SHA1 (fixes #2517)
8 8
  * [mod_extforward] fix compilation without IPv6, (not) using undefined var (fixes #2515, thx mm)
9
  * [ssl] fix SNI handling; only use key+cert from SNI specific config (fixes #2525, CVE-2013-4508)
9 10

  
10 11
- 1.4.33 - 2013-09-27
11 12
  * mod_fastcgi: fix mix up of "mode" => "authorizer" in other fastcgi configs (fixes #2465, thx peex)

Also available in: Unified diff