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