Blame modules/ssl/ssl_scache.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*                      _             _
Packit 90a5c9
 *  _ __ ___   ___   __| |    ___ ___| |  mod_ssl
Packit 90a5c9
 * | '_ ` _ \ / _ \ / _` |   / __/ __| |  Apache Interface to OpenSSL
Packit 90a5c9
 * | | | | | | (_) | (_| |   \__ \__ \ |
Packit 90a5c9
 * |_| |_| |_|\___/ \__,_|___|___/___/_|
Packit 90a5c9
 *                      |_____|
Packit 90a5c9
 *  ssl_scache.c
Packit 90a5c9
 *  Session Cache Abstraction
Packit 90a5c9
 */
Packit 90a5c9
                             /* ``Open-Source Software: generous
Packit 90a5c9
                                  programmers from around the world all
Packit 90a5c9
                                  join forces to help you shoot
Packit 90a5c9
                                  yourself in the foot for free.''
Packit 90a5c9
                                                 -- Unknown         */
Packit 90a5c9
#include "ssl_private.h"
Packit 90a5c9
#include "mod_status.h"
Packit 90a5c9
Packit 90a5c9
/*  _________________________________________________________________
Packit 90a5c9
**
Packit 90a5c9
**  Session Cache: Common Abstraction Layer
Packit 90a5c9
**  _________________________________________________________________
Packit 90a5c9
*/
Packit 90a5c9
Packit 90a5c9
apr_status_t ssl_scache_init(server_rec *s, apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    SSLModConfigRec *mc = myModConfig(s);
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    struct ap_socache_hints hints;
Packit 90a5c9
Packit 90a5c9
    /* The very first invocation of this function will be the
Packit 90a5c9
     * post_config invocation during server startup; do nothing for
Packit 90a5c9
     * this first (and only the first) time through, since the pool
Packit 90a5c9
     * will be immediately cleared anyway.  For every subsequent
Packit 90a5c9
     * invocation, initialize the configured cache. */
Packit 90a5c9
    if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG)
Packit 90a5c9
        return APR_SUCCESS;
Packit 90a5c9
Packit 90a5c9
#ifdef HAVE_OCSP_STAPLING
Packit 90a5c9
    if (mc->stapling_cache) {
Packit 90a5c9
        memset(&hints, 0, sizeof hints);
Packit 90a5c9
        hints.avg_obj_size = 1500;
Packit 90a5c9
        hints.avg_id_len = 20;
Packit 90a5c9
        hints.expiry_interval = 300;
Packit 90a5c9
Packit 90a5c9
        rv = mc->stapling_cache->init(mc->stapling_cache_context,
Packit 90a5c9
                                     "mod_ssl-stapling", &hints, s, p);
Packit 90a5c9
        if (rv) {
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01872)
Packit 90a5c9
                         "Could not initialize stapling cache. Exiting.");
Packit 90a5c9
            return ssl_die(s);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Warn the user that he should use the session cache.
Packit 90a5c9
     * But we can operate without it, of course.
Packit 90a5c9
     */
Packit 90a5c9
    if (mc->sesscache == NULL) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(01873)
Packit 90a5c9
                     "Init: Session Cache is not configured "
Packit 90a5c9
                     "[hint: SSLSessionCache]");
Packit 90a5c9
        return APR_SUCCESS;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    memset(&hints, 0, sizeof hints);
Packit 90a5c9
    hints.avg_obj_size = 150;
Packit 90a5c9
    hints.avg_id_len = 30;
Packit 90a5c9
    hints.expiry_interval = 30;
Packit 90a5c9
Packit 90a5c9
    rv = mc->sesscache->init(mc->sesscache_context, "mod_ssl-session", &hints, s, p);
Packit 90a5c9
    if (rv) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01874)
Packit 90a5c9
                     "Could not initialize session cache. Exiting.");
Packit 90a5c9
        return ssl_die(s);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
