refactored buggy trustchain building, fixed refcount bug
[strongswan.git] / src / charon / 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 * $Id$
16 */
17
18 #include "credential_manager.h"
19
20 #include <daemon.h>
21 #include <utils/linked_list.h>
22 #include <utils/mutex.h>
23 #include <credentials/certificates/x509.h>
24 #include <credentials/certificates/crl.h>
25 #include <credentials/certificates/ocsp_request.h>
26 #include <credentials/certificates/ocsp_response.h>
27
28 #define MAX_CA_LEVELS 6
29
30 typedef struct private_credential_manager_t private_credential_manager_t;
31
32 /**
33 * private data of credential_manager
34 */
35 struct private_credential_manager_t {
36
37 /**
38 * public functions
39 */
40 credential_manager_t public;
41
42 /**
43 * list of credential sets
44 */
45 linked_list_t *sets;
46
47 /**
48 * mutex to gain exclusive access
49 */
50 mutex_t *mutex;
51 };
52
53 /** data to pass to create_private_enumerator */
54 typedef struct {
55 private_credential_manager_t *this;
56 key_type_t type;
57 identification_t* keyid;
58 } private_data_t;
59
60 /** data to pass to create_cert_enumerator */
61 typedef struct {
62 private_credential_manager_t *this;
63 certificate_type_t cert;
64 key_type_t key;
65 identification_t *id;
66 bool trusted;
67 } cert_data_t;
68
69 /** data to pass to create_cdp_enumerator */
70 typedef struct {
71 private_credential_manager_t *this;
72 certificate_type_t type;
73 identification_t *id;
74 } cdp_data_t;
75
76 /** data to pass to create_shared_enumerator */
77 typedef struct {
78 private_credential_manager_t *this;
79 shared_key_type_t type;
80 identification_t *me;
81 identification_t *other;
82 } shared_data_t;
83
84 /**
85 * cleanup function for cert data
86 */
87 static void destroy_cert_data(cert_data_t *data)
88 {
89 data->this->mutex->unlock(data->this->mutex);
90 free(data);
91 }
92
93 /**
94 * enumerator constructor for certificates
95 */
96 static enumerator_t *create_cert(credential_set_t *set, cert_data_t *data)
97 {
98 return set->create_cert_enumerator(set, data->cert, data->key,
99 data->id, data->trusted);
100 }
101
102 /**
103 * Implementation of credential_manager_t.create_cert_enumerator.
104 */
105 static enumerator_t *create_cert_enumerator(private_credential_manager_t *this,
106 certificate_type_t certificate, key_type_t key,
107 identification_t *id, bool trusted)
108 {
109 cert_data_t *data = malloc_thing(cert_data_t);
110 data->this = this;
111 data->cert = certificate;
112 data->key = key;
113 data->id = id;
114 data->trusted = trusted;
115
116 this->mutex->lock(this->mutex);
117 return enumerator_create_nested(this->sets->create_enumerator(this->sets),
118 (void*)create_cert, data,
119 (void*)destroy_cert_data);
120 }
121
122 /**
123 * Implementation of credential_manager_t.get_cert.
124 */
125 static certificate_t *get_cert(private_credential_manager_t *this,
126 certificate_type_t cert, key_type_t key,
127 identification_t *id, bool trusted)
128 {
129 certificate_t *current, *found = NULL;
130 enumerator_t *enumerator;
131
132 this->mutex->lock(this->mutex);
133 enumerator = create_cert_enumerator(this, cert, key, id, trusted);
134 if (enumerator->enumerate(enumerator, &current))
135 {
136 /* TODO: best match? order by keyid, subject, sualtname */
137 found = current->get_ref(current);
138 }
139 enumerator->destroy(enumerator);
140 this->mutex->unlock(this->mutex);
141 return found;
142 }
143
144
145 /**
146 * cleanup function for cdp data
147 */
148 static void destroy_cdp_data(cdp_data_t *data)
149 {
150 data->this->mutex->unlock(data->this->mutex);
151 free(data);
152 }
153
154 /**
155 * enumerator constructor for CDPs
156 */
157 static enumerator_t *create_cdp(credential_set_t *set, cdp_data_t *data)
158 {
159 return set->create_cdp_enumerator(set, data->type, data->id);
160 }
161 /**
162 * Implementation of credential_manager_t.create_cdp_enumerator.
163 */
164 static enumerator_t * create_cdp_enumerator(private_credential_manager_t *this,
165 credential_type_t type, identification_t *id)
166 {
167 cdp_data_t *data = malloc_thing(cdp_data_t);
168 data->this = this;
169 data->type = type;
170 data->id = id;
171
172 this->mutex->lock(this->mutex);
173 return enumerator_create_nested(this->sets->create_enumerator(this->sets),
174 (void*)create_cdp, data,
175 (void*)destroy_cdp_data);
176 }
177
178 /**
179 * cleanup function for private data
180 */
181 static void destroy_private_data(private_data_t *data)
182 {
183 data->this->mutex->unlock(data->this->mutex);
184 free(data);
185 }
186
187 /**
188 * enumerator constructor for private keys
189 */
190 static enumerator_t *create_private(credential_set_t *set, private_data_t *data)
191 {
192 return set->create_private_enumerator(set, data->type, data->keyid);
193 }
194
195 /**
196 * Implementation of credential_manager_t.get_private_by_keyid.
197 */
198 static enumerator_t* create_private_enumerator(
199 private_credential_manager_t *this,
200 key_type_t key, identification_t *keyid)
201 {
202 private_data_t *data;
203
204 data = malloc_thing(private_data_t);
205 data->this = this;
206 data->type = key;
207 data->keyid = keyid;
208 this->mutex->lock(this->mutex);
209 return enumerator_create_nested(this->sets->create_enumerator(this->sets),
210 (void*)create_private, data, (void*)destroy_private_data);
211 }
212
213 /**
214 * Implementation of credential_manager_t.get_private_by_keyid.
215 */
216 static private_key_t *get_private_by_keyid(private_credential_manager_t *this,
217 key_type_t key, identification_t *keyid)
218 {
219 private_key_t *found = NULL;
220 enumerator_t *enumerator;
221
222 enumerator = create_private_enumerator(this, key, keyid);
223 if (enumerator->enumerate(enumerator, &found))
224 {
225 found->get_ref(found);
226 }
227 enumerator->destroy(enumerator);
228 return found;
229 }
230
231 /**
232 * cleanup function for shared data
233 */
234 static void destroy_shared_data(shared_data_t *data)
235 {
236 data->this->mutex->unlock(data->this->mutex);
237 free(data);
238 }
239
240 /**
241 * enumerator constructor for shared keys
242 */
243 static enumerator_t *create_shared(credential_set_t *set, shared_data_t *data)
244 {
245 return set->create_shared_enumerator(set, data->type, data->me, data->other);
246 }
247
248 /**
249 * Implementation of credential_manager_t.create_shared_enumerator.
250 */
251 static enumerator_t *create_shared_enumerator(private_credential_manager_t *this,
252 shared_key_type_t type,
253 identification_t *me, identification_t *other)
254 {
255 shared_data_t *data = malloc_thing(shared_data_t);
256 data->this = this;
257 data->type = type;
258 data->me = me;
259 data->other = other;
260
261 this->mutex->lock(this->mutex);
262 return enumerator_create_nested(this->sets->create_enumerator(this->sets),
263 (void*)create_shared, data,
264 (void*)destroy_shared_data);
265 }
266
267 /**
268 * Implementation of credential_manager_t.get_shared.
269 */
270 static shared_key_t *get_shared(private_credential_manager_t *this,
271 shared_key_type_t type, identification_t *me,
272 identification_t *other)
273 {
274 shared_key_t *current, *found = NULL;
275 id_match_t *best_me = ID_MATCH_NONE, *best_other = ID_MATCH_NONE;
276 id_match_t *match_me, *match_other;
277 enumerator_t *enumerator;
278
279 enumerator = create_shared_enumerator(this, type, me, other);
280 while (enumerator->enumerate(enumerator, &current, &match_me, &match_other))
281 {
282 if (match_other > best_other ||
283 (match_other == best_other && match_me > best_me))
284 {
285 DESTROY_IF(found);
286 found = current->get_ref(current);
287 best_me = match_me;
288 best_other = match_other;
289 }
290 }
291 enumerator->destroy(enumerator);
292 return found;
293 }
294
295 /**
296 * forward declaration
297 */
298 static certificate_t *get_trusted_cert(private_credential_manager_t *this,
299 key_type_t type, identification_t *id,
300 auth_info_t *auth, bool crl, bool ocsp);
301 /**
302 * return null ;-)
303 */
304 static void *return_null()
305 {
306 return NULL;
307 }
308
309 /**
310 * credential_set_t implementation around an OCSP response
311 */
312 typedef struct ocsp_wrapper_t {
313 credential_set_t set;
314 ocsp_response_t *response;
315 } ocsp_wrapper_t;
316
317 /**
318 * enumerator for ocsp_wrapper_t.create_cert_enumerator()
319 */
320 typedef struct {
321 enumerator_t public;
322 enumerator_t *inner;
323 certificate_type_t cert;
324 key_type_t key;
325 identification_t *id;
326 } ocsp_wrapper_enumerator_t;
327
328 /**
329 * enumerate function for ocsp_wrapper_enumerator_t
330 */
331 static bool ocsp_wrapper_enum_enumerate(ocsp_wrapper_enumerator_t *this,
332 certificate_t **cert)
333 {
334 certificate_t *current;
335 public_key_t *public;
336
337 while (this->inner->enumerate(this->inner, &current))
338 {
339 if (this->cert != CERT_ANY && this->cert != current->get_type(current))
340 { /* CERT type requested, but does not match */
341 continue;
342 }
343 public = current->get_public_key(current);
344 if (this->key != KEY_ANY && !public)
345 { /* key type requested, but no public key */
346 DESTROY_IF(public);
347 continue;
348 }
349 if (this->key != KEY_ANY && public && this->key != public->get_type(public))
350 { /* key type requested, but public key has another type */
351 DESTROY_IF(public);
352 continue;
353 }
354 DESTROY_IF(public);
355 if (this->id && !current->has_subject(current, this->id))
356 { /* subject requested, but does not match */
357 continue;
358 }
359 *cert = current;
360 return TRUE;
361 }
362 return FALSE;
363 }
364
365 /**
366 * destroy function for ocsp_wrapper_enumerator_t
367 */
368 static void ocsp_wrapper_enum_destroy(ocsp_wrapper_enumerator_t *this)
369 {
370 this->inner->destroy(this->inner);
371 free(this);
372 }
373
374 /**
375 * implementation of ocsp_wrapper_t.set.create_cert_enumerator
376 */
377 static enumerator_t *ocsp_wrapper_create_enumerator(ocsp_wrapper_t *this,
378 certificate_type_t cert, key_type_t key,
379 identification_t *id, bool trusted)
380 {
381 ocsp_wrapper_enumerator_t *enumerator;
382
383 if (trusted)
384 {
385 return NULL;
386 }
387
388 enumerator = malloc_thing(ocsp_wrapper_enumerator_t);
389 enumerator->cert = cert;
390 enumerator->key = key;
391 enumerator->id = id;
392 enumerator->inner = this->response->create_cert_enumerator(this->response);
393 enumerator->public.enumerate = (void*)ocsp_wrapper_enum_enumerate;
394 enumerator->public.destroy = (void*)ocsp_wrapper_enum_destroy;
395 return &enumerator->public;
396 }
397
398 /**
399 * create credential_set wrapper around an OCSP response
400 */
401 static ocsp_wrapper_t *ocsp_wrapper_create(ocsp_response_t *response)
402 {
403 ocsp_wrapper_t *this = malloc_thing(ocsp_wrapper_t);
404
405 this->response = response;
406 this->set.create_private_enumerator = (void*)return_null;
407 this->set.create_cert_enumerator = (void*)ocsp_wrapper_create_enumerator;
408 this->set.create_shared_enumerator = (void*)return_null;
409 this->set.create_cdp_enumerator = (void*)return_null;
410
411 return this;
412 }
413
414 /**
415 * Do an OCSP request
416 */
417 static ocsp_response_t *fetch_ocsp(private_credential_manager_t *this, char *url,
418 certificate_t *subject, certificate_t *issuer)
419 {
420 certificate_t *request, *response, *issuer_cert;
421 chunk_t send, receive;
422 identification_t *responder;
423 auth_info_t *auth;
424 ocsp_wrapper_t *wrapper;
425
426 /* TODO: requestor name, signature */
427 request = lib->creds->create(lib->creds,
428 CRED_CERTIFICATE, CERT_X509_OCSP_REQUEST,
429 BUILD_CA_CERT, issuer->get_ref(issuer),
430 BUILD_CERT, subject->get_ref(subject), BUILD_END);
431 if (!request)
432 {
433 DBG1(DBG_CFG, "generating OCSP request failed");
434 return NULL;
435 }
436
437 send = request->get_encoding(request);
438 request->destroy(request);
439 if (lib->fetcher->fetch(lib->fetcher, url, &receive,
440 FETCH_REQUEST_DATA, send,
441 FETCH_REQUEST_TYPE, "application/ocsp-request",
442 FETCH_END) != SUCCESS)
443 {
444 DBG1(DBG_CFG, "OCSP request to %s failed", url);
445 chunk_free(&send);
446 return NULL;
447 }
448 chunk_free(&send);
449
450 response = lib->creds->create(lib->creds,
451 CRED_CERTIFICATE, CERT_X509_OCSP_RESPONSE,
452 BUILD_BLOB_ASN1_DER, receive, BUILD_END);
453 if (!response)
454 {
455 DBG1(DBG_CFG, "parsing OCSP response from %s failed", url);
456 return NULL;
457 }
458
459 responder = response->get_issuer(response);
460 auth = auth_info_create();
461 wrapper = ocsp_wrapper_create((ocsp_response_t*)response);
462 this->sets->insert_first(this->sets, wrapper);
463 issuer_cert = get_trusted_cert(this, KEY_ANY, responder, auth, FALSE, FALSE);
464 this->sets->remove(this->sets, wrapper, NULL);
465 free(wrapper);
466 auth->destroy(auth);
467 if (!issuer_cert)
468 {
469 DBG1(DBG_CFG, "verifying OCSP response failed, no trusted "
470 "certificate found");
471 response->destroy(response);
472 return NULL;
473 }
474 if (!response->issued_by(response, issuer_cert, TRUE))
475 {
476 DBG1(DBG_CFG, "verifying OCSP response signature failed");
477 response->destroy(response);
478 issuer_cert->destroy(issuer_cert);
479 return NULL;
480 }
481 issuer_cert->destroy(issuer_cert);
482
483 /* TODO: cache response? */
484
485 return (ocsp_response_t*)response;
486 }
487
488 /**
489 * validate a x509 certificate using OCSP
490 */
491 static cert_validation_t check_ocsp(private_credential_manager_t *this,
492 x509_t *subject, x509_t *issuer,
493 auth_info_t *auth)
494 {
495 public_key_t *public;
496 enumerator_t *enumerator;
497 ocsp_response_t *response = NULL;
498 certificate_t *cert, *sub = (certificate_t*)subject;
499 cert_validation_t valid = VALIDATION_SKIPPED;
500 identification_t *keyid = NULL;
501 char *url;
502
503 cert = &issuer->interface;
504 public = cert->get_public_key(cert);
505 if (public)
506 {
507 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
508 }
509
510 /* find a OCSP response by Authority key identifier (cache) */
511 if (keyid)
512 {
513 time_t update, best_update = 0;
514
515 enumerator = create_cert_enumerator(this, CERT_X509_OCSP_RESPONSE,
516 KEY_ANY, keyid, TRUE);
517 while (enumerator->enumerate(enumerator, &cert))
518 { /* get newest valid response */
519 if (cert->has_subject(cert, sub->get_subject(sub)) &&
520 cert->get_validity(cert, NULL, &update, NULL) &&
521 update > best_update)
522 {
523 best_update = update;
524 DESTROY_IF(&response->certificate);
525 response = (ocsp_response_t*)cert;
526 valid = VALIDATION_FAILED;
527 }
528 }
529 enumerator->destroy(enumerator);
530 }
531 /* fallback to URL fetching from CDPs */
532 if (!response && keyid)
533 {
534 enumerator = create_cdp_enumerator(this, CERT_X509_OCSP_RESPONSE, keyid);
535 while (enumerator->enumerate(enumerator, &url))
536 {
537 valid = VALIDATION_FAILED;
538 response = fetch_ocsp(this, url, &subject->interface, &issuer->interface);
539 if (response)
540 {
541 break;
542 }
543 }
544 enumerator->destroy(enumerator);
545 }
546 /* fallback to URL fetching from subject certificate's URIs */
547 if (!response)
548 {
549 enumerator = subject->create_ocsp_uri_enumerator(subject);
550 while (enumerator->enumerate(enumerator, &url))
551 {
552 valid = VALIDATION_FAILED;
553 response = fetch_ocsp(this, url, &subject->interface, &issuer->interface);
554 if (response)
555 {
556 break;
557 }
558 }
559 enumerator->destroy(enumerator);
560 }
561 /* look for subject in response */
562 if (response)
563 {
564 time_t revocation, this_update, next_update;
565 crl_reason_t reason;
566
567 valid = response->get_status(response, subject, issuer, &revocation,
568 &reason, &this_update, &next_update);
569 switch (valid)
570 {
571 case VALIDATION_FAILED:
572 DBG1(DBG_CFG, "subject not found in OCSP response");
573 break;
574 case VALIDATION_REVOKED:
575 DBG1(DBG_CFG, "certificate %D revoked by OCSP at %T: %N",
576 cert->get_subject(cert), &revocation,
577 crl_reason_names, reason);
578 break;
579 case VALIDATION_GOOD:
580 break;
581 default:
582 break;
583 }
584 cert = (certificate_t*)response;
585 cert->destroy(cert);
586 }
587 DESTROY_IF(public);
588 if (auth)
589 {
590 auth->add_item(auth, AUTHZ_OCSP_VALIDATION, &valid);
591 }
592 return valid;
593 }
594
595 /**
596 * fetch a CRL from an URL
597 */
598 static certificate_t* fetch_crl(private_credential_manager_t *this, char *url)
599 {
600 certificate_t *crl_cert;
601 chunk_t chunk;
602
603 /* TODO: unlock the manager while fetching? */
604 DBG1(DBG_CFG, "fetching crl from '%s' ...", url);
605 if (lib->fetcher->fetch(lib->fetcher, url, &chunk, FETCH_END) != SUCCESS)
606 {
607 DBG1(DBG_CFG, " crl fetching failed");
608 return NULL;
609 }
610 crl_cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509_CRL,
611 BUILD_BLOB_ASN1_DER, chunk, BUILD_END);
612 if (!crl_cert)
613 {
614 DBG1(DBG_CFG, " crl fetched successfully but parsing failed");
615 return NULL;
616 }
617
618 /* verify the signature of the fetched crl */
619 {
620 bool ok;
621 identification_t *issuer = crl_cert->get_issuer(crl_cert);
622 auth_info_t *auth = auth_info_create();
623 certificate_t *issuer_cert = get_trusted_cert(this, KEY_ANY, issuer,
624 auth, FALSE, FALSE);
625 auth->destroy(auth);
626
627 if (!issuer_cert)
628 {
629 DBG1(DBG_CFG, " crl is untrusted: issuer certificate not found");
630 crl_cert->destroy(crl_cert);
631 return NULL;
632 }
633 ok = crl_cert->issued_by(crl_cert, issuer_cert, TRUE);
634 issuer_cert->destroy(issuer_cert);
635
636 DBG1(DBG_CFG, " crl is %strusted: %s signature",
637 ok ? "":"un", ok ? "good" : "bad");
638 if (!ok)
639 {
640 crl_cert->destroy(crl_cert);
641 return NULL;
642 }
643 }
644 return crl_cert;
645 }
646
647 /**
648 * validate a x509 certificate using CRL
649 */
650 static cert_validation_t check_crl(private_credential_manager_t *this,
651 x509_t *subject, x509_t *issuer,
652 auth_info_t *auth)
653 {
654 identification_t *keyid = NULL;
655 certificate_t *best_cert = NULL;
656 cert_validation_t valid = VALIDATION_SKIPPED;
657 bool stale = TRUE;
658
659 /* derive the authorityKeyIdentifier from the issuer's public key */
660 {
661 certificate_t *cert = &issuer->interface;
662 public_key_t *public = cert->get_public_key(cert);
663
664 if (public)
665 {
666 keyid = public->get_id(public, ID_PUBKEY_SHA1);
667 public->destroy(public);
668 }
669 }
670
671 /* find a local crl by authorityKeyIdentifier */
672 if (keyid)
673 {
674 enumerator_t *enumerator = create_cert_enumerator(this, CERT_X509_CRL,
675 KEY_ANY, keyid, TRUE);
676 certificate_t *cert;
677
678 while (enumerator->enumerate(enumerator, &cert))
679 {
680 crl_t *crl = (crl_t*)cert;
681 crl_t *best_crl = (crl_t*)best_cert;
682
683 /* select most recent crl */
684 if (best_cert == NULL || crl->is_newer(crl, best_crl))
685 {
686 DESTROY_IF(best_cert);
687 best_cert = cert->get_ref(cert);
688 }
689 }
690 enumerator->destroy(enumerator);
691 }
692
693 /* check the validity of the local crl if one was found */
694 if (best_cert)
695 {
696 stale = !best_cert->get_validity(best_cert, NULL, NULL, NULL);
697 DBG1(DBG_CFG, "locally-stored crl is %s", stale? "stale":"valid");
698 }
699 else
700 {
701 DBG1(DBG_CFG, "no locally-stored crl found");
702 }
703
704 /* fallback to fetching crls from cdps defined in ca info sections */
705 if (stale && keyid)
706 {
707 enumerator_t *enumerator = create_cdp_enumerator(this, CERT_X509_CRL,
708 keyid);
709 char *uri;
710
711 while (enumerator->enumerate(enumerator, &uri))
712 {
713 certificate_t *cert = fetch_crl(this, uri);
714
715 /* redefine default since we have at least one uri */
716 valid = VALIDATION_FAILED;
717
718 if (cert)
719 {
720 crl_t *crl = (crl_t*)cert;
721 crl_t *best_crl = (crl_t*)best_cert;
722
723 /* select most recent crl */
724 if (best_cert == NULL || crl->is_newer(crl, best_crl))
725 {
726 DESTROY_IF(best_cert);
727 best_cert = cert;
728 stale = !best_cert->get_validity(best_cert, NULL, NULL, NULL);
729 DBG1(DBG_CFG, "fetched crl is %s", stale? "stale":"valid");
730 if (!stale)
731 {
732 break;
733 }
734 }
735 else
736 {
737 cert->destroy(cert);
738 }
739 }
740 }
741 enumerator->destroy(enumerator);
742 }
743
744 /* fallback to fetching crls from cdps defined in the subject's certificate */
745 if (stale)
746 {
747 enumerator_t *enumerator = subject->create_crl_uri_enumerator(subject);
748 char *uri;
749
750 while (enumerator->enumerate(enumerator, &uri))
751 {
752 certificate_t *cert = fetch_crl(this, uri);
753
754 /* redefine default since we have at least one uri */
755 valid = VALIDATION_FAILED;
756
757 if (cert)
758 {
759 crl_t *crl = (crl_t*)cert;
760 crl_t *best_crl = (crl_t*)best_cert;
761
762 /* select most recent crl */
763 if (best_cert == NULL || crl->is_newer(crl, best_crl))
764 {
765 DESTROY_IF(best_cert);
766 best_cert = cert;
767 stale = !best_cert->get_validity(best_cert, NULL, NULL, NULL);
768 DBG1(DBG_CFG, "fetched crl is %s", stale? "stale":"valid");
769 if (!stale)
770 {
771 break;
772 }
773 }
774 else
775 {
776 cert->destroy(cert);
777 }
778 }
779 }
780 enumerator->destroy(enumerator);
781 }
782
783 /* if we have a crl, check the revocation status */
784 if (best_cert)
785 {
786 chunk_t serial;
787 time_t revocation;
788 crl_reason_t reason;
789 crl_t *crl = (crl_t*)best_cert;
790 enumerator_t *enumerator = crl->create_enumerator(crl);
791
792 /* redefine default */
793 valid = stale ? VALIDATION_UNKNOWN : VALIDATION_GOOD;
794
795 while (enumerator->enumerate(enumerator, &serial, &revocation, &reason))
796 {
797 if (chunk_equals(serial, subject->get_serial(subject)))
798 {
799 DBG1(DBG_CFG, "certificate was revoked on %T, reason: %N",
800 &revocation, crl_reason_names, reason);
801 valid = VALIDATION_REVOKED;
802 break;
803 }
804 }
805 enumerator->destroy(enumerator);
806 best_cert->destroy(best_cert);
807 }
808
809 if (auth)
810 {
811 auth->add_item(auth, AUTHZ_CRL_VALIDATION, &valid);
812 }
813 return valid;
814 }
815
816 /**
817 * check a certificate for its lifetime
818 */
819 static bool check_certificate(private_credential_manager_t *this,
820 certificate_t *subject, certificate_t *issuer,
821 bool crl, bool ocsp, auth_info_t *auth)
822 {
823 time_t not_before, not_after;
824
825 if (!subject->get_validity(subject, NULL, &not_before, &not_after))
826 {
827 DBG1(DBG_CFG, "certificate invalid (valid from %T to %T)",
828 &not_before, &not_after);
829 return FALSE;
830 }
831 if (issuer && !subject->issued_by(subject, issuer, TRUE))
832 {
833 DBG1(DBG_CFG, "certificate %D not issued by %D",
834 subject->get_subject(subject), issuer->get_subject(issuer));
835 return FALSE;
836 }
837 if (issuer && issuer->get_type(issuer) == CERT_X509 &&
838 subject->get_type(subject) == CERT_X509)
839 {
840 if (ocsp)
841 {
842 switch (check_ocsp(this, (x509_t*)subject, (x509_t*)issuer, auth))
843 {
844 case VALIDATION_GOOD:
845 DBG1(DBG_CFG, "certificate %D validated by OCSP",
846 subject->get_subject(subject));
847 return TRUE;
848 case VALIDATION_REVOKED:
849 return FALSE;
850 case VALIDATION_SKIPPED:
851 DBG2(DBG_CFG, "OCSP check skipped, no OCSP URI found");
852 break;
853 case VALIDATION_FAILED:
854 DBG1(DBG_CFG, "OCSP check failed, fallback to CRL");
855 break;
856 }
857 }
858 if (crl)
859 {
860 switch (check_crl(this, (x509_t*)subject, (x509_t*)issuer, auth))
861 {
862 case VALIDATION_GOOD:
863 DBG1(DBG_CFG, "certificate status is good");
864 break;
865 case VALIDATION_REVOKED:
866 /* has already been logged */
867 break;
868 case VALIDATION_UNKNOWN:
869 DBG1(DBG_CFG, "certificate status is unknown");
870 break;
871 case VALIDATION_FAILED:
872 case VALIDATION_SKIPPED:
873 DBG1(DBG_CFG, "certificate status is not available");
874 break;
875 default:
876 break;
877 }
878 }
879 }
880 return TRUE;
881 }
882
883 /**
884 * credential_set_t implementation around a auth_info_t
885 */
886 typedef struct auth_wrapper_t {
887 credential_set_t set;
888 auth_info_t *auth;
889 } auth_wrapper_t;
890
891 /**
892 * enumerator for auth_wrapper_t.create_cert_enumerator()
893 */
894 typedef struct {
895 enumerator_t public;
896 enumerator_t *inner;
897 certificate_type_t cert;
898 key_type_t key;
899 identification_t *id;
900 } auth_wrapper_enumerator_t;
901
902 /**
903 * enumerate function for auth_wrapper_enumerator_t
904 */
905 static bool auth_wrapper_enum_enumerate(auth_wrapper_enumerator_t *this,
906 certificate_t **cert)
907 {
908 auth_item_t type;
909 certificate_t *current;
910 public_key_t *public;
911
912 while (this->inner->enumerate(this->inner, &type, &current))
913 {
914 if (type != AUTHN_SUBJECT_CERT &&
915 type != AUTHN_IM_CERT)
916 {
917 continue;
918 }
919
920 if (this->cert != CERT_ANY && this->cert != current->get_type(current))
921 { /* CERT type requested, but does not match */
922 continue;
923 }
924 public = current->get_public_key(current);
925 if (this->key != KEY_ANY && !public)
926 { /* key type requested, but no public key */
927 DESTROY_IF(public);
928 continue;
929 }
930 if (this->key != KEY_ANY && public && this->key != public->get_type(public))
931 { /* key type requested, but public key has another type */
932 DESTROY_IF(public);
933 continue;
934 }
935 DESTROY_IF(public);
936 if (this->id && !current->has_subject(current, this->id))
937 { /* subject requested, but does not match */
938 continue;
939 }
940 *cert = current;
941 return TRUE;
942 }
943 return FALSE;
944 }
945
946 /**
947 * destroy function for auth_wrapper_enumerator_t
948 */
949 static void auth_wrapper_enum_destroy(auth_wrapper_enumerator_t *this)
950 {
951 this->inner->destroy(this->inner);
952 free(this);
953 }
954
955 /**
956 * implementation of auth_wrapper_t.set.create_cert_enumerator
957 */
958 static enumerator_t *auth_wrapper_create_enumerator(auth_wrapper_t *this,
959 certificate_type_t cert, key_type_t key,
960 identification_t *id, bool trusted)
961 {
962 auth_wrapper_enumerator_t *enumerator;
963
964 if (trusted)
965 {
966 return NULL;
967 }
968
969 enumerator = malloc_thing(auth_wrapper_enumerator_t);
970 enumerator->cert = cert;
971 enumerator->key = key;
972 enumerator->id = id;
973 enumerator->inner = this->auth->create_item_enumerator(this->auth);
974 enumerator->public.enumerate = (void*)auth_wrapper_enum_enumerate;
975 enumerator->public.destroy = (void*)auth_wrapper_enum_destroy;
976 return &enumerator->public;
977 }
978
979 /**
980 * create credential_set wrapper around auth_info_t
981 */
982 static auth_wrapper_t *auth_wrapper_create(auth_info_t *auth)
983 {
984 auth_wrapper_t *this = malloc_thing(auth_wrapper_t);
985
986 this->auth = auth;
987 this->set.create_private_enumerator = (void*)return_null;
988 this->set.create_cert_enumerator = (void*)auth_wrapper_create_enumerator;
989 this->set.create_shared_enumerator = (void*)return_null;
990 this->set.create_cdp_enumerator = (void*)return_null;
991
992 return this;
993 }
994
995 /**
996 * Get a trusted certificate
997 */
998 static certificate_t *get_trusted_cert(private_credential_manager_t *this,
999 key_type_t type, identification_t *id,
1000 auth_info_t *auth, bool crl, bool ocsp)
1001 {
1002 enumerator_t *enumerator;
1003 auth_wrapper_t *wrapper;
1004 certificate_t *subject, *issuer, *candidate;
1005 public_key_t *public;
1006 bool trusted = FALSE;
1007 auth_info_t *auth1, *auth2;
1008 u_int level = 0;
1009
1010 this->mutex->lock(this->mutex);
1011 wrapper = auth_wrapper_create(auth);
1012 this->sets->insert_first(this->sets, wrapper);
1013
1014 /* check if we have a trusted certificate for that peer */
1015 auth1 = auth_info_create();
1016 subject = get_cert(this, CERT_ANY, type, id, TRUE);
1017 if (subject)
1018 {
1019 if (check_certificate(this, subject, NULL, crl, ocsp, auth1))
1020 {
1021 public = subject->get_public_key(subject);
1022 if (public)
1023 {
1024 DBG2(DBG_CFG, "using trusted certificate %D",
1025 subject->get_subject(subject));
1026 this->sets->remove(this->sets, wrapper, NULL);
1027 free(wrapper);
1028 this->mutex->unlock(this->mutex);
1029 auth->add_item(auth1, AUTHZ_SUBJECT_CERT, subject);
1030 public->destroy(public);
1031 auth->merge(auth, auth1);
1032 auth1->destroy(auth1);
1033 return subject;
1034 }
1035 }
1036 subject->destroy(subject);
1037 }
1038 auth1->destroy(auth1);
1039
1040 /* check for an untrusted certificate */
1041 auth1 = auth_info_create();
1042 subject = get_cert(this, CERT_ANY, type, id, FALSE);
1043 if (!subject)
1044 {
1045 DBG1(DBG_CFG, "no end entity certificate found for %D", id);
1046 }
1047 else
1048 {
1049 issuer = subject;
1050 do
1051 {
1052 /* look for a trusted certificate */
1053 auth2 = auth_info_create();
1054 enumerator = create_cert_enumerator(this, issuer->get_type(issuer),
1055 KEY_ANY, issuer->get_issuer(issuer), TRUE);
1056 while (enumerator->enumerate(enumerator, &candidate))
1057 {
1058 if (check_certificate(this, issuer, candidate, crl, ocsp,
1059 issuer == subject ? auth2 : NULL) &&
1060 check_certificate(this, candidate, NULL, crl, ocsp, NULL))
1061 {
1062 DBG2(DBG_CFG, "using trusted root CA certificate %D",
1063 candidate->get_subject(candidate));
1064 issuer = candidate;
1065 trusted = TRUE;
1066 auth1->merge(auth1, auth2);
1067 auth1->add_item(auth1, AUTHZ_CA_CERT, candidate);
1068 break;
1069 }
1070 }
1071 enumerator->destroy(enumerator);
1072 auth2->destroy(auth2);
1073 if (trusted)
1074 {
1075 break;
1076 }
1077
1078 /* no trusted certificate found, look for an untrusted */
1079 enumerator = create_cert_enumerator(this, issuer->get_type(issuer),
1080 KEY_ANY, issuer->get_issuer(issuer), FALSE);
1081 while (enumerator->enumerate(enumerator, &candidate))
1082 {
1083 auth2 = auth_info_create();
1084 if (check_certificate(this, issuer, candidate, crl, ocsp,
1085 issuer == subject ? auth2 : NULL))
1086 {
1087 if (issuer != subject)
1088 {
1089 DBG2(DBG_CFG, "using intermediate CA certificate %D",
1090 candidate->get_subject(candidate));
1091 auth1->add_item(auth1, AUTHZ_IM_CERT, candidate);
1092 }
1093 else
1094 {
1095 DBG2(DBG_CFG, "using end entity certificate %D",
1096 candidate->get_subject(candidate));
1097 }
1098 issuer = candidate;
1099 auth1->merge(auth1, auth2);
1100 auth2->destroy(auth2);
1101 /* check next level */
1102 break;
1103 }
1104 auth2->destroy(auth2);
1105 }
1106 enumerator->destroy(enumerator);
1107 }
1108 while (++level < MAX_CA_LEVELS);
1109
1110 if (!trusted)
1111 {
1112 subject->destroy(subject);
1113 subject = NULL;
1114 }
1115 }
1116 this->sets->remove(this->sets, wrapper, NULL);
1117 free(wrapper);
1118 this->mutex->unlock(this->mutex);
1119 if (subject)
1120 {
1121 auth->add_item(auth, AUTHZ_SUBJECT_CERT, subject);
1122 auth->merge(auth, auth1);
1123 auth1->destroy(auth1);
1124 return subject;
1125 }
1126 auth1->destroy(auth1);
1127 return NULL;
1128 }
1129
1130 /**
1131 * Implementation of credential_manager_t.get_public.
1132 */
1133 static public_key_t *get_public(private_credential_manager_t *this,
1134 key_type_t type, identification_t *id,
1135 auth_info_t *auth)
1136 {
1137 public_key_t *public;
1138 certificate_t *cert;
1139
1140 cert = get_trusted_cert(this, type, id, auth, TRUE, TRUE);
1141 if (cert)
1142 {
1143 public = cert->get_public_key(cert);
1144 cert->destroy(cert);
1145 return public;
1146 }
1147 return NULL;
1148 }
1149
1150 /**
1151 * Get the issuing certificate of a subject certificate
1152 */
1153 static certificate_t *get_issuer_cert(private_credential_manager_t *this,
1154 certificate_t *subject)
1155 {
1156 enumerator_t *enumerator;
1157 certificate_t *issuer = NULL, *candidate;
1158
1159 enumerator = create_cert_enumerator(this, subject->get_type(subject), KEY_ANY,
1160 subject->get_issuer(subject), FALSE);
1161 while (enumerator->enumerate(enumerator, &candidate))
1162 {
1163 if (subject->issued_by(subject, candidate, FALSE))
1164 {
1165 issuer = candidate->get_ref(candidate);
1166 break;
1167 }
1168 }
1169 enumerator->destroy(enumerator);
1170 return issuer;
1171 }
1172
1173 /**
1174 * Check if a certificate's keyid is contained in the auth helper
1175 */
1176 static bool auth_contains_cacert(auth_info_t *auth, certificate_t *cert)
1177 {
1178 enumerator_t *enumerator;
1179 identification_t *value;
1180 auth_item_t type;
1181 bool found = FALSE;
1182
1183 enumerator = auth->create_item_enumerator(auth);
1184 while (enumerator->enumerate(enumerator, &type, &value))
1185 {
1186 if (type == AUTHN_CA_CERT && cert->equals(cert, (certificate_t*)value))
1187 {
1188 found = TRUE;
1189 break;
1190 }
1191 }
1192 enumerator->destroy(enumerator);
1193 return found;
1194 }
1195
1196 /**
1197 * build a trustchain from subject up to a trust anchor in trusted
1198 */
1199 static auth_info_t *build_trustchain(private_credential_manager_t *this,
1200 certificate_t *subject, auth_info_t *auth)
1201 {
1202 certificate_t *issuer, *current;
1203 auth_info_t *trustchain;
1204 u_int level = 0;
1205
1206 trustchain = auth_info_create();
1207
1208 if (!auth->get_item(auth, AUTHN_CA_CERT, (void**)&current))
1209 {
1210 /* no trust anchor specified, return this cert only */
1211 trustchain->add_item(trustchain, AUTHZ_SUBJECT_CERT, subject);
1212 return trustchain;
1213 }
1214
1215 current = subject->get_ref(subject);
1216 while (TRUE)
1217 {
1218 if (auth_contains_cacert(auth, current))
1219 {
1220 trustchain->add_item(trustchain, AUTHZ_CA_CERT, current);
1221 current->destroy(current);
1222 return trustchain;
1223 }
1224 if (subject == current)
1225 {
1226 trustchain->add_item(trustchain, AUTHZ_SUBJECT_CERT, current);
1227 }
1228 else
1229 {
1230 trustchain->add_item(trustchain, AUTHZ_IM_CERT, current);
1231 }
1232 issuer = get_issuer_cert(this, current);
1233 if (!issuer || issuer->equals(issuer, current) || level > MAX_CA_LEVELS)
1234 {
1235 DESTROY_IF(issuer);
1236 current->destroy(current);
1237 break;
1238 }
1239 current->destroy(current);
1240 current = issuer;
1241 level++;
1242 }
1243 trustchain->destroy(trustchain);
1244 return NULL;
1245 }
1246
1247 /**
1248 * find a private key of a give certificate
1249 */
1250 static private_key_t *get_private_by_cert(private_credential_manager_t *this,
1251 certificate_t *cert, key_type_t type)
1252 {
1253 private_key_t *private = NULL;
1254 identification_t* keyid;
1255 public_key_t *public;
1256
1257 public = cert->get_public_key(cert);
1258 if (public)
1259 {
1260 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
1261 if (keyid)
1262 {
1263 private = get_private_by_keyid(this, type, keyid);
1264 }
1265 public->destroy(public);
1266 }
1267 return private;
1268 }
1269
1270 /**
1271 * Implementation of credential_manager_t.get_private.
1272 */
1273 static private_key_t *get_private(private_credential_manager_t *this,
1274 key_type_t type, identification_t *id,
1275 auth_info_t *auth)
1276 {
1277 enumerator_t *enumerator;
1278 certificate_t *cert;
1279 private_key_t *private = NULL;
1280 auth_info_t *trustchain;
1281
1282 /* check if this is a lookup by key ID, and do it if so */
1283 if (id)
1284 {
1285 switch (id->get_type(id))
1286 {
1287 case ID_PUBKEY_SHA1:
1288 case ID_PUBKEY_INFO_SHA1:
1289 return get_private_by_keyid(this, type, id);
1290 default:
1291 break;
1292 }
1293 }
1294
1295 this->mutex->lock(this->mutex);
1296 /* get all available end entity certificates for ourself */
1297 enumerator = create_cert_enumerator(this, CERT_ANY, type, id, FALSE);
1298 while (enumerator->enumerate(enumerator, &cert))
1299 {
1300 private = get_private_by_cert(this, cert, type);
1301 if (private)
1302 {
1303 trustchain = build_trustchain(this, cert, auth);
1304 if (trustchain)
1305 {
1306 auth->merge(auth, trustchain);
1307 trustchain->destroy(trustchain);
1308 break;
1309 }
1310 private->destroy(private);
1311 }
1312 }
1313 enumerator->destroy(enumerator);
1314 this->mutex->unlock(this->mutex);
1315 return private;
1316 }
1317
1318 /**
1319 * Implementation of credential_manager_t.add_set.
1320 */
1321 static void add_set(private_credential_manager_t *this,
1322 credential_set_t *set)
1323 {
1324 this->mutex->lock(this->mutex);
1325 this->sets->insert_last(this->sets, set);
1326 this->mutex->unlock(this->mutex);
1327 }
1328 /**
1329 * Implementation of credential_manager_t.remove_set.
1330 */
1331 static void remove_set(private_credential_manager_t *this, credential_set_t *set)
1332 {
1333 this->mutex->lock(this->mutex);
1334 this->sets->remove(this->sets, set, NULL);
1335 this->mutex->unlock(this->mutex);
1336 }
1337
1338 /**
1339 * Implementation of credential_manager_t.destroy
1340 */
1341 static void destroy(private_credential_manager_t *this)
1342 {
1343 this->sets->destroy(this->sets);
1344 this->mutex->destroy(this->mutex);
1345 free(this);
1346 }
1347
1348 /*
1349 * see header file
1350 */
1351 credential_manager_t *credential_manager_create()
1352 {
1353 private_credential_manager_t *this = malloc_thing(private_credential_manager_t);
1354
1355 this->public.create_cert_enumerator = (enumerator_t *(*)(credential_manager_t *this,certificate_type_t cert, key_type_t key,identification_t *id,bool))create_cert_enumerator;
1356 this->public.create_shared_enumerator = (enumerator_t *(*)(credential_manager_t *this, shared_key_type_t type,identification_t *me, identification_t *other))create_shared_enumerator;
1357 this->public.create_cdp_enumerator = (enumerator_t *(*)(credential_manager_t*, credential_type_t type, identification_t *id))create_cdp_enumerator;
1358 this->public.get_cert = (certificate_t *(*)(credential_manager_t *this,certificate_type_t cert, key_type_t key,identification_t *, bool))get_cert;
1359 this->public.get_shared = (shared_key_t *(*)(credential_manager_t *this,shared_key_type_t type,identification_t *me, identification_t *other))get_shared;
1360 this->public.get_private = (private_key_t*(*)(credential_manager_t*, key_type_t type, identification_t *, auth_info_t*))get_private;
1361 this->public.get_public = (public_key_t*(*)(credential_manager_t*, key_type_t type, identification_t *, auth_info_t*))get_public;
1362 this->public.add_set = (void(*)(credential_manager_t*, credential_set_t *set))add_set;
1363 this->public.remove_set = (void(*)(credential_manager_t*, credential_set_t *set))remove_set;
1364 this->public.destroy = (void(*)(credential_manager_t*))destroy;
1365
1366 this->sets = linked_list_create();
1367 this->mutex = mutex_create(MUTEX_RECURSIVE);
1368
1369 return &this->public;
1370 }
1371