support of ECDSA signatures for all certificate types
[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 case OID_ECDSA_WITH_SHA1:
456 scheme = SIGN_ECDSA_WITH_SHA1;
457 break;
458 default:
459 return FALSE;
460 }
461 if (key == NULL)
462 {
463 return FALSE;
464 }
465 valid = key->verify(key, scheme, this->tbsCertList, this->signature);
466 key->destroy(key);
467 return valid;
468 }
469
470 /**
471 * Implementation of certificate_t.get_public_key
472 */
473 static public_key_t* get_public_key(private_x509_crl_t *this)
474 {
475 return NULL;
476 }
477
478 /**
479 * Implementation of certificate_t.asdf
480 */
481 static private_x509_crl_t* get_ref(private_x509_crl_t *this)
482 {
483 ref_get(&this->ref);
484 return this;
485 }
486
487 /**
488 * Implementation of certificate_t.get_validity.
489 */
490 static bool get_validity(private_x509_crl_t *this, time_t *when,
491 time_t *not_before, time_t *not_after)
492 {
493 time_t t;
494
495 if (when)
496 {
497 t = *when;
498 }
499 else
500 {
501 t = time(NULL);
502 }
503 if (not_before)
504 {
505 *not_before = this->thisUpdate;
506 }
507 if (not_after)
508 {
509 *not_after = this->nextUpdate;
510 }
511 return (t <= this->nextUpdate);
512 }
513
514 /**
515 * Implementation of certificate_t.is_newer.
516 */
517 static bool is_newer(private_x509_crl_t *this, crl_t *that)
518 {
519 chunk_t that_crlNumber = that->get_serial(that);
520 bool new;
521
522 /* compare crlNumbers if available - otherwise use thisUpdate */
523 if (this->crlNumber.ptr != NULL && that_crlNumber.ptr != NULL)
524 {
525 new = chunk_compare(this->crlNumber, that_crlNumber) > 0;
526 DBG1(" crl #%#B is %s - existing crl #%#B %s",
527 &this->crlNumber, new ? "newer":"not newer",
528 &that_crlNumber, new ? "replaced":"retained");
529 }
530 else
531 {
532 certificate_t *this_cert = &this->public.crl.certificate;
533 certificate_t *that_cert = &that->certificate;
534
535 time_t this_update, that_update, now = time(NULL);
536
537 this_cert->get_validity(this_cert, &now, &this_update, NULL);
538 that_cert->get_validity(that_cert, &now, &that_update, NULL);
539 new = this_update > that_update;
540 DBG1(" crl from %#T is %s - existing crl from %#T %s",
541 &this_update, FALSE, new ? "newer":"not newer",
542 &that_update, FALSE, new ? "replaced":"retained");
543 }
544 return new;
545 }
546
547 /**
548 * Implementation of certificate_t.get_encoding.
549 */
550 static chunk_t get_encoding(private_x509_crl_t *this)
551 {
552 return chunk_clone(this->encoding);
553 }
554
555 /**
556 * Implementation of certificate_t.equals.
557 */
558 static bool equals(private_x509_crl_t *this, certificate_t *other)
559 {
560 chunk_t encoding;
561 bool equal;
562
563 if ((certificate_t*)this == other)
564 {
565 return TRUE;
566 }
567 if (other->equals == (void*)equals)
568 { /* skip allocation if we have the same implementation */
569 return chunk_equals(this->encoding, ((private_x509_crl_t*)other)->encoding);
570 }
571 encoding = other->get_encoding(other);
572 equal = chunk_equals(this->encoding, encoding);
573 free(encoding.ptr);
574 return equal;
575 }
576
577 /**
578 * Implementation of certificate_t.destroy
579 */
580 static void destroy(private_x509_crl_t *this)
581 {
582 if (ref_put(&this->ref))
583 {
584 this->revoked->destroy_function(this->revoked, free);
585 DESTROY_IF(this->issuer);
586 DESTROY_IF(this->authKeyIdentifier);
587 free(this->encoding.ptr);
588 free(this);
589 }
590 }
591
592 /**
593 * create an empty but initialized X.509 crl
594 */
595 static private_x509_crl_t* create_empty(void)
596 {
597 private_x509_crl_t *this = malloc_thing(private_x509_crl_t);
598
599 this->public.crl.get_serial = (chunk_t (*)(crl_t*))get_serial;
600 this->public.crl.get_authKeyIdentifier = (identification_t* (*)(crl_t*))get_authKeyIdentifier;
601 this->public.crl.create_enumerator = (enumerator_t* (*)(crl_t*))create_enumerator;
602 this->public.crl.certificate.get_type = (certificate_type_t (*)(certificate_t *this))get_type;
603 this->public.crl.certificate.get_subject = (identification_t* (*)(certificate_t *this))get_issuer;
604 this->public.crl.certificate.get_issuer = (identification_t* (*)(certificate_t *this))get_issuer;
605 this->public.crl.certificate.has_subject = (id_match_t (*)(certificate_t*, identification_t *subject))has_issuer;
606 this->public.crl.certificate.has_issuer = (id_match_t (*)(certificate_t*, identification_t *issuer))has_issuer;
607 this->public.crl.certificate.issued_by = (bool (*)(certificate_t *this, certificate_t *issuer))issued_by;
608 this->public.crl.certificate.get_public_key = (public_key_t* (*)(certificate_t *this))get_public_key;
609 this->public.crl.certificate.get_validity = (bool (*)(certificate_t*, time_t *when, time_t *, time_t*))get_validity;
610 this->public.crl.certificate.is_newer = (bool (*)(certificate_t*,certificate_t*))is_newer;
611 this->public.crl.certificate.get_encoding = (chunk_t (*)(certificate_t*))get_encoding;
612 this->public.crl.certificate.equals = (bool (*)(certificate_t*, certificate_t *other))equals;
613 this->public.crl.certificate.get_ref = (certificate_t* (*)(certificate_t *this))get_ref;
614 this->public.crl.certificate.destroy = (void (*)(certificate_t *this))destroy;
615
616 this->encoding = chunk_empty;
617 this->tbsCertList = chunk_empty;
618 this->issuer = NULL;
619 this->crlNumber = chunk_empty;
620 this->revoked = linked_list_create();
621 this->authKeyIdentifier = NULL;
622 this->authKeySerialNumber = chunk_empty;
623 this->ref = 1;
624
625 return this;
626 }
627
628 /**
629 * create an X.509 crl from a chunk
630 */
631 static private_x509_crl_t* create_from_chunk(chunk_t chunk)
632 {
633 private_x509_crl_t *this = create_empty();
634
635 this->encoding = chunk;
636 if (!parse(this))
637 {
638 destroy(this);
639 return NULL;
640 }
641 return this;
642 }
643
644 /**
645 * create an X.509 crl from a file
646 */
647 static private_x509_crl_t* create_from_file(char *path)
648 {
649 bool pgp = FALSE;
650 chunk_t chunk;
651 private_x509_crl_t *this;
652
653 if (!pem_asn1_load_file(path, NULL, &chunk, &pgp))
654 {
655 return NULL;
656 }
657
658 this = create_from_chunk(chunk);
659
660 if (this == NULL)
661 {
662 DBG1(" could not parse loaded crl file '%s'",path);
663 return NULL;
664 }
665 DBG1(" loaded crl file '%s'", path);
666 return this;
667 }
668
669 typedef struct private_builder_t private_builder_t;
670 /**
671 * Builder implementation for certificate loading
672 */
673 struct private_builder_t {
674 /** implements the builder interface */
675 builder_t public;
676 /** loaded CRL */
677 private_x509_crl_t *crl;
678 };
679
680 /**
681 * Implementation of builder_t.build
682 */
683 static private_x509_crl_t *build(private_builder_t *this)
684 {
685 private_x509_crl_t *crl = this->crl;
686
687 free(this);
688 return crl;
689 }
690
691 /**
692 * Implementation of builder_t.add
693 */
694 static void add(private_builder_t *this, builder_part_t part, ...)
695 {
696 va_list args;
697
698 if (this->crl)
699 {
700 DBG1("ignoring surplus build part %N", builder_part_names, part);
701 return;
702 }
703
704 va_start(args, part);
705 switch (part)
706 {
707 case BUILD_FROM_FILE:
708 this->crl = create_from_file(va_arg(args, char*));
709 break;
710 case BUILD_BLOB_ASN1_DER:
711 {
712 this->crl = create_from_chunk(va_arg(args, chunk_t));
713 break;
714 }
715 default:
716 DBG1("ignoring unsupported build part %N", builder_part_names, part);
717 break;
718 }
719 va_end(args);
720 }
721
722 /**
723 * Builder construction function
724 */
725 builder_t *x509_crl_builder(certificate_type_t type)
726 {
727 private_builder_t *this;
728
729 if (type != CERT_X509_CRL)
730 {
731 return NULL;
732 }
733
734 this = malloc_thing(private_builder_t);
735
736 this->crl = NULL;
737 this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
738 this->public.build = (void*(*)(builder_t *this))build;
739
740 return &this->public;
741 }
742