fixed destroy() bug
[strongswan.git] / src / libstrongswan / crypto / ac.c
1 /**
2 * @file ac.c
3 *
4 * @brief Implementation of x509ac_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2002 Ueli Galizzi, Ariane Seiler
10 * Copyright (C) 2003 Martin Berner, Lukas Suter
11 * Copyright (C) 2007 Andreas Steffen, Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <library.h>
25 #include <debug.h>
26
27 #include <asn1/asn1.h>
28 #include <utils/identification.h>
29 #include <utils/linked_list.h>
30
31 #include "ac.h"
32
33 typedef struct private_x509ac_t private_x509ac_t;
34
35 /**
36 * Private data of a x509ac_t object.
37 */
38 struct private_x509ac_t {
39 /**
40 * Public interface for this attribute certificate.
41 */
42 x509ac_t public;
43
44 /**
45 * Time when attribute certificate was installed
46 */
47 time_t installed;
48
49 /**
50 * X.509 attribute certificate in DER format
51 */
52 chunk_t certificate;
53
54 /**
55 * X.509 attribute certificate body over which signature is computed
56 */
57 chunk_t certificateInfo;
58
59 /**
60 * Version of the X.509 attribute certificate
61 */
62 u_int version;
63
64 /**
65 * Serial number of the X.509 attribute certificate
66 */
67 chunk_t serialNumber;
68
69 /**
70 * ID representing the issuer of the holder certificate
71 */
72 identification_t *holderIssuer;
73
74 /**
75 * Serial number of the holder certificate
76 */
77 chunk_t holderSerial;
78
79 /**
80 * ID representing the holder
81 */
82 identification_t *entityName;
83
84 /**
85 * ID representing the attribute certificate issuer
86 */
87 identification_t *issuerName;
88
89 /**
90 * Signature algorithm
91 */
92 int sigAlg;
93
94 /**
95 * Start time of certificate validity
96 */
97 time_t notBefore;
98
99 /**
100 * End time of certificate validity
101 */
102 time_t notAfter;
103
104 /**
105 * List of charging attributes
106 */
107 linked_list_t *charging;
108
109 /**
110 * List of groub attributes
111 */
112 linked_list_t *groups;
113
114 /**
115 * Authority Key Identifier
116 */
117 chunk_t authKeyID;
118
119 /**
120 * Authority Key Serial Number
121 */
122 chunk_t authKeySerialNumber;
123
124 /**
125 * No revocation information available
126 */
127 bool noRevAvail;
128
129 /**
130 * Signature algorithm (must be identical to sigAlg)
131 */
132 int algorithm;
133
134 /**
135 * Signature
136 */
137 chunk_t signature;
138 };
139
140 /**
141 * definition of ietfAttribute kinds
142 */
143 typedef enum {
144 IETF_ATTRIBUTE_OCTETS = 0,
145 IETF_ATTRIBUTE_OID = 1,
146 IETF_ATTRIBUTE_STRING = 2
147 } ietfAttribute_t;
148
149 /**
150 * access structure for an ietfAttribute
151 */
152 typedef struct ietfAttr ietfAttr_t;
153
154 struct ietfAttr {
155 time_t installed;
156 int count;
157 ietfAttribute_t kind;
158 chunk_t value;
159 };
160
161 /**
162 * ASN.1 definition of ietfAttrSyntax
163 */
164 static const asn1Object_t ietfAttrSyntaxObjects[] =
165 {
166 { 0, "ietfAttrSyntax", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
167 { 1, "policyAuthority", ASN1_CONTEXT_C_0, ASN1_OPT |
168 ASN1_BODY }, /* 1 */
169 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
170 { 1, "values", ASN1_SEQUENCE, ASN1_LOOP }, /* 3 */
171 { 2, "octets", ASN1_OCTET_STRING, ASN1_OPT |
172 ASN1_BODY }, /* 4 */
173 { 2, "end choice", ASN1_EOC, ASN1_END }, /* 5 */
174 { 2, "oid", ASN1_OID, ASN1_OPT |
175 ASN1_BODY }, /* 6 */
176 { 2, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
177 { 2, "string", ASN1_UTF8STRING, ASN1_OPT |
178 ASN1_BODY }, /* 8 */
179 { 2, "end choice", ASN1_EOC, ASN1_END }, /* 9 */
180 { 1, "end loop", ASN1_EOC, ASN1_END } /* 10 */
181 };
182
183 #define IETF_ATTR_OCTETS 4
184 #define IETF_ATTR_OID 6
185 #define IETF_ATTR_STRING 8
186 #define IETF_ATTR_ROOF 11
187
188 /**
189 * ASN.1 definition of roleSyntax
190 */
191 static const asn1Object_t roleSyntaxObjects[] =
192 {
193 { 0, "roleSyntax", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
194 { 1, "roleAuthority", ASN1_CONTEXT_C_0, ASN1_OPT |
195 ASN1_OBJ }, /* 1 */
196 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
197 { 1, "roleName", ASN1_CONTEXT_C_1, ASN1_OBJ } /* 3 */
198 };
199
200 #define ROLE_ROOF 4
201
202 /**
203 * ASN.1 definition of an X509 attribute certificate
204 */
205 static const asn1Object_t acObjects[] =
206 {
207 { 0, "AttributeCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
208 { 1, "AttributeCertificateInfo", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
209 { 2, "version", ASN1_INTEGER, ASN1_DEF |
210 ASN1_BODY }, /* 2 */
211 { 2, "holder", ASN1_SEQUENCE, ASN1_NONE }, /* 3 */
212 { 3, "baseCertificateID", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 4 */
213 { 4, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 5 */
214 { 4, "serial", ASN1_INTEGER, ASN1_BODY }, /* 6 */
215 { 4, "issuerUID", ASN1_BIT_STRING, ASN1_OPT |
216 ASN1_BODY }, /* 7 */
217 { 4, "end opt", ASN1_EOC, ASN1_END }, /* 8 */
218 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 9 */
219 { 3, "entityName", ASN1_CONTEXT_C_1, ASN1_OPT |
220 ASN1_OBJ }, /* 10 */
221 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 11 */
222 { 3, "objectDigestInfo", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 12 */
223 { 4, "digestedObjectType", ASN1_ENUMERATED, ASN1_BODY }, /* 13*/
224 { 4, "otherObjectTypeID", ASN1_OID, ASN1_OPT |
225 ASN1_BODY }, /* 14 */
226 { 4, "end opt", ASN1_EOC, ASN1_END }, /* 15*/
227 { 4, "digestAlgorithm", ASN1_EOC, ASN1_RAW }, /* 16 */
228 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 17 */
229 { 2, "v2Form", ASN1_CONTEXT_C_0, ASN1_NONE }, /* 18 */
230 { 3, "issuerName", ASN1_SEQUENCE, ASN1_OPT |
231 ASN1_OBJ }, /* 19 */
232 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 20 */
233 { 3, "baseCertificateID", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 21 */
234 { 4, "issuerSerial", ASN1_SEQUENCE, ASN1_NONE }, /* 22 */
235 { 5, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 23 */
236 { 5, "serial", ASN1_INTEGER, ASN1_BODY }, /* 24 */
237 { 5, "issuerUID", ASN1_BIT_STRING, ASN1_OPT |
238 ASN1_BODY }, /* 25 */
239 { 5, "end opt", ASN1_EOC, ASN1_END }, /* 26 */
240 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 27 */
241 { 3, "objectDigestInfo", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 28 */
242 { 4, "digestInfo", ASN1_SEQUENCE, ASN1_OBJ }, /* 29 */
243 { 5, "digestedObjectType", ASN1_ENUMERATED, ASN1_BODY }, /* 30 */
244 { 5, "otherObjectTypeID", ASN1_OID, ASN1_OPT |
245 ASN1_BODY }, /* 31 */
246 { 5, "end opt", ASN1_EOC, ASN1_END }, /* 32 */
247 { 5, "digestAlgorithm", ASN1_EOC, ASN1_RAW }, /* 33 */
248 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 34 */
249 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 35 */
250 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 36 */
251 { 2, "attrCertValidityPeriod", ASN1_SEQUENCE, ASN1_NONE }, /* 37 */
252 { 3, "notBeforeTime", ASN1_GENERALIZEDTIME, ASN1_BODY }, /* 38 */
253 { 3, "notAfterTime", ASN1_GENERALIZEDTIME, ASN1_BODY }, /* 39 */
254 { 2, "attributes", ASN1_SEQUENCE, ASN1_LOOP }, /* 40 */
255 { 3, "attribute", ASN1_SEQUENCE, ASN1_NONE }, /* 41 */
256 { 4, "type", ASN1_OID, ASN1_BODY }, /* 42 */
257 { 4, "values", ASN1_SET, ASN1_LOOP }, /* 43 */
258 { 5, "value", ASN1_EOC, ASN1_RAW }, /* 44 */
259 { 4, "end loop", ASN1_EOC, ASN1_END }, /* 45 */
260 { 2, "end loop", ASN1_EOC, ASN1_END }, /* 46 */
261 { 2, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 47 */
262 { 3, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 48 */
263 { 4, "extnID", ASN1_OID, ASN1_BODY }, /* 49 */
264 { 4, "critical", ASN1_BOOLEAN, ASN1_DEF |
265 ASN1_BODY }, /* 50 */
266 { 4, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 51 */
267 { 2, "end loop", ASN1_EOC, ASN1_END }, /* 52 */
268 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 53 */
269 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY } /* 54 */
270 };
271
272 #define AC_OBJ_CERTIFICATE 0
273 #define AC_OBJ_CERTIFICATE_INFO 1
274 #define AC_OBJ_VERSION 2
275 #define AC_OBJ_HOLDER_ISSUER 5
276 #define AC_OBJ_HOLDER_SERIAL 6
277 #define AC_OBJ_ENTITY_NAME 10
278 #define AC_OBJ_ISSUER_NAME 19
279 #define AC_OBJ_ISSUER 23
280 #define AC_OBJ_SIG_ALG 35
281 #define AC_OBJ_SERIAL_NUMBER 36
282 #define AC_OBJ_NOT_BEFORE 38
283 #define AC_OBJ_NOT_AFTER 39
284 #define AC_OBJ_ATTRIBUTE_TYPE 42
285 #define AC_OBJ_ATTRIBUTE_VALUE 44
286 #define AC_OBJ_EXTN_ID 49
287 #define AC_OBJ_CRITICAL 50
288 #define AC_OBJ_EXTN_VALUE 51
289 #define AC_OBJ_ALGORITHM 53
290 #define AC_OBJ_SIGNATURE 54
291 #define AC_OBJ_ROOF 55
292
293 /**
294 * Implements x509ac_t.is_valid
295 */
296 static err_t is_valid(const private_x509ac_t *this, time_t *until)
297 {
298 time_t current_time = time(NULL);
299
300 DBG2(" not before : %T", &this->notBefore);
301 DBG2(" current time: %T", &current_time);
302 DBG2(" not after : %T", &this->notAfter);
303
304 if (until != NULL &&
305 (*until == UNDEFINED_TIME || this->notAfter < *until))
306 {
307 *until = this->notAfter;
308 }
309 if (current_time < this->notBefore)
310 {
311 return "is not valid yet";
312 }
313 if (current_time > this->notAfter)
314 {
315 return "has expired";
316 }
317 DBG2(" attribute certificate is valid");
318 return NULL;
319 }
320
321 /**
322 * parses a directoryName
323 */
324 static bool parse_directoryName(chunk_t blob, int level, bool implicit, identification_t **name)
325 {
326 bool has_directoryName;
327 linked_list_t *list = linked_list_create();
328
329 parse_generalNames(blob, level, implicit, list);
330 has_directoryName = list->get_count(list) > 0;
331
332 if (has_directoryName)
333 {
334 iterator_t *iterator = list->create_iterator(list, TRUE);
335 identification_t *directoryName;
336 bool first = TRUE;
337
338 while (iterator->iterate(iterator, (void**)&directoryName))
339 {
340 if (first)
341 {
342 *name = directoryName;
343 first = FALSE;
344 }
345 else
346 {
347 DBG1("more than one directory name - first selected");
348 directoryName->destroy(directoryName);
349 }
350 }
351 iterator->destroy(iterator);
352 }
353 else
354 {
355 DBG1("no directoryName found");
356 }
357
358 list->destroy(list);
359 return has_directoryName;
360 }
361
362 /**
363 * parses ietfAttrSyntax
364 */
365 static void parse_ietfAttrSyntax(chunk_t blob, int level0, linked_list_t *list)
366 {
367 /* TODO */
368 }
369
370 /**
371 * parses roleSyntax
372 */
373 static void parse_roleSyntax(chunk_t blob, int level0)
374 {
375 asn1_ctx_t ctx;
376 chunk_t object;
377 u_int level;
378 int objectID = 0;
379
380 asn1_init(&ctx, blob, level0, FALSE, FALSE);
381 while (objectID < ROLE_ROOF)
382 {
383 if (!extract_object(roleSyntaxObjects, &objectID, &object, &level, &ctx))
384 {
385 return;
386 }
387
388 switch (objectID)
389 {
390 default:
391 break;
392 }
393 objectID++;
394 }
395 }
396
397 /**
398 * Parses an X.509 attribute certificate
399 */
400 static bool parse_certificate(chunk_t blob, private_x509ac_t *this)
401 {
402 asn1_ctx_t ctx;
403 bool critical;
404 chunk_t object;
405 u_int level;
406 u_int type = OID_UNKNOWN;
407 u_int extn_oid = OID_UNKNOWN;
408 int objectID = 0;
409
410 asn1_init(&ctx, blob, 0, FALSE, FALSE);
411 while (objectID < AC_OBJ_ROOF)
412 {
413 if (!extract_object(acObjects, &objectID, &object, &level, &ctx))
414 {
415 return FALSE;
416 }
417
418 /* those objects which will parsed further need the next higher level */
419 level++;
420
421 switch (objectID)
422 {
423 case AC_OBJ_CERTIFICATE:
424 this->certificate = object;
425 break;
426 case AC_OBJ_CERTIFICATE_INFO:
427 this->certificateInfo = object;
428 break;
429 case AC_OBJ_VERSION:
430 this->version = (object.len) ? (1 + (u_int)*object.ptr) : 1;
431 DBG2(" v%d", this->version);
432 if (this->version != 2)
433 {
434 DBG1("v%d attribute certificates are not supported", this->version);
435 return FALSE;
436 }
437 break;
438 case AC_OBJ_HOLDER_ISSUER:
439 if (!parse_directoryName(object, level, FALSE, &this->holderIssuer))
440 {
441 return FALSE;
442 }
443 break;
444 case AC_OBJ_HOLDER_SERIAL:
445 this->holderSerial = object;
446 break;
447 case AC_OBJ_ENTITY_NAME:
448 if (!parse_directoryName(object, level, TRUE, &this->entityName))
449 {
450 return FALSE;
451 }
452 break;
453 case AC_OBJ_ISSUER_NAME:
454 if (!parse_directoryName(object, level, FALSE, &this->issuerName))
455 {
456 return FALSE;
457 }
458 break;
459 case AC_OBJ_SIG_ALG:
460 this->sigAlg = parse_algorithmIdentifier(object, level, NULL);
461 break;
462 case AC_OBJ_SERIAL_NUMBER:
463 this->serialNumber = object;
464 break;
465 case AC_OBJ_NOT_BEFORE:
466 this->notBefore = asn1totime(&object, ASN1_GENERALIZEDTIME);
467 break;
468 case AC_OBJ_NOT_AFTER:
469 this->notAfter = asn1totime(&object, ASN1_GENERALIZEDTIME);
470 break;
471 case AC_OBJ_ATTRIBUTE_TYPE:
472 type = known_oid(object);
473 break;
474 case AC_OBJ_ATTRIBUTE_VALUE:
475 {
476 switch (type)
477 {
478 case OID_AUTHENTICATION_INFO:
479 DBG2(" need to parse authenticationInfo");
480 break;
481 case OID_ACCESS_IDENTITY:
482 DBG2(" need to parse accessIdentity");
483 break;
484 case OID_CHARGING_IDENTITY:
485 parse_ietfAttrSyntax(object, level, this->charging);
486 break;
487 case OID_GROUP:
488 parse_ietfAttrSyntax(object, level, this->groups);
489 break;
490 case OID_ROLE:
491 parse_roleSyntax(object, level);
492 break;
493 default:
494 break;
495 }
496 }
497 break;
498 case AC_OBJ_EXTN_ID:
499 extn_oid = known_oid(object);
500 break;
501 case AC_OBJ_CRITICAL:
502 critical = object.len && *object.ptr;
503 DBG2(" %s",(critical)?"TRUE":"FALSE");
504 break;
505 case AC_OBJ_EXTN_VALUE:
506 {
507 switch (extn_oid)
508 {
509 case OID_CRL_DISTRIBUTION_POINTS:
510 DBG2(" need to parse crlDistributionPoints");
511 break;
512 case OID_AUTHORITY_KEY_ID:
513 parse_authorityKeyIdentifier(object, level,
514 &this->authKeyID, &this->authKeySerialNumber);
515 break;
516 case OID_TARGET_INFORMATION:
517 DBG2(" need to parse targetInformation");
518 break;
519 case OID_NO_REV_AVAIL:
520 this->noRevAvail = TRUE;
521 break;
522 default:
523 break;
524 }
525 }
526 break;
527 case AC_OBJ_ALGORITHM:
528 this->algorithm = parse_algorithmIdentifier(object, level, NULL);
529 break;
530 case AC_OBJ_SIGNATURE:
531 this->signature = object;
532 break;
533 default:
534 break;
535 }
536 objectID++;
537 }
538 this->installed = time(NULL);
539 return FALSE;
540 }
541
542 /**
543 * Implements x509ac_t.destroy
544 */
545 static void destroy(private_x509ac_t *this)
546 {
547 DESTROY_IF(this->holderIssuer);
548 DESTROY_IF(this->entityName);
549 DESTROY_IF(this->issuerName);
550 free(this->certificate.ptr);
551 free(this);
552 }
553
554 /*
555 * Described in header.
556 */
557 x509ac_t *x509ac_create_from_chunk(chunk_t chunk)
558 {
559 private_x509ac_t *this = malloc_thing(private_x509ac_t);
560
561 /* initialize */
562 this->holderIssuer = NULL;
563 this->entityName = NULL;
564 this->issuerName = NULL;
565
566 /* public functions */
567 this->public.is_valid = (err_t (*) (const x509ac_t*,time_t*))is_valid;
568 this->public.destroy = (void (*) (x509ac_t*))destroy;
569
570 if (!parse_certificate(chunk, this))
571 {
572 destroy(this);
573 return NULL;
574 }
575 return &this->public;
576 }
577
578 /*
579 * Described in header.
580 */
581 x509ac_t *x509ac_create_from_file(const char *filename)
582 {
583 bool pgp = FALSE;
584 chunk_t chunk = chunk_empty;
585
586 if (!pem_asn1_load_file(filename, NULL, "attribute certificate", &chunk, &pgp))
587 {
588 return NULL;
589 }
590 return x509ac_create_from_chunk(chunk);
591 }
592