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