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