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
22 #include "ietf_attr_list.h"
27 #include <asn1/asn1.h>
28 #include <asn1/asn1_parser.h>
30 #include <utils/identification.h>
31 #include <utils/linked_list.h>
32 #include <credentials/certificates/x509.h>
34 extern identification_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 identification_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
= 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
;
571 chunk_t authorityCertIssuer
;
572 chunk_t authorityCertSerialNumber
;
573 x509_t
*x509
= (x509_t
*)this->signerCert
;
574 identification_t
*issuer
= this->signerCert
->get_issuer(this->signerCert
);
575 public_key_t
*public = this->signerCert
->get_public_key(this->signerCert
);
579 identification_t
*keyid
= public->get_id(public, ID_PUBKEY_SHA1
);
581 this->authKeyIdentifier
= keyid
= keyid
->clone(keyid
);
582 keyIdentifier
= keyid
->get_encoding(keyid
);
583 public->destroy(public);
587 keyIdentifier
= chunk_empty
;
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",
599 authorityCertSerialNumber
608 static chunk_t
build_extensions(private_x509_ac_t
*this)
610 return asn1_wrap(ASN1_SEQUENCE
, "mc",
611 build_authorityKeyIdentifier(this),
612 ASN1_noRevAvail_ext
);
616 * build attributeCertificateInfo
618 static chunk_t
build_attr_cert_info(private_x509_ac_t
*this)
620 return asn1_wrap(ASN1_SEQUENCE
, "cmmcmmmm",
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));
633 * build an X.509 attribute certificate
635 static chunk_t
build_ac(private_x509_ac_t
*this)
637 chunk_t signatureValue
;
638 chunk_t attributeCertificateInfo
;
640 attributeCertificateInfo
= build_attr_cert_info(this);
642 this->signerKey
->sign(this->signerKey
, SIGN_RSA_EMSA_PKCS1_SHA1
,
643 attributeCertificateInfo
, &signatureValue
);
645 return asn1_wrap(ASN1_SEQUENCE
, "mcm",
646 attributeCertificateInfo
,
647 asn1_algorithmIdentifier(OID_SHA1_WITH_RSA
),
648 asn1_bitstring("m", signatureValue
));
652 * Implementation of certificate_t.get_type
654 static certificate_type_t
get_type(private_x509_ac_t
*this)
660 * Implementation of certificate_t.get_subject
662 static identification_t
* get_subject(private_x509_ac_t
*this)
664 return this->entityName
;
668 * Implementation of certificate_t.get_issuer
670 static identification_t
* get_issuer(private_x509_ac_t
*this)
672 return this->issuerName
;
676 * Implementation of certificate_t.has_subject.
678 static id_match_t
has_subject(private_x509_ac_t
*this, identification_t
*subject
)
680 return ID_MATCH_NONE
;
684 * Implementation of certificate_t.has_issuer.
686 static id_match_t
has_issuer(private_x509_ac_t
*this, identification_t
*issuer
)
690 if (issuer
->get_type(issuer
) == ID_PUBKEY_SHA1
)
692 if (this->authKeyIdentifier
)
694 match
= issuer
->matches(issuer
, this->authKeyIdentifier
);
698 match
= ID_MATCH_NONE
;
703 match
= this->issuerName
->matches(this->issuerName
, issuer
);
709 * Implementation of certificate_t.issued_by
711 static bool issued_by(private_x509_ac_t
*this, certificate_t
*issuer
)
714 signature_scheme_t scheme
;
716 x509_t
*x509
= (x509_t
*)issuer
;
718 /* check if issuer is an X.509 AA certificate */
719 if (issuer
->get_type(issuer
) != CERT_X509
)
723 if (!(x509
->get_flags(x509
) & X509_AA
))
728 /* get the public key of the issuer */
729 key
= issuer
->get_public_key(issuer
);
731 /* compare keyIdentifiers if available, otherwise use DNs */
732 if (this->authKeyIdentifier
&& key
)
734 identification_t
*subjectKeyIdentifier
= key
->get_id(key
, ID_PUBKEY_SHA1
);
736 if (!subjectKeyIdentifier
->equals(subjectKeyIdentifier
,
737 this->authKeyIdentifier
))
744 if (!this->issuerName
->equals(this->issuerName
, issuer
->get_subject(issuer
)))
749 /* TODO: generic OID to scheme mapper? */
750 switch (this->algorithm
)
752 case OID_MD5_WITH_RSA
:
753 scheme
= SIGN_RSA_EMSA_PKCS1_MD5
;
755 case OID_SHA1_WITH_RSA
:
756 scheme
= SIGN_RSA_EMSA_PKCS1_SHA1
;
758 case OID_SHA256_WITH_RSA
:
759 scheme
= SIGN_RSA_EMSA_PKCS1_SHA256
;
761 case OID_SHA384_WITH_RSA
:
762 scheme
= SIGN_RSA_EMSA_PKCS1_SHA384
;
764 case OID_SHA512_WITH_RSA
:
765 scheme
= SIGN_RSA_EMSA_PKCS1_SHA512
;
774 valid
= key
->verify(key
, scheme
, this->certificateInfo
, this->signature
);
780 * Implementation of certificate_t.get_public_key.
782 static public_key_t
* get_public_key(private_x509_ac_t
*this)
788 * Implementation of certificate_t.get_ref.
790 static private_x509_ac_t
* get_ref(private_x509_ac_t
*this)
797 * Implementation of certificate_t.get_validity.
799 static bool get_validity(private_x509_ac_t
*this, time_t *when
,
800 time_t *not_before
, time_t *not_after
)
814 *not_before
= this->notBefore
;
818 *not_after
= this->notAfter
;
820 return (t
>= this->notBefore
&& t
<= this->notAfter
);
824 * Implementation of certificate_t.is_newer.
826 static bool is_newer(private_x509_ac_t
*this, ac_t
*that
)
828 certificate_t
*this_cert
= &this->public.interface
.certificate
;
829 certificate_t
*that_cert
= &that
->certificate
;
830 time_t this_update
, that_update
, now
= time(NULL
);
833 this_cert
->get_validity(this_cert
, &now
, &this_update
, NULL
);
834 that_cert
->get_validity(that_cert
, &now
, &that_update
, NULL
);
835 new = this_update
> that_update
;
836 DBG1(" attr cert from %#T is %s - existing attr_cert from %#T %s",
837 &this_update
, FALSE
, new ?
"newer":"not newer",
838 &that_update
, FALSE
, new ?
"replaced":"retained");
843 * Implementation of certificate_t.get_encoding.
845 static chunk_t
get_encoding(private_x509_ac_t
*this)
847 return chunk_clone(this->encoding
);
851 * Implementation of certificate_t.equals.
853 static bool equals(private_x509_ac_t
*this, certificate_t
*other
)
858 if ((certificate_t
*)this == other
)
862 if (other
->equals
== (void*)equals
)
863 { /* skip allocation if we have the same implementation */
864 return chunk_equals(this->encoding
, ((private_x509_ac_t
*)other
)->encoding
);
866 encoding
= other
->get_encoding(other
);
867 equal
= chunk_equals(this->encoding
, encoding
);
873 * Implementation of x509_ac_t.destroy
875 static void destroy(private_x509_ac_t
*this)
877 if (ref_put(&this->ref
))
879 DESTROY_IF(this->holderIssuer
);
880 DESTROY_IF(this->entityName
);
881 DESTROY_IF(this->issuerName
);
882 DESTROY_IF(this->authKeyIdentifier
);
883 DESTROY_IF(this->holderCert
);
884 DESTROY_IF(this->signerCert
);
885 DESTROY_IF(this->signerKey
);
887 ietfAttr_list_destroy(this->charging
);
888 ietfAttr_list_destroy(this->groups
);
889 free(this->encoding
.ptr
);
895 * create an empty but initialized X.509 attribute certificate
897 static private_x509_ac_t
*create_empty(void)
899 private_x509_ac_t
*this = malloc_thing(private_x509_ac_t
);
901 /* public functions */
902 this->public.interface
.certificate
.get_type
= (certificate_type_t (*)(certificate_t
*this))get_type
;
903 this->public.interface
.certificate
.get_subject
= (identification_t
* (*)(certificate_t
*this))get_subject
;
904 this->public.interface
.certificate
.get_issuer
= (identification_t
* (*)(certificate_t
*this))get_issuer
;
905 this->public.interface
.certificate
.has_subject
= (id_match_t(*)(certificate_t
*, identification_t
*subject
))has_subject
;
906 this->public.interface
.certificate
.has_issuer
= (id_match_t(*)(certificate_t
*, identification_t
*issuer
))has_issuer
;
907 this->public.interface
.certificate
.issued_by
= (bool (*)(certificate_t
*this, certificate_t
*issuer
))issued_by
;
908 this->public.interface
.certificate
.get_public_key
= (public_key_t
* (*)(certificate_t
*this))get_public_key
;
909 this->public.interface
.certificate
.get_validity
= (bool(*)(certificate_t
*, time_t *when
, time_t *, time_t*))get_validity
;
910 this->public.interface
.certificate
.is_newer
= (bool (*)(certificate_t
*,certificate_t
*))is_newer
;
911 this->public.interface
.certificate
.get_encoding
= (chunk_t(*)(certificate_t
*))get_encoding
;
912 this->public.interface
.certificate
.equals
= (bool(*)(certificate_t
*, certificate_t
*other
))equals
;
913 this->public.interface
.certificate
.get_ref
= (certificate_t
* (*)(certificate_t
*this))get_ref
;
914 this->public.interface
.certificate
.destroy
= (void (*)(certificate_t
*this))destroy
;
917 this->encoding
= chunk_empty
;
918 this->holderIssuer
= NULL
;
919 this->entityName
= NULL
;
920 this->issuerName
= NULL
;
921 this->authKeyIdentifier
= NULL
;
922 this->holderCert
= NULL
;
923 this->signerCert
= NULL
;
924 this->signerKey
= NULL
;
925 this->charging
= linked_list_create();
926 this->groups
= linked_list_create();
933 * create X.509 attribute certificate from a chunk
935 static private_x509_ac_t
* create_from_chunk(chunk_t chunk
)
937 private_x509_ac_t
*this = create_empty();
939 this->encoding
= chunk
;
940 if (!parse_certificate(this))
949 * create X.509 crl from a file
951 static private_x509_ac_t
* create_from_file(char *path
)
955 private_x509_ac_t
*this;
957 if (!pem_asn1_load_file(path
, NULL
, &chunk
, &pgp
))
962 this = create_from_chunk(chunk
);
966 DBG1(" could not parse loaded attribute certificate file '%s'", path
);
969 DBG1(" loaded attribute certificate file '%s'", path
);
973 typedef struct private_builder_t private_builder_t
;
975 * Builder implementation for certificate loading
977 struct private_builder_t
{
978 /** implements the builder interface */
980 /** X.509 attribute certificate to build */
981 private_x509_ac_t
*ac
;
985 * Implementation of builder_t.build
987 static private_x509_ac_t
* build(private_builder_t
*this)
989 private_x509_ac_t
*ac
= this->ac
;
998 /* synthesis if TRUE or analysis if FALSE */
999 if (ac
->encoding
.ptr
== NULL
)
1001 if (ac
->holderCert
&& ac
->signerCert
&& ac
->signerKey
)
1003 ac
->encoding
= build_ac(ac
);
1016 * Implementation of builder_t.add
1018 static void add(private_builder_t
*this, builder_part_t part
, ...)
1021 certificate_t
*cert
;
1023 va_start(args
, part
);
1026 case BUILD_FROM_FILE
:
1031 this->ac
= create_from_file(va_arg(args
, char*));
1033 case BUILD_BLOB_ASN1_DER
:
1038 this->ac
= create_from_chunk(va_arg(args
, chunk_t
));
1040 case BUILD_NOT_BEFORE_TIME
:
1041 this->ac
->notBefore
= va_arg(args
, time_t);
1043 case BUILD_NOT_AFTER_TIME
:
1044 this->ac
->notAfter
= va_arg(args
, time_t);
1047 this->ac
->serialNumber
= va_arg(args
, chunk_t
);
1049 case BUILD_IETF_GROUP_ATTR
:
1050 ietfAttr_list_create_from_string(va_arg(args
, char*),
1054 cert
= va_arg(args
, certificate_t
*);
1055 if (cert
->get_type(cert
) == CERT_X509
)
1057 this->ac
->holderCert
= cert
;
1061 cert
->destroy(cert
);
1064 case BUILD_SIGNING_CERT
:
1065 cert
= va_arg(args
, certificate_t
*);
1066 if (cert
->get_type(cert
) == CERT_X509
)
1068 this->ac
->signerCert
= cert
;
1072 cert
->destroy(cert
);
1075 case BUILD_SIGNING_KEY
:
1076 this->ac
->signerKey
= va_arg(args
, private_key_t
*);
1079 DBG1("ignoring unsupported build part %N", builder_part_names
, part
);
1086 * Builder construction function
1088 builder_t
*x509_ac_builder(certificate_type_t type
)
1090 private_builder_t
*this;
1092 if (type
!= CERT_X509_AC
)
1097 this = malloc_thing(private_builder_t
);
1099 this->ac
= create_empty();
1100 this->public.add
= (void(*)(builder_t
*this, builder_part_t part
, ...))add
;
1101 this->public.build
= (void*(*)(builder_t
*this))build
;
1103 return &this->public;