2 * Copyright (C) 2002 Ueli Galizzi, Ariane Seiler
3 * Copyright (C) 2003 Martin Berner, Lukas Suter
4 * Copyright (C) 2002-2008 Andreas Steffen
6 * Hochschule fuer Technik Rapperswil
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 #include "ietf_attr_list.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/keys/private_key.h>
34 extern chunk_t
x509_parse_authorityKeyIdentifier(chunk_t blob
,
35 int level0
, chunk_t
*authKeySerialNumber
);
37 typedef struct private_x509_ac_t private_x509_ac_t
;
40 * private data of x509_ac_t object
42 struct private_x509_ac_t
{
50 * X.509 attribute certificate encoding in ASN.1 DER format
55 * X.509 attribute certificate body over which signature is computed
57 chunk_t certificateInfo
;
60 * Version of the X.509 attribute certificate
65 * Serial number of the X.509 attribute certificate
70 * ID representing the issuer of the holder certificate
72 identification_t
*holderIssuer
;
75 * Serial number of the holder certificate
80 * ID representing the holder
82 identification_t
*entityName
;
85 * ID representing the attribute certificate issuer
87 identification_t
*issuerName
;
90 * Start time of certificate validity
95 * End time of certificate validity
100 * List of charging attributes
102 linked_list_t
*charging
;
105 * List of groub attributes
107 linked_list_t
*groups
;
110 * Authority Key Identifier
112 chunk_t authKeyIdentifier
;
115 * Authority Key Serial Number
117 chunk_t authKeySerialNumber
;
120 * No revocation information available
125 * Signature algorithm
137 certificate_t
*holderCert
;
142 certificate_t
*signerCert
;
145 * Signer private key;
147 private_key_t
*signerKey
;
155 static u_char ASN1_group_oid_str
[] = {
157 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x0a ,0x04
160 static const chunk_t ASN1_group_oid
= chunk_from_buf(ASN1_group_oid_str
);
162 static u_char ASN1_authorityKeyIdentifier_oid_str
[] = {
167 static const chunk_t ASN1_authorityKeyIdentifier_oid
=
168 chunk_from_buf(ASN1_authorityKeyIdentifier_oid_str
);
170 static u_char ASN1_noRevAvail_ext_str
[] = {
178 static const chunk_t ASN1_noRevAvail_ext
= chunk_from_buf(ASN1_noRevAvail_ext_str
);
181 * declaration of function implemented in x509_cert.c
183 extern void x509_parse_generalNames(chunk_t blob
, int level0
, bool implicit
,
184 linked_list_t
*list
);
186 * parses a directoryName
188 static bool parse_directoryName(chunk_t blob
, int level
, bool implicit
, identification_t
**name
)
190 bool has_directoryName
;
191 linked_list_t
*list
= linked_list_create();
193 x509_parse_generalNames(blob
, level
, implicit
, list
);
194 has_directoryName
= list
->get_count(list
) > 0;
196 if (has_directoryName
)
198 iterator_t
*iterator
= list
->create_iterator(list
, TRUE
);
199 identification_t
*directoryName
;
202 while (iterator
->iterate(iterator
, (void**)&directoryName
))
206 *name
= directoryName
;
211 DBG1("more than one directory name - first selected");
212 directoryName
->destroy(directoryName
);
215 iterator
->destroy(iterator
);
219 DBG1("no directoryName found");
223 return has_directoryName
;
227 * ASN.1 definition of roleSyntax
229 static const asn1Object_t roleSyntaxObjects
[] =
231 { 0, "roleSyntax", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
232 { 1, "roleAuthority", ASN1_CONTEXT_C_0
, ASN1_OPT
|
234 { 1, "end opt", ASN1_EOC
, ASN1_END
}, /* 2 */
235 { 1, "roleName", ASN1_CONTEXT_C_1
, ASN1_OBJ
}, /* 3 */
236 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
242 static void parse_roleSyntax(chunk_t blob
, int level0
)
244 asn1_parser_t
*parser
;
248 parser
= asn1_parser_create(roleSyntaxObjects
, blob
);
249 parser
->set_top_level(parser
, level0
);
251 while (parser
->iterate(parser
, &objectID
, &object
))
259 parser
->destroy(parser
);
263 * ASN.1 definition of an X509 attribute certificate
265 static const asn1Object_t acObjects
[] =
267 { 0, "AttributeCertificate", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 0 */
268 { 1, "AttributeCertificateInfo", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 1 */
269 { 2, "version", ASN1_INTEGER
, ASN1_DEF
|
271 { 2, "holder", ASN1_SEQUENCE
, ASN1_NONE
}, /* 3 */
272 { 3, "baseCertificateID", ASN1_CONTEXT_C_0
, ASN1_OPT
}, /* 4 */
273 { 4, "issuer", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 5 */
274 { 4, "serial", ASN1_INTEGER
, ASN1_BODY
}, /* 6 */
275 { 4, "issuerUID", ASN1_BIT_STRING
, ASN1_OPT
|
277 { 4, "end opt", ASN1_EOC
, ASN1_END
}, /* 8 */
278 { 3, "end opt", ASN1_EOC
, ASN1_END
}, /* 9 */
279 { 3, "entityName", ASN1_CONTEXT_C_1
, ASN1_OPT
|
281 { 3, "end opt", ASN1_EOC
, ASN1_END
}, /* 11 */
282 { 3, "objectDigestInfo", ASN1_CONTEXT_C_2
, ASN1_OPT
}, /* 12 */
283 { 4, "digestedObjectType", ASN1_ENUMERATED
, ASN1_BODY
}, /* 13 */
284 { 4, "otherObjectTypeID", ASN1_OID
, ASN1_OPT
|
285 ASN1_BODY
}, /* 14 */
286 { 4, "end opt", ASN1_EOC
, ASN1_END
}, /* 15 */
287 { 4, "digestAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 16 */
288 { 3, "end opt", ASN1_EOC
, ASN1_END
}, /* 17 */
289 { 2, "v2Form", ASN1_CONTEXT_C_0
, ASN1_NONE
}, /* 18 */
290 { 3, "issuerName", ASN1_SEQUENCE
, ASN1_OPT
|
292 { 3, "end opt", ASN1_EOC
, ASN1_END
}, /* 20 */
293 { 3, "baseCertificateID", ASN1_CONTEXT_C_0
, ASN1_OPT
}, /* 21 */
294 { 4, "issuerSerial", ASN1_SEQUENCE
, ASN1_NONE
}, /* 22 */
295 { 5, "issuer", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 23 */
296 { 5, "serial", ASN1_INTEGER
, ASN1_BODY
}, /* 24 */
297 { 5, "issuerUID", ASN1_BIT_STRING
, ASN1_OPT
|
298 ASN1_BODY
}, /* 25 */
299 { 5, "end opt", ASN1_EOC
, ASN1_END
}, /* 26 */
300 { 3, "end opt", ASN1_EOC
, ASN1_END
}, /* 27 */
301 { 3, "objectDigestInfo", ASN1_CONTEXT_C_1
, ASN1_OPT
}, /* 28 */
302 { 4, "digestInfo", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 29 */
303 { 5, "digestedObjectType", ASN1_ENUMERATED
, ASN1_BODY
}, /* 30 */
304 { 5, "otherObjectTypeID", ASN1_OID
, ASN1_OPT
|
305 ASN1_BODY
}, /* 31 */
306 { 5, "end opt", ASN1_EOC
, ASN1_END
}, /* 32 */
307 { 5, "digestAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 33 */
308 { 3, "end opt", ASN1_EOC
, ASN1_END
}, /* 34 */
309 { 2, "signature", ASN1_EOC
, ASN1_RAW
}, /* 35 */
310 { 2, "serialNumber", ASN1_INTEGER
, ASN1_BODY
}, /* 36 */
311 { 2, "attrCertValidityPeriod", ASN1_SEQUENCE
, ASN1_NONE
}, /* 37 */
312 { 3, "notBeforeTime", ASN1_GENERALIZEDTIME
, ASN1_BODY
}, /* 38 */
313 { 3, "notAfterTime", ASN1_GENERALIZEDTIME
, ASN1_BODY
}, /* 39 */
314 { 2, "attributes", ASN1_SEQUENCE
, ASN1_LOOP
}, /* 40 */
315 { 3, "attribute", ASN1_SEQUENCE
, ASN1_NONE
}, /* 41 */
316 { 4, "type", ASN1_OID
, ASN1_BODY
}, /* 42 */
317 { 4, "values", ASN1_SET
, ASN1_LOOP
}, /* 43 */
318 { 5, "value", ASN1_EOC
, ASN1_RAW
}, /* 44 */
319 { 4, "end loop", ASN1_EOC
, ASN1_END
}, /* 45 */
320 { 2, "end loop", ASN1_EOC
, ASN1_END
}, /* 46 */
321 { 2, "extensions", ASN1_SEQUENCE
, ASN1_LOOP
}, /* 47 */
322 { 3, "extension", ASN1_SEQUENCE
, ASN1_NONE
}, /* 48 */
323 { 4, "extnID", ASN1_OID
, ASN1_BODY
}, /* 49 */
324 { 4, "critical", ASN1_BOOLEAN
, ASN1_DEF
|
325 ASN1_BODY
}, /* 50 */
326 { 4, "extnValue", ASN1_OCTET_STRING
, ASN1_BODY
}, /* 51 */
327 { 2, "end loop", ASN1_EOC
, ASN1_END
}, /* 52 */
328 { 1, "signatureAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 53 */
329 { 1, "signatureValue", ASN1_BIT_STRING
, ASN1_BODY
}, /* 54 */
330 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
332 #define AC_OBJ_CERTIFICATE_INFO 1
333 #define AC_OBJ_VERSION 2
334 #define AC_OBJ_HOLDER_ISSUER 5
335 #define AC_OBJ_HOLDER_SERIAL 6
336 #define AC_OBJ_ENTITY_NAME 10
337 #define AC_OBJ_ISSUER_NAME 19
338 #define AC_OBJ_ISSUER 23
339 #define AC_OBJ_SIG_ALG 35
340 #define AC_OBJ_SERIAL_NUMBER 36
341 #define AC_OBJ_NOT_BEFORE 38
342 #define AC_OBJ_NOT_AFTER 39
343 #define AC_OBJ_ATTRIBUTE_TYPE 42
344 #define AC_OBJ_ATTRIBUTE_VALUE 44
345 #define AC_OBJ_EXTN_ID 49
346 #define AC_OBJ_CRITICAL 50
347 #define AC_OBJ_EXTN_VALUE 51
348 #define AC_OBJ_ALGORITHM 53
349 #define AC_OBJ_SIGNATURE 54
352 * Parses an X.509 attribute certificate
354 static bool parse_certificate(private_x509_ac_t
*this)
356 asn1_parser_t
*parser
;
359 int type
= OID_UNKNOWN
;
360 int extn_oid
= OID_UNKNOWN
;
361 int sig_alg
= OID_UNKNOWN
;
362 bool success
= FALSE
;
365 parser
= asn1_parser_create(acObjects
, this->encoding
);
367 while (parser
->iterate(parser
, &objectID
, &object
))
369 u_int level
= parser
->get_level(parser
)+1;
373 case AC_OBJ_CERTIFICATE_INFO
:
374 this->certificateInfo
= object
;
377 this->version
= (object
.len
) ?
(1 + (u_int
)*object
.ptr
) : 1;
378 DBG2(" v%d", this->version
);
379 if (this->version
!= 2)
381 DBG1("v%d attribute certificates are not supported", this->version
);
385 case AC_OBJ_HOLDER_ISSUER
:
386 if (!parse_directoryName(object
, level
, FALSE
, &this->holderIssuer
))
391 case AC_OBJ_HOLDER_SERIAL
:
392 this->holderSerial
= object
;
394 case AC_OBJ_ENTITY_NAME
:
395 if (!parse_directoryName(object
, level
, TRUE
, &this->entityName
))
400 case AC_OBJ_ISSUER_NAME
:
401 if (!parse_directoryName(object
, level
, FALSE
, &this->issuerName
))
407 sig_alg
= asn1_parse_algorithmIdentifier(object
, level
, NULL
);
409 case AC_OBJ_SERIAL_NUMBER
:
410 this->serialNumber
= chunk_clone(object
);
412 case AC_OBJ_NOT_BEFORE
:
413 this->notBefore
= asn1_to_time(&object
, ASN1_GENERALIZEDTIME
);
415 case AC_OBJ_NOT_AFTER
:
416 this->notAfter
= asn1_to_time(&object
, ASN1_GENERALIZEDTIME
);
418 case AC_OBJ_ATTRIBUTE_TYPE
:
419 type
= asn1_known_oid(object
);
421 case AC_OBJ_ATTRIBUTE_VALUE
:
425 case OID_AUTHENTICATION_INFO
:
426 DBG2(" need to parse authenticationInfo");
428 case OID_ACCESS_IDENTITY
:
429 DBG2(" need to parse accessIdentity");
431 case OID_CHARGING_IDENTITY
:
432 ietfAttr_list_create_from_chunk(object
, this->charging
, level
);
435 ietfAttr_list_create_from_chunk(object
, this->groups
, level
);
438 parse_roleSyntax(object
, level
);
446 extn_oid
= asn1_known_oid(object
);
448 case AC_OBJ_CRITICAL
:
449 critical
= object
.len
&& *object
.ptr
;
450 DBG2(" %s",(critical
)?
"TRUE":"FALSE");
452 case AC_OBJ_EXTN_VALUE
:
456 case OID_CRL_DISTRIBUTION_POINTS
:
457 DBG2(" need to parse crlDistributionPoints");
459 case OID_AUTHORITY_KEY_ID
:
460 this->authKeyIdentifier
= x509_parse_authorityKeyIdentifier(object
,
461 level
, &this->authKeySerialNumber
);
463 case OID_TARGET_INFORMATION
:
464 DBG2(" need to parse targetInformation");
466 case OID_NO_REV_AVAIL
:
467 this->noRevAvail
= TRUE
;
474 case AC_OBJ_ALGORITHM
:
475 this->algorithm
= asn1_parse_algorithmIdentifier(object
, level
,
477 if (this->algorithm
!= sig_alg
)
479 DBG1(" signature algorithms do not agree");
484 case AC_OBJ_SIGNATURE
:
485 this->signature
= object
;
491 success
= parser
->success(parser
);
494 parser
->destroy(parser
);
499 * build directoryName
501 static chunk_t
build_directoryName(asn1_t tag
, chunk_t name
)
503 return asn1_wrap(tag
, "m",
504 asn1_simple_object(ASN1_CONTEXT_C_4
, name
));
510 static chunk_t
build_holder(private_x509_ac_t
*this)
512 x509_t
* x509
= (x509_t
*)this->holderCert
;
513 identification_t
*issuer
= this->holderCert
->get_issuer(this->holderCert
);
514 identification_t
*subject
= this->holderCert
->get_subject(this->holderCert
);
516 return asn1_wrap(ASN1_SEQUENCE
, "mm",
517 asn1_wrap(ASN1_CONTEXT_C_0
, "mm",
518 build_directoryName(ASN1_SEQUENCE
, issuer
->get_encoding(issuer
)),
519 asn1_simple_object(ASN1_INTEGER
, x509
->get_serial(x509
))
521 build_directoryName(ASN1_CONTEXT_C_1
, subject
->get_encoding(subject
)));
527 static chunk_t
build_v2_form(private_x509_ac_t
*this)
529 identification_t
*subject
= this->signerCert
->get_subject(this->signerCert
);
531 return asn1_wrap(ASN1_CONTEXT_C_0
, "m",
532 build_directoryName(ASN1_SEQUENCE
, subject
->get_encoding(subject
)));
536 * build attrCertValidityPeriod
538 static chunk_t
build_attr_cert_validity(private_x509_ac_t
*this)
540 return asn1_wrap(ASN1_SEQUENCE
, "mm",
541 asn1_from_time(&this->notBefore
, ASN1_GENERALIZEDTIME
),
542 asn1_from_time(&this->notAfter
, ASN1_GENERALIZEDTIME
));
547 * build attribute type
549 static chunk_t
build_attribute_type(const chunk_t type
, chunk_t content
)
551 return asn1_wrap(ASN1_SEQUENCE
, "cm",
553 asn1_wrap(ASN1_SET
, "m", content
));
559 static chunk_t
build_attributes(private_x509_ac_t
*this)
561 return asn1_wrap(ASN1_SEQUENCE
, "m",
562 build_attribute_type(ASN1_group_oid
, ietfAttr_list_encode(this->groups
)));
566 * build authorityKeyIdentifier
568 static chunk_t
build_authorityKeyIdentifier(private_x509_ac_t
*this)
570 chunk_t keyIdentifier
= chunk_empty
;
571 chunk_t authorityCertIssuer
;
572 chunk_t authorityCertSerialNumber
;
573 identification_t
*issuer
;
574 public_key_t
*public;
577 x509
= (x509_t
*)this->signerCert
;
578 issuer
= this->signerCert
->get_issuer(this->signerCert
);
579 public = this->signerCert
->get_public_key(this->signerCert
);
582 if (public->get_fingerprint(public, KEY_ID_PUBKEY_SHA1
, &keyIdentifier
))
584 this->authKeyIdentifier
= chunk_clone(keyIdentifier
);
586 public->destroy(public);
588 authorityCertIssuer
= build_directoryName(ASN1_CONTEXT_C_1
,
589 issuer
->get_encoding(issuer
));
590 authorityCertSerialNumber
= asn1_simple_object(ASN1_CONTEXT_S_2
,
591 x509
->get_serial(x509
));
592 return asn1_wrap(ASN1_SEQUENCE
, "cm",
593 ASN1_authorityKeyIdentifier_oid
,
594 asn1_wrap(ASN1_OCTET_STRING
, "m",
595 asn1_wrap(ASN1_SEQUENCE
, "cmm",
598 authorityCertSerialNumber
607 static chunk_t
build_extensions(private_x509_ac_t
*this)
609 return asn1_wrap(ASN1_SEQUENCE
, "mc",
610 build_authorityKeyIdentifier(this),
611 ASN1_noRevAvail_ext
);
615 * build attributeCertificateInfo
617 static chunk_t
build_attr_cert_info(private_x509_ac_t
*this)
619 return asn1_wrap(ASN1_SEQUENCE
, "cmmmmmmm",
623 asn1_algorithmIdentifier(OID_SHA1_WITH_RSA
),
624 asn1_simple_object(ASN1_INTEGER
, this->serialNumber
),
625 build_attr_cert_validity(this),
626 build_attributes(this),
627 build_extensions(this));
632 * build an X.509 attribute certificate
634 static chunk_t
build_ac(private_x509_ac_t
*this)
636 chunk_t signatureValue
;
637 chunk_t attributeCertificateInfo
;
639 attributeCertificateInfo
= build_attr_cert_info(this);
641 this->signerKey
->sign(this->signerKey
, SIGN_RSA_EMSA_PKCS1_SHA1
,
642 attributeCertificateInfo
, &signatureValue
);
644 return asn1_wrap(ASN1_SEQUENCE
, "mmm",
645 attributeCertificateInfo
,
646 asn1_algorithmIdentifier(OID_SHA1_WITH_RSA
),
647 asn1_bitstring("m", signatureValue
));
651 * Implementation of ac_t.get_serial.
653 static chunk_t
get_serial(private_x509_ac_t
*this)
655 return this->serialNumber
;
659 * Implementation of ac_t.get_holderSerial.
661 static chunk_t
get_holderSerial(private_x509_ac_t
*this)
663 return this->holderSerial
;
667 * Implementation of ac_t.get_holderIssuer.
669 static identification_t
* get_holderIssuer(private_x509_ac_t
*this)
671 return this->holderIssuer
;
675 * Implementation of ac_t.get_authKeyIdentifier.
677 static chunk_t
get_authKeyIdentifier(private_x509_ac_t
*this)
679 return this->authKeyIdentifier
;
683 * Implementation of certificate_t.get_type
685 static certificate_type_t
get_type(private_x509_ac_t
*this)
691 * Implementation of certificate_t.get_subject
693 static identification_t
* get_subject(private_x509_ac_t
*this)
695 return this->entityName
;
699 * Implementation of certificate_t.get_issuer
701 static identification_t
* get_issuer(private_x509_ac_t
*this)
703 return this->issuerName
;
707 * Implementation of certificate_t.has_subject.
709 static id_match_t
has_subject(private_x509_ac_t
*this, identification_t
*subject
)
711 return ID_MATCH_NONE
;
715 * Implementation of certificate_t.has_issuer.
717 static id_match_t
has_issuer(private_x509_ac_t
*this, identification_t
*issuer
)
719 if (issuer
->get_type(issuer
) == ID_KEY_ID
&& this->authKeyIdentifier
.ptr
&&
720 chunk_equals(this->authKeyIdentifier
, issuer
->get_encoding(issuer
)))
722 return ID_MATCH_PERFECT
;
724 return this->issuerName
->matches(this->issuerName
, issuer
);
728 * Implementation of certificate_t.issued_by
730 static bool issued_by(private_x509_ac_t
*this, certificate_t
*issuer
)
733 signature_scheme_t scheme
;
735 x509_t
*x509
= (x509_t
*)issuer
;
737 /* check if issuer is an X.509 AA certificate */
738 if (issuer
->get_type(issuer
) != CERT_X509
)
742 if (!(x509
->get_flags(x509
) & X509_AA
))
747 /* get the public key of the issuer */
748 key
= issuer
->get_public_key(issuer
);
750 /* compare keyIdentifiers if available, otherwise use DNs */
751 if (this->authKeyIdentifier
.ptr
&& key
)
755 if (!key
->get_fingerprint(key
, KEY_ID_PUBKEY_SHA1
, &fingerprint
) ||
756 !chunk_equals(fingerprint
, this->authKeyIdentifier
))
763 if (!this->issuerName
->equals(this->issuerName
,
764 issuer
->get_subject(issuer
)))
770 /* determine signature scheme */
771 scheme
= signature_scheme_from_oid(this->algorithm
);
773 if (scheme
== SIGN_UNKNOWN
|| key
== NULL
)
777 valid
= key
->verify(key
, scheme
, this->certificateInfo
, this->signature
);
783 * Implementation of certificate_t.get_public_key.
785 static public_key_t
* get_public_key(private_x509_ac_t
*this)
791 * Implementation of certificate_t.get_ref.
793 static private_x509_ac_t
* get_ref(private_x509_ac_t
*this)
800 * Implementation of certificate_t.get_validity.
802 static bool get_validity(private_x509_ac_t
*this, time_t *when
,
803 time_t *not_before
, time_t *not_after
)
817 *not_before
= this->notBefore
;
821 *not_after
= this->notAfter
;
823 return (t
>= this->notBefore
&& t
<= this->notAfter
);
827 * Implementation of certificate_t.is_newer.
829 static bool is_newer(private_x509_ac_t
*this, ac_t
*that
)
831 certificate_t
*this_cert
= &this->public.interface
.certificate
;
832 certificate_t
*that_cert
= &that
->certificate
;
833 time_t this_update
, that_update
, now
= time(NULL
);
836 this_cert
->get_validity(this_cert
, &now
, &this_update
, NULL
);
837 that_cert
->get_validity(that_cert
, &now
, &that_update
, NULL
);
838 new = this_update
> that_update
;
839 DBG1(" attr cert from %T is %s - existing attr_cert from %T %s",
840 &this_update
, FALSE
, new ?
"newer":"not newer",
841 &that_update
, FALSE
, new ?
"replaced":"retained");
846 * Implementation of certificate_t.get_encoding.
848 static chunk_t
get_encoding(private_x509_ac_t
*this)
850 return chunk_clone(this->encoding
);
854 * Implementation of certificate_t.equals.
856 static bool equals(private_x509_ac_t
*this, certificate_t
*other
)
861 if ((certificate_t
*)this == other
)
865 if (other
->equals
== (void*)equals
)
866 { /* skip allocation if we have the same implementation */
867 return chunk_equals(this->encoding
, ((private_x509_ac_t
*)other
)->encoding
);
869 encoding
= other
->get_encoding(other
);
870 equal
= chunk_equals(this->encoding
, encoding
);
876 * Implementation of x509_ac_t.destroy
878 static void destroy(private_x509_ac_t
*this)
880 if (ref_put(&this->ref
))
882 DESTROY_IF(this->holderIssuer
);
883 DESTROY_IF(this->entityName
);
884 DESTROY_IF(this->issuerName
);
885 DESTROY_IF(this->holderCert
);
886 DESTROY_IF(this->signerCert
);
887 DESTROY_IF(this->signerKey
);
889 ietfAttr_list_destroy(this->charging
);
890 ietfAttr_list_destroy(this->groups
);
891 free(this->serialNumber
.ptr
);
892 free(this->authKeyIdentifier
.ptr
);
893 free(this->encoding
.ptr
);
899 * create an empty but initialized X.509 attribute certificate
901 static private_x509_ac_t
*create_empty(void)
903 private_x509_ac_t
*this = malloc_thing(private_x509_ac_t
);
905 /* public functions */
906 this->public.interface
.get_serial
= (chunk_t (*)(ac_t
*))get_serial
;
907 this->public.interface
.get_holderSerial
= (chunk_t (*)(ac_t
*))get_holderSerial
;
908 this->public.interface
.get_holderIssuer
= (identification_t
* (*)(ac_t
*))get_holderIssuer
;
909 this->public.interface
.get_authKeyIdentifier
= (chunk_t(*)(ac_t
*))get_authKeyIdentifier
;
910 this->public.interface
.certificate
.get_type
= (certificate_type_t (*)(certificate_t
*this))get_type
;
911 this->public.interface
.certificate
.get_subject
= (identification_t
* (*)(certificate_t
*this))get_subject
;
912 this->public.interface
.certificate
.get_issuer
= (identification_t
* (*)(certificate_t
*this))get_issuer
;
913 this->public.interface
.certificate
.has_subject
= (id_match_t(*)(certificate_t
*, identification_t
*subject
))has_subject
;
914 this->public.interface
.certificate
.has_issuer
= (id_match_t(*)(certificate_t
*, identification_t
*issuer
))has_issuer
;
915 this->public.interface
.certificate
.issued_by
= (bool (*)(certificate_t
*this, certificate_t
*issuer
))issued_by
;
916 this->public.interface
.certificate
.get_public_key
= (public_key_t
* (*)(certificate_t
*this))get_public_key
;
917 this->public.interface
.certificate
.get_validity
= (bool(*)(certificate_t
*, time_t *when
, time_t *, time_t*))get_validity
;
918 this->public.interface
.certificate
.is_newer
= (bool (*)(certificate_t
*,certificate_t
*))is_newer
;
919 this->public.interface
.certificate
.get_encoding
= (chunk_t(*)(certificate_t
*))get_encoding
;
920 this->public.interface
.certificate
.equals
= (bool(*)(certificate_t
*, certificate_t
*other
))equals
;
921 this->public.interface
.certificate
.get_ref
= (certificate_t
* (*)(certificate_t
*this))get_ref
;
922 this->public.interface
.certificate
.destroy
= (void (*)(certificate_t
*this))destroy
;
925 this->encoding
= chunk_empty
;
926 this->serialNumber
= chunk_empty
;
927 this->holderSerial
= chunk_empty
;
928 this->authKeyIdentifier
= chunk_empty
;
929 this->holderIssuer
= NULL
;
930 this->entityName
= NULL
;
931 this->issuerName
= NULL
;
932 this->holderCert
= NULL
;
933 this->signerCert
= NULL
;
934 this->signerKey
= NULL
;
935 this->charging
= linked_list_create();
936 this->groups
= linked_list_create();
943 * create X.509 attribute certificate from a chunk
945 static private_x509_ac_t
* create_from_chunk(chunk_t chunk
)
947 private_x509_ac_t
*this = create_empty();
949 this->encoding
= chunk
;
950 if (!parse_certificate(this))
958 typedef struct private_builder_t private_builder_t
;
960 * Builder implementation for certificate loading
962 struct private_builder_t
{
963 /** implements the builder interface */
965 /** X.509 attribute certificate to build */
966 private_x509_ac_t
*ac
;
970 * Implementation of builder_t.build
972 static private_x509_ac_t
* build(private_builder_t
*this)
974 private_x509_ac_t
*ac
= this->ac
;
978 /* synthesis if encoding does not exist */
979 if (ac
&& ac
->encoding
.ptr
== NULL
)
981 if (ac
->holderCert
&& ac
->signerCert
&& ac
->signerKey
)
983 ac
->encoding
= build_ac(ac
);
996 * Implementation of builder_t.add
998 static void add(private_builder_t
*this, builder_part_t part
, ...)
1001 certificate_t
*cert
;
1004 va_start(args
, part
);
1007 case BUILD_BLOB_ASN1_DER
:
1012 chunk
= va_arg(args
, chunk_t
);
1013 this->ac
= create_from_chunk(chunk_clone(chunk
));
1015 case BUILD_NOT_BEFORE_TIME
:
1016 this->ac
->notBefore
= va_arg(args
, time_t);
1018 case BUILD_NOT_AFTER_TIME
:
1019 this->ac
->notAfter
= va_arg(args
, time_t);
1022 chunk
= va_arg(args
, chunk_t
);
1023 this->ac
->serialNumber
= chunk_clone(chunk
);
1025 case BUILD_IETF_GROUP_ATTR
:
1026 ietfAttr_list_create_from_string(va_arg(args
, char*),
1030 cert
= va_arg(args
, certificate_t
*);
1031 if (cert
->get_type(cert
) == CERT_X509
)
1033 this->ac
->holderCert
= cert
->get_ref(cert
);
1036 case BUILD_SIGNING_CERT
:
1037 cert
= va_arg(args
, certificate_t
*);
1038 if (cert
->get_type(cert
) == CERT_X509
)
1040 this->ac
->signerCert
= cert
->get_ref(cert
);
1043 case BUILD_SIGNING_KEY
:
1044 this->ac
->signerKey
= va_arg(args
, private_key_t
*);
1045 this->ac
->signerKey
->get_ref(this->ac
->signerKey
);
1048 /* abort if unsupported option */
1053 builder_cancel(&this->public);
1060 * Builder construction function
1062 builder_t
*x509_ac_builder(certificate_type_t type
)
1064 private_builder_t
*this;
1066 if (type
!= CERT_X509_AC
)
1071 this = malloc_thing(private_builder_t
);
1073 this->ac
= create_empty();
1074 this->public.add
= (void(*)(builder_t
*this, builder_part_t part
, ...))add
;
1075 this->public.build
= (void*(*)(builder_t
*this))build
;
1077 return &this->public;