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 | * The signatures of the `(dbm|mc)_cache_...()` functions may be a bit |
---|
24 | * confusing: "store" and "expire" take a server_rec, "fetch" an |
---|
25 | * mgs_handle_t, and "delete" the `void*` required for a |
---|
26 | * `gnutls_db_remove_func`. The first two have matching `..._session` |
---|
27 | * functions to fit their respective GnuTLS session cache signatures. |
---|
28 | * |
---|
29 | * This is because "store", "expire" (dbm only), and "fetch" are also |
---|
30 | * needed for the OCSP cache. Their `..._session` variants have been |
---|
31 | * created to take care of the session cache specific parts, mainly |
---|
32 | * calculating the DB key from the session ID. They have to match the |
---|
33 | * appropriate GnuTLS DB function signatures. |
---|
34 | * |
---|
35 | * Additionally, there are the `mc_cache_(store|fetch)_generic()` |
---|
36 | * functions. They exist because memcached requires string keys while |
---|
37 | * DBM accepts binary keys, and provide wrappers to turn binary keys |
---|
38 | * into hex strings with a `mod_gnutls:` prefix. |
---|
39 | * |
---|
40 | * To update cached OCSP responses independent of client connections, |
---|
41 | * "store" and "expire" have to work without a connection context. On |
---|
42 | * the other hand "fetch" does not need to do that, because cached |
---|
43 | * OCSP responses will be retrieved for use in client connections. |
---|
44 | */ |
---|
45 | |
---|
46 | #include "gnutls_cache.h" |
---|
47 | #include "mod_gnutls.h" |
---|
48 | #include "gnutls_config.h" |
---|
49 | |
---|
50 | #include <ap_socache.h> |
---|
51 | |
---|
52 | #if HAVE_APR_MEMCACHE |
---|
53 | #include "apr_memcache.h" |
---|
54 | #endif |
---|
55 | |
---|
56 | #include "apr_dbm.h" |
---|
57 | #include <apr_escape.h> |
---|
58 | |
---|
59 | #include "ap_mpm.h" |
---|
60 | #include <util_mutex.h> |
---|
61 | |
---|
62 | #include <unistd.h> |
---|
63 | #include <sys/types.h> |
---|
64 | |
---|
65 | #if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE) |
---|
66 | #include "unixd.h" |
---|
67 | #endif |
---|
68 | |
---|
69 | /** Default session cache timeout */ |
---|
70 | #define MGS_DEFAULT_CACHE_TIMEOUT 300 |
---|
71 | |
---|
72 | /** Prefix for keys used with a memcached cache */ |
---|
73 | #define MC_TAG "mod_gnutls:" |
---|
74 | /** Maximum length of the hex string representation of a GnuTLS |
---|
75 | * session ID: two characters per byte, plus one more for `\0` */ |
---|
76 | #if GNUTLS_VERSION_NUMBER >= 0x030400 |
---|
77 | #define GNUTLS_SESSION_ID_STRING_LEN ((GNUTLS_MAX_SESSION_ID_SIZE * 2) + 1) |
---|
78 | #else |
---|
79 | #define GNUTLS_SESSION_ID_STRING_LEN ((GNUTLS_MAX_SESSION_ID * 2) + 1) |
---|
80 | #endif |
---|
81 | |
---|
82 | #if MODULE_MAGIC_NUMBER_MAJOR < 20081201 |
---|
83 | #define ap_unixd_config unixd_config |
---|
84 | #endif |
---|
85 | |
---|
86 | #ifdef APLOG_USE_MODULE |
---|
87 | APLOG_USE_MODULE(gnutls); |
---|
88 | #endif |
---|
89 | |
---|
90 | /** |
---|
91 | * Turn a GnuTLS session ID into the key format we use with DBM |
---|
92 | * caches. Name the Session ID as `server:port.SessionID` to disallow |
---|
93 | * resuming sessions on different servers. |
---|
94 | * |
---|
95 | * @return `0` on success, `-1` on failure |
---|
96 | */ |
---|
97 | static int mgs_session_id2dbm(conn_rec *c, unsigned char *id, int idlen, |
---|
98 | gnutls_datum_t *dbmkey) |
---|
99 | { |
---|
100 | char sz[GNUTLS_SESSION_ID_STRING_LEN]; |
---|
101 | apr_status_t rv = apr_escape_hex(sz, id, idlen, 0, NULL); |
---|
102 | if (rv != APR_SUCCESS) |
---|
103 | return -1; |
---|
104 | |
---|
105 | char *newkey = apr_psprintf(c->pool, "%s:%d.%s", |
---|
106 | c->base_server->server_hostname, |
---|
107 | c->base_server->port, sz); |
---|
108 | dbmkey->size = strlen(newkey); |
---|
109 | /* signedness does not matter for arbitrary bits */ |
---|
110 | dbmkey->data = (unsigned char*) newkey; |
---|
111 | return 0; |
---|
112 | } |
---|
113 | |
---|
114 | /** The OPENSSL_TIME_FORMAT macro and mgs_time2sz() serve to print |
---|
115 | * time in a format compatible with OpenSSL's `ASN1_TIME_print()` |
---|
116 | * function. */ |
---|
117 | #define OPENSSL_TIME_FORMAT "%b %d %k:%M:%S %Y %Z" |
---|
118 | |
---|
119 | char *mgs_time2sz(time_t in_time, char *str, int strsize) |
---|
120 | { |
---|
121 | apr_time_exp_t vtm; |
---|
122 | apr_size_t ret_size; |
---|
123 | apr_time_t t; |
---|
124 | |
---|
125 | |
---|
126 | apr_time_ansi_put(&t, in_time); |
---|
127 | apr_time_exp_gmt(&vtm, t); |
---|
128 | apr_strftime(str, &ret_size, strsize - 1, OPENSSL_TIME_FORMAT, &vtm); |
---|
129 | |
---|
130 | return str; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | static int socache_store(server_rec *server, gnutls_datum_t key, |
---|
136 | gnutls_datum_t data, apr_time_t expiry) |
---|
137 | { |
---|
138 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
139 | ap_get_module_config(server->module_config, &gnutls_module); |
---|
140 | |
---|
141 | apr_pool_t *spool; |
---|
142 | apr_pool_create(&spool, NULL); |
---|
143 | |
---|
144 | if (sc->cache->prov->flags & AP_SOCACHE_FLAG_NOTMPSAFE) |
---|
145 | apr_global_mutex_lock(sc->cache->mutex); |
---|
146 | apr_status_t rv = sc->cache->prov->store(sc->cache->socache, server, |
---|
147 | key.data, key.size, |
---|
148 | expiry, |
---|
149 | data.data, data.size, |
---|
150 | spool); |
---|
151 | if (sc->cache->prov->flags & AP_SOCACHE_FLAG_NOTMPSAFE) |
---|
152 | apr_global_mutex_unlock(sc->cache->mutex); |
---|
153 | |
---|
154 | if (rv != APR_SUCCESS) |
---|
155 | { |
---|
156 | ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, server, |
---|
157 | "error storing in cache '%s:%s'", |
---|
158 | sc->cache->prov->name, sc->cache_config); |
---|
159 | apr_pool_destroy(spool); |
---|
160 | return -1; |
---|
161 | } |
---|
162 | |
---|
163 | ap_log_error(APLOG_MARK, APLOG_TRACE1, rv, server, |
---|
164 | "stored %u bytes of data (%u byte key) in cache '%s:%s'", |
---|
165 | data.size, key.size, |
---|
166 | sc->cache->prov->name, sc->cache_config); |
---|
167 | apr_pool_destroy(spool); |
---|
168 | return 0; |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | static int socache_store_session(void *baton, gnutls_datum_t key, |
---|
174 | gnutls_datum_t data) |
---|
175 | { |
---|
176 | mgs_handle_t *ctxt = baton; |
---|
177 | gnutls_datum_t dbmkey; |
---|
178 | |
---|
179 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0) |
---|
180 | return -1; |
---|
181 | |
---|
182 | apr_time_t expiry = apr_time_now() + ctxt->sc->cache_timeout; |
---|
183 | |
---|
184 | return socache_store(ctxt->c->base_server, dbmkey, data, expiry); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | // 4K should be enough for OCSP responses and sessions alike |
---|
190 | #define SOCACHE_FETCH_BUF_SIZE 4096 |
---|
191 | static gnutls_datum_t socache_fetch(server_rec *server, gnutls_datum_t key, |
---|
192 | apr_pool_t *pool) |
---|
193 | { |
---|
194 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
195 | ap_get_module_config(server->module_config, &gnutls_module); |
---|
196 | |
---|
197 | gnutls_datum_t data = {NULL, 0}; |
---|
198 | data.data = gnutls_malloc(SOCACHE_FETCH_BUF_SIZE); |
---|
199 | if (data.data == NULL) |
---|
200 | return data; |
---|
201 | data.size = SOCACHE_FETCH_BUF_SIZE; |
---|
202 | |
---|
203 | apr_pool_t *spool; |
---|
204 | apr_pool_create(&spool, pool); |
---|
205 | |
---|
206 | if (sc->cache->prov->flags & AP_SOCACHE_FLAG_NOTMPSAFE) |
---|
207 | apr_global_mutex_lock(sc->cache->mutex); |
---|
208 | apr_status_t rv = sc->cache->prov->retrieve(sc->cache->socache, server, |
---|
209 | key.data, key.size, |
---|
210 | data.data, &data.size, |
---|
211 | spool); |
---|
212 | if (sc->cache->prov->flags & AP_SOCACHE_FLAG_NOTMPSAFE) |
---|
213 | apr_global_mutex_unlock(sc->cache->mutex); |
---|
214 | |
---|
215 | if (rv != APR_SUCCESS) |
---|
216 | { |
---|
217 | /* APR_NOTFOUND means there's no such object. */ |
---|
218 | if (rv == APR_NOTFOUND) |
---|
219 | ap_log_error(APLOG_MARK, APLOG_TRACE1, rv, server, |
---|
220 | "requested entry not found in cache '%s:%s'.", |
---|
221 | sc->cache->prov->name, sc->cache_config); |
---|
222 | else |
---|
223 | ap_log_error(APLOG_MARK, APLOG_WARNING, rv, server, |
---|
224 | "error fetching from cache '%s:%s'", |
---|
225 | sc->cache->prov->name, sc->cache_config); |
---|
226 | /* free unused buffer */ |
---|
227 | gnutls_free(data.data); |
---|
228 | data.data = NULL; |
---|
229 | data.size = 0; |
---|
230 | } |
---|
231 | else |
---|
232 | { |
---|
233 | ap_log_error(APLOG_MARK, APLOG_TRACE1, rv, server, |
---|
234 | "fetched %u bytes from cache '%s:%s'", |
---|
235 | data.size, sc->cache->prov->name, sc->cache_config); |
---|
236 | } |
---|
237 | apr_pool_destroy(spool); |
---|
238 | |
---|
239 | return data; |
---|
240 | } |
---|
241 | |
---|
242 | static gnutls_datum_t socache_fetch_session(void *baton, gnutls_datum_t key) |
---|
243 | { |
---|
244 | gnutls_datum_t data = {NULL, 0}; |
---|
245 | gnutls_datum_t dbmkey; |
---|
246 | mgs_handle_t *ctxt = baton; |
---|
247 | |
---|
248 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &dbmkey) < 0) |
---|
249 | return data; |
---|
250 | |
---|
251 | return socache_fetch(ctxt->c->base_server, dbmkey, ctxt->c->pool); |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | static int socache_delete(void *baton, gnutls_datum_t key) |
---|
257 | { |
---|
258 | gnutls_datum_t tmpkey; |
---|
259 | mgs_handle_t *ctxt = baton; |
---|
260 | |
---|
261 | if (mgs_session_id2dbm(ctxt->c, key.data, key.size, &tmpkey) < 0) |
---|
262 | return -1; |
---|
263 | |
---|
264 | if (ctxt->sc->cache->prov->flags & AP_SOCACHE_FLAG_NOTMPSAFE) |
---|
265 | apr_global_mutex_lock(ctxt->sc->cache->mutex); |
---|
266 | apr_status_t rv = ctxt->sc->cache->prov->remove(ctxt->sc->cache->socache, |
---|
267 | ctxt->c->base_server, |
---|
268 | key.data, key.size, |
---|
269 | ctxt->c->pool); |
---|
270 | if (ctxt->sc->cache->prov->flags & AP_SOCACHE_FLAG_NOTMPSAFE) |
---|
271 | apr_global_mutex_unlock(ctxt->sc->cache->mutex); |
---|
272 | |
---|
273 | if (rv != APR_SUCCESS) { |
---|
274 | ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, |
---|
275 | ctxt->c->base_server, |
---|
276 | "error deleting from cache '%s:%s'", |
---|
277 | ctxt->sc->cache->prov->name, ctxt->sc->cache_config); |
---|
278 | return -1; |
---|
279 | } |
---|
280 | return 0; |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | |
---|
285 | static apr_status_t cleanup_socache(void *data) |
---|
286 | { |
---|
287 | server_rec *s = data; |
---|
288 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
289 | ap_get_module_config(s->module_config, &gnutls_module); |
---|
290 | ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, s, |
---|
291 | "Cleaning up socache '%s:%s'", |
---|
292 | sc->cache->prov->name, sc->cache_config); |
---|
293 | sc->cache->prov->destroy(sc->cache->socache, s); |
---|
294 | return APR_SUCCESS; |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | |
---|
299 | int mgs_cache_post_config(apr_pool_t *pconf, apr_pool_t *ptemp, |
---|
300 | server_rec *s, mgs_srvconf_rec *sc) |
---|
301 | { |
---|
302 | apr_status_t rv = APR_SUCCESS; |
---|
303 | /* if GnuTLSCache was never explicitly set: */ |
---|
304 | if (sc->cache_type == mgs_cache_unset || sc->cache_type == mgs_cache_none) |
---|
305 | { |
---|
306 | sc->cache_type = mgs_cache_none; |
---|
307 | /* Cache disabled, done. */ |
---|
308 | return APR_SUCCESS; |
---|
309 | } |
---|
310 | /* if GnuTLSCacheTimeout was never explicitly set: */ |
---|
311 | if (sc->cache_timeout == MGS_TIMEOUT_UNSET) |
---|
312 | sc->cache_timeout = apr_time_from_sec(MGS_DEFAULT_CACHE_TIMEOUT); |
---|
313 | |
---|
314 | /* initialize mutex only once */ |
---|
315 | if (sc->cache == NULL) |
---|
316 | { |
---|
317 | sc->cache = apr_palloc(pconf, sizeof(struct mgs_cache)); |
---|
318 | rv = ap_global_mutex_create(&sc->cache->mutex, NULL, |
---|
319 | MGS_CACHE_MUTEX_NAME, |
---|
320 | NULL, s, pconf, 0); |
---|
321 | if (rv != APR_SUCCESS) |
---|
322 | return rv; |
---|
323 | } |
---|
324 | |
---|
325 | char *pname = NULL; |
---|
326 | |
---|
327 | if (sc->cache_type == mgs_cache_dbm || sc->cache_type == mgs_cache_gdbm) |
---|
328 | { |
---|
329 | pname = "dbm"; |
---|
330 | sc->cache->store = socache_store; |
---|
331 | sc->cache->fetch = socache_fetch; |
---|
332 | //return dbm_cache_post_config(pconf, s, sc); |
---|
333 | } |
---|
334 | #if HAVE_APR_MEMCACHE |
---|
335 | else if (sc->cache_type == mgs_cache_memcache) |
---|
336 | { |
---|
337 | pname = "memcache"; |
---|
338 | sc->cache->store = socache_store; |
---|
339 | sc->cache->fetch = socache_fetch; |
---|
340 | } |
---|
341 | #endif |
---|
342 | else if (sc->cache_type == mgs_cache_shmcb) |
---|
343 | { |
---|
344 | pname = "shmcb"; |
---|
345 | sc->cache->store = socache_store; |
---|
346 | sc->cache->fetch = socache_fetch; |
---|
347 | } |
---|
348 | |
---|
349 | /* Find the right socache provider */ |
---|
350 | sc->cache->prov = ap_lookup_provider(AP_SOCACHE_PROVIDER_GROUP, |
---|
351 | pname, |
---|
352 | AP_SOCACHE_PROVIDER_VERSION); |
---|
353 | if (sc->cache->prov) |
---|
354 | { |
---|
355 | /* Cache found; create it, passing anything beyond the colon. */ |
---|
356 | const char *err = sc->cache->prov->create(&sc->cache->socache, |
---|
357 | sc->cache_config, |
---|
358 | ptemp, pconf); |
---|
359 | if (err != NULL) |
---|
360 | { |
---|
361 | ap_log_error(APLOG_MARK, APLOG_EMERG, APR_EGENERAL, s, |
---|
362 | "Creating cache '%s:%s' failed: %s", |
---|
363 | pname, sc->cache_config, err); |
---|
364 | return HTTP_INSUFFICIENT_STORAGE; |
---|
365 | } |
---|
366 | ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, s, |
---|
367 | "%s: Socache '%s' created.", __func__, pname); |
---|
368 | |
---|
369 | // TODO: provide hints |
---|
370 | rv = sc->cache->prov->init(sc->cache->socache, |
---|
371 | "mod_gnutls-session", NULL, s, pconf); |
---|
372 | if (rv != APR_SUCCESS) |
---|
373 | { |
---|
374 | ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, |
---|
375 | "Initializing cache '%s:%s' failed!", |
---|
376 | pname, sc->cache_config); |
---|
377 | return HTTP_INSUFFICIENT_STORAGE; |
---|
378 | } |
---|
379 | ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, s, |
---|
380 | "%s: socache '%s:%s' created.", __func__, |
---|
381 | pname, sc->cache_config); |
---|
382 | } |
---|
383 | else |
---|
384 | { |
---|
385 | ap_log_error(APLOG_MARK, APLOG_EMERG, APR_EGENERAL, s, |
---|
386 | "Could not find socache provider '%s', please make sure " |
---|
387 | "that the provider name is valid and the " |
---|
388 | "appropriate mod_socache submodule is loaded.", pname); |
---|
389 | return HTTP_NOT_FOUND; |
---|
390 | } |
---|
391 | |
---|
392 | apr_pool_pre_cleanup_register(pconf, s, cleanup_socache); |
---|
393 | |
---|
394 | return APR_SUCCESS; |
---|
395 | } |
---|
396 | |
---|
397 | int mgs_cache_child_init(apr_pool_t * p, |
---|
398 | server_rec * s, |
---|
399 | mgs_srvconf_rec * sc) |
---|
400 | { |
---|
401 | /* reinit cache mutex */ |
---|
402 | const char *lockfile = apr_global_mutex_lockfile(sc->cache->mutex); |
---|
403 | apr_status_t rv = apr_global_mutex_child_init(&sc->cache->mutex, |
---|
404 | lockfile, p); |
---|
405 | if (rv != APR_SUCCESS) |
---|
406 | ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, |
---|
407 | "Failed to reinit mutex '%s'", MGS_CACHE_MUTEX_NAME); |
---|
408 | |
---|
409 | return 0; |
---|
410 | } |
---|
411 | |
---|
412 | #include <assert.h> |
---|
413 | |
---|
414 | int mgs_cache_session_init(mgs_handle_t * ctxt) |
---|
415 | { |
---|
416 | if (ctxt->sc->cache_type != mgs_cache_none) |
---|
417 | { |
---|
418 | gnutls_db_set_retrieve_function(ctxt->session, |
---|
419 | socache_fetch_session); |
---|
420 | gnutls_db_set_remove_function(ctxt->session, |
---|
421 | socache_delete); |
---|
422 | gnutls_db_set_store_function(ctxt->session, |
---|
423 | socache_store_session); |
---|
424 | gnutls_db_set_ptr(ctxt->session, ctxt); |
---|
425 | } |
---|
426 | return 0; |
---|
427 | } |
---|