void ssl_scache_kill(server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    SSLModConfigRec *mc = myModConfig(s);
Packit 90a5c9
Packit 90a5c9
    if (mc->sesscache) {
Packit 90a5c9
        mc->sesscache->destroy(mc->sesscache_context, s);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
#ifdef HAVE_OCSP_STAPLING
Packit 90a5c9
    if (mc->stapling_cache) {
Packit 90a5c9
        mc->stapling_cache->destroy(mc->stapling_cache_context, s);
Packit 90a5c9
    }
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
BOOL ssl_scache_store(server_rec *s, IDCONST UCHAR *id, int idlen,
Packit 90a5c9
                      apr_time_t expiry, SSL_SESSION *sess,
Packit 90a5c9
                      apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    SSLModConfigRec *mc = myModConfig(s);
Packit 90a5c9
    unsigned char encoded[MODSSL_SESSION_MAX_DER], *ptr;
Packit 90a5c9
    unsigned int len;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    /* Serialise the session. */
Packit 90a5c9
    len = i2d_SSL_SESSION(sess, NULL);
Packit 90a5c9
    if (len > sizeof encoded) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(01875)
Packit 90a5c9
                     "session is too big (%u bytes)", len);
Packit 90a5c9
        return FALSE;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ptr = encoded;
Packit 90a5c9
    len = i2d_SSL_SESSION(sess, &ptr);
Packit 90a5c9
Packit 90a5c9
    if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
Packit 90a5c9
        ssl_mutex_on(s);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    rv = mc->sesscache->store(mc->sesscache_context, s, id, idlen,
Packit 90a5c9
                              expiry, encoded, len, p);
Packit 90a5c9
Packit 90a5c9
    if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
Packit 90a5c9
        ssl_mutex_off(s);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return rv == APR_SUCCESS ? TRUE : FALSE;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
SSL_SESSION *ssl_scache_retrieve(server_rec *s, IDCONST UCHAR *id, int idlen,
Packit 90a5c9
                                 apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    SSLModConfigRec *mc = myModConfig(s);
Packit 90a5c9
    unsigned char dest[MODSSL_SESSION_MAX_DER];
Packit 90a5c9
    unsigned int destlen = MODSSL_SESSION_MAX_DER;
Packit 90a5c9
    const unsigned char *ptr;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
Packit 90a5c9
        ssl_mutex_on(s);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    rv = mc->sesscache->retrieve(mc->sesscache_context, s, id, idlen,
Packit 90a5c9
                                 dest, &destlen, p);
Packit 90a5c9
Packit 90a5c9
    if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
Packit 90a5c9
        ssl_mutex_off(s);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ptr = dest;
Packit 90a5c9
Packit 90a5c9
    return d2i_SSL_SESSION(NULL, &ptr, destlen);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
void ssl_scache_remove(server_rec *s, IDCONST UCHAR *id, int idlen,
Packit 90a5c9
                       apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    SSLModConfigRec *mc = myModConfig(s);
Packit 90a5c9
Packit 90a5c9
    if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
Packit 90a5c9
        ssl_mutex_on(s);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    mc->sesscache->remove(mc->sesscache_context, s, id, idlen, p);
Packit 90a5c9
Packit 90a5c9
    if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
Packit 90a5c9
        ssl_mutex_off(s);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*  _________________________________________________________________
Packit 90a5c9
**
Packit 90a5c9
**  SSL Extension to mod_status
Packit 90a5c9
**  _________________________________________________________________
Packit 90a5c9
*/
Packit 90a5c9
static int ssl_ext_status_hook(request_rec *r, int flags)
Packit 90a5c9
{
Packit 90a5c9
    SSLModConfigRec *mc = myModConfig(r->server);
Packit 90a5c9
Packit 90a5c9
    if (mc == NULL || mc->sesscache == NULL)
Packit 90a5c9
        return OK;
Packit 90a5c9
Packit 90a5c9
    if (!(flags & AP_STATUS_SHORT)) {
Packit 90a5c9
        ap_rputs("
\n", r);
Packit 90a5c9
        ap_rputs("\n", r);
Packit 90a5c9
        ap_rputs("\n", r);
Packit 90a5c9
        ap_rputs("<font color=\"#ffffff\" face=\"Arial,Helvetica\">SSL/TLS Session Cache Status:</font>\r", r);
Packit 90a5c9
        ap_rputs("\n", r);
Packit 90a5c9
        ap_rputs("\n", r);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        ap_rputs("TLSSessionCacheStatus\n", r);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
Packit 90a5c9
        ssl_mutex_on(r->server);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    mc->sesscache->status(mc->sesscache_context, r, flags);
Packit 90a5c9
Packit 90a5c9
    if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
Packit 90a5c9
        ssl_mutex_off(r->server);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (!(flags & AP_STATUS_SHORT)) {
Packit 90a5c9
        ap_rputs("\n", r);
Packit 90a5c9
        ap_rputs("\n", r);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
void ssl_scache_status_register(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    APR_OPTIONAL_HOOK(ap, status_hook, ssl_ext_status_hook, NULL, NULL,
Packit 90a5c9
                      APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9