1 | /* |
---|
2 | * Copyright 2004-2005 Paul Querna |
---|
3 | * Copyright 2014 Nikos Mavrogiannopoulos |
---|
4 | * Copyright 2015-2018 Fiona Klute |
---|
5 | * |
---|
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
7 | * you may not use this file except in compliance with the License. |
---|
8 | * You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | /** |
---|
20 | * @file |
---|
21 | * |
---|
22 | * Generic object cache for mod_gnutls. |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef __MOD_GNUTLS_CACHE_H__ |
---|
26 | #define __MOD_GNUTLS_CACHE_H__ |
---|
27 | |
---|
28 | #include "mod_gnutls.h" |
---|
29 | #include <httpd.h> |
---|
30 | #include <ap_socache.h> |
---|
31 | |
---|
32 | /** Name of the mod_gnutls cache access mutex, for use with Apache's |
---|
33 | * `Mutex` directive */ |
---|
34 | #define MGS_CACHE_MUTEX_NAME "gnutls-cache" |
---|
35 | |
---|
36 | /** |
---|
37 | * Configure a cache instance |
---|
38 | * |
---|
39 | * This function is supposed to be called during config and |
---|
40 | * initializes an mgs_cache_t by finding the named socache provider |
---|
41 | * and creating a cache instance with the given configuration. Note |
---|
42 | * that the socache instance is only created, not initialized, which |
---|
43 | * is supposed to happen during post_config. |
---|
44 | * |
---|
45 | * @param cache pointer to the mgs_cache_t, memory will be allocated |
---|
46 | * if currently NULL. |
---|
47 | * |
---|
48 | * @param server associated server for logging purposes |
---|
49 | * |
---|
50 | * @param type socache provider type |
---|
51 | * |
---|
52 | * @param config configuration string for the socache provider, may be |
---|
53 | * `NULL` if the provider accepts an empty configuration |
---|
54 | * |
---|
55 | * @param pconf configuration memory pool |
---|
56 | * |
---|
57 | * @param ptemp temporary memory pool |
---|
58 | */ |
---|
59 | const char *mgs_cache_inst_config(mgs_cache_t *cache, server_rec *server, |
---|
60 | const char* type, const char* config, |
---|
61 | apr_pool_t *pconf, apr_pool_t *ptemp); |
---|
62 | |
---|
63 | /** |
---|
64 | * Initialize the internal cache configuration structure. This |
---|
65 | * function is called after the configuration file(s) have been |
---|
66 | * parsed. |
---|
67 | * |
---|
68 | * @param pconf configuration memory pool |
---|
69 | * @param ptemp temporary memory pool |
---|
70 | * @param s default server of the Apache configuration, head of the |
---|
71 | * server list |
---|
72 | * @param sc mod_gnutls data associated with `s` |
---|
73 | */ |
---|
74 | int mgs_cache_post_config(apr_pool_t *pconf, apr_pool_t *ptemp, |
---|
75 | server_rec *s, mgs_srvconf_rec *sc); |
---|
76 | |
---|
77 | /** |
---|
78 | * (Re-)Initialize the cache in a child process after forking. |
---|
79 | * |
---|
80 | * @param p child memory pool provided by Apache |
---|
81 | * @param s default server of the Apache configuration, head of the |
---|
82 | * server list |
---|
83 | * @param cache the cache to reinit |
---|
84 | * @param mutex_name name of the mutex associated with the cache for |
---|
85 | * logging purposes |
---|
86 | */ |
---|
87 | int mgs_cache_child_init(apr_pool_t *p, server_rec *server, |
---|
88 | mgs_cache_t cache, const char *mutex_name); |
---|
89 | |
---|
90 | /** |
---|
91 | * Set up caching for the given TLS session. |
---|
92 | * |
---|
93 | * @param ctxt mod_gnutls session context |
---|
94 | * |
---|
95 | * @return 0 |
---|
96 | */ |
---|
97 | int mgs_cache_session_init(mgs_handle_t *ctxt); |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | /** |
---|
102 | * Convert a `time_t` into a null terminated string in a format |
---|
103 | * compatible with OpenSSL's `ASN1_TIME_print()`. |
---|
104 | * |
---|
105 | * @param t time_t time |
---|
106 | * @param str Location to store the time string |
---|
107 | * @param strsize The maximum length that can be stored in `str` |
---|
108 | * |
---|
109 | * @return `str` |
---|
110 | */ |
---|
111 | char *mgs_time2sz(time_t t, char *str, int strsize); |
---|
112 | |
---|
113 | /** |
---|
114 | * Store function for the mod_gnutls object caches. |
---|
115 | * |
---|
116 | * @param cache the cache to store the entry in |
---|
117 | * @param s server associated with the cache entry |
---|
118 | * @param key key for the cache entry |
---|
119 | * @param data data to be cached |
---|
120 | * @param expiry expiration time |
---|
121 | * |
---|
122 | * @return `-1` on error, `0` on success |
---|
123 | */ |
---|
124 | int mgs_cache_store(mgs_cache_t cache, server_rec *server, gnutls_datum_t key, |
---|
125 | gnutls_datum_t data, apr_time_t expiry); |
---|
126 | |
---|
127 | /** |
---|
128 | * Fetch function for the mod_gnutls object caches. |
---|
129 | * |
---|
130 | * *Warning*: The `data` element of the returned `gnutls_datum_t` is |
---|
131 | * allocated using `gnutls_malloc()` for compatibility with the GnuTLS |
---|
132 | * session caching API, and must be released using `gnutls_free()`. |
---|
133 | * |
---|
134 | * @param cache the cache to fetch from |
---|
135 | * |
---|
136 | * @param server server context for the request |
---|
137 | * |
---|
138 | * @param key key for the cache entry to be fetched |
---|
139 | * |
---|
140 | * @param pool pool to allocate the response and other temporary |
---|
141 | * memory from |
---|
142 | * |
---|
143 | * @return the requested cache entry, or `{NULL, 0}` |
---|
144 | */ |
---|
145 | gnutls_datum_t mgs_cache_fetch(mgs_cache_t cache, server_rec *server, |
---|
146 | gnutls_datum_t key, apr_pool_t *pool); |
---|
147 | |
---|
148 | /** |
---|
149 | * Internal cache configuration structure |
---|
150 | */ |
---|
151 | struct mgs_cache { |
---|
152 | /** Socache provider to use for this cache */ |
---|
153 | const ap_socache_provider_t *prov; |
---|
154 | /** The actual socache instance */ |
---|
155 | ap_socache_instance_t *socache; |
---|
156 | /** Cache configuration string (as passed to the socache create |
---|
157 | * function, for logging) */ |
---|
158 | const char *config; |
---|
159 | /** Mutex for cache access (used only if the cache type is not |
---|
160 | * thread-safe) */ |
---|
161 | apr_global_mutex_t *mutex; |
---|
162 | }; |
---|
163 | |
---|
164 | #endif /** __MOD_GNUTLS_CACHE_H__ */ |
---|