Accept NULL auth_cfg_t passed to credential_manager_t.get_private()
[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, cache_cert, void,
454 private_credential_manager_t *this, certificate_t *cert)
455 {
456 credential_set_t *set;
457 enumerator_t *enumerator;
458
459 if (this->lock->try_write_lock(this->lock))
460 {
461 enumerator = this->sets->create_enumerator(this->sets);
462 while (enumerator->enumerate(enumerator, &set))
463 {
464 set->cache_cert(set, cert);
465 }
466 enumerator->destroy(enumerator);
467 this->lock->unlock(this->lock);
468 }
469 else
470 { /* we can't cache now as other threads are active, queue for later */
471 this->queue_mutex->lock(this->queue_mutex);
472 this->cache_queue->insert_last(this->cache_queue, cert->get_ref(cert));
473 this->queue_mutex->unlock(this->queue_mutex);
474 }
475 }
476
477 /**
478 * Try to cache certificates queued for caching
479 */
480 static void cache_queue(private_credential_manager_t *this)
481 {
482 credential_set_t *set;
483 certificate_t *cert;
484 enumerator_t *enumerator;
485
486 this->queue_mutex->lock(this->queue_mutex);
487 if (this->cache_queue->get_count(this->cache_queue) > 0 &&
488 this->lock->try_write_lock(this->lock))
489 {
490 while (this->cache_queue->remove_last(this->cache_queue,
491 (void**)&cert) == SUCCESS)
492 {
493 enumerator = this->sets->create_enumerator(this->sets);
494 while (enumerator->enumerate(enumerator, &set))
495 {
496 set->cache_cert(set, cert);
497 }
498 enumerator->destroy(enumerator);
499 cert->destroy(cert);
500 }
501 this->lock->unlock(this->lock);
502 }
503 this->queue_mutex->unlock(this->queue_mutex);
504 }
505
506 /**
507 * check a certificate for its lifetime
508 */
509 static bool check_certificate(private_credential_manager_t *this,
510 certificate_t *subject, certificate_t *issuer, bool online,
511 int pathlen, bool trusted, auth_cfg_t *auth)
512 {
513 time_t not_before, not_after;
514 cert_validator_t *validator;
515 enumerator_t *enumerator;
516
517 if (!subject->get_validity(subject, NULL, &not_before, &not_after))
518 {
519 DBG1(DBG_CFG, "subject certificate invalid (valid from %T to %T)",
520 &not_before, FALSE, &not_after, FALSE);
521 return FALSE;
522 }
523 if (!issuer->get_validity(issuer, NULL, &not_before, &not_after))
524 {
525 DBG1(DBG_CFG, "issuer certificate invalid (valid from %T to %T)",
526 &not_before, FALSE, &not_after, FALSE);
527 return FALSE;
528 }
529
530 enumerator = this->validators->create_enumerator(this->validators);
531 while (enumerator->enumerate(enumerator, &validator))
532 {
533 if (!validator->validate(validator, subject, issuer,
534 online, pathlen, trusted, auth))
535 {
536 enumerator->destroy(enumerator);
537 return FALSE;
538 }
539 }
540 enumerator->destroy(enumerator);
541 return TRUE;
542 }
543
544 /**
545 * Get a trusted certificate from a credential set
546 */
547 static certificate_t *get_pretrusted_cert(private_credential_manager_t *this,
548 key_type_t type, identification_t *id)
549 {
550 certificate_t *subject;
551 public_key_t *public;
552
553 subject = get_cert(this, CERT_ANY, type, id, TRUE);
554 if (!subject)
555 {
556 return NULL;
557 }
558 public = subject->get_public_key(subject);
559 if (!public)
560 {
561 subject->destroy(subject);
562 return NULL;
563 }
564 public->destroy(public);
565 return subject;
566 }
567
568 /**
569 * Get the issuing certificate of a subject certificate
570 */
571 static certificate_t *get_issuer_cert(private_credential_manager_t *this,
572 certificate_t *subject, bool trusted)
573 {
574 enumerator_t *enumerator;
575 certificate_t *issuer = NULL, *candidate;
576
577 enumerator = create_cert_enumerator(this, subject->get_type(subject), KEY_ANY,
578 subject->get_issuer(subject), trusted);
579 while (enumerator->enumerate(enumerator, &candidate))
580 {
581 if (this->cache->issued_by(this->cache, subject, candidate))
582 {
583 issuer = candidate->get_ref(candidate);
584 break;
585 }
586 }
587 enumerator->destroy(enumerator);
588 return issuer;
589 }
590
591 /**
592 * Get the strength of certificate, add it to auth
593 */
594 static void get_key_strength(certificate_t *cert, auth_cfg_t *auth)
595 {
596 uintptr_t strength;
597 public_key_t *key;
598 key_type_t type;
599
600 key = cert->get_public_key(cert);
601 if (key)
602 {
603 type = key->get_type(key);
604 strength = key->get_keysize(key);
605 DBG2(DBG_CFG, " certificate \"%Y\" key: %d bit %N",
606 cert->get_subject(cert), strength, key_type_names, type);
607 switch (type)
608 {
609 case KEY_RSA:
610 auth->add(auth, AUTH_RULE_RSA_STRENGTH, strength);
611 break;
612 case KEY_ECDSA:
613 auth->add(auth, AUTH_RULE_ECDSA_STRENGTH, strength);
614 break;
615 default:
616 break;
617 }
618 key->destroy(key);
619 }
620 }
621
622 /**
623 * try to verify the trust chain of subject, return TRUE if trusted
624 */
625 static bool verify_trust_chain(private_credential_manager_t *this,
626 certificate_t *subject, auth_cfg_t *result,
627 bool trusted, bool online)
628 {
629 certificate_t *current, *issuer;
630 auth_cfg_t *auth;
631 int pathlen;
632
633 auth = auth_cfg_create();
634 get_key_strength(subject, auth);
635 current = subject->get_ref(subject);
636 auth->add(auth, AUTH_RULE_SUBJECT_CERT, current->get_ref(current));
637
638 for (pathlen = 0; pathlen <= MAX_TRUST_PATH_LEN; pathlen++)
639 {
640 issuer = get_issuer_cert(this, current, TRUE);
641 if (issuer)
642 {
643 /* accept only self-signed CAs as trust anchor */
644 if (this->cache->issued_by(this->cache, issuer, issuer))
645 {
646 auth->add(auth, AUTH_RULE_CA_CERT, issuer->get_ref(issuer));
647 DBG1(DBG_CFG, " using trusted ca certificate \"%Y\"",
648 issuer->get_subject(issuer));
649 trusted = TRUE;
650 }
651 else
652 {
653 auth->add(auth, AUTH_RULE_IM_CERT, issuer->get_ref(issuer));
654 DBG1(DBG_CFG, " using trusted intermediate ca certificate "
655 "\"%Y\"", issuer->get_subject(issuer));
656 }
657 }
658 else
659 {
660 issuer = get_issuer_cert(this, current, FALSE);
661 if (issuer)
662 {
663 if (current->equals(current, issuer))
664 {
665 DBG1(DBG_CFG, " self-signed certificate \"%Y\" is not trusted",
666 current->get_subject(current));
667 issuer->destroy(issuer);
668 break;
669 }
670 auth->add(auth, AUTH_RULE_IM_CERT, issuer->get_ref(issuer));
671 DBG1(DBG_CFG, " using untrusted intermediate certificate "
672 "\"%Y\"", issuer->get_subject(issuer));
673 }
674 else
675 {
676 DBG1(DBG_CFG, "no issuer certificate found for \"%Y\"",
677 current->get_subject(current));
678 break;
679 }
680 }
681 if (!check_certificate(this, current, issuer, online,
682 pathlen, trusted, auth))
683 {
684 trusted = FALSE;
685 issuer->destroy(issuer);
686 break;
687 }
688 if (issuer)
689 {
690 get_key_strength(issuer, auth);
691 }
692 current->destroy(current);
693 current = issuer;
694 if (trusted)
695 {
696 DBG1(DBG_CFG, " reached self-signed root ca with a path length of %d",
697 pathlen);
698 break;
699 }
700 }
701 current->destroy(current);
702 if (pathlen > MAX_TRUST_PATH_LEN)
703 {
704 DBG1(DBG_CFG, "maximum path length of %d exceeded", MAX_TRUST_PATH_LEN);
705 }
706 if (trusted)
707 {
708 result->merge(result, auth, FALSE);
709 }
710 auth->destroy(auth);
711 return trusted;
712 }
713
714 /**
715 * List find match function for certificates
716 */
717 static bool cert_equals(certificate_t *a, certificate_t *b)
718 {
719 return a->equals(a, b);
720 }
721
722 /**
723 * enumerator for trusted certificates
724 */
725 typedef struct {
726 /** implements enumerator_t interface */
727 enumerator_t public;
728 /** enumerator over candidate peer certificates */
729 enumerator_t *candidates;
730 /** reference to the credential_manager */
731 private_credential_manager_t *this;
732 /** type of the requested key */
733 key_type_t type;
734 /** identity the requested key belongs to */
735 identification_t *id;
736 /** TRUE to do CRL/OCSP checking */
737 bool online;
738 /** pretrusted certificate we have served at first invocation */
739 certificate_t *pretrusted;
740 /** currently enumerating auth config */
741 auth_cfg_t *auth;
742 /** list of failed candidates */
743 linked_list_t *failed;
744 } trusted_enumerator_t;
745
746 METHOD(enumerator_t, trusted_enumerate, bool,
747 trusted_enumerator_t *this, certificate_t **cert, auth_cfg_t **auth)
748 {
749 certificate_t *current;
750
751 DESTROY_IF(this->auth);
752 this->auth = auth_cfg_create();
753
754 if (!this->candidates)
755 {
756 /* first invocation, build enumerator for next one */
757 this->candidates = create_cert_enumerator(this->this, CERT_ANY,
758 this->type, this->id, FALSE);
759 /* check if we have a trusted certificate for that peer */
760 this->pretrusted = get_pretrusted_cert(this->this, this->type, this->id);
761 if (this->pretrusted)
762 {
763 /* if we find a trusted self signed certificate, we just accept it.
764 * However, in order to fulfill authorization rules, we try to build
765 * the trust chain if it is not self signed */
766 if (this->this->cache->issued_by(this->this->cache,
767 this->pretrusted, this->pretrusted) ||
768 verify_trust_chain(this->this, this->pretrusted, this->auth,
769 TRUE, this->online))
770 {
771 DBG1(DBG_CFG, " using trusted certificate \"%Y\"",
772 this->pretrusted->get_subject(this->pretrusted));
773 *cert = this->pretrusted;
774 if (!this->auth->get(this->auth, AUTH_RULE_SUBJECT_CERT))
775 { /* add cert to auth info, if not returned by trustchain */
776 this->auth->add(this->auth, AUTH_RULE_SUBJECT_CERT,
777 this->pretrusted->get_ref(this->pretrusted));
778 }
779 if (auth)
780 {
781 *auth = this->auth;
782 }
783 return TRUE;
784 }
785 }
786 }
787 /* try to verify the trust chain for each certificate found */
788 while (this->candidates->enumerate(this->candidates, &current))
789 {
790 if (this->pretrusted &&
791 this->pretrusted->equals(this->pretrusted, current))
792 { /* skip pretrusted certificate we already served */
793 continue;
794 }
795
796 if (this->failed->find_first(this->failed, (void*)cert_equals,
797 NULL, current) == SUCCESS)
798 { /* check each candidate only once */
799 continue;
800 }
801
802 DBG1(DBG_CFG, " using certificate \"%Y\"",
803 current->get_subject(current));
804 if (verify_trust_chain(this->this, current, this->auth, FALSE,
805 this->online))
806 {
807 *cert = current;
808 if (auth)
809 {
810 *auth = this->auth;
811 }
812 return TRUE;
813 }
814 this->failed->insert_last(this->failed, current->get_ref(current));
815 }
816 return FALSE;
817 }
818
819 METHOD(enumerator_t, trusted_destroy, void,
820 trusted_enumerator_t *this)
821 {
822 DESTROY_IF(this->pretrusted);
823 DESTROY_IF(this->auth);
824 DESTROY_IF(this->candidates);
825 this->failed->destroy_offset(this->failed, offsetof(certificate_t, destroy));
826 free(this);
827 }
828
829 METHOD(credential_manager_t, create_trusted_enumerator, enumerator_t*,
830 private_credential_manager_t *this, key_type_t type,
831 identification_t *id, bool online)
832 {
833 trusted_enumerator_t *enumerator;
834
835 INIT(enumerator,
836 .public = {
837 .enumerate = (void*)_trusted_enumerate,
838 .destroy = _trusted_destroy,
839 },
840 .this = this,
841 .type = type,
842 .id = id,
843 .online = online,
844 .failed = linked_list_create(),
845 );
846 return &enumerator->public;
847 }
848
849 /**
850 * enumerator for public keys
851 */
852 typedef struct {
853 /** implements enumerator_t interface */
854 enumerator_t public;
855 /** enumerator over candidate peer certificates */
856 enumerator_t *inner;
857 /** reference to the credential_manager */
858 private_credential_manager_t *this;
859 /** currently enumerating key */
860 public_key_t *current;
861 /** credset wrapper around auth config */
862 auth_cfg_wrapper_t *wrapper;
863 } public_enumerator_t;
864
865 METHOD(enumerator_t, public_enumerate, bool,
866 public_enumerator_t *this, public_key_t **key, auth_cfg_t **auth)
867 {
868 certificate_t *cert;
869
870 while (this->inner->enumerate(this->inner, &cert, auth))
871 {
872 DESTROY_IF(this->current);
873 this->current = cert->get_public_key(cert);
874 if (this->current)
875 {
876 *key = this->current;
877 return TRUE;
878 }
879 }
880 return FALSE;
881 }
882
883 METHOD(enumerator_t, public_destroy, void,
884 public_enumerator_t *this)
885 {
886 DESTROY_IF(this->current);
887 this->inner->destroy(this->inner);
888 if (this->wrapper)
889 {
890 remove_local_set(this->this, &this->wrapper->set);
891 this->wrapper->destroy(this->wrapper);
892 }
893 this->this->lock->unlock(this->this->lock);
894
895 /* check for delayed certificate cache queue */
896 cache_queue(this->this);
897 free(this);
898 }
899
900 METHOD(credential_manager_t, create_public_enumerator, enumerator_t*,
901 private_credential_manager_t *this, key_type_t type, identification_t *id,
902 auth_cfg_t *auth)
903 {
904 public_enumerator_t *enumerator;
905
906 INIT(enumerator,
907 .public = {
908 .enumerate = (void*)_public_enumerate,
909 .destroy = _public_destroy,
910 },
911 .inner = create_trusted_enumerator(this, type, id, TRUE),
912 .this = this,
913 );
914 if (auth)
915 {
916 enumerator->wrapper = auth_cfg_wrapper_create(auth);
917 add_local_set(this, &enumerator->wrapper->set, FALSE);
918 }
919 this->lock->read_lock(this->lock);
920 return &enumerator->public;
921 }
922
923 /**
924 * Check if an helper contains a certificate as trust anchor
925 */
926 static bool auth_contains_cacert(auth_cfg_t *auth, certificate_t *cert)
927 {
928 enumerator_t *enumerator;
929 identification_t *value;
930 auth_rule_t type;
931 bool found = FALSE;
932
933 enumerator = auth->create_enumerator(auth);
934 while (enumerator->enumerate(enumerator, &type, &value))
935 {
936 if (type == AUTH_RULE_CA_CERT &&
937 cert->equals(cert, (certificate_t*)value))
938 {
939 found = TRUE;
940 break;
941 }
942 }
943 enumerator->destroy(enumerator);
944 return found;
945 }
946
947 /**
948 * build a trustchain from subject up to a trust anchor in trusted
949 */
950 static auth_cfg_t *build_trustchain(private_credential_manager_t *this,
951 certificate_t *subject, auth_cfg_t *auth)
952 {
953 certificate_t *issuer, *current;
954 auth_cfg_t *trustchain;
955 int pathlen = 0;
956 bool has_anchor;
957
958 trustchain = auth_cfg_create();
959 has_anchor = auth->get(auth, AUTH_RULE_CA_CERT) != NULL;
960 current = subject->get_ref(subject);
961 while (TRUE)
962 {
963 if (auth_contains_cacert(auth, current))
964 {
965 trustchain->add(trustchain, AUTH_RULE_CA_CERT, current);
966 return trustchain;
967 }
968 if (subject == current)
969 {
970 trustchain->add(trustchain, AUTH_RULE_SUBJECT_CERT, current);
971 }
972 else
973 {
974 if (!has_anchor &&
975 this->cache->issued_by(this->cache, current, current))
976 { /* If no trust anchor specified, accept any CA */
977 trustchain->add(trustchain, AUTH_RULE_CA_CERT, current);
978 return trustchain;
979 }
980 trustchain->add(trustchain, AUTH_RULE_IM_CERT, current);
981 }
982 if (pathlen++ > MAX_TRUST_PATH_LEN)
983 {
984 break;
985 }
986 issuer = get_issuer_cert(this, current, FALSE);
987 if (!issuer)
988 {
989 if (!has_anchor)
990 { /* If no trust anchor specified, accept incomplete chains */
991 return trustchain;
992 }
993 break;
994 }
995 if (has_anchor && issuer->equals(issuer, current))
996 {
997 issuer->destroy(issuer);
998 break;
999 }
1000 current = issuer;
1001 }
1002 trustchain->destroy(trustchain);
1003 return NULL;
1004 }
1005
1006 /**
1007 * find a private key of a give certificate
1008 */
1009 static private_key_t *get_private_by_cert(private_credential_manager_t *this,
1010 certificate_t *cert, key_type_t type)
1011 {
1012 private_key_t *private = NULL;
1013 identification_t *keyid;
1014 chunk_t chunk;
1015 public_key_t *public;
1016
1017 public = cert->get_public_key(cert);
1018 if (public)
1019 {
1020 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
1021 {
1022 keyid = identification_create_from_encoding(ID_KEY_ID, chunk);
1023 private = get_private_by_keyid(this, type, keyid);
1024 keyid->destroy(keyid);
1025 }
1026 public->destroy(public);
1027 }
1028 return private;
1029 }
1030
1031 METHOD(credential_manager_t, get_private, private_key_t*,
1032 private_credential_manager_t *this, key_type_t type, identification_t *id,
1033 auth_cfg_t *auth)
1034 {
1035 enumerator_t *enumerator;
1036 certificate_t *cert;
1037 private_key_t *private = NULL;
1038 auth_cfg_t *trustchain;
1039
1040 /* check if this is a lookup by key ID, and do it if so */
1041 if (id && id->get_type(id) == ID_KEY_ID)
1042 {
1043 private = get_private_by_keyid(this, type, id);
1044 if (private)
1045 {
1046 return private;
1047 }
1048 }
1049
1050 if (auth)
1051 {
1052 /* if a specific certificate is preferred, check for a matching key */
1053 cert = auth->get(auth, AUTH_RULE_SUBJECT_CERT);
1054 if (cert)
1055 {
1056 private = get_private_by_cert(this, cert, type);
1057 if (private)
1058 {
1059 trustchain = build_trustchain(this, cert, auth);
1060 if (trustchain)
1061 {
1062 auth->merge(auth, trustchain, FALSE);
1063 trustchain->destroy(trustchain);
1064 }
1065 return private;
1066 }
1067 }
1068
1069 /* try to build a trust chain for each certificate found */
1070 enumerator = create_cert_enumerator(this, CERT_ANY, type, id, FALSE);
1071 while (enumerator->enumerate(enumerator, &cert))
1072 {
1073 private = get_private_by_cert(this, cert, type);
1074 if (private)
1075 {
1076 trustchain = build_trustchain(this, cert, auth);
1077 if (trustchain)
1078 {
1079 auth->merge(auth, trustchain, FALSE);
1080 trustchain->destroy(trustchain);
1081 break;
1082 }
1083 private->destroy(private);
1084 private = NULL;
1085 }
1086 }
1087 enumerator->destroy(enumerator);
1088 }
1089
1090 /* if no valid trustchain was found, fall back to the first usable cert */
1091 if (!private)
1092 {
1093 enumerator = create_cert_enumerator(this, CERT_ANY, type, id, FALSE);
1094 while (enumerator->enumerate(enumerator, &cert))
1095 {
1096 private = get_private_by_cert(this, cert, type);
1097 if (private)
1098 {
1099 if (auth)
1100 {
1101 auth->add(auth, AUTH_RULE_SUBJECT_CERT, cert->get_ref(cert));
1102 }
1103 break;
1104 }
1105 }
1106 enumerator->destroy(enumerator);
1107 }
1108 return private;
1109 }
1110
1111 METHOD(credential_manager_t, flush_cache, void,
1112 private_credential_manager_t *this, certificate_type_t type)
1113 {
1114 this->cache->flush(this->cache, type);
1115 }
1116
1117 METHOD(credential_manager_t, issued_by, bool,
1118 private_credential_manager_t *this, certificate_t *subject,
1119 certificate_t *issuer)
1120 {
1121 return this->cache->issued_by(this->cache, subject, issuer);
1122 }
1123
1124 METHOD(credential_manager_t, add_set, void,
1125 private_credential_manager_t *this, credential_set_t *set)
1126 {
1127 this->lock->write_lock(this->lock);
1128 this->sets->insert_last(this->sets, set);
1129 this->lock->unlock(this->lock);
1130 }
1131
1132 METHOD(credential_manager_t, remove_set, void,
1133 private_credential_manager_t *this, credential_set_t *set)
1134 {
1135 this->lock->write_lock(this->lock);
1136 this->sets->remove(this->sets, set, NULL);
1137 this->lock->unlock(this->lock);
1138 }
1139
1140 METHOD(credential_manager_t, add_validator, void,
1141 private_credential_manager_t *this, cert_validator_t *vdtr)
1142 {
1143 this->lock->write_lock(this->lock);
1144 this->sets->insert_last(this->validators, vdtr);
1145 this->lock->unlock(this->lock);
1146 }
1147
1148 METHOD(credential_manager_t, remove_validator, void,
1149 private_credential_manager_t *this, cert_validator_t *vdtr)
1150 {
1151 this->lock->write_lock(this->lock);
1152 this->validators->remove(this->validators, vdtr, NULL);
1153 this->lock->unlock(this->lock);
1154 }
1155
1156 METHOD(credential_manager_t, destroy, void,
1157 private_credential_manager_t *this)
1158 {
1159 cache_queue(this);
1160 this->cache_queue->destroy(this->cache_queue);
1161 this->sets->remove(this->sets, this->cache, NULL);
1162 this->sets->destroy(this->sets);
1163 this->local_sets->destroy(this->local_sets);
1164 this->exclusive_local_sets->destroy(this->exclusive_local_sets);
1165 this->cache->destroy(this->cache);
1166 this->validators->destroy(this->validators);
1167 this->lock->destroy(this->lock);
1168 this->queue_mutex->destroy(this->queue_mutex);
1169 free(this);
1170 }
1171
1172 /*
1173 * see header file
1174 */
1175 credential_manager_t *credential_manager_create()
1176 {
1177 private_credential_manager_t *this;
1178
1179 INIT(this,
1180 .public = {
1181 .create_cert_enumerator = _create_cert_enumerator,
1182 .create_shared_enumerator = _create_shared_enumerator,
1183 .create_cdp_enumerator = _create_cdp_enumerator,
1184 .get_cert = _get_cert,
1185 .get_shared = _get_shared,
1186 .get_private = _get_private,
1187 .create_trusted_enumerator = _create_trusted_enumerator,
1188 .create_public_enumerator = _create_public_enumerator,
1189 .flush_cache = _flush_cache,
1190 .cache_cert = _cache_cert,
1191 .issued_by = _issued_by,
1192 .add_set = _add_set,
1193 .remove_set = _remove_set,
1194 .add_local_set = _add_local_set,
1195 .remove_local_set = _remove_local_set,
1196 .add_validator = _add_validator,
1197 .remove_validator = _remove_validator,
1198 .destroy = _destroy,
1199 },
1200 .sets = linked_list_create(),
1201 .validators = linked_list_create(),
1202 .cache = cert_cache_create(),
1203 .cache_queue = linked_list_create(),
1204 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
1205 .queue_mutex = mutex_create(MUTEX_TYPE_DEFAULT),
1206 );
1207
1208 this->local_sets = thread_value_create((thread_cleanup_t)this->sets->destroy);
1209 this->exclusive_local_sets = thread_value_create((thread_cleanup_t)this->sets->destroy);
1210 this->sets->insert_first(this->sets, this->cache);
1211
1212 return &this->public;
1213 }