Apache HTTPD
tls_cache.c
Go to the documentation of this file.
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#include <assert.h>
17#include <apr_lib.h>
18#include <apr_strings.h>
19#include <apr_hash.h>
20
21#include <httpd.h>
22#include <http_connection.h>
23#include <http_log.h>
24#include <ap_socache.h>
25#include <util_mutex.h>
26
27#include <rustls.h>
28
29#include "tls_conf.h"
30#include "tls_core.h"
31#include "tls_cache.h"
32
33extern module AP_MODULE_DECLARE_DATA tls_module;
35
36#define TLS_CACHE_DEF_PROVIDER "shmcb"
37#define TLS_CACHE_DEF_DIR "tls"
38#define TLS_CACHE_DEF_FILE "session_cache"
39#define TLS_CACHE_DEF_SIZE 512000
40
41static const char *cache_provider_unknown(const char *name, apr_pool_t *p)
42{
44 const char *known_names;
45
49 return apr_psprintf(p, "cache type '%s' not supported "
50 "(known names: %s). Maybe you need to load the "
51 "appropriate socache module (mod_socache_%s?).",
53}
54
56{
57 (void)plog;
58 (void)ptemp;
59 /* we make this visible, in case someone wants to configure it.
60 * this does not mean that we will really use it, which is determined
61 * by configuration and cache provider capabilities. */
63}
64
66{
67 const char *err = NULL;
68 const char *name, *args = NULL;
69 apr_status_t rv;
70
71 if (gconf->session_cache) {
72 goto cleanup;
73 }
74 else if (!apr_strnatcasecmp("none", gconf->session_cache_spec)) {
75 gconf->session_cache_provider = NULL;
76 gconf->session_cache = NULL;
77 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, gconf->ap_server, APLOGNO(10346)
78 "session cache explicitly disabled");
79 goto cleanup;
80 }
81 else if (!apr_strnatcasecmp("default", gconf->session_cache_spec)) {
82 const char *path = TLS_CACHE_DEF_DIR;
83
84#if AP_MODULE_MAGIC_AT_LEAST(20180906, 2)
86#endif
87 gconf->session_cache_spec = apr_psprintf(p, "%s:%s/%s(%ld)",
89 gconf->session_cache_spec = "shmcb:mod_tls-sesss(64000)";
90 }
91
92 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, gconf->ap_server, APLOGNO(10347)
93 "Using session cache: %s", gconf->session_cache_spec);
94 name = gconf->session_cache_spec;
95 args = ap_strchr((char*)name, ':');
96 if (args) {
98 ++args;
99 }
100 gconf->session_cache_provider = ap_lookup_provider(AP_SOCACHE_PROVIDER_GROUP,
102 if (!gconf->session_cache_provider) {
104 goto cleanup;
105 }
106 err = gconf->session_cache_provider->create(&gconf->session_cache, args, ptemp, p);
107 if (err != NULL) goto cleanup;
108
109 if (gconf->session_cache_provider->flags & AP_SOCACHE_FLAG_NOTMPSAFE
110 && !gconf->session_cache_mutex) {
111 /* we need a global lock to access the cache */
112 rv = ap_global_mutex_create(&gconf->session_cache_mutex, NULL,
113 TLS_SESSION_CACHE_MUTEX_TYPE, NULL, gconf->ap_server, p, 0);
114 if (APR_SUCCESS != rv) {
115 err = apr_psprintf(p, "error setting up global %s mutex: %d",
117 gconf->session_cache_mutex = NULL;
118 goto cleanup;
119 }
120 }
121
122cleanup:
123 if (NULL != err) {
124 gconf->session_cache_provider = NULL;
125 gconf->session_cache = NULL;
126 }
127 return err;
128}
129
131 const char *spec, tls_conf_global_t *gconf, apr_pool_t *p, apr_pool_t *ptemp)
132{
133 gconf->session_cache_spec = spec;
134 return cache_init(gconf, p, ptemp);
135}
136
138{
140 const char *err;
142
143 err = cache_init(sc->global, p, ptemp);
144 if (err) {
146 "session cache [%s] could not be initialized, will continue "
147 "without session one. Since this will impact performance, "
148 "consider making use of the 'TLSSessionCache' directive. The "
149 "error was: %s", sc->global->session_cache_spec, err);
150 }
151
152 if (sc->global->session_cache) {
153 struct ap_socache_hints hints;
154
155 ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, s, "provider init session cache [%s]",
157 memset(&hints, 0, sizeof(hints));
158 hints.avg_obj_size = 100;
159 hints.avg_id_len = 33;
160 hints.expiry_interval = 30;
161
163 sc->global->session_cache, "mod_tls-sess", &hints, s, p);
164 if (APR_SUCCESS != rv) {
166 "error initializing session cache.");
167 }
168 }
169 return rv;
170}
171
173{
175 const char *lockfile;
176 apr_status_t rv;
177
178 if (sc->global->session_cache_mutex) {
181 if (APR_SUCCESS != rv) {
183 "Cannot reinit %s mutex (file `%s`)",
185 }
186 }
187}
188
196
198{
199 if (gconf->session_cache_mutex) {
200 apr_status_t rv = apr_global_mutex_lock(gconf->session_cache_mutex);
201 if (APR_SUCCESS != rv) {
202 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, gconf->ap_server, APLOGNO(10351)
203 "Failed to acquire TLS session cache lock");
204 }
205 }
206}
207
209{
210 if (gconf->session_cache_mutex) {
211 apr_status_t rv = apr_global_mutex_unlock(gconf->session_cache_mutex);
212 if (APR_SUCCESS != rv) {
213 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, gconf->ap_server, APLOGNO(10352)
214 "Failed to release TLS session cache lock");
215 }
216 }
217}
218
220 void *userdata,
221 const rustls_slice_bytes *key,
222 int remove_after,
223 unsigned char *buf,
224 size_t count,
225 size_t *out_n)
226{
227 conn_rec *c = userdata;
231 unsigned int vlen, klen;
232 const unsigned char *kdata;
233
234 if (!sc->global->session_cache) goto not_found;
236
237 kdata = key->data;
238 klen = (unsigned int)key->len;
239 vlen = (unsigned int)count;
241 sc->global->session_cache, cc->server, kdata, klen, buf, &vlen, c->pool);
242
243 if (APLOGctrace4(c)) {
245 ap_log_cerror(APLOG_MARK, APLOG_TRACE4, rv, c, "retrieve key %d[%8x], found %d val",
246 klen, apr_hashfunc_default((const char*)kdata, &n), vlen);
247 }
248 if (remove_after || (APR_SUCCESS != rv && !APR_STATUS_IS_NOTFOUND(rv))) {
250 sc->global->session_cache, cc->server, key->data, klen, c->pool);
251 }
252
254 if (APR_SUCCESS != rv) goto not_found;
255 cc->session_id_cache_hit = 1;
256 *out_n = count;
257 return RUSTLS_RESULT_OK;
258
260 *out_n = 0;
262}
263
265 void *userdata,
266 const rustls_slice_bytes *key,
267 const rustls_slice_bytes *val)
268{
269 conn_rec *c = userdata;
274 unsigned int klen, vlen;
275 const unsigned char *kdata;
276
277 if (!sc->global->session_cache) goto not_stored;
279
281 kdata = key->data;
282 klen = (unsigned int)key->len;
283 vlen = (unsigned int)val->len;
286 (unsigned char*)val->data, vlen, c->pool);
287 if (APLOGctrace4(c)) {
289 "stored %d key bytes, with %d val bytes", klen, vlen);
290 }
292 if (APR_SUCCESS != rv) goto not_stored;
293 return RUSTLS_RESULT_OK;
294
297}
298
int n
Definition ap_regex.h:278
Small object cache provider interface.
APR Hash Tables.
APR general purpose library routines.
APR Strings library.
static apr_pool_t * pconf
Definition event.c:441
#define APLOG_USE_MODULE(foo)
request_rec int int apr_table_t const char * path
#define APLOGNO(n)
Definition http_log.h:117
#define APLOG_TRACE4
Definition http_log.h:75
#define APLOG_ERR
Definition http_log.h:67
#define APLOG_TRACE3
Definition http_log.h:74
#define ap_log_error
Definition http_log.h:370
#define ap_log_cerror
Definition http_log.h:498
#define APLOGctrace4(c)
Definition http_log.h:260
#define APLOG_MARK
Definition http_log.h:283
#define APLOG_WARNING
Definition http_log.h:68
#define APLOG_EMERG
Definition http_log.h:64
#define APLOG_TRACE1
Definition http_log.h:72
#define APLOG_DEBUG
Definition http_log.h:71
const unsigned char * buf
Definition util_md5.h:50
apr_status_t ap_global_mutex_create(apr_global_mutex_t **mutex, const char **name, const char *type, const char *instance_id, server_rec *server, apr_pool_t *pool, apr_int32_t options)
Definition util_mutex.c:407
apr_status_t ap_mutex_register(apr_pool_t *pconf, const char *type, const char *default_dir, apr_lockmech_e default_mech, apr_int32_t options)
Definition util_mutex.c:254
apr_array_header_t * ap_list_provider_names(apr_pool_t *pool, const char *provider_group, const char *provider_version)
Definition provider.c:127
void * ap_lookup_provider(const char *provider_group, const char *provider_name, const char *provider_version)
Definition provider.c:99
#define APR_ENOENT
Definition apr_errno.h:662
unsigned int count
Definition apr_md5.h:152
#define APR_STATUS_IS_NOTFOUND(s)
Definition apr_errno.h:574
#define AP_SOCACHE_PROVIDER_GROUP
Definition ap_socache.h:218
#define AP_SOCACHE_FLAG_NOTMPSAFE
Definition ap_socache.h:46
#define AP_SOCACHE_PROVIDER_VERSION
Definition ap_socache.h:220
#define ap_strchr(s, c)
Definition httpd.h:2351
apr_size_t size
apr_uint32_t val
Definition apr_atomic.h:66
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const char * key
void const char apr_status_t(* cleanup)(void *))
apr_ssize_t * klen
Definition apr_hash.h:71
apr_vformatter_buff_t * c
Definition apr_lib.h:175
@ APR_LOCK_DEFAULT
const char * s
Definition apr_strings.h:95
apr_int32_t apr_int32_t apr_int32_t err
const char const char *const * args
apr_int64_t apr_time_t
Definition apr_time.h:45
#define apr_time_from_sec(sec)
Definition apr_time.h:78
Apache connection library.
Apache Logging library.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
char * name
apr_status_t(* store)(ap_socache_instance_t *instance, server_rec *s, const unsigned char *id, unsigned int idlen, apr_time_t expiry, unsigned char *data, unsigned int datalen, apr_pool_t *pool)
Definition ap_socache.h:151
apr_status_t(* remove)(ap_socache_instance_t *instance, server_rec *s, const unsigned char *id, unsigned int idlen, apr_pool_t *pool)
Definition ap_socache.h:185
void(* destroy)(ap_socache_instance_t *instance, server_rec *s)
Definition ap_socache.h:137
apr_status_t(* init)(ap_socache_instance_t *instance, const char *cname, const struct ap_socache_hints *hints, server_rec *s, apr_pool_t *pool)
Definition ap_socache.h:128
apr_status_t(* retrieve)(ap_socache_instance_t *instance, server_rec *s, const unsigned char *id, unsigned int idlen, unsigned char *data, unsigned int *datalen, apr_pool_t *pool)
Definition ap_socache.h:171
Structure to store things which are per connection.
Definition httpd.h:1152
A structure to store information for each virtual server.
Definition httpd.h:1322
int session_id_cache_hit
Definition tls_core.h:62
server_rec * server
Definition tls_core.h:40
const struct ap_socache_provider_t * session_cache_provider
Definition tls_conf.h:84
const char * session_cache_spec
Definition tls_conf.h:83
struct apr_global_mutex_t * session_cache_mutex
Definition tls_conf.h:86
struct ap_socache_instance_t * session_cache
Definition tls_conf.h:85
tls_conf_global_t * global
Definition tls_conf.h:97
static rustls_result tls_cache_get(void *userdata, const rustls_slice_bytes *key, int remove_after, unsigned char *buf, size_t count, size_t *out_n)
Definition tls_cache.c:219
static rustls_result tls_cache_put(void *userdata, const rustls_slice_bytes *key, const rustls_slice_bytes *val)
Definition tls_cache.c:264
#define TLS_CACHE_DEF_SIZE
Definition tls_cache.c:39
void tls_cache_free(server_rec *s)
Definition tls_cache.c:189
static void tls_cache_lock(tls_conf_global_t *gconf)
Definition tls_cache.c:197
void tls_cache_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
Definition tls_cache.c:55
const char * tls_cache_set_specification(const char *spec, tls_conf_global_t *gconf, apr_pool_t *p, apr_pool_t *ptemp)
Definition tls_cache.c:130
static void tls_cache_unlock(tls_conf_global_t *gconf)
Definition tls_cache.c:208
apr_status_t tls_cache_init_server(rustls_server_config_builder *builder, server_rec *s)
Definition tls_cache.c:299
apr_status_t tls_cache_post_config(apr_pool_t *p, apr_pool_t *ptemp, server_rec *s)
Definition tls_cache.c:137
void tls_cache_init_child(apr_pool_t *p, server_rec *s)
Definition tls_cache.c:172
static const char * cache_provider_unknown(const char *name, apr_pool_t *p)
Definition tls_cache.c:41
#define TLS_CACHE_DEF_PROVIDER
Definition tls_cache.c:36
static const char * cache_init(tls_conf_global_t *gconf, apr_pool_t *p, apr_pool_t *ptemp)
Definition tls_cache.c:65
#define TLS_CACHE_DEF_DIR
Definition tls_cache.c:37
#define TLS_CACHE_DEF_FILE
Definition tls_cache.c:38
#define TLS_SESSION_CACHE_MUTEX_TYPE
Definition tls_cache.h:20
tls_conf_server_t * tls_conf_server_get(server_rec *s)
Definition tls_conf.c:68
tls_conf_conn_t * tls_conf_conn_get(conn_rec *c)
Definition tls_core.c:45
Apache Mutex support library.
typedef int(WSAAPI *apr_winapi_fpt_WSAPoll)(IN OUT LPWSAPOLLFD fdArray