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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 /*                      _             _
18  *  _ __ ___   ___   __| |    ___ ___| |  mod_ssl
19  * | '_ ` _ \ / _ \ / _` |   / __/ __| |  Apache Interface to OpenSSL
20  * | | | | | | (_) | (_| |   \__ \__ \ |
21  * |_| |_| |_|\___/ \__,_|___|___/___/_|
22  *                      |_____|
23  *  ssl_scache.c
24  *  Session Cache Abstraction
25  */
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.''
30                                                  -- Unknown         */
31 #include "ssl_private.h"
32 #include "mod_status.h"
33 
34 /*  _________________________________________________________________
35 **
36 **  Session Cache: Common Abstraction Layer
37 **  _________________________________________________________________
38 */
39 
ssl_scache_init(server_rec * s,apr_pool_t * p)40 void ssl_scache_init(server_rec *s, apr_pool_t *p)
41 {
42     SSLModConfigRec *mc = myModConfig(s);
43     apr_status_t rv;
44     struct ap_socache_hints hints;
45 
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)
52         return;
53 
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;
60 
61         rv = mc->stapling_cache->init(mc->stapling_cache_context,
62                                      "mod_ssl-stapling", &hints, s, p);
63         if (rv) {
64             ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01872)
65                          "Could not initialize stapling cache. Exiting.");
66             ssl_die(s);
67         }
68     }
69 #endif
70 
71     /*
72      * Warn the user that he should use the session cache.
73      * But we can operate without it, of course.
74      */
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]");
79         return;
80     }
81 
82     memset(&hints, 0, sizeof hints);
83     hints.avg_obj_size = 150;
84     hints.avg_id_len = 30;
85     hints.expiry_interval = 30;
86 
87     rv = mc->sesscache->init(mc->sesscache_context, "mod_ssl-session", &hints, s, p);
88     if (rv) {
89         ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01874)
90                      "Could not initialize session cache. Exiting.");
91         ssl_die(s);
92     }
93 }
94 
ssl_scache_kill(server_rec * s)95 void ssl_scache_kill(server_rec *s)
96 {
97     SSLModConfigRec *mc = myModConfig(s);
98 
99     if (mc->sesscache) {
100         mc->sesscache->destroy(mc->sesscache_context, s);
101     }
102 
103 #ifdef HAVE_OCSP_STAPLING
104     if (mc->stapling_cache) {
105         mc->stapling_cache->destroy(mc->stapling_cache_context, s);
106     }
107 #endif
108 
109 }
110 
ssl_scache_store(server_rec * s,UCHAR * id,int idlen,apr_time_t expiry,SSL_SESSION * sess,apr_pool_t * p)111 BOOL ssl_scache_store(server_rec *s, UCHAR *id, int idlen,
112                       apr_time_t expiry, SSL_SESSION *sess,
113                       apr_pool_t *p)
114 {
115     SSLModConfigRec *mc = myModConfig(s);
116     unsigned char encoded[SSL_SESSION_MAX_DER], *ptr;
117     unsigned int len;
118     apr_status_t rv;
119 
120     /* Serialise the session. */
121     len = i2d_SSL_SESSION(sess, NULL);
122     if (len > sizeof encoded) {
123         ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(01875)
124                      "session is too big (%u bytes)", len);
125         return FALSE;
126     }
127 
128     ptr = encoded;
129     len = i2d_SSL_SESSION(sess, &ptr);
130 
131     if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
132         ssl_mutex_on(s);
133     }
134 
135     rv = mc->sesscache->store(mc->sesscache_context, s, id, idlen,
136                               expiry, encoded, len, p);
137 
138     if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
139         ssl_mutex_off(s);
140     }
141 
142     return rv == APR_SUCCESS ? TRUE : FALSE;
143 }
144 
ssl_scache_retrieve(server_rec * s,UCHAR * id,int idlen,apr_pool_t * p)145 SSL_SESSION *ssl_scache_retrieve(server_rec *s, UCHAR *id, int idlen,
146                                  apr_pool_t *p)
147 {
148     SSLModConfigRec *mc = myModConfig(s);
149     unsigned char dest[SSL_SESSION_MAX_DER];
150     unsigned int destlen = SSL_SESSION_MAX_DER;
151     MODSSL_D2I_SSL_SESSION_CONST unsigned char *ptr;
152     apr_status_t rv;
153 
154     if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
155         ssl_mutex_on(s);
156     }
157 
158     rv = mc->sesscache->retrieve(mc->sesscache_context, s, id, idlen,
159                                  dest, &destlen, p);
160 
161     if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
162         ssl_mutex_off(s);
163     }
164 
165     if (rv != APR_SUCCESS) {
166         return NULL;
167     }
168 
169     ptr = dest;
170 
171     return d2i_SSL_SESSION(NULL, &ptr, destlen);
172 }
173 
ssl_scache_remove(server_rec * s,UCHAR * id,int idlen,apr_pool_t * p)174 void ssl_scache_remove(server_rec *s, UCHAR *id, int idlen,
175                        apr_pool_t *p)
176 {
177     SSLModConfigRec *mc = myModConfig(s);
178 
179     if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
180         ssl_mutex_on(s);
181     }
182 
183     mc->sesscache->remove(mc->sesscache_context, s, id, idlen, p);
184 
185     if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
186         ssl_mutex_off(s);
187     }
188 }
189 
190 /*  _________________________________________________________________
191 **
192 **  SSL Extension to mod_status
193 **  _________________________________________________________________
194 */
ssl_ext_status_hook(request_rec * r,int flags)195 static int ssl_ext_status_hook(request_rec *r, int flags)
196 {
197     SSLModConfigRec *mc = myModConfig(r->server);
198 
199     if (mc == NULL || flags & AP_STATUS_SHORT || mc->sesscache == NULL)
200         return OK;
201 
202     ap_rputs("<hr>\n", r);
203     ap_rputs("<table cellspacing=0 cellpadding=0>\n", r);
204     ap_rputs("<tr><td bgcolor=\"#000000\">\n", r);
205     ap_rputs("<b><font color=\"#ffffff\" face=\"Arial,Helvetica\">SSL/TLS Session Cache Status:</font></b>\r", r);
206     ap_rputs("</td></tr>\n", r);
207     ap_rputs("<tr><td bgcolor=\"#ffffff\">\n", r);
208 
209     if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
210         ssl_mutex_on(r->server);
211     }
212 
213     mc->sesscache->status(mc->sesscache_context, r, flags);
214 
215     if (mc->sesscache->flags & AP_SOCACHE_FLAG_NOTMPSAFE) {
216         ssl_mutex_off(r->server);
217     }
218 
219     ap_rputs("</td></tr>\n", r);
220     ap_rputs("</table>\n", r);
221     return OK;
222 }
223 
ssl_scache_status_register(apr_pool_t * p)224 void ssl_scache_status_register(apr_pool_t *p)
225 {
226     APR_OPTIONAL_HOOK(ap, status_hook, ssl_ext_status_hook, NULL, NULL,
227                       APR_HOOK_MIDDLE);
228 }
229 
230