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