15ca74bb6319d976862ce61a5421e8fe373c9868
[strongswan.git] / src / libstrongswan / plugins / x509 / x509_crl.c
1 /*
2 * Copyright (C) 2008 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 "x509_crl.h"
19
20 typedef struct private_x509_crl_t private_x509_crl_t;
21 typedef struct revoked_t revoked_t;
22
23 #include <debug.h>
24 #include <library.h>
25 #include <asn1/oid.h>
26 #include <asn1/asn1.h>
27 #include <asn1/asn1_parser.h>
28 #include <asn1/pem.h>
29 #include <credentials/certificates/x509.h>
30 #include <utils/linked_list.h>
31
32 /**
33 * entry for a revoked certificate
34 */
35 struct revoked_t {
36 /**
37 * serial of the revoked certificate
38 */
39 chunk_t serial;
40
41 /**
42 * date of revocation
43 */
44 time_t date;
45
46 /**
47 * reason for revocation
48 */
49 crl_reason_t reason;
50 };
51
52 /**
53 * private data of x509_crl
54 */
55 struct private_x509_crl_t {
56
57 /**
58 * public functions
59 */
60 x509_crl_t public;
61
62 /**
63 * X.509 crl encoding in ASN.1 DER format
64 */
65 chunk_t encoding;
66
67 /**
68 * X.509 crl body over which signature is computed
69 */
70 chunk_t tbsCertList;
71
72 /**
73 * Version of the X.509 crl
74 */
75 u_int version;
76
77 /**
78 * ID representing the crl issuer
79 */
80 identification_t *issuer;
81
82 /**
83 * CRL number
84 */
85 chunk_t crlNumber;
86
87 /**
88 * Time when the crl was generated
89 */
90 time_t thisUpdate;
91
92 /**
93 * Time when an update crl will be available
94 */
95 time_t nextUpdate;
96
97 /**
98 * list of revoked certificates as revoked_t
99 */
100 linked_list_t *revoked;
101
102 /**
103 * Authority Key Identifier
104 */
105 identification_t *authKeyIdentifier;
106
107 /**
108 * Authority Key Serial Number
109 */
110 chunk_t authKeySerialNumber;
111
112 /**
113 * Signature algorithm
114 */
115 int algorithm;
116
117 /**
118 * Signature
119 */
120 chunk_t signature;
121
122 /**
123 * reference counter
124 */
125 refcount_t ref;
126 };
127
128 /**
129 * from x509_cert
130 */
131 extern identification_t* x509_parse_authorityKeyIdentifier(
132 chunk_t blob, int level0,
133 chunk_t *authKeySerialNumber);
134
135 /**
136 * ASN.1 definition of an X.509 certificate revocation list
137 */
138 static const asn1Object_t crlObjects[] = {
139 { 0, "certificateList", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
140 { 1, "tbsCertList", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
141 { 2, "version", ASN1_INTEGER, ASN1_OPT |
142 ASN1_BODY }, /* 2 */
143 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 3 */
144 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 4 */
145 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 5 */
146 { 2, "thisUpdate", ASN1_EOC, ASN1_RAW }, /* 6 */
147 { 2, "nextUpdate", ASN1_EOC, ASN1_RAW }, /* 7 */
148 { 2, "revokedCertificates", ASN1_SEQUENCE, ASN1_OPT |
149 ASN1_LOOP }, /* 8 */
150 { 3, "certList", ASN1_SEQUENCE, ASN1_NONE }, /* 9 */
151 { 4, "userCertificate", ASN1_INTEGER, ASN1_BODY }, /* 10 */
152 { 4, "revocationDate", ASN1_EOC, ASN1_RAW }, /* 11 */
153 { 4, "crlEntryExtensions", ASN1_SEQUENCE, ASN1_OPT |
154 ASN1_LOOP }, /* 12 */
155 { 5, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 13 */
156 { 6, "extnID", ASN1_OID, ASN1_BODY }, /* 14 */
157 { 6, "critical", ASN1_BOOLEAN, ASN1_DEF |
158 ASN1_BODY }, /* 15 */
159 { 6, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 16 */
160 { 4, "end opt or loop", ASN1_EOC, ASN1_END }, /* 17 */
161 { 2, "end opt or loop", ASN1_EOC, ASN1_END }, /* 18 */
162 { 2, "optional extensions", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 19 */
163 { 3, "crlExtensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 20 */
164 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 21 */
165 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 22 */
166 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF |
167 ASN1_BODY }, /* 23 */
168 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 24 */
169 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 25 */
170 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 26 */
171 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 27 */
172 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY }, /* 28 */
173 { 0, "exit", ASN1_EOC, ASN1_EXIT }
174 };
175 #define CRL_OBJ_TBS_CERT_LIST 1
176 #define CRL_OBJ_VERSION 2
177 #define CRL_OBJ_SIG_ALG 4
178 #define CRL_OBJ_ISSUER 5
179 #define CRL_OBJ_THIS_UPDATE 6
180 #define CRL_OBJ_NEXT_UPDATE 7
181 #define CRL_OBJ_USER_CERTIFICATE 10
182 #define CRL_OBJ_REVOCATION_DATE 11
183 #define CRL_OBJ_CRL_ENTRY_EXTN_ID 14
184 #define CRL_OBJ_CRL_ENTRY_CRITICAL 15
185 #define CRL_OBJ_CRL_ENTRY_EXTN_VALUE 16
186 #define CRL_OBJ_EXTN_ID 22
187 #define CRL_OBJ_CRITICAL 23
188 #define CRL_OBJ_EXTN_VALUE 24
189 #define CRL_OBJ_ALGORITHM 27
190 #define CRL_OBJ_SIGNATURE 28
191
192 /**
193 * Parses an X.509 Certificate Revocation List (CRL)
194 */
195 static bool parse(private_x509_crl_t *this)
196 {
197 asn1_parser_t *parser;
198 chunk_t object;
199 chunk_t extnID;
200 chunk_t userCertificate = chunk_empty;
201 int objectID;
202 int sig_alg = OID_UNKNOWN;
203 bool success = FALSE;
204 bool critical;
205 revoked_t *revoked = NULL;
206
207 parser = asn1_parser_create(crlObjects, this->encoding);
208
209 while (parser->iterate(parser, &objectID, &object))
210 {
211 u_int level = parser->get_level(parser)+1;
212
213 switch (objectID)
214 {
215 case CRL_OBJ_TBS_CERT_LIST:
216 this->tbsCertList = object;
217 break;
218 case CRL_OBJ_VERSION:
219 this->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
220 DBG2(" v%d", this->version);
221 break;
222 case CRL_OBJ_SIG_ALG:
223 sig_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
224 break;
225 case CRL_OBJ_ISSUER:
226 this->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
227 DBG2(" '%D'", this->issuer);
228 break;
229 case CRL_OBJ_THIS_UPDATE:
230 this->thisUpdate = asn1_parse_time(object, level);
231 break;
232 case CRL_OBJ_NEXT_UPDATE:
233 this->nextUpdate = asn1_parse_time(object, level);
234 break;
235 case CRL_OBJ_USER_CERTIFICATE:
236 userCertificate = object;
237 break;
238 case CRL_OBJ_REVOCATION_DATE:
239 revoked = malloc_thing(revoked_t);
240 revoked->serial = userCertificate;
241 revoked->date = asn1_parse_time(object, level);
242 revoked->reason = CRL_UNSPECIFIED;
243 this->revoked->insert_last(this->revoked, (void *)revoked);
244 break;
245 case CRL_OBJ_CRL_ENTRY_EXTN_ID:
246 case CRL_OBJ_EXTN_ID:
247 extnID = object;
248 break;
249 case CRL_OBJ_CRL_ENTRY_CRITICAL:
250 case CRL_OBJ_CRITICAL:
251 critical = object.len && *object.ptr;
252 DBG2(" %s", critical ? "TRUE" : "FALSE");
253 break;
254 case CRL_OBJ_CRL_ENTRY_EXTN_VALUE:
255 case CRL_OBJ_EXTN_VALUE:
256 {
257 int extn_oid = asn1_known_oid(extnID);
258
259 if (revoked && extn_oid == OID_CRL_REASON_CODE)
260 {
261 if (*object.ptr == ASN1_ENUMERATED &&
262 asn1_length(&object) == 1)
263 {
264 revoked->reason = *object.ptr;
265 }
266 DBG2(" '%N'", crl_reason_names, revoked->reason);
267 }
268 else if (extn_oid == OID_AUTHORITY_KEY_ID)
269 {
270
271 this->authKeyIdentifier = x509_parse_authorityKeyIdentifier(object,
272 level, &this->authKeySerialNumber);
273 }
274 else if (extn_oid == OID_CRL_NUMBER)
275 {
276 if (!asn1_parse_simple_object(&object, ASN1_INTEGER,
277 level, "crlNumber"))
278 {
279 goto end;
280 }
281 this->crlNumber = object;
282 }
283 }
284 break;
285 case CRL_OBJ_ALGORITHM:
286 {
287 this->algorithm = asn1_parse_algorithmIdentifier(object, level, NULL);
288 if (this->algorithm != sig_alg)
289 {
290 DBG1(" signature algorithms do not agree");
291 goto end;
292 }
293 break;
294 }
295 case CRL_OBJ_SIGNATURE:
296 this->signature = object;
297 break;
298 default:
299 break;
300 }
301 }
302 success = parser->success(parser);
303
304 end:
305 parser->destroy(parser);
306 return success;
307 }
308
309 /**
310 * enumerator filter callback for create_enumerator
311 */
312 static bool filter(void *data, revoked_t **revoked, chunk_t *serial, void *p2,
313 time_t *date, void *p3, crl_reason_t *reason)
314 {
315 if (serial)
316 {
317 *serial = (*revoked)->serial;
318 }
319 if (date)
320 {
321 *date = (*revoked)->date;
322 }
323 if (reason)
324 {
325 *reason = (*revoked)->reason;
326 }
327 return TRUE;
328 }
329
330 /**
331 * Implementation of crl_t.get_serial.
332 */
333 static chunk_t get_serial(private_x509_crl_t *this)
334 {
335 return this->crlNumber;
336 }
337
338 /**
339 * Implementation of crl_t.get_authKeyIdentifier.
340 */
341 static identification_t* get_authKeyIdentifier(private_x509_crl_t *this)
342 {
343 return this->authKeyIdentifier;
344 }
345 /**
346 * Implementation of crl_t.create_enumerator.
347 */
348 static enumerator_t* create_enumerator(private_x509_crl_t *this)
349 {
350 return enumerator_create_filter(
351 this->revoked->create_enumerator(this->revoked),
352 (void*)filter, NULL, NULL);
353 }
354
355 /**
356 * Implementation of certificate_t.get_type
357 */
358 static certificate_type_t get_type(private_x509_crl_t *this)
359 {
360 return CERT_X509_CRL;
361 }
362
363 /**
364 * Implementation of certificate_t.get_issuer and get_subject
365 */
366 static identification_t* get_issuer(private_x509_crl_t *this)
367 {
368 return this->issuer;
369 }
370
371 /**
372 * Implementation of certificate_t.has_subject and has_issuer.
373 */
374 static id_match_t has_issuer(private_x509_crl_t *this, identification_t *issuer)
375 {
376 id_match_t match;
377
378 if (issuer->get_type(issuer) == ID_PUBKEY_SHA1)
379 {
380 if (this->authKeyIdentifier)
381 {
382 match = issuer->matches(issuer, this->authKeyIdentifier);
383 }
384 else
385 {
386 match = ID_MATCH_NONE;
387 }
388 }
389 else
390 {
391 match = this->issuer->matches(this->issuer, issuer);
392 }
393 return match;
394 }
395
396 /**
397 * Implementation of certificate_t.issued_by
398 */
399 static bool issued_by(private_x509_crl_t *this, certificate_t *issuer)
400 {
401 public_key_t *key;
402 signature_scheme_t scheme;
403 bool valid;
404 x509_t *x509 = (x509_t*)issuer;
405
406 /* check if issuer is an X.509 CA certificate */
407 if (issuer->get_type(issuer) != CERT_X509)
408 {
409 return FALSE;
410 }
411 if (!(x509->get_flags(x509) & X509_CA))
412 {
413 return FALSE;
414 }
415
416 /* get the public key of the issuer */
417 key = issuer->get_public_key(issuer);
418
419 /* compare keyIdentifiers if available, otherwise use DNs */
420 if (this->authKeyIdentifier && key)
421 {
422 identification_t *subjectKeyIdentifier = key->get_id(key, ID_PUBKEY_SHA1);
423
424 if (!subjectKeyIdentifier->equals(subjectKeyIdentifier,
425 this->authKeyIdentifier))
426 {
427 return FALSE;
428 }
429 }
430 else
431 {
432 if (!this->issuer->equals(this->issuer, issuer->get_subject(issuer)))
433 {
434 return FALSE;
435 }
436 }
437 /* TODO: generic OID to scheme mapper? */
438 switch (this->algorithm)
439 {
440 case OID_MD5_WITH_RSA:
441 scheme = SIGN_RSA_EMSA_PKCS1_MD5;
442 break;
443 case OID_SHA1_WITH_RSA:
444 scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
445 break;
446 case OID_SHA256_WITH_RSA:
447 scheme = SIGN_RSA_EMSA_PKCS1_SHA256;
448 break;
449 case OID_SHA384_WITH_RSA:
450 scheme = SIGN_RSA_EMSA_PKCS1_SHA384;
451 break;
452 case OID_SHA512_WITH_RSA:
453 scheme = SIGN_RSA_EMSA_PKCS1_SHA512;
454 break;
455 default:
456 return FALSE;
457 }
458 if (key == NULL)
459 {
460 return FALSE;
461 }
462 valid = key->verify(key, scheme, this->tbsCertList, this->signature);
463 key->destroy(key);
464 return valid;
465 }
466
467 /**
468 * Implementation of certificate_t.get_public_key
469 */
470 static public_key_t* get_public_key(private_x509_crl_t *this)
471 {
472 return NULL;
473 }
474
475 /**
476 * Implementation of certificate_t.asdf
477 */
478 static private_x509_crl_t* get_ref(private_x509_crl_t *this)
479 {
480 ref_get(&this->ref);
481 return this;
482 }
483
484 /**
485 * Implementation of certificate_t.get_validity.
486 */
487 static bool get_validity(private_x509_crl_t *this, time_t *when,
488 time_t *not_before, time_t *not_after)
489 {
490 time_t t;
491
492 if (when)
493 {
494 t = *when;
495 }
496 else
497 {
498 t = time(NULL);
499 }
500 if (not_before)
501 {
502 *not_before = this->thisUpdate;
503 }
504 if (not_after)
505 {
506 *not_after = this->nextUpdate;
507 }
508 return (t <= this->nextUpdate);
509 }
510
511 /**
512 * Implementation of certificate_t.is_newer.
513 */
514 static bool is_newer(private_x509_crl_t *this, crl_t *that)
515 {
516 chunk_t that_crlNumber = that->get_serial(that);
517 bool new;
518
519 /* compare crlNumbers if available - otherwise use thisUpdate */
520 if (this->crlNumber.ptr != NULL && that_crlNumber.ptr != NULL)
521 {
522 new = chunk_compare(this->crlNumber, that_crlNumber) > 0;
523 DBG1(" crl #%#B is %s - existing crl #%#B %s",
524 &this->crlNumber, new ? "newer":"not newer",
525 &that_crlNumber, new ? "replaced":"retained");
526 }
527 else
528 {
529 certificate_t *this_cert = &this->public.crl.certificate;
530 certificate_t *that_cert = &that->certificate;
531
532 time_t this_update, that_update, now = time(NULL);
533
534 this_cert->get_validity(this_cert, &now, &this_update, NULL);
535 that_cert->get_validity(that_cert, &now, &that_update, NULL);
536 new = this_update > that_update;
537 DBG1(" crl from %#T is %s - existing crl from %#T %s",
538 &this_update, FALSE, new ? "newer":"not newer",
539 &that_update, FALSE, new ? "replaced":"retained");
540 }
541 return new;
542 }
543
544 /**
545 * Implementation of certificate_t.get_encoding.
546 */
547 static chunk_t get_encoding(private_x509_crl_t *this)
548 {
549 return chunk_clone(this->encoding);
550 }
551
552 /**
553 * Implementation of certificate_t.equals.
554 */
555 static bool equals(private_x509_crl_t *this, certificate_t *other)
556 {
557 chunk_t encoding;
558 bool equal;
559
560 if ((certificate_t*)this == other)
561 {
562 return TRUE;
563 }
564 if (other->equals == (void*)equals)
565 { /* skip allocation if we have the same implementation */
566 return chunk_equals(this->encoding, ((private_x509_crl_t*)other)->encoding);
567 }
568 encoding = other->get_encoding(other);
569 equal = chunk_equals(this->encoding, encoding);
570 free(encoding.ptr);
571 return equal;
572 }
573
574 /**
575 * Implementation of certificate_t.destroy
576 */
577 static void destroy(private_x509_crl_t *this)
578 {
579 if (ref_put(&this->ref))
580 {
581 this->revoked->destroy_function(this->revoked, free);
582 DESTROY_IF(this->issuer);
583 DESTROY_IF(this->authKeyIdentifier);
584 free(this->encoding.ptr);
585 free(this);
586 }
587 }
588
589 /**
590 * create an empty but initialized X.509 crl
591 */
592 static private_x509_crl_t* create_empty(void)
593 {
594 private_x509_crl_t *this = malloc_thing(private_x509_crl_t);
595
596 this->public.crl.get_serial = (chunk_t (*)(crl_t*))get_serial;
597 this->public.crl.get_authKeyIdentifier = (identification_t* (*)(crl_t*))get_authKeyIdentifier;
598 this->public.crl.create_enumerator = (enumerator_t* (*)(crl_t*))create_enumerator;
599 this->public.crl.certificate.get_type = (certificate_type_t (*)(certificate_t *this))get_type;
600 this->public.crl.certificate.get_subject = (identification_t* (*)(certificate_t *this))get_issuer;
601 this->public.crl.certificate.get_issuer = (identification_t* (*)(certificate_t *this))get_issuer;
602 this->public.crl.certificate.has_subject = (id_match_t (*)(certificate_t*, identification_t *subject))has_issuer;
603 this->public.crl.certificate.has_issuer = (id_match_t (*)(certificate_t*, identification_t *issuer))has_issuer;
604 this->public.crl.certificate.issued_by = (bool (*)(certificate_t *this, certificate_t *issuer))issued_by;
605 this->public.crl.certificate.get_public_key = (public_key_t* (*)(certificate_t *this))get_public_key;
606 this->public.crl.certificate.get_validity = (bool (*)(certificate_t*, time_t *when, time_t *, time_t*))get_validity;
607 this->public.crl.certificate.is_newer = (bool (*)(certificate_t*,certificate_t*))is_newer;
608 this->public.crl.certificate.get_encoding = (chunk_t (*)(certificate_t*))get_encoding;
609 this->public.crl.certificate.equals = (bool (*)(certificate_t*, certificate_t *other))equals;
610 this->public.crl.certificate.get_ref = (certificate_t* (*)(certificate_t *this))get_ref;
611 this->public.crl.certificate.destroy = (void (*)(certificate_t *this))destroy;
612
613 this->encoding = chunk_empty;
614 this->tbsCertList = chunk_empty;
615 this->issuer = NULL;
616 this->crlNumber = chunk_empty;
617 this->revoked = linked_list_create();
618 this->authKeyIdentifier = NULL;
619 this->authKeySerialNumber = chunk_empty;
620 this->ref = 1;
621
622 return this;
623 }
624
625 /**
626 * create an X.509 crl from a chunk
627 */
628 static private_x509_crl_t* create_from_chunk(chunk_t chunk)
629 {
630 private_x509_crl_t *this = create_empty();
631
632 this->encoding = chunk;
633 if (!parse(this))
634 {
635 destroy(this);
636 return NULL;
637 }
638 return this;
639 }
640
641 /**
642 * create an X.509 crl from a file
643 */
644 static private_x509_crl_t* create_from_file(char *path)
645 {
646 bool pgp = FALSE;
647 chunk_t chunk;
648 private_x509_crl_t *this;
649
650 if (!pem_asn1_load_file(path, NULL, &chunk, &pgp))
651 {
652 return NULL;
653 }
654
655 this = create_from_chunk(chunk);
656
657 if (this == NULL)
658 {
659 DBG1(" could not parse loaded crl file '%s'",path);
660 return NULL;
661 }
662 DBG1(" loaded crl file '%s'", path);
663 return this;
664 }
665
666 typedef struct private_builder_t private_builder_t;
667 /**
668 * Builder implementation for certificate loading
669 */
670 struct private_builder_t {
671 /** implements the builder interface */
672 builder_t public;
673 /** loaded CRL */
674 private_x509_crl_t *crl;
675 };
676
677 /**
678 * Implementation of builder_t.build
679 */
680 static private_x509_crl_t *build(private_builder_t *this)
681 {
682 private_x509_crl_t *crl = this->crl;
683
684 free(this);
685 return crl;
686 }
687
688 /**
689 * Implementation of builder_t.add
690 */
691 static void add(private_builder_t *this, builder_part_t part, ...)
692 {
693 va_list args;
694
695 if (this->crl)
696 {
697 DBG1("ignoring surplus build part %N", builder_part_names, part);
698 return;
699 }
700
701 va_start(args, part);
702 switch (part)
703 {
704 case BUILD_FROM_FILE:
705 this->crl = create_from_file(va_arg(args, char*));
706 break;
707 case BUILD_BLOB_ASN1_DER:
708 {
709 this->crl = create_from_chunk(va_arg(args, chunk_t));
710 break;
711 }
712 default:
713 DBG1("ignoring unsupported build part %N", builder_part_names, part);
714 break;
715 }
716 va_end(args);
717 }
718
719 /**
720 * Builder construction function
721 */
722 builder_t *x509_crl_builder(certificate_type_t type)
723 {
724 private_builder_t *this;
725
726 if (type != CERT_X509_CRL)
727 {
728 return NULL;
729 }
730
731 this = malloc_thing(private_builder_t);
732
733 this->crl = NULL;
734 this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
735 this->public.build = (void*(*)(builder_t *this))build;
736
737 return &this->public;
738 }
739