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) {
228 * Create the server host:port string because we need it a lot
230 sc->szVHostID = ssl_util_vhostid(p, s);
231 sc->nVHostID_length = strlen(sc->szVHostID);
233 /* Fix up stuff that may not have been set */
234 if (sc->bEnabled == UNSET) {
235 sc->bEnabled = FALSE;
238 if (sc->nSessionCacheTimeout == UNSET) {
239 sc->nSessionCacheTimeout = SSL_SESSION_CACHE_TIMEOUT;
242 if (sc->nPassPhraseDialogType == SSL_PPTYPE_UNSET) {
243 sc->nPassPhraseDialogType = SSL_PPTYPE_BUILTIN;
246 /* Open the dedicated SSL logfile */
247 ssl_log_open(base_server, s, p);
250 ssl_init_SSLLibrary(base_server);
253 ssl_util_thread_setup(base_server, p);
257 * Seed the Pseudo Random Number Generator (PRNG)
258 * only need ptemp here; nothing inside allocated from the pool
259 * needs to live once we return from ssl_rand_seed().
261 ssl_rand_seed(base_server, ptemp, SSL_RSCTX_STARTUP, "Init: ");
264 * read server private keys/public certs into memory.
265 * decrypting any encrypted keys via configured SSLPassPhraseDialogs
266 * anything that needs to live longer than ptemp needs to also survive
267 * restarts, in which case they'll live inside s->process->pool.
269 ssl_pphrase_Handle(base_server, ptemp);
271 ssl_tmp_keys_init(base_server);
274 * SSL external crypto device ("engine") support
276 #ifdef SSL_EXPERIMENTAL_ENGINE
277 ssl_init_Engine(base_server, p);
281 * initialize the mutex handling
283 if (!ssl_mutex_init(base_server, p)) {
284 return HTTP_INTERNAL_SERVER_ERROR;
288 * initialize session caching
290 ssl_scache_init(base_server, p);
295 ssl_log(base_server, SSL_LOG_INFO,
296 "Init: Initializing (virtual) servers for SSL");
298 for (s = base_server; s; s = s->next) {
301 * Either now skip this server when SSL is disabled for
302 * it or give out some information about what we're
309 ssl_log(s, SSL_LOG_INFO|SSL_INIT,
310 "Configuring server for SSL protocol");
313 * Read the server certificate and key
315 ssl_init_ConfigureServer(s, p, ptemp, sc);
319 * Configuration consistency checks
321 ssl_init_CheckServers(base_server, ptemp);
324 * Announce mod_ssl and SSL library in HTTP Server field
325 * as ``mod_ssl/X.X.X OpenSSL/X.X.X''
327 ssl_add_version_components(p, base_server);
329 SSL_init_app_data2_idx(); /* for SSL_get_app_data2() at request time */
335 * Support for external a Crypto Device ("engine"), usually
336 * a hardware accellerator card for crypto operations.
338 #ifdef SSL_EXPERIMENTAL_ENGINE
339 void ssl_init_Engine(server_rec *s, apr_pool_t *p)
341 SSLModConfigRec *mc = myModConfig(s);
344 if (mc->szCryptoDevice) {
345 if (!(e = ENGINE_by_id(mc->szCryptoDevice))) {
346 ssl_log(s, SSL_LOG_ERROR,
347 "Init: Failed to load Crypto Device API `%s'",
352 if (strEQ(mc->szCryptoDevice, "chil")) {
353 ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
356 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
357 ssl_log(s, SSL_LOG_ERROR,
358 "Init: Failed to enable Crypto Device API `%s'",
368 static void ssl_init_check_server(server_rec *s,
374 * check for important parameters and the
375 * possibility that the user forgot to set them.
377 if (!sc->szPublicCertFiles[0]) {
378 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
379 "No SSL Certificate set [hint: SSLCertificateFile]");
384 * Check for problematic re-initializations
386 if (sc->pPublicCert[SSL_AIDX_RSA] ||
387 sc->pPublicCert[SSL_AIDX_DSA])
389 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
390 "Illegal attempt to re-initialise SSL for server "
391 "(theoretically shouldn't happen!)");
396 static SSL_CTX *ssl_init_ctx(server_rec *s,
403 int protocol = sc->nProtocol;
406 * Create the new per-server SSL context
408 if (protocol == SSL_PROTOCOL_NONE) {
409 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
410 "No SSL protocols available [hint: SSLProtocol]");
415 (protocol & SSL_PROTOCOL_SSLV2 ? "SSLv2, " : ""),
416 (protocol & SSL_PROTOCOL_SSLV3 ? "SSLv3, " : ""),
417 (protocol & SSL_PROTOCOL_TLSV1 ? "TLSv1, " : ""),
419 cp[strlen(cp)-2] = NUL;
421 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
422 "Creating new SSL context (protocols: %s)", cp);
424 if (protocol == SSL_PROTOCOL_SSLV2) {
425 ctx = SSL_CTX_new(SSLv2_server_method()); /* only SSLv2 is left */
428 ctx = SSL_CTX_new(SSLv23_server_method()); /* be more flexible */
433 SSL_CTX_set_options(ctx, SSL_OP_ALL);
435 if (!(protocol & SSL_PROTOCOL_SSLV2)) {
436 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
439 if (!(protocol & SSL_PROTOCOL_SSLV3)) {
440 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
443 if (!(protocol & SSL_PROTOCOL_TLSV1)) {
444 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
447 SSL_CTX_set_app_data(ctx, s);
450 * Configure additional context ingredients
452 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
457 static void ssl_init_session_cache_ctx(server_rec *s,
462 SSL_CTX *ctx = sc->pSSLCtx;
463 SSLModConfigRec *mc = myModConfig(s);
464 long cache_mode = SSL_SESS_CACHE_OFF;
466 if (mc->nSessionCacheMode != SSL_SCMODE_NONE) {
467 /* SSL_SESS_CACHE_NO_INTERNAL_LOOKUP will force OpenSSL
468 * to ignore process local-caching and
469 * to always get/set/delete sessions using mod_ssl's callbacks.
471 cache_mode = SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL_LOOKUP;
474 SSL_CTX_set_session_cache_mode(ctx, cache_mode);
476 SSL_CTX_sess_set_new_cb(ctx, ssl_callback_NewSessionCacheEntry);
477 SSL_CTX_sess_set_get_cb(ctx, ssl_callback_GetSessionCacheEntry);
478 SSL_CTX_sess_set_remove_cb(ctx, ssl_callback_DelSessionCacheEntry);
481 static void ssl_init_verify(server_rec *s,
486 SSL_CTX *ctx = sc->pSSLCtx;
488 int verify = SSL_VERIFY_NONE;
489 STACK_OF(X509_NAME) *ca_list;
491 if (sc->nVerifyClient == SSL_CVERIFY_UNSET) {
492 sc->nVerifyClient = SSL_CVERIFY_NONE;
495 if (sc->nVerifyDepth == UNSET) {
496 sc->nVerifyDepth = 1;
500 * Configure callbacks for SSL context
502 if (sc->nVerifyClient == SSL_CVERIFY_REQUIRE) {
503 verify |= SSL_VERIFY_PEER_STRICT;
506 if ((sc->nVerifyClient == SSL_CVERIFY_OPTIONAL) ||
507 (sc->nVerifyClient == SSL_CVERIFY_OPTIONAL_NO_CA))
509 verify |= SSL_VERIFY_PEER;
512 SSL_CTX_set_verify(ctx, verify, ssl_callback_SSLVerify);
515 * Configure Client Authentication details
517 if (sc->szCACertificateFile || sc->szCACertificatePath) {
518 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
519 "Configuring client authentication");
521 if (!SSL_CTX_load_verify_locations(ctx,
522 sc->szCACertificateFile,
523 sc->szCACertificatePath))
525 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
526 "Unable to configure verify locations "
527 "for client authentication");
531 ca_list = ssl_init_FindCAList(s, ptemp,
532 sc->szCACertificateFile,
533 sc->szCACertificatePath);
535 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
536 "Unable to determine list of available "
537 "CA certificates for client authentication");
541 SSL_CTX_set_client_CA_list(ctx, (STACK *)ca_list);
545 * Give a warning when no CAs were configured but client authentication
546 * should take place. This cannot work.
548 if (sc->nVerifyClient == SSL_CVERIFY_REQUIRE) {
549 ca_list = (STACK_OF(X509_NAME) *)SSL_CTX_get_client_CA_list(ctx);
551 if (sk_X509_NAME_num(ca_list) == 0) {
552 ssl_log(s, SSL_LOG_WARN,
553 "Init: Oops, you want to request client authentication, "
554 "but no CAs are known for verification!? "
555 "[Hint: SSLCACertificate*]");
560 static void ssl_init_cipher_suite(server_rec *s,
565 SSL_CTX *ctx = sc->pSSLCtx;
566 const char *suite = sc->szCipherSuite;
569 * Configure SSL Cipher Suite
575 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
576 "Configuring permitted SSL ciphers [%s]",
579 if (!SSL_CTX_set_cipher_list(ctx, suite)) {
580 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
581 "Unable to configure permitted SSL ciphers");
586 static void ssl_init_crl(server_rec *s,
592 * Configure Certificate Revocation List (CRL) Details
595 if (!(sc->szCARevocationFile || sc->szCARevocationPath)) {
599 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
600 "Configuring certificate revocation facility");
602 sc->pRevocationStore =
603 SSL_X509_STORE_create((char *)sc->szCARevocationFile,
604 (char *)sc->szCARevocationPath);
606 if (!sc->pRevocationStore) {
607 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
608 "Unable to configure X.509 CRL storage "
609 "for certificate revocation");
614 static void ssl_init_cert_chain(server_rec *s,
619 BOOL skip_first = TRUE;
621 const char *chain = sc->szCertificateChain;
624 * Optionally configure extra server certificate chain certificates.
625 * This is usually done by OpenSSL automatically when one of the
626 * server cert issuers are found under SSLCACertificatePath or in
627 * SSLCACertificateFile. But because these are intended for client
628 * authentication it can conflict. For instance when you use a
629 * Global ID server certificate you've to send out the intermediate
630 * CA certificate, too. When you would just configure this with
631 * SSLCACertificateFile and also use client authentication mod_ssl
632 * would accept all clients also issued by this CA. Obviously this
633 * isn't what we want in this situation. So this feature here exists
634 * to allow one to explicity configure CA certificates which are
635 * used only for the server certificate chain.
641 for (i = 0; (i < SSL_AIDX_MAX) && sc->szPublicCertFiles[i]; i++) {
642 if (strEQ(sc->szPublicCertFiles[i], chain)) {
648 n = SSL_CTX_use_certificate_chain(sc->pSSLCtx,
652 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
653 "Failed to configure CA certificate chain!");
657 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
658 "Configuring server certificate chain "
659 "(%d CA certificate%s)",
660 n, n == 1 ? "" : "s");
663 static int ssl_server_import_cert(server_rec *s,
668 SSLModConfigRec *mc = myModConfig(s);
671 const char *type = ssl_asn1_keystr(idx);
674 if (!(asn1 = ssl_asn1_table_get(mc->tPublicCert, id))) {
678 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
679 "Configuring %s server certificate", type);
682 if (!(cert = d2i_X509(NULL, &ptr, asn1->nData))) {
683 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
684 "Unable to import %s server certificate", type);
688 if (SSL_CTX_use_certificate(sc->pSSLCtx, cert) <= 0) {
689 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
690 "Unable to configure %s server certificate", type);
694 sc->pPublicCert[idx] = cert;
699 static int ssl_server_import_key(server_rec *s,
704 SSLModConfigRec *mc = myModConfig(s);
707 const char *type = ssl_asn1_keystr(idx);
708 int pkey_type = (idx == SSL_AIDX_RSA) ? EVP_PKEY_RSA : EVP_PKEY_DSA;
711 if (!(asn1 = ssl_asn1_table_get(mc->tPrivateKey, id))) {
715 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
716 "Configuring %s server private key", type);
719 if (!(pkey = d2i_PrivateKey(pkey_type, NULL, &ptr, asn1->nData)))
721 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
722 "Unable to import %s server private key", type);
726 if (SSL_CTX_use_PrivateKey(sc->pSSLCtx, pkey) <= 0) {
727 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
728 "Unable to configure %s server private key", type);
732 sc->pPrivateKey[idx] = pkey;
737 static void ssl_check_public_cert(server_rec *s,
750 * Some information about the certificate(s)
753 if (SSL_X509_isSGC(cert)) {
754 ssl_log(s, SSL_LOG_INFO|SSL_INIT,
755 "%s server certificate enables "
756 "Server Gated Cryptography (SGC)",
757 ssl_asn1_keystr(type));
760 if (SSL_X509_getBC(cert, &is_ca, &pathlen)) {
762 ssl_log(s, SSL_LOG_WARN|SSL_INIT,
763 "%s server certificate is a CA certificate "
764 "(BasicConstraints: CA == TRUE !?)",
765 ssl_asn1_keystr(type));
769 ssl_log(s, SSL_LOG_WARN|SSL_INIT,
770 "%s server certificate is not a leaf certificate "
771 "(BasicConstraints: pathlen == %d > 0 !?)",
772 ssl_asn1_keystr(type), pathlen);
776 if (SSL_X509_getCN(ptemp, cert, &cn)) {
777 int fnm_flags = FNM_PERIOD|FNM_CASE_BLIND;
779 if (apr_is_fnmatch(cn) &&
780 (apr_fnmatch(cn, s->server_hostname,
781 fnm_flags) == FNM_NOMATCH))
783 ssl_log(s, SSL_LOG_WARN|SSL_INIT,
784 "%s server certificate wildcard CommonName (CN) `%s' "
785 "does NOT match server name!?",
786 ssl_asn1_keystr(type), cn);
788 else if (strNE(s->server_hostname, cn)) {
789 ssl_log(s, SSL_LOG_WARN|SSL_INIT,
790 "%s server certificate CommonName (CN) `%s' "
791 "does NOT match server name!?",
792 ssl_asn1_keystr(type), cn);
798 * Configure a particular server
800 void ssl_init_ConfigureServer(server_rec *s,
805 const char *rsa_id, *dsa_id;
806 const char *vhost_id = sc->szVHostID;
810 int have_rsa, have_dsa;
812 ssl_init_check_server(s, p, ptemp, sc);
814 ctx = ssl_init_ctx(s, p, ptemp, sc);
816 ssl_init_session_cache_ctx(s, p, ptemp, sc);
818 ssl_init_verify(s, p, ptemp, sc);
820 ssl_init_cipher_suite(s, p, ptemp, sc);
822 ssl_init_crl(s, p, ptemp, sc);
824 ssl_init_cert_chain(s, p, ptemp, sc);
826 SSL_CTX_set_tmp_rsa_callback(ctx, ssl_callback_TmpRSA);
827 SSL_CTX_set_tmp_dh_callback(ctx, ssl_callback_TmpDH);
829 if (sc->nLogLevel >= SSL_LOG_INFO) {
830 /* this callback only logs if SSLLogLevel >= info */
831 SSL_CTX_set_info_callback(ctx, ssl_callback_LogTracingState);
835 * Configure server certificate(s)
837 rsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_RSA);
838 dsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_DSA);
840 have_rsa = ssl_server_import_cert(s, sc, rsa_id, SSL_AIDX_RSA);
841 have_dsa = ssl_server_import_cert(s, sc, dsa_id, SSL_AIDX_DSA);
843 if (!(have_rsa || have_dsa)) {
844 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
845 "Oops, no RSA or DSA server certificate found?!");
846 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
847 "You have to perform a *full* server restart "
848 "when you added or removed a certificate and/or key file");
852 for (i = 0; i < SSL_AIDX_MAX; i++) {
853 ssl_check_public_cert(s, ptemp, sc->pPublicCert[i], i);
856 have_rsa = ssl_server_import_key(s, sc, rsa_id, SSL_AIDX_RSA);
857 have_dsa = ssl_server_import_key(s, sc, dsa_id, SSL_AIDX_DSA);
859 if (!(have_rsa || have_dsa)) {
860 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
861 "Oops, no RSA or DSA server private key found?!");
866 * Optionally copy DSA parameters for certificate from private key
867 * (see http://www.psy.uq.edu.au/~ftp/Crypto/ssleay/TODO.html)
869 if (sc->pPublicCert[SSL_AIDX_DSA] &&
870 sc->pPrivateKey[SSL_AIDX_DSA])
872 pkey = X509_get_pubkey(sc->pPublicCert[SSL_AIDX_DSA]);
874 if (pkey && (EVP_PKEY_key_type(pkey) == EVP_PKEY_DSA) &&
875 EVP_PKEY_missing_parameters(pkey))
877 EVP_PKEY_copy_parameters(pkey,
878 sc->pPrivateKey[SSL_AIDX_DSA]);
883 void ssl_init_CheckServers(server_rec *base_server, apr_pool_t *p)
891 BOOL conflict = FALSE;
894 * Give out warnings when a server has HTTPS configured
895 * for the HTTP port or vice versa
897 for (s = base_server; s; s = s->next) {
900 if (sc->bEnabled && (s->port == DEFAULT_HTTP_PORT)) {
901 ssl_log(base_server, SSL_LOG_WARN,
902 "Init: (%s) You configured HTTPS(%d) "
903 "on the standard HTTP(%d) port!",
904 ssl_util_vhostid(p, s),
905 DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT);
908 if (!sc->bEnabled && (s->port == DEFAULT_HTTPS_PORT)) {
909 ssl_log(base_server, SSL_LOG_WARN,
910 "Init: (%s) You configured HTTP(%d) "
911 "on the standard HTTPS(%d) port!",
912 ssl_util_vhostid(p, s),
913 DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT);
918 * Give out warnings when more than one SSL-aware virtual server uses the
919 * same IP:port. This doesn't work because mod_ssl then will always use
920 * just the certificate/keys of one virtual host (which one cannot be said
921 * easily - but that doesn't matter here).
923 table = apr_hash_make(p);
925 for (s = base_server; s; s = s->next) {
932 key = apr_psprintf(p, "%pA:%u",
933 &s->addrs->host_addr, s->addrs->host_port);
936 if ((ps = (server_rec *)apr_hash_get(table, key, klen))) {
937 ssl_log(base_server, SSL_LOG_WARN,
938 "Init: SSL server IP/port conflict: "
939 "%s (%s:%d) vs. %s (%s:%d)",
940 ssl_util_vhostid(p, s),
941 (s->defn_name ? s->defn_name : "unknown"),
943 ssl_util_vhostid(p, ps),
944 (ps->defn_name ? ps->defn_name : "unknown"),
945 ps->defn_line_number);
950 apr_hash_set(table, key, klen, s);
954 ssl_log(base_server, SSL_LOG_WARN,
955 "Init: You should not use name-based "
956 "virtual hosts in conjunction with SSL!!");
960 static int ssl_init_FindCAList_X509NameCmp(X509_NAME **a, X509_NAME **b)
962 return(X509_NAME_cmp(*a, *b));
965 static void ssl_init_PushCAList(STACK_OF(X509_NAME) *ca_list,
966 server_rec *s, const char *file)
969 STACK_OF(X509_NAME) *sk;
971 sk = (STACK_OF(X509_NAME) *)SSL_load_client_CA_file(file);
977 for (n = 0; n < sk_X509_NAME_num(sk); n++) {
979 X509_NAME *name = sk_X509_NAME_value(sk, n);
981 ssl_log(s, SSL_LOG_TRACE,
982 "CA certificate: %s",
983 X509_NAME_oneline(name, name_buf, sizeof(name_buf)));
986 * note that SSL_load_client_CA_file() checks for duplicates,
987 * but since we call it multiple times when reading a directory
988 * we must also check for duplicates ourselves.
991 if (sk_X509_NAME_find(ca_list, name) < 0) {
992 /* this will be freed when ca_list is */
993 sk_X509_NAME_push(ca_list, name);
996 /* need to free this ourselves, else it will leak */
997 X509_NAME_free(name);
1001 sk_X509_NAME_free(sk);
1004 STACK_OF(X509_NAME) *ssl_init_FindCAList(server_rec *s,
1006 const char *ca_file,
1007 const char *ca_path)
1009 STACK_OF(X509_NAME) *ca_list;
1012 * Start with a empty stack/list where new
1013 * entries get added in sorted order.
1015 ca_list = sk_X509_NAME_new(ssl_init_FindCAList_X509NameCmp);
1018 * Process CA certificate bundle file
1021 ssl_init_PushCAList(ca_list, s, ca_file);
1025 * Process CA certificate path files
1029 apr_finfo_t direntry;
1030 apr_int32_t finfo_flags = APR_FINFO_MIN|APR_FINFO_NAME;
1032 if (apr_dir_open(&dir, ca_path, ptemp) != APR_SUCCESS) {
1033 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_ERRNO|SSL_INIT,
1034 "Failed to open SSLCACertificatePath `%s'",
1039 while ((apr_dir_read(&direntry, finfo_flags, dir)) == APR_SUCCESS) {
1041 if (direntry.filetype == APR_DIR) {
1042 continue; /* don't try to load directories */
1044 file = apr_pstrcat(ptemp, ca_path, "/", direntry.name, NULL);
1045 ssl_init_PushCAList(ca_list, s, file);
1054 sk_X509_NAME_set_cmp_func(ca_list, NULL);
1059 void ssl_init_Child(apr_pool_t *p, server_rec *s)
1061 SSLModConfigRec *mc = myModConfig(s);
1062 mc->pid = getpid(); /* only call getpid() once per-process */
1064 /* XXX: there should be an ap_srand() function */
1065 srand((unsigned int)time(NULL));
1067 /* open the mutex lockfile */
1068 ssl_mutex_reinit(s, p);
1071 #define MODSSL_CFG_ITEM_FREE(func, item) \
1077 apr_status_t ssl_init_ModuleKill(void *data)
1079 SSLSrvConfigRec *sc;
1080 server_rec *base_server = (server_rec *)data;
1084 * Drop the session cache and mutex
1086 ssl_scache_kill(base_server);
1089 * Destroy the temporary keys and params
1091 ssl_tmp_keys_free(base_server);
1094 * Free the non-pool allocated structures
1095 * in the per-server configurations
1097 for (s = base_server; s; s = s->next) {
1099 sc = mySrvConfig(s);
1101 for (i=0; i < SSL_AIDX_MAX; i++) {
1102 MODSSL_CFG_ITEM_FREE(X509_free,
1103 sc->pPublicCert[i]);
1105 MODSSL_CFG_ITEM_FREE(EVP_PKEY_free,
1106 sc->pPrivateKey[i]);
1109 MODSSL_CFG_ITEM_FREE(X509_STORE_free,
1110 sc->pRevocationStore);
1112 MODSSL_CFG_ITEM_FREE(SSL_CTX_free,
1117 * Try to kill the internals of the SSL library.
1120 ERR_remove_state(0);