Avoid relocking while enumerator is alive
[strongswan.git] / src / libstrongswan / credentials / keys / key_encoding.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "key_encoding.h"
17
18 #include <stdint.h>
19
20 #include <utils/linked_list.h>
21 #include <utils/hashtable.h>
22 #include <threading/rwlock.h>
23
24 typedef struct private_key_encoding_t private_key_encoding_t;
25
26 /**
27 * Private data of an key_encoding_t object.
28 */
29 struct private_key_encoding_t {
30
31 /**
32 * Public key_encoding_t interface.
33 */
34 key_encoding_t public;
35
36 /**
37 * cached encodings, a table for each encoding_type_t, containing chunk_t*
38 */
39 hashtable_t *cache[KEY_ENCODING_MAX];
40
41 /**
42 * Registered encoding fuctions, key_encoder_t
43 */
44 linked_list_t *encoders;
45
46 /**
47 * lock to access cache/encoders
48 */
49 rwlock_t *lock;
50 };
51
52 /**
53 * See header.
54 */
55 bool key_encoding_args(va_list args, ...)
56 {
57 va_list parts, copy;
58 bool failed = FALSE;
59
60 va_start(parts, args);
61
62 while (!failed)
63 {
64 key_encoding_part_t current, target;
65 chunk_t *out, data;
66
67 /* get the part we are looking for */
68 target = va_arg(parts, key_encoding_part_t);
69 if (target == KEY_PART_END)
70 {
71 break;
72 }
73 out = va_arg(parts, chunk_t*);
74
75 va_copy(copy, args);
76 while (!failed)
77 {
78 current = va_arg(copy, key_encoding_part_t);
79 if (current == KEY_PART_END)
80 {
81 failed = TRUE;
82 break;
83 }
84 data = va_arg(copy, chunk_t);
85 if (current == target)
86 {
87 *out = data;
88 break;
89 }
90 }
91 va_end(copy);
92 }
93 va_end(parts);
94 return !failed;
95 }
96
97 /**
98 * hashtable hash() function
99 */
100 static u_int hash(void *key)
101 {
102 return (uintptr_t)key;
103 }
104
105 /**
106 * hashtable equals() function
107 */
108 static bool equals(void *key1, void *key2)
109 {
110 return key1 == key2;
111 }
112
113 /**
114 * Implementation of key_encoding_t.get_cache
115 */
116 static bool get_cache(private_key_encoding_t *this, key_encoding_type_t type,
117 void *cache, chunk_t *encoding)
118 {
119 chunk_t *chunk;
120
121 if (type >= KEY_ENCODING_MAX || type < 0)
122 {
123 return FALSE;
124 }
125 this->lock->read_lock(this->lock);
126 chunk = this->cache[type]->get(this->cache[type], cache);
127 if (chunk)
128 {
129 *encoding = *chunk;
130 }
131 this->lock->unlock(this->lock);
132 return !!chunk;
133 }
134
135 /**
136 * Implementation of key_encoding_t.encode
137 */
138 static bool encode(private_key_encoding_t *this, key_encoding_type_t type,
139 void *cache, chunk_t *encoding, ...)
140 {
141 enumerator_t *enumerator;
142 va_list args, copy;
143 key_encoder_t encode;
144 bool success = FALSE;
145 chunk_t *chunk;
146
147 if (type >= KEY_ENCODING_MAX || type < 0)
148 {
149 return FALSE;
150 }
151 this->lock->read_lock(this->lock);
152 if (cache)
153 {
154 chunk = this->cache[type]->get(this->cache[type], cache);
155 if (chunk)
156 {
157 *encoding = *chunk;
158 this->lock->unlock(this->lock);
159 return TRUE;
160 }
161 }
162 va_start(args, encoding);
163 enumerator = this->encoders->create_enumerator(this->encoders);
164 while (enumerator->enumerate(enumerator, &encode))
165 {
166 va_copy(copy, args);
167 success = encode(type, encoding, copy);
168 va_end(copy);
169 if (success)
170 {
171 break;
172 }
173 }
174 enumerator->destroy(enumerator);
175 this->lock->unlock(this->lock);
176 va_end(args);
177
178 if (success && cache)
179 {
180 chunk = malloc_thing(chunk_t);
181 *chunk = *encoding;
182 this->lock->write_lock(this->lock);
183 this->cache[type]->put(this->cache[type], cache, chunk);
184 this->lock->unlock(this->lock);
185 }
186 return success;
187 }
188
189 /**
190 * Implementation of key_encoding_t.cache
191 */
192 static void cache(private_key_encoding_t *this, key_encoding_type_t type,
193 void *cache, chunk_t encoding)
194 {
195 chunk_t *chunk;
196
197 if (type >= KEY_ENCODING_MAX || type < 0)
198 {
199 return free(encoding.ptr);
200 }
201 chunk = malloc_thing(chunk_t);
202 *chunk = encoding;
203 this->lock->write_lock(this->lock);
204 chunk = this->cache[type]->put(this->cache[type], cache, chunk);
205 this->lock->unlock(this->lock);
206 /* free an encoding already associated to the cache */
207 if (chunk)
208 {
209 free(chunk->ptr);
210 free(chunk);
211 }
212 }
213
214 /**
215 * Implementation of key_encoding_t.clear_cache
216 */
217 static void clear_cache(private_key_encoding_t *this, void *cache)
218 {
219 key_encoding_type_t type;
220 chunk_t *chunk;
221
222 this->lock->write_lock(this->lock);
223 for (type = 0; type < KEY_ENCODING_MAX; type++)
224 {
225 chunk = this->cache[type]->remove(this->cache[type], cache);
226 if (chunk)
227 {
228 chunk_free(chunk);
229 free(chunk);
230 }
231 }
232 this->lock->unlock(this->lock);
233 }
234
235 /**
236 * Implementation of key_encoding_t.add_encoder
237 */
238 static void add_encoder(private_key_encoding_t *this, key_encoder_t encoder)
239 {
240 this->lock->write_lock(this->lock);
241 this->encoders->insert_last(this->encoders, encoder);
242 this->lock->unlock(this->lock);
243 }
244
245 /**
246 * Implementation of key_encoding_t.remove_encoder
247 */
248 static void remove_encoder(private_key_encoding_t *this, key_encoder_t encoder)
249 {
250 this->lock->write_lock(this->lock);
251 this->encoders->remove(this->encoders, encoder, NULL);
252 this->lock->unlock(this->lock);
253 }
254
255 /**
256 * Implementation of key_encoder_t.destroy.
257 */
258 static void destroy(private_key_encoding_t *this)
259 {
260 key_encoding_type_t type;
261
262 for (type = 0; type < KEY_ENCODING_MAX; type++)
263 {
264 /* We explicitly do not free remaining encodings. All keys should
265 * have gone now, and they are responsible for cleaning out their
266 * cache entries. Not flushing here allows the leak detective to
267 * complain if a key did not flush cached encodings. */
268 this->cache[type]->destroy(this->cache[type]);
269 }
270 this->encoders->destroy(this->encoders);
271 this->lock->destroy(this->lock);
272 free(this);
273 }
274
275 /**
276 * See header
277 */
278 key_encoding_t *key_encoding_create()
279 {
280 private_key_encoding_t *this = malloc_thing(private_key_encoding_t);
281 key_encoding_type_t type;
282
283 this->public.encode = (bool(*)(key_encoding_t*, key_encoding_type_t type, void *cache, chunk_t *encoding, ...))encode;
284 this->public.get_cache = (bool(*)(key_encoding_t*, key_encoding_type_t type, void *cache, chunk_t *encoding))get_cache;
285 this->public.cache = (void(*)(key_encoding_t*, key_encoding_type_t type, void *cache, chunk_t encoding))cache;
286 this->public.clear_cache = (void(*)(key_encoding_t*, void *cache))clear_cache;
287 this->public.add_encoder = (void(*)(key_encoding_t*, key_encoder_t encoder))add_encoder;
288 this->public.remove_encoder = (void(*)(key_encoding_t*, key_encoder_t encoder))remove_encoder;
289 this->public.destroy = (void(*)(key_encoding_t*))destroy;
290
291 for (type = 0; type < KEY_ENCODING_MAX; type++)
292 {
293 this->cache[type] = hashtable_create(hash, equals, 8);
294 }
295 this->encoders = linked_list_create();
296 this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);
297
298 return &this->public;
299 }
300