1 /* Copyright 2001-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * _ __ ___ ___ __| | ___ ___| | mod_ssl
18 * | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
19 * | | | | | | (_) | (_| | \__ \__ \ |
20 * |_| |_| |_|\___/ \__,_|___|___/___/_|
23 * Initialization of Servers
28 #include "ssl_private.h"
30 /* _________________________________________________________________
32 ** Module Initialization
33 ** _________________________________________________________________
36 static char *ssl_add_version_component(apr_pool_t *p,
40 char *val = ssl_var_lookup(p, s, NULL, NULL, name);
43 ap_add_version_component(p, val);
49 static char *version_components[] = {
50 "SSL_VERSION_PRODUCT",
51 "SSL_VERSION_INTERFACE",
52 "SSL_VERSION_LIBRARY",
56 static void ssl_add_version_components(apr_pool_t *p,
59 char *vals[sizeof(version_components)/sizeof(char *)];
62 for (i=0; version_components[i]; i++) {
63 vals[i] = ssl_add_version_component(p, s,
64 version_components[i]);
67 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
68 "Server: %s, Interface: %s, Library: %s",
69 AP_SERVER_BASEVERSION,
70 vals[1], /* SSL_VERSION_INTERFACE */
71 vals[2]); /* SSL_VERSION_LIBRARY */
76 * Handle the Temporary RSA Keys and DH Params
79 #define MODSSL_TMP_KEY_FREE(mc, type, idx) \
80 if (mc->pTmpKeys[idx]) { \
81 type##_free((type *)mc->pTmpKeys[idx]); \
82 mc->pTmpKeys[idx] = NULL; \
85 #define MODSSL_TMP_KEYS_FREE(mc, type) \
86 MODSSL_TMP_KEY_FREE(mc, type, SSL_TMP_KEY_##type##_512); \
87 MODSSL_TMP_KEY_FREE(mc, type, SSL_TMP_KEY_##type##_1024)
89 static void ssl_tmp_keys_free(server_rec *s)
91 SSLModConfigRec *mc = myModConfig(s);
93 MODSSL_TMP_KEYS_FREE(mc, RSA);
94 MODSSL_TMP_KEYS_FREE(mc, DH);
97 static int ssl_tmp_key_init_rsa(server_rec *s,
100 SSLModConfigRec *mc = myModConfig(s);
102 if (!(mc->pTmpKeys[idx] =
103 RSA_generate_key(bits, RSA_F4, NULL, NULL)))
105 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
106 "Init: Failed to generate temporary "
107 "%d bit RSA private key", bits);
114 static int ssl_tmp_key_init_dh(server_rec *s,
117 SSLModConfigRec *mc = myModConfig(s);
119 if (!(mc->pTmpKeys[idx] =
120 ssl_dh_GetTmpParam(bits)))
122 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
123 "Init: Failed to generate temporary "
124 "%d bit DH parameters", bits);
131 #define MODSSL_TMP_KEY_INIT_RSA(s, bits) \
132 ssl_tmp_key_init_rsa(s, bits, SSL_TMP_KEY_RSA_##bits)
134 #define MODSSL_TMP_KEY_INIT_DH(s, bits) \
135 ssl_tmp_key_init_dh(s, bits, SSL_TMP_KEY_DH_##bits)
137 static int ssl_tmp_keys_init(server_rec *s)
139 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
140 "Init: Generating temporary RSA private keys (512/1024 bits)");
142 if (MODSSL_TMP_KEY_INIT_RSA(s, 512) ||
143 MODSSL_TMP_KEY_INIT_RSA(s, 1024)) {
147 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
148 "Init: Generating temporary DH parameters (512/1024 bits)");
150 if (MODSSL_TMP_KEY_INIT_DH(s, 512) ||
151 MODSSL_TMP_KEY_INIT_DH(s, 1024)) {
159 * Per-module initialization
161 int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
163 server_rec *base_server)
165 SSLModConfigRec *mc = myModConfig(base_server);
169 /* We initialize mc->pid per-process in the child init,
170 * but it should be initialized for startup before we
171 * call ssl_rand_seed() below.
176 * Let us cleanup on restarts and exists
178 apr_pool_cleanup_register(p, base_server,
180 apr_pool_cleanup_null);
183 * Any init round fixes the global config
185 ssl_config_global_create(base_server); /* just to avoid problems */
186 ssl_config_global_fix(mc);
189 * try to fix the configuration and open the dedicated SSL
190 * logfile as early as possible
192 for (s = base_server; s; s = s->next) {
204 * Create the server host:port string because we need it a lot
206 sc->vhost_id = ssl_util_vhostid(p, s);
207 sc->vhost_id_len = strlen(sc->vhost_id);
209 /* If sc->enabled is UNSET, then SSL is optional on this vhost */
210 /* Fix up stuff that may not have been set */
211 if (sc->enabled == SSL_ENABLED_UNSET) {
212 sc->enabled = SSL_ENABLED_FALSE;
214 if (sc->proxy_enabled == UNSET) {
215 sc->proxy_enabled = FALSE;
218 if (sc->session_cache_timeout == UNSET) {
219 sc->session_cache_timeout = SSL_SESSION_CACHE_TIMEOUT;
222 if (sc->server->pphrase_dialog_type == SSL_PPTYPE_UNSET) {
223 sc->server->pphrase_dialog_type = SSL_PPTYPE_BUILTIN;
229 * SSL external crypto device ("engine") support
231 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
232 ssl_init_Engine(base_server, p);
236 ssl_util_thread_setup(p);
239 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
240 "Init: Initialized %s library", SSL_LIBRARY_NAME);
243 * Seed the Pseudo Random Number Generator (PRNG)
244 * only need ptemp here; nothing inside allocated from the pool
245 * needs to live once we return from ssl_rand_seed().
247 ssl_rand_seed(base_server, ptemp, SSL_RSCTX_STARTUP, "Init: ");
250 * read server private keys/public certs into memory.
251 * decrypting any encrypted keys via configured SSLPassPhraseDialogs
252 * anything that needs to live longer than ptemp needs to also survive
253 * restarts, in which case they'll live inside s->process->pool.
255 ssl_pphrase_Handle(base_server, ptemp);
257 if (ssl_tmp_keys_init(base_server)) {
262 * initialize the mutex handling
264 if (!ssl_mutex_init(base_server, p)) {
265 return HTTP_INTERNAL_SERVER_ERROR;
269 * initialize session caching
271 ssl_scache_init(base_server, p);
276 ap_log_error(APLOG_MARK, APLOG_INFO, 0, base_server,
277 "Init: Initializing (virtual) servers for SSL");
279 for (s = base_server; s; s = s->next) {
282 * Either now skip this server when SSL is disabled for
283 * it or give out some information about what we're
288 * Read the server certificate and key
290 ssl_init_ConfigureServer(s, p, ptemp, sc);
294 * Configuration consistency checks
296 ssl_init_CheckServers(base_server, ptemp);
299 * Announce mod_ssl and SSL library in HTTP Server field
300 * as ``mod_ssl/X.X.X OpenSSL/X.X.X''
302 ssl_add_version_components(p, base_server);
304 SSL_init_app_data2_idx(); /* for SSL_get_app_data2() at request time */
310 * Support for external a Crypto Device ("engine"), usually
311 * a hardware accellerator card for crypto operations.
313 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
314 void ssl_init_Engine(server_rec *s, apr_pool_t *p)
316 SSLModConfigRec *mc = myModConfig(s);
319 if (mc->szCryptoDevice) {
320 if (!(e = ENGINE_by_id(mc->szCryptoDevice))) {
321 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
322 "Init: Failed to load Crypto Device API `%s'",
324 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
328 if (strEQ(mc->szCryptoDevice, "chil")) {
329 ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
332 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
333 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
334 "Init: Failed to enable Crypto Device API `%s'",
336 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
345 static void ssl_init_server_check(server_rec *s,
351 * check for important parameters and the
352 * possibility that the user forgot to set them.
354 if (!mctx->pks->cert_files[0]) {
355 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
356 "No SSL Certificate set [hint: SSLCertificateFile]");
361 * Check for problematic re-initializations
363 if (mctx->pks->certs[SSL_AIDX_RSA] ||
364 mctx->pks->certs[SSL_AIDX_DSA])
366 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
367 "Illegal attempt to re-initialise SSL for server "
368 "(theoretically shouldn't happen!)");
373 static void ssl_init_ctx_protocol(server_rec *s,
379 SSL_METHOD *method = NULL;
381 int protocol = mctx->protocol;
384 * Create the new per-server SSL context
386 if (protocol == SSL_PROTOCOL_NONE) {
387 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
388 "No SSL protocols available [hint: SSLProtocol]");
393 (protocol & SSL_PROTOCOL_SSLV2 ? "SSLv2, " : ""),
394 (protocol & SSL_PROTOCOL_SSLV3 ? "SSLv3, " : ""),
395 (protocol & SSL_PROTOCOL_TLSV1 ? "TLSv1, " : ""),
397 cp[strlen(cp)-2] = NUL;
399 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
400 "Creating new SSL context (protocols: %s)", cp);
402 if (protocol == SSL_PROTOCOL_SSLV2) {
404 SSLv2_client_method() : /* proxy */
405 SSLv2_server_method(); /* server */
406 ctx = SSL_CTX_new(method); /* only SSLv2 is left */
410 SSLv23_client_method() : /* proxy */
411 SSLv23_server_method(); /* server */
412 ctx = SSL_CTX_new(method); /* be more flexible */
417 SSL_CTX_set_options(ctx, SSL_OP_ALL);
419 if (!(protocol & SSL_PROTOCOL_SSLV2)) {
420 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
423 if (!(protocol & SSL_PROTOCOL_SSLV3)) {
424 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
427 if (!(protocol & SSL_PROTOCOL_TLSV1)) {
428 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
431 SSL_CTX_set_app_data(ctx, s);
434 * Configure additional context ingredients
436 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
439 static void ssl_init_ctx_session_cache(server_rec *s,
444 SSL_CTX *ctx = mctx->ssl_ctx;
445 SSLModConfigRec *mc = myModConfig(s);
446 long cache_mode = SSL_SESS_CACHE_OFF;
448 if (mc->nSessionCacheMode != SSL_SCMODE_NONE) {
449 /* SSL_SESS_CACHE_NO_INTERNAL will force OpenSSL
450 * to ignore process local-caching and
451 * to always get/set/delete sessions using mod_ssl's callbacks.
453 cache_mode = SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL;
456 SSL_CTX_set_session_cache_mode(ctx, cache_mode);
458 SSL_CTX_sess_set_new_cb(ctx, ssl_callback_NewSessionCacheEntry);
459 SSL_CTX_sess_set_get_cb(ctx, ssl_callback_GetSessionCacheEntry);
460 SSL_CTX_sess_set_remove_cb(ctx, ssl_callback_DelSessionCacheEntry);
463 static void ssl_init_ctx_callbacks(server_rec *s,
468 SSL_CTX *ctx = mctx->ssl_ctx;
470 SSL_CTX_set_tmp_rsa_callback(ctx, ssl_callback_TmpRSA);
471 SSL_CTX_set_tmp_dh_callback(ctx, ssl_callback_TmpDH);
473 if (s->loglevel >= APLOG_DEBUG) {
474 /* this callback only logs if LogLevel >= info */
475 SSL_CTX_set_info_callback(ctx, ssl_callback_LogTracingState);
479 static void ssl_init_ctx_verify(server_rec *s,
484 SSL_CTX *ctx = mctx->ssl_ctx;
486 int verify = SSL_VERIFY_NONE;
487 STACK_OF(X509_NAME) *ca_list;
489 if (mctx->auth.verify_mode == SSL_CVERIFY_UNSET) {
490 mctx->auth.verify_mode = SSL_CVERIFY_NONE;
493 if (mctx->auth.verify_depth == UNSET) {
494 mctx->auth.verify_depth = 1;
498 * Configure callbacks for SSL context
500 if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
501 verify |= SSL_VERIFY_PEER_STRICT;
504 if ((mctx->auth.verify_mode == SSL_CVERIFY_OPTIONAL) ||
505 (mctx->auth.verify_mode == SSL_CVERIFY_OPTIONAL_NO_CA))
507 verify |= SSL_VERIFY_PEER;
510 SSL_CTX_set_verify(ctx, verify, ssl_callback_SSLVerify);
513 * Configure Client Authentication details
515 if (mctx->auth.ca_cert_file || mctx->auth.ca_cert_path) {
516 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
517 "Configuring client authentication");
519 if (!SSL_CTX_load_verify_locations(ctx,
520 MODSSL_PCHAR_CAST mctx->auth.ca_cert_file,
521 MODSSL_PCHAR_CAST mctx->auth.ca_cert_path))
523 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
524 "Unable to configure verify locations "
525 "for client authentication");
526 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
530 ca_list = ssl_init_FindCAList(s, ptemp,
531 mctx->auth.ca_cert_file,
532 mctx->auth.ca_cert_path);
534 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
535 "Unable to determine list of available "
536 "CA certificates for client authentication");
540 SSL_CTX_set_client_CA_list(ctx, (STACK *)ca_list);
544 * Give a warning when no CAs were configured but client authentication
545 * should take place. This cannot work.
547 if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
548 ca_list = (STACK_OF(X509_NAME) *)SSL_CTX_get_client_CA_list(ctx);
550 if (sk_X509_NAME_num(ca_list) == 0) {
551 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
552 "Init: Oops, you want to request client "
553 "authentication, but no CAs are known for "
554 "verification!? [Hint: SSLCACertificate*]");
559 static void ssl_init_ctx_cipher_suite(server_rec *s,
564 SSL_CTX *ctx = mctx->ssl_ctx;
565 const char *suite = mctx->auth.cipher_suite;
568 * Configure SSL Cipher Suite
574 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
575 "Configuring permitted SSL ciphers [%s]",
578 if (!SSL_CTX_set_cipher_list(ctx, MODSSL_PCHAR_CAST suite)) {
579 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
580 "Unable to configure permitted SSL ciphers");
581 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
586 static void ssl_init_ctx_crl(server_rec *s,
592 * Configure Certificate Revocation List (CRL) Details
595 if (!(mctx->crl_file || mctx->crl_path)) {
599 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
600 "Configuring certificate revocation facility");
603 SSL_X509_STORE_create((char *)mctx->crl_file,
604 (char *)mctx->crl_path);
607 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
608 "Unable to configure X.509 CRL storage "
609 "for certificate revocation");
610 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
615 static void ssl_init_ctx_cert_chain(server_rec *s,
620 BOOL skip_first = FALSE;
622 const char *chain = mctx->cert_chain;
625 * Optionally configure extra server certificate chain certificates.
626 * This is usually done by OpenSSL automatically when one of the
627 * server cert issuers are found under SSLCACertificatePath or in
628 * SSLCACertificateFile. But because these are intended for client
629 * authentication it can conflict. For instance when you use a
630 * Global ID server certificate you've to send out the intermediate
631 * CA certificate, too. When you would just configure this with
632 * SSLCACertificateFile and also use client authentication mod_ssl
633 * would accept all clients also issued by this CA. Obviously this
634 * isn't what we want in this situation. So this feature here exists
635 * to allow one to explicity configure CA certificates which are
636 * used only for the server certificate chain.
642 for (i = 0; (i < SSL_AIDX_MAX) && mctx->pks->cert_files[i]; i++) {
643 if (strEQ(mctx->pks->cert_files[i], chain)) {
649 n = SSL_CTX_use_certificate_chain(mctx->ssl_ctx,
653 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
654 "Failed to configure CA certificate chain!");
658 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
659 "Configuring server certificate chain "
660 "(%d CA certificate%s)",
661 n, n == 1 ? "" : "s");
664 static void ssl_init_ctx(server_rec *s,
669 ssl_init_ctx_protocol(s, p, ptemp, mctx);
671 ssl_init_ctx_session_cache(s, p, ptemp, mctx);
673 ssl_init_ctx_callbacks(s, p, ptemp, mctx);
675 ssl_init_ctx_verify(s, p, ptemp, mctx);
677 ssl_init_ctx_cipher_suite(s, p, ptemp, mctx);
679 ssl_init_ctx_crl(s, p, ptemp, mctx);
682 /* XXX: proxy support? */
683 ssl_init_ctx_cert_chain(s, p, ptemp, mctx);
687 static int ssl_server_import_cert(server_rec *s,
692 SSLModConfigRec *mc = myModConfig(s);
695 const char *type = ssl_asn1_keystr(idx);
698 if (!(asn1 = ssl_asn1_table_get(mc->tPublicCert, id))) {
702 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
703 "Configuring %s server certificate", type);
706 if (!(cert = d2i_X509(NULL, &ptr, asn1->nData))) {
707 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
708 "Unable to import %s server certificate", type);
709 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
713 if (SSL_CTX_use_certificate(mctx->ssl_ctx, cert) <= 0) {
714 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
715 "Unable to configure %s server certificate", type);
716 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
720 mctx->pks->certs[idx] = cert;
725 static int ssl_server_import_key(server_rec *s,
730 SSLModConfigRec *mc = myModConfig(s);
733 const char *type = ssl_asn1_keystr(idx);
734 int pkey_type = (idx == SSL_AIDX_RSA) ? EVP_PKEY_RSA : EVP_PKEY_DSA;
737 if (!(asn1 = ssl_asn1_table_get(mc->tPrivateKey, id))) {
741 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
742 "Configuring %s server private key", type);
745 if (!(pkey = d2i_PrivateKey(pkey_type, NULL, &ptr, asn1->nData)))
747 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
748 "Unable to import %s server private key", type);
749 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
753 if (SSL_CTX_use_PrivateKey(mctx->ssl_ctx, pkey) <= 0) {
754 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
755 "Unable to configure %s server private key", type);
756 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
761 * XXX: wonder if this is still needed, this is old todo doc.
762 * (see http://www.psy.uq.edu.au/~ftp/Crypto/ssleay/TODO.html)
764 if ((pkey_type == EVP_PKEY_DSA) && mctx->pks->certs[idx]) {
765 EVP_PKEY *pubkey = X509_get_pubkey(mctx->pks->certs[idx]);
767 if (pubkey && EVP_PKEY_missing_parameters(pubkey)) {
768 EVP_PKEY_copy_parameters(pubkey, pkey);
769 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
770 "Copying DSA parameters from private key to certificate");
771 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
772 EVP_PKEY_free(pubkey);
776 mctx->pks->keys[idx] = pkey;
781 static void ssl_check_public_cert(server_rec *s,
794 * Some information about the certificate(s)
797 if (SSL_X509_isSGC(cert)) {
798 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
799 "%s server certificate enables "
800 "Server Gated Cryptography (SGC)",
801 ssl_asn1_keystr(type));
804 if (SSL_X509_getBC(cert, &is_ca, &pathlen)) {
806 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
807 "%s server certificate is a CA certificate "
808 "(BasicConstraints: CA == TRUE !?)",
809 ssl_asn1_keystr(type));
813 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
814 "%s server certificate is not a leaf certificate "
815 "(BasicConstraints: pathlen == %d > 0 !?)",
816 ssl_asn1_keystr(type), pathlen);
820 if (SSL_X509_getCN(ptemp, cert, &cn)) {
821 int fnm_flags = APR_FNM_PERIOD|APR_FNM_CASE_BLIND;
823 if (apr_fnmatch_test(cn) &&
824 (apr_fnmatch(cn, s->server_hostname,
825 fnm_flags) == APR_FNM_NOMATCH))
827 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
828 "%s server certificate wildcard CommonName (CN) `%s' "
829 "does NOT match server name!?",
830 ssl_asn1_keystr(type), cn);
832 else if (strNE(s->server_hostname, cn)) {
833 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
834 "%s server certificate CommonName (CN) `%s' "
835 "does NOT match server name!?",
836 ssl_asn1_keystr(type), cn);
841 static void ssl_init_server_certs(server_rec *s,
846 const char *rsa_id, *dsa_id;
847 const char *vhost_id = mctx->sc->vhost_id;
849 int have_rsa, have_dsa;
851 rsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_RSA);
852 dsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_DSA);
854 have_rsa = ssl_server_import_cert(s, mctx, rsa_id, SSL_AIDX_RSA);
855 have_dsa = ssl_server_import_cert(s, mctx, dsa_id, SSL_AIDX_DSA);
857 if (!(have_rsa || have_dsa)) {
858 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
859 "Oops, no RSA or DSA server certificate found?!");
863 for (i = 0; i < SSL_AIDX_MAX; i++) {
864 ssl_check_public_cert(s, ptemp, mctx->pks->certs[i], i);
867 have_rsa = ssl_server_import_key(s, mctx, rsa_id, SSL_AIDX_RSA);
868 have_dsa = ssl_server_import_key(s, mctx, dsa_id, SSL_AIDX_DSA);
870 if (!(have_rsa || have_dsa)) {
871 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
872 "Oops, no RSA or DSA server private key found?!");
877 static void ssl_init_proxy_certs(server_rec *s,
883 STACK_OF(X509_INFO) *sk;
884 modssl_pk_proxy_t *pkp = mctx->pkp;
886 SSL_CTX_set_client_cert_cb(mctx->ssl_ctx,
887 ssl_callback_proxy_cert);
889 if (!(pkp->cert_file || pkp->cert_path)) {
893 sk = sk_X509_INFO_new_null();
895 if (pkp->cert_file) {
896 SSL_X509_INFO_load_file(ptemp, sk, pkp->cert_file);
899 if (pkp->cert_path) {
900 SSL_X509_INFO_load_path(ptemp, sk, pkp->cert_path);
903 if ((ncerts = sk_X509_INFO_num(sk)) <= 0) {
904 sk_X509_INFO_free(sk);
905 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
906 "no client certs found for SSL proxy");
910 /* Check that all client certs have got certificates and private
912 for (n = 0; n < ncerts; n++) {
913 X509_INFO *inf = sk_X509_INFO_value(sk, n);
915 if (!inf->x509 || !inf->x_pkey) {
916 sk_X509_INFO_free(sk);
917 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s,
918 "incomplete client cert configured for SSL proxy "
919 "(missing or encrypted private key?)");
925 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
926 "loaded %d client certs for SSL proxy",
931 static void ssl_init_proxy_ctx(server_rec *s,
936 ssl_init_ctx(s, p, ptemp, sc->proxy);
938 ssl_init_proxy_certs(s, p, ptemp, sc->proxy);
941 static void ssl_init_server_ctx(server_rec *s,
946 ssl_init_server_check(s, p, ptemp, sc->server);
948 ssl_init_ctx(s, p, ptemp, sc->server);
950 ssl_init_server_certs(s, p, ptemp, sc->server);
954 * Configure a particular server
956 void ssl_init_ConfigureServer(server_rec *s,
961 /* Initialize the server if SSL is enabled or optional.
963 if ((sc->enabled == SSL_ENABLED_TRUE) || (sc->enabled == SSL_ENABLED_OPTIONAL)) {
964 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
965 "Configuring server for SSL protocol");
966 ssl_init_server_ctx(s, p, ptemp, sc);
969 if (sc->proxy_enabled) {
970 ssl_init_proxy_ctx(s, p, ptemp, sc);
974 void ssl_init_CheckServers(server_rec *base_server, apr_pool_t *p)
982 BOOL conflict = FALSE;
985 * Give out warnings when a server has HTTPS configured
986 * for the HTTP port or vice versa
988 for (s = base_server; s; s = s->next) {
991 if ((sc->enabled == SSL_ENABLED_TRUE) && (s->port == DEFAULT_HTTP_PORT)) {
992 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
994 "Init: (%s) You configured HTTPS(%d) "
995 "on the standard HTTP(%d) port!",
996 ssl_util_vhostid(p, s),
997 DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT);
1000 if ((sc->enabled == SSL_ENABLED_FALSE) && (s->port == DEFAULT_HTTPS_PORT)) {
1001 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1003 "Init: (%s) You configured HTTP(%d) "
1004 "on the standard HTTPS(%d) port!",
1005 ssl_util_vhostid(p, s),
1006 DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT);
1011 * Give out warnings when more than one SSL-aware virtual server uses the
1012 * same IP:port. This doesn't work because mod_ssl then will always use
1013 * just the certificate/keys of one virtual host (which one cannot be said
1014 * easily - but that doesn't matter here).
1016 table = apr_hash_make(p);
1018 for (s = base_server; s; s = s->next) {
1019 sc = mySrvConfig(s);
1021 if (!((sc->enabled == SSL_ENABLED_TRUE) && s->addrs)) {
1025 key = apr_psprintf(p, "%pA:%u",
1026 &s->addrs->host_addr, s->addrs->host_port);
1029 if ((ps = (server_rec *)apr_hash_get(table, key, klen))) {
1030 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1032 "Init: SSL server IP/port conflict: "
1033 "%s (%s:%d) vs. %s (%s:%d)",
1034 ssl_util_vhostid(p, s),
1035 (s->defn_name ? s->defn_name : "unknown"),
1036 s->defn_line_number,
1037 ssl_util_vhostid(p, ps),
1038 (ps->defn_name ? ps->defn_name : "unknown"),
1039 ps->defn_line_number);
1044 apr_hash_set(table, key, klen, s);
1048 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, base_server,
1049 "Init: You should not use name-based "
1050 "virtual hosts in conjunction with SSL!!");
1054 #ifdef SSLC_VERSION_NUMBER
1055 static int ssl_init_FindCAList_X509NameCmp(char **a, char **b)
1057 return(X509_NAME_cmp((void*)*a, (void*)*b));
1060 static int ssl_init_FindCAList_X509NameCmp(X509_NAME **a, X509_NAME **b)
1062 return(X509_NAME_cmp(*a, *b));
1066 static void ssl_init_PushCAList(STACK_OF(X509_NAME) *ca_list,
1067 server_rec *s, const char *file)
1070 STACK_OF(X509_NAME) *sk;
1072 sk = (STACK_OF(X509_NAME) *)
1073 SSL_load_client_CA_file(MODSSL_PCHAR_CAST file);
1079 for (n = 0; n < sk_X509_NAME_num(sk); n++) {
1081 X509_NAME *name = sk_X509_NAME_value(sk, n);
1083 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1084 "CA certificate: %s",
1085 X509_NAME_oneline(name, name_buf, sizeof(name_buf)));
1088 * note that SSL_load_client_CA_file() checks for duplicates,
1089 * but since we call it multiple times when reading a directory
1090 * we must also check for duplicates ourselves.
1093 if (sk_X509_NAME_find(ca_list, name) < 0) {
1094 /* this will be freed when ca_list is */
1095 sk_X509_NAME_push(ca_list, name);
1098 /* need to free this ourselves, else it will leak */
1099 X509_NAME_free(name);
1103 sk_X509_NAME_free(sk);
1106 STACK_OF(X509_NAME) *ssl_init_FindCAList(server_rec *s,
1108 const char *ca_file,
1109 const char *ca_path)
1111 STACK_OF(X509_NAME) *ca_list;
1114 * Start with a empty stack/list where new
1115 * entries get added in sorted order.
1117 ca_list = sk_X509_NAME_new(ssl_init_FindCAList_X509NameCmp);
1120 * Process CA certificate bundle file
1123 ssl_init_PushCAList(ca_list, s, ca_file);
1127 * Process CA certificate path files
1131 apr_finfo_t direntry;
1132 apr_int32_t finfo_flags = APR_FINFO_TYPE|APR_FINFO_NAME;
1135 if ((rv = apr_dir_open(&dir, ca_path, ptemp)) != APR_SUCCESS) {
1136 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
1137 "Failed to open SSLCACertificatePath `%s'",
1142 while ((apr_dir_read(&direntry, finfo_flags, dir)) == APR_SUCCESS) {
1144 if (direntry.filetype == APR_DIR) {
1145 continue; /* don't try to load directories */
1147 file = apr_pstrcat(ptemp, ca_path, "/", direntry.name, NULL);
1148 ssl_init_PushCAList(ca_list, s, file);
1157 sk_X509_NAME_set_cmp_func(ca_list, NULL);
1162 void ssl_init_Child(apr_pool_t *p, server_rec *s)
1164 SSLModConfigRec *mc = myModConfig(s);
1165 mc->pid = getpid(); /* only call getpid() once per-process */
1167 /* XXX: there should be an ap_srand() function */
1168 srand((unsigned int)time(NULL));
1170 /* open the mutex lockfile */
1171 ssl_mutex_reinit(s, p);
1174 #define MODSSL_CFG_ITEM_FREE(func, item) \
1180 static void ssl_init_ctx_cleanup(modssl_ctx_t *mctx)
1182 MODSSL_CFG_ITEM_FREE(X509_STORE_free, mctx->crl);
1184 MODSSL_CFG_ITEM_FREE(SSL_CTX_free, mctx->ssl_ctx);
1187 static void ssl_init_ctx_cleanup_proxy(modssl_ctx_t *mctx)
1189 ssl_init_ctx_cleanup(mctx);
1191 if (mctx->pkp->certs) {
1192 sk_X509_INFO_pop_free(mctx->pkp->certs, X509_INFO_free);
1196 static void ssl_init_ctx_cleanup_server(modssl_ctx_t *mctx)
1200 ssl_init_ctx_cleanup(mctx);
1202 for (i=0; i < SSL_AIDX_MAX; i++) {
1203 MODSSL_CFG_ITEM_FREE(X509_free,
1204 mctx->pks->certs[i]);
1206 MODSSL_CFG_ITEM_FREE(EVP_PKEY_free,
1207 mctx->pks->keys[i]);
1211 apr_status_t ssl_init_ModuleKill(void *data)
1213 SSLSrvConfigRec *sc;
1214 server_rec *base_server = (server_rec *)data;
1218 * Drop the session cache and mutex
1220 ssl_scache_kill(base_server);
1223 * Destroy the temporary keys and params
1225 ssl_tmp_keys_free(base_server);
1228 * Free the non-pool allocated structures
1229 * in the per-server configurations
1231 for (s = base_server; s; s = s->next) {
1232 sc = mySrvConfig(s);
1234 ssl_init_ctx_cleanup_proxy(sc->proxy);
1236 ssl_init_ctx_cleanup_server(sc->server);