fixed stupid bug in fetch_ocsp()
[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 /**
303 * credential_set_t implementation around an OCSP response
304 */
305 typedef struct ocsp_wrapper_t {
306 credential_set_t set;
307 ocsp_response_t *response;
308 } ocsp_wrapper_t;
309
310 /**
311 * enumerator for ocsp_wrapper_t.create_cert_enumerator()
312 */
313 typedef struct {
314 enumerator_t public;
315 enumerator_t *inner;
316 certificate_type_t cert;
317 key_type_t key;
318 identification_t *id;
319 } ocsp_wrapper_enumerator_t;
320
321 /**
322 * enumerate function for ocsp_wrapper_enumerator_t
323 */
324 static bool ocsp_wrapper_enum_enumerate(ocsp_wrapper_enumerator_t *this,
325 certificate_t **cert)
326 {
327 certificate_t *current;
328 public_key_t *public;
329
330 while (this->inner->enumerate(this->inner, &current))
331 {
332 if (this->cert != CERT_ANY && this->cert != current->get_type(current))
333 { /* CERT type requested, but does not match */
334 continue;
335 }
336 public = current->get_public_key(current);
337 if (this->key != KEY_ANY && !public)
338 { /* key type requested, but no public key */
339 DESTROY_IF(public);
340 continue;
341 }
342 if (this->key != KEY_ANY && public && this->key != public->get_type(public))
343 { /* key type requested, but public key has another type */
344 DESTROY_IF(public);
345 continue;
346 }
347 DESTROY_IF(public);
348 if (this->id && !current->has_subject(current, this->id))
349 { /* subject requested, but does not match */
350 continue;
351 }
352 *cert = current;
353 return TRUE;
354 }
355 return FALSE;
356 }
357
358 /**
359 * destroy function for ocsp_wrapper_enumerator_t
360 */
361 static void ocsp_wrapper_enum_destroy(ocsp_wrapper_enumerator_t *this)
362 {
363 this->inner->destroy(this->inner);
364 free(this);
365 }
366
367 /**
368 * implementation of ocsp_wrapper_t.set.create_cert_enumerator
369 */
370 static enumerator_t *ocsp_wrapper_create_enumerator(ocsp_wrapper_t *this,
371 certificate_type_t cert, key_type_t key,
372 identification_t *id, bool trusted)
373 {
374 ocsp_wrapper_enumerator_t *enumerator;
375
376 if (trusted)
377 {
378 return NULL;
379 }
380
381 enumerator = malloc_thing(ocsp_wrapper_enumerator_t);
382 enumerator->cert = cert;
383 enumerator->key = key;
384 enumerator->id = id;
385 enumerator->inner = this->response->create_cert_enumerator(this->response);
386 enumerator->public.enumerate = (void*)ocsp_wrapper_enum_enumerate;
387 enumerator->public.destroy = (void*)ocsp_wrapper_enum_destroy;
388 return &enumerator->public;
389 }
390
391 /**
392 * create credential_set wrapper around an OCSP response
393 */
394 static ocsp_wrapper_t *ocsp_wrapper_create(ocsp_response_t *response)
395 {
396 ocsp_wrapper_t *this = malloc_thing(ocsp_wrapper_t);
397
398 this->response = response;
399 this->set.create_private_enumerator = (void*)return_null;
400 this->set.create_cert_enumerator = (void*)ocsp_wrapper_create_enumerator;
401 this->set.create_shared_enumerator = (void*)return_null;
402 this->set.create_cdp_enumerator = (void*)return_null;
403
404 return this;
405 }
406
407 /**
408 * Do an OCSP request
409 */
410 static certificate_t *fetch_ocsp(private_credential_manager_t *this, char *url,
411 certificate_t *subject, certificate_t *issuer)
412 {
413 certificate_t *request, *response;
414 chunk_t send, receive;
415
416 /* TODO: requestor name, signature */
417 request = lib->creds->create(lib->creds,
418 CRED_CERTIFICATE, CERT_X509_OCSP_REQUEST,
419 BUILD_CA_CERT, issuer->get_ref(issuer),
420 BUILD_CERT, subject->get_ref(subject), BUILD_END);
421 if (!request)
422 {
423 DBG1(DBG_CFG, " generating ocsp request failed");
424 return NULL;
425 }
426
427 send = request->get_encoding(request);
428 request->destroy(request);
429
430 DBG1(DBG_CFG, "requesting ocsp response from '%s' ...", url);
431 if (lib->fetcher->fetch(lib->fetcher, url, &receive,
432 FETCH_REQUEST_DATA, send,
433 FETCH_REQUEST_TYPE, "application/ocsp-request",
434 FETCH_END) != SUCCESS)
435 {
436 DBG1(DBG_CFG, " ocsp request to %s failed", url);
437 chunk_free(&send);
438 return NULL;
439 }
440 chunk_free(&send);
441
442 response = lib->creds->create(lib->creds,
443 CRED_CERTIFICATE, CERT_X509_OCSP_RESPONSE,
444 BUILD_BLOB_ASN1_DER, receive, BUILD_END);
445 if (!response)
446 {
447 DBG1(DBG_CFG, " parsing ocsp response failed");
448 return NULL;
449 }
450
451
452 /* verify the signature of the ocsp response */
453 {
454 certificate_t *issuer_cert;
455 identification_t *responder;
456 auth_info_t *auth;
457 ocsp_wrapper_t *wrapper;
458 bool ok;
459
460 auth = auth_info_create();
461 wrapper = ocsp_wrapper_create((ocsp_response_t*)response);
462 this->sets->insert_first(this->sets, wrapper);
463 responder = response->get_issuer(response);
464 issuer_cert = get_trusted_cert(this, KEY_ANY, responder, auth, FALSE, FALSE);
465 this->sets->remove(this->sets, wrapper, NULL);
466 free(wrapper);
467 auth->destroy(auth);
468
469 if (!issuer_cert)
470 {
471 DBG1(DBG_CFG, " ocsp response untrusted: no signer certificate found");
472 response->destroy(response);
473 return NULL;
474 }
475 ok = response->issued_by(response, issuer_cert, TRUE);
476 issuer_cert->destroy(issuer_cert);
477 DBG1(DBG_CFG, " ocsp response is %strusted: %s signature",
478 ok ? "":"un", ok ? "good" : "bad");
479 if (!ok)
480 {
481 response->destroy(response);
482 return NULL;
483 }
484 }
485 /* TODO: cache response? */
486 return response;
487 }
488
489 /**
490 * validate a x509 certificate using OCSP
491 */
492 static cert_validation_t check_ocsp(private_credential_manager_t *this,
493 x509_t *subject, x509_t *issuer,
494 auth_info_t *auth)
495 {
496 certificate_t *sub = (certificate_t*)subject;
497 certificate_t *best_cert = NULL;
498 certificate_t *cert;
499 public_key_t *public;
500 cert_validation_t valid = VALIDATION_SKIPPED;
501 identification_t *keyid = NULL;
502 bool stale = TRUE;
503
504 /* derive the authorityKeyIdentifier from the issuer's public key */
505 cert = &issuer->interface;
506 public = cert->get_public_key(cert);
507 if (public)
508 {
509 keyid = public->get_id(public, ID_PUBKEY_SHA1);
510 }
511
512 /* find a cached ocsp response by authorityKeyIdentifier */
513 if (keyid)
514 {
515 enumerator_t *enumerator = create_cert_enumerator(this,
516 CERT_X509_OCSP_RESPONSE,
517 KEY_ANY, keyid, TRUE);
518 certificate_t *cert;
519
520 while (enumerator->enumerate(enumerator, &cert))
521 {
522 if (cert->has_subject(cert, sub->get_subject(sub)))
523 {
524 /* select most recent ocsp response */
525 if (best_cert == NULL || cert->is_newer(cert, best_cert))
526 {
527 DESTROY_IF(best_cert);
528 best_cert = cert->get_ref(cert);
529 }
530 }
531 }
532 enumerator->destroy(enumerator);
533 }
534
535 /* check the validity of the cached ocsp response if one was found */
536 if (best_cert)
537 {
538 stale = !best_cert->get_validity(best_cert, NULL, NULL, NULL);
539 DBG1(DBG_CFG, "cached ocsp response is %s", stale? "stale":"valid");
540 }
541
542 /* fallback to URL fetching from CDPs */
543 if (stale && keyid)
544 {
545 enumerator_t *enumerator = create_cdp_enumerator(this,
546 CERT_X509_OCSP_RESPONSE, keyid);
547 char *uri;
548
549 while (enumerator->enumerate(enumerator, &uri))
550 {
551 certificate_t* cert = fetch_ocsp(this, uri, &subject->interface,
552 &issuer->interface);
553
554 /* redefine default since we have at least one uri */
555 valid = VALIDATION_FAILED;
556
557 if (cert)
558 {
559 /* select most recent ocsp response until valid one is found */
560 if (best_cert == NULL || cert->is_newer(cert, best_cert))
561 {
562 DESTROY_IF(best_cert);
563 best_cert = cert;
564 stale = !best_cert->get_validity(best_cert, NULL, NULL, NULL);
565 DBG1(DBG_CFG, "ocsp response is %s", stale? "stale":"valid");
566 if (!stale)
567 {
568 break;
569 }
570 }
571 else
572 {
573 cert->destroy(cert);
574 }
575 }
576 }
577 enumerator->destroy(enumerator);
578 }
579
580 /* fallback to URL fetching from subject certificate's URIs */
581 if (stale)
582 {
583 enumerator_t *enumerator = subject->create_ocsp_uri_enumerator(subject);
584 char *uri;
585
586 while (enumerator->enumerate(enumerator, &uri))
587 {
588 certificate_t* cert = fetch_ocsp(this, uri, &subject->interface,
589 &issuer->interface);
590
591 /* redefine default since we have at least one uri */
592 valid = VALIDATION_FAILED;
593
594 if (cert)
595 {
596 /* select most recent ocsp response until valid one is found */
597 if (best_cert == NULL || cert->is_newer(cert, best_cert))
598 {
599 DESTROY_IF(best_cert);
600 best_cert = cert;
601 stale = !best_cert->get_validity(best_cert, NULL, NULL, NULL);
602 DBG1(DBG_CFG, "ocsp response is %s", stale? "stale":"valid");
603 if (!stale)
604 {
605 break;
606 }
607 }
608 else
609 {
610 cert->destroy(cert);
611 }
612 }
613 }
614 enumerator->destroy(enumerator);
615 }
616 DESTROY_IF(public);
617
618 /* if we have an ocsp response, check the revocation status */
619 if (best_cert)
620 {
621 time_t revocation, this_update, next_update;
622 crl_reason_t reason;
623 ocsp_response_t *response = (ocsp_response_t*)best_cert;
624
625 valid = response->get_status(response, subject, issuer, &revocation,
626 &reason, &this_update, &next_update);
627 switch (valid)
628 {
629 case VALIDATION_FAILED:
630 DBG1(DBG_CFG, "subject not found in ocsp response");
631 break;
632 case VALIDATION_REVOKED:
633 DBG1(DBG_CFG, "certificate was revoked on %T, reason: %N",
634 &revocation, crl_reason_names, reason);
635 break;
636 case VALIDATION_GOOD:
637 case VALIDATION_UNKNOWN:
638 default:
639 break;
640 }
641 best_cert->destroy(best_cert);
642 }
643
644 if (auth)
645 {
646 auth->add_item(auth, AUTHZ_OCSP_VALIDATION, &valid);
647 }
648 return valid;
649 }
650
651 /**
652 * fetch a CRL from an URL
653 */
654 static certificate_t* fetch_crl(private_credential_manager_t *this, char *url)
655 {
656 certificate_t *crl_cert;
657 chunk_t chunk;
658
659 /* TODO: unlock the manager while fetching? */
660 DBG1(DBG_CFG, "fetching crl from '%s' ...", url);
661 if (lib->fetcher->fetch(lib->fetcher, url, &chunk, FETCH_END) != SUCCESS)
662 {
663 DBG1(DBG_CFG, " crl fetching failed");
664 return NULL;
665 }
666 crl_cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509_CRL,
667 BUILD_BLOB_ASN1_DER, chunk, BUILD_END);
668 if (!crl_cert)
669 {
670 DBG1(DBG_CFG, " crl fetched successfully but parsing failed");
671 return NULL;
672 }
673
674 /* verify the signature of the fetched crl */
675 {
676 bool ok;
677 identification_t *issuer = crl_cert->get_issuer(crl_cert);
678 auth_info_t *auth = auth_info_create();
679 certificate_t *issuer_cert = get_trusted_cert(this, KEY_ANY, issuer,
680 auth, FALSE, FALSE);
681 auth->destroy(auth);
682
683 if (!issuer_cert)
684 {
685 DBG1(DBG_CFG, " crl is untrusted: issuer certificate not found");
686 crl_cert->destroy(crl_cert);
687 return NULL;
688 }
689 ok = crl_cert->issued_by(crl_cert, issuer_cert, TRUE);
690 issuer_cert->destroy(issuer_cert);
691
692 DBG1(DBG_CFG, " crl is %strusted: %s signature",
693 ok ? "":"un", ok ? "good" : "bad");
694 if (!ok)
695 {
696 crl_cert->destroy(crl_cert);
697 return NULL;
698 }
699 }
700 return crl_cert;
701 }
702
703 /**
704 * validate a x509 certificate using CRL
705 */
706 static cert_validation_t check_crl(private_credential_manager_t *this,
707 x509_t *subject, x509_t *issuer,
708 auth_info_t *auth)
709 {
710 identification_t *keyid = NULL;
711 certificate_t *best_cert = NULL;
712 certificate_t *cert;
713 public_key_t *public;
714 cert_validation_t valid = VALIDATION_SKIPPED;
715 bool stale = TRUE;
716
717 /* derive the authorityKeyIdentifier from the issuer's public key */
718 cert = &issuer->interface;
719 public = cert->get_public_key(cert);
720 if (public)
721 {
722 keyid = public->get_id(public, ID_PUBKEY_SHA1);
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 DESTROY_IF(public);
824
825 /* if we have a crl, check the revocation status */
826 if (best_cert)
827 {
828 chunk_t subject_serial = subject->get_serial(subject);
829 chunk_t serial;
830 time_t revocation;
831 crl_reason_t reason;
832 crl_t *crl = (crl_t*)best_cert;
833 enumerator_t *enumerator = crl->create_enumerator(crl);
834
835 /* redefine default */
836 valid = stale ? VALIDATION_UNKNOWN : VALIDATION_GOOD;
837
838 while (enumerator->enumerate(enumerator, &serial, &revocation, &reason))
839 {
840 if (chunk_equals(serial, subject_serial))
841 {
842 DBG1(DBG_CFG, "certificate was revoked on %T, reason: %N",
843 &revocation, crl_reason_names, reason);
844 valid = VALIDATION_REVOKED;
845 break;
846 }
847 }
848 enumerator->destroy(enumerator);
849 best_cert->destroy(best_cert);
850 }
851
852 if (auth)
853 {
854 auth->add_item(auth, AUTHZ_CRL_VALIDATION, &valid);
855 }
856 return valid;
857 }
858
859 /**
860 * check a certificate for its lifetime
861 */
862 static bool check_certificate(private_credential_manager_t *this,
863 certificate_t *subject, certificate_t *issuer,
864 bool crl, bool ocsp, auth_info_t *auth)
865 {
866 time_t not_before, not_after;
867
868 if (!subject->get_validity(subject, NULL, &not_before, &not_after))
869 {
870 DBG1(DBG_CFG, "certificate invalid (valid from %T to %T)",
871 &not_before, &not_after);
872 return FALSE;
873 }
874 if (issuer && !subject->issued_by(subject, issuer, TRUE))
875 {
876 DBG1(DBG_CFG, "certificate not issued by \"%D\"",
877 subject->get_subject(subject), issuer->get_subject(issuer));
878 return FALSE;
879 }
880 if (issuer && issuer->get_type(issuer) == CERT_X509 &&
881 subject->get_type(subject) == CERT_X509)
882 {
883 if (ocsp)
884 {
885 switch (check_ocsp(this, (x509_t*)subject, (x509_t*)issuer, auth))
886 {
887 case VALIDATION_GOOD:
888 DBG1(DBG_CFG, "certificate status is good");
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 return TRUE;
909 case VALIDATION_REVOKED:
910 /* has already been logged */
911 return FALSE;
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 DBG1(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 DBG1(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 DBG1(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 DBG1(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