563f4bdd56259afc491f25b0d6b04e6d3694352e
[strongswan.git] / src / libstrongswan / credentials / sets / cert_cache.c
1 /*
2 * Copyright (C) 2008 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 "cert_cache.h"
17
18 #include <time.h>
19
20 #include <library.h>
21 #include <threading/rwlock.h>
22 #include <collections/linked_list.h>
23
24 /** cache size, a power of 2 for fast modulo */
25 #define CACHE_SIZE 32
26
27 /** attempts to acquire a cache lock */
28 #define REPLACE_TRIES 5
29
30 typedef struct private_cert_cache_t private_cert_cache_t;
31 typedef struct relation_t relation_t;
32
33 /**
34 * A trusted relation between subject and issuer
35 */
36 struct relation_t {
37
38 /**
39 * subject of this relation
40 */
41 certificate_t *subject;
42
43 /**
44 * issuer of this relation
45 */
46 certificate_t *issuer;
47
48 /**
49 * Signature scheme used to sign this relation
50 */
51 signature_scheme_t scheme;
52
53 /**
54 * Cache hits
55 */
56 u_int hits;
57
58 /**
59 * Lock for this relation
60 */
61 rwlock_t *lock;
62 };
63
64 /**
65 * private data of cert_cache
66 */
67 struct private_cert_cache_t {
68
69 /**
70 * public functions
71 */
72 cert_cache_t public;
73
74 /**
75 * array of trusted subject-issuer relations
76 */
77 relation_t relations[CACHE_SIZE];
78 };
79
80 /**
81 * Cache relation in a free slot/replace an other
82 */
83 static void cache(private_cert_cache_t *this,
84 certificate_t *subject, certificate_t *issuer,
85 signature_scheme_t scheme)
86 {
87 relation_t *rel;
88 int i, offset, try;
89 u_int total_hits = 0;
90
91 /* check for a unused relation slot first */
92 for (i = 0; i < CACHE_SIZE; i++)
93 {
94 rel = &this->relations[i];
95
96 if (!rel->subject && rel->lock->try_write_lock(rel->lock))
97 {
98 /* double-check having lock */
99 if (!rel->subject)
100 {
101 rel->subject = subject->get_ref(subject);
102 rel->issuer = issuer->get_ref(issuer);
103 rel->scheme = scheme;
104 return rel->lock->unlock(rel->lock);
105 }
106 rel->lock->unlock(rel->lock);
107 }
108 total_hits += rel->hits;
109 }
110 /* run several attempts to replace a random slot, never block. */
111 for (try = 0; try < REPLACE_TRIES; try++)
112 {
113 /* replace a random relation */
114 offset = random();
115 for (i = 0; i < CACHE_SIZE; i++)
116 {
117 rel = &this->relations[(i + offset) % CACHE_SIZE];
118
119 if (rel->hits > total_hits / CACHE_SIZE)
120 { /* skip often used slots */
121 continue;
122 }
123 if (rel->lock->try_write_lock(rel->lock))
124 {
125 if (rel->subject)
126 {
127 rel->subject->destroy(rel->subject);
128 rel->issuer->destroy(rel->issuer);
129 }
130 rel->subject = subject->get_ref(subject);
131 rel->issuer = issuer->get_ref(issuer);
132 rel->scheme = scheme;
133 rel->hits = 0;
134 return rel->lock->unlock(rel->lock);
135 }
136 }
137 /* give other threads a chance to release locks */
138 sched_yield();
139 }
140 }
141
142 METHOD(cert_cache_t, issued_by, bool,
143 private_cert_cache_t *this, certificate_t *subject, certificate_t *issuer,
144 signature_scheme_t *schemep)
145 {
146 relation_t *found = NULL, *current;
147 signature_scheme_t scheme;
148 int i;
149
150 for (i = 0; i < CACHE_SIZE; i++)
151 {
152 current = &this->relations[i];
153
154 current->lock->read_lock(current->lock);
155 if (current->subject)
156 {
157 /* check for equal issuer */
158 if (issuer->equals(issuer, current->issuer))
159 {
160 /* reuse issuer instance in cache() */
161 issuer = current->issuer;
162 if (subject->equals(subject, current->subject))
163 {
164 /* write hit counter is not locked, but not critical */
165 current->hits++;
166 found = current;;
167 if (schemep)
168 {
169 *schemep = current->scheme;
170 }
171 }
172 }
173 }
174 current->lock->unlock(current->lock);
175 if (found)
176 {
177 return TRUE;
178 }
179 }
180 /* no cache hit, check and cache signature */
181 if (subject->issued_by(subject, issuer, &scheme))
182 {
183 cache(this, subject, issuer, scheme);
184 if (schemep)
185 {
186 *schemep = scheme;
187 }
188 return TRUE;
189 }
190 return FALSE;
191 }
192
193 /**
194 * certificate enumerator implemenation
195 */
196 typedef struct {
197 /** implements enumerator_t interface */
198 enumerator_t public;
199 /** type of requested certificate */
200 certificate_type_t cert;
201 /** type of requested key */
202 key_type_t key;
203 /** ID to get a cert for */
204 identification_t *id;
205 /** cache */
206 relation_t *relations;
207 /** current position in array cache */
208 int index;
209 /** currently locked relation */
210 int locked;
211 } cert_enumerator_t;
212
213 /**
214 * filter function for certs enumerator
215 */
216 static bool cert_enumerate(cert_enumerator_t *this, certificate_t **out)
217 {
218 public_key_t *public;
219 relation_t *rel;
220
221 if (this->locked >= 0)
222 {
223 rel = &this->relations[this->locked];
224 rel->lock->unlock(rel->lock);
225 this->locked = -1;
226 }
227
228 while (++this->index < CACHE_SIZE)
229 {
230 rel = &this->relations[this->index];
231 rel->lock->read_lock(rel->lock);
232 this->locked = this->index;
233 if (rel->subject)
234 {
235 /* CRL lookup is done using issuer/authkeyidentifier */
236 if (this->key == KEY_ANY && this->id &&
237 (this->cert == CERT_ANY || this->cert == CERT_X509_CRL) &&
238 rel->subject->get_type(rel->subject) == CERT_X509_CRL &&
239 rel->subject->has_issuer(rel->subject, this->id))
240 {
241 *out = rel->subject;
242 return TRUE;
243 }
244 if ((this->cert == CERT_ANY ||
245 rel->subject->get_type(rel->subject) == this->cert) &&
246 (!this->id || rel->subject->has_subject(rel->subject, this->id)))
247 {
248 if (this->key == KEY_ANY)
249 {
250 *out = rel->subject;
251 return TRUE;
252 }
253 public = rel->subject->get_public_key(rel->subject);
254 if (public)
255 {
256 if (public->get_type(public) == this->key)
257 {
258 public->destroy(public);
259 *out = rel->subject;
260 return TRUE;
261 }
262 public->destroy(public);
263 }
264 }
265 }
266 this->locked = -1;
267 rel->lock->unlock(rel->lock);
268 }
269 return FALSE;
270 }
271
272 /**
273 * clean up enumeration data
274 */
275 static void cert_enumerator_destroy(cert_enumerator_t *this)
276 {
277 relation_t *rel;
278
279 if (this->locked >= 0)
280 {
281 rel = &this->relations[this->locked];
282 rel->lock->unlock(rel->lock);
283 }
284 free(this);
285 }
286
287 METHOD(credential_set_t, create_enumerator, enumerator_t*,
288 private_cert_cache_t *this, certificate_type_t cert, key_type_t key,
289 identification_t *id, bool trusted)
290 {
291 cert_enumerator_t *enumerator;
292
293 if (trusted)
294 {
295 return NULL;
296 }
297 enumerator = malloc_thing(cert_enumerator_t);
298 enumerator->public.enumerate = (void*)cert_enumerate;
299 enumerator->public.destroy = (void*)cert_enumerator_destroy;
300 enumerator->cert = cert;
301 enumerator->key = key;
302 enumerator->id = id;
303 enumerator->relations = this->relations;
304 enumerator->index = -1;
305 enumerator->locked = -1;
306
307 return &enumerator->public;
308 }
309
310 METHOD(cert_cache_t, flush, void,
311 private_cert_cache_t *this, certificate_type_t type)
312 {
313 relation_t *rel;
314 int i;
315
316 for (i = 0; i < CACHE_SIZE; i++)
317 {
318 rel = &this->relations[i];
319 if (!rel->subject)
320 {
321 continue;
322 }
323 /* check with cheap read lock first */
324 if (type != CERT_ANY)
325 {
326 rel->lock->read_lock(rel->lock);
327 if (!rel->subject || type != rel->subject->get_type(rel->subject))
328 {
329 rel->lock->unlock(rel->lock);
330 continue;
331 }
332 rel->lock->unlock(rel->lock);
333 }
334 /* double check in write lock */
335 rel->lock->write_lock(rel->lock);
336 if (rel->subject)
337 {
338 if (type == CERT_ANY || type == rel->subject->get_type(rel->subject))
339 {
340 rel->subject->destroy(rel->subject);
341 rel->issuer->destroy(rel->issuer);
342 rel->subject = NULL;
343 rel->issuer = NULL;
344 rel->hits = 0;
345 }
346 }
347 rel->lock->unlock(rel->lock);
348 }
349 }
350
351 METHOD(cert_cache_t, destroy, void,
352 private_cert_cache_t *this)
353 {
354 relation_t *rel;
355 int i;
356
357 for (i = 0; i < CACHE_SIZE; i++)
358 {
359 rel = &this->relations[i];
360 if (rel->subject)
361 {
362 rel->subject->destroy(rel->subject);
363 rel->issuer->destroy(rel->issuer);
364 }
365 rel->lock->destroy(rel->lock);
366 }
367 free(this);
368 }
369
370 /*
371 * see header file
372 */
373 cert_cache_t *cert_cache_create()
374 {
375 private_cert_cache_t *this;
376 int i;
377
378 INIT(this,
379 .public = {
380 .set = {
381 .create_cert_enumerator = _create_enumerator,
382 .create_private_enumerator = (void*)return_null,
383 .create_shared_enumerator = (void*)return_null,
384 .create_cdp_enumerator = (void*)return_null,
385 .cache_cert = (void*)nop,
386 },
387 .issued_by = _issued_by,
388 .flush = _flush,
389 .destroy = _destroy,
390 },
391 );
392
393 for (i = 0; i < CACHE_SIZE; i++)
394 {
395 this->relations[i].subject = NULL;
396 this->relations[i].issuer = NULL;
397 this->relations[i].hits = 0;
398 this->relations[i].lock = rwlock_create(RWLOCK_TYPE_DEFAULT);
399 }
400
401 return &this->public;
402 }