1 /* Copyright 2001-2005 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * _ __ ___ ___ __| | ___ ___| | mod_ssl
19 * | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
20 * | | | | | | (_) | (_| | \__ \__ \ |
21 * |_| |_| |_|\___/ \__,_|___|___/___/_|
24 * Initialization of Servers
29 #include "ssl_private.h"
31 /* _________________________________________________________________
33 ** Module Initialization
34 ** _________________________________________________________________
37 static char *ssl_add_version_component(apr_pool_t *p,
41 char *val = ssl_var_lookup(p, s, NULL, NULL, name);
44 ap_add_version_component(p, val);
50 static char *version_components[] = {
51 "SSL_VERSION_PRODUCT",
52 "SSL_VERSION_INTERFACE",
53 "SSL_VERSION_LIBRARY",
57 static void ssl_add_version_components(apr_pool_t *p,
60 char *vals[sizeof(version_components)/sizeof(char *)];
63 for (i=0; version_components[i]; i++) {
64 vals[i] = ssl_add_version_component(p, s,
65 version_components[i]);
68 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
69 "Server: %s, Interface: %s, Library: %s",
70 AP_SERVER_BASEVERSION,
71 vals[1], /* SSL_VERSION_INTERFACE */
72 vals[2]); /* SSL_VERSION_LIBRARY */
77 * Handle the Temporary RSA Keys and DH Params
80 #define MODSSL_TMP_KEY_FREE(mc, type, idx) \
81 if (mc->pTmpKeys[idx]) { \
82 type##_free((type *)mc->pTmpKeys[idx]); \
83 mc->pTmpKeys[idx] = NULL; \
86 #define MODSSL_TMP_KEYS_FREE(mc, type) \
87 MODSSL_TMP_KEY_FREE(mc, type, SSL_TMP_KEY_##type##_512); \
88 MODSSL_TMP_KEY_FREE(mc, type, SSL_TMP_KEY_##type##_1024)
90 static void ssl_tmp_keys_free(server_rec *s)
92 SSLModConfigRec *mc = myModConfig(s);
94 MODSSL_TMP_KEYS_FREE(mc, RSA);
95 MODSSL_TMP_KEYS_FREE(mc, DH);
98 static int ssl_tmp_key_init_rsa(server_rec *s,
101 SSLModConfigRec *mc = myModConfig(s);
103 if (!(mc->pTmpKeys[idx] =
104 RSA_generate_key(bits, RSA_F4, NULL, NULL)))
106 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
107 "Init: Failed to generate temporary "
108 "%d bit RSA private key", bits);
115 static int ssl_tmp_key_init_dh(server_rec *s,
118 SSLModConfigRec *mc = myModConfig(s);
120 if (!(mc->pTmpKeys[idx] =
121 ssl_dh_GetTmpParam(bits)))
123 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
124 "Init: Failed to generate temporary "
125 "%d bit DH parameters", bits);
132 #define MODSSL_TMP_KEY_INIT_RSA(s, bits) \
133 ssl_tmp_key_init_rsa(s, bits, SSL_TMP_KEY_RSA_##bits)
135 #define MODSSL_TMP_KEY_INIT_DH(s, bits) \
136 ssl_tmp_key_init_dh(s, bits, SSL_TMP_KEY_DH_##bits)
138 static int ssl_tmp_keys_init(server_rec *s)
140 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
141 "Init: Generating temporary RSA private keys (512/1024 bits)");
143 if (MODSSL_TMP_KEY_INIT_RSA(s, 512) ||
144 MODSSL_TMP_KEY_INIT_RSA(s, 1024)) {
148 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
149 "Init: Generating temporary DH parameters (512/1024 bits)");
151 if (MODSSL_TMP_KEY_INIT_DH(s, 512) ||
152 MODSSL_TMP_KEY_INIT_DH(s, 1024)) {
160 * Per-module initialization
162 int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
164 server_rec *base_server)
166 SSLModConfigRec *mc = myModConfig(base_server);
170 /* We initialize mc->pid per-process in the child init,
171 * but it should be initialized for startup before we
172 * call ssl_rand_seed() below.
177 * Let us cleanup on restarts and exists
179 apr_pool_cleanup_register(p, base_server,
181 apr_pool_cleanup_null);
184 * Any init round fixes the global config
186 ssl_config_global_create(base_server); /* just to avoid problems */
187 ssl_config_global_fix(mc);
190 * try to fix the configuration and open the dedicated SSL
191 * logfile as early as possible
193 for (s = base_server; s; s = s->next) {
205 * Create the server host:port string because we need it a lot
207 sc->vhost_id = ssl_util_vhostid(p, s);
208 sc->vhost_id_len = strlen(sc->vhost_id);
210 if (ap_get_server_protocol(s) &&
211 strcmp("https", ap_get_server_protocol(s)) == 0) {
212 sc->enabled = SSL_ENABLED_TRUE;
215 /* If sc->enabled is UNSET, then SSL is optional on this vhost */
216 /* Fix up stuff that may not have been set */
217 if (sc->enabled == SSL_ENABLED_UNSET) {
218 sc->enabled = SSL_ENABLED_FALSE;
220 if (sc->proxy_enabled == UNSET) {
221 sc->proxy_enabled = FALSE;
224 if (sc->session_cache_timeout == UNSET) {
225 sc->session_cache_timeout = SSL_SESSION_CACHE_TIMEOUT;
228 if (sc->server->pphrase_dialog_type == SSL_PPTYPE_UNSET) {
229 sc->server->pphrase_dialog_type = SSL_PPTYPE_BUILTIN;
235 * SSL external crypto device ("engine") support
237 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
238 ssl_init_Engine(base_server, p);
242 ssl_util_thread_setup(p);
245 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
246 "Init: Initialized %s library", SSL_LIBRARY_NAME);
249 * Seed the Pseudo Random Number Generator (PRNG)
250 * only need ptemp here; nothing inside allocated from the pool
251 * needs to live once we return from ssl_rand_seed().
253 ssl_rand_seed(base_server, ptemp, SSL_RSCTX_STARTUP, "Init: ");
256 * read server private keys/public certs into memory.
257 * decrypting any encrypted keys via configured SSLPassPhraseDialogs
258 * anything that needs to live longer than ptemp needs to also survive
259 * restarts, in which case they'll live inside s->process->pool.
261 ssl_pphrase_Handle(base_server, ptemp);
263 if (ssl_tmp_keys_init(base_server)) {
268 * initialize the mutex handling
270 if (!ssl_mutex_init(base_server, p)) {
271 return HTTP_INTERNAL_SERVER_ERROR;
275 * initialize session caching
277 ssl_scache_init(base_server, p);
282 ap_log_error(APLOG_MARK, APLOG_INFO, 0, base_server,
283 "Init: Initializing (virtual) servers for SSL");
285 for (s = base_server; s; s = s->next) {
288 * Either now skip this server when SSL is disabled for
289 * it or give out some information about what we're
294 * Read the server certificate and key
296 ssl_init_ConfigureServer(s, p, ptemp, sc);
300 * Configuration consistency checks
302 ssl_init_CheckServers(base_server, ptemp);
305 * Announce mod_ssl and SSL library in HTTP Server field
306 * as ``mod_ssl/X.X.X OpenSSL/X.X.X''
308 ssl_add_version_components(p, base_server);
310 SSL_init_app_data2_idx(); /* for SSL_get_app_data2() at request time */
316 * Support for external a Crypto Device ("engine"), usually
317 * a hardware accellerator card for crypto operations.
319 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
320 void ssl_init_Engine(server_rec *s, apr_pool_t *p)
322 SSLModConfigRec *mc = myModConfig(s);
325 if (mc->szCryptoDevice) {
326 if (!(e = ENGINE_by_id(mc->szCryptoDevice))) {
327 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
328 "Init: Failed to load Crypto Device API `%s'",
330 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
334 if (strEQ(mc->szCryptoDevice, "chil")) {
335 ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
338 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
339 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
340 "Init: Failed to enable Crypto Device API `%s'",
342 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
351 static void ssl_init_server_check(server_rec *s,
357 * check for important parameters and the
358 * possibility that the user forgot to set them.
360 if (!mctx->pks->cert_files[0]) {
361 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
362 "No SSL Certificate set [hint: SSLCertificateFile]");
367 * Check for problematic re-initializations
369 if (mctx->pks->certs[SSL_AIDX_RSA] ||
370 mctx->pks->certs[SSL_AIDX_DSA])
372 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
373 "Illegal attempt to re-initialise SSL for server "
374 "(theoretically shouldn't happen!)");
379 static void ssl_init_ctx_protocol(server_rec *s,
385 SSL_METHOD *method = NULL;
387 int protocol = mctx->protocol;
390 * Create the new per-server SSL context
392 if (protocol == SSL_PROTOCOL_NONE) {
393 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
394 "No SSL protocols available [hint: SSLProtocol]");
399 (protocol & SSL_PROTOCOL_SSLV2 ? "SSLv2, " : ""),
400 (protocol & SSL_PROTOCOL_SSLV3 ? "SSLv3, " : ""),
401 (protocol & SSL_PROTOCOL_TLSV1 ? "TLSv1, " : ""),
403 cp[strlen(cp)-2] = NUL;
405 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
406 "Creating new SSL context (protocols: %s)", cp);
408 if (protocol == SSL_PROTOCOL_SSLV2) {
410 SSLv2_client_method() : /* proxy */
411 SSLv2_server_method(); /* server */
413 else if (protocol == SSL_PROTOCOL_SSLV3) {
415 SSLv3_client_method() : /* proxy */
416 SSLv3_server_method(); /* server */
418 else if (protocol == SSL_PROTOCOL_TLSV1) {
420 TLSv1_client_method() : /* proxy */
421 TLSv1_server_method(); /* server */
423 else { /* For multiple protocols, we need a flexible method */
425 SSLv23_client_method() : /* proxy */
426 SSLv23_server_method(); /* server */
428 ctx = SSL_CTX_new(method);
432 SSL_CTX_set_options(ctx, SSL_OP_ALL);
434 if (!(protocol & SSL_PROTOCOL_SSLV2)) {
435 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
438 if (!(protocol & SSL_PROTOCOL_SSLV3)) {
439 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
442 if (!(protocol & SSL_PROTOCOL_TLSV1)) {
443 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
446 #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
448 SSLSrvConfigRec *sc = mySrvConfig(s);
449 if (sc->cipher_server_pref == TRUE) {
450 SSL_CTX_set_options(ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
455 SSL_CTX_set_app_data(ctx, s);
458 * Configure additional context ingredients
460 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
462 #ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
464 * Disallow a session from being resumed during a renegotiation,
465 * so that an acceptable cipher suite can be negotiated.
467 SSL_CTX_set_options(ctx, SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
471 static void ssl_init_ctx_session_cache(server_rec *s,
476 SSL_CTX *ctx = mctx->ssl_ctx;
477 SSLModConfigRec *mc = myModConfig(s);
478 long cache_mode = SSL_SESS_CACHE_OFF;
479 if (mc->nSessionCacheMode != SSL_SCMODE_NONE) {
480 /* SSL_SESS_CACHE_NO_INTERNAL will force OpenSSL
481 * to ignore process local-caching and
482 * to always get/set/delete sessions using mod_ssl's callbacks.
484 cache_mode = SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL;
487 SSL_CTX_set_session_cache_mode(ctx, cache_mode);
489 SSL_CTX_sess_set_new_cb(ctx, ssl_callback_NewSessionCacheEntry);
490 SSL_CTX_sess_set_get_cb(ctx, ssl_callback_GetSessionCacheEntry);
491 SSL_CTX_sess_set_remove_cb(ctx, ssl_callback_DelSessionCacheEntry);
494 static void ssl_init_ctx_callbacks(server_rec *s,
499 SSL_CTX *ctx = mctx->ssl_ctx;
501 SSL_CTX_set_tmp_rsa_callback(ctx, ssl_callback_TmpRSA);
502 SSL_CTX_set_tmp_dh_callback(ctx, ssl_callback_TmpDH);
504 if (s->loglevel >= APLOG_DEBUG) {
505 /* this callback only logs if LogLevel >= info */
506 SSL_CTX_set_info_callback(ctx, ssl_callback_LogTracingState);
510 static void ssl_init_ctx_verify(server_rec *s,
515 SSL_CTX *ctx = mctx->ssl_ctx;
517 int verify = SSL_VERIFY_NONE;
518 STACK_OF(X509_NAME) *ca_list;
520 if (mctx->auth.verify_mode == SSL_CVERIFY_UNSET) {
521 mctx->auth.verify_mode = SSL_CVERIFY_NONE;
524 if (mctx->auth.verify_depth == UNSET) {
525 mctx->auth.verify_depth = 1;
529 * Configure callbacks for SSL context
531 if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
532 verify |= SSL_VERIFY_PEER_STRICT;
535 if ((mctx->auth.verify_mode == SSL_CVERIFY_OPTIONAL) ||
536 (mctx->auth.verify_mode == SSL_CVERIFY_OPTIONAL_NO_CA))
538 verify |= SSL_VERIFY_PEER;
541 SSL_CTX_set_verify(ctx, verify, ssl_callback_SSLVerify);
544 * Configure Client Authentication details
546 if (mctx->auth.ca_cert_file || mctx->auth.ca_cert_path) {
547 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
548 "Configuring client authentication");
550 if (!SSL_CTX_load_verify_locations(ctx,
551 MODSSL_PCHAR_CAST mctx->auth.ca_cert_file,
552 MODSSL_PCHAR_CAST mctx->auth.ca_cert_path))
554 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
555 "Unable to configure verify locations "
556 "for client authentication");
557 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
561 if (mctx->pks && (mctx->pks->ca_name_file || mctx->pks->ca_name_path)) {
562 ca_list = ssl_init_FindCAList(s, ptemp,
563 mctx->pks->ca_name_file,
564 mctx->pks->ca_name_path);
566 ca_list = ssl_init_FindCAList(s, ptemp,
567 mctx->auth.ca_cert_file,
568 mctx->auth.ca_cert_path);
570 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
571 "Unable to determine list of acceptable "
572 "CA certificates for client authentication");
576 SSL_CTX_set_client_CA_list(ctx, (STACK *)ca_list);
580 * Give a warning when no CAs were configured but client authentication
581 * should take place. This cannot work.
583 if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
584 ca_list = (STACK_OF(X509_NAME) *)SSL_CTX_get_client_CA_list(ctx);
586 if (sk_X509_NAME_num(ca_list) == 0) {
587 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
588 "Init: Oops, you want to request client "
589 "authentication, but no CAs are known for "
590 "verification!? [Hint: SSLCACertificate*]");
595 static void ssl_init_ctx_cipher_suite(server_rec *s,
600 SSL_CTX *ctx = mctx->ssl_ctx;
601 const char *suite = mctx->auth.cipher_suite;
604 * Configure SSL Cipher Suite
610 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
611 "Configuring permitted SSL ciphers [%s]",
614 if (!SSL_CTX_set_cipher_list(ctx, MODSSL_PCHAR_CAST suite)) {
615 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
616 "Unable to configure permitted SSL ciphers");
617 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
622 static void ssl_init_ctx_crl(server_rec *s,
628 * Configure Certificate Revocation List (CRL) Details
631 if (!(mctx->crl_file || mctx->crl_path)) {
635 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
636 "Configuring certificate revocation facility");
639 SSL_X509_STORE_create((char *)mctx->crl_file,
640 (char *)mctx->crl_path);
643 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
644 "Unable to configure X.509 CRL storage "
645 "for certificate revocation");
646 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
651 static void ssl_init_ctx_cert_chain(server_rec *s,
656 BOOL skip_first = FALSE;
658 const char *chain = mctx->cert_chain;
661 * Optionally configure extra server certificate chain certificates.
662 * This is usually done by OpenSSL automatically when one of the
663 * server cert issuers are found under SSLCACertificatePath or in
664 * SSLCACertificateFile. But because these are intended for client
665 * authentication it can conflict. For instance when you use a
666 * Global ID server certificate you've to send out the intermediate
667 * CA certificate, too. When you would just configure this with
668 * SSLCACertificateFile and also use client authentication mod_ssl
669 * would accept all clients also issued by this CA. Obviously this
670 * isn't what we want in this situation. So this feature here exists
671 * to allow one to explicity configure CA certificates which are
672 * used only for the server certificate chain.
678 for (i = 0; (i < SSL_AIDX_MAX) && mctx->pks->cert_files[i]; i++) {
679 if (strEQ(mctx->pks->cert_files[i], chain)) {
685 n = SSL_CTX_use_certificate_chain(mctx->ssl_ctx,
689 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
690 "Failed to configure CA certificate chain!");
694 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
695 "Configuring server certificate chain "
696 "(%d CA certificate%s)",
697 n, n == 1 ? "" : "s");
700 static void ssl_init_ctx(server_rec *s,
705 ssl_init_ctx_protocol(s, p, ptemp, mctx);
707 ssl_init_ctx_session_cache(s, p, ptemp, mctx);
709 ssl_init_ctx_callbacks(s, p, ptemp, mctx);
711 ssl_init_ctx_verify(s, p, ptemp, mctx);
713 ssl_init_ctx_cipher_suite(s, p, ptemp, mctx);
715 ssl_init_ctx_crl(s, p, ptemp, mctx);
718 /* XXX: proxy support? */
719 ssl_init_ctx_cert_chain(s, p, ptemp, mctx);
723 static int ssl_server_import_cert(server_rec *s,
728 SSLModConfigRec *mc = myModConfig(s);
730 MODSSL_D2I_X509_CONST unsigned char *ptr;
731 const char *type = ssl_asn1_keystr(idx);
734 if (!(asn1 = ssl_asn1_table_get(mc->tPublicCert, id))) {
738 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
739 "Configuring %s server certificate", type);
742 if (!(cert = d2i_X509(NULL, &ptr, asn1->nData))) {
743 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
744 "Unable to import %s server certificate", type);
745 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
749 if (SSL_CTX_use_certificate(mctx->ssl_ctx, cert) <= 0) {
750 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
751 "Unable to configure %s server certificate", type);
752 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
756 mctx->pks->certs[idx] = cert;
761 static int ssl_server_import_key(server_rec *s,
766 SSLModConfigRec *mc = myModConfig(s);
768 MODSSL_D2I_PrivateKey_CONST unsigned char *ptr;
769 const char *type = ssl_asn1_keystr(idx);
770 int pkey_type = (idx == SSL_AIDX_RSA) ? EVP_PKEY_RSA : EVP_PKEY_DSA;
773 if (!(asn1 = ssl_asn1_table_get(mc->tPrivateKey, id))) {
777 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
778 "Configuring %s server private key", type);
781 if (!(pkey = d2i_PrivateKey(pkey_type, NULL, &ptr, asn1->nData)))
783 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
784 "Unable to import %s server private key", type);
785 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
789 if (SSL_CTX_use_PrivateKey(mctx->ssl_ctx, pkey) <= 0) {
790 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
791 "Unable to configure %s server private key", type);
792 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
797 * XXX: wonder if this is still needed, this is old todo doc.
798 * (see http://www.psy.uq.edu.au/~ftp/Crypto/ssleay/TODO.html)
800 if ((pkey_type == EVP_PKEY_DSA) && mctx->pks->certs[idx]) {
801 EVP_PKEY *pubkey = X509_get_pubkey(mctx->pks->certs[idx]);
803 if (pubkey && EVP_PKEY_missing_parameters(pubkey)) {
804 EVP_PKEY_copy_parameters(pubkey, pkey);
805 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
806 "Copying DSA parameters from private key to certificate");
807 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
808 EVP_PKEY_free(pubkey);
812 mctx->pks->keys[idx] = pkey;
817 static void ssl_check_public_cert(server_rec *s,
830 * Some information about the certificate(s)
833 if (SSL_X509_isSGC(cert)) {
834 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
835 "%s server certificate enables "
836 "Server Gated Cryptography (SGC)",
837 ssl_asn1_keystr(type));
840 if (SSL_X509_getBC(cert, &is_ca, &pathlen)) {
842 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
843 "%s server certificate is a CA certificate "
844 "(BasicConstraints: CA == TRUE !?)",
845 ssl_asn1_keystr(type));
849 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
850 "%s server certificate is not a leaf certificate "
851 "(BasicConstraints: pathlen == %d > 0 !?)",
852 ssl_asn1_keystr(type), pathlen);
856 if (SSL_X509_getCN(ptemp, cert, &cn)) {
857 int fnm_flags = APR_FNM_PERIOD|APR_FNM_CASE_BLIND;
859 if (apr_fnmatch_test(cn) &&
860 (apr_fnmatch(cn, s->server_hostname,
861 fnm_flags) == APR_FNM_NOMATCH))
863 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
864 "%s server certificate wildcard CommonName (CN) `%s' "
865 "does NOT match server name!?",
866 ssl_asn1_keystr(type), cn);
868 else if (strNE(s->server_hostname, cn)) {
869 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
870 "%s server certificate CommonName (CN) `%s' "
871 "does NOT match server name!?",
872 ssl_asn1_keystr(type), cn);
877 static void ssl_init_server_certs(server_rec *s,
882 const char *rsa_id, *dsa_id;
883 const char *vhost_id = mctx->sc->vhost_id;
885 int have_rsa, have_dsa;
887 rsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_RSA);
888 dsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_DSA);
890 have_rsa = ssl_server_import_cert(s, mctx, rsa_id, SSL_AIDX_RSA);
891 have_dsa = ssl_server_import_cert(s, mctx, dsa_id, SSL_AIDX_DSA);
893 if (!(have_rsa || have_dsa)) {
894 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
895 "Oops, no RSA or DSA server certificate found "
896 "for '%s:%d'?!", s->server_hostname, s->port);
900 for (i = 0; i < SSL_AIDX_MAX; i++) {
901 ssl_check_public_cert(s, ptemp, mctx->pks->certs[i], i);
904 have_rsa = ssl_server_import_key(s, mctx, rsa_id, SSL_AIDX_RSA);
905 have_dsa = ssl_server_import_key(s, mctx, dsa_id, SSL_AIDX_DSA);
907 if (!(have_rsa || have_dsa)) {
908 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
909 "Oops, no RSA or DSA server private key found?!");
914 static void ssl_init_proxy_certs(server_rec *s,
920 STACK_OF(X509_INFO) *sk;
921 modssl_pk_proxy_t *pkp = mctx->pkp;
923 SSL_CTX_set_client_cert_cb(mctx->ssl_ctx,
924 ssl_callback_proxy_cert);
926 if (!(pkp->cert_file || pkp->cert_path)) {
930 sk = sk_X509_INFO_new_null();
932 if (pkp->cert_file) {
933 SSL_X509_INFO_load_file(ptemp, sk, pkp->cert_file);
936 if (pkp->cert_path) {
937 SSL_X509_INFO_load_path(ptemp, sk, pkp->cert_path);
940 if ((ncerts = sk_X509_INFO_num(sk)) <= 0) {
941 sk_X509_INFO_free(sk);
942 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
943 "no client certs found for SSL proxy");
947 /* Check that all client certs have got certificates and private
949 for (n = 0; n < ncerts; n++) {
950 X509_INFO *inf = sk_X509_INFO_value(sk, n);
952 if (!inf->x509 || !inf->x_pkey) {
953 sk_X509_INFO_free(sk);
954 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s,
955 "incomplete client cert configured for SSL proxy "
956 "(missing or encrypted private key?)");
962 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
963 "loaded %d client certs for SSL proxy",
968 static void ssl_init_proxy_ctx(server_rec *s,
973 ssl_init_ctx(s, p, ptemp, sc->proxy);
975 ssl_init_proxy_certs(s, p, ptemp, sc->proxy);
978 static void ssl_init_server_ctx(server_rec *s,
983 ssl_init_server_check(s, p, ptemp, sc->server);
985 ssl_init_ctx(s, p, ptemp, sc->server);
987 ssl_init_server_certs(s, p, ptemp, sc->server);
991 * Configure a particular server
993 void ssl_init_ConfigureServer(server_rec *s,
998 /* Initialize the server if SSL is enabled or optional.
1000 if ((sc->enabled == SSL_ENABLED_TRUE) || (sc->enabled == SSL_ENABLED_OPTIONAL)) {
1001 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
1002 "Configuring server for SSL protocol");
1003 ssl_init_server_ctx(s, p, ptemp, sc);
1006 if (sc->proxy_enabled) {
1007 ssl_init_proxy_ctx(s, p, ptemp, sc);
1011 void ssl_init_CheckServers(server_rec *base_server, apr_pool_t *p)
1014 SSLSrvConfigRec *sc;
1019 BOOL conflict = FALSE;
1022 * Give out warnings when a server has HTTPS configured
1023 * for the HTTP port or vice versa
1025 for (s = base_server; s; s = s->next) {
1026 sc = mySrvConfig(s);
1028 if ((sc->enabled == SSL_ENABLED_TRUE) && (s->port == DEFAULT_HTTP_PORT)) {
1029 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1031 "Init: (%s) You configured HTTPS(%d) "
1032 "on the standard HTTP(%d) port!",
1033 ssl_util_vhostid(p, s),
1034 DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT);
1037 if ((sc->enabled == SSL_ENABLED_FALSE) && (s->port == DEFAULT_HTTPS_PORT)) {
1038 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1040 "Init: (%s) You configured HTTP(%d) "
1041 "on the standard HTTPS(%d) port!",
1042 ssl_util_vhostid(p, s),
1043 DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT);
1048 * Give out warnings when more than one SSL-aware virtual server uses the
1049 * same IP:port. This doesn't work because mod_ssl then will always use
1050 * just the certificate/keys of one virtual host (which one cannot be said
1051 * easily - but that doesn't matter here).
1053 table = apr_hash_make(p);
1055 for (s = base_server; s; s = s->next) {
1058 sc = mySrvConfig(s);
1060 if (!((sc->enabled == SSL_ENABLED_TRUE) && s->addrs)) {
1064 apr_sockaddr_ip_get(&addr, s->addrs->host_addr);
1065 key = apr_psprintf(p, "%s:%u", addr, s->addrs->host_port);
1068 if ((ps = (server_rec *)apr_hash_get(table, key, klen))) {
1069 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1071 "Init: SSL server IP/port conflict: "
1072 "%s (%s:%d) vs. %s (%s:%d)",
1073 ssl_util_vhostid(p, s),
1074 (s->defn_name ? s->defn_name : "unknown"),
1075 s->defn_line_number,
1076 ssl_util_vhostid(p, ps),
1077 (ps->defn_name ? ps->defn_name : "unknown"),
1078 ps->defn_line_number);
1083 apr_hash_set(table, key, klen, s);
1087 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, base_server,
1088 "Init: You should not use name-based "
1089 "virtual hosts in conjunction with SSL!!");
1093 #ifdef SSLC_VERSION_NUMBER
1094 static int ssl_init_FindCAList_X509NameCmp(char **a, char **b)
1096 return(X509_NAME_cmp((void*)*a, (void*)*b));
1099 static int ssl_init_FindCAList_X509NameCmp(X509_NAME **a, X509_NAME **b)
1101 return(X509_NAME_cmp(*a, *b));
1105 static void ssl_init_PushCAList(STACK_OF(X509_NAME) *ca_list,
1106 server_rec *s, const char *file)
1109 STACK_OF(X509_NAME) *sk;
1111 sk = (STACK_OF(X509_NAME) *)
1112 SSL_load_client_CA_file(MODSSL_PCHAR_CAST file);
1118 for (n = 0; n < sk_X509_NAME_num(sk); n++) {
1120 X509_NAME *name = sk_X509_NAME_value(sk, n);
1122 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1123 "CA certificate: %s",
1124 X509_NAME_oneline(name, name_buf, sizeof(name_buf)));
1127 * note that SSL_load_client_CA_file() checks for duplicates,
1128 * but since we call it multiple times when reading a directory
1129 * we must also check for duplicates ourselves.
1132 if (sk_X509_NAME_find(ca_list, name) < 0) {
1133 /* this will be freed when ca_list is */
1134 sk_X509_NAME_push(ca_list, name);
1137 /* need to free this ourselves, else it will leak */
1138 X509_NAME_free(name);
1142 sk_X509_NAME_free(sk);
1145 STACK_OF(X509_NAME) *ssl_init_FindCAList(server_rec *s,
1147 const char *ca_file,
1148 const char *ca_path)
1150 STACK_OF(X509_NAME) *ca_list;
1153 * Start with a empty stack/list where new
1154 * entries get added in sorted order.
1156 ca_list = sk_X509_NAME_new(ssl_init_FindCAList_X509NameCmp);
1159 * Process CA certificate bundle file
1162 ssl_init_PushCAList(ca_list, s, ca_file);
1166 * Process CA certificate path files
1170 apr_finfo_t direntry;
1171 apr_int32_t finfo_flags = APR_FINFO_TYPE|APR_FINFO_NAME;
1174 if ((rv = apr_dir_open(&dir, ca_path, ptemp)) != APR_SUCCESS) {
1175 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
1176 "Failed to open Certificate Path `%s'",
1181 while ((apr_dir_read(&direntry, finfo_flags, dir)) == APR_SUCCESS) {
1183 if (direntry.filetype == APR_DIR) {
1184 continue; /* don't try to load directories */
1186 file = apr_pstrcat(ptemp, ca_path, "/", direntry.name, NULL);
1187 ssl_init_PushCAList(ca_list, s, file);
1196 sk_X509_NAME_set_cmp_func(ca_list, NULL);
1201 void ssl_init_Child(apr_pool_t *p, server_rec *s)
1203 SSLModConfigRec *mc = myModConfig(s);
1204 mc->pid = getpid(); /* only call getpid() once per-process */
1206 /* XXX: there should be an ap_srand() function */
1207 srand((unsigned int)time(NULL));
1209 /* open the mutex lockfile */
1210 ssl_mutex_reinit(s, p);
1213 #define MODSSL_CFG_ITEM_FREE(func, item) \
1219 static void ssl_init_ctx_cleanup(modssl_ctx_t *mctx)
1221 MODSSL_CFG_ITEM_FREE(X509_STORE_free, mctx->crl);
1223 MODSSL_CFG_ITEM_FREE(SSL_CTX_free, mctx->ssl_ctx);
1226 static void ssl_init_ctx_cleanup_proxy(modssl_ctx_t *mctx)
1228 ssl_init_ctx_cleanup(mctx);
1230 if (mctx->pkp->certs) {
1231 sk_X509_INFO_pop_free(mctx->pkp->certs, X509_INFO_free);
1235 static void ssl_init_ctx_cleanup_server(modssl_ctx_t *mctx)
1239 ssl_init_ctx_cleanup(mctx);
1241 for (i=0; i < SSL_AIDX_MAX; i++) {
1242 MODSSL_CFG_ITEM_FREE(X509_free,
1243 mctx->pks->certs[i]);
1245 MODSSL_CFG_ITEM_FREE(EVP_PKEY_free,
1246 mctx->pks->keys[i]);
1250 apr_status_t ssl_init_ModuleKill(void *data)
1252 SSLSrvConfigRec *sc;
1253 server_rec *base_server = (server_rec *)data;
1257 * Drop the session cache and mutex
1259 ssl_scache_kill(base_server);
1262 * Destroy the temporary keys and params
1264 ssl_tmp_keys_free(base_server);
1267 * Free the non-pool allocated structures
1268 * in the per-server configurations
1270 for (s = base_server; s; s = s->next) {
1271 sc = mySrvConfig(s);
1273 ssl_init_ctx_cleanup_proxy(sc->proxy);
1275 ssl_init_ctx_cleanup_server(sc->server);