/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* _ _
* _ __ ___ ___ __| | ___ ___| | mod_ssl
* | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
* | | | | | | (_) | (_| | \__ \__ \ |
* |_| |_| |_|\___/ \__,_|___|___/___/_|
* |_____|
* Initialization of Servers
*/
/* ``Recursive, adj.;
see Recursive.''
-- Unknown */
#include "ssl_private.h"
#include "mod_ssl.h"
#include "mod_ssl_openssl.h"
#include "mpm_common.h"
/* _________________________________________________________________
**
** Module Initialization
** _________________________________________________________________
*/
#ifdef HAVE_ECC
#else
#define KEYTYPES "RSA or DSA"
#endif
/*
* Grab well-defined DH parameters from OpenSSL, see the get_rfc*
*/
{
if (!dh) {
return NULL;
}
return NULL;
}
return dh;
}
/* Storage and initialization for DH parameters. */
static struct dhparam {
} dhparams[] = {
{ get_rfc2409_prime_1024, NULL, 0 }
};
static void init_dh_params(void)
{
unsigned n;
}
static void free_dh_params(void)
{
unsigned n;
/* DH_free() is a noop for a NULL parameter, so these are harmless
* in the (unexpected) case where these variables are already
* NULL. */
}
}
/* Hand out the same DH structure though once generated as we leak
* memory otherwise and freeing the structure up after use would be
* hard to track and in fact is not needed at all as it is safe to
* use the same parameters over and over again security wise (in
* contrast to the keys itself) and code safe as the returned structure
* is duplicated by OpenSSL anyway. Hence no modification happens
* to our copy. */
{
unsigned n;
return NULL; /* impossible to reach. */
}
server_rec *s)
{
"SSL_VERSION_LIBRARY_INTERFACE");
"%s compiled against Server: %s, Library: %s",
}
/*
* Per-module initialization
*/
{
server_rec *s;
if (SSLeay() < MODSSL_LIBRARY_VERSION) {
"Init: this version of mod_ssl was compiled against "
"a newer library (%s, version currently loaded is %s)"
" - may result in undefined or erroneous behavior",
}
/* We initialize mc->pid per-process in the child init,
* but it should be initialized for startup before we
* call ssl_rand_seed() below.
*/
/*
* Let us cleanup on restarts and exits
*/
/*
* Any init round fixes the global config
*/
/*
* try to fix the configuration and open the dedicated SSL
* logfile as early as possible
*/
for (s = base_server; s; s = s->next) {
sc = mySrvConfig(s);
}
}
/*
* Create the server host:port string because we need it a lot
*/
/* Default to enabled if SSLEngine is not set explicitly, and
* the protocol is https. */
if (ap_get_server_protocol(s)
}
/* Fix up stuff that may not have been set. If sc->enabled is
* UNSET, then SSL is disabled on this vhost. */
}
}
}
}
#ifdef HAVE_FIPS
}
#endif
}
#if APR_HAS_THREADS
#endif
/*
* SSL external crypto device ("engine") support
*/
#if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
return rv;
}
#endif
"Init: Initialized %s library", MODSSL_LIBRARY_NAME);
/*
* Seed the Pseudo Random Number Generator (PRNG)
* only need ptemp here; nothing inside allocated from the pool
* needs to live once we return from ssl_rand_seed().
*/
#ifdef HAVE_FIPS
if (!FIPS_mode()) {
if (FIPS_mode_set(1)) {
"Operating in SSL FIPS mode");
}
else {
return ssl_die(s);
}
}
}
else {
"SSL FIPS mode disabled");
}
#endif
/*
* initialize the mutex handling
*/
if (!ssl_mutex_init(base_server, p)) {
return HTTP_INTERNAL_SERVER_ERROR;
}
#ifdef HAVE_OCSP_STAPLING
#endif
/*
* initialize session caching
*/
return rv;
}
/*
* initialize servers
*/
"Init: Initializing (virtual) servers for SSL");
for (s = base_server; s; s = s->next) {
sc = mySrvConfig(s);
/*
* Either now skip this server when SSL is disabled for
* it or give out some information about what we're
* configuring.
*/
/*
* Read the server certificate and key
*/
!= APR_SUCCESS) {
return rv;
}
}
"Init: Wiped out the queried pass phrases from memory");
}
/*
* Configuration consistency checks
*/
return rv;
}
for (s = base_server; s; s = s->next) {
sc = mySrvConfig(s);
return rv;
}
}
return rv;
}
}
}
/*
* Announce mod_ssl and SSL library in HTTP Server field
*/
SSL_init_app_data2_idx(); /* for SSL_get_app_data2() at request time */
return OK;
}
/*
* Support for external a Crypto Device ("engine"), usually
* a hardware accellerator card for crypto operations.
*/
#if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
{
ENGINE *e;
if (mc->szCryptoDevice) {
"Init: Failed to load Crypto Device API `%s'",
mc->szCryptoDevice);
return ssl_die(s);
}
}
#endif
if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
"Init: Failed to enable Crypto Device API `%s'",
mc->szCryptoDevice);
return ssl_die(s);
}
"Init: loaded Crypto Device API `%s'",
mc->szCryptoDevice);
ENGINE_free(e);
}
return APR_SUCCESS;
}
#endif
#ifdef HAVE_TLSEXT
apr_pool_t *p,
{
/*
* Configure TLS extensions support
*/
"Configuring TLS extension handling");
/*
* Server name indication (SNI)
*/
"Unable to initialize TLS servername extension "
"callback (incompatible OpenSSL version?)");
return ssl_die(s);
}
#ifdef HAVE_OCSP_STAPLING
/*
* OCSP Stapling support, status_request extension
*/
return rv;
}
}
#endif
#ifdef HAVE_SRP
/*
* TLS-SRP support
*/
int err;
"Unable to initialize SRP verifier structure "
"[%s seed]",
return ssl_die(s);
}
if (err != SRP_NO_ERROR) {
"Unable to load SRP verifier file [error %d]", err);
return ssl_die(s);
}
}
#endif
return APR_SUCCESS;
}
#endif
apr_pool_t *p,
{
char *cp;
/*
* Create the new per-server SSL context
*/
if (protocol == SSL_PROTOCOL_NONE) {
"No SSL protocols available [hint: SSLProtocol]");
return ssl_die(s);
}
cp = apr_pstrcat(p,
#ifdef HAVE_TLSV1_X
#endif
NULL);
"Creating new SSL context (protocols: %s)", cp);
if (protocol == SSL_PROTOCOL_SSLV3) {
SSLv3_client_method() : /* proxy */
SSLv3_server_method(); /* server */
}
else if (protocol == SSL_PROTOCOL_TLSV1) {
TLSv1_client_method() : /* proxy */
TLSv1_server_method(); /* server */
}
#ifdef HAVE_TLSV1_X
else if (protocol == SSL_PROTOCOL_TLSV1_1) {
TLSv1_1_client_method() : /* proxy */
TLSv1_1_server_method(); /* server */
}
else if (protocol == SSL_PROTOCOL_TLSV1_2) {
TLSv1_2_client_method() : /* proxy */
TLSv1_2_server_method(); /* server */
}
#endif
else { /* For multiple protocols, we need a flexible method */
SSLv23_client_method() : /* proxy */
SSLv23_server_method(); /* server */
}
/* always disable SSLv2, as per RFC 6176 */
if (!(protocol & SSL_PROTOCOL_SSLV3)) {
}
if (!(protocol & SSL_PROTOCOL_TLSV1)) {
}
#ifdef HAVE_TLSV1_X
if (!(protocol & SSL_PROTOCOL_TLSV1_1)) {
}
if (!(protocol & SSL_PROTOCOL_TLSV1_2)) {
}
#endif
}
#endif
#ifndef OPENSSL_NO_COMP
#ifdef SSL_OP_NO_COMPRESSION
/* OpenSSL >= 1.0 only */
#else
#endif
}
#endif
#ifdef SSL_OP_NO_TICKET
/*
* Configure using RFC 5077 TLS session tickets
* for session resumption.
*/
}
#endif
}
#endif
SSL_CTX_set_app_data(ctx, s);
/*
* Configure additional context ingredients
*/
#ifdef HAVE_ECC
#endif
/*
* Disallow a session from being resumed during a renegotiation,
* so that an acceptable cipher suite can be negotiated.
*/
#endif
#ifdef SSL_MODE_RELEASE_BUFFERS
/* If httpd is configured to reduce mem usage, ask openssl to do so, too */
#endif
return APR_SUCCESS;
}
apr_pool_t *p,
{
}
}
apr_pool_t *p,
{
#ifdef HAVE_TLS_ALPN
#endif
#ifdef HAVE_TLS_NPN
#endif
}
apr_pool_t *p,
{
}
}
/*
* Configure callbacks for SSL context
*/
}
{
}
/*
* Configure Client Authentication details
*/
"Configuring client authentication");
{
"Unable to configure verify locations "
"for client authentication");
return ssl_die(s);
}
} else
if (sk_X509_NAME_num(ca_list) <= 0) {
"Unable to determine list of acceptable "
"CA certificates for client authentication");
return ssl_die(s);
}
}
/*
* Give a warning when no CAs were configured but client authentication
* should take place. This cannot work.
*/
if (sk_X509_NAME_num(ca_list) == 0) {
"Init: Oops, you want to request client "
"authentication, but no CAs are known for "
"verification!? [Hint: SSLCACertificate*]");
}
}
return APR_SUCCESS;
}
apr_pool_t *p,
{
const char *suite;
/*
* Configure SSL Cipher Suite. Always disable NULL and export ciphers,
* see also ssl_engine_config.c:ssl_cmd_SSLCipherSuite().
* OpenSSL's SSL_DEFAULT_CIPHER_LIST already includes !aNULL:!eNULL,
* so only prepend !EXP in this case.
*/
"Configuring permitted SSL ciphers [%s]",
suite);
"Unable to configure permitted SSL ciphers");
return ssl_die(s);
}
return APR_SUCCESS;
}
apr_pool_t *p,
{
unsigned long crlflags = 0;
/*
* Configure Certificate Revocation List (CRL) Details
*/
"Host %s: CRL checking has been enabled, but "
"neither %sCARevocationFile nor %sCARevocationPath "
return ssl_die(s);
}
return APR_SUCCESS;
}
"Configuring certificate revocation facility");
"Host %s: unable to configure X.509 CRL storage "
return ssl_die(s);
}
switch (mctx->crl_check_mode) {
case SSL_CRLCHECK_LEAF:
break;
case SSL_CRLCHECK_CHAIN:
break;
default:
crlflags = 0;
}
if (crlflags) {
} else {
"Host %s: X.509 CRL storage locations configured, "
"but CRL checking (%sCARevocationCheck) is not "
}
return APR_SUCCESS;
}
apr_pool_t *p,
{
int i, n;
/*
* Optionally configure extra server certificate chain certificates.
* This is usually done by OpenSSL automatically when one of the
* server cert issuers are found under SSLCACertificatePath or in
* SSLCACertificateFile. But because these are intended for client
* authentication it can conflict. For instance when you use a
* Global ID server certificate you've to send out the intermediate
* CA certificate, too. When you would just configure this with
* SSLCACertificateFile and also use client authentication mod_ssl
* would accept all clients also issued by this CA. Obviously this
* isn't what we want in this situation. So this feature here exists
* to allow one to explicity configure CA certificates which are
* used only for the server certificate chain.
*/
if (!chain) {
return APR_SUCCESS;
}
skip_first = TRUE;
break;
}
}
(char *)chain,
skip_first, NULL);
if (n < 0) {
"Failed to configure CA certificate chain!");
return ssl_die(s);
}
"Configuring server certificate chain "
"(%d CA certificate%s)",
n, n == 1 ? "" : "s");
return APR_SUCCESS;
}
apr_pool_t *p,
{
return rv;
}
return rv;
}
return rv;
}
return rv;
}
/* XXX: proxy support? */
return rv;
}
#ifdef HAVE_TLSEXT
APR_SUCCESS) {
return rv;
}
#endif
}
return APR_SUCCESS;
}
const char *key_id)
{
if (!cert) {
return;
}
/*
* Some information about the certificate(s)
*/
if (is_ca) {
"%s server certificate is a CA certificate "
"(BasicConstraints: CA == TRUE !?)", key_id);
}
if (pathlen > 0) {
"%s server certificate is not a leaf certificate "
"(BasicConstraints: pathlen == %d > 0 !?)",
}
}
"%s server certificate does NOT include an ID "
"which matches the server name", key_id);
}
}
/* prevent OpenSSL from showing its "Enter PEM pass phrase:" prompt */
void *userdata) {
return 0;
}
apr_pool_t *p,
{
int i;
#ifdef HAVE_ECC
int nid;
#endif
#ifndef HAVE_SSL_CONF_CMD
#endif
/* no OpenSSL default prompts for any of the SSL_CTX_use_* calls, please */
/* Iterate over the SSLCertificateFile array */
const char *));
i++) {
/* first the certificate (public key) */
if (mctx->cert_chain) {
SSL_FILETYPE_PEM) < 1)) {
"Failed to configure certificate %s, check %s",
return APR_EGENERAL;
}
} else {
certfile) < 1)) {
"Failed to configure certificate %s (with chain),"
return APR_EGENERAL;
}
}
/* and second, the private key */
} else {
}
SSL_FILETYPE_PEM) < 1) &&
const unsigned char *ptr;
/* perhaps it's an encrypted private key, so try again */
"Failed to configure encrypted (?) private key %s,"
return APR_EGENERAL;
}
}
"Certificate and private key %s from %s and %s "
return APR_EGENERAL;
}
#ifdef HAVE_SSL_CONF_CMD
/*
* workaround for those OpenSSL versions where SSL_CTX_get0_certificate
* is not yet available: create an SSL struct which we dispose of
* as soon as we no longer need access to the cert. (Strictly speaking,
* SSL_CTX_get0_certificate does not depend on the SSL_CONF stuff,
* but there's no reliable way to check for its existence, so we
* assume that if SSL_CONF is available, it's OpenSSL 1.0.2 or later,
* and SSL_CTX_get0_certificate is implemented.)
*/
#else
if (ssl) {
/* Workaround bug in SSL_get_certificate in OpenSSL 0.9.8y */
}
#endif
"Unable to retrieve certificate %s", key_id);
#ifndef HAVE_SSL_CONF_CMD
if (ssl)
#endif
return APR_EGENERAL;
}
/* warn about potential cert issues */
#if defined(HAVE_OCSP_STAPLING) && !defined(SSL_CTRL_SET_CURRENT_CERT)
/*
* OpenSSL up to 1.0.1: configure stapling as we go. In 1.0.2
* and later, there's SSL_CTX_set_current_cert, which allows
* iterating over all certs in an SSL_CTX (including those possibly
* loaded via SSLOpenSSLConfCmd Certificate), so for 1.0.2 and
* later, we defer to the code in ssl_init_server_ctx.
*/
"Unable to configure certificate %s for stapling",
key_id);
}
#endif
#ifndef HAVE_SSL_CONF_CMD
#endif
"Certificate and private key %s configured from %s and %s",
}
/*
* Try to read DH parameters from the (first) SSLCertificateFile
*/
"Custom DH parameters (%d bits) for %s loaded from %s",
}
#ifdef HAVE_ECC
/*
* Similarly, try to read the ECDH curve name from SSLCertificateFile...
*/
"ECDH curve %s for %s specified in %s",
}
/*
* ...otherwise, enable auto curve selection (OpenSSL 1.0.2 and later)
* or configure NIST P-256 (required to enable ECDHE for earlier versions)
*/
else {
#if defined(SSL_CTX_set_ecdh_auto)
#else
#endif
}
#endif
return APR_SUCCESS;
}
#ifdef HAVE_TLS_SESSION_TICKETS
apr_pool_t *p,
{
char *path;
if (!ticket_key->file_path) {
return APR_SUCCESS;
}
if (rv != APR_SUCCESS) {
"Failed to open ticket key file %s: (%d) %pm",
return ssl_die(s);
}
if (rv != APR_SUCCESS) {
"Failed to read %d bytes from %s: (%d) %pm",
return ssl_die(s);
}
"Unable to initialize TLS session ticket key callback "
"(incompatible OpenSSL version?)");
return ssl_die(s);
}
"TLS session ticket key for %s successfully loaded from %s",
return APR_SUCCESS;
}
#endif
apr_pool_t *p,
{
int n, ncerts = 0;
return APR_SUCCESS;
}
sk = sk_X509_INFO_new_null();
}
}
"no client certs found for SSL proxy");
return APR_SUCCESS;
}
/* Check that all client certs have got certificates and private
* keys. */
for (n = 0; n < ncerts; n++) {
"incomplete client cert configured for SSL proxy "
"(missing or encrypted private key?)");
return ssl_die(s);
}
"private key do not match");
return ssl_die(s);
}
}
"loaded %d client certs for SSL proxy",
ncerts);
return APR_SUCCESS;
}
/* If SSLProxyMachineCertificateChainFile is configured, load all
* the CA certs and have OpenSSL attempt to construct a full chain
* from each configured end-entity cert up to a root. This will
* allow selection of the correct cert given a list of root CA
* names in the certificate request from the server. */
sctx = X509_STORE_CTX_new();
if (!sctx) {
"SSL proxy client cert initialization failed");
return ssl_die(s);
}
for (n = 0; n < ncerts; n++) {
int i;
/* Attempt to verify the client cert */
"verification failed: %s :",
}
/* Clear X509_verify_cert errors */
/* Obtain a copy of the verified chain */
/* Discard end entity cert from the chain */
if ((i = sk_X509_num(chain)) > 0) {
/* Store the chain for later use */
}
else {
/* Discard empty chain */
}
APLOGNO(02271)
"loaded %i intermediate CA%s for cert %i: ",
i, i == 1 ? "" : "s", n);
if (i > 0) {
int j;
for (j = 0; j < i; j++) {
}
}
}
/* get ready for next X509_STORE_CTX_init */
}
return APR_SUCCESS;
}
apr_pool_t *p,
{
return rv;
}
return rv;
}
return APR_SUCCESS;
}
apr_pool_t *p,
{
#ifdef HAVE_SSL_CONF_CMD
int i;
#endif
/*
* Check for problematic re-initializations
*/
"Illegal attempt to re-initialise SSL for server "
"(SSLEngine On should go in the VirtualHost, not in global scope.)");
return APR_EGENERAL;
}
return rv;
}
!= APR_SUCCESS) {
return rv;
}
#ifdef HAVE_SSL_CONF_CMD
"\"SSLOpenSSLConfCmd %s %s\" failed for %s",
return ssl_die(s);
} else {
"\"SSLOpenSSLConfCmd %s %s\" applied to %s",
}
}
if (SSL_CONF_CTX_finish(cctx) == 0) {
"SSL_CONF_CTX_finish() failed");
return ssl_die(s);
}
#endif
"Failed to configure at least one certificate and key "
return ssl_die(s);
}
#if defined(HAVE_OCSP_STAPLING) && defined(SSL_CTRL_SET_CURRENT_CERT)
/*
* OpenSSL 1.0.2 and later allows iterating over all SSL_CTX certs
* by means of SSL_CTX_set_current_cert. Enabling stapling at this
* (late) point makes sure that we catch both certificates loaded
* via SSLCertificateFile and SSLOpenSSLConfCmd Certificate.
*/
int i = 0;
while (ret) {
cert)) {
"Unable to configure certificate %s:%d "
}
i++;
}
}
#endif
#ifdef HAVE_TLS_SESSION_TICKETS
return rv;
}
#endif
return APR_SUCCESS;
}
/*
* Configure a particular server
*/
apr_pool_t *p,
{
/* Initialize the server if SSL is enabled or optional.
*/
!= APR_SUCCESS) {
return rv;
}
}
if (sc->proxy_enabled) {
return rv;
}
}
return APR_SUCCESS;
}
{
server_rec *s;
#ifndef HAVE_TLSEXT
const char *key;
#endif
/*
* Give out warnings when a server has HTTPS configured
* for the HTTP port or vice versa
*/
for (s = base_server; s; s = s->next) {
sc = mySrvConfig(s);
"Init: (%s) You configured HTTPS(%d) "
"on the standard HTTP(%d) port!",
ssl_util_vhostid(p, s),
}
"Init: (%s) You configured HTTP(%d) "
"on the standard HTTPS(%d) port!",
ssl_util_vhostid(p, s),
}
}
#ifndef HAVE_TLSEXT
/*
* Give out warnings when more than one SSL-aware virtual server uses the
* same IP:port and an OpenSSL version without support for TLS extensions
* (SNI in particular) is used.
*/
table = apr_hash_make(p);
for (s = base_server; s; s = s->next) {
char *addr;
sc = mySrvConfig(s);
continue;
}
"%s (%s:%d) vs. %s (%s:%d)",
ssl_util_vhostid(p, s),
s->defn_line_number,
ssl_util_vhostid(p, ps),
continue;
}
}
if (conflict) {
"Init: Name-based SSL virtual hosts require "
"an OpenSSL version with support for TLS extensions "
"(RFC 6066 - Server Name Indication / SNI), "
"but the currently used library version (%s) is "
}
#endif
return APR_SUCCESS;
}
const X509_NAME * const *b)
{
return(X509_NAME_cmp(*a, *b));
}
const char *file)
{
int n;
if (!sk) {
return;
}
for (n = 0; n < sk_X509_NAME_num(sk); n++) {
"CA certificate: %s",
/*
* note that SSL_load_client_CA_file() checks for duplicates,
* but since we call it multiple times when reading a directory
* we must also check for duplicates ourselves.
*/
/* this will be freed when ca_list is */
}
else {
/* need to free this ourselves, else it will leak */
}
}
}
const char *ca_file,
const char *ca_path)
{
/*
* entries get added in sorted order.
*/
/*
* Process CA certificate bundle file
*/
if (ca_file) {
/*
* If ca_list is still empty after trying to load ca_file
* then the file failed to load, and users should hear about that.
*/
if (sk_X509_NAME_num(ca_list) == 0) {
"Failed to load SSLCACertificateFile: %s", ca_file);
}
}
/*
* Process CA certificate path files
*/
if (ca_path) {
"Failed to open Certificate Path `%s'",
ca_path);
return NULL;
}
const char *file;
continue; /* don't try to load directories */
}
}
}
/*
* Cleanup
*/
return ca_list;
}
{
/* XXX: there should be an ap_srand() function */
/* open the mutex lockfile */
ssl_mutex_reinit(s, p);
#ifdef HAVE_OCSP_STAPLING
ssl_stapling_mutex_reinit(s, p);
#endif
}
if (item) { \
}
{
#ifdef HAVE_SRP
}
#endif
}
{
int i = 0;
for (i = 0; i < ncerts; i++) {
}
}
}
}
}
{
server_rec *s;
/*
* Drop the session cache and mutex
*/
/*
* Free the non-pool allocated structures
* in the per-server configurations
*/
for (s = base_server; s; s = s->next) {
sc = mySrvConfig(s);
}
return APR_SUCCESS;
}