1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * _ __ ___ ___ __| | ___ ___| | mod_ssl
19 * | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
20 * | | | | | | (_) | (_| | \__ \__ \ |
21 * |_| |_| |_|\___/ \__,_|___|___/___/_|
24 * Initialization of Servers
31 /* _________________________________________________________________
33 ** Module Initialization
34 ** _________________________________________________________________
38 static void ssl_add_version_components(apr_pool_t *p,
41 char *modver = ssl_var_lookup(p, s, NULL, NULL, "SSL_VERSION_INTERFACE");
42 char *libver = ssl_var_lookup(p, s, NULL, NULL, "SSL_VERSION_LIBRARY");
43 char *incver = ssl_var_lookup(p, s, NULL, NULL,
44 "SSL_VERSION_LIBRARY_INTERFACE");
46 ap_add_version_component(p, modver);
47 ap_add_version_component(p, libver);
49 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
50 "%s compiled against Server: %s, Library: %s",
51 modver, AP_SERVER_BASEVERSION, incver);
56 * Initialize SSL library
58 static void ssl_init_SSLLibrary(server_rec *s)
60 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
61 "Init: Initializing %s library", SSL_LIBRARY_NAME);
63 SSL_load_error_strings();
65 OpenSSL_add_all_algorithms(); /* Required for eg SHA256 client certs */
69 * Handle the Temporary RSA Keys and DH Params
72 #define MODSSL_TMP_KEY_FREE(mc, type, idx) \
73 if (mc->pTmpKeys[idx]) { \
74 type##_free((type *)mc->pTmpKeys[idx]); \
75 mc->pTmpKeys[idx] = NULL; \
78 #define MODSSL_TMP_KEYS_FREE(mc, type) \
79 MODSSL_TMP_KEY_FREE(mc, type, SSL_TMP_KEY_##type##_512); \
80 MODSSL_TMP_KEY_FREE(mc, type, SSL_TMP_KEY_##type##_1024)
82 static void ssl_tmp_keys_free(server_rec *s)
84 SSLModConfigRec *mc = myModConfig(s);
86 MODSSL_TMP_KEYS_FREE(mc, RSA);
87 MODSSL_TMP_KEYS_FREE(mc, DH);
90 static int ssl_tmp_key_init_rsa(server_rec *s,
93 SSLModConfigRec *mc = myModConfig(s);
95 if (!(mc->pTmpKeys[idx] =
96 RSA_generate_key(bits, RSA_F4, NULL, NULL)))
98 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
99 "Init: Failed to generate temporary "
100 "%d bit RSA private key", bits);
107 static int ssl_tmp_key_init_dh(server_rec *s,
110 SSLModConfigRec *mc = myModConfig(s);
112 if (!(mc->pTmpKeys[idx] =
113 ssl_dh_GetTmpParam(bits)))
115 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
116 "Init: Failed to generate temporary "
117 "%d bit DH parameters", bits);
124 #define MODSSL_TMP_KEY_INIT_RSA(s, bits) \
125 ssl_tmp_key_init_rsa(s, bits, SSL_TMP_KEY_RSA_##bits)
127 #define MODSSL_TMP_KEY_INIT_DH(s, bits) \
128 ssl_tmp_key_init_dh(s, bits, SSL_TMP_KEY_DH_##bits)
130 static int ssl_tmp_keys_init(server_rec *s)
132 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
133 "Init: Generating temporary RSA private keys (512/1024 bits)");
135 if (MODSSL_TMP_KEY_INIT_RSA(s, 512) ||
136 MODSSL_TMP_KEY_INIT_RSA(s, 1024)) {
140 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
141 "Init: Generating temporary DH parameters (512/1024 bits)");
143 if (MODSSL_TMP_KEY_INIT_DH(s, 512) ||
144 MODSSL_TMP_KEY_INIT_DH(s, 1024)) {
152 * Per-module initialization
154 int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
156 server_rec *base_server)
158 SSLModConfigRec *mc = myModConfig(base_server);
163 * Let us cleanup on restarts and exists
165 apr_pool_cleanup_register(p, base_server,
167 apr_pool_cleanup_null);
170 * Any init round fixes the global config
172 ssl_config_global_create(base_server); /* just to avoid problems */
173 ssl_config_global_fix(mc);
176 * try to fix the configuration and open the dedicated SSL
177 * logfile as early as possible
179 for (s = base_server; s; s = s->next) {
191 * Create the server host:port string because we need it a lot
193 sc->vhost_id = ssl_util_vhostid(p, s);
194 sc->vhost_id_len = strlen(sc->vhost_id);
196 /* Fix up stuff that may not have been set */
197 if (sc->enabled == UNSET) {
201 if (sc->proxy_enabled == UNSET) {
202 sc->proxy_enabled = FALSE;
205 if (sc->session_cache_timeout == UNSET) {
206 sc->session_cache_timeout = SSL_SESSION_CACHE_TIMEOUT;
209 if (sc->server->pphrase_dialog_type == SSL_PPTYPE_UNSET) {
210 sc->server->pphrase_dialog_type = SSL_PPTYPE_BUILTIN;
216 ssl_util_thread_setup(p);
220 * SSL external crypto device ("engine") support
222 #ifdef SSL_EXPERIMENTAL_ENGINE
223 ssl_init_Engine(base_server, p);
226 ssl_init_SSLLibrary(base_server);
229 * Seed the Pseudo Random Number Generator (PRNG)
230 * only need ptemp here; nothing inside allocated from the pool
231 * needs to live once we return from ssl_rand_seed().
233 ssl_rand_seed(base_server, ptemp, SSL_RSCTX_STARTUP, "Init: ");
236 * read server private keys/public certs into memory.
237 * decrypting any encrypted keys via configured SSLPassPhraseDialogs
238 * anything that needs to live longer than ptemp needs to also survive
239 * restarts, in which case they'll live inside s->process->pool.
241 ssl_pphrase_Handle(base_server, ptemp);
243 if (ssl_tmp_keys_init(base_server)) {
248 * initialize the mutex handling
250 if (!ssl_mutex_init(base_server, p)) {
251 return HTTP_INTERNAL_SERVER_ERROR;
255 * initialize session caching
257 ssl_scache_init(base_server, p);
262 ap_log_error(APLOG_MARK, APLOG_INFO, 0, base_server,
263 "Init: Initializing (virtual) servers for SSL");
265 for (s = base_server; s; s = s->next) {
268 * Either now skip this server when SSL is disabled for
269 * it or give out some information about what we're
274 * Read the server certificate and key
276 ssl_init_ConfigureServer(s, p, ptemp, sc);
280 * Configuration consistency checks
282 ssl_init_CheckServers(base_server, ptemp);
285 * Announce mod_ssl and SSL library in HTTP Server field
286 * as ``mod_ssl/X.X.X OpenSSL/X.X.X''
288 ssl_add_version_components(p, base_server);
290 SSL_init_app_data2_idx(); /* for SSL_get_app_data2() at request time */
296 * Support for external a Crypto Device ("engine"), usually
297 * a hardware accellerator card for crypto operations.
299 #ifdef SSL_EXPERIMENTAL_ENGINE
300 void ssl_init_Engine(server_rec *s, apr_pool_t *p)
302 SSLModConfigRec *mc = myModConfig(s);
305 if (mc->szCryptoDevice) {
306 if (!(e = ENGINE_by_id(mc->szCryptoDevice))) {
307 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
308 "Init: Failed to load Crypto Device API `%s'",
310 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
314 if (strEQ(mc->szCryptoDevice, "chil")) {
315 ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
318 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
319 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
320 "Init: Failed to enable Crypto Device API `%s'",
322 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
331 static void ssl_init_server_check(server_rec *s,
337 * check for important parameters and the
338 * possibility that the user forgot to set them.
340 if (!mctx->pks->cert_files[0]) {
341 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
342 "No SSL Certificate set [hint: SSLCertificateFile]");
347 * Check for problematic re-initializations
349 if (mctx->pks->certs[SSL_AIDX_RSA] ||
350 mctx->pks->certs[SSL_AIDX_DSA])
352 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
353 "Illegal attempt to re-initialise SSL for server "
354 "(theoretically shouldn't happen!)");
359 static void ssl_init_ctx_protocol(server_rec *s,
365 SSL_METHOD *method = NULL;
367 int protocol = mctx->protocol;
368 SSLSrvConfigRec *sc = mySrvConfig(s);
371 * Create the new per-server SSL context
373 if (protocol == SSL_PROTOCOL_NONE) {
374 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
375 "No SSL protocols available [hint: SSLProtocol]");
380 (protocol & SSL_PROTOCOL_SSLV2 ? "SSLv2, " : ""),
381 (protocol & SSL_PROTOCOL_SSLV3 ? "SSLv3, " : ""),
382 (protocol & SSL_PROTOCOL_TLSV1 ? "TLSv1, " : ""),
384 cp[strlen(cp)-2] = NUL;
386 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
387 "Creating new SSL context (protocols: %s)", cp);
389 if (protocol == SSL_PROTOCOL_SSLV2) {
391 SSLv2_client_method() : /* proxy */
392 SSLv2_server_method(); /* server */
393 ctx = SSL_CTX_new(method); /* only SSLv2 is left */
397 SSLv23_client_method() : /* proxy */
398 SSLv23_server_method(); /* server */
399 ctx = SSL_CTX_new(method); /* be more flexible */
404 SSL_CTX_set_options(ctx, SSL_OP_ALL);
406 if (!(protocol & SSL_PROTOCOL_SSLV2)) {
407 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
410 if (!(protocol & SSL_PROTOCOL_SSLV3)) {
411 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
414 if (!(protocol & SSL_PROTOCOL_TLSV1)) {
415 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
418 #ifdef SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
419 if (sc->insecure_reneg == TRUE) {
420 SSL_CTX_set_options(ctx, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
424 SSL_CTX_set_app_data(ctx, s);
427 * Configure additional context ingredients
429 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
431 #ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
433 * Disallow a session from being resumed during a renegotiation,
434 * so that an acceptable cipher suite can be negotiated.
436 SSL_CTX_set_options(ctx, SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
440 static void ssl_init_ctx_session_cache(server_rec *s,
445 SSL_CTX *ctx = mctx->ssl_ctx;
446 SSLModConfigRec *mc = myModConfig(s);
447 long cache_mode = SSL_SESS_CACHE_OFF;
449 if (mc->nSessionCacheMode != SSL_SCMODE_NONE) {
450 /* SSL_SESS_CACHE_NO_INTERNAL will force OpenSSL
451 * to ignore process local-caching and
452 * to always get/set/delete sessions using mod_ssl's callbacks.
454 cache_mode = SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL;
457 SSL_CTX_set_session_cache_mode(ctx, cache_mode);
459 SSL_CTX_sess_set_new_cb(ctx, ssl_callback_NewSessionCacheEntry);
460 SSL_CTX_sess_set_get_cb(ctx, ssl_callback_GetSessionCacheEntry);
461 SSL_CTX_sess_set_remove_cb(ctx, ssl_callback_DelSessionCacheEntry);
464 static void ssl_init_ctx_callbacks(server_rec *s,
469 SSL_CTX *ctx = mctx->ssl_ctx;
471 SSL_CTX_set_tmp_rsa_callback(ctx, ssl_callback_TmpRSA);
472 SSL_CTX_set_tmp_dh_callback(ctx, ssl_callback_TmpDH);
474 SSL_CTX_set_info_callback(ctx, ssl_callback_Info);
477 static void ssl_init_ctx_verify(server_rec *s,
482 SSL_CTX *ctx = mctx->ssl_ctx;
484 int verify = SSL_VERIFY_NONE;
485 STACK_OF(X509_NAME) *ca_list;
487 if (mctx->auth.verify_mode == SSL_CVERIFY_UNSET) {
488 mctx->auth.verify_mode = SSL_CVERIFY_NONE;
491 if (mctx->auth.verify_depth == UNSET) {
492 mctx->auth.verify_depth = 1;
496 * Configure callbacks for SSL context
498 if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
499 verify |= SSL_VERIFY_PEER_STRICT;
502 if ((mctx->auth.verify_mode == SSL_CVERIFY_OPTIONAL) ||
503 (mctx->auth.verify_mode == SSL_CVERIFY_OPTIONAL_NO_CA))
505 verify |= SSL_VERIFY_PEER;
508 SSL_CTX_set_verify(ctx, verify, ssl_callback_SSLVerify);
511 * Configure Client Authentication details
513 if (mctx->auth.ca_cert_file || mctx->auth.ca_cert_path) {
514 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
515 "Configuring client authentication");
517 if (!SSL_CTX_load_verify_locations(ctx,
518 MODSSL_PCHAR_CAST mctx->auth.ca_cert_file,
519 MODSSL_PCHAR_CAST mctx->auth.ca_cert_path))
521 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
522 "Unable to configure verify locations "
523 "for client authentication");
524 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
528 ca_list = ssl_init_FindCAList(s, ptemp,
529 mctx->auth.ca_cert_file,
530 mctx->auth.ca_cert_path);
532 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
533 "Unable to determine list of available "
534 "CA certificates for client authentication");
538 SSL_CTX_set_client_CA_list(ctx, (STACK *)ca_list);
542 * Give a warning when no CAs were configured but client authentication
543 * should take place. This cannot work.
545 if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
546 ca_list = (STACK_OF(X509_NAME) *)SSL_CTX_get_client_CA_list(ctx);
548 if (sk_X509_NAME_num(ca_list) == 0) {
549 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
550 "Init: Oops, you want to request client "
551 "authentication, but no CAs are known for "
552 "verification!? [Hint: SSLCACertificate*]");
557 static void ssl_init_ctx_cipher_suite(server_rec *s,
562 SSL_CTX *ctx = mctx->ssl_ctx;
563 const char *suite = mctx->auth.cipher_suite;
566 * Configure SSL Cipher Suite
572 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
573 "Configuring permitted SSL ciphers [%s]",
576 if (!SSL_CTX_set_cipher_list(ctx, MODSSL_PCHAR_CAST suite)) {
577 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
578 "Unable to configure permitted SSL ciphers");
579 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
584 static void ssl_init_ctx_crl(server_rec *s,
590 * Configure Certificate Revocation List (CRL) Details
593 if (!(mctx->crl_file || mctx->crl_path)) {
597 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
598 "Configuring certificate revocation facility");
601 SSL_X509_STORE_create((char *)mctx->crl_file,
602 (char *)mctx->crl_path);
605 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
606 "Unable to configure X.509 CRL storage "
607 "for certificate revocation");
608 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
613 static void ssl_init_ctx_cert_chain(server_rec *s,
618 BOOL skip_first = FALSE;
620 const char *chain = mctx->cert_chain;
623 * Optionally configure extra server certificate chain certificates.
624 * This is usually done by OpenSSL automatically when one of the
625 * server cert issuers are found under SSLCACertificatePath or in
626 * SSLCACertificateFile. But because these are intended for client
627 * authentication it can conflict. For instance when you use a
628 * Global ID server certificate you've to send out the intermediate
629 * CA certificate, too. When you would just configure this with
630 * SSLCACertificateFile and also use client authentication mod_ssl
631 * would accept all clients also issued by this CA. Obviously this
632 * isn't what we want in this situation. So this feature here exists
633 * to allow one to explicity configure CA certificates which are
634 * used only for the server certificate chain.
640 for (i = 0; (i < SSL_AIDX_MAX) && mctx->pks->cert_files[i]; i++) {
641 if (strEQ(mctx->pks->cert_files[i], chain)) {
647 n = SSL_CTX_use_certificate_chain(mctx->ssl_ctx,
651 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
652 "Failed to configure CA certificate chain!");
656 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
657 "Configuring server certificate chain "
658 "(%d CA certificate%s)",
659 n, n == 1 ? "" : "s");
662 static void ssl_init_ctx(server_rec *s,
667 ssl_init_ctx_protocol(s, p, ptemp, mctx);
669 ssl_init_ctx_session_cache(s, p, ptemp, mctx);
671 ssl_init_ctx_callbacks(s, p, ptemp, mctx);
673 ssl_init_ctx_verify(s, p, ptemp, mctx);
675 ssl_init_ctx_cipher_suite(s, p, ptemp, mctx);
677 ssl_init_ctx_crl(s, p, ptemp, mctx);
680 /* XXX: proxy support? */
681 ssl_init_ctx_cert_chain(s, p, ptemp, mctx);
685 static int ssl_server_import_cert(server_rec *s,
690 SSLModConfigRec *mc = myModConfig(s);
692 MODSSL_D2I_X509_CONST unsigned char *ptr;
693 const char *type = ssl_asn1_keystr(idx);
696 if (!(asn1 = ssl_asn1_table_get(mc->tPublicCert, id))) {
700 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
701 "Configuring %s server certificate", type);
704 if (!(cert = d2i_X509(NULL, &ptr, asn1->nData))) {
705 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
706 "Unable to import %s server certificate", type);
707 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
711 if (SSL_CTX_use_certificate(mctx->ssl_ctx, cert) <= 0) {
712 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
713 "Unable to configure %s server certificate", type);
714 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
718 mctx->pks->certs[idx] = cert;
723 static int ssl_server_import_key(server_rec *s,
728 SSLModConfigRec *mc = myModConfig(s);
730 MODSSL_D2I_PrivateKey_CONST unsigned char *ptr;
731 const char *type = ssl_asn1_keystr(idx);
732 int pkey_type = (idx == SSL_AIDX_RSA) ? EVP_PKEY_RSA : EVP_PKEY_DSA;
735 if (!(asn1 = ssl_asn1_table_get(mc->tPrivateKey, id))) {
739 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
740 "Configuring %s server private key", type);
743 if (!(pkey = d2i_PrivateKey(pkey_type, NULL, &ptr, asn1->nData)))
745 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
746 "Unable to import %s server private key", type);
747 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
751 if (SSL_CTX_use_PrivateKey(mctx->ssl_ctx, pkey) <= 0) {
752 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
753 "Unable to configure %s server private key", type);
754 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
759 * XXX: wonder if this is still needed, this is old todo doc.
760 * (see http://www.psy.uq.edu.au/~ftp/Crypto/ssleay/TODO.html)
762 if ((pkey_type == EVP_PKEY_DSA) && mctx->pks->certs[idx]) {
763 EVP_PKEY *pubkey = X509_get_pubkey(mctx->pks->certs[idx]);
765 if (pubkey && EVP_PKEY_missing_parameters(pubkey)) {
766 EVP_PKEY_copy_parameters(pubkey, pkey);
767 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
768 "Copying DSA parameters from private key to certificate");
769 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
770 EVP_PKEY_free(pubkey);
774 mctx->pks->keys[idx] = pkey;
779 static void ssl_check_public_cert(server_rec *s,
792 * Some information about the certificate(s)
795 if (SSL_X509_isSGC(cert)) {
796 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
797 "%s server certificate enables "
798 "Server Gated Cryptography (SGC)",
799 ssl_asn1_keystr(type));
802 if (SSL_X509_getBC(cert, &is_ca, &pathlen)) {
804 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
805 "%s server certificate is a CA certificate "
806 "(BasicConstraints: CA == TRUE !?)",
807 ssl_asn1_keystr(type));
811 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
812 "%s server certificate is not a leaf certificate "
813 "(BasicConstraints: pathlen == %d > 0 !?)",
814 ssl_asn1_keystr(type), pathlen);
818 if (SSL_X509_getCN(ptemp, cert, &cn)) {
819 int fnm_flags = FNM_PERIOD|FNM_CASE_BLIND;
821 if (apr_fnmatch_test(cn) &&
822 (apr_fnmatch(cn, s->server_hostname,
823 fnm_flags) == FNM_NOMATCH))
825 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
826 "%s server certificate wildcard CommonName (CN) `%s' "
827 "does NOT match server name!?",
828 ssl_asn1_keystr(type), cn);
830 else if (strNE(s->server_hostname, cn)) {
831 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
832 "%s server certificate CommonName (CN) `%s' "
833 "does NOT match server name!?",
834 ssl_asn1_keystr(type), cn);
839 static void ssl_init_server_certs(server_rec *s,
844 const char *rsa_id, *dsa_id;
845 const char *vhost_id = mctx->sc->vhost_id;
847 int have_rsa, have_dsa;
849 rsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_RSA);
850 dsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_DSA);
852 have_rsa = ssl_server_import_cert(s, mctx, rsa_id, SSL_AIDX_RSA);
853 have_dsa = ssl_server_import_cert(s, mctx, dsa_id, SSL_AIDX_DSA);
855 if (!(have_rsa || have_dsa)) {
856 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
857 "Oops, no RSA or DSA server certificate found?!");
861 for (i = 0; i < SSL_AIDX_MAX; i++) {
862 ssl_check_public_cert(s, ptemp, mctx->pks->certs[i], i);
865 have_rsa = ssl_server_import_key(s, mctx, rsa_id, SSL_AIDX_RSA);
866 have_dsa = ssl_server_import_key(s, mctx, dsa_id, SSL_AIDX_DSA);
868 if (!(have_rsa || have_dsa)) {
869 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
870 "Oops, no RSA or DSA server private key found?!");
875 static void ssl_init_proxy_certs(server_rec *s,
881 STACK_OF(X509_INFO) *sk;
882 modssl_pk_proxy_t *pkp = mctx->pkp;
884 SSL_CTX_set_client_cert_cb(mctx->ssl_ctx,
885 ssl_callback_proxy_cert);
887 if (!(pkp->cert_file || pkp->cert_path)) {
891 sk = sk_X509_INFO_new_null();
893 if (pkp->cert_file) {
894 SSL_X509_INFO_load_file(ptemp, sk, pkp->cert_file);
897 if (pkp->cert_path) {
898 SSL_X509_INFO_load_path(ptemp, sk, pkp->cert_path);
901 if ((ncerts = sk_X509_INFO_num(sk)) <= 0) {
902 sk_X509_INFO_free(sk);
903 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
904 "no client certs found for SSL proxy");
908 /* Check that all client certs have got certificates and private
910 for (n = 0; n < ncerts; n++) {
911 X509_INFO *inf = sk_X509_INFO_value(sk, n);
913 if (!inf->x509 || !inf->x_pkey) {
914 sk_X509_INFO_free(sk);
915 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s,
916 "incomplete client cert configured for SSL proxy "
917 "(missing or encrypted private key?)");
923 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
924 "loaded %d client certs for SSL proxy",
929 static void ssl_init_proxy_ctx(server_rec *s,
934 ssl_init_ctx(s, p, ptemp, sc->proxy);
936 ssl_init_proxy_certs(s, p, ptemp, sc->proxy);
939 static void ssl_init_server_ctx(server_rec *s,
944 ssl_init_server_check(s, p, ptemp, sc->server);
946 ssl_init_ctx(s, p, ptemp, sc->server);
948 ssl_init_server_certs(s, p, ptemp, sc->server);
952 * Configure a particular server
954 void ssl_init_ConfigureServer(server_rec *s,
960 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
961 "Configuring server for SSL protocol");
962 ssl_init_server_ctx(s, p, ptemp, sc);
965 if (sc->proxy_enabled) {
966 ssl_init_proxy_ctx(s, p, ptemp, sc);
970 void ssl_init_CheckServers(server_rec *base_server, apr_pool_t *p)
978 BOOL conflict = FALSE;
981 * Give out warnings when a server has HTTPS configured
982 * for the HTTP port or vice versa
984 for (s = base_server; s; s = s->next) {
987 if (sc->enabled && (s->port == DEFAULT_HTTP_PORT)) {
988 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
990 "Init: (%s) You configured HTTPS(%d) "
991 "on the standard HTTP(%d) port!",
992 ssl_util_vhostid(p, s),
993 DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT);
996 if (!sc->enabled && (s->port == DEFAULT_HTTPS_PORT)) {
997 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
999 "Init: (%s) You configured HTTP(%d) "
1000 "on the standard HTTPS(%d) port!",
1001 ssl_util_vhostid(p, s),
1002 DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT);
1007 * Give out warnings when more than one SSL-aware virtual server uses the
1008 * same IP:port. This doesn't work because mod_ssl then will always use
1009 * just the certificate/keys of one virtual host (which one cannot be said
1010 * easily - but that doesn't matter here).
1012 table = apr_hash_make(p);
1014 for (s = base_server; s; s = s->next) {
1015 sc = mySrvConfig(s);
1017 if (!(sc->enabled && s->addrs)) {
1021 key = apr_psprintf(p, "%pA:%u",
1022 &s->addrs->host_addr, s->addrs->host_port);
1025 if ((ps = (server_rec *)apr_hash_get(table, key, klen))) {
1026 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1028 "Init: SSL server IP/port conflict: "
1029 "%s (%s:%d) vs. %s (%s:%d)",
1030 ssl_util_vhostid(p, s),
1031 (s->defn_name ? s->defn_name : "unknown"),
1032 s->defn_line_number,
1033 ssl_util_vhostid(p, ps),
1034 (ps->defn_name ? ps->defn_name : "unknown"),
1035 ps->defn_line_number);
1040 apr_hash_set(table, key, klen, s);
1044 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, base_server,
1045 "Init: You should not use name-based "
1046 "virtual hosts in conjunction with SSL!!");
1050 #ifdef SSLC_VERSION_NUMBER
1051 static int ssl_init_FindCAList_X509NameCmp(char **a, char **b)
1053 return(X509_NAME_cmp((void*)*a, (void*)*b));
1056 static int ssl_init_FindCAList_X509NameCmp(X509_NAME **a, X509_NAME **b)
1058 return(X509_NAME_cmp(*a, *b));
1062 static void ssl_init_PushCAList(STACK_OF(X509_NAME) *ca_list,
1063 server_rec *s, const char *file)
1066 STACK_OF(X509_NAME) *sk;
1068 sk = (STACK_OF(X509_NAME) *)
1069 SSL_load_client_CA_file(MODSSL_PCHAR_CAST file);
1075 for (n = 0; n < sk_X509_NAME_num(sk); n++) {
1077 X509_NAME *name = sk_X509_NAME_value(sk, n);
1079 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1080 "CA certificate: %s",
1081 X509_NAME_oneline(name, name_buf, sizeof(name_buf)));
1084 * note that SSL_load_client_CA_file() checks for duplicates,
1085 * but since we call it multiple times when reading a directory
1086 * we must also check for duplicates ourselves.
1089 if (sk_X509_NAME_find(ca_list, name) < 0) {
1090 /* this will be freed when ca_list is */
1091 sk_X509_NAME_push(ca_list, name);
1094 /* need to free this ourselves, else it will leak */
1095 X509_NAME_free(name);
1099 sk_X509_NAME_free(sk);
1102 STACK_OF(X509_NAME) *ssl_init_FindCAList(server_rec *s,
1104 const char *ca_file,
1105 const char *ca_path)
1107 STACK_OF(X509_NAME) *ca_list;
1110 * Start with a empty stack/list where new
1111 * entries get added in sorted order.
1113 ca_list = sk_X509_NAME_new(ssl_init_FindCAList_X509NameCmp);
1116 * Process CA certificate bundle file
1119 ssl_init_PushCAList(ca_list, s, ca_file);
1123 * Process CA certificate path files
1127 apr_finfo_t direntry;
1128 apr_int32_t finfo_flags = APR_FINFO_TYPE|APR_FINFO_NAME;
1131 if ((rv = apr_dir_open(&dir, ca_path, ptemp)) != APR_SUCCESS) {
1132 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
1133 "Failed to open SSLCACertificatePath `%s'",
1138 while ((apr_dir_read(&direntry, finfo_flags, dir)) == APR_SUCCESS) {
1140 if (direntry.filetype == APR_DIR) {
1141 continue; /* don't try to load directories */
1143 file = apr_pstrcat(ptemp, ca_path, "/", direntry.name, NULL);
1144 ssl_init_PushCAList(ca_list, s, file);
1153 sk_X509_NAME_set_cmp_func(ca_list, NULL);
1158 void ssl_init_Child(apr_pool_t *p, server_rec *s)
1160 SSLModConfigRec *mc = myModConfig(s);
1161 mc->pid = getpid(); /* only call getpid() once per-process */
1163 /* XXX: there should be an ap_srand() function */
1164 srand((unsigned int)time(NULL));
1166 /* open the mutex lockfile */
1167 ssl_mutex_reinit(s, p);
1170 #define MODSSL_CFG_ITEM_FREE(func, item) \
1176 static void ssl_init_ctx_cleanup(modssl_ctx_t *mctx)
1178 MODSSL_CFG_ITEM_FREE(X509_STORE_free, mctx->crl);
1180 MODSSL_CFG_ITEM_FREE(SSL_CTX_free, mctx->ssl_ctx);
1183 static void ssl_init_ctx_cleanup_proxy(modssl_ctx_t *mctx)
1185 ssl_init_ctx_cleanup(mctx);
1187 if (mctx->pkp->certs) {
1188 sk_X509_INFO_pop_free(mctx->pkp->certs, X509_INFO_free);
1192 static void ssl_init_ctx_cleanup_server(modssl_ctx_t *mctx)
1196 ssl_init_ctx_cleanup(mctx);
1198 for (i=0; i < SSL_AIDX_MAX; i++) {
1199 MODSSL_CFG_ITEM_FREE(X509_free,
1200 mctx->pks->certs[i]);
1202 MODSSL_CFG_ITEM_FREE(EVP_PKEY_free,
1203 mctx->pks->keys[i]);
1207 apr_status_t ssl_init_ModuleKill(void *data)
1209 SSLSrvConfigRec *sc;
1210 server_rec *base_server = (server_rec *)data;
1214 * Drop the session cache and mutex
1216 ssl_scache_kill(base_server);
1219 * Destroy the temporary keys and params
1221 ssl_tmp_keys_free(base_server);
1224 * Free the non-pool allocated structures
1225 * in the per-server configurations
1227 for (s = base_server; s; s = s->next) {
1228 sc = mySrvConfig(s);
1230 ssl_init_ctx_cleanup_proxy(sc->proxy);
1232 ssl_init_ctx_cleanup_server(sc->server);
1236 * Try to kill the internals of the SSL library.
1238 ERR_remove_state(0);