Migrated x509_crl_t to INIT/METHOD macros
[strongswan.git] / src / libstrongswan / plugins / x509 / x509_crl.c
1 /*
2 * Copyright (C) 2008-2009 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
16 #include "x509_crl.h"
17
18 typedef struct private_x509_crl_t private_x509_crl_t;
19 typedef struct revoked_t revoked_t;
20
21 #include <time.h>
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 <credentials/certificates/x509.h>
29 #include <utils/linked_list.h>
30
31 /**
32 * entry for a revoked certificate
33 */
34 struct revoked_t {
35 /**
36 * serial of the revoked certificate
37 */
38 chunk_t serial;
39
40 /**
41 * date of revocation
42 */
43 time_t date;
44
45 /**
46 * reason for revocation
47 */
48 crl_reason_t reason;
49 };
50
51 /**
52 * private data of x509_crl
53 */
54 struct private_x509_crl_t {
55
56 /**
57 * public functions
58 */
59 x509_crl_t public;
60
61 /**
62 * X.509 crl encoding in ASN.1 DER format
63 */
64 chunk_t encoding;
65
66 /**
67 * X.509 crl body over which signature is computed
68 */
69 chunk_t tbsCertList;
70
71 /**
72 * Version of the X.509 crl
73 */
74 u_int version;
75
76 /**
77 * ID representing the crl issuer
78 */
79 identification_t *issuer;
80
81 /**
82 * CRL number
83 */
84 chunk_t crlNumber;
85
86 /**
87 * Time when the crl was generated
88 */
89 time_t thisUpdate;
90
91 /**
92 * Time when an update crl will be available
93 */
94 time_t nextUpdate;
95
96 /**
97 * list of revoked certificates as revoked_t
98 */
99 linked_list_t *revoked;
100
101 /**
102 * Authority Key Identifier
103 */
104 chunk_t authKeyIdentifier;
105
106 /**
107 * Authority Key Serial Number
108 */
109 chunk_t authKeySerialNumber;
110
111 /**
112 * Signature algorithm
113 */
114 int algorithm;
115
116 /**
117 * Signature
118 */
119 chunk_t signature;
120
121 /**
122 * reference counter
123 */
124 refcount_t ref;
125 };
126
127 /**
128 * from x509_cert
129 */
130 extern chunk_t x509_parse_authorityKeyIdentifier(
131 chunk_t blob, int level0,
132 chunk_t *authKeySerialNumber);
133
134 /**
135 * ASN.1 definition of an X.509 certificate revocation list
136 */
137 static const asn1Object_t crlObjects[] = {
138 { 0, "certificateList", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
139 { 1, "tbsCertList", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
140 { 2, "version", ASN1_INTEGER, ASN1_OPT |
141 ASN1_BODY }, /* 2 */
142 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 3 */
143 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 4 */
144 { 2, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 5 */
145 { 2, "thisUpdate", ASN1_EOC, ASN1_RAW }, /* 6 */
146 { 2, "nextUpdate", ASN1_EOC, ASN1_RAW }, /* 7 */
147 { 2, "revokedCertificates", ASN1_SEQUENCE, ASN1_OPT |
148 ASN1_LOOP }, /* 8 */
149 { 3, "certList", ASN1_SEQUENCE, ASN1_NONE }, /* 9 */
150 { 4, "userCertificate", ASN1_INTEGER, ASN1_BODY }, /* 10 */
151 { 4, "revocationDate", ASN1_EOC, ASN1_RAW }, /* 11 */
152 { 4, "crlEntryExtensions", ASN1_SEQUENCE, ASN1_OPT |
153 ASN1_LOOP }, /* 12 */
154 { 5, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 13 */
155 { 6, "extnID", ASN1_OID, ASN1_BODY }, /* 14 */
156 { 6, "critical", ASN1_BOOLEAN, ASN1_DEF |
157 ASN1_BODY }, /* 15 */
158 { 6, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 16 */
159 { 4, "end opt or loop", ASN1_EOC, ASN1_END }, /* 17 */
160 { 2, "end opt or loop", ASN1_EOC, ASN1_END }, /* 18 */
161 { 2, "optional extensions", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 19 */
162 { 3, "crlExtensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 20 */
163 { 4, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 21 */
164 { 5, "extnID", ASN1_OID, ASN1_BODY }, /* 22 */
165 { 5, "critical", ASN1_BOOLEAN, ASN1_DEF |
166 ASN1_BODY }, /* 23 */
167 { 5, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 24 */
168 { 3, "end loop", ASN1_EOC, ASN1_END }, /* 25 */
169 { 2, "end opt", ASN1_EOC, ASN1_END }, /* 26 */
170 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 27 */
171 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY }, /* 28 */
172 { 0, "exit", ASN1_EOC, ASN1_EXIT }
173 };
174 #define CRL_OBJ_TBS_CERT_LIST 1
175 #define CRL_OBJ_VERSION 2
176 #define CRL_OBJ_SIG_ALG 4
177 #define CRL_OBJ_ISSUER 5
178 #define CRL_OBJ_THIS_UPDATE 6
179 #define CRL_OBJ_NEXT_UPDATE 7
180 #define CRL_OBJ_USER_CERTIFICATE 10
181 #define CRL_OBJ_REVOCATION_DATE 11
182 #define CRL_OBJ_CRL_ENTRY_EXTN_ID 14
183 #define CRL_OBJ_CRL_ENTRY_CRITICAL 15
184 #define CRL_OBJ_CRL_ENTRY_EXTN_VALUE 16
185 #define CRL_OBJ_EXTN_ID 22
186 #define CRL_OBJ_CRITICAL 23
187 #define CRL_OBJ_EXTN_VALUE 24
188 #define CRL_OBJ_ALGORITHM 27
189 #define CRL_OBJ_SIGNATURE 28
190
191 /**
192 * Parses an X.509 Certificate Revocation List (CRL)
193 */
194 static bool parse(private_x509_crl_t *this)
195 {
196 asn1_parser_t *parser;
197 chunk_t object;
198 chunk_t extnID;
199 chunk_t userCertificate = chunk_empty;
200 int objectID;
201 int sig_alg = OID_UNKNOWN;
202 bool success = FALSE;
203 bool critical;
204 revoked_t *revoked = NULL;
205
206 parser = asn1_parser_create(crlObjects, this->encoding);
207
208 while (parser->iterate(parser, &objectID, &object))
209 {
210 u_int level = parser->get_level(parser)+1;
211
212 switch (objectID)
213 {
214 case CRL_OBJ_TBS_CERT_LIST:
215 this->tbsCertList = object;
216 break;
217 case CRL_OBJ_VERSION:
218 this->version = (object.len) ? (1+(u_int)*object.ptr) : 1;
219 DBG2(DBG_LIB, " v%d", this->version);
220 break;
221 case CRL_OBJ_SIG_ALG:
222 sig_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
223 break;
224 case CRL_OBJ_ISSUER:
225 this->issuer = identification_create_from_encoding(ID_DER_ASN1_DN, object);
226 DBG2(DBG_LIB, " '%Y'", this->issuer);
227 break;
228 case CRL_OBJ_THIS_UPDATE:
229 this->thisUpdate = asn1_parse_time(object, level);
230 break;
231 case CRL_OBJ_NEXT_UPDATE:
232 this->nextUpdate = asn1_parse_time(object, level);
233 break;
234 case CRL_OBJ_USER_CERTIFICATE:
235 userCertificate = object;
236 break;
237 case CRL_OBJ_REVOCATION_DATE:
238 revoked = malloc_thing(revoked_t);
239 revoked->serial = userCertificate;
240 revoked->date = asn1_parse_time(object, level);
241 revoked->reason = CRL_REASON_UNSPECIFIED;
242 this->revoked->insert_last(this->revoked, (void *)revoked);
243 break;
244 case CRL_OBJ_CRL_ENTRY_EXTN_ID:
245 case CRL_OBJ_EXTN_ID:
246 extnID = object;
247 break;
248 case CRL_OBJ_CRL_ENTRY_CRITICAL:
249 case CRL_OBJ_CRITICAL:
250 critical = object.len && *object.ptr;
251 DBG2(DBG_LIB, " %s", critical ? "TRUE" : "FALSE");
252 break;
253 case CRL_OBJ_CRL_ENTRY_EXTN_VALUE:
254 case CRL_OBJ_EXTN_VALUE:
255 {
256 int extn_oid = asn1_known_oid(extnID);
257
258 if (revoked && extn_oid == OID_CRL_REASON_CODE)
259 {
260 if (*object.ptr == ASN1_ENUMERATED &&
261 asn1_length(&object) == 1)
262 {
263 revoked->reason = *object.ptr;
264 }
265 DBG2(DBG_LIB, " '%N'", crl_reason_names,
266 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(DBG_LIB, " 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 METHOD(crl_t, get_serial, chunk_t,
331 private_x509_crl_t *this)
332 {
333 return this->crlNumber;
334 }
335
336 METHOD(crl_t, get_authKeyIdentifier, chunk_t,
337 private_x509_crl_t *this)
338 {
339 return this->authKeyIdentifier;
340 }
341
342 METHOD(crl_t, create_enumerator, enumerator_t*,
343 private_x509_crl_t *this)
344 {
345 return enumerator_create_filter(
346 this->revoked->create_enumerator(this->revoked),
347 (void*)filter, NULL, NULL);
348 }
349
350 METHOD(certificate_t, get_type, certificate_type_t,
351 private_x509_crl_t *this)
352 {
353 return CERT_X509_CRL;
354 }
355
356 METHOD(certificate_t, get_issuer, identification_t*,
357 private_x509_crl_t *this)
358 {
359 return this->issuer;
360 }
361
362 METHOD(certificate_t, has_issuer, id_match_t,
363 private_x509_crl_t *this, identification_t *issuer)
364 {
365 if (issuer->get_type(issuer) == ID_KEY_ID && this->authKeyIdentifier.ptr &&
366 chunk_equals(this->authKeyIdentifier, issuer->get_encoding(issuer)))
367 {
368 return ID_MATCH_PERFECT;
369 }
370 return this->issuer->matches(this->issuer, issuer);
371 }
372
373 METHOD(certificate_t, issued_by, bool,
374 private_x509_crl_t *this, certificate_t *issuer)
375 {
376 public_key_t *key;
377 signature_scheme_t scheme;
378 bool valid;
379 x509_t *x509 = (x509_t*)issuer;
380
381 /* check if issuer is an X.509 CA certificate */
382 if (issuer->get_type(issuer) != CERT_X509)
383 {
384 return FALSE;
385 }
386 if (!(x509->get_flags(x509) & X509_CA))
387 {
388 return FALSE;
389 }
390
391 /* get the public key of the issuer */
392 key = issuer->get_public_key(issuer);
393
394 /* compare keyIdentifiers if available, otherwise use DNs */
395 if (this->authKeyIdentifier.ptr && key)
396 {
397 chunk_t fingerprint;
398
399 if (!key->get_fingerprint(key, KEY_ID_PUBKEY_SHA1, &fingerprint) ||
400 !chunk_equals(fingerprint, this->authKeyIdentifier))
401 {
402 return FALSE;
403 }
404 }
405 else
406 {
407 if (!this->issuer->equals(this->issuer, issuer->get_subject(issuer)))
408 {
409 return FALSE;
410 }
411 }
412
413 /* determine signature scheme */
414 scheme = signature_scheme_from_oid(this->algorithm);
415
416 if (scheme == SIGN_UNKNOWN || key == NULL)
417 {
418 return FALSE;
419 }
420 valid = key->verify(key, scheme, this->tbsCertList, this->signature);
421 key->destroy(key);
422 return valid;
423 }
424
425 METHOD(certificate_t, get_public_key, public_key_t*,
426 private_x509_crl_t *this)
427 {
428 return NULL;
429 }
430
431 METHOD(certificate_t, get_ref, certificate_t*,
432 private_x509_crl_t *this)
433 {
434 ref_get(&this->ref);
435 return &this->public.crl.certificate;
436 }
437
438 METHOD(certificate_t, get_validity, bool,
439 private_x509_crl_t *this, time_t *when,
440 time_t *not_before, time_t *not_after)
441 {
442 time_t t = when ? *when : time(NULL);
443
444 if (not_before)
445 {
446 *not_before = this->thisUpdate;
447 }
448 if (not_after)
449 {
450 *not_after = this->nextUpdate;
451 }
452 return (t <= this->nextUpdate);
453 }
454
455 METHOD(certificate_t, is_newer, bool,
456 private_x509_crl_t *this, certificate_t *other)
457 {
458 chunk_t other_crlNumber = chunk_empty;
459 bool new;
460
461 if (other->get_type(other) == CERT_X509_CRL)
462 {
463 crl_t *crl = (crl_t*)other;
464 other_crlNumber = crl->get_serial(crl);
465 }
466
467 /* compare crlNumbers if available - otherwise use thisUpdate */
468 if (this->crlNumber.ptr != NULL && other_crlNumber.ptr != NULL)
469 {
470 new = chunk_compare(this->crlNumber, other_crlNumber) > 0;
471 DBG1(DBG_LIB, " crl #%#B is %s - existing crl #%#B %s",
472 &this->crlNumber, new ? "newer":"not newer",
473 &other_crlNumber, new ? "replaced":"retained");
474 }
475 else
476 {
477 time_t this_update, that_update, now = time(NULL);
478
479 get_validity(this, &now, &this_update, NULL);
480 other->get_validity(other, &now, &that_update, NULL);
481 new = this_update > that_update;
482 DBG1(DBG_LIB, " crl from %T is %s - existing crl from %T %s",
483 &this_update, FALSE, new ? "newer":"not newer",
484 &that_update, FALSE, new ? "replaced":"retained");
485 }
486 return new;
487 }
488
489 METHOD(certificate_t, get_encoding, chunk_t,
490 private_x509_crl_t *this)
491 {
492 return chunk_clone(this->encoding);
493 }
494
495 METHOD(certificate_t, equals, bool,
496 private_x509_crl_t *this, certificate_t *other)
497 {
498 chunk_t encoding;
499 bool equal;
500
501 if ((certificate_t*)this == other)
502 {
503 return TRUE;
504 }
505 if (other->equals == (void*)equals)
506 { /* skip allocation if we have the same implementation */
507 return chunk_equals(this->encoding, ((private_x509_crl_t*)other)->encoding);
508 }
509 encoding = other->get_encoding(other);
510 equal = chunk_equals(this->encoding, encoding);
511 free(encoding.ptr);
512 return equal;
513 }
514
515 METHOD(certificate_t, destroy, void,
516 private_x509_crl_t *this)
517 {
518 if (ref_put(&this->ref))
519 {
520 this->revoked->destroy_function(this->revoked, free);
521 DESTROY_IF(this->issuer);
522 free(this->authKeyIdentifier.ptr);
523 free(this->encoding.ptr);
524 free(this);
525 }
526 }
527
528 /**
529 * create an empty but initialized X.509 crl
530 */
531 static private_x509_crl_t* create_empty(void)
532 {
533 private_x509_crl_t *this;
534
535 INIT(this,
536 .public = {
537 .crl = {
538 .certificate = {
539 .get_type = _get_type,
540 .get_subject = _get_issuer,
541 .get_issuer = _get_issuer,
542 .has_subject = _has_issuer,
543 .has_issuer = _has_issuer,
544 .issued_by = _issued_by,
545 .get_public_key = _get_public_key,
546 .get_validity = _get_validity,
547 .is_newer = _is_newer,
548 .get_encoding = _get_encoding,
549 .equals = _equals,
550 .get_ref = _get_ref,
551 .destroy = _destroy,
552 },
553 .get_serial = _get_serial,
554 .get_authKeyIdentifier = _get_authKeyIdentifier,
555 .create_enumerator = _create_enumerator,
556 },
557 },
558 .revoked = linked_list_create(),
559 .ref = 1,
560 );
561 return this;
562 }
563
564 /**
565 * See header.
566 */
567 x509_crl_t *x509_crl_load(certificate_type_t type, va_list args)
568 {
569 chunk_t blob = chunk_empty;
570
571 while (TRUE)
572 {
573 switch (va_arg(args, builder_part_t))
574 {
575 case BUILD_BLOB_ASN1_DER:
576 blob = va_arg(args, chunk_t);
577 continue;
578 case BUILD_END:
579 break;
580 default:
581 return NULL;
582 }
583 break;
584 }
585 if (blob.ptr)
586 {
587 private_x509_crl_t *crl = create_empty();
588
589 crl->encoding = chunk_clone(blob);
590 if (parse(crl))
591 {
592 return &crl->public;
593 }
594 destroy(crl);
595 }
596 return NULL;
597 };
598