fixed compiler warning
[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 certificate_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 failed");
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, " ocsp response untrusted: no signer certificate found");
470 response->destroy(response);
471 return NULL;
472 }
473 if (!response->issued_by(response, issuer_cert, TRUE))
474 {
475 DBG1(DBG_CFG, " ocsp response untrusted: bad signature");
476 response->destroy(response);
477 issuer_cert->destroy(issuer_cert);
478 return NULL;
479 }
480 issuer_cert->destroy(issuer_cert);
481
482 /* TODO: cache response? */
483 return response;
484 }
485
486 /**
487 * validate a x509 certificate using OCSP
488 */
489 static cert_validation_t check_ocsp(private_credential_manager_t *this,
490 x509_t *subject, x509_t *issuer,
491 auth_info_t *auth)
492 {
493 certificate_t *sub = (certificate_t*)subject;
494 certificate_t *best_cert = NULL;
495 cert_validation_t valid = VALIDATION_SKIPPED;
496 identification_t *keyid = NULL;
497 bool stale = TRUE;
498
499 /* derive the authorityKeyIdentifier from the issuer's public key */
500 {
501 certificate_t *cert = &issuer->interface;
502 public_key_t *public = cert->get_public_key(cert);
503
504 if (public)
505 {
506 keyid = public->get_id(public, ID_PUBKEY_SHA1);
507 public->destroy(public);
508 }
509 }
510
511 /* find a cached ocsp response by authorityKeyIdentifier */
512 if (keyid)
513 {
514 enumerator_t *enumerator = create_cert_enumerator(this,
515 CERT_X509_OCSP_RESPONSE,
516 KEY_ANY, keyid, TRUE);
517 certificate_t *cert;
518
519 while (enumerator->enumerate(enumerator, &cert))
520 {
521 if (cert->has_subject(cert, sub->get_subject(sub)))
522 {
523 /* select most recent ocsp response */
524 if (best_cert == NULL || cert->is_newer(cert, best_cert))
525 {
526 DESTROY_IF(best_cert);
527 best_cert = cert->get_ref(cert);
528 }
529 }
530 }
531 enumerator->destroy(enumerator);
532 }
533
534 /* check the validity of the cached ocsp response if one was found */
535 if (best_cert)
536 {
537 stale = !best_cert->get_validity(best_cert, NULL, NULL, NULL);
538 DBG1(DBG_CFG, "cached ocsp response is %s", stale? "stale":"valid");
539 }
540
541 /* fallback to URL fetching from CDPs */
542 if (stale && keyid)
543 {
544 enumerator_t *enumerator = create_cdp_enumerator(this,
545 CERT_X509_OCSP_RESPONSE, keyid);
546 char *uri;
547
548 while (enumerator->enumerate(enumerator, &uri))
549 {
550 certificate_t* cert = fetch_ocsp(this, uri, &subject->interface,
551 &issuer->interface);
552
553 /* redefine default since we have at least one uri */
554 valid = VALIDATION_FAILED;
555
556 if (cert)
557 {
558 /* select most recent ocsp response until valid one is found */
559 if (best_cert == NULL || cert->is_newer(cert, best_cert))
560 {
561 DESTROY_IF(best_cert);
562 best_cert = cert;
563 stale = !best_cert->get_validity(best_cert, NULL, NULL, NULL);
564 DBG1(DBG_CFG, "ocsp response is %s", stale? "stale":"valid");
565 if (!stale)
566 {
567 break;
568 }
569 }
570 else
571 {
572 cert->destroy(cert);
573 }
574 }
575 }
576 enumerator->destroy(enumerator);
577 }
578
579 /* fallback to URL fetching from subject certificate's URIs */
580 if (stale)
581 {
582 enumerator_t *enumerator = subject->create_ocsp_uri_enumerator(subject);
583 char *uri;
584
585 while (enumerator->enumerate(enumerator, &uri))
586 {
587 certificate_t* cert = fetch_ocsp(this, uri, &subject->interface,
588 &issuer->interface);
589
590 /* redefine default since we have at least one uri */
591 valid = VALIDATION_FAILED;
592
593 if (cert)
594 {
595 /* select most recent ocsp response until valid one is found */
596 if (best_cert == NULL || cert->is_newer(cert, best_cert))
597 {
598 DESTROY_IF(best_cert);
599 best_cert = cert;
600 stale = !best_cert->get_validity(best_cert, NULL, NULL, NULL);
601 DBG1(DBG_CFG, "ocsp response is %s", stale? "stale":"valid");
602 if (!stale)
603 {
604 break;
605 }
606 }
607 else
608 {
609 cert->destroy(cert);
610 }
611 }
612 }
613 enumerator->destroy(enumerator);
614 }
615
616 /* if we have an ocsp response, check the revocation status */
617 if (best_cert)
618 {
619 time_t revocation, this_update, next_update;
620 crl_reason_t reason;
621 ocsp_response_t *response = (ocsp_response_t*)best_cert;
622
623 valid = response->get_status(response, subject, issuer, &revocation,
624 &reason, &this_update, &next_update);
625 switch (valid)
626 {
627 case VALIDATION_FAILED:
628 DBG1(DBG_CFG, "subject not found in ocsp response");
629 break;
630 case VALIDATION_REVOKED:
631 DBG1(DBG_CFG, "certificate was revoked on %T, reason: %N",
632 &revocation, crl_reason_names, reason);
633 break;
634 case VALIDATION_GOOD:
635 case VALIDATION_UNKNOWN:
636 default:
637 break;
638 }
639 best_cert->destroy(best_cert);
640 }
641
642 if (auth)
643 {
644 auth->add_item(auth, AUTHZ_OCSP_VALIDATION, &valid);
645 }
646 return valid;
647 }
648
649 /**
650 * fetch a CRL from an URL
651 */
652 static certificate_t* fetch_crl(private_credential_manager_t *this, char *url)
653 {
654 certificate_t *crl_cert;
655 chunk_t chunk;
656
657 /* TODO: unlock the manager while fetching? */
658 DBG1(DBG_CFG, "fetching crl from '%s' ...", url);
659 if (lib->fetcher->fetch(lib->fetcher, url, &chunk, FETCH_END) != SUCCESS)
660 {
661 DBG1(DBG_CFG, " crl fetching failed");
662 return NULL;
663 }
664 crl_cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509_CRL,
665 BUILD_BLOB_ASN1_DER, chunk, BUILD_END);
666 if (!crl_cert)
667 {
668 DBG1(DBG_CFG, " crl fetched successfully but parsing failed");
669 return NULL;
670 }
671
672 /* verify the signature of the fetched crl */
673 {
674 bool ok;
675 identification_t *issuer = crl_cert->get_issuer(crl_cert);
676 auth_info_t *auth = auth_info_create();
677 certificate_t *issuer_cert = get_trusted_cert(this, KEY_ANY, issuer,
678 auth, FALSE, FALSE);
679 auth->destroy(auth);
680
681 if (!issuer_cert)
682 {
683 DBG1(DBG_CFG, " crl is untrusted: issuer certificate not found");
684 crl_cert->destroy(crl_cert);
685 return NULL;
686 }
687 ok = crl_cert->issued_by(crl_cert, issuer_cert, TRUE);
688 issuer_cert->destroy(issuer_cert);
689
690 DBG1(DBG_CFG, " crl is %strusted: %s signature",
691 ok ? "":"un", ok ? "good" : "bad");
692 if (!ok)
693 {
694 crl_cert->destroy(crl_cert);
695 return NULL;
696 }
697 }
698 return crl_cert;
699 }
700
701 /**
702 * validate a x509 certificate using CRL
703 */
704 static cert_validation_t check_crl(private_credential_manager_t *this,
705 x509_t *subject, x509_t *issuer,
706 auth_info_t *auth)
707 {
708 identification_t *keyid = NULL;
709 certificate_t *best_cert = NULL;
710 cert_validation_t valid = VALIDATION_SKIPPED;
711 bool stale = TRUE;
712
713 /* derive the authorityKeyIdentifier from the issuer's public key */
714 {
715 certificate_t *cert = &issuer->interface;
716 public_key_t *public = cert->get_public_key(cert);
717
718 if (public)
719 {
720 keyid = public->get_id(public, ID_PUBKEY_SHA1);
721 public->destroy(public);
722 }
723 }
724
725 /* find a cached crl by authorityKeyIdentifier */
726 if (keyid)
727 {
728 enumerator_t *enumerator = create_cert_enumerator(this, CERT_X509_CRL,
729 KEY_ANY, keyid, TRUE);
730 certificate_t *cert;
731
732 while (enumerator->enumerate(enumerator, &cert))
733 {
734 /* select most recent crl */
735 if (best_cert == NULL || cert->is_newer(cert, best_cert))
736 {
737 DESTROY_IF(best_cert);
738 best_cert = cert->get_ref(cert);
739 }
740 }
741 enumerator->destroy(enumerator);
742 }
743
744 /* check the validity of the cached crl if one was found */
745 if (best_cert)
746 {
747 stale = !best_cert->get_validity(best_cert, NULL, NULL, NULL);
748 DBG1(DBG_CFG, "cached crl is %s", stale? "stale":"valid");
749 }
750
751 /* fallback to fetching crls from cdps defined in ca info sections */
752 if (stale && keyid)
753 {
754 enumerator_t *enumerator = create_cdp_enumerator(this, CERT_X509_CRL,
755 keyid);
756 char *uri;
757
758 while (enumerator->enumerate(enumerator, &uri))
759 {
760 certificate_t *cert = fetch_crl(this, uri);
761
762 /* redefine default since we have at least one uri */
763 valid = VALIDATION_FAILED;
764
765 if (cert)
766 {
767 /* select most recent crl until valid one is found */
768 if (best_cert == NULL || cert->is_newer(cert, best_cert))
769 {
770 DESTROY_IF(best_cert);
771 best_cert = cert;
772 stale = !best_cert->get_validity(best_cert, NULL, NULL, NULL);
773 DBG1(DBG_CFG, "fetched crl is %s", stale? "stale":"valid");
774 if (!stale)
775 {
776 break;
777 }
778 }
779 else
780 {
781 cert->destroy(cert);
782 }
783 }
784 }
785 enumerator->destroy(enumerator);
786 }
787
788 /* fallback to fetching crls from cdps defined in the subject's certificate */
789 if (stale)
790 {
791 enumerator_t *enumerator = subject->create_crl_uri_enumerator(subject);
792 char *uri;
793
794 while (enumerator->enumerate(enumerator, &uri))
795 {
796 certificate_t *cert = fetch_crl(this, uri);
797
798 /* redefine default since we have at least one uri */
799 valid = VALIDATION_FAILED;
800
801 if (cert)
802 {
803 /* select most recent crl until valid one is found */
804 if (best_cert == NULL || cert->is_newer(cert, best_cert))
805 {
806 DESTROY_IF(best_cert);
807 best_cert = cert;
808 stale = !best_cert->get_validity(best_cert, NULL, NULL, NULL);
809 DBG1(DBG_CFG, "fetched crl is %s", stale? "stale":"valid");
810 if (!stale)
811 {
812 break;
813 }
814 }
815 else
816 {
817 cert->destroy(cert);
818 }
819 }
820 }
821 enumerator->destroy(enumerator);
822 }
823
824 /* if we have a crl, check the revocation status */
825 if (best_cert)
826 {
827 chunk_t subject_serial = subject->get_serial(subject);
828 chunk_t serial;
829 time_t revocation;
830 crl_reason_t reason;
831 crl_t *crl = (crl_t*)best_cert;
832 enumerator_t *enumerator = crl->create_enumerator(crl);
833
834 /* redefine default */
835 valid = stale ? VALIDATION_UNKNOWN : VALIDATION_GOOD;
836
837 while (enumerator->enumerate(enumerator, &serial, &revocation, &reason))
838 {
839 if (chunk_equals(serial, subject_serial))
840 {
841 DBG1(DBG_CFG, "certificate was revoked on %T, reason: %N",
842 &revocation, crl_reason_names, reason);
843 valid = VALIDATION_REVOKED;
844 break;
845 }
846 }
847 enumerator->destroy(enumerator);
848 best_cert->destroy(best_cert);
849 }
850
851 if (auth)
852 {
853 auth->add_item(auth, AUTHZ_CRL_VALIDATION, &valid);
854 }
855 return valid;
856 }
857
858 /**
859 * check a certificate for its lifetime
860 */
861 static bool check_certificate(private_credential_manager_t *this,
862 certificate_t *subject, certificate_t *issuer,
863 bool crl, bool ocsp, auth_info_t *auth)
864 {
865 time_t not_before, not_after;
866
867 if (!subject->get_validity(subject, NULL, &not_before, &not_after))
868 {
869 DBG1(DBG_CFG, "certificate invalid (valid from %T to %T)",
870 &not_before, &not_after);
871 return FALSE;
872 }
873 if (issuer && !subject->issued_by(subject, issuer, TRUE))
874 {
875 DBG1(DBG_CFG, "certificate %D not issued by %D",
876 subject->get_subject(subject), issuer->get_subject(issuer));
877 return FALSE;
878 }
879 if (issuer && issuer->get_type(issuer) == CERT_X509 &&
880 subject->get_type(subject) == CERT_X509)
881 {
882 if (ocsp)
883 {
884 switch (check_ocsp(this, (x509_t*)subject, (x509_t*)issuer, auth))
885 {
886 case VALIDATION_GOOD:
887 DBG1(DBG_CFG, "certificate status is good",
888 subject->get_subject(subject));
889 return TRUE;
890 case VALIDATION_REVOKED:
891 /* has already been logged */
892 return FALSE;
893 case VALIDATION_SKIPPED:
894 DBG2(DBG_CFG, "OCSP check skipped, no OCSP URI found");
895 break;
896 case VALIDATION_FAILED:
897 case VALIDATION_UNKNOWN:
898 DBG1(DBG_CFG, "OCSP check failed, fallback to CRL");
899 break;
900 }
901 }
902 if (crl)
903 {
904 switch (check_crl(this, (x509_t*)subject, (x509_t*)issuer, auth))
905 {
906 case VALIDATION_GOOD:
907 DBG1(DBG_CFG, "certificate status is good");
908 break;
909 case VALIDATION_REVOKED:
910 /* has already been logged */
911 break;
912 case VALIDATION_UNKNOWN:
913 DBG1(DBG_CFG, "certificate status is unknown");
914 break;
915 case VALIDATION_FAILED:
916 case VALIDATION_SKIPPED:
917 DBG1(DBG_CFG, "certificate status is not available");
918 break;
919 default:
920 break;
921 }
922 }
923 }
924 return TRUE;
925 }
926
927 /**
928 * credential_set_t implementation around a auth_info_t
929 */
930 typedef struct auth_wrapper_t {
931 credential_set_t set;
932 auth_info_t *auth;
933 } auth_wrapper_t;
934
935 /**
936 * enumerator for auth_wrapper_t.create_cert_enumerator()
937 */
938 typedef struct {
939 enumerator_t public;
940 enumerator_t *inner;
941 certificate_type_t cert;
942 key_type_t key;
943 identification_t *id;
944 } auth_wrapper_enumerator_t;
945
946 /**
947 * enumerate function for auth_wrapper_enumerator_t
948 */
949 static bool auth_wrapper_enum_enumerate(auth_wrapper_enumerator_t *this,
950 certificate_t **cert)
951 {
952 auth_item_t type;
953 certificate_t *current;
954 public_key_t *public;
955
956 while (this->inner->enumerate(this->inner, &type, &current))
957 {
958 if (type != AUTHN_SUBJECT_CERT &&
959 type != AUTHN_IM_CERT)
960 {
961 continue;
962 }
963
964 if (this->cert != CERT_ANY && this->cert != current->get_type(current))
965 { /* CERT type requested, but does not match */
966 continue;
967 }
968 public = current->get_public_key(current);
969 if (this->key != KEY_ANY && !public)
970 { /* key type requested, but no public key */
971 DESTROY_IF(public);
972 continue;
973 }
974 if (this->key != KEY_ANY && public && this->key != public->get_type(public))
975 { /* key type requested, but public key has another type */
976 DESTROY_IF(public);
977 continue;
978 }
979 DESTROY_IF(public);
980 if (this->id && !current->has_subject(current, this->id))
981 { /* subject requested, but does not match */
982 continue;
983 }
984 *cert = current;
985 return TRUE;
986 }
987 return FALSE;
988 }
989
990 /**
991 * destroy function for auth_wrapper_enumerator_t
992 */
993 static void auth_wrapper_enum_destroy(auth_wrapper_enumerator_t *this)
994 {
995 this->inner->destroy(this->inner);
996 free(this);
997 }
998
999 /**
1000 * implementation of auth_wrapper_t.set.create_cert_enumerator
1001 */
1002 static enumerator_t *auth_wrapper_create_enumerator(auth_wrapper_t *this,
1003 certificate_type_t cert, key_type_t key,
1004 identification_t *id, bool trusted)
1005 {
1006 auth_wrapper_enumerator_t *enumerator;
1007
1008 if (trusted)
1009 {
1010 return NULL;
1011 }
1012
1013 enumerator = malloc_thing(auth_wrapper_enumerator_t);
1014 enumerator->cert = cert;
1015 enumerator->key = key;
1016 enumerator->id = id;
1017 enumerator->inner = this->auth->create_item_enumerator(this->auth);
1018 enumerator->public.enumerate = (void*)auth_wrapper_enum_enumerate;
1019 enumerator->public.destroy = (void*)auth_wrapper_enum_destroy;
1020 return &enumerator->public;
1021 }
1022
1023 /**
1024 * create credential_set wrapper around auth_info_t
1025 */
1026 static auth_wrapper_t *auth_wrapper_create(auth_info_t *auth)
1027 {
1028 auth_wrapper_t *this = malloc_thing(auth_wrapper_t);
1029
1030 this->auth = auth;
1031 this->set.create_private_enumerator = (void*)return_null;
1032 this->set.create_cert_enumerator = (void*)auth_wrapper_create_enumerator;
1033 this->set.create_shared_enumerator = (void*)return_null;
1034 this->set.create_cdp_enumerator = (void*)return_null;
1035
1036 return this;
1037 }
1038
1039 /**
1040 * Get a trusted certificate
1041 */
1042 static certificate_t *get_trusted_cert(private_credential_manager_t *this,
1043 key_type_t type, identification_t *id,
1044 auth_info_t *auth, bool crl, bool ocsp)
1045 {
1046 enumerator_t *enumerator;
1047 auth_wrapper_t *wrapper;
1048 certificate_t *subject, *issuer, *candidate;
1049 public_key_t *public;
1050 bool trusted = FALSE;
1051 auth_info_t *auth1, *auth2;
1052 u_int level = 0;
1053
1054 this->mutex->lock(this->mutex);
1055 wrapper = auth_wrapper_create(auth);
1056 this->sets->insert_first(this->sets, wrapper);
1057
1058 /* check if we have a trusted certificate for that peer */
1059 auth1 = auth_info_create();
1060 subject = get_cert(this, CERT_ANY, type, id, TRUE);
1061 if (subject)
1062 {
1063 if (check_certificate(this, subject, NULL, crl, ocsp, auth1))
1064 {
1065 public = subject->get_public_key(subject);
1066 if (public)
1067 {
1068 DBG2(DBG_CFG, "using trusted certificate %D",
1069 subject->get_subject(subject));
1070 this->sets->remove(this->sets, wrapper, NULL);
1071 free(wrapper);
1072 this->mutex->unlock(this->mutex);
1073 auth->add_item(auth1, AUTHZ_SUBJECT_CERT, subject);
1074 public->destroy(public);
1075 auth->merge(auth, auth1);
1076 auth1->destroy(auth1);
1077 return subject;
1078 }
1079 }
1080 subject->destroy(subject);
1081 }
1082 auth1->destroy(auth1);
1083
1084 /* check for an untrusted certificate */
1085 auth1 = auth_info_create();
1086 subject = get_cert(this, CERT_ANY, type, id, FALSE);
1087 if (!subject)
1088 {
1089 DBG1(DBG_CFG, "no end entity certificate found for %D", id);
1090 }
1091 else
1092 {
1093 issuer = subject;
1094 do
1095 {
1096 /* look for a trusted certificate */
1097 auth2 = auth_info_create();
1098 enumerator = create_cert_enumerator(this, issuer->get_type(issuer),
1099 KEY_ANY, issuer->get_issuer(issuer), TRUE);
1100 while (enumerator->enumerate(enumerator, &candidate))
1101 {
1102 if (check_certificate(this, issuer, candidate, crl, ocsp,
1103 issuer == subject ? auth2 : NULL) &&
1104 check_certificate(this, candidate, NULL, crl, ocsp, NULL))
1105 {
1106 DBG2(DBG_CFG, "using trusted root CA certificate %D",
1107 candidate->get_subject(candidate));
1108 issuer = candidate;
1109 trusted = TRUE;
1110 auth1->merge(auth1, auth2);
1111 auth1->add_item(auth1, AUTHZ_CA_CERT, candidate);
1112 break;
1113 }
1114 }
1115 enumerator->destroy(enumerator);
1116 auth2->destroy(auth2);
1117 if (trusted)
1118 {
1119 break;
1120 }
1121
1122 /* no trusted certificate found, look for an untrusted */
1123 enumerator = create_cert_enumerator(this, issuer->get_type(issuer),
1124 KEY_ANY, issuer->get_issuer(issuer), FALSE);
1125 while (enumerator->enumerate(enumerator, &candidate))
1126 {
1127 auth2 = auth_info_create();
1128 if (check_certificate(this, issuer, candidate, crl, ocsp,
1129 issuer == subject ? auth2 : NULL))
1130 {
1131 if (issuer != subject)
1132 {
1133 DBG2(DBG_CFG, "using intermediate CA certificate %D",
1134 candidate->get_subject(candidate));
1135 auth1->add_item(auth1, AUTHZ_IM_CERT, candidate);
1136 }
1137 else
1138 {
1139 DBG2(DBG_CFG, "using end entity certificate %D",
1140 candidate->get_subject(candidate));
1141 }
1142 issuer = candidate;
1143 auth1->merge(auth1, auth2);
1144 auth2->destroy(auth2);
1145 /* check next level */
1146 break;
1147 }
1148 auth2->destroy(auth2);
1149 }
1150 enumerator->destroy(enumerator);
1151 }
1152 while (++level < MAX_CA_LEVELS);
1153
1154 if (!trusted)
1155 {
1156 subject->destroy(subject);
1157 subject = NULL;
1158 }
1159 }
1160 this->sets->remove(this->sets, wrapper, NULL);
1161 free(wrapper);
1162 this->mutex->unlock(this->mutex);
1163 if (subject)
1164 {
1165 auth->add_item(auth, AUTHZ_SUBJECT_CERT, subject);
1166 auth->merge(auth, auth1);
1167 auth1->destroy(auth1);
1168 return subject;
1169 }
1170 auth1->destroy(auth1);
1171 return NULL;
1172 }
1173
1174 /**
1175 * Implementation of credential_manager_t.get_public.
1176 */
1177 static public_key_t *get_public(private_credential_manager_t *this,
1178 key_type_t type, identification_t *id,
1179 auth_info_t *auth)
1180 {
1181 public_key_t *public;
1182 certificate_t *cert;
1183
1184 cert = get_trusted_cert(this, type, id, auth, TRUE, TRUE);
1185 if (cert)
1186 {
1187 public = cert->get_public_key(cert);
1188 cert->destroy(cert);
1189 return public;
1190 }
1191 return NULL;
1192 }
1193
1194 /**
1195 * Get the issuing certificate of a subject certificate
1196 */
1197 static certificate_t *get_issuer_cert(private_credential_manager_t *this,
1198 certificate_t *subject)
1199 {
1200 enumerator_t *enumerator;
1201 certificate_t *issuer = NULL, *candidate;
1202
1203 enumerator = create_cert_enumerator(this, subject->get_type(subject), KEY_ANY,
1204 subject->get_issuer(subject), FALSE);
1205 while (enumerator->enumerate(enumerator, &candidate))
1206 {
1207 if (subject->issued_by(subject, candidate, FALSE))
1208 {
1209 issuer = candidate->get_ref(candidate);
1210 break;
1211 }
1212 }
1213 enumerator->destroy(enumerator);
1214 return issuer;
1215 }
1216
1217 /**
1218 * Check if a certificate's keyid is contained in the auth helper
1219 */
1220 static bool auth_contains_cacert(auth_info_t *auth, certificate_t *cert)
1221 {
1222 enumerator_t *enumerator;
1223 identification_t *value;
1224 auth_item_t type;
1225 bool found = FALSE;
1226
1227 enumerator = auth->create_item_enumerator(auth);
1228 while (enumerator->enumerate(enumerator, &type, &value))
1229 {
1230 if (type == AUTHN_CA_CERT && cert->equals(cert, (certificate_t*)value))
1231 {
1232 found = TRUE;
1233 break;
1234 }
1235 }
1236 enumerator->destroy(enumerator);
1237 return found;
1238 }
1239
1240 /**
1241 * build a trustchain from subject up to a trust anchor in trusted
1242 */
1243 static auth_info_t *build_trustchain(private_credential_manager_t *this,
1244 certificate_t *subject, auth_info_t *auth)
1245 {
1246 certificate_t *issuer, *current;
1247 auth_info_t *trustchain;
1248 u_int level = 0;
1249
1250 trustchain = auth_info_create();
1251
1252 if (!auth->get_item(auth, AUTHN_CA_CERT, (void**)&current))
1253 {
1254 /* no trust anchor specified, return this cert only */
1255 trustchain->add_item(trustchain, AUTHZ_SUBJECT_CERT, subject);
1256 return trustchain;
1257 }
1258
1259 current = subject->get_ref(subject);
1260 while (TRUE)
1261 {
1262 if (auth_contains_cacert(auth, current))
1263 {
1264 trustchain->add_item(trustchain, AUTHZ_CA_CERT, current);
1265 current->destroy(current);
1266 return trustchain;
1267 }
1268 if (subject == current)
1269 {
1270 trustchain->add_item(trustchain, AUTHZ_SUBJECT_CERT, current);
1271 }
1272 else
1273 {
1274 trustchain->add_item(trustchain, AUTHZ_IM_CERT, current);
1275 }
1276 issuer = get_issuer_cert(this, current);
1277 if (!issuer || issuer->equals(issuer, current) || level > MAX_CA_LEVELS)
1278 {
1279 DESTROY_IF(issuer);
1280 current->destroy(current);
1281 break;
1282 }
1283 current->destroy(current);
1284 current = issuer;
1285 level++;
1286 }
1287 trustchain->destroy(trustchain);
1288 return NULL;
1289 }
1290
1291 /**
1292 * find a private key of a give certificate
1293 */
1294 static private_key_t *get_private_by_cert(private_credential_manager_t *this,
1295 certificate_t *cert, key_type_t type)
1296 {
1297 private_key_t *private = NULL;
1298 identification_t* keyid;
1299 public_key_t *public;
1300
1301 public = cert->get_public_key(cert);
1302 if (public)
1303 {
1304 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
1305 if (keyid)
1306 {
1307 private = get_private_by_keyid(this, type, keyid);
1308 }
1309 public->destroy(public);
1310 }
1311 return private;
1312 }
1313
1314 /**
1315 * Implementation of credential_manager_t.get_private.
1316 */
1317 static private_key_t *get_private(private_credential_manager_t *this,
1318 key_type_t type, identification_t *id,
1319 auth_info_t *auth)
1320 {
1321 enumerator_t *enumerator;
1322 certificate_t *cert;
1323 private_key_t *private = NULL;
1324 auth_info_t *trustchain;
1325
1326 /* check if this is a lookup by key ID, and do it if so */
1327 if (id)
1328 {
1329 switch (id->get_type(id))
1330 {
1331 case ID_PUBKEY_SHA1:
1332 case ID_PUBKEY_INFO_SHA1:
1333 return get_private_by_keyid(this, type, id);
1334 default:
1335 break;
1336 }
1337 }
1338
1339 this->mutex->lock(this->mutex);
1340 /* get all available end entity certificates for ourself */
1341 enumerator = create_cert_enumerator(this, CERT_ANY, type, id, FALSE);
1342 while (enumerator->enumerate(enumerator, &cert))
1343 {
1344 private = get_private_by_cert(this, cert, type);
1345 if (private)
1346 {
1347 trustchain = build_trustchain(this, cert, auth);
1348 if (trustchain)
1349 {
1350 auth->merge(auth, trustchain);
1351 trustchain->destroy(trustchain);
1352 break;
1353 }
1354 private->destroy(private);
1355 }
1356 }
1357 enumerator->destroy(enumerator);
1358 this->mutex->unlock(this->mutex);
1359 return private;
1360 }
1361
1362 /**
1363 * Implementation of credential_manager_t.add_set.
1364 */
1365 static void add_set(private_credential_manager_t *this,
1366 credential_set_t *set)
1367 {
1368 this->mutex->lock(this->mutex);
1369 this->sets->insert_last(this->sets, set);
1370 this->mutex->unlock(this->mutex);
1371 }
1372 /**
1373 * Implementation of credential_manager_t.remove_set.
1374 */
1375 static void remove_set(private_credential_manager_t *this, credential_set_t *set)
1376 {
1377 this->mutex->lock(this->mutex);
1378 this->sets->remove(this->sets, set, NULL);
1379 this->mutex->unlock(this->mutex);
1380 }
1381
1382 /**
1383 * Implementation of credential_manager_t.destroy
1384 */
1385 static void destroy(private_credential_manager_t *this)
1386 {
1387 this->sets->destroy(this->sets);
1388 this->mutex->destroy(this->mutex);
1389 free(this);
1390 }
1391
1392 /*
1393 * see header file
1394 */
1395 credential_manager_t *credential_manager_create()
1396 {
1397 private_credential_manager_t *this = malloc_thing(private_credential_manager_t);
1398
1399 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;
1400 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;
1401 this->public.create_cdp_enumerator = (enumerator_t *(*)(credential_manager_t*, credential_type_t type, identification_t *id))create_cdp_enumerator;
1402 this->public.get_cert = (certificate_t *(*)(credential_manager_t *this,certificate_type_t cert, key_type_t key,identification_t *, bool))get_cert;
1403 this->public.get_shared = (shared_key_t *(*)(credential_manager_t *this,shared_key_type_t type,identification_t *me, identification_t *other))get_shared;
1404 this->public.get_private = (private_key_t*(*)(credential_manager_t*, key_type_t type, identification_t *, auth_info_t*))get_private;
1405 this->public.get_public = (public_key_t*(*)(credential_manager_t*, key_type_t type, identification_t *, auth_info_t*))get_public;
1406 this->public.add_set = (void(*)(credential_manager_t*, credential_set_t *set))add_set;
1407 this->public.remove_set = (void(*)(credential_manager_t*, credential_set_t *set))remove_set;
1408 this->public.destroy = (void(*)(credential_manager_t*))destroy;
1409
1410 this->sets = linked_list_create();
1411 this->mutex = mutex_create(MUTEX_RECURSIVE);
1412
1413 return &this->public;
1414 }
1415