pluto now uses x509 plugin for attribute certificate handling
[strongswan.git] / src / libstrongswan / plugins / x509 / x509_ac.c
1 /*
2 * Copyright (C) 2002 Ueli Galizzi, Ariane Seiler
3 * Copyright (C) 2003 Martin Berner, Lukas Suter
4 * Copyright (C) 2002-2009 Andreas Steffen
5 * Copyright (C) 2009 Martin Willi
6 *
7 * HSR Hochschule fuer Technik Rapperswil
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 */
19
20 #include "x509_ac.h"
21
22 #include <time.h>
23
24 #include <library.h>
25 #include <debug.h>
26 #include <asn1/oid.h>
27 #include <asn1/asn1.h>
28 #include <asn1/asn1_parser.h>
29 #include <utils/identification.h>
30 #include <utils/linked_list.h>
31 #include <credentials/certificates/x509.h>
32 #include <credentials/ietf_attributes/ietf_attributes.h>
33 #include <credentials/keys/private_key.h>
34
35 extern chunk_t x509_parse_authorityKeyIdentifier(chunk_t blob,
36 int level0, chunk_t *authKeySerialNumber);
37
38 typedef struct private_x509_ac_t private_x509_ac_t;
39
40 /**
41 * private data of x509_ac_t object
42 */
43 struct private_x509_ac_t {
44
45 /**
46 * public functions
47 */
48 x509_ac_t public;
49
50 /**
51 * X.509 attribute certificate encoding in ASN.1 DER format
52 */
53 chunk_t encoding;
54
55 /**
56 * X.509 attribute certificate body over which signature is computed
57 */
58 chunk_t certificateInfo;
59
60 /**
61 * Version of the X.509 attribute certificate
62 */
63 u_int version;
64
65 /**
66 * Serial number of the X.509 attribute certificate
67 */
68 chunk_t serialNumber;
69
70 /**
71 * ID representing the issuer of the holder certificate
72 */
73 identification_t *holderIssuer;
74
75 /**
76 * Serial number of the holder certificate
77 */
78 chunk_t holderSerial;
79
80 /**
81 * ID representing the holder
82 */
83 identification_t *entityName;
84
85 /**
86 * ID representing the attribute certificate issuer
87 */
88 identification_t *issuerName;
89
90 /**
91 * Start time of certificate validity
92 */
93 time_t notBefore;
94
95 /**
96 * End time of certificate validity
97 */
98 time_t notAfter;
99
100 /**
101 * List of charging attributes
102 */
103 ietf_attributes_t *charging;
104
105 /**
106 * List of groub attributes
107 */
108 ietf_attributes_t *groups;
109
110 /**
111 * Authority Key Identifier
112 */
113 chunk_t authKeyIdentifier;
114
115 /**
116 * Authority Key Serial Number
117 */
118 chunk_t authKeySerialNumber;
119
120 /**
121 * No revocation information available
122 */
123 bool noRevAvail;
124
125 /**
126 * Signature algorithm
127 */
128 int algorithm;
129
130 /**
131 * Signature
132 */
133 chunk_t signature;
134
135 /**
136 * Holder certificate
137 */
138 certificate_t *holderCert;
139
140 /**
141 * Signer certificate
142 */
143 certificate_t *signerCert;
144
145 /**
146 * Signer private key;
147 */
148 private_key_t *signerKey;
149
150 /**
151 * reference count
152 */
153 refcount_t ref;
154 };
155
156 static chunk_t ASN1_noRevAvail_ext = chunk_from_chars(
157 0x30, 0x09,
158 0x06, 0x03,
159 0x55, 0x1d, 0x38,
160 0x04, 0x02,
161 0x05, 0x00
162 );
163
164 /**
165 * declaration of function implemented in x509_cert.c
166 */
167 extern void x509_parse_generalNames(chunk_t blob, int level0, bool implicit,
168 linked_list_t *list);
169 /**
170 * parses a directoryName
171 */
172 static bool parse_directoryName(chunk_t blob, int level, bool implicit, identification_t **name)
173 {
174 bool has_directoryName;
175 linked_list_t *list = linked_list_create();
176
177 x509_parse_generalNames(blob, level, implicit, list);
178 has_directoryName = list->get_count(list) > 0;
179
180 if (has_directoryName)
181 {
182 iterator_t *iterator = list->create_iterator(list, TRUE);
183 identification_t *directoryName;
184 bool first = TRUE;
185
186 while (iterator->iterate(iterator, (void**)&directoryName))
187 {
188 if (first)
189 {
190 *name = directoryName;
191 first = FALSE;
192 }
193 else
194 {
195 DBG1("more than one directory name - first selected");
196 directoryName->destroy(directoryName);
197 }
198 }
199 iterator->destroy(iterator);
200 }
201 else
202 {
203 DBG1("no directoryName found");
204 }
205
206 list->destroy(list);
207 return has_directoryName;
208 }
209
210 /**
211 * ASN.1 definition of roleSyntax
212 */
213 static const asn1Object_t roleSyntaxObjects[] =
214 {
215 { 0, "roleSyntax", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
216 { 1, "roleAuthority", ASN1_CONTEXT_C_0, ASN1_OPT |
217 ASN1_OBJ }, /* 1 */
218 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
219 { 1, "roleName", ASN1_CONTEXT_C_1, ASN1_OBJ }, /* 3 */
220 { 0, "exit", ASN1_EOC, ASN1_EXIT }
221 };
222
223 /**
224 * Parses roleSyntax
225 */
226 static void parse_roleSyntax(chunk_t blob, int level0)
227 {
228 asn1_parser_t *parser;
229 chunk_t object;
230 int objectID;
231
232 parser = asn1_parser_create(roleSyntaxObjects, blob);
233 parser->set_top_level(parser, level0);
234
235 while (parser->iterate(parser, &objectID, &object))
236 {
237 switch (objectID)
238 {
239 default:
240 break;
241 }
242 }
243 parser->destroy(parser);
244 }
245
246 /**
247 * ASN.1 definition of an X509 attribute certificate
248 */
249 static const asn1Object_t acObjects[] =
250 {
251 { 0, "AttributeCertificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 0 */
252 { 1, "AttributeCertificateInfo", ASN1_SEQUENCE, ASN1_OBJ }, /* 1 */
253 { 2, "version", ASN1_INTEGER, ASN1_DEF |
254 ASN1_BODY }, /* 2 */
255 { 2, "holder", ASN1_SEQUENCE, ASN1_NONE }, /* 3 */
256 { 3, "baseCertificateID", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 4 */
257 { 4, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 5 */
258 { 4, "serial", ASN1_INTEGER, ASN1_BODY }, /* 6 */
259 { 4, "issuerUID", ASN1_BIT_STRING, ASN1_OPT |
260 ASN1_BODY }, /* 7 */
261 { 4, "end opt", ASN1_EOC, ASN1_END }, /* 8 */
262 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 9 */
263 { 3, "entityName", ASN1_CONTEXT_C_1, ASN1_OPT |
264 ASN1_OBJ }, /* 10 */
265 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 11 */
266 { 3, "objectDigestInfo", ASN1_CONTEXT_C_2, ASN1_OPT }, /* 12 */
267 { 4, "digestedObjectType", ASN1_ENUMERATED, ASN1_BODY }, /* 13 */
268 { 4, "otherObjectTypeID", ASN1_OID, ASN1_OPT |
269 ASN1_BODY }, /* 14 */
270 { 4, "end opt", ASN1_EOC, ASN1_END }, /* 15 */
271 { 4, "digestAlgorithm", ASN1_EOC, ASN1_RAW }, /* 16 */
272 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 17 */
273 { 2, "v2Form", ASN1_CONTEXT_C_0, ASN1_NONE }, /* 18 */
274 { 3, "issuerName", ASN1_SEQUENCE, ASN1_OPT |
275 ASN1_OBJ }, /* 19 */
276 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 20 */
277 { 3, "baseCertificateID", ASN1_CONTEXT_C_0, ASN1_OPT }, /* 21 */
278 { 4, "issuerSerial", ASN1_SEQUENCE, ASN1_NONE }, /* 22 */
279 { 5, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 23 */
280 { 5, "serial", ASN1_INTEGER, ASN1_BODY }, /* 24 */
281 { 5, "issuerUID", ASN1_BIT_STRING, ASN1_OPT |
282 ASN1_BODY }, /* 25 */
283 { 5, "end opt", ASN1_EOC, ASN1_END }, /* 26 */
284 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 27 */
285 { 3, "objectDigestInfo", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 28 */
286 { 4, "digestInfo", ASN1_SEQUENCE, ASN1_OBJ }, /* 29 */
287 { 5, "digestedObjectType", ASN1_ENUMERATED, ASN1_BODY }, /* 30 */
288 { 5, "otherObjectTypeID", ASN1_OID, ASN1_OPT |
289 ASN1_BODY }, /* 31 */
290 { 5, "end opt", ASN1_EOC, ASN1_END }, /* 32 */
291 { 5, "digestAlgorithm", ASN1_EOC, ASN1_RAW }, /* 33 */
292 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 34 */
293 { 2, "signature", ASN1_EOC, ASN1_RAW }, /* 35 */
294 { 2, "serialNumber", ASN1_INTEGER, ASN1_BODY }, /* 36 */
295 { 2, "attrCertValidityPeriod", ASN1_SEQUENCE, ASN1_NONE }, /* 37 */
296 { 3, "notBeforeTime", ASN1_GENERALIZEDTIME, ASN1_BODY }, /* 38 */
297 { 3, "notAfterTime", ASN1_GENERALIZEDTIME, ASN1_BODY }, /* 39 */
298 { 2, "attributes", ASN1_SEQUENCE, ASN1_LOOP }, /* 40 */
299 { 3, "attribute", ASN1_SEQUENCE, ASN1_NONE }, /* 41 */
300 { 4, "type", ASN1_OID, ASN1_BODY }, /* 42 */
301 { 4, "values", ASN1_SET, ASN1_LOOP }, /* 43 */
302 { 5, "value", ASN1_EOC, ASN1_RAW }, /* 44 */
303 { 4, "end loop", ASN1_EOC, ASN1_END }, /* 45 */
304 { 2, "end loop", ASN1_EOC, ASN1_END }, /* 46 */
305 { 2, "extensions", ASN1_SEQUENCE, ASN1_LOOP }, /* 47 */
306 { 3, "extension", ASN1_SEQUENCE, ASN1_NONE }, /* 48 */
307 { 4, "extnID", ASN1_OID, ASN1_BODY }, /* 49 */
308 { 4, "critical", ASN1_BOOLEAN, ASN1_DEF |
309 ASN1_BODY }, /* 50 */
310 { 4, "extnValue", ASN1_OCTET_STRING, ASN1_BODY }, /* 51 */
311 { 2, "end loop", ASN1_EOC, ASN1_END }, /* 52 */
312 { 1, "signatureAlgorithm", ASN1_EOC, ASN1_RAW }, /* 53 */
313 { 1, "signatureValue", ASN1_BIT_STRING, ASN1_BODY }, /* 54 */
314 { 0, "exit", ASN1_EOC, ASN1_EXIT }
315 };
316 #define AC_OBJ_CERTIFICATE_INFO 1
317 #define AC_OBJ_VERSION 2
318 #define AC_OBJ_HOLDER_ISSUER 5
319 #define AC_OBJ_HOLDER_SERIAL 6
320 #define AC_OBJ_ENTITY_NAME 10
321 #define AC_OBJ_ISSUER_NAME 19
322 #define AC_OBJ_ISSUER 23
323 #define AC_OBJ_SIG_ALG 35
324 #define AC_OBJ_SERIAL_NUMBER 36
325 #define AC_OBJ_NOT_BEFORE 38
326 #define AC_OBJ_NOT_AFTER 39
327 #define AC_OBJ_ATTRIBUTE_TYPE 42
328 #define AC_OBJ_ATTRIBUTE_VALUE 44
329 #define AC_OBJ_EXTN_ID 49
330 #define AC_OBJ_CRITICAL 50
331 #define AC_OBJ_EXTN_VALUE 51
332 #define AC_OBJ_ALGORITHM 53
333 #define AC_OBJ_SIGNATURE 54
334
335 /**
336 * Parses an X.509 attribute certificate
337 */
338 static bool parse_certificate(private_x509_ac_t *this)
339 {
340 asn1_parser_t *parser;
341 chunk_t object;
342 int objectID;
343 int type = OID_UNKNOWN;
344 int extn_oid = OID_UNKNOWN;
345 int sig_alg = OID_UNKNOWN;
346 bool success = FALSE;
347 bool critical;
348
349 parser = asn1_parser_create(acObjects, this->encoding);
350
351 while (parser->iterate(parser, &objectID, &object))
352 {
353 u_int level = parser->get_level(parser)+1;
354
355 switch (objectID)
356 {
357 case AC_OBJ_CERTIFICATE_INFO:
358 this->certificateInfo = object;
359 break;
360 case AC_OBJ_VERSION:
361 this->version = (object.len) ? (1 + (u_int)*object.ptr) : 1;
362 DBG2(" v%d", this->version);
363 if (this->version != 2)
364 {
365 DBG1("v%d attribute certificates are not supported", this->version);
366 goto end;
367 }
368 break;
369 case AC_OBJ_HOLDER_ISSUER:
370 if (!parse_directoryName(object, level, FALSE, &this->holderIssuer))
371 {
372 goto end;
373 }
374 break;
375 case AC_OBJ_HOLDER_SERIAL:
376 this->holderSerial = object;
377 break;
378 case AC_OBJ_ENTITY_NAME:
379 if (!parse_directoryName(object, level, TRUE, &this->entityName))
380 {
381 goto end;
382 }
383 break;
384 case AC_OBJ_ISSUER_NAME:
385 if (!parse_directoryName(object, level, FALSE, &this->issuerName))
386 {
387 goto end;
388 }
389 break;
390 case AC_OBJ_SIG_ALG:
391 sig_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
392 break;
393 case AC_OBJ_SERIAL_NUMBER:
394 this->serialNumber = chunk_clone(object);
395 break;
396 case AC_OBJ_NOT_BEFORE:
397 this->notBefore = asn1_to_time(&object, ASN1_GENERALIZEDTIME);
398 break;
399 case AC_OBJ_NOT_AFTER:
400 this->notAfter = asn1_to_time(&object, ASN1_GENERALIZEDTIME);
401 break;
402 case AC_OBJ_ATTRIBUTE_TYPE:
403 type = asn1_known_oid(object);
404 break;
405 case AC_OBJ_ATTRIBUTE_VALUE:
406 {
407 switch (type)
408 {
409 case OID_AUTHENTICATION_INFO:
410 DBG2(" need to parse authenticationInfo");
411 break;
412 case OID_ACCESS_IDENTITY:
413 DBG2(" need to parse accessIdentity");
414 break;
415 case OID_CHARGING_IDENTITY:
416 DBG2("-- > --");
417 this->charging = ietf_attributes_create_from_encoding(object);
418 DBG2("-- < --");
419 break;
420 case OID_GROUP:
421 DBG2("-- > --");
422 this->groups = ietf_attributes_create_from_encoding(object);
423 DBG2("-- < --");
424 break;
425 case OID_ROLE:
426 parse_roleSyntax(object, level);
427 break;
428 default:
429 break;
430 }
431 break;
432 }
433 case AC_OBJ_EXTN_ID:
434 extn_oid = asn1_known_oid(object);
435 break;
436 case AC_OBJ_CRITICAL:
437 critical = object.len && *object.ptr;
438 DBG2(" %s",(critical)?"TRUE":"FALSE");
439 break;
440 case AC_OBJ_EXTN_VALUE:
441 {
442 switch (extn_oid)
443 {
444 case OID_CRL_DISTRIBUTION_POINTS:
445 DBG2(" need to parse crlDistributionPoints");
446 break;
447 case OID_AUTHORITY_KEY_ID:
448 this->authKeyIdentifier = x509_parse_authorityKeyIdentifier(object,
449 level, &this->authKeySerialNumber);
450 break;
451 case OID_TARGET_INFORMATION:
452 DBG2(" need to parse targetInformation");
453 break;
454 case OID_NO_REV_AVAIL:
455 this->noRevAvail = TRUE;
456 break;
457 default:
458 break;
459 }
460 break;
461 }
462 case AC_OBJ_ALGORITHM:
463 this->algorithm = asn1_parse_algorithmIdentifier(object, level,
464 NULL);
465 if (this->algorithm != sig_alg)
466 {
467 DBG1(" signature algorithms do not agree");
468 success = FALSE;
469 goto end;
470 }
471 break;
472 case AC_OBJ_SIGNATURE:
473 this->signature = object;
474 break;
475 default:
476 break;
477 }
478 }
479 success = parser->success(parser);
480
481 end:
482 parser->destroy(parser);
483 return success;
484 }
485
486 /**
487 * build directoryName
488 */
489 static chunk_t build_directoryName(asn1_t tag, chunk_t name)
490 {
491 return asn1_wrap(tag, "m",
492 asn1_simple_object(ASN1_CONTEXT_C_4, name));
493 }
494
495 /**
496 * build holder
497 */
498 static chunk_t build_holder(private_x509_ac_t *this)
499 {
500 x509_t* x509 = (x509_t*)this->holderCert;
501 identification_t *issuer = this->holderCert->get_issuer(this->holderCert);
502 identification_t *subject = this->holderCert->get_subject(this->holderCert);
503
504 return asn1_wrap(ASN1_SEQUENCE, "mm",
505 asn1_wrap(ASN1_CONTEXT_C_0, "mm",
506 build_directoryName(ASN1_SEQUENCE, issuer->get_encoding(issuer)),
507 asn1_simple_object(ASN1_INTEGER, x509->get_serial(x509))
508 ),
509 build_directoryName(ASN1_CONTEXT_C_1, subject->get_encoding(subject)));
510 }
511
512 /**
513 * build v2Form
514 */
515 static chunk_t build_v2_form(private_x509_ac_t *this)
516 {
517 identification_t *subject = this->signerCert->get_subject(this->signerCert);
518
519 return asn1_wrap(ASN1_CONTEXT_C_0, "m",
520 build_directoryName(ASN1_SEQUENCE, subject->get_encoding(subject)));
521 }
522
523 /**
524 * build attrCertValidityPeriod
525 */
526 static chunk_t build_attr_cert_validity(private_x509_ac_t *this)
527 {
528 return asn1_wrap(ASN1_SEQUENCE, "mm",
529 asn1_from_time(&this->notBefore, ASN1_GENERALIZEDTIME),
530 asn1_from_time(&this->notAfter, ASN1_GENERALIZEDTIME));
531 }
532
533
534 /**
535 * build attribute type
536 */
537 static chunk_t build_attribute_type(int type, chunk_t content)
538 {
539 return asn1_wrap(ASN1_SEQUENCE, "mm",
540 asn1_build_known_oid(type),
541 asn1_wrap(ASN1_SET, "m", content));
542 }
543
544 /**
545 * build attributes
546 */
547 static chunk_t build_attributes(private_x509_ac_t *this)
548 {
549 return asn1_wrap(ASN1_SEQUENCE, "m",
550 build_attribute_type(OID_GROUP, this->groups->get_encoding(this->groups)));
551 }
552
553 /**
554 * build authorityKeyIdentifier
555 */
556 static chunk_t build_authorityKeyIdentifier(private_x509_ac_t *this)
557 {
558 chunk_t keyIdentifier = chunk_empty;
559 chunk_t authorityCertIssuer;
560 chunk_t authorityCertSerialNumber;
561 identification_t *issuer;
562 public_key_t *public;
563 x509_t *x509;
564
565 x509 = (x509_t*)this->signerCert;
566 issuer = this->signerCert->get_issuer(this->signerCert);
567 public = this->signerCert->get_public_key(this->signerCert);
568 if (public)
569 {
570 if (public->get_fingerprint(public, KEY_ID_PUBKEY_SHA1, &keyIdentifier))
571 {
572 this->authKeyIdentifier = chunk_clone(keyIdentifier);
573 }
574 public->destroy(public);
575 }
576 authorityCertIssuer = build_directoryName(ASN1_CONTEXT_C_1,
577 issuer->get_encoding(issuer));
578 authorityCertSerialNumber = asn1_simple_object(ASN1_CONTEXT_S_2,
579 x509->get_serial(x509));
580 return asn1_wrap(ASN1_SEQUENCE, "mm",
581 asn1_build_known_oid(OID_AUTHORITY_KEY_ID),
582 asn1_wrap(ASN1_OCTET_STRING, "m",
583 asn1_wrap(ASN1_SEQUENCE, "cmm",
584 keyIdentifier,
585 authorityCertIssuer,
586 authorityCertSerialNumber
587 )
588 )
589 );
590 }
591
592 /**
593 * build extensions
594 */
595 static chunk_t build_extensions(private_x509_ac_t *this)
596 {
597 return asn1_wrap(ASN1_SEQUENCE, "mc",
598 build_authorityKeyIdentifier(this),
599 ASN1_noRevAvail_ext);
600 }
601
602 /**
603 * build attributeCertificateInfo
604 */
605 static chunk_t build_attr_cert_info(private_x509_ac_t *this)
606 {
607 return asn1_wrap(ASN1_SEQUENCE, "cmmmmmmm",
608 ASN1_INTEGER_1,
609 build_holder(this),
610 build_v2_form(this),
611 asn1_algorithmIdentifier(OID_SHA1_WITH_RSA),
612 asn1_simple_object(ASN1_INTEGER, this->serialNumber),
613 build_attr_cert_validity(this),
614 build_attributes(this),
615 build_extensions(this));
616 }
617
618
619 /**
620 * build an X.509 attribute certificate
621 */
622 static chunk_t build_ac(private_x509_ac_t *this)
623 {
624 chunk_t signatureValue;
625 chunk_t attributeCertificateInfo;
626
627 attributeCertificateInfo = build_attr_cert_info(this);
628
629 this->signerKey->sign(this->signerKey, SIGN_RSA_EMSA_PKCS1_SHA1,
630 attributeCertificateInfo, &signatureValue);
631
632 return asn1_wrap(ASN1_SEQUENCE, "mmm",
633 attributeCertificateInfo,
634 asn1_algorithmIdentifier(OID_SHA1_WITH_RSA),
635 asn1_bitstring("m", signatureValue));
636 }
637
638 /**
639 * Implementation of ac_t.get_serial.
640 */
641 static chunk_t get_serial(private_x509_ac_t *this)
642 {
643 return this->serialNumber;
644 }
645
646 /**
647 * Implementation of ac_t.get_holderSerial.
648 */
649 static chunk_t get_holderSerial(private_x509_ac_t *this)
650 {
651 return this->holderSerial;
652 }
653
654 /**
655 * Implementation of ac_t.get_holderIssuer.
656 */
657 static identification_t* get_holderIssuer(private_x509_ac_t *this)
658 {
659 return this->holderIssuer;
660 }
661
662 /**
663 * Implementation of ac_t.get_authKeyIdentifier.
664 */
665 static chunk_t get_authKeyIdentifier(private_x509_ac_t *this)
666 {
667 return this->authKeyIdentifier;
668 }
669
670 /**
671 * Implementation of certificate_t.get_groups.
672 */
673 static ietf_attributes_t* get_groups(private_x509_ac_t *this)
674 {
675 return this->groups ? this->groups->get_ref(this->groups) : NULL;
676 }
677
678 /**
679 * Implementation of certificate_t.get_type
680 */
681 static certificate_type_t get_type(private_x509_ac_t *this)
682 {
683 return CERT_X509_AC;
684 }
685
686 /**
687 * Implementation of certificate_t.get_subject
688 */
689 static identification_t* get_subject(private_x509_ac_t *this)
690 {
691 return this->entityName;
692 }
693
694 /**
695 * Implementation of certificate_t.get_issuer
696 */
697 static identification_t* get_issuer(private_x509_ac_t *this)
698 {
699 return this->issuerName;
700 }
701
702 /**
703 * Implementation of certificate_t.has_subject.
704 */
705 static id_match_t has_subject(private_x509_ac_t *this, identification_t *subject)
706 {
707 return ID_MATCH_NONE;
708 }
709
710 /**
711 * Implementation of certificate_t.has_issuer.
712 */
713 static id_match_t has_issuer(private_x509_ac_t *this, identification_t *issuer)
714 {
715 if (issuer->get_type(issuer) == ID_KEY_ID && this->authKeyIdentifier.ptr &&
716 chunk_equals(this->authKeyIdentifier, issuer->get_encoding(issuer)))
717 {
718 return ID_MATCH_PERFECT;
719 }
720 return this->issuerName->matches(this->issuerName, issuer);
721 }
722
723 /**
724 * Implementation of certificate_t.issued_by
725 */
726 static bool issued_by(private_x509_ac_t *this, certificate_t *issuer)
727 {
728 public_key_t *key;
729 signature_scheme_t scheme;
730 bool valid;
731 x509_t *x509 = (x509_t*)issuer;
732
733 /* check if issuer is an X.509 AA certificate */
734 if (issuer->get_type(issuer) != CERT_X509)
735 {
736 return FALSE;
737 }
738 if (!(x509->get_flags(x509) & X509_AA))
739 {
740 return FALSE;
741 }
742
743 /* get the public key of the issuer */
744 key = issuer->get_public_key(issuer);
745
746 /* compare keyIdentifiers if available, otherwise use DNs */
747 if (this->authKeyIdentifier.ptr && key)
748 {
749 chunk_t fingerprint;
750
751 if (!key->get_fingerprint(key, KEY_ID_PUBKEY_SHA1, &fingerprint) ||
752 !chunk_equals(fingerprint, this->authKeyIdentifier))
753 {
754 return FALSE;
755 }
756 }
757 else
758 {
759 if (!this->issuerName->equals(this->issuerName,
760 issuer->get_subject(issuer)))
761 {
762 return FALSE;
763 }
764 }
765
766 /* determine signature scheme */
767 scheme = signature_scheme_from_oid(this->algorithm);
768
769 if (scheme == SIGN_UNKNOWN || key == NULL)
770 {
771 return FALSE;
772 }
773 valid = key->verify(key, scheme, this->certificateInfo, this->signature);
774 key->destroy(key);
775 return valid;
776 }
777
778 /**
779 * Implementation of certificate_t.get_public_key.
780 */
781 static public_key_t* get_public_key(private_x509_ac_t *this)
782 {
783 return NULL;
784 }
785
786 /**
787 * Implementation of certificate_t.get_ref.
788 */
789 static private_x509_ac_t* get_ref(private_x509_ac_t *this)
790 {
791 ref_get(&this->ref);
792 return this;
793 }
794
795 /**
796 * Implementation of certificate_t.get_validity.
797 */
798 static bool get_validity(private_x509_ac_t *this, time_t *when,
799 time_t *not_before, time_t *not_after)
800 {
801 time_t t;
802
803 if (when)
804 {
805 t = *when;
806 }
807 else
808 {
809 t = time(NULL);
810 }
811 if (not_before)
812 {
813 *not_before = this->notBefore;
814 }
815 if (not_after)
816 {
817 *not_after = this->notAfter;
818 }
819 if (t < this->notBefore)
820 {
821 DBG1("attribute certificate is not valid before %T",
822 this->notBefore, TRUE);
823 return FALSE;
824 }
825 if (t > this->notAfter)
826 {
827 DBG1("attribute certificate expired on %T",
828 this->notAfter, TRUE);
829 return FALSE;
830 }
831 return TRUE;
832 }
833
834 /**
835 * Implementation of certificate_t.is_newer.
836 */
837 static bool is_newer(private_x509_ac_t *this, ac_t *that)
838 {
839 certificate_t *this_cert = &this->public.interface.certificate;
840 certificate_t *that_cert = &that->certificate;
841 time_t this_update, that_update, now = time(NULL);
842 bool new;
843
844 this_cert->get_validity(this_cert, &now, &this_update, NULL);
845 that_cert->get_validity(that_cert, &now, &that_update, NULL);
846 new = this_update > that_update;
847 DBG1(" attr cert from %T is %s - existing attr_cert from %T %s",
848 &this_update, FALSE, new ? "newer":"not newer",
849 &that_update, FALSE, new ? "replaced":"retained");
850 return new;
851 }
852
853 /**
854 * Implementation of certificate_t.get_encoding.
855 */
856 static chunk_t get_encoding(private_x509_ac_t *this)
857 {
858 return chunk_clone(this->encoding);
859 }
860
861 /**
862 * Implementation of certificate_t.equals.
863 */
864 static bool equals(private_x509_ac_t *this, certificate_t *other)
865 {
866 chunk_t encoding;
867 bool equal;
868
869 if ((certificate_t*)this == other)
870 {
871 return TRUE;
872 }
873 if (other->equals == (void*)equals)
874 { /* skip allocation if we have the same implementation */
875 return chunk_equals(this->encoding, ((private_x509_ac_t*)other)->encoding);
876 }
877 encoding = other->get_encoding(other);
878 equal = chunk_equals(this->encoding, encoding);
879 free(encoding.ptr);
880 return equal;
881 }
882
883 /**
884 * Implementation of x509_ac_t.destroy
885 */
886 static void destroy(private_x509_ac_t *this)
887 {
888 if (ref_put(&this->ref))
889 {
890 DESTROY_IF(this->holderIssuer);
891 DESTROY_IF(this->entityName);
892 DESTROY_IF(this->issuerName);
893 DESTROY_IF(this->holderCert);
894 DESTROY_IF(this->signerCert);
895 DESTROY_IF(this->signerKey);
896 DESTROY_IF(this->charging);
897 DESTROY_IF(this->groups);
898 free(this->serialNumber.ptr);
899 free(this->authKeyIdentifier.ptr);
900 free(this->encoding.ptr);
901 free(this);
902 }
903 }
904
905 /**
906 * create an empty but initialized X.509 attribute certificate
907 */
908 static private_x509_ac_t *create_empty(void)
909 {
910 private_x509_ac_t *this = malloc_thing(private_x509_ac_t);
911
912 /* public functions */
913 this->public.interface.get_serial = (chunk_t (*)(ac_t*))get_serial;
914 this->public.interface.get_holderSerial = (chunk_t (*)(ac_t*))get_holderSerial;
915 this->public.interface.get_holderIssuer = (identification_t* (*)(ac_t*))get_holderIssuer;
916 this->public.interface.get_authKeyIdentifier = (chunk_t (*)(ac_t*))get_authKeyIdentifier;
917 this->public.interface.get_groups = (ietf_attributes_t* (*)(ac_t*))get_groups;
918 this->public.interface.certificate.get_type = (certificate_type_t (*)(certificate_t *this))get_type;
919 this->public.interface.certificate.get_subject = (identification_t* (*)(certificate_t *this))get_subject;
920 this->public.interface.certificate.get_issuer = (identification_t* (*)(certificate_t *this))get_issuer;
921 this->public.interface.certificate.has_subject = (id_match_t(*)(certificate_t*, identification_t *subject))has_subject;
922 this->public.interface.certificate.has_issuer = (id_match_t(*)(certificate_t*, identification_t *issuer))has_issuer;
923 this->public.interface.certificate.issued_by = (bool (*)(certificate_t *this, certificate_t *issuer))issued_by;
924 this->public.interface.certificate.get_public_key = (public_key_t* (*)(certificate_t *this))get_public_key;
925 this->public.interface.certificate.get_validity = (bool(*)(certificate_t*, time_t *when, time_t *, time_t*))get_validity;
926 this->public.interface.certificate.is_newer = (bool (*)(certificate_t*,certificate_t*))is_newer;
927 this->public.interface.certificate.get_encoding = (chunk_t(*)(certificate_t*))get_encoding;
928 this->public.interface.certificate.equals = (bool(*)(certificate_t*, certificate_t *other))equals;
929 this->public.interface.certificate.get_ref = (certificate_t* (*)(certificate_t *this))get_ref;
930 this->public.interface.certificate.destroy = (void (*)(certificate_t *this))destroy;
931
932 /* initialize */
933 this->encoding = chunk_empty;
934 this->serialNumber = chunk_empty;
935 this->holderSerial = chunk_empty;
936 this->authKeyIdentifier = chunk_empty;
937 this->holderIssuer = NULL;
938 this->entityName = NULL;
939 this->issuerName = NULL;
940 this->holderCert = NULL;
941 this->signerCert = NULL;
942 this->signerKey = NULL;
943 this->charging = NULL;
944 this->groups = NULL;
945 this->ref = 1;
946
947 return this;
948 }
949
950 /**
951 * See header.
952 */
953 x509_ac_t *x509_ac_load(certificate_type_t type, va_list args)
954 {
955 chunk_t blob = chunk_empty;
956
957 while (TRUE)
958 {
959 switch (va_arg(args, builder_part_t))
960 {
961 case BUILD_BLOB_ASN1_DER:
962 blob = va_arg(args, chunk_t);
963 continue;
964 case BUILD_END:
965 break;
966 default:
967 return NULL;
968 }
969 break;
970 }
971 if (blob.ptr)
972 {
973 private_x509_ac_t *ac = create_empty();
974
975 ac->encoding = chunk_clone(blob);
976 if (parse_certificate(ac))
977 {
978 return &ac->public;
979 }
980 destroy(ac);
981 }
982 return NULL;
983 }
984
985 /**
986 * See header.
987 */
988 x509_ac_t *x509_ac_gen(certificate_type_t type, va_list args)
989 {
990 private_x509_ac_t *ac;
991
992 ac = create_empty();
993 while (TRUE)
994 {
995 switch (va_arg(args, builder_part_t))
996 {
997 case BUILD_NOT_BEFORE_TIME:
998 ac->notBefore = va_arg(args, time_t);
999 continue;
1000 case BUILD_NOT_AFTER_TIME:
1001 ac->notAfter = va_arg(args, time_t);
1002 continue;
1003 case BUILD_SERIAL:
1004 ac->serialNumber = chunk_clone(va_arg(args, chunk_t));
1005 continue;
1006 case BUILD_IETF_GROUP_ATTR:
1007 ac->groups = ietf_attributes_create_from_string(va_arg(args, char*));
1008 continue;
1009 case BUILD_CERT:
1010 ac->holderCert = va_arg(args, certificate_t*);
1011 ac->holderCert->get_ref(ac->holderCert);
1012 continue;
1013 case BUILD_SIGNING_CERT:
1014 ac->signerCert = va_arg(args, certificate_t*);
1015 ac->signerCert->get_ref(ac->signerCert);
1016 continue;
1017 case BUILD_SIGNING_KEY:
1018 ac->signerKey = va_arg(args, private_key_t*);
1019 ac->signerKey->get_ref(ac->signerKey);
1020 continue;
1021 case BUILD_END:
1022 break;
1023 default:
1024 destroy(ac);
1025 return NULL;
1026 }
1027 break;
1028 }
1029
1030 if (ac->signerKey && ac->holderCert && ac->signerCert &&
1031 ac->holderCert->get_type(ac->holderCert) == CERT_X509 &&
1032 ac->signerCert->get_type(ac->signerCert) == CERT_X509)
1033 {
1034 ac->encoding = build_ac(ac);
1035 return &ac->public;
1036 }
1037 destroy(ac);
1038 return NULL;
1039 }
1040