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 * Session Cache Abstraction
26 /* ``Open-Source Software: generous
27 programmers from around the world all
28 join forces to help you shoot
29 yourself in the foot for free.''
31 #include "ssl_private.h"
32 #include "mod_status.h"
34 /* _________________________________________________________________
36 ** Session Cache: Common Abstraction Layer
37 ** _________________________________________________________________
40 apr_status_t ssl_scache_init(server_rec *s, apr_pool_t *p)
42 SSLModConfigRec *mc = myModConfig(s);
44 struct ap_socache_hints hints;
46 /* The very first invocation of this function will be the
47 * post_config invocation during server startup; do nothing for
48 * this first (and only the first) time through, since the pool
49 * will be immediately cleared anyway. For every subsequent
50 * invocation, initialize the configured cache. */
51 if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG)
54 #ifdef HAVE_OCSP_STAPLING
55 if (mc->stapling_cache) {
56 memset(&hints, 0, sizeof hints);
57 hints.avg_obj_size = 1500;
58 hints.avg_id_len = 20;
59 hints.expiry_interval = 300;
61 rv = mc->stapling_cache->init(mc->stapling_cache_context,
62 "mod_ssl-stapling", &hints, s, p);
64 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01872)
65 "Could not initialize stapling cache. Exiting.");
72 * Warn the user that he should use the session cache.
73 * But we can operate without it, of course.
75 if (mc->sesscache == NULL) {
76 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(01873)
77 "Init: Session Cache is not configured "
78 "[hint: SSLSessionCache]");
82 memset(&hints, 0, sizeof hints);
83 hints.avg_obj_size = 150;
84 hints.avg_id_len = 30;
85 hints.expiry_interval = 30;
87 rv = mc->sesscache->init(mc->sesscache_context, "mod_ssl-session", &hints, s, p);
89 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01874)
90 "Could not initialize session cache. Exiting.");
97 void ssl_scache_kill(server_rec *s)
99 SSLModConfigRec *mc = myModConfig(s);
102 mc->sesscache->destroy(mc->sesscache_context, s);
105 #ifdef HAVE_OCSP_STAPLING
106 if (mc->stapling_cache) {
107 mc->stapling_cache->destroy(mc->stapling_cache_context, s);
113 BOOL ssl_scache_store(server_rec *s, UCHAR *id, int idlen,
114 apr_time_t expiry, SSL_SESSION *sess,
117 SSLModConfigRec *mc = myModConfig(s);
118 unsigned char encoded[SSL_SESSION_MAX_DER], *ptr;
122 /* Serialise the session. */
123 len = i2d_SSL_SESSION(sess, NULL);
124 if (len > sizeof encoded) {
125 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(01875)
126 "session is too big (%u bytes)", len);
131 len = i2d_SSL_SESSION(sess, &ptr);
133 if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
137 rv = mc->sesscache->store(mc->sesscache_context, s, id, idlen,
138 expiry, encoded, len, p);
140 if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
144 return rv == APR_SUCCESS ? TRUE : FALSE;
147 SSL_SESSION *ssl_scache_retrieve(server_rec *s, UCHAR *id, int idlen,
150 SSLModConfigRec *mc = myModConfig(s);
151 unsigned char dest[SSL_SESSION_MAX_DER];
152 unsigned int destlen = SSL_SESSION_MAX_DER;
153 const unsigned char *ptr;
156 if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
160 rv = mc->sesscache->retrieve(mc->sesscache_context, s, id, idlen,
163 if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
167 if (rv != APR_SUCCESS) {
173 return d2i_SSL_SESSION(NULL, &ptr, destlen);
176 void ssl_scache_remove(server_rec *s, UCHAR *id, int idlen,
179 SSLModConfigRec *mc = myModConfig(s);
181 if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
185 mc->sesscache->remove(mc->sesscache_context, s, id, idlen, p);
187 if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
192 /* _________________________________________________________________
194 ** SSL Extension to mod_status
195 ** _________________________________________________________________
197 static int ssl_ext_status_hook(request_rec *r, int flags)
199 SSLModConfigRec *mc = myModConfig(r->server);
201 if (mc == NULL || flags & AP_STATUS_SHORT || mc->sesscache == NULL)
204 ap_rputs("<hr>\n", r);
205 ap_rputs("<table cellspacing=0 cellpadding=0>\n", r);
206 ap_rputs("<tr><td bgcolor=\"#000000\">\n", r);
207 ap_rputs("<b><font color=\"#ffffff\" face=\"Arial,Helvetica\">SSL/TLS Session Cache Status:</font></b>\r", r);
208 ap_rputs("</td></tr>\n", r);
209 ap_rputs("<tr><td bgcolor=\"#ffffff\">\n", r);
211 if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
212 ssl_mutex_on(r->server);
215 mc->sesscache->status(mc->sesscache_context, r, flags);
217 if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
218 ssl_mutex_off(r->server);
221 ap_rputs("</td></tr>\n", r);
222 ap_rputs("</table>\n", r);
226 void ssl_scache_status_register(apr_pool_t *p)
228 APR_OPTIONAL_HOOK(ap, status_hook, ssl_ext_status_hook, NULL, NULL,