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