1 | /* |
---|
2 | * Copyright 2004-2005 Paul Querna |
---|
3 | * Copyright 2008 Nikos Mavrogiannopoulos |
---|
4 | * Copyright 2011 Dash Shendy |
---|
5 | * Copyright 2015-2018 Fiona Klute |
---|
6 | * |
---|
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
8 | * you may not use this file except in compliance with the License. |
---|
9 | * You may obtain a copy of the License at |
---|
10 | * |
---|
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
12 | * |
---|
13 | * Unless required by applicable law or agreed to in writing, software |
---|
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
16 | * See the License for the specific language governing permissions and |
---|
17 | * limitations under the License. |
---|
18 | */ |
---|
19 | |
---|
20 | /** |
---|
21 | * @file gnutls_cache.c |
---|
22 | * |
---|
23 | * This file contains the cache implementation used for session |
---|
24 | * caching and OCSP stapling. The `socache_*_session` functions |
---|
25 | * implement the GnuTLS session cache API using the configured cache, |
---|
26 | * using mgs_cache_store() and mgs_cache_fetch() as appropriate (see |
---|
27 | * gnutls_cache.h). |
---|
28 | */ |
---|
29 | |
---|
30 | #include "gnutls_cache.h" |
---|
31 | #include "mod_gnutls.h" |
---|
32 | #include "gnutls_config.h" |
---|
33 | |
---|
34 | #include <ap_socache.h> |
---|
35 | #include <apr_escape.h> |
---|
36 | #include <util_mutex.h> |
---|
37 | |
---|
38 | /** Default session cache timeout */ |
---|
39 | #define MGS_DEFAULT_CACHE_TIMEOUT 300 |
---|
40 | |
---|
41 | /** Maximum length of the hex string representation of a GnuTLS |
---|
42 | * session ID: two characters per byte, plus one more for `\0` */ |
---|
43 | #if GNUTLS_VERSION_NUMBER >= 0x030400 |
---|
44 | #define GNUTLS_SESSION_ID_STRING_LEN ((GNUTLS_MAX_SESSION_ID_SIZE * 2) + 1) |
---|
45 | #else |
---|
46 | #define GNUTLS_SESSION_ID_STRING_LEN ((GNUTLS_MAX_SESSION_ID * 2) + 1) |
---|
47 | #endif |
---|
48 | |
---|
49 | #ifdef APLOG_USE_MODULE |
---|
50 | APLOG_USE_MODULE(gnutls); |
---|
51 | #endif |
---|
52 | |
---|
53 | /** |
---|
54 | * Turn a GnuTLS session ID into the key format we use for |
---|
55 | * caches. Name the Session ID as `server:port.SessionID` to disallow |
---|
56 | * resuming sessions on different servers. |
---|
57 | * |
---|
58 | * @return `0` on success, `-1` on failure |
---|
59 | */ |
---|
60 | static int mgs_session_id2dbm(conn_rec *c, unsigned char *id, int idlen, |
---|
61 | gnutls_datum_t *dbmkey) |
---|
62 | { |
---|
63 | char sz[GNUTLS_SESSION_ID_STRING_LEN]; |
---|
64 | apr_status_t rv = apr_escape_hex(sz, id, idlen, 0, NULL); |
---|
65 | if (rv != APR_SUCCESS) |
---|
66 | return -1; |
---|
67 | |
---|
68 | char *newkey = apr_psprintf(c->pool, "%s:%d.%s", |
---|
69 | c->base_server->server_hostname, |
---|
70 | c->base_server->port, sz); |
---|
71 | dbmkey->size = strlen(newkey); |
---|
72 | /* signedness does not matter for arbitrary bits */ |
---|
73 | dbmkey->data = (unsigned char*) newkey; |
---|
74 | return 0; |
---|
75 | } |
---|
76 | |
---|
77 | /** The OPENSSL_TIME_FORMAT macro and mgs_time2sz() serve to print |
---|
78 | * time in a format compatible with OpenSSL's `ASN1_TIME_print()` |
---|
79 | * function. */ |
---|
80 | #define OPENSSL_TIME_FORMAT "%b %d %k:%M:%S %Y %Z" |
---|
81 | |
---|
82 | char *mgs_time2sz(time_t in_time, char *str, int strsize) |
---|
83 | { |
---|
84 | apr_time_exp_t vtm; |
---|
85 | apr_size_t ret_size; |
---|
86 | apr_time_t t; |
---|
87 | |
---|
88 | |
---|
89 | apr_time_ansi_put(&t, in_time); |
---|
90 | apr_time_exp_gmt(&vtm, t); |
---|
91 | apr_strftime(str, &ret_size, strsize - 1, OPENSSL_TIME_FORMAT, &vtm); |
---|
92 | |
---|
93 | return str; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | int mgs_cache_store(mgs_cache_t cache, server_rec *server, |
---|
99 | gnutls_datum_t key, gnutls_datum_t data, |
---|
100 | apr_time_t expiry) |
---|
101 | { |
---|
102 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
103 | ap_get_module_config(server->module_config, &gnutls_module); |
---|
104 | |
---|
105 | apr_pool_t *spool; |
---|
106 | apr_pool_create(&spool, NULL); |
---|
107 | |
---|
108 | if (cache->prov->flags & AP_SOCACHE_FLAG_NOTMPSAFE) |
---|
109 | apr_global_mutex_lock(cache->mutex); |
---|
110 | apr_status_t rv = cache->prov->store(cache->socache, server, |
---|
111 | key.data, key.size, |
---|
112 | expiry, |
---|
113 | data.data, data.size, |
---|
114 | spool); |
---|
115 | if (cache->prov->flags & AP_SOCACHE_FLAG_NOTMPSAFE) |
---|
116 | apr_global_mutex_unlock(cache->mutex); |
---|
117 | |
---|
118 | if (rv != APR_SUCCESS) |
---|
119 | { |
---|
120 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, server, |
---|
121 | "error storing in cache '%s:%s'", |
---|
122 | cache->prov->name, sc->cache->config); |
---|
123 | apr_pool_destroy(spool); |
---|
124 | return -1; |
---|
125 | } |
---|
126 | |
---|
127 | ap_log_error(APLOG_MARK, APLOG_TRACE1, rv, server, |
---|
128 | "stored %u bytes of data (%u byte key) in cache '%s:%s'", |
---|
129 | data.size, key.size, |
---|
130 | cache->prov->name, sc->cache->config); |
---|
131 | apr_pool_destroy(spool); |
---|
132 | return 0; |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | |
---|
137 | /** |
---|
138 | * Store function for the GnuTLS session cache, see |
---|
139 | * gnutls_db_set_store_function(). |
---|
140 | * |
---|
141 | * @param baton mgs_handle_t for the connection, as set via |
---|
142 | * gnutls_db_set_ptr() |
---|
143 | * |
---|
144 | * @param key object key to store |
---|
145 | * |
---|
146 | * @param data the object to store |
---|
147 | * |
---|
148 | * @return `0` in case of success, `-1` in case of failure |
---|
149 | */ |
---|
150 | static int socache_store_session(void *baton, gnutls_datum_t key, |
---|
151 | gnutls_datum_t data) |
---|
152 | { |
---|
153 | mgs_handle_t *ctxt = baton; |
---|
154 | gnutls_datum_t dbmkey; |
---|
155 | |
---|
156 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0) |
---|
157 | return -1; |
---|
158 | |
---|
159 | apr_time_t expiry = apr_time_now() + ctxt->sc->cache_timeout; |
---|
160 | |
---|
161 | return mgs_cache_store(ctxt->sc->cache, ctxt->c->base_server, |
---|
162 | dbmkey, data, expiry); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | // 4K should be enough for OCSP responses and sessions alike |
---|
168 | #define SOCACHE_FETCH_BUF_SIZE 4096 |
---|
169 | gnutls_datum_t mgs_cache_fetch(mgs_cache_t cache, server_rec *server, |
---|
170 | gnutls_datum_t key, apr_pool_t *pool) |
---|
171 | { |
---|
172 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
173 | ap_get_module_config(server->module_config, &gnutls_module); |
---|
174 | |
---|
175 | gnutls_datum_t data = {NULL, 0}; |
---|
176 | data.data = gnutls_malloc(SOCACHE_FETCH_BUF_SIZE); |
---|
177 | if (data.data == NULL) |
---|
178 | return data; |
---|
179 | data.size = SOCACHE_FETCH_BUF_SIZE; |
---|
180 | |
---|
181 | apr_pool_t *spool; |
---|
182 | apr_pool_create(&spool, pool); |
---|
183 | |
---|
184 | if (cache->prov->flags & AP_SOCACHE_FLAG_NOTMPSAFE) |
---|
185 | apr_global_mutex_lock(cache->mutex); |
---|
186 | apr_status_t rv = cache->prov->retrieve(cache->socache, server, |
---|
187 | key.data, key.size, |
---|
188 | data.data, &data.size, |
---|
189 | spool); |
---|
190 | if (cache->prov->flags & AP_SOCACHE_FLAG_NOTMPSAFE) |
---|
191 | apr_global_mutex_unlock(cache->mutex); |
---|
192 | |
---|
193 | if (rv != APR_SUCCESS) |
---|
194 | { |
---|
195 | /* APR_NOTFOUND means there's no such object. */ |
---|
196 | if (rv == APR_NOTFOUND) |
---|
197 | ap_log_error(APLOG_MARK, APLOG_TRACE1, rv, server, |
---|
198 | "requested entry not found in cache '%s:%s'.", |
---|
199 | cache->prov->name, sc->cache->config); |
---|
200 | else |
---|
201 | ap_log_error(APLOG_MARK, APLOG_WARNING, rv, server, |
---|
202 | "error fetching from cache '%s:%s'", |
---|
203 | cache->prov->name, sc->cache->config); |
---|
204 | /* free unused buffer */ |
---|
205 | gnutls_free(data.data); |
---|
206 | data.data = NULL; |
---|
207 | data.size = 0; |
---|
208 | } |
---|
209 | else |
---|
210 | { |
---|
211 | ap_log_error(APLOG_MARK, APLOG_TRACE1, rv, server, |
---|
212 | "fetched %u bytes from cache '%s:%s'", |
---|
213 | data.size, cache->prov->name, sc->cache->config); |
---|
214 | } |
---|
215 | apr_pool_destroy(spool); |
---|
216 | |
---|
217 | return data; |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | |
---|
222 | /** |
---|
223 | * Fetch function for the GnuTLS session cache, see |
---|
224 | * gnutls_db_set_retrieve_function(). |
---|
225 | * |
---|
226 | * *Warning*: The `data` element of the returned `gnutls_datum_t` is |
---|
227 | * allocated using `gnutls_malloc()` for compatibility with the GnuTLS |
---|
228 | * session caching API, and must be released using `gnutls_free()`. |
---|
229 | * |
---|
230 | * @param baton mgs_handle_t for the connection, as set via |
---|
231 | * gnutls_db_set_ptr() |
---|
232 | * |
---|
233 | * @param key object key to fetch |
---|
234 | * |
---|
235 | * @return the requested cache entry, or `{NULL, 0}` |
---|
236 | */ |
---|
237 | static gnutls_datum_t socache_fetch_session(void *baton, gnutls_datum_t key) |
---|
238 | { |
---|
239 | gnutls_datum_t data = {NULL, 0}; |
---|
240 | gnutls_datum_t dbmkey; |
---|
241 | mgs_handle_t *ctxt = baton; |
---|
242 | |
---|
243 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0) |
---|
244 | return data; |
---|
245 | |
---|
246 | return mgs_cache_fetch(ctxt->sc->cache, ctxt->c->base_server, |
---|
247 | dbmkey, ctxt->c->pool); |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | |
---|
252 | /** |
---|
253 | * Remove function for the GnuTLS session cache, see |
---|
254 | * gnutls_db_set_remove_function(). |
---|
255 | * |
---|
256 | * @param baton mgs_handle_t for the connection, as set via |
---|
257 | * gnutls_db_set_ptr() |
---|
258 | * |
---|
259 | * @param key object key to remove |
---|
260 | * |
---|
261 | * @return `0` in case of success, `-1` in case of failure |
---|
262 | */ |
---|
263 | static int socache_delete_session(void *baton, gnutls_datum_t key) |
---|
264 | { |
---|
265 | gnutls_datum_t tmpkey; |
---|
266 | mgs_handle_t *ctxt = baton; |
---|
267 | |
---|
268 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &tmpkey) < 0) |
---|
269 | return -1; |
---|
270 | |
---|
271 | if (ctxt->sc->cache->prov->flags & AP_SOCACHE_FLAG_NOTMPSAFE) |
---|
272 | apr_global_mutex_lock(ctxt->sc->cache->mutex); |
---|
273 | apr_status_t rv = ctxt->sc->cache->prov->remove(ctxt->sc->cache->socache, |
---|
274 | ctxt->c->base_server, |
---|
275 | key.data, key.size, |
---|
276 | ctxt->c->pool); |
---|
277 | if (ctxt->sc->cache->prov->flags & AP_SOCACHE_FLAG_NOTMPSAFE) |
---|
278 | apr_global_mutex_unlock(ctxt->sc->cache->mutex); |
---|
279 | |
---|
280 | if (rv != APR_SUCCESS) { |
---|
281 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, |
---|
282 | ctxt->c->base_server, |
---|
283 | "error deleting from cache '%s:%s'", |
---|
284 | ctxt->sc->cache->prov->name, ctxt->sc->cache->config); |
---|
285 | return -1; |
---|
286 | } |
---|
287 | return 0; |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | |
---|
292 | static apr_status_t cleanup_socache(void *data) |
---|
293 | { |
---|
294 | server_rec *s = data; |
---|
295 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
296 | ap_get_module_config(s->module_config, &gnutls_module); |
---|
297 | ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, s, |
---|
298 | "Cleaning up socache '%s:%s'", |
---|
299 | sc->cache->prov->name, sc->cache->config); |
---|
300 | sc->cache->prov->destroy(sc->cache->socache, s); |
---|
301 | return APR_SUCCESS; |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | |
---|
306 | int mgs_cache_post_config(apr_pool_t *pconf, apr_pool_t *ptemp, |
---|
307 | server_rec *s, mgs_srvconf_rec *sc) |
---|
308 | { |
---|
309 | apr_status_t rv = APR_SUCCESS; |
---|
310 | /* GnuTLSCache was never explicitly set or is disabled: */ |
---|
311 | if (sc->cache_enable == GNUTLS_ENABLED_UNSET |
---|
312 | || sc->cache_enable == GNUTLS_ENABLED_FALSE) |
---|
313 | { |
---|
314 | sc->cache_enable = GNUTLS_ENABLED_FALSE; |
---|
315 | /* Cache disabled, done. */ |
---|
316 | return APR_SUCCESS; |
---|
317 | } |
---|
318 | /* if GnuTLSCacheTimeout was never explicitly set: */ |
---|
319 | if (sc->cache_timeout == MGS_TIMEOUT_UNSET) |
---|
320 | sc->cache_timeout = apr_time_from_sec(MGS_DEFAULT_CACHE_TIMEOUT); |
---|
321 | |
---|
322 | /* initialize cache structure and mutex if needed */ |
---|
323 | if (sc->cache == NULL) |
---|
324 | { |
---|
325 | sc->cache = apr_pcalloc(pconf, sizeof(struct mgs_cache)); |
---|
326 | rv = ap_global_mutex_create(&sc->cache->mutex, NULL, |
---|
327 | MGS_CACHE_MUTEX_NAME, |
---|
328 | NULL, s, pconf, 0); |
---|
329 | if (rv != APR_SUCCESS) |
---|
330 | return rv; |
---|
331 | } |
---|
332 | |
---|
333 | /* Find the right socache provider */ |
---|
334 | sc->cache->prov = ap_lookup_provider(AP_SOCACHE_PROVIDER_GROUP, |
---|
335 | sc->cache_type, |
---|
336 | AP_SOCACHE_PROVIDER_VERSION); |
---|
337 | if (sc->cache->prov) |
---|
338 | { |
---|
339 | /* Create and configure the cache instance. */ |
---|
340 | sc->cache->config = sc->cache_config; |
---|
341 | const char *err = sc->cache->prov->create(&sc->cache->socache, |
---|
342 | sc->cache->config, |
---|
343 | ptemp, pconf); |
---|
344 | if (err != NULL) |
---|
345 | { |
---|
346 | ap_log_error(APLOG_MARK, APLOG_EMERG, APR_EGENERAL, s, |
---|
347 | "Creating cache '%s:%s' failed: %s", |
---|
348 | sc->cache_type, sc->cache->config, err); |
---|
349 | return HTTP_INSUFFICIENT_STORAGE; |
---|
350 | } |
---|
351 | ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, s, |
---|
352 | "%s: Socache '%s' created.", __func__, sc->cache_type); |
---|
353 | |
---|
354 | // TODO: provide hints |
---|
355 | rv = sc->cache->prov->init(sc->cache->socache, |
---|
356 | "mod_gnutls-session", NULL, s, pconf); |
---|
357 | if (rv != APR_SUCCESS) |
---|
358 | { |
---|
359 | ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, |
---|
360 | "Initializing cache '%s:%s' failed!", |
---|
361 | sc->cache_type, sc->cache->config); |
---|
362 | return HTTP_INSUFFICIENT_STORAGE; |
---|
363 | } |
---|
364 | ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, s, |
---|
365 | "%s: socache '%s:%s' created.", __func__, |
---|
366 | sc->cache_type, sc->cache->config); |
---|
367 | } |
---|
368 | else |
---|
369 | { |
---|
370 | ap_log_error(APLOG_MARK, APLOG_EMERG, APR_EGENERAL, s, |
---|
371 | "Could not find socache provider '%s', please make sure " |
---|
372 | "that the provider name is valid and the " |
---|
373 | "appropriate mod_socache submodule is loaded.", |
---|
374 | sc->cache_type); |
---|
375 | return HTTP_NOT_FOUND; |
---|
376 | } |
---|
377 | |
---|
378 | apr_pool_pre_cleanup_register(pconf, s, cleanup_socache); |
---|
379 | |
---|
380 | return APR_SUCCESS; |
---|
381 | } |
---|
382 | |
---|
383 | int mgs_cache_child_init(apr_pool_t * p, |
---|
384 | server_rec * s, |
---|
385 | mgs_srvconf_rec * sc) |
---|
386 | { |
---|
387 | /* reinit cache mutex */ |
---|
388 | const char *lockfile = apr_global_mutex_lockfile(sc->cache->mutex); |
---|
389 | apr_status_t rv = apr_global_mutex_child_init(&sc->cache->mutex, |
---|
390 | lockfile, p); |
---|
391 | if (rv != APR_SUCCESS) |
---|
392 | ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, |
---|
393 | "Failed to reinit mutex '%s'", MGS_CACHE_MUTEX_NAME); |
---|
394 | |
---|
395 | return 0; |
---|
396 | } |
---|
397 | |
---|
398 | int mgs_cache_session_init(mgs_handle_t * ctxt) |
---|
399 | { |
---|
400 | if (ctxt->sc->cache_enable) |
---|
401 | { |
---|
402 | gnutls_db_set_retrieve_function(ctxt->session, |
---|
403 | socache_fetch_session); |
---|
404 | gnutls_db_set_remove_function(ctxt->session, |
---|
405 | socache_delete_session); |
---|
406 | gnutls_db_set_store_function(ctxt->session, |
---|
407 | socache_store_session); |
---|
408 | gnutls_db_set_ptr(ctxt->session, ctxt); |
---|
409 | } |
---|
410 | return 0; |
---|
411 | } |
---|