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