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