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) {
236 * Create the server host:port string because we need it a lot
238 sc->vhost_id = ssl_util_vhostid(p, s);
239 sc->vhost_id_len = strlen(sc->vhost_id);
241 /* Fix up stuff that may not have been set */
242 if (sc->enabled == UNSET) {
246 if (sc->session_cache_timeout == UNSET) {
247 sc->session_cache_timeout = SSL_SESSION_CACHE_TIMEOUT;
250 if (sc->server->pphrase_dialog_type == SSL_PPTYPE_UNSET) {
251 sc->server->pphrase_dialog_type = SSL_PPTYPE_BUILTIN;
254 /* Open the dedicated SSL logfile */
255 ssl_log_open(base_server, s, p);
258 ssl_init_SSLLibrary(base_server);
261 ssl_util_thread_setup(base_server, p);
265 * Seed the Pseudo Random Number Generator (PRNG)
266 * only need ptemp here; nothing inside allocated from the pool
267 * needs to live once we return from ssl_rand_seed().
269 ssl_rand_seed(base_server, ptemp, SSL_RSCTX_STARTUP, "Init: ");
272 * read server private keys/public certs into memory.
273 * decrypting any encrypted keys via configured SSLPassPhraseDialogs
274 * anything that needs to live longer than ptemp needs to also survive
275 * restarts, in which case they'll live inside s->process->pool.
277 ssl_pphrase_Handle(base_server, ptemp);
279 ssl_tmp_keys_init(base_server);
282 * SSL external crypto device ("engine") support
284 #ifdef SSL_EXPERIMENTAL_ENGINE
285 ssl_init_Engine(base_server, p);
289 * initialize the mutex handling
291 if (!ssl_mutex_init(base_server, p)) {
292 return HTTP_INTERNAL_SERVER_ERROR;
296 * initialize session caching
298 ssl_scache_init(base_server, p);
303 ssl_log(base_server, SSL_LOG_INFO,
304 "Init: Initializing (virtual) servers for SSL");
306 for (s = base_server; s; s = s->next) {
309 * Either now skip this server when SSL is disabled for
310 * it or give out some information about what we're
317 ssl_log(s, SSL_LOG_INFO|SSL_INIT,
318 "Configuring server for SSL protocol");
321 * Read the server certificate and key
323 ssl_init_ConfigureServer(s, p, ptemp, sc);
327 * Configuration consistency checks
329 ssl_init_CheckServers(base_server, ptemp);
332 * Announce mod_ssl and SSL library in HTTP Server field
333 * as ``mod_ssl/X.X.X OpenSSL/X.X.X''
335 ssl_add_version_components(p, base_server);
337 SSL_init_app_data2_idx(); /* for SSL_get_app_data2() at request time */
343 * Support for external a Crypto Device ("engine"), usually
344 * a hardware accellerator card for crypto operations.
346 #ifdef SSL_EXPERIMENTAL_ENGINE
347 void ssl_init_Engine(server_rec *s, apr_pool_t *p)
349 SSLModConfigRec *mc = myModConfig(s);
352 if (mc->szCryptoDevice) {
353 if (!(e = ENGINE_by_id(mc->szCryptoDevice))) {
354 ssl_log(s, SSL_LOG_ERROR,
355 "Init: Failed to load Crypto Device API `%s'",
360 if (strEQ(mc->szCryptoDevice, "chil")) {
361 ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
364 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
365 ssl_log(s, SSL_LOG_ERROR,
366 "Init: Failed to enable Crypto Device API `%s'",
376 static void ssl_init_server_check(server_rec *s,
382 * check for important parameters and the
383 * possibility that the user forgot to set them.
385 if (!mctx->pks->cert_files[0]) {
386 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
387 "No SSL Certificate set [hint: SSLCertificateFile]");
392 * Check for problematic re-initializations
394 if (mctx->pks->certs[SSL_AIDX_RSA] ||
395 mctx->pks->certs[SSL_AIDX_DSA])
397 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
398 "Illegal attempt to re-initialise SSL for server "
399 "(theoretically shouldn't happen!)");
404 static void ssl_init_ctx(server_rec *s,
410 SSL_METHOD *method = NULL;
412 int protocol = mctx->protocol;
415 * Create the new per-server SSL context
417 if (protocol == SSL_PROTOCOL_NONE) {
418 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
419 "No SSL protocols available [hint: SSLProtocol]");
424 (protocol & SSL_PROTOCOL_SSLV2 ? "SSLv2, " : ""),
425 (protocol & SSL_PROTOCOL_SSLV3 ? "SSLv3, " : ""),
426 (protocol & SSL_PROTOCOL_TLSV1 ? "TLSv1, " : ""),
428 cp[strlen(cp)-2] = NUL;
430 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
431 "Creating new SSL context (protocols: %s)", cp);
433 if (protocol == SSL_PROTOCOL_SSLV2) {
435 SSLv2_client_method() : /* proxy */
436 SSLv2_server_method(); /* server */
437 ctx = SSL_CTX_new(method); /* only SSLv2 is left */
441 SSLv23_client_method() : /* proxy */
442 SSLv23_server_method(); /* server */
443 ctx = SSL_CTX_new(method); /* be more flexible */
448 SSL_CTX_set_options(ctx, SSL_OP_ALL);
450 if (!(protocol & SSL_PROTOCOL_SSLV2)) {
451 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
454 if (!(protocol & SSL_PROTOCOL_SSLV3)) {
455 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
458 if (!(protocol & SSL_PROTOCOL_TLSV1)) {
459 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
462 SSL_CTX_set_app_data(ctx, s);
465 * Configure additional context ingredients
467 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
470 static void ssl_init_ctx_session_cache(server_rec *s,
475 SSL_CTX *ctx = mctx->ssl_ctx;
476 SSLModConfigRec *mc = myModConfig(s);
477 long cache_mode = SSL_SESS_CACHE_OFF;
479 if (mc->nSessionCacheMode != SSL_SCMODE_NONE) {
480 /* SSL_SESS_CACHE_NO_INTERNAL_LOOKUP 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_LOOKUP;
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 (mctx->sc->log_level >= SSL_LOG_INFO) {
505 /* this callback only logs if SSLLogLevel >= 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 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
548 "Configuring client authentication");
550 if (!SSL_CTX_load_verify_locations(ctx,
551 mctx->auth.ca_cert_file,
552 mctx->auth.ca_cert_path))
554 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
555 "Unable to configure verify locations "
556 "for client authentication");
560 ca_list = ssl_init_FindCAList(s, ptemp,
561 mctx->auth.ca_cert_file,
562 mctx->auth.ca_cert_path);
564 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
565 "Unable to determine list of available "
566 "CA certificates for client authentication");
570 SSL_CTX_set_client_CA_list(ctx, (STACK *)ca_list);
574 * Give a warning when no CAs were configured but client authentication
575 * should take place. This cannot work.
577 if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
578 ca_list = (STACK_OF(X509_NAME) *)SSL_CTX_get_client_CA_list(ctx);
580 if (sk_X509_NAME_num(ca_list) == 0) {
581 ssl_log(s, SSL_LOG_WARN,
582 "Init: Oops, you want to request client authentication, "
583 "but no CAs are known for verification!? "
584 "[Hint: SSLCACertificate*]");
589 static void ssl_init_ctx_cipher_suite(server_rec *s,
594 SSL_CTX *ctx = mctx->ssl_ctx;
595 const char *suite = mctx->auth.cipher_suite;
598 * Configure SSL Cipher Suite
604 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
605 "Configuring permitted SSL ciphers [%s]",
608 if (!SSL_CTX_set_cipher_list(ctx, suite)) {
609 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
610 "Unable to configure permitted SSL ciphers");
615 static void ssl_init_ctx_crl(server_rec *s,
621 * Configure Certificate Revocation List (CRL) Details
624 if (!(mctx->crl_file || mctx->crl_path)) {
628 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
629 "Configuring certificate revocation facility");
632 SSL_X509_STORE_create((char *)mctx->crl_file,
633 (char *)mctx->crl_path);
636 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
637 "Unable to configure X.509 CRL storage "
638 "for certificate revocation");
643 static void ssl_init_ctx_cert_chain(server_rec *s,
648 BOOL skip_first = TRUE;
650 const char *chain = mctx->cert_chain;
653 * Optionally configure extra server certificate chain certificates.
654 * This is usually done by OpenSSL automatically when one of the
655 * server cert issuers are found under SSLCACertificatePath or in
656 * SSLCACertificateFile. But because these are intended for client
657 * authentication it can conflict. For instance when you use a
658 * Global ID server certificate you've to send out the intermediate
659 * CA certificate, too. When you would just configure this with
660 * SSLCACertificateFile and also use client authentication mod_ssl
661 * would accept all clients also issued by this CA. Obviously this
662 * isn't what we want in this situation. So this feature here exists
663 * to allow one to explicity configure CA certificates which are
664 * used only for the server certificate chain.
670 for (i = 0; (i < SSL_AIDX_MAX) && mctx->pks->cert_files[i]; i++) {
671 if (strEQ(mctx->pks->cert_files[i], chain)) {
677 n = SSL_CTX_use_certificate_chain(mctx->ssl_ctx,
681 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
682 "Failed to configure CA certificate chain!");
686 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
687 "Configuring server certificate chain "
688 "(%d CA certificate%s)",
689 n, n == 1 ? "" : "s");
692 static int ssl_server_import_cert(server_rec *s,
697 SSLModConfigRec *mc = myModConfig(s);
700 const char *type = ssl_asn1_keystr(idx);
703 if (!(asn1 = ssl_asn1_table_get(mc->tPublicCert, id))) {
707 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
708 "Configuring %s server certificate", type);
711 if (!(cert = d2i_X509(NULL, &ptr, asn1->nData))) {
712 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
713 "Unable to import %s server certificate", type);
717 if (SSL_CTX_use_certificate(mctx->ssl_ctx, cert) <= 0) {
718 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
719 "Unable to configure %s server certificate", type);
723 mctx->pks->certs[idx] = cert;
728 static int ssl_server_import_key(server_rec *s,
733 SSLModConfigRec *mc = myModConfig(s);
736 const char *type = ssl_asn1_keystr(idx);
737 int pkey_type = (idx == SSL_AIDX_RSA) ? EVP_PKEY_RSA : EVP_PKEY_DSA;
740 if (!(asn1 = ssl_asn1_table_get(mc->tPrivateKey, id))) {
744 ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
745 "Configuring %s server private key", type);
748 if (!(pkey = d2i_PrivateKey(pkey_type, NULL, &ptr, asn1->nData)))
750 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
751 "Unable to import %s server private key", type);
755 if (SSL_CTX_use_PrivateKey(mctx->ssl_ctx, pkey) <= 0) {
756 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
757 "Unable to configure %s server private key", type);
762 * XXX: wonder if this is still needed, this is old todo doc.
763 * (see http://www.psy.uq.edu.au/~ftp/Crypto/ssleay/TODO.html)
765 if ((pkey_type == EVP_PKEY_DSA) && mctx->pks->certs[idx]) {
766 EVP_PKEY *pubkey = X509_get_pubkey(mctx->pks->certs[idx]);
768 if (pubkey && EVP_PKEY_missing_parameters(pubkey)) {
769 EVP_PKEY_copy_parameters(pubkey, pkey);
770 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
771 "Copying DSA parameters from private key to certificate");
775 mctx->pks->keys[idx] = pkey;
780 static void ssl_check_public_cert(server_rec *s,
793 * Some information about the certificate(s)
796 if (SSL_X509_isSGC(cert)) {
797 ssl_log(s, SSL_LOG_INFO|SSL_INIT,
798 "%s server certificate enables "
799 "Server Gated Cryptography (SGC)",
800 ssl_asn1_keystr(type));
803 if (SSL_X509_getBC(cert, &is_ca, &pathlen)) {
805 ssl_log(s, SSL_LOG_WARN|SSL_INIT,
806 "%s server certificate is a CA certificate "
807 "(BasicConstraints: CA == TRUE !?)",
808 ssl_asn1_keystr(type));
812 ssl_log(s, SSL_LOG_WARN|SSL_INIT,
813 "%s server certificate is not a leaf certificate "
814 "(BasicConstraints: pathlen == %d > 0 !?)",
815 ssl_asn1_keystr(type), pathlen);
819 if (SSL_X509_getCN(ptemp, cert, &cn)) {
820 int fnm_flags = FNM_PERIOD|FNM_CASE_BLIND;
822 if (apr_is_fnmatch(cn) &&
823 (apr_fnmatch(cn, s->server_hostname,
824 fnm_flags) == FNM_NOMATCH))
826 ssl_log(s, SSL_LOG_WARN|SSL_INIT,
827 "%s server certificate wildcard CommonName (CN) `%s' "
828 "does NOT match server name!?",
829 ssl_asn1_keystr(type), cn);
831 else if (strNE(s->server_hostname, cn)) {
832 ssl_log(s, SSL_LOG_WARN|SSL_INIT,
833 "%s server certificate CommonName (CN) `%s' "
834 "does NOT match server name!?",
835 ssl_asn1_keystr(type), cn);
840 static void ssl_init_server_certs(server_rec *s,
845 const char *rsa_id, *dsa_id;
846 const char *vhost_id = mctx->sc->vhost_id;
848 int have_rsa, have_dsa;
850 rsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_RSA);
851 dsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_DSA);
853 have_rsa = ssl_server_import_cert(s, mctx, rsa_id, SSL_AIDX_RSA);
854 have_dsa = ssl_server_import_cert(s, mctx, dsa_id, SSL_AIDX_DSA);
856 if (!(have_rsa || have_dsa)) {
857 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
858 "Oops, no RSA or DSA server certificate found?!");
862 for (i = 0; i < SSL_AIDX_MAX; i++) {
863 ssl_check_public_cert(s, ptemp, mctx->pks->certs[i], i);
866 have_rsa = ssl_server_import_key(s, mctx, rsa_id, SSL_AIDX_RSA);
867 have_dsa = ssl_server_import_key(s, mctx, dsa_id, SSL_AIDX_DSA);
869 if (!(have_rsa || have_dsa)) {
870 ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
871 "Oops, no RSA or DSA server private key found?!");
877 * Configure a particular server
879 void ssl_init_ConfigureServer(server_rec *s,
884 ssl_init_server_check(s, p, ptemp, sc->server);
886 ssl_init_ctx(s, p, ptemp, sc->server);
888 ssl_init_ctx_session_cache(s, p, ptemp, sc->server);
890 ssl_init_ctx_callbacks(s, p, ptemp, sc->server);
892 ssl_init_ctx_verify(s, p, ptemp, sc->server);
894 ssl_init_ctx_cipher_suite(s, p, ptemp, sc->server);
896 ssl_init_ctx_crl(s, p, ptemp, sc->server);
898 ssl_init_ctx_cert_chain(s, p, ptemp, sc->server);
900 ssl_init_server_certs(s, p, ptemp, sc->server);
903 void ssl_init_CheckServers(server_rec *base_server, apr_pool_t *p)
911 BOOL conflict = FALSE;
914 * Give out warnings when a server has HTTPS configured
915 * for the HTTP port or vice versa
917 for (s = base_server; s; s = s->next) {
920 if (sc->enabled && (s->port == DEFAULT_HTTP_PORT)) {
921 ssl_log(base_server, SSL_LOG_WARN,
922 "Init: (%s) You configured HTTPS(%d) "
923 "on the standard HTTP(%d) port!",
924 ssl_util_vhostid(p, s),
925 DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT);
928 if (!sc->enabled && (s->port == DEFAULT_HTTPS_PORT)) {
929 ssl_log(base_server, SSL_LOG_WARN,
930 "Init: (%s) You configured HTTP(%d) "
931 "on the standard HTTPS(%d) port!",
932 ssl_util_vhostid(p, s),
933 DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT);
938 * Give out warnings when more than one SSL-aware virtual server uses the
939 * same IP:port. This doesn't work because mod_ssl then will always use
940 * just the certificate/keys of one virtual host (which one cannot be said
941 * easily - but that doesn't matter here).
943 table = apr_hash_make(p);
945 for (s = base_server; s; s = s->next) {
952 key = apr_psprintf(p, "%pA:%u",
953 &s->addrs->host_addr, s->addrs->host_port);
956 if ((ps = (server_rec *)apr_hash_get(table, key, klen))) {
957 ssl_log(base_server, SSL_LOG_WARN,
958 "Init: SSL server IP/port conflict: "
959 "%s (%s:%d) vs. %s (%s:%d)",
960 ssl_util_vhostid(p, s),
961 (s->defn_name ? s->defn_name : "unknown"),
963 ssl_util_vhostid(p, ps),
964 (ps->defn_name ? ps->defn_name : "unknown"),
965 ps->defn_line_number);
970 apr_hash_set(table, key, klen, s);
974 ssl_log(base_server, SSL_LOG_WARN,
975 "Init: You should not use name-based "
976 "virtual hosts in conjunction with SSL!!");
980 static int ssl_init_FindCAList_X509NameCmp(X509_NAME **a, X509_NAME **b)
982 return(X509_NAME_cmp(*a, *b));
985 static void ssl_init_PushCAList(STACK_OF(X509_NAME) *ca_list,
986 server_rec *s, const char *file)
989 STACK_OF(X509_NAME) *sk;
991 sk = (STACK_OF(X509_NAME) *)SSL_load_client_CA_file(file);
997 for (n = 0; n < sk_X509_NAME_num(sk); n++) {
999 X509_NAME *name = sk_X509_NAME_value(sk, n);
1001 ssl_log(s, SSL_LOG_TRACE,
1002 "CA certificate: %s",
1003 X509_NAME_oneline(name, name_buf, sizeof(name_buf)));
1006 * note that SSL_load_client_CA_file() checks for duplicates,
1007 * but since we call it multiple times when reading a directory
1008 * we must also check for duplicates ourselves.
1011 if (sk_X509_NAME_find(ca_list, name) < 0) {
1012 /* this will be freed when ca_list is */
1013 sk_X509_NAME_push(ca_list, name);
1016 /* need to free this ourselves, else it will leak */
1017 X509_NAME_free(name);
1021 sk_X509_NAME_free(sk);
1024 STACK_OF(X509_NAME) *ssl_init_FindCAList(server_rec *s,
1026 const char *ca_file,
1027 const char *ca_path)
1029 STACK_OF(X509_NAME) *ca_list;
1032 * Start with a empty stack/list where new
1033 * entries get added in sorted order.
1035 ca_list = sk_X509_NAME_new(ssl_init_FindCAList_X509NameCmp);
1038 * Process CA certificate bundle file
1041 ssl_init_PushCAList(ca_list, s, ca_file);
1045 * Process CA certificate path files
1049 apr_finfo_t direntry;
1050 apr_int32_t finfo_flags = APR_FINFO_MIN|APR_FINFO_NAME;
1052 if (apr_dir_open(&dir, ca_path, ptemp) != APR_SUCCESS) {
1053 ssl_log(s, SSL_LOG_ERROR|SSL_ADD_ERRNO|SSL_INIT,
1054 "Failed to open SSLCACertificatePath `%s'",
1059 while ((apr_dir_read(&direntry, finfo_flags, dir)) == APR_SUCCESS) {
1061 if (direntry.filetype == APR_DIR) {
1062 continue; /* don't try to load directories */
1064 file = apr_pstrcat(ptemp, ca_path, "/", direntry.name, NULL);
1065 ssl_init_PushCAList(ca_list, s, file);
1074 sk_X509_NAME_set_cmp_func(ca_list, NULL);
1079 void ssl_init_Child(apr_pool_t *p, server_rec *s)
1081 SSLModConfigRec *mc = myModConfig(s);
1082 mc->pid = getpid(); /* only call getpid() once per-process */
1084 /* XXX: there should be an ap_srand() function */
1085 srand((unsigned int)time(NULL));
1087 /* open the mutex lockfile */
1088 ssl_mutex_reinit(s, p);
1091 #define MODSSL_CFG_ITEM_FREE(func, item) \
1097 apr_status_t ssl_init_ModuleKill(void *data)
1099 SSLSrvConfigRec *sc;
1100 server_rec *base_server = (server_rec *)data;
1104 * Drop the session cache and mutex
1106 ssl_scache_kill(base_server);
1109 * Destroy the temporary keys and params
1111 ssl_tmp_keys_free(base_server);
1114 * Free the non-pool allocated structures
1115 * in the per-server configurations
1117 for (s = base_server; s; s = s->next) {
1119 sc = mySrvConfig(s);
1121 for (i=0; i < SSL_AIDX_MAX; i++) {
1122 MODSSL_CFG_ITEM_FREE(X509_free,
1123 sc->server->pks->certs[i]);
1125 MODSSL_CFG_ITEM_FREE(EVP_PKEY_free,
1126 sc->server->pks->keys[i]);
1129 MODSSL_CFG_ITEM_FREE(X509_STORE_free,
1132 MODSSL_CFG_ITEM_FREE(SSL_CTX_free,
1133 sc->server->ssl_ctx);
1137 * Try to kill the internals of the SSL library.
1140 ERR_remove_state(0);