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-2003 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 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
102 "Server: %s, Interface: %s, Library: %s",
103 AP_SERVER_BASEVERSION,
104 vals[1], /* SSL_VERSION_INTERFACE */
105 vals[2]); /* SSL_VERSION_LIBRARY */
110 * Handle the Temporary RSA Keys and DH Params
113 #define MODSSL_TMP_KEY_FREE(mc, type, idx) \
114 if (mc->pTmpKeys[idx]) { \
115 type##_free((type *)mc->pTmpKeys[idx]); \
116 mc->pTmpKeys[idx] = NULL; \
119 #define MODSSL_TMP_KEYS_FREE(mc, type) \
120 MODSSL_TMP_KEY_FREE(mc, type, SSL_TMP_KEY_##type##_512); \
121 MODSSL_TMP_KEY_FREE(mc, type, SSL_TMP_KEY_##type##_1024)
123 static void ssl_tmp_keys_free(server_rec *s)
125 SSLModConfigRec *mc = myModConfig(s);
127 MODSSL_TMP_KEYS_FREE(mc, RSA);
128 MODSSL_TMP_KEYS_FREE(mc, DH);
131 static int ssl_tmp_key_init_rsa(server_rec *s,
134 SSLModConfigRec *mc = myModConfig(s);
136 if (!(mc->pTmpKeys[idx] =
137 RSA_generate_key(bits, RSA_F4, NULL, NULL)))
139 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
140 "Init: Failed to generate temporary "
141 "%d bit RSA private key", bits);
148 static int ssl_tmp_key_init_dh(server_rec *s,
151 SSLModConfigRec *mc = myModConfig(s);
153 if (!(mc->pTmpKeys[idx] =
154 ssl_dh_GetTmpParam(bits)))
156 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
157 "Init: Failed to generate temporary "
158 "%d bit DH parameters", bits);
165 #define MODSSL_TMP_KEY_INIT_RSA(s, bits) \
166 ssl_tmp_key_init_rsa(s, bits, SSL_TMP_KEY_RSA_##bits)
168 #define MODSSL_TMP_KEY_INIT_DH(s, bits) \
169 ssl_tmp_key_init_dh(s, bits, SSL_TMP_KEY_DH_##bits)
171 static int ssl_tmp_keys_init(server_rec *s)
173 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
174 "Init: Generating temporary RSA private keys (512/1024 bits)");
176 if (MODSSL_TMP_KEY_INIT_RSA(s, 512) ||
177 MODSSL_TMP_KEY_INIT_RSA(s, 1024)) {
181 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
182 "Init: Generating temporary DH parameters (512/1024 bits)");
184 if (MODSSL_TMP_KEY_INIT_DH(s, 512) ||
185 MODSSL_TMP_KEY_INIT_DH(s, 1024)) {
193 * Per-module initialization
195 int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
197 server_rec *base_server)
199 SSLModConfigRec *mc = myModConfig(base_server);
203 /* We initialize mc->pid per-process in the child init,
204 * but it should be initialized for startup before we
205 * call ssl_rand_seed() below.
210 * Let us cleanup on restarts and exists
212 apr_pool_cleanup_register(p, base_server,
214 apr_pool_cleanup_null);
217 * Any init round fixes the global config
219 ssl_config_global_create(base_server); /* just to avoid problems */
220 ssl_config_global_fix(mc);
223 * try to fix the configuration and open the dedicated SSL
224 * logfile as early as possible
226 for (s = base_server; s; s = s->next) {
238 * Create the server host:port string because we need it a lot
240 sc->vhost_id = ssl_util_vhostid(p, s);
241 sc->vhost_id_len = strlen(sc->vhost_id);
244 /* If sc->enabled is UNSET, then SSL is optional on this vhost */
245 /* Fix up stuff that may not have been set */
246 if (sc->enabled == UNSET) {
250 if (sc->proxy_enabled == UNSET) {
251 sc->proxy_enabled = FALSE;
254 if (sc->session_cache_timeout == UNSET) {
255 sc->session_cache_timeout = SSL_SESSION_CACHE_TIMEOUT;
258 if (sc->server->pphrase_dialog_type == SSL_PPTYPE_UNSET) {
259 sc->server->pphrase_dialog_type = SSL_PPTYPE_BUILTIN;
265 * SSL external crypto device ("engine") support
267 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
268 ssl_init_Engine(base_server, p);
272 ssl_util_thread_setup(p);
275 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
276 "Init: Initialized %s library", SSL_LIBRARY_NAME);
279 * Seed the Pseudo Random Number Generator (PRNG)
280 * only need ptemp here; nothing inside allocated from the pool
281 * needs to live once we return from ssl_rand_seed().
283 ssl_rand_seed(base_server, ptemp, SSL_RSCTX_STARTUP, "Init: ");
286 * read server private keys/public certs into memory.
287 * decrypting any encrypted keys via configured SSLPassPhraseDialogs
288 * anything that needs to live longer than ptemp needs to also survive
289 * restarts, in which case they'll live inside s->process->pool.
291 ssl_pphrase_Handle(base_server, ptemp);
293 if (ssl_tmp_keys_init(base_server)) {
298 * initialize the mutex handling
300 if (!ssl_mutex_init(base_server, p)) {
301 return HTTP_INTERNAL_SERVER_ERROR;
305 * initialize session caching
307 ssl_scache_init(base_server, p);
312 ap_log_error(APLOG_MARK, APLOG_INFO, 0, base_server,
313 "Init: Initializing (virtual) servers for SSL");
315 for (s = base_server; s; s = s->next) {
318 * Either now skip this server when SSL is disabled for
319 * it or give out some information about what we're
324 * Read the server certificate and key
326 ssl_init_ConfigureServer(s, p, ptemp, sc);
330 * Configuration consistency checks
332 ssl_init_CheckServers(base_server, ptemp);
335 * Announce mod_ssl and SSL library in HTTP Server field
336 * as ``mod_ssl/X.X.X OpenSSL/X.X.X''
338 ssl_add_version_components(p, base_server);
340 SSL_init_app_data2_idx(); /* for SSL_get_app_data2() at request time */
346 * Support for external a Crypto Device ("engine"), usually
347 * a hardware accellerator card for crypto operations.
349 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
350 void ssl_init_Engine(server_rec *s, apr_pool_t *p)
352 SSLModConfigRec *mc = myModConfig(s);
355 if (mc->szCryptoDevice) {
356 if (!(e = ENGINE_by_id(mc->szCryptoDevice))) {
357 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
358 "Init: Failed to load Crypto Device API `%s'",
363 if (strEQ(mc->szCryptoDevice, "chil")) {
364 ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
367 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
368 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
369 "Init: Failed to enable Crypto Device API `%s'",
379 static void ssl_init_server_check(server_rec *s,
385 * check for important parameters and the
386 * possibility that the user forgot to set them.
388 if (!mctx->pks->cert_files[0]) {
389 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
390 "No SSL Certificate set [hint: SSLCertificateFile]");
395 * Check for problematic re-initializations
397 if (mctx->pks->certs[SSL_AIDX_RSA] ||
398 mctx->pks->certs[SSL_AIDX_DSA])
400 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
401 "Illegal attempt to re-initialise SSL for server "
402 "(theoretically shouldn't happen!)");
407 static void ssl_init_ctx_protocol(server_rec *s,
413 SSL_METHOD *method = NULL;
415 int protocol = mctx->protocol;
418 * Create the new per-server SSL context
420 if (protocol == SSL_PROTOCOL_NONE) {
421 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
422 "No SSL protocols available [hint: SSLProtocol]");
427 (protocol & SSL_PROTOCOL_SSLV2 ? "SSLv2, " : ""),
428 (protocol & SSL_PROTOCOL_SSLV3 ? "SSLv3, " : ""),
429 (protocol & SSL_PROTOCOL_TLSV1 ? "TLSv1, " : ""),
431 cp[strlen(cp)-2] = NUL;
433 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
434 "Creating new SSL context (protocols: %s)", cp);
436 if (protocol == SSL_PROTOCOL_SSLV2) {
438 SSLv2_client_method() : /* proxy */
439 SSLv2_server_method(); /* server */
440 ctx = SSL_CTX_new(method); /* only SSLv2 is left */
444 SSLv23_client_method() : /* proxy */
445 SSLv23_server_method(); /* server */
446 ctx = SSL_CTX_new(method); /* be more flexible */
451 SSL_CTX_set_options(ctx, SSL_OP_ALL);
453 if (!(protocol & SSL_PROTOCOL_SSLV2)) {
454 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
457 if (!(protocol & SSL_PROTOCOL_SSLV3)) {
458 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
461 if (!(protocol & SSL_PROTOCOL_TLSV1)) {
462 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
465 SSL_CTX_set_app_data(ctx, s);
468 * Configure additional context ingredients
470 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
473 static void ssl_init_ctx_session_cache(server_rec *s,
478 SSL_CTX *ctx = mctx->ssl_ctx;
479 SSLModConfigRec *mc = myModConfig(s);
480 long cache_mode = SSL_SESS_CACHE_OFF;
482 if (mc->nSessionCacheMode != SSL_SCMODE_NONE) {
483 /* SSL_SESS_CACHE_NO_INTERNAL_LOOKUP will force OpenSSL
484 * to ignore process local-caching and
485 * to always get/set/delete sessions using mod_ssl's callbacks.
487 cache_mode = SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL_LOOKUP;
490 SSL_CTX_set_session_cache_mode(ctx, cache_mode);
492 SSL_CTX_sess_set_new_cb(ctx, ssl_callback_NewSessionCacheEntry);
493 SSL_CTX_sess_set_get_cb(ctx, ssl_callback_GetSessionCacheEntry);
494 SSL_CTX_sess_set_remove_cb(ctx, ssl_callback_DelSessionCacheEntry);
497 static void ssl_init_ctx_callbacks(server_rec *s,
502 SSL_CTX *ctx = mctx->ssl_ctx;
504 SSL_CTX_set_tmp_rsa_callback(ctx, ssl_callback_TmpRSA);
505 SSL_CTX_set_tmp_dh_callback(ctx, ssl_callback_TmpDH);
507 if (s->loglevel >= APLOG_DEBUG) {
508 /* this callback only logs if LogLevel >= info */
509 SSL_CTX_set_info_callback(ctx, ssl_callback_LogTracingState);
513 static void ssl_init_ctx_verify(server_rec *s,
518 SSL_CTX *ctx = mctx->ssl_ctx;
520 int verify = SSL_VERIFY_NONE;
521 STACK_OF(X509_NAME) *ca_list;
523 if (mctx->auth.verify_mode == SSL_CVERIFY_UNSET) {
524 mctx->auth.verify_mode = SSL_CVERIFY_NONE;
527 if (mctx->auth.verify_depth == UNSET) {
528 mctx->auth.verify_depth = 1;
532 * Configure callbacks for SSL context
534 if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
535 verify |= SSL_VERIFY_PEER_STRICT;
538 if ((mctx->auth.verify_mode == SSL_CVERIFY_OPTIONAL) ||
539 (mctx->auth.verify_mode == SSL_CVERIFY_OPTIONAL_NO_CA))
541 verify |= SSL_VERIFY_PEER;
544 SSL_CTX_set_verify(ctx, verify, ssl_callback_SSLVerify);
547 * Configure Client Authentication details
549 if (mctx->auth.ca_cert_file || mctx->auth.ca_cert_path) {
550 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
551 "Configuring client authentication");
553 if (!SSL_CTX_load_verify_locations(ctx,
554 MODSSL_PCHAR_CAST mctx->auth.ca_cert_file,
555 MODSSL_PCHAR_CAST mctx->auth.ca_cert_path))
557 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
558 "Unable to configure verify locations "
559 "for client authentication");
560 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
564 ca_list = ssl_init_FindCAList(s, ptemp,
565 mctx->auth.ca_cert_file,
566 mctx->auth.ca_cert_path);
568 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
569 "Unable to determine list of available "
570 "CA certificates for client authentication");
574 SSL_CTX_set_client_CA_list(ctx, (STACK *)ca_list);
578 * Give a warning when no CAs were configured but client authentication
579 * should take place. This cannot work.
581 if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
582 ca_list = (STACK_OF(X509_NAME) *)SSL_CTX_get_client_CA_list(ctx);
584 if (sk_X509_NAME_num(ca_list) == 0) {
585 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
586 "Init: Oops, you want to request client "
587 "authentication, but no CAs are known for "
588 "verification!? [Hint: SSLCACertificate*]");
593 static void ssl_init_ctx_cipher_suite(server_rec *s,
598 SSL_CTX *ctx = mctx->ssl_ctx;
599 const char *suite = mctx->auth.cipher_suite;
602 * Configure SSL Cipher Suite
608 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
609 "Configuring permitted SSL ciphers [%s]",
612 if (!SSL_CTX_set_cipher_list(ctx, MODSSL_PCHAR_CAST suite)) {
613 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
614 "Unable to configure permitted SSL ciphers");
615 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
620 static void ssl_init_ctx_crl(server_rec *s,
626 * Configure Certificate Revocation List (CRL) Details
629 if (!(mctx->crl_file || mctx->crl_path)) {
633 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
634 "Configuring certificate revocation facility");
637 SSL_X509_STORE_create((char *)mctx->crl_file,
638 (char *)mctx->crl_path);
641 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
642 "Unable to configure X.509 CRL storage "
643 "for certificate revocation");
644 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
649 static void ssl_init_ctx_cert_chain(server_rec *s,
654 BOOL skip_first = FALSE;
656 const char *chain = mctx->cert_chain;
659 * Optionally configure extra server certificate chain certificates.
660 * This is usually done by OpenSSL automatically when one of the
661 * server cert issuers are found under SSLCACertificatePath or in
662 * SSLCACertificateFile. But because these are intended for client
663 * authentication it can conflict. For instance when you use a
664 * Global ID server certificate you've to send out the intermediate
665 * CA certificate, too. When you would just configure this with
666 * SSLCACertificateFile and also use client authentication mod_ssl
667 * would accept all clients also issued by this CA. Obviously this
668 * isn't what we want in this situation. So this feature here exists
669 * to allow one to explicity configure CA certificates which are
670 * used only for the server certificate chain.
676 for (i = 0; (i < SSL_AIDX_MAX) && mctx->pks->cert_files[i]; i++) {
677 if (strEQ(mctx->pks->cert_files[i], chain)) {
683 n = SSL_CTX_use_certificate_chain(mctx->ssl_ctx,
687 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
688 "Failed to configure CA certificate chain!");
692 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
693 "Configuring server certificate chain "
694 "(%d CA certificate%s)",
695 n, n == 1 ? "" : "s");
698 static void ssl_init_ctx(server_rec *s,
703 ssl_init_ctx_protocol(s, p, ptemp, mctx);
705 ssl_init_ctx_session_cache(s, p, ptemp, mctx);
707 ssl_init_ctx_callbacks(s, p, ptemp, mctx);
709 ssl_init_ctx_verify(s, p, ptemp, mctx);
711 ssl_init_ctx_cipher_suite(s, p, ptemp, mctx);
713 ssl_init_ctx_crl(s, p, ptemp, mctx);
716 /* XXX: proxy support? */
717 ssl_init_ctx_cert_chain(s, p, ptemp, mctx);
721 static int ssl_server_import_cert(server_rec *s,
726 SSLModConfigRec *mc = myModConfig(s);
729 const char *type = ssl_asn1_keystr(idx);
732 if (!(asn1 = ssl_asn1_table_get(mc->tPublicCert, id))) {
736 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
737 "Configuring %s server certificate", type);
740 if (!(cert = d2i_X509(NULL, &ptr, asn1->nData))) {
741 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
742 "Unable to import %s server certificate", type);
743 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
747 if (SSL_CTX_use_certificate(mctx->ssl_ctx, cert) <= 0) {
748 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
749 "Unable to configure %s server certificate", type);
750 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
754 mctx->pks->certs[idx] = cert;
759 static int ssl_server_import_key(server_rec *s,
764 SSLModConfigRec *mc = myModConfig(s);
767 const char *type = ssl_asn1_keystr(idx);
768 int pkey_type = (idx == SSL_AIDX_RSA) ? EVP_PKEY_RSA : EVP_PKEY_DSA;
771 if (!(asn1 = ssl_asn1_table_get(mc->tPrivateKey, id))) {
775 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
776 "Configuring %s server private key", type);
779 if (!(pkey = d2i_PrivateKey(pkey_type, NULL, &ptr, asn1->nData)))
781 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
782 "Unable to import %s server private key", type);
783 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
787 if (SSL_CTX_use_PrivateKey(mctx->ssl_ctx, pkey) <= 0) {
788 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
789 "Unable to configure %s server private key", type);
790 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
795 * XXX: wonder if this is still needed, this is old todo doc.
796 * (see http://www.psy.uq.edu.au/~ftp/Crypto/ssleay/TODO.html)
798 if ((pkey_type == EVP_PKEY_DSA) && mctx->pks->certs[idx]) {
799 EVP_PKEY *pubkey = X509_get_pubkey(mctx->pks->certs[idx]);
801 if (pubkey && EVP_PKEY_missing_parameters(pubkey)) {
802 EVP_PKEY_copy_parameters(pubkey, pkey);
803 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
804 "Copying DSA parameters from private key to certificate");
805 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
806 EVP_PKEY_free(pubkey);
810 mctx->pks->keys[idx] = pkey;
815 static void ssl_check_public_cert(server_rec *s,
828 * Some information about the certificate(s)
831 if (SSL_X509_isSGC(cert)) {
832 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
833 "%s server certificate enables "
834 "Server Gated Cryptography (SGC)",
835 ssl_asn1_keystr(type));
838 if (SSL_X509_getBC(cert, &is_ca, &pathlen)) {
840 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
841 "%s server certificate is a CA certificate "
842 "(BasicConstraints: CA == TRUE !?)",
843 ssl_asn1_keystr(type));
847 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
848 "%s server certificate is not a leaf certificate "
849 "(BasicConstraints: pathlen == %d > 0 !?)",
850 ssl_asn1_keystr(type), pathlen);
854 if (SSL_X509_getCN(ptemp, cert, &cn)) {
855 int fnm_flags = APR_FNM_PERIOD|APR_FNM_CASE_BLIND;
857 if (apr_fnmatch_test(cn) &&
858 (apr_fnmatch(cn, s->server_hostname,
859 fnm_flags) == APR_FNM_NOMATCH))
861 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
862 "%s server certificate wildcard CommonName (CN) `%s' "
863 "does NOT match server name!?",
864 ssl_asn1_keystr(type), cn);
866 else if (strNE(s->server_hostname, cn)) {
867 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
868 "%s server certificate CommonName (CN) `%s' "
869 "does NOT match server name!?",
870 ssl_asn1_keystr(type), cn);
875 static void ssl_init_server_certs(server_rec *s,
880 const char *rsa_id, *dsa_id;
881 const char *vhost_id = mctx->sc->vhost_id;
883 int have_rsa, have_dsa;
885 rsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_RSA);
886 dsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_DSA);
888 have_rsa = ssl_server_import_cert(s, mctx, rsa_id, SSL_AIDX_RSA);
889 have_dsa = ssl_server_import_cert(s, mctx, dsa_id, SSL_AIDX_DSA);
891 if (!(have_rsa || have_dsa)) {
892 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
893 "Oops, no RSA or DSA server certificate found?!");
897 for (i = 0; i < SSL_AIDX_MAX; i++) {
898 ssl_check_public_cert(s, ptemp, mctx->pks->certs[i], i);
901 have_rsa = ssl_server_import_key(s, mctx, rsa_id, SSL_AIDX_RSA);
902 have_dsa = ssl_server_import_key(s, mctx, dsa_id, SSL_AIDX_DSA);
904 if (!(have_rsa || have_dsa)) {
905 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
906 "Oops, no RSA or DSA server private key found?!");
911 static void ssl_init_proxy_certs(server_rec *s,
917 STACK_OF(X509_INFO) *sk;
918 modssl_pk_proxy_t *pkp = mctx->pkp;
920 SSL_CTX_set_client_cert_cb(mctx->ssl_ctx,
921 ssl_callback_proxy_cert);
923 if (!(pkp->cert_file || pkp->cert_path)) {
927 sk = sk_X509_INFO_new_null();
929 if (pkp->cert_file) {
930 SSL_X509_INFO_load_file(ptemp, sk, pkp->cert_file);
933 if (pkp->cert_path) {
934 SSL_X509_INFO_load_path(ptemp, sk, pkp->cert_path);
937 if ((ncerts = sk_X509_INFO_num(sk)) > 0) {
938 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
939 "loaded %d client certs for SSL proxy",
945 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
946 "no client certs found for SSL proxy");
947 sk_X509_INFO_free(sk);
951 static void ssl_init_proxy_ctx(server_rec *s,
956 ssl_init_ctx(s, p, ptemp, sc->proxy);
958 ssl_init_proxy_certs(s, p, ptemp, sc->proxy);
961 static void ssl_init_server_ctx(server_rec *s,
966 ssl_init_server_check(s, p, ptemp, sc->server);
968 ssl_init_ctx(s, p, ptemp, sc->server);
970 ssl_init_server_certs(s, p, ptemp, sc->server);
974 * Configure a particular server
976 void ssl_init_ConfigureServer(server_rec *s,
981 /* A bit of a hack, but initialize the server if SSL is optional or
985 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
986 "Configuring server for SSL protocol");
987 ssl_init_server_ctx(s, p, ptemp, sc);
990 if (sc->proxy_enabled) {
991 ssl_init_proxy_ctx(s, p, ptemp, sc);
995 void ssl_init_CheckServers(server_rec *base_server, apr_pool_t *p)
1003 BOOL conflict = FALSE;
1006 * Give out warnings when a server has HTTPS configured
1007 * for the HTTP port or vice versa
1009 for (s = base_server; s; s = s->next) {
1010 sc = mySrvConfig(s);
1012 if ((sc->enabled == TRUE) && (s->port == DEFAULT_HTTP_PORT)) {
1013 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1015 "Init: (%s) You configured HTTPS(%d) "
1016 "on the standard HTTP(%d) port!",
1017 ssl_util_vhostid(p, s),
1018 DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT);
1021 if (!sc->enabled && (s->port == DEFAULT_HTTPS_PORT)) {
1022 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1024 "Init: (%s) You configured HTTP(%d) "
1025 "on the standard HTTPS(%d) port!",
1026 ssl_util_vhostid(p, s),
1027 DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT);
1032 * Give out warnings when more than one SSL-aware virtual server uses the
1033 * same IP:port. This doesn't work because mod_ssl then will always use
1034 * just the certificate/keys of one virtual host (which one cannot be said
1035 * easily - but that doesn't matter here).
1037 table = apr_hash_make(p);
1039 for (s = base_server; s; s = s->next) {
1040 sc = mySrvConfig(s);
1042 if (!(sc->enabled && s->addrs)) {
1046 key = apr_psprintf(p, "%pA:%u",
1047 &s->addrs->host_addr, s->addrs->host_port);
1050 if ((ps = (server_rec *)apr_hash_get(table, key, klen))) {
1051 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1053 "Init: SSL server IP/port conflict: "
1054 "%s (%s:%d) vs. %s (%s:%d)",
1055 ssl_util_vhostid(p, s),
1056 (s->defn_name ? s->defn_name : "unknown"),
1057 s->defn_line_number,
1058 ssl_util_vhostid(p, ps),
1059 (ps->defn_name ? ps->defn_name : "unknown"),
1060 ps->defn_line_number);
1065 apr_hash_set(table, key, klen, s);
1069 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, base_server,
1070 "Init: You should not use name-based "
1071 "virtual hosts in conjunction with SSL!!");
1075 #ifdef SSLC_VERSION_NUMBER
1076 static int ssl_init_FindCAList_X509NameCmp(char **a, char **b)
1078 return(X509_NAME_cmp((void*)*a, (void*)*b));
1081 static int ssl_init_FindCAList_X509NameCmp(X509_NAME **a, X509_NAME **b)
1083 return(X509_NAME_cmp(*a, *b));
1087 static void ssl_init_PushCAList(STACK_OF(X509_NAME) *ca_list,
1088 server_rec *s, const char *file)
1091 STACK_OF(X509_NAME) *sk;
1093 sk = (STACK_OF(X509_NAME) *)
1094 SSL_load_client_CA_file(MODSSL_PCHAR_CAST file);
1100 for (n = 0; n < sk_X509_NAME_num(sk); n++) {
1102 X509_NAME *name = sk_X509_NAME_value(sk, n);
1104 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1105 "CA certificate: %s",
1106 X509_NAME_oneline(name, name_buf, sizeof(name_buf)));
1109 * note that SSL_load_client_CA_file() checks for duplicates,
1110 * but since we call it multiple times when reading a directory
1111 * we must also check for duplicates ourselves.
1114 if (sk_X509_NAME_find(ca_list, name) < 0) {
1115 /* this will be freed when ca_list is */
1116 sk_X509_NAME_push(ca_list, name);
1119 /* need to free this ourselves, else it will leak */
1120 X509_NAME_free(name);
1124 sk_X509_NAME_free(sk);
1127 STACK_OF(X509_NAME) *ssl_init_FindCAList(server_rec *s,
1129 const char *ca_file,
1130 const char *ca_path)
1132 STACK_OF(X509_NAME) *ca_list;
1135 * Start with a empty stack/list where new
1136 * entries get added in sorted order.
1138 ca_list = sk_X509_NAME_new(ssl_init_FindCAList_X509NameCmp);
1141 * Process CA certificate bundle file
1144 ssl_init_PushCAList(ca_list, s, ca_file);
1148 * Process CA certificate path files
1152 apr_finfo_t direntry;
1153 apr_int32_t finfo_flags = APR_FINFO_TYPE|APR_FINFO_NAME;
1156 if ((rv = apr_dir_open(&dir, ca_path, ptemp)) != APR_SUCCESS) {
1157 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
1158 "Failed to open SSLCACertificatePath `%s'",
1163 while ((apr_dir_read(&direntry, finfo_flags, dir)) == APR_SUCCESS) {
1165 if (direntry.filetype == APR_DIR) {
1166 continue; /* don't try to load directories */
1168 file = apr_pstrcat(ptemp, ca_path, "/", direntry.name, NULL);
1169 ssl_init_PushCAList(ca_list, s, file);
1178 sk_X509_NAME_set_cmp_func(ca_list, NULL);
1183 void ssl_init_Child(apr_pool_t *p, server_rec *s)
1185 SSLModConfigRec *mc = myModConfig(s);
1186 mc->pid = getpid(); /* only call getpid() once per-process */
1188 /* XXX: there should be an ap_srand() function */
1189 srand((unsigned int)time(NULL));
1191 /* open the mutex lockfile */
1192 ssl_mutex_reinit(s, p);
1195 #define MODSSL_CFG_ITEM_FREE(func, item) \
1201 static void ssl_init_ctx_cleanup(modssl_ctx_t *mctx)
1203 MODSSL_CFG_ITEM_FREE(X509_STORE_free, mctx->crl);
1205 MODSSL_CFG_ITEM_FREE(SSL_CTX_free, mctx->ssl_ctx);
1208 static void ssl_init_ctx_cleanup_proxy(modssl_ctx_t *mctx)
1210 ssl_init_ctx_cleanup(mctx);
1212 if (mctx->pkp->certs) {
1213 sk_X509_INFO_pop_free(mctx->pkp->certs, X509_INFO_free);
1217 static void ssl_init_ctx_cleanup_server(modssl_ctx_t *mctx)
1221 ssl_init_ctx_cleanup(mctx);
1223 for (i=0; i < SSL_AIDX_MAX; i++) {
1224 MODSSL_CFG_ITEM_FREE(X509_free,
1225 mctx->pks->certs[i]);
1227 MODSSL_CFG_ITEM_FREE(EVP_PKEY_free,
1228 mctx->pks->keys[i]);
1232 apr_status_t ssl_init_ModuleKill(void *data)
1234 SSLSrvConfigRec *sc;
1235 server_rec *base_server = (server_rec *)data;
1239 * Drop the session cache and mutex
1241 ssl_scache_kill(base_server);
1244 * Destroy the temporary keys and params
1246 ssl_tmp_keys_free(base_server);
1249 * Free the non-pool allocated structures
1250 * in the per-server configurations
1252 for (s = base_server; s; s = s->next) {
1253 sc = mySrvConfig(s);
1255 ssl_init_ctx_cleanup_proxy(sc->proxy);
1257 ssl_init_ctx_cleanup_server(sc->server);