2 ** _ __ ___ ___ __| | ___ ___| | mod_ssl
3 ** | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
4 ** | | | | | | (_) | (_| | \__ \__ \ | www.modssl.org
5 ** |_| |_| |_|\___/ \__,_|___|___/___/_| ftp.modssl.org
8 ** Initialization of Servers
11 /* ====================================================================
12 * The Apache Software License, Version 1.1
14 * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in
26 * the documentation and/or other materials provided with the
29 * 3. The end-user documentation included with the redistribution,
30 * if any, must include the following acknowledgment:
31 * "This product includes software developed by the
32 * Apache Software Foundation (http://www.apache.org/)."
33 * Alternately, this acknowledgment may appear in the software itself,
34 * if and wherever such third-party acknowledgments normally appear.
36 * 4. The names "Apache" and "Apache Software Foundation" must
37 * not be used to endorse or promote products derived from this
38 * software without prior written permission. For written
39 * permission, please contact apache@apache.org.
41 * 5. Products derived from this software may not be called "Apache",
42 * nor may "Apache" appear in their name, without prior written
43 * permission of the Apache Software Foundation.
45 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
49 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * ====================================================================
64 /* _________________________________________________________________
66 ** Module Initialization
67 ** _________________________________________________________________
70 static char *ssl_add_version_component(apr_pool_t *p,
74 char *val = ssl_var_lookup(p, s, NULL, NULL, name);
77 ap_add_version_component(p, val);
83 static char *version_components[] = {
84 "SSL_VERSION_PRODUCT",
85 "SSL_VERSION_INTERFACE",
86 "SSL_VERSION_LIBRARY",
90 static void ssl_add_version_components(apr_pool_t *p,
93 char *vals[sizeof(version_components)/sizeof(char *)];
96 for (i=0; version_components[i]; i++) {
97 vals[i] = ssl_add_version_component(p, s,
98 version_components[i]);
101 ssl_log(s, SSL_LOG_INFO,
102 "Server: %s, Interface: %s, Library: %s",
103 AP_SERVER_BASEVERSION,
104 vals[1], /* SSL_VERSION_INTERFACE */
105 vals[2]); /* SSL_VERSION_LIBRARY */
110 * Initialize SSL library
112 static void ssl_init_SSLLibrary(server_rec *s)
114 ssl_log(s, SSL_LOG_INFO,
115 "Init: Initializing %s library", SSL_LIBRARY_NAME);
117 CRYPTO_malloc_init();
118 SSL_load_error_strings();
123 * Handle the Temporary RSA Keys and DH Params
126 #define MODSSL_TMP_KEY_FREE(mc, type, idx) \
127 if (mc->pTmpKeys[idx]) { \
128 type##_free((type *)mc->pTmpKeys[idx]); \
129 mc->pTmpKeys[idx] = NULL; \
132 #define MODSSL_TMP_KEYS_FREE(mc, type) \
133 MODSSL_TMP_KEY_FREE(mc, type, SSL_TMP_KEY_##type##_512); \
134 MODSSL_TMP_KEY_FREE(mc, type, SSL_TMP_KEY_##type##_1024)
136 static void ssl_tmp_keys_free(server_rec *s)
138 SSLModConfigRec *mc = myModConfig(s);
140 MODSSL_TMP_KEYS_FREE(mc, RSA);
141 MODSSL_TMP_KEYS_FREE(mc, DH);
144 static void ssl_tmp_key_init_rsa(server_rec *s,
147 SSLModConfigRec *mc = myModConfig(s);
149 if (!(mc->pTmpKeys[idx] =
150 RSA_generate_key(bits, RSA_F4, NULL, NULL)))
152 ssl_log(s, SSL_LOG_ERROR,
153 "Init: Failed to generate temporary "
154 "%d bit RSA private key", bits);
160 static void ssl_tmp_key_init_dh(server_rec *s,
163 SSLModConfigRec *mc = myModConfig(s);
165 if (!(mc->pTmpKeys[idx] =
166 ssl_dh_GetTmpParam(bits)))
168 ssl_log(s, SSL_LOG_ERROR,
169 "Init: Failed to generate temporary "
170 "%d bit DH parameters", bits);
175 #define MODSSL_TMP_KEY_INIT_RSA(s, bits) \
176 ssl_tmp_key_init_rsa(s, bits, SSL_TMP_KEY_RSA_##bits)
178 #define MODSSL_TMP_KEY_INIT_DH(s, bits) \
179 ssl_tmp_key_init_dh(s, bits, SSL_TMP_KEY_DH_##bits)
181 static void ssl_tmp_keys_init(server_rec *s)
183 ssl_log(s, SSL_LOG_INFO,
184 "Init: Generating temporary RSA private keys (512/1024 bits)");
186 MODSSL_TMP_KEY_INIT_RSA(s, 512);
187 MODSSL_TMP_KEY_INIT_RSA(s, 1024);
189 ssl_log(s, SSL_LOG_INFO,
190 "Init: Generating temporary DH parameters (512/1024 bits)");
192 MODSSL_TMP_KEY_INIT_DH(s, 512);
193 MODSSL_TMP_KEY_INIT_DH(s, 1024);
197 * Per-module initialization
199 int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
201 server_rec *base_server)
203 SSLModConfigRec *mc = myModConfig(base_server);
208 * Let us cleanup on restarts and exists
210 apr_pool_cleanup_register(p, base_server,
212 apr_pool_cleanup_null);
215 * Any init round fixes the global config
217 ssl_config_global_create(base_server); /* just to avoid problems */
218 ssl_config_global_fix(mc);
221 * try to fix the configuration and open the dedicated SSL
222 * logfile as early as possible
224 for (s = base_server; s; s = s->next) {
227 /* Fix up stuff that may not have been set */
228 if (sc->bEnabled == UNSET) {
229 sc->bEnabled = FALSE;
232 if (sc->nSessionCacheTimeout == UNSET) {
233 sc->nSessionCacheTimeout = SSL_SESSION_CACHE_TIMEOUT;
236 if (sc->nPassPhraseDialogType == SSL_PPTYPE_UNSET) {
237 sc->nPassPhraseDialogType = SSL_PPTYPE_BUILTIN;
240 /* Open the dedicated SSL logfile */
241 ssl_log_open(base_server, s, p);
244 ssl_init_SSLLibrary(base_server);
247 ssl_util_thread_setup(base_server, p);
251 * Seed the Pseudo Random Number Generator (PRNG)
252 * only need ptemp here; nothing inside allocated from the pool
253 * needs to live once we return from ssl_rand_seed().
255 ssl_rand_seed(base_server, ptemp, SSL_RSCTX_STARTUP, "Init: ");
258 * read server private keys/public certs into memory.
259 * decrypting any encrypted keys via configured SSLPassPhraseDialogs
260 * anything that needs to live longer than ptemp needs to also survive
261 * restarts, in which case they'll live inside s->process->pool.
263 ssl_pphrase_Handle(base_server, ptemp);
265 ssl_tmp_keys_init(base_server);
268 * SSL external crypto device ("engine") support
270 #ifdef SSL_EXPERIMENTAL_ENGINE
271 ssl_init_Engine(base_server, p);
275 * Warn the user that he should use the session cache.
276 * But we can operate without it, of course.
278 if (mc->nSessionCacheMode == SSL_SCMODE_UNSET) {
279 ssl_log(base_server, SSL_LOG_WARN,
280 "Init: Session Cache is not configured "
281 "[hint: SSLSessionCache]");
282 mc->nSessionCacheMode = SSL_SCMODE_NONE;
286 * initialize the mutex handling
288 if (!ssl_mutex_init(base_server, p)) {
289 return HTTP_INTERNAL_SERVER_ERROR;
293 * initialize session caching
295 ssl_scache_init(base_server, p);
300 ssl_log(base_server, SSL_LOG_INFO,
301 "Init: Initializing (virtual) servers for SSL");
303 for (s = base_server; s; s = s->next) {
306 * Either now skip this server when SSL is disabled for
307 * it or give out some information about what we're
314 ssl_log(s, SSL_LOG_INFO,
315 "Init: Configuring server %s for SSL protocol",
316 ssl_util_vhostid(p, s));
319 * Read the server certificate and key
321 ssl_init_ConfigureServer(s, p, ptemp, sc);
325 * Configuration consistency checks
327 ssl_init_CheckServers(base_server, ptemp);
330 * Announce mod_ssl and SSL library in HTTP Server field
331 * as ``mod_ssl/X.X.X OpenSSL/X.X.X''
333 ssl_add_version_components(p, base_server);
335 SSL_init_app_data2_idx(); /* for SSL_get_app_data2() at request time */
341 * Support for external a Crypto Device ("engine"), usually
342 * a hardware accellerator card for crypto operations.
344 #ifdef SSL_EXPERIMENTAL_ENGINE
345 void ssl_init_Engine(server_rec *s, apr_pool_t *p)
347 SSLModConfigRec *mc = myModConfig(s);
350 if (mc->szCryptoDevice) {
351 if (!(e = ENGINE_by_id(mc->szCryptoDevice))) {
352 ssl_log(s, SSL_LOG_ERROR,
353 "Init: Failed to load Crypto Device API `%s'",
358 if (strEQ(mc->szCryptoDevice, "chil")) {
359 ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
362 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
363 ssl_log(s, SSL_LOG_ERROR,
364 "Init: Failed to enable Crypto Device API `%s'",
374 static SSL_CTX *ssl_init_ctx(server_rec *s,
380 const char *vhost_id = sc->szVHostID;
382 int protocol = sc->nProtocol;
385 * Create the new per-server SSL context
387 if (protocol == SSL_PROTOCOL_NONE) {
388 ssl_log(s, SSL_LOG_ERROR,
389 "Init: (%s) No SSL protocols available [hint: SSLProtocol]",
395 (protocol & SSL_PROTOCOL_SSLV2 ? "SSLv2, " : ""),
396 (protocol & SSL_PROTOCOL_SSLV3 ? "SSLv3, " : ""),
397 (protocol & SSL_PROTOCOL_TLSV1 ? "TLSv1, " : ""),
399 cp[strlen(cp)-2] = NUL;
401 ssl_log(s, SSL_LOG_TRACE,
402 "Init: (%s) Creating new SSL context (protocols: %s)",
405 if (protocol == SSL_PROTOCOL_SSLV2) {
406 ctx = SSL_CTX_new(SSLv2_server_method()); /* only SSLv2 is left */
409 ctx = SSL_CTX_new(SSLv23_server_method()); /* be more flexible */
414 SSL_CTX_set_options(ctx, SSL_OP_ALL);
416 if (!(protocol & SSL_PROTOCOL_SSLV2)) {
417 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
420 if (!(protocol & SSL_PROTOCOL_SSLV3)) {
421 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
424 if (!(protocol & SSL_PROTOCOL_TLSV1)) {
425 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
428 SSL_CTX_set_app_data(ctx, s);
431 * Configure additional context ingredients
433 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
438 static void ssl_init_verify(server_rec *s,
443 SSL_CTX *ctx = sc->pSSLCtx;
444 const char *vhost_id = sc->szVHostID;
446 int verify = SSL_VERIFY_NONE;
447 STACK_OF(X509_NAME) *ca_list;
449 if (sc->nVerifyClient == SSL_CVERIFY_UNSET) {
450 sc->nVerifyClient = SSL_CVERIFY_NONE;
453 if (sc->nVerifyDepth == UNSET) {
454 sc->nVerifyDepth = 1;
458 * Configure callbacks for SSL context
460 if (sc->nVerifyClient == SSL_CVERIFY_REQUIRE) {
461 verify |= SSL_VERIFY_PEER_STRICT;
464 if ((sc->nVerifyClient == SSL_CVERIFY_OPTIONAL) ||
465 (sc->nVerifyClient == SSL_CVERIFY_OPTIONAL_NO_CA))
467 verify |= SSL_VERIFY_PEER;
470 SSL_CTX_set_verify(ctx, verify, ssl_callback_SSLVerify);
473 * Configure Client Authentication details
475 if (sc->szCACertificateFile || sc->szCACertificatePath) {
476 ssl_log(s, SSL_LOG_TRACE,
477 "Init: (%s) Configuring client authentication", vhost_id);
479 if (!SSL_CTX_load_verify_locations(ctx,
480 sc->szCACertificateFile,
481 sc->szCACertificatePath))
483 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR,
484 "Init: (%s) Unable to configure verify locations "
485 "for client authentication", vhost_id);
489 ca_list = ssl_init_FindCAList(s, ptemp,
490 sc->szCACertificateFile,
491 sc->szCACertificatePath);
493 ssl_log(s, SSL_LOG_ERROR,
494 "Init: (%s) Unable to determine list of available "
495 "CA certificates for client authentication",
500 SSL_CTX_set_client_CA_list(ctx, (STACK *)ca_list);
504 * Give a warning when no CAs were configured but client authentication
505 * should take place. This cannot work.
507 if (sc->nVerifyClient == SSL_CVERIFY_REQUIRE) {
508 ca_list = (STACK_OF(X509_NAME) *)SSL_CTX_get_client_CA_list(ctx);
510 if (sk_X509_NAME_num(ca_list) == 0) {
511 ssl_log(s, SSL_LOG_WARN,
512 "Init: Ops, you want to request client authentication, "
513 "but no CAs are known for verification!? "
514 "[Hint: SSLCACertificate*]");
520 * Configure a particular server
522 void ssl_init_ConfigureServer(server_rec *s,
527 SSLModConfigRec *mc = myModConfig(s);
529 const char *vhost_id, *rsa_id, *dsa_id;
540 * Create the server host:port string because we need it a lot
542 sc->szVHostID = vhost_id = ssl_util_vhostid(p, s);
543 sc->nVHostID_length = strlen(sc->szVHostID);
546 * Now check for important parameters and the
547 * possibility that the user forgot to set them.
549 if (!sc->szPublicCertFiles[0]) {
550 ssl_log(s, SSL_LOG_ERROR,
551 "Init: (%s) No SSL Certificate set [hint: SSLCertificateFile]",
557 * Check for problematic re-initializations
559 if (sc->pPublicCert[SSL_AIDX_RSA] ||
560 sc->pPublicCert[SSL_AIDX_DSA])
562 ssl_log(s, SSL_LOG_ERROR,
563 "Init: (%s) Illegal attempt to re-initialise SSL for server "
564 "(theoretically shouldn't happen!)", vhost_id);
568 ctx = ssl_init_ctx(s, p, ptemp, sc);
570 if (mc->nSessionCacheMode == SSL_SCMODE_NONE) {
571 cache_mode = SSL_SESS_CACHE_OFF;
574 /* SSL_SESS_CACHE_NO_INTERNAL_LOOKUP will force OpenSSL
575 * to ignore process local-caching and
576 * to always get/set/delete sessions using mod_ssl's callbacks.
578 cache_mode = SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL_LOOKUP;
581 SSL_CTX_set_session_cache_mode(ctx, cache_mode);
583 ssl_init_verify(s, p, ptemp, sc);
585 SSL_CTX_sess_set_new_cb(ctx, ssl_callback_NewSessionCacheEntry);
586 SSL_CTX_sess_set_get_cb(ctx, ssl_callback_GetSessionCacheEntry);
587 SSL_CTX_sess_set_remove_cb(ctx, ssl_callback_DelSessionCacheEntry);
589 SSL_CTX_set_tmp_rsa_callback(ctx, ssl_callback_TmpRSA);
590 SSL_CTX_set_tmp_dh_callback(ctx, ssl_callback_TmpDH);
592 if (sc->nLogLevel >= SSL_LOG_INFO) {
593 /* this callback only logs if SSLLogLevel >= info */
594 SSL_CTX_set_info_callback(ctx, ssl_callback_LogTracingState);
598 * Configure SSL Cipher Suite
600 if (sc->szCipherSuite) {
601 ssl_log(s, SSL_LOG_TRACE,
602 "Init: (%s) Configuring permitted SSL ciphers [%s]",
603 vhost_id, sc->szCipherSuite);
605 if (!SSL_CTX_set_cipher_list(ctx, sc->szCipherSuite)) {
606 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR,
607 "Init: (%s) Unable to configure permitted SSL ciphers",
615 * Configure Certificate Revocation List (CRL) Details
617 if (sc->szCARevocationFile || sc->szCARevocationPath) {
618 ssl_log(s, SSL_LOG_TRACE,
619 "Init: (%s) Configuring certificate revocation facility",
622 sc->pRevocationStore =
623 SSL_X509_STORE_create((char *)sc->szCARevocationFile,
624 (char *)sc->szCARevocationPath);
626 if (!sc->pRevocationStore) {
627 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR,
628 "Init: (%s) Unable to configure X.509 CRL storage "
629 "for certificate revocation",
636 * Configure server certificate(s)
638 rsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_RSA);
639 dsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_DSA);
641 if ((asn1 = ssl_asn1_table_get(mc->tPublicCert, rsa_id))) {
642 ssl_log(s, SSL_LOG_TRACE,
643 "Init: (%s) Configuring RSA server certificate",
647 if (!(sc->pPublicCert[SSL_AIDX_RSA] =
648 d2i_X509(NULL, &ptr, asn1->nData)))
650 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR,
651 "Init: (%s) Unable to import RSA server certificate",
656 if (SSL_CTX_use_certificate(ctx, sc->pPublicCert[SSL_AIDX_RSA]) <= 0) {
657 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR,
658 "Init: (%s) Unable to configure RSA server certificate",
666 if ((asn1 = ssl_asn1_table_get(mc->tPublicCert, dsa_id))) {
667 ssl_log(s, SSL_LOG_TRACE,
668 "Init: (%s) Configuring DSA server certificate",
672 if (!(sc->pPublicCert[SSL_AIDX_DSA] =
673 d2i_X509(NULL, &ptr, asn1->nData)))
675 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR,
676 "Init: (%s) Unable to import DSA server certificate",
681 if (SSL_CTX_use_certificate(ctx, sc->pPublicCert[SSL_AIDX_DSA]) <= 0) {
682 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR,
683 "Init: (%s) Unable to configure DSA server certificate",
692 ssl_log(s, SSL_LOG_ERROR,
693 "Init: (%s) Ops, no RSA or DSA server certificate found?!",
695 ssl_log(s, SSL_LOG_ERROR,
696 "Init: (%s) You have to perform a *full* server restart "
697 "when you added or removed a certificate and/or key file",
703 * Some information about the certificate(s)
705 for (i = 0; i < SSL_AIDX_MAX; i++) {
706 if (sc->pPublicCert[i]) {
707 if (SSL_X509_isSGC(sc->pPublicCert[i])) {
708 ssl_log(s, SSL_LOG_INFO,
709 "Init: (%s) %s server certificate enables "
710 "Server Gated Cryptography (SGC)",
711 vhost_id, ssl_asn1_keystr(i));
714 if (SSL_X509_getBC(sc->pPublicCert[i], &is_ca, &pathlen)) {
716 ssl_log(s, SSL_LOG_WARN,
717 "Init: (%s) %s server certificate "
718 "is a CA certificate "
719 "(BasicConstraints: CA == TRUE !?)",
720 vhost_id, ssl_asn1_keystr(i));
724 ssl_log(s, SSL_LOG_WARN,
725 "Init: (%s) %s server certificate "
726 "is not a leaf certificate "
727 "(BasicConstraints: pathlen == %d > 0 !?)",
728 vhost_id, ssl_asn1_keystr(i), pathlen);
732 if (SSL_X509_getCN(p, sc->pPublicCert[i], &cp)) {
733 int fnm_flags = FNM_PERIOD|FNM_CASE_BLIND;
735 if (apr_is_fnmatch(cp) &&
736 (apr_fnmatch(cp, s->server_hostname,
737 fnm_flags) == FNM_NOMATCH))
739 ssl_log(s, SSL_LOG_WARN,
740 "Init: (%s) %s server certificate "
741 "wildcard CommonName (CN) `%s' "
742 "does NOT match server name!?",
743 vhost_id, ssl_asn1_keystr(i), cp);
745 else if (strNE(s->server_hostname, cp)) {
746 ssl_log(s, SSL_LOG_WARN,
747 "Init: (%s) %s server certificate "
748 "CommonName (CN) `%s' "
749 "does NOT match server name!?",
750 vhost_id, ssl_asn1_keystr(i), cp);
757 * Configure server private key(s)
761 if ((asn1 = ssl_asn1_table_get(mc->tPrivateKey, rsa_id))) {
762 ssl_log(s, SSL_LOG_TRACE,
763 "Init: (%s) Configuring RSA server private key",
767 if (!(sc->pPrivateKey[SSL_AIDX_RSA] =
768 d2i_PrivateKey(EVP_PKEY_RSA, NULL, &ptr, asn1->nData)))
770 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR,
771 "Init: (%s) Unable to import RSA server private key",
776 if (SSL_CTX_use_PrivateKey(ctx, sc->pPrivateKey[SSL_AIDX_RSA]) <= 0) {
777 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR,
778 "Init: (%s) Unable to configure RSA server private key",
786 if ((asn1 = ssl_asn1_table_get(mc->tPrivateKey, dsa_id))) {
787 ssl_log(s, SSL_LOG_TRACE,
788 "Init: (%s) Configuring DSA server private key",
792 if (!(sc->pPrivateKey[SSL_AIDX_DSA] =
793 d2i_PrivateKey(EVP_PKEY_DSA, NULL, &ptr, asn1->nData)))
795 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR,
796 "Init: (%s) Unable to import DSA server private key",
801 if (SSL_CTX_use_PrivateKey(ctx, sc->pPrivateKey[SSL_AIDX_DSA]) <= 0) {
802 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR,
803 "Init: (%s) Unable to configure DSA server private key",
812 ssl_log(s, SSL_LOG_ERROR,
813 "Init: (%s) Ops, no RSA or DSA server private key found?!",
819 * Optionally copy DSA parameters for certificate from private key
820 * (see http://www.psy.uq.edu.au/~ftp/Crypto/ssleay/TODO.html)
822 if (sc->pPublicCert[SSL_AIDX_DSA] &&
823 sc->pPrivateKey[SSL_AIDX_DSA])
825 pkey = X509_get_pubkey(sc->pPublicCert[SSL_AIDX_DSA]);
827 if (pkey && (EVP_PKEY_key_type(pkey) == EVP_PKEY_DSA) &&
828 EVP_PKEY_missing_parameters(pkey))
830 EVP_PKEY_copy_parameters(pkey,
831 sc->pPrivateKey[SSL_AIDX_DSA]);
836 * Optionally configure extra server certificate chain certificates.
837 * This is usually done by OpenSSL automatically when one of the
838 * server cert issuers are found under SSLCACertificatePath or in
839 * SSLCACertificateFile. But because these are intended for client
840 * authentication it can conflict. For instance when you use a
841 * Global ID server certificate you've to send out the intermediate
842 * CA certificate, too. When you would just configure this with
843 * SSLCACertificateFile and also use client authentication mod_ssl
844 * would accept all clients also issued by this CA. Obviously this
845 * isn't what we want in this situation. So this feature here exists
846 * to allow one to explicity configure CA certificates which are
847 * used only for the server certificate chain.
849 if (sc->szCertificateChain) {
850 BOOL skip_first = FALSE;
852 for (i = 0; (i < SSL_AIDX_MAX) && sc->szPublicCertFiles[i]; i++) {
853 if (strEQ(sc->szPublicCertFiles[i], sc->szCertificateChain)) {
859 n = SSL_CTX_use_certificate_chain(ctx,
860 (char *)sc->szCertificateChain,
863 ssl_log(s, SSL_LOG_ERROR,
864 "Init: (%s) Failed to configure CA certificate chain!",
869 ssl_log(s, SSL_LOG_TRACE,
870 "Init: (%s) Configuring server certificate chain "
871 "(%d CA certificate%s)",
872 vhost_id, n, n == 1 ? "" : "s");
876 void ssl_init_CheckServers(server_rec *base_server, apr_pool_t *p)
884 BOOL conflict = FALSE;
887 * Give out warnings when a server has HTTPS configured
888 * for the HTTP port or vice versa
890 for (s = base_server; s; s = s->next) {
893 if (sc->bEnabled && (s->port == DEFAULT_HTTP_PORT)) {
894 ssl_log(base_server, SSL_LOG_WARN,
895 "Init: (%s) You configured HTTPS(%d) "
896 "on the standard HTTP(%d) port!",
897 ssl_util_vhostid(p, s),
898 DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT);
901 if (!sc->bEnabled && (s->port == DEFAULT_HTTPS_PORT)) {
902 ssl_log(base_server, SSL_LOG_WARN,
903 "Init: (%s) You configured HTTP(%d) "
904 "on the standard HTTPS(%d) port!",
905 ssl_util_vhostid(p, s),
906 DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT);
911 * Give out warnings when more than one SSL-aware virtual server uses the
912 * same IP:port. This doesn't work because mod_ssl then will always use
913 * just the certificate/keys of one virtual host (which one cannot be said
914 * easily - but that doesn't matter here).
916 table = apr_hash_make(p);
918 for (s = base_server; s; s = s->next) {
925 key = apr_psprintf(p, "%pA:%u",
926 &s->addrs->host_addr, s->addrs->host_port);
929 if ((ps = (server_rec *)apr_hash_get(table, key, klen))) {
930 ssl_log(base_server, SSL_LOG_WARN,
931 "Init: SSL server IP/port conflict: "
932 "%s (%s:%d) vs. %s (%s:%d)",
933 ssl_util_vhostid(p, s),
934 (s->defn_name ? s->defn_name : "unknown"),
936 ssl_util_vhostid(p, ps),
937 (ps->defn_name ? ps->defn_name : "unknown"),
938 ps->defn_line_number);
943 apr_hash_set(table, key, klen, s);
947 ssl_log(base_server, SSL_LOG_WARN,
948 "Init: You should not use name-based "
949 "virtual hosts in conjunction with SSL!!");
953 static int ssl_init_FindCAList_X509NameCmp(X509_NAME **a, X509_NAME **b)
955 return(X509_NAME_cmp(*a, *b));
958 static void ssl_init_PushCAList(STACK_OF(X509_NAME) *ca_list,
959 server_rec *s, const char *file)
962 STACK_OF(X509_NAME) *sk;
964 sk = (STACK_OF(X509_NAME) *)SSL_load_client_CA_file(file);
970 for (n = 0; n < sk_X509_NAME_num(sk); n++) {
972 X509_NAME *name = sk_X509_NAME_value(sk, n);
974 ssl_log(s, SSL_LOG_TRACE,
975 "CA certificate: %s",
976 X509_NAME_oneline(name, name_buf, sizeof(name_buf)));
979 * note that SSL_load_client_CA_file() checks for duplicates,
980 * but since we call it multiple times when reading a directory
981 * we must also check for duplicates ourselves.
984 if (sk_X509_NAME_find(ca_list, name) < 0) {
985 /* this will be freed when ca_list is */
986 sk_X509_NAME_push(ca_list, name);
989 /* need to free this ourselves, else it will leak */
990 X509_NAME_free(name);
994 sk_X509_NAME_free(sk);
997 STACK_OF(X509_NAME) *ssl_init_FindCAList(server_rec *s,
1000 const char *ca_path)
1002 STACK_OF(X509_NAME) *ca_list;
1005 * Start with a empty stack/list where new
1006 * entries get added in sorted order.
1008 ca_list = sk_X509_NAME_new(ssl_init_FindCAList_X509NameCmp);
1011 * Process CA certificate bundle file
1014 ssl_init_PushCAList(ca_list, s, ca_file);
1018 * Process CA certificate path files
1022 apr_finfo_t direntry;
1023 apr_int32_t finfo_flags = APR_FINFO_MIN|APR_FINFO_NAME;
1025 if (apr_dir_open(&dir, ca_path, ptemp) != APR_SUCCESS) {
1026 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_ERRNO,
1027 "Init: Failed to open SSLCACertificatePath `%s'",
1032 while ((apr_dir_read(&direntry, finfo_flags, dir)) == APR_SUCCESS) {
1034 if (direntry.filetype == APR_DIR) {
1035 continue; /* don't try to load directories */
1037 file = apr_pstrcat(ptemp, ca_path, "/", direntry.name, NULL);
1038 ssl_init_PushCAList(ca_list, s, file);
1047 sk_X509_NAME_set_cmp_func(ca_list, NULL);
1052 void ssl_init_Child(apr_pool_t *p, server_rec *s)
1054 SSLModConfigRec *mc = myModConfig(s);
1055 mc->pid = getpid(); /* only call getpid() once per-process */
1057 /* XXX: there should be an ap_srand() function */
1058 srand((unsigned int)time(NULL));
1060 /* open the mutex lockfile */
1061 ssl_mutex_reinit(s, p);
1064 #define MODSSL_CFG_ITEM_FREE(func, item) \
1070 apr_status_t ssl_init_ModuleKill(void *data)
1072 SSLSrvConfigRec *sc;
1073 server_rec *base_server = (server_rec *)data;
1077 * Drop the session cache and mutex
1079 ssl_scache_kill(base_server);
1082 * Destroy the temporary keys and params
1084 ssl_tmp_keys_free(base_server);
1087 * Free the non-pool allocated structures
1088 * in the per-server configurations
1090 for (s = base_server; s; s = s->next) {
1092 sc = mySrvConfig(s);
1094 for (i=0; i < SSL_AIDX_MAX; i++) {
1095 MODSSL_CFG_ITEM_FREE(X509_free,
1096 sc->pPublicCert[i]);
1098 MODSSL_CFG_ITEM_FREE(EVP_PKEY_free,
1099 sc->pPrivateKey[i]);
1102 MODSSL_CFG_ITEM_FREE(X509_STORE_free,
1103 sc->pRevocationStore);
1105 MODSSL_CFG_ITEM_FREE(SSL_CTX_free,
1110 * Try to kill the internals of the SSL library.
1113 ERR_remove_state(0);