Pass an additional anchor flag to validate() hook if we reach the root CA
[strongswan.git] / src / libstrongswan / plugins / revocation / revocation_validator.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2009 Andreas Steffen
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "revocation_validator.h"
19
20 #include <debug.h>
21 #include <credentials/certificates/x509.h>
22 #include <credentials/certificates/crl.h>
23 #include <credentials/certificates/ocsp_request.h>
24 #include <credentials/certificates/ocsp_response.h>
25 #include <credentials/sets/ocsp_response_wrapper.h>
26 #include <selectors/traffic_selector.h>
27
28 typedef struct private_revocation_validator_t private_revocation_validator_t;
29
30 /**
31 * Private data of an revocation_validator_t object.
32 */
33 struct private_revocation_validator_t {
34
35 /**
36 * Public revocation_validator_t interface.
37 */
38 revocation_validator_t public;
39 };
40
41 /**
42 * Do an OCSP request
43 */
44 static certificate_t *fetch_ocsp(char *url, certificate_t *subject,
45 certificate_t *issuer)
46 {
47 certificate_t *request, *response;
48 chunk_t send, receive;
49
50 /* TODO: requestor name, signature */
51 request = lib->creds->create(lib->creds,
52 CRED_CERTIFICATE, CERT_X509_OCSP_REQUEST,
53 BUILD_CA_CERT, issuer,
54 BUILD_CERT, subject, BUILD_END);
55 if (!request)
56 {
57 DBG1(DBG_CFG, "generating ocsp request failed");
58 return NULL;
59 }
60
61 if (!request->get_encoding(request, CERT_ASN1_DER, &send))
62 {
63 DBG1(DBG_CFG, "encoding ocsp request failed");
64 request->destroy(request);
65 return NULL;
66 }
67 request->destroy(request);
68
69 DBG1(DBG_CFG, " requesting ocsp status from '%s' ...", url);
70 if (lib->fetcher->fetch(lib->fetcher, url, &receive,
71 FETCH_REQUEST_DATA, send,
72 FETCH_REQUEST_TYPE, "application/ocsp-request",
73 FETCH_END) != SUCCESS)
74 {
75 DBG1(DBG_CFG, "ocsp request to %s failed", url);
76 chunk_free(&send);
77 return NULL;
78 }
79 chunk_free(&send);
80
81 response = lib->creds->create(lib->creds,
82 CRED_CERTIFICATE, CERT_X509_OCSP_RESPONSE,
83 BUILD_BLOB_ASN1_DER, receive, BUILD_END);
84 chunk_free(&receive);
85 if (!response)
86 {
87 DBG1(DBG_CFG, "parsing ocsp response failed");
88 return NULL;
89 }
90 return response;
91 }
92
93 /**
94 * check the signature of an OCSP response
95 */
96 static bool verify_ocsp(ocsp_response_t *response, auth_cfg_t *auth)
97 {
98 certificate_t *issuer, *subject;
99 identification_t *responder;
100 ocsp_response_wrapper_t *wrapper;
101 enumerator_t *enumerator;
102 auth_cfg_t *current;
103 bool verified = FALSE;
104
105 wrapper = ocsp_response_wrapper_create((ocsp_response_t*)response);
106 lib->credmgr->add_local_set(lib->credmgr, &wrapper->set);
107
108 subject = &response->certificate;
109 responder = subject->get_issuer(subject);
110 enumerator = lib->credmgr->create_trusted_enumerator(lib->credmgr,
111 KEY_ANY, responder, FALSE);
112 while (enumerator->enumerate(enumerator, &issuer, &current))
113 {
114 if (lib->credmgr->issued_by(lib->credmgr, subject, issuer))
115 {
116 DBG1(DBG_CFG, " ocsp response correctly signed by \"%Y\"",
117 issuer->get_subject(issuer));
118 if (auth)
119 {
120 auth->merge(auth, current, FALSE);
121 }
122 verified = TRUE;
123 break;
124 }
125 }
126 enumerator->destroy(enumerator);
127
128 lib->credmgr->remove_local_set(lib->credmgr, &wrapper->set);
129 wrapper->destroy(wrapper);
130 return verified;
131 }
132
133 /**
134 * Get the better of two OCSP responses, and check for usable OCSP info
135 */
136 static certificate_t *get_better_ocsp(certificate_t *cand, certificate_t *best,
137 x509_t *subject, x509_t *issuer, cert_validation_t *valid,
138 auth_cfg_t *auth, bool cache)
139 {
140 ocsp_response_t *response;
141 time_t revocation, this_update, next_update, valid_until;
142 crl_reason_t reason;
143 bool revoked = FALSE;
144
145 response = (ocsp_response_t*)cand;
146
147 /* check ocsp signature */
148 if (!verify_ocsp(response, auth))
149 {
150 DBG1(DBG_CFG, "ocsp response verification failed");
151 cand->destroy(cand);
152 return best;
153 }
154 /* check if response contains our certificate */
155 switch (response->get_status(response, subject, issuer, &revocation, &reason,
156 &this_update, &next_update))
157 {
158 case VALIDATION_REVOKED:
159 /* subject has been revoked by a valid OCSP response */
160 DBG1(DBG_CFG, "certificate was revoked on %T, reason: %N",
161 &revocation, TRUE, crl_reason_names, reason);
162 revoked = TRUE;
163 break;
164 case VALIDATION_GOOD:
165 /* results in either good or stale */
166 break;
167 default:
168 case VALIDATION_FAILED:
169 /* candidate unusable, does not contain our cert */
170 DBG1(DBG_CFG, " ocsp response contains no status on our certificate");
171 cand->destroy(cand);
172 return best;
173 }
174
175 /* select the better of the two responses */
176 if (best == NULL || certificate_is_newer(cand, best))
177 {
178 DESTROY_IF(best);
179 best = cand;
180 if (best->get_validity(best, NULL, NULL, &valid_until))
181 {
182 DBG1(DBG_CFG, " ocsp response is valid: until %T",
183 &valid_until, FALSE);
184 *valid = VALIDATION_GOOD;
185 if (cache)
186 { /* cache non-stale only, stale certs get refetched */
187 lib->credmgr->cache_cert(lib->credmgr, best);
188 }
189 }
190 else
191 {
192 DBG1(DBG_CFG, " ocsp response is stale: since %T",
193 &valid_until, FALSE);
194 *valid = VALIDATION_STALE;
195 }
196 }
197 else
198 {
199 *valid = VALIDATION_STALE;
200 cand->destroy(cand);
201 }
202 if (revoked)
203 { /* revoked always counts, even if stale */
204 *valid = VALIDATION_REVOKED;
205 }
206 return best;
207 }
208
209 /**
210 * validate a x509 certificate using OCSP
211 */
212 static cert_validation_t check_ocsp(x509_t *subject, x509_t *issuer,
213 auth_cfg_t *auth)
214 {
215 enumerator_t *enumerator;
216 cert_validation_t valid = VALIDATION_SKIPPED;
217 certificate_t *best = NULL, *current;
218 identification_t *keyid = NULL;
219 public_key_t *public;
220 chunk_t chunk;
221 char *uri = NULL;
222
223 /** lookup cache for valid OCSP responses */
224 enumerator = lib->credmgr->create_cert_enumerator(lib->credmgr,
225 CERT_X509_OCSP_RESPONSE, KEY_ANY, NULL, FALSE);
226 while (enumerator->enumerate(enumerator, &current))
227 {
228 current->get_ref(current);
229 best = get_better_ocsp(current, best, subject, issuer,
230 &valid, auth, FALSE);
231 if (best && valid != VALIDATION_STALE)
232 {
233 DBG1(DBG_CFG, " using cached ocsp response");
234 break;
235 }
236 }
237 enumerator->destroy(enumerator);
238
239 /* derive the authorityKeyIdentifier from the issuer's public key */
240 current = &issuer->interface;
241 public = current->get_public_key(current);
242 if (public && public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
243 {
244 keyid = identification_create_from_encoding(ID_KEY_ID, chunk);
245 }
246 /** fetch from configured OCSP responder URLs */
247 if (keyid && valid != VALIDATION_GOOD && valid != VALIDATION_REVOKED)
248 {
249 enumerator = lib->credmgr->create_cdp_enumerator(lib->credmgr,
250 CERT_X509_OCSP_RESPONSE, keyid);
251 while (enumerator->enumerate(enumerator, &uri))
252 {
253 current = fetch_ocsp(uri, &subject->interface, &issuer->interface);
254 if (current)
255 {
256 best = get_better_ocsp(current, best, subject, issuer,
257 &valid, auth, TRUE);
258 if (best && valid != VALIDATION_STALE)
259 {
260 break;
261 }
262 }
263 }
264 enumerator->destroy(enumerator);
265 }
266 DESTROY_IF(public);
267 DESTROY_IF(keyid);
268
269 /* fallback to URL fetching from subject certificate's URIs */
270 if (valid != VALIDATION_GOOD && valid != VALIDATION_REVOKED)
271 {
272 enumerator = subject->create_ocsp_uri_enumerator(subject);
273 while (enumerator->enumerate(enumerator, &uri))
274 {
275 current = fetch_ocsp(uri, &subject->interface, &issuer->interface);
276 if (current)
277 {
278 best = get_better_ocsp(current, best, subject, issuer,
279 &valid, auth, TRUE);
280 if (best && valid != VALIDATION_STALE)
281 {
282 break;
283 }
284 }
285 }
286 enumerator->destroy(enumerator);
287 }
288 /* an uri was found, but no result. switch validation state to failed */
289 if (valid == VALIDATION_SKIPPED && uri)
290 {
291 valid = VALIDATION_FAILED;
292 }
293 if (auth)
294 {
295 auth->add(auth, AUTH_RULE_OCSP_VALIDATION, valid);
296 if (valid == VALIDATION_GOOD)
297 { /* successful OCSP check fulfills also CRL constraint */
298 auth->add(auth, AUTH_RULE_CRL_VALIDATION, VALIDATION_GOOD);
299 }
300 }
301 DESTROY_IF(best);
302 return valid;
303 }
304
305 /**
306 * fetch a CRL from an URL
307 */
308 static certificate_t* fetch_crl(char *url)
309 {
310 certificate_t *crl;
311 chunk_t chunk;
312
313 DBG1(DBG_CFG, " fetching crl from '%s' ...", url);
314 if (lib->fetcher->fetch(lib->fetcher, url, &chunk, FETCH_END) != SUCCESS)
315 {
316 DBG1(DBG_CFG, "crl fetching failed");
317 return NULL;
318 }
319 crl = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509_CRL,
320 BUILD_BLOB_ASN1_DER, chunk, BUILD_END);
321 chunk_free(&chunk);
322 if (!crl)
323 {
324 DBG1(DBG_CFG, "crl fetched successfully but parsing failed");
325 return NULL;
326 }
327 return crl;
328 }
329
330 /**
331 * check the signature of an CRL
332 */
333 static bool verify_crl(certificate_t *crl, auth_cfg_t *auth)
334 {
335 certificate_t *issuer;
336 enumerator_t *enumerator;
337 bool verified = FALSE;
338 auth_cfg_t *current;
339
340 enumerator = lib->credmgr->create_trusted_enumerator(lib->credmgr,
341 KEY_ANY, crl->get_issuer(crl), FALSE);
342 while (enumerator->enumerate(enumerator, &issuer, &current))
343 {
344 if (lib->credmgr->issued_by(lib->credmgr, crl, issuer))
345 {
346 DBG1(DBG_CFG, " crl correctly signed by \"%Y\"",
347 issuer->get_subject(issuer));
348 if (auth)
349 {
350 auth->merge(auth, current, FALSE);
351 }
352 verified = TRUE;
353 break;
354 }
355 }
356 enumerator->destroy(enumerator);
357
358 return verified;
359 }
360
361 /**
362 * Get the better of two CRLs, and check for usable CRL info
363 */
364 static certificate_t *get_better_crl(certificate_t *cand, certificate_t *best,
365 x509_t *subject, cert_validation_t *valid, auth_cfg_t *auth, bool cache)
366 {
367 enumerator_t *enumerator;
368 time_t revocation, valid_until;
369 crl_reason_t reason;
370 chunk_t serial;
371 crl_t *crl;
372
373 /* check CRL signature */
374 if (!verify_crl(cand, auth))
375 {
376 DBG1(DBG_CFG, "crl response verification failed");
377 cand->destroy(cand);
378 return best;
379 }
380
381 crl = (crl_t*)cand;
382 enumerator = crl->create_enumerator(crl);
383 while (enumerator->enumerate(enumerator, &serial, &revocation, &reason))
384 {
385 if (chunk_equals(serial, subject->get_serial(subject)))
386 {
387 DBG1(DBG_CFG, "certificate was revoked on %T, reason: %N",
388 &revocation, TRUE, crl_reason_names, reason);
389 *valid = VALIDATION_REVOKED;
390 enumerator->destroy(enumerator);
391 DESTROY_IF(best);
392 return cand;
393 }
394 }
395 enumerator->destroy(enumerator);
396
397 /* select the better of the two CRLs */
398 if (best == NULL || crl_is_newer(crl, (crl_t*)best))
399 {
400 DESTROY_IF(best);
401 best = cand;
402 if (best->get_validity(best, NULL, NULL, &valid_until))
403 {
404 DBG1(DBG_CFG, " crl is valid: until %T", &valid_until, FALSE);
405 *valid = VALIDATION_GOOD;
406 if (cache)
407 { /* we cache non-stale crls only, as a stale crls are refetched */
408 lib->credmgr->cache_cert(lib->credmgr, best);
409 }
410 }
411 else
412 {
413 DBG1(DBG_CFG, " crl is stale: since %T", &valid_until, FALSE);
414 *valid = VALIDATION_STALE;
415 }
416 }
417 else
418 {
419 *valid = VALIDATION_STALE;
420 cand->destroy(cand);
421 }
422 return best;
423 }
424
425 /**
426 * Find or fetch a certificate for a given crlIssuer
427 */
428 static cert_validation_t find_crl(x509_t *subject, identification_t *issuer,
429 auth_cfg_t *auth, certificate_t **best, bool *uri_found)
430 {
431 cert_validation_t valid = VALIDATION_SKIPPED;
432 enumerator_t *enumerator;
433 certificate_t *current;
434 char *uri;
435
436 /* find a cached crl */
437 enumerator = lib->credmgr->create_cert_enumerator(lib->credmgr,
438 CERT_X509_CRL, KEY_ANY, issuer, FALSE);
439 while (enumerator->enumerate(enumerator, &current))
440 {
441 current->get_ref(current);
442 *best = get_better_crl(current, *best, subject, &valid, auth, FALSE);
443 if (*best && valid != VALIDATION_STALE)
444 {
445 DBG1(DBG_CFG, " using cached crl");
446 break;
447 }
448 }
449 enumerator->destroy(enumerator);
450
451 /* fallback to fetching crls from credential sets cdps */
452 if (valid != VALIDATION_GOOD && valid != VALIDATION_REVOKED)
453 {
454 enumerator = lib->credmgr->create_cdp_enumerator(lib->credmgr,
455 CERT_X509_CRL, issuer);
456 while (enumerator->enumerate(enumerator, &uri))
457 {
458 *uri_found = TRUE;
459 current = fetch_crl(uri);
460 if (!current->has_issuer(current, issuer))
461 {
462 DBG1(DBG_CFG, "issuer of fetched CRL '%Y' does not match CRL "
463 "issuer '%Y'", current->get_issuer(current), issuer);
464 current->destroy(current);
465 continue;
466 }
467 if (current)
468 {
469 *best = get_better_crl(current, *best, subject,
470 &valid, auth, TRUE);
471 if (*best && valid != VALIDATION_STALE)
472 {
473 break;
474 }
475 }
476 }
477 enumerator->destroy(enumerator);
478 }
479 return valid;
480 }
481
482 /**
483 * validate a x509 certificate using CRL
484 */
485 static cert_validation_t check_crl(x509_t *subject, x509_t *issuer,
486 auth_cfg_t *auth)
487 {
488 cert_validation_t valid = VALIDATION_SKIPPED;
489 certificate_t *best = NULL;
490 identification_t *id;
491 x509_cdp_t *cdp;
492 bool uri_found = FALSE;
493 certificate_t *current;
494 enumerator_t *enumerator;
495 chunk_t chunk;
496
497 /* use issuers subjectKeyIdentifier to find a cached CRL / fetch from CDP */
498 chunk = issuer->get_subjectKeyIdentifier(issuer);
499 if (chunk.len)
500 {
501 id = identification_create_from_encoding(ID_KEY_ID, chunk);
502 valid = find_crl(subject, id, auth, &best, &uri_found);
503 id->destroy(id);
504 }
505
506 /* find a cached CRL or fetch via configured CDP via CRLIssuer */
507 enumerator = subject->create_crl_uri_enumerator(subject);
508 while (valid != VALIDATION_GOOD && valid != VALIDATION_REVOKED &&
509 enumerator->enumerate(enumerator, &cdp))
510 {
511 if (id)
512 {
513 valid = find_crl(subject, cdp->issuer, auth, &best, &uri_found);
514 }
515 }
516 enumerator->destroy(enumerator);
517
518 /* fallback to fetching CRLs from CDPs found in subjects certificate */
519 if (valid != VALIDATION_GOOD && valid != VALIDATION_REVOKED)
520 {
521 enumerator = subject->create_crl_uri_enumerator(subject);
522 while (enumerator->enumerate(enumerator, &cdp))
523 {
524 uri_found = TRUE;
525 current = fetch_crl(cdp->uri);
526 if (current)
527 {
528 if (cdp->issuer && !current->has_issuer(current, cdp->issuer))
529 {
530 DBG1(DBG_CFG, "issuer of fetched CRL '%Y' does not match "
531 "certificates CRL issuer '%Y'",
532 current->get_issuer(current), cdp->issuer);
533 current->destroy(current);
534 continue;
535 }
536 best = get_better_crl(current, best, subject, &valid,
537 auth, TRUE);
538 if (best && valid != VALIDATION_STALE)
539 {
540 break;
541 }
542 }
543 }
544 enumerator->destroy(enumerator);
545 }
546
547 /* an uri was found, but no result. switch validation state to failed */
548 if (valid == VALIDATION_SKIPPED && uri_found)
549 {
550 valid = VALIDATION_FAILED;
551 }
552 if (auth)
553 {
554 if (valid == VALIDATION_SKIPPED)
555 { /* if we skipped CRL validation, we use the result of OCSP for
556 * constraint checking */
557 auth->add(auth, AUTH_RULE_CRL_VALIDATION,
558 auth->get(auth, AUTH_RULE_OCSP_VALIDATION));
559 }
560 else
561 {
562 auth->add(auth, AUTH_RULE_CRL_VALIDATION, valid);
563 }
564 }
565 DESTROY_IF(best);
566 return valid;
567 }
568
569 METHOD(cert_validator_t, validate, bool,
570 private_revocation_validator_t *this, certificate_t *subject,
571 certificate_t *issuer, bool online, int pathlen, bool anchor,
572 auth_cfg_t *auth)
573 {
574 if (subject->get_type(subject) == CERT_X509 &&
575 issuer->get_type(issuer) == CERT_X509 &&
576 online)
577 {
578 DBG1(DBG_CFG, "checking certificate status of \"%Y\"",
579 subject->get_subject(subject));
580 switch (check_ocsp((x509_t*)subject, (x509_t*)issuer,
581 pathlen ? NULL : auth))
582 {
583 case VALIDATION_GOOD:
584 DBG1(DBG_CFG, "certificate status is good");
585 return TRUE;
586 case VALIDATION_REVOKED:
587 /* has already been logged */
588 return FALSE;
589 case VALIDATION_SKIPPED:
590 DBG2(DBG_CFG, "ocsp check skipped, no ocsp found");
591 break;
592 case VALIDATION_STALE:
593 DBG1(DBG_CFG, "ocsp information stale, fallback to crl");
594 break;
595 case VALIDATION_FAILED:
596 DBG1(DBG_CFG, "ocsp check failed, fallback to crl");
597 break;
598 }
599 switch (check_crl((x509_t*)subject, (x509_t*)issuer,
600 pathlen ? NULL : auth))
601 {
602 case VALIDATION_GOOD:
603 DBG1(DBG_CFG, "certificate status is good");
604 return TRUE;
605 case VALIDATION_REVOKED:
606 /* has already been logged */
607 return FALSE;
608 case VALIDATION_FAILED:
609 case VALIDATION_SKIPPED:
610 DBG1(DBG_CFG, "certificate status is not available");
611 break;
612 case VALIDATION_STALE:
613 DBG1(DBG_CFG, "certificate status is unknown, crl is stale");
614 break;
615 }
616 }
617 return TRUE;
618 }
619
620 METHOD(revocation_validator_t, destroy, void,
621 private_revocation_validator_t *this)
622 {
623 free(this);
624 }
625
626 /**
627 * See header
628 */
629 revocation_validator_t *revocation_validator_create()
630 {
631 private_revocation_validator_t *this;
632
633 INIT(this,
634 .public = {
635 .validator.validate = _validate,
636 .destroy = _destroy,
637 },
638 );
639
640 return &this->public;
641 }