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