Add an option to disable libstrongswan certificate caching
[strongswan.git] / src / libstrongswan / credentials / credential_manager.c
1 /*
2 * Copyright (C) 2007 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 "credential_manager.h"
17
18 #include <library.h>
19 #include <debug.h>
20 #include <threading/thread_value.h>
21 #include <threading/mutex.h>
22 #include <threading/rwlock.h>
23 #include <utils/linked_list.h>
24 #include <credentials/sets/cert_cache.h>
25 #include <credentials/sets/auth_cfg_wrapper.h>
26 #include <credentials/certificates/x509.h>
27
28 /**
29 * Maximum length of a certificate trust chain
30 */
31 #define MAX_TRUST_PATH_LEN 7
32
33 typedef struct private_credential_manager_t private_credential_manager_t;
34
35 /**
36 * private data of credential_manager
37 */
38 struct private_credential_manager_t {
39
40 /**
41 * public functions
42 */
43 credential_manager_t public;
44
45 /**
46 * list of credential sets
47 */
48 linked_list_t *sets;
49
50 /**
51 * thread local set of credentials, linked_list_t with credential_set_t's
52 */
53 thread_value_t *local_sets;
54
55 /**
56 * Exclusive local sets, linked_list_t with credential_set_t
57 */
58 thread_value_t *exclusive_local_sets;
59
60 /**
61 * trust relationship and certificate cache
62 */
63 cert_cache_t *cache;
64
65 /**
66 * certificates queued for persistent caching
67 */
68 linked_list_t *cache_queue;
69
70 /**
71 * list of certificate validators, cert_validator_t
72 */
73 linked_list_t *validators;
74
75 /**
76 * read-write lock to sets list
77 */
78 rwlock_t *lock;
79
80 /**
81 * mutex for cache queue
82 */
83 mutex_t *queue_mutex;
84 };
85
86 /** data to pass to create_private_enumerator */
87 typedef struct {
88 private_credential_manager_t *this;
89 key_type_t type;
90 identification_t* keyid;
91 } private_data_t;
92
93 /** data to pass to create_cert_enumerator */
94 typedef struct {
95 private_credential_manager_t *this;
96 certificate_type_t cert;
97 key_type_t key;
98 identification_t *id;
99 bool trusted;
100 } cert_data_t;
101
102 /** data to pass to create_cdp_enumerator */
103 typedef struct {
104 private_credential_manager_t *this;
105 certificate_type_t type;
106 identification_t *id;
107 } cdp_data_t;
108
109 /** data to pass to create_shared_enumerator */
110 typedef struct {
111 private_credential_manager_t *this;
112 shared_key_type_t type;
113 identification_t *me;
114 identification_t *other;
115 } shared_data_t;
116
117 /** enumerator over local and global sets */
118 typedef struct {
119 /** implements enumerator_t */
120 enumerator_t public;
121 /** enumerator over global sets */
122 enumerator_t *global;
123 /** enumerator over local sets */
124 enumerator_t *local;
125 /** enumerator over exclusive local sets */
126 enumerator_t *exclusive;
127 } sets_enumerator_t;
128
129
130 METHOD(enumerator_t, sets_enumerate, bool,
131 sets_enumerator_t *this, credential_set_t **set)
132 {
133 if (this->exclusive)
134 {
135 if (this->exclusive->enumerate(this->exclusive, set))
136 { /* only enumerate last added */
137 this->exclusive->destroy(this->exclusive);
138 this->exclusive = NULL;
139 return TRUE;
140 }
141 }
142 if (this->global)
143 {
144 if (this->global->enumerate(this->global, set))
145 {
146 return TRUE;
147 }
148 /* end of global sets, look for local */
149 this->global->destroy(this->global);
150 this->global = NULL;
151 }
152 if (this->local)
153 {
154 return this->local->enumerate(this->local, set);
155 }
156 return FALSE;
157 }
158
159 METHOD(enumerator_t, sets_destroy, void,
160 sets_enumerator_t *this)
161 {
162 DESTROY_IF(this->global);
163 DESTROY_IF(this->local);
164 DESTROY_IF(this->exclusive);
165 free(this);
166 }
167
168 /**
169 * create an enumerator over both, global and local sets
170 */
171 static enumerator_t *create_sets_enumerator(private_credential_manager_t *this)
172 {
173 sets_enumerator_t *enumerator;
174 linked_list_t *list;
175
176 INIT(enumerator,
177 .public = {
178 .enumerate = (void*)_sets_enumerate,
179 .destroy = _sets_destroy,
180 },
181 );
182
183 list = this->exclusive_local_sets->get(this->exclusive_local_sets);
184 if (list && list->get_count(list))
185 {
186 enumerator->exclusive = list->create_enumerator(list);
187 }
188 else
189 {
190 enumerator->global = this->sets->create_enumerator(this->sets);
191 list = this->local_sets->get(this->local_sets);
192 if (list)
193 {
194 enumerator->local = list->create_enumerator(list);
195 }
196 }
197 return &enumerator->public;
198 }
199
200 /**
201 * cleanup function for cert data
202 */
203 static void destroy_cert_data(cert_data_t *data)
204 {
205 data->this->lock->unlock(data->this->lock);
206 free(data);
207 }
208
209 /**
210 * enumerator constructor for certificates
211 */
212 static enumerator_t *create_cert(credential_set_t *set, cert_data_t *data)
213 {
214 return set->create_cert_enumerator(set, data->cert, data->key,
215 data->id, data->trusted);
216 }
217
218 METHOD(credential_manager_t, create_cert_enumerator, enumerator_t*,
219 private_credential_manager_t *this, certificate_type_t certificate,
220 key_type_t key, identification_t *id, bool trusted)
221 {
222 cert_data_t *data = malloc_thing(cert_data_t);
223 data->this = this;
224 data->cert = certificate;
225 data->key = key;
226 data->id = id;
227 data->trusted = trusted;
228
229 this->lock->read_lock(this->lock);
230 return enumerator_create_nested(create_sets_enumerator(this),
231 (void*)create_cert, data,
232 (void*)destroy_cert_data);
233 }
234
235 METHOD(credential_manager_t, get_cert, certificate_t*,
236 private_credential_manager_t *this, certificate_type_t cert, key_type_t key,
237 identification_t *id, bool trusted)
238 {
239 certificate_t *current, *found = NULL;
240 enumerator_t *enumerator;
241
242 enumerator = create_cert_enumerator(this, cert, key, id, trusted);
243 if (enumerator->enumerate(enumerator, &current))
244 {
245 /* TODO: best match? order by keyid, subject, sualtname */
246 found = current->get_ref(current);
247 }
248 enumerator->destroy(enumerator);
249 return found;
250 }
251
252
253 /**
254 * cleanup function for cdp data
255 */
256 static void destroy_cdp_data(cdp_data_t *data)
257 {
258 data->this->lock->unlock(data->this->lock);
259 free(data);
260 }
261
262 /**
263 * enumerator constructor for CDPs
264 */
265 static enumerator_t *create_cdp(credential_set_t *set, cdp_data_t *data)
266 {
267 return set->create_cdp_enumerator(set, data->type, data->id);
268 }
269
270 METHOD(credential_manager_t, create_cdp_enumerator, enumerator_t*,
271 private_credential_manager_t *this, certificate_type_t type,
272 identification_t *id)
273 {
274 cdp_data_t *data;
275
276 INIT(data,
277 .this = this,
278 .type = type,
279 .id = id,
280 );
281 this->lock->read_lock(this->lock);
282 return enumerator_create_nested(create_sets_enumerator(this),
283 (void*)create_cdp, data,
284 (void*)destroy_cdp_data);
285 }
286
287 /**
288 * cleanup function for private data
289 */
290 static void destroy_private_data(private_data_t *data)
291 {
292 data->this->lock->unlock(data->this->lock);
293 free(data);
294 }
295
296 /**
297 * enumerator constructor for private keys
298 */
299 static enumerator_t *create_private(credential_set_t *set, private_data_t *data)
300 {
301 return set->create_private_enumerator(set, data->type, data->keyid);
302 }
303
304 /**
305 * Create an enumerator over private keys
306 */
307 static enumerator_t *create_private_enumerator(
308 private_credential_manager_t *this, key_type_t key, identification_t *keyid)
309 {
310 private_data_t *data;
311
312 INIT(data,
313 .this = this,
314 .type = key,
315 .keyid = keyid,
316 );
317 this->lock->read_lock(this->lock);
318 return enumerator_create_nested(create_sets_enumerator(this),
319 (void*)create_private, data,
320 (void*)destroy_private_data);
321 }
322
323 /**
324 * Look up a private key by its key identifier
325 */
326 static private_key_t* get_private_by_keyid(private_credential_manager_t *this,
327 key_type_t key, identification_t *keyid)
328 {
329 private_key_t *found = NULL;
330 enumerator_t *enumerator;
331
332 enumerator = create_private_enumerator(this, key, keyid);
333 if (enumerator->enumerate(enumerator, &found))
334 {
335 found->get_ref(found);
336 }
337 enumerator->destroy(enumerator);
338 return found;
339 }
340
341 /**
342 * cleanup function for shared data
343 */
344 static void destroy_shared_data(shared_data_t *data)
345 {
346 data->this->lock->unlock(data->this->lock);
347 free(data);
348 }
349
350 /**
351 * enumerator constructor for shared keys
352 */
353 static enumerator_t *create_shared(credential_set_t *set, shared_data_t *data)
354 {
355 return set->create_shared_enumerator(set, data->type, data->me, data->other);
356 }
357
358 METHOD(credential_manager_t, create_shared_enumerator, enumerator_t*,
359 private_credential_manager_t *this, shared_key_type_t type,
360 identification_t *me, identification_t *other)
361 {
362 shared_data_t *data;
363
364 INIT(data,
365 .this = this,
366 .type = type,
367 .me = me,
368 .other = other,
369 );
370 this->lock->read_lock(this->lock);
371 return enumerator_create_nested(create_sets_enumerator(this),
372 (void*)create_shared, data,
373 (void*)destroy_shared_data);
374 }
375
376 METHOD(credential_manager_t, get_shared, shared_key_t*,
377 private_credential_manager_t *this, shared_key_type_t type,
378 identification_t *me, identification_t *other)
379 {
380 shared_key_t *current, *found = NULL;
381 id_match_t *best_me = ID_MATCH_NONE, *best_other = ID_MATCH_NONE;
382 id_match_t *match_me, *match_other;
383 enumerator_t *enumerator;
384
385 enumerator = create_shared_enumerator(this, type, me, other);
386 while (enumerator->enumerate(enumerator, &current, &match_me, &match_other))
387 {
388 if (match_other > best_other ||
389 (match_other == best_other && match_me > best_me))
390 {
391 DESTROY_IF(found);
392 found = current->get_ref(current);
393 best_me = match_me;
394 best_other = match_other;
395 }
396 }
397 enumerator->destroy(enumerator);
398 return found;
399 }
400
401 METHOD(credential_manager_t, add_local_set, void,
402 private_credential_manager_t *this, credential_set_t *set, bool exclusive)
403 {
404 linked_list_t *sets;
405 thread_value_t *tv;
406
407 if (exclusive)
408 {
409 tv = this->exclusive_local_sets;
410 }
411 else
412 {
413 tv = this->local_sets;
414 }
415 sets = tv->get(tv);
416 if (!sets)
417 {
418 sets = linked_list_create();
419 tv->set(tv, sets);
420 }
421 if (exclusive)
422 {
423 sets->insert_first(sets, set);
424 }
425 else
426 {
427 sets->insert_last(sets, set);
428 }
429 }
430
431 METHOD(credential_manager_t, remove_local_set, void,
432 private_credential_manager_t *this, credential_set_t *set)
433 {
434 linked_list_t *sets;
435 thread_value_t *tv;
436
437 tv = this->local_sets;
438 sets = tv->get(tv);
439 if (sets && sets->remove(sets, set, NULL) && sets->get_count(sets) == 0)
440 {
441 tv->set(tv, NULL);
442 sets->destroy(sets);
443 }
444 tv = this->exclusive_local_sets;
445 sets = tv->get(tv);
446 if (sets && sets->remove(sets, set, NULL) && sets->get_count(sets) == 0)
447 {
448 tv->set(tv, NULL);
449 sets->destroy(sets);
450 }
451 }
452
453 METHOD(credential_manager_t, issued_by, bool,
454 private_credential_manager_t *this, certificate_t *subject,
455 certificate_t *issuer, signature_scheme_t *scheme)
456 {
457 if (this->cache)
458 {
459 return this->cache->issued_by(this->cache, subject, issuer, scheme);
460 }
461 return subject->issued_by(subject, issuer, scheme);
462 }
463
464 METHOD(credential_manager_t, cache_cert, void,
465 private_credential_manager_t *this, certificate_t *cert)
466 {
467 credential_set_t *set;
468 enumerator_t *enumerator;
469
470 if (this->lock->try_write_lock(this->lock))
471 {
472 enumerator = this->sets->create_enumerator(this->sets);
473 while (enumerator->enumerate(enumerator, &set))
474 {
475 set->cache_cert(set, cert);
476 }
477 enumerator->destroy(enumerator);
478 this->lock->unlock(this->lock);
479 }
480 else
481 { /* we can't cache now as other threads are active, queue for later */
482 this->queue_mutex->lock(this->queue_mutex);
483 this->cache_queue->insert_last(this->cache_queue, cert->get_ref(cert));
484 this->queue_mutex->unlock(this->queue_mutex);
485 }
486 }
487
488 /**
489 * Try to cache certificates queued for caching
490 */
491 static void cache_queue(private_credential_manager_t *this)
492 {
493 credential_set_t *set;
494 certificate_t *cert;
495 enumerator_t *enumerator;
496
497 this->queue_mutex->lock(this->queue_mutex);
498 if (this->cache_queue->get_count(this->cache_queue) > 0 &&
499 this->lock->try_write_lock(this->lock))
500 {
501 while (this->cache_queue->remove_last(this->cache_queue,
502 (void**)&cert) == SUCCESS)
503 {
504 enumerator = this->sets->create_enumerator(this->sets);
505 while (enumerator->enumerate(enumerator, &set))
506 {
507 set->cache_cert(set, cert);
508 }
509 enumerator->destroy(enumerator);
510 cert->destroy(cert);
511 }
512 this->lock->unlock(this->lock);
513 }
514 this->queue_mutex->unlock(this->queue_mutex);
515 }
516
517 /**
518 * check a certificate for its lifetime
519 */
520 static bool check_certificate(private_credential_manager_t *this,
521 certificate_t *subject, certificate_t *issuer, bool online,
522 int pathlen, bool trusted, auth_cfg_t *auth)
523 {
524 time_t not_before, not_after;
525 cert_validator_t *validator;
526 enumerator_t *enumerator;
527
528 if (!subject->get_validity(subject, NULL, &not_before, &not_after))
529 {
530 DBG1(DBG_CFG, "subject certificate invalid (valid from %T to %T)",
531 &not_before, FALSE, &not_after, FALSE);
532 return FALSE;
533 }
534 if (!issuer->get_validity(issuer, NULL, &not_before, &not_after))
535 {
536 DBG1(DBG_CFG, "issuer certificate invalid (valid from %T to %T)",
537 &not_before, FALSE, &not_after, FALSE);
538 return FALSE;
539 }
540
541 enumerator = this->validators->create_enumerator(this->validators);
542 while (enumerator->enumerate(enumerator, &validator))
543 {
544 if (!validator->validate(validator, subject, issuer,
545 online, pathlen, trusted, auth))
546 {
547 enumerator->destroy(enumerator);
548 return FALSE;
549 }
550 }
551 enumerator->destroy(enumerator);
552 return TRUE;
553 }
554
555 /**
556 * Get a trusted certificate from a credential set
557 */
558 static certificate_t *get_pretrusted_cert(private_credential_manager_t *this,
559 key_type_t type, identification_t *id)
560 {
561 certificate_t *subject;
562 public_key_t *public;
563
564 subject = get_cert(this, CERT_ANY, type, id, TRUE);
565 if (!subject)
566 {
567 return NULL;
568 }
569 public = subject->get_public_key(subject);
570 if (!public)
571 {
572 subject->destroy(subject);
573 return NULL;
574 }
575 public->destroy(public);
576 return subject;
577 }
578
579 /**
580 * Get the issuing certificate of a subject certificate
581 */
582 static certificate_t *get_issuer_cert(private_credential_manager_t *this,
583 certificate_t *subject, bool trusted,
584 signature_scheme_t *scheme)
585 {
586 enumerator_t *enumerator;
587 certificate_t *issuer = NULL, *candidate;
588
589 enumerator = create_cert_enumerator(this, subject->get_type(subject), KEY_ANY,
590 subject->get_issuer(subject), trusted);
591 while (enumerator->enumerate(enumerator, &candidate))
592 {
593 if (issued_by(this, subject, candidate, scheme))
594 {
595 issuer = candidate->get_ref(candidate);
596 break;
597 }
598 }
599 enumerator->destroy(enumerator);
600 return issuer;
601 }
602
603 /**
604 * Get the strength of certificate, add it to auth
605 */
606 static void get_key_strength(certificate_t *cert, auth_cfg_t *auth)
607 {
608 uintptr_t strength;
609 public_key_t *key;
610 key_type_t type;
611
612 key = cert->get_public_key(cert);
613 if (key)
614 {
615 type = key->get_type(key);
616 strength = key->get_keysize(key);
617 DBG2(DBG_CFG, " certificate \"%Y\" key: %d bit %N",
618 cert->get_subject(cert), strength, key_type_names, type);
619 switch (type)
620 {
621 case KEY_RSA:
622 auth->add(auth, AUTH_RULE_RSA_STRENGTH, strength);
623 break;
624 case KEY_ECDSA:
625 auth->add(auth, AUTH_RULE_ECDSA_STRENGTH, strength);
626 break;
627 default:
628 break;
629 }
630 key->destroy(key);
631 }
632 }
633
634 /**
635 * try to verify the trust chain of subject, return TRUE if trusted
636 */
637 static bool verify_trust_chain(private_credential_manager_t *this,
638 certificate_t *subject, auth_cfg_t *result,
639 bool trusted, bool online)
640 {
641 certificate_t *current, *issuer;
642 auth_cfg_t *auth;
643 signature_scheme_t scheme;
644 int pathlen;
645
646 auth = auth_cfg_create();
647 get_key_strength(subject, auth);
648 current = subject->get_ref(subject);
649 auth->add(auth, AUTH_RULE_SUBJECT_CERT, current->get_ref(current));
650
651 for (pathlen = 0; pathlen <= MAX_TRUST_PATH_LEN; pathlen++)
652 {
653 issuer = get_issuer_cert(this, current, TRUE, &scheme);
654 if (issuer)
655 {
656 /* accept only self-signed CAs as trust anchor */
657 if (issued_by(this, issuer, issuer, NULL))
658 {
659 auth->add(auth, AUTH_RULE_CA_CERT, issuer->get_ref(issuer));
660 DBG1(DBG_CFG, " using trusted ca certificate \"%Y\"",
661 issuer->get_subject(issuer));
662 trusted = TRUE;
663 }
664 else
665 {
666 auth->add(auth, AUTH_RULE_IM_CERT, issuer->get_ref(issuer));
667 DBG1(DBG_CFG, " using trusted intermediate ca certificate "
668 "\"%Y\"", issuer->get_subject(issuer));
669 }
670 auth->add(auth, AUTH_RULE_SIGNATURE_SCHEME, scheme);
671 }
672 else
673 {
674 issuer = get_issuer_cert(this, current, FALSE, &scheme);
675 if (issuer)
676 {
677 if (current->equals(current, issuer))
678 {
679 DBG1(DBG_CFG, " self-signed certificate \"%Y\" is not trusted",
680 current->get_subject(current));
681 issuer->destroy(issuer);
682 break;
683 }
684 auth->add(auth, AUTH_RULE_IM_CERT, issuer->get_ref(issuer));
685 DBG1(DBG_CFG, " using untrusted intermediate certificate "
686 "\"%Y\"", issuer->get_subject(issuer));
687 auth->add(auth, AUTH_RULE_SIGNATURE_SCHEME, scheme);
688 }
689 else
690 {
691 DBG1(DBG_CFG, "no issuer certificate found for \"%Y\"",
692 current->get_subject(current));
693 break;
694 }
695 }
696 if (!check_certificate(this, current, issuer, online,
697 pathlen, trusted, auth))
698 {
699 trusted = FALSE;
700 issuer->destroy(issuer);
701 break;
702 }
703 if (issuer)
704 {
705 get_key_strength(issuer, auth);
706 }
707 current->destroy(current);
708 current = issuer;
709 if (trusted)
710 {
711 DBG1(DBG_CFG, " reached self-signed root ca with a path length of %d",
712 pathlen);
713 break;
714 }
715 }
716 current->destroy(current);
717 if (pathlen > MAX_TRUST_PATH_LEN)
718 {
719 DBG1(DBG_CFG, "maximum path length of %d exceeded", MAX_TRUST_PATH_LEN);
720 }
721 if (trusted)
722 {
723 result->merge(result, auth, FALSE);
724 }
725 auth->destroy(auth);
726 return trusted;
727 }
728
729 /**
730 * List find match function for certificates
731 */
732 static bool cert_equals(certificate_t *a, certificate_t *b)
733 {
734 return a->equals(a, b);
735 }
736
737 /**
738 * enumerator for trusted certificates
739 */
740 typedef struct {
741 /** implements enumerator_t interface */
742 enumerator_t public;
743 /** enumerator over candidate peer certificates */
744 enumerator_t *candidates;
745 /** reference to the credential_manager */
746 private_credential_manager_t *this;
747 /** type of the requested key */
748 key_type_t type;
749 /** identity the requested key belongs to */
750 identification_t *id;
751 /** TRUE to do CRL/OCSP checking */
752 bool online;
753 /** pretrusted certificate we have served at first invocation */
754 certificate_t *pretrusted;
755 /** currently enumerating auth config */
756 auth_cfg_t *auth;
757 /** list of failed candidates */
758 linked_list_t *failed;
759 } trusted_enumerator_t;
760
761 METHOD(enumerator_t, trusted_enumerate, bool,
762 trusted_enumerator_t *this, certificate_t **cert, auth_cfg_t **auth)
763 {
764 certificate_t *current;
765
766 DESTROY_IF(this->auth);
767 this->auth = auth_cfg_create();
768
769 if (!this->candidates)
770 {
771 /* first invocation, build enumerator for next one */
772 this->candidates = create_cert_enumerator(this->this, CERT_ANY,
773 this->type, this->id, FALSE);
774 /* check if we have a trusted certificate for that peer */
775 this->pretrusted = get_pretrusted_cert(this->this, this->type, this->id);
776 if (this->pretrusted)
777 {
778 /* if we find a trusted self signed certificate, we just accept it.
779 * However, in order to fulfill authorization rules, we try to build
780 * the trust chain if it is not self signed */
781 if (issued_by(this->this, this->pretrusted, this->pretrusted, NULL) ||
782 verify_trust_chain(this->this, this->pretrusted, this->auth,
783 TRUE, this->online))
784 {
785 DBG1(DBG_CFG, " using trusted certificate \"%Y\"",
786 this->pretrusted->get_subject(this->pretrusted));
787 *cert = this->pretrusted;
788 if (!this->auth->get(this->auth, AUTH_RULE_SUBJECT_CERT))
789 { /* add cert to auth info, if not returned by trustchain */
790 this->auth->add(this->auth, AUTH_RULE_SUBJECT_CERT,
791 this->pretrusted->get_ref(this->pretrusted));
792 }
793 if (auth)
794 {
795 *auth = this->auth;
796 }
797 return TRUE;
798 }
799 }
800 }
801 /* try to verify the trust chain for each certificate found */
802 while (this->candidates->enumerate(this->candidates, &current))
803 {
804 if (this->pretrusted &&
805 this->pretrusted->equals(this->pretrusted, current))
806 { /* skip pretrusted certificate we already served */
807 continue;
808 }
809
810 if (this->failed->find_first(this->failed, (void*)cert_equals,
811 NULL, current) == SUCCESS)
812 { /* check each candidate only once */
813 continue;
814 }
815
816 DBG1(DBG_CFG, " using certificate \"%Y\"",
817 current->get_subject(current));
818 if (verify_trust_chain(this->this, current, this->auth, FALSE,
819 this->online))
820 {
821 *cert = current;
822 if (auth)
823 {
824 *auth = this->auth;
825 }
826 return TRUE;
827 }
828 this->failed->insert_last(this->failed, current->get_ref(current));
829 }
830 return FALSE;
831 }
832
833 METHOD(enumerator_t, trusted_destroy, void,
834 trusted_enumerator_t *this)
835 {
836 DESTROY_IF(this->pretrusted);
837 DESTROY_IF(this->auth);
838 DESTROY_IF(this->candidates);
839 this->failed->destroy_offset(this->failed, offsetof(certificate_t, destroy));
840 free(this);
841 }
842
843 METHOD(credential_manager_t, create_trusted_enumerator, enumerator_t*,
844 private_credential_manager_t *this, key_type_t type,
845 identification_t *id, bool online)
846 {
847 trusted_enumerator_t *enumerator;
848
849 INIT(enumerator,
850 .public = {
851 .enumerate = (void*)_trusted_enumerate,
852 .destroy = _trusted_destroy,
853 },
854 .this = this,
855 .type = type,
856 .id = id,
857 .online = online,
858 .failed = linked_list_create(),
859 );
860 return &enumerator->public;
861 }
862
863 /**
864 * enumerator for public keys
865 */
866 typedef struct {
867 /** implements enumerator_t interface */
868 enumerator_t public;
869 /** enumerator over candidate peer certificates */
870 enumerator_t *inner;
871 /** reference to the credential_manager */
872 private_credential_manager_t *this;
873 /** currently enumerating key */
874 public_key_t *current;
875 /** credset wrapper around auth config */
876 auth_cfg_wrapper_t *wrapper;
877 } public_enumerator_t;
878
879 METHOD(enumerator_t, public_enumerate, bool,
880 public_enumerator_t *this, public_key_t **key, auth_cfg_t **auth)
881 {
882 certificate_t *cert;
883
884 while (this->inner->enumerate(this->inner, &cert, auth))
885 {
886 DESTROY_IF(this->current);
887 this->current = cert->get_public_key(cert);
888 if (this->current)
889 {
890 *key = this->current;
891 return TRUE;
892 }
893 }
894 return FALSE;
895 }
896
897 METHOD(enumerator_t, public_destroy, void,
898 public_enumerator_t *this)
899 {
900 DESTROY_IF(this->current);
901 this->inner->destroy(this->inner);
902 if (this->wrapper)
903 {
904 remove_local_set(this->this, &this->wrapper->set);
905 this->wrapper->destroy(this->wrapper);
906 }
907 this->this->lock->unlock(this->this->lock);
908
909 /* check for delayed certificate cache queue */
910 cache_queue(this->this);
911 free(this);
912 }
913
914 METHOD(credential_manager_t, create_public_enumerator, enumerator_t*,
915 private_credential_manager_t *this, key_type_t type, identification_t *id,
916 auth_cfg_t *auth)
917 {
918 public_enumerator_t *enumerator;
919
920 INIT(enumerator,
921 .public = {
922 .enumerate = (void*)_public_enumerate,
923 .destroy = _public_destroy,
924 },
925 .inner = create_trusted_enumerator(this, type, id, TRUE),
926 .this = this,
927 );
928 if (auth)
929 {
930 enumerator->wrapper = auth_cfg_wrapper_create(auth);
931 add_local_set(this, &enumerator->wrapper->set, FALSE);
932 }
933 this->lock->read_lock(this->lock);
934 return &enumerator->public;
935 }
936
937 /**
938 * Check if a helper contains a certificate as trust anchor
939 */
940 static bool auth_contains_cacert(auth_cfg_t *auth, certificate_t *cert)
941 {
942 enumerator_t *enumerator;
943 identification_t *value;
944 auth_rule_t type;
945 bool found = FALSE;
946
947 enumerator = auth->create_enumerator(auth);
948 while (enumerator->enumerate(enumerator, &type, &value))
949 {
950 if (type == AUTH_RULE_CA_CERT &&
951 cert->equals(cert, (certificate_t*)value))
952 {
953 found = TRUE;
954 break;
955 }
956 }
957 enumerator->destroy(enumerator);
958 return found;
959 }
960
961 /**
962 * build a trustchain from subject up to a trust anchor in trusted
963 */
964 static auth_cfg_t *build_trustchain(private_credential_manager_t *this,
965 certificate_t *subject, auth_cfg_t *auth)
966 {
967 certificate_t *issuer, *current;
968 auth_cfg_t *trustchain;
969 int pathlen = 0;
970 bool has_anchor;
971
972 trustchain = auth_cfg_create();
973 has_anchor = auth->get(auth, AUTH_RULE_CA_CERT) != NULL;
974 current = subject->get_ref(subject);
975 while (TRUE)
976 {
977 if (auth_contains_cacert(auth, current))
978 {
979 trustchain->add(trustchain, AUTH_RULE_CA_CERT, current);
980 return trustchain;
981 }
982 if (subject == current)
983 {
984 trustchain->add(trustchain, AUTH_RULE_SUBJECT_CERT, current);
985 }
986 else
987 {
988 if (!has_anchor && issued_by(this, current, current, NULL))
989 { /* If no trust anchor specified, accept any CA */
990 trustchain->add(trustchain, AUTH_RULE_CA_CERT, current);
991 return trustchain;
992 }
993 trustchain->add(trustchain, AUTH_RULE_IM_CERT, current);
994 }
995 if (pathlen++ > MAX_TRUST_PATH_LEN)
996 {
997 break;
998 }
999 issuer = get_issuer_cert(this, current, FALSE, NULL);
1000 if (!issuer)
1001 {
1002 if (!has_anchor)
1003 { /* If no trust anchor specified, accept incomplete chains */
1004 return trustchain;
1005 }
1006 break;
1007 }
1008 if (has_anchor && issuer->equals(issuer, current))
1009 {
1010 issuer->destroy(issuer);
1011 break;
1012 }
1013 current = issuer;
1014 }
1015 trustchain->destroy(trustchain);
1016 return NULL;
1017 }
1018
1019 /**
1020 * find a private key of a given certificate
1021 */
1022 static private_key_t *get_private_by_cert(private_credential_manager_t *this,
1023 certificate_t *cert, key_type_t type)
1024 {
1025 private_key_t *private = NULL;
1026 identification_t *keyid;
1027 chunk_t chunk;
1028 public_key_t *public;
1029
1030 public = cert->get_public_key(cert);
1031 if (public)
1032 {
1033 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
1034 {
1035 keyid = identification_create_from_encoding(ID_KEY_ID, chunk);
1036 private = get_private_by_keyid(this, type, keyid);
1037 keyid->destroy(keyid);
1038 }
1039 public->destroy(public);
1040 }
1041 return private;
1042 }
1043
1044 METHOD(credential_manager_t, get_private, private_key_t*,
1045 private_credential_manager_t *this, key_type_t type, identification_t *id,
1046 auth_cfg_t *auth)
1047 {
1048 enumerator_t *enumerator;
1049 certificate_t *cert;
1050 private_key_t *private = NULL;
1051 auth_cfg_t *trustchain;
1052
1053 /* check if this is a lookup by key ID, and do it if so */
1054 if (id && id->get_type(id) == ID_KEY_ID)
1055 {
1056 private = get_private_by_keyid(this, type, id);
1057 if (private)
1058 {
1059 return private;
1060 }
1061 }
1062
1063 if (auth)
1064 {
1065 /* if a specific certificate is preferred, check for a matching key */
1066 cert = auth->get(auth, AUTH_RULE_SUBJECT_CERT);
1067 if (cert)
1068 {
1069 private = get_private_by_cert(this, cert, type);
1070 if (private)
1071 {
1072 trustchain = build_trustchain(this, cert, auth);
1073 if (trustchain)
1074 {
1075 auth->merge(auth, trustchain, FALSE);
1076 trustchain->destroy(trustchain);
1077 }
1078 return private;
1079 }
1080 }
1081
1082 /* try to build a trust chain for each certificate found */
1083 enumerator = create_cert_enumerator(this, CERT_ANY, type, id, FALSE);
1084 while (enumerator->enumerate(enumerator, &cert))
1085 {
1086 private = get_private_by_cert(this, cert, type);
1087 if (private)
1088 {
1089 trustchain = build_trustchain(this, cert, auth);
1090 if (trustchain)
1091 {
1092 auth->merge(auth, trustchain, FALSE);
1093 trustchain->destroy(trustchain);
1094 break;
1095 }
1096 private->destroy(private);
1097 private = NULL;
1098 }
1099 }
1100 enumerator->destroy(enumerator);
1101 }
1102
1103 /* if no valid trustchain was found, fall back to the first usable cert */
1104 if (!private)
1105 {
1106 enumerator = create_cert_enumerator(this, CERT_ANY, type, id, FALSE);
1107 while (enumerator->enumerate(enumerator, &cert))
1108 {
1109 private = get_private_by_cert(this, cert, type);
1110 if (private)
1111 {
1112 if (auth)
1113 {
1114 auth->add(auth, AUTH_RULE_SUBJECT_CERT, cert->get_ref(cert));
1115 }
1116 break;
1117 }
1118 }
1119 enumerator->destroy(enumerator);
1120 }
1121 return private;
1122 }
1123
1124 METHOD(credential_manager_t, flush_cache, void,
1125 private_credential_manager_t *this, certificate_type_t type)
1126 {
1127 if (this->cache)
1128 {
1129 this->cache->flush(this->cache, type);
1130 }
1131 }
1132
1133 METHOD(credential_manager_t, add_set, void,
1134 private_credential_manager_t *this, credential_set_t *set)
1135 {
1136 this->lock->write_lock(this->lock);
1137 this->sets->insert_last(this->sets, set);
1138 this->lock->unlock(this->lock);
1139 }
1140
1141 METHOD(credential_manager_t, remove_set, void,
1142 private_credential_manager_t *this, credential_set_t *set)
1143 {
1144 this->lock->write_lock(this->lock);
1145 this->sets->remove(this->sets, set, NULL);
1146 this->lock->unlock(this->lock);
1147 }
1148
1149 METHOD(credential_manager_t, add_validator, void,
1150 private_credential_manager_t *this, cert_validator_t *vdtr)
1151 {
1152 this->lock->write_lock(this->lock);
1153 this->sets->insert_last(this->validators, vdtr);
1154 this->lock->unlock(this->lock);
1155 }
1156
1157 METHOD(credential_manager_t, remove_validator, void,
1158 private_credential_manager_t *this, cert_validator_t *vdtr)
1159 {
1160 this->lock->write_lock(this->lock);
1161 this->validators->remove(this->validators, vdtr, NULL);
1162 this->lock->unlock(this->lock);
1163 }
1164
1165 METHOD(credential_manager_t, destroy, void,
1166 private_credential_manager_t *this)
1167 {
1168 cache_queue(this);
1169 this->cache_queue->destroy(this->cache_queue);
1170 if (this->cache)
1171 {
1172 this->sets->remove(this->sets, this->cache, NULL);
1173 this->cache->destroy(this->cache);
1174 }
1175 this->sets->destroy(this->sets);
1176 this->local_sets->destroy(this->local_sets);
1177 this->exclusive_local_sets->destroy(this->exclusive_local_sets);
1178 this->validators->destroy(this->validators);
1179 this->lock->destroy(this->lock);
1180 this->queue_mutex->destroy(this->queue_mutex);
1181 free(this);
1182 }
1183
1184 /*
1185 * see header file
1186 */
1187 credential_manager_t *credential_manager_create()
1188 {
1189 private_credential_manager_t *this;
1190
1191 INIT(this,
1192 .public = {
1193 .create_cert_enumerator = _create_cert_enumerator,
1194 .create_shared_enumerator = _create_shared_enumerator,
1195 .create_cdp_enumerator = _create_cdp_enumerator,
1196 .get_cert = _get_cert,
1197 .get_shared = _get_shared,
1198 .get_private = _get_private,
1199 .create_trusted_enumerator = _create_trusted_enumerator,
1200 .create_public_enumerator = _create_public_enumerator,
1201 .flush_cache = _flush_cache,
1202 .cache_cert = _cache_cert,
1203 .issued_by = _issued_by,
1204 .add_set = _add_set,
1205 .remove_set = _remove_set,
1206 .add_local_set = _add_local_set,
1207 .remove_local_set = _remove_local_set,
1208 .add_validator = _add_validator,
1209 .remove_validator = _remove_validator,
1210 .destroy = _destroy,
1211 },
1212 .sets = linked_list_create(),
1213 .validators = linked_list_create(),
1214 .cache_queue = linked_list_create(),
1215 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
1216 .queue_mutex = mutex_create(MUTEX_TYPE_DEFAULT),
1217 );
1218
1219 this->local_sets = thread_value_create((thread_cleanup_t)this->sets->destroy);
1220 this->exclusive_local_sets = thread_value_create((thread_cleanup_t)this->sets->destroy);
1221 if (lib->settings->get_bool(lib->settings, "libstrongswan.cert_cache", TRUE))
1222 {
1223 this->cache = cert_cache_create();
1224 this->sets->insert_first(this->sets, this->cache);
1225 }
1226
1227 return &this->public;
1228 }