2 * Copyright (C)2008 Andreas Steffen
3 * Hochschule fuer Technik Rapperswil, Switzerland
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 #include <asn1/asn1.h>
23 #include <asn1/asn1_parser.h>
24 #include <utils/linked_list.h>
28 typedef struct private_pkcs9_t private_pkcs9_t
;
31 * Private data of a pkcs9_t attribute list.
33 struct private_pkcs9_t
{
40 * DER encoding of PKCS#9 attributes
45 * Linked list of PKCS#9 attributes
47 linked_list_t
*attributes
;
50 typedef struct attribute_t attribute_t
;
53 * Definition of an attribute_t object.
57 * Object Identifier (OID)
72 * Destroys the attribute.
74 * @param this attribute to destroy
76 void (*destroy
) (attribute_t
*this);
81 * PKCS#9 attribute type OIDs
83 static u_char ASN1_contentType_oid_str
[] = {
85 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x03
88 static u_char ASN1_messageDigest_oid_str
[] = {
90 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x04
93 static u_char ASN1_signingTime_oid_str
[] = {
95 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x05
98 static char ASN1_messageType_oid_str
[] = {
100 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01, 0x09, 0x02
103 static char ASN1_senderNonce_oid_str
[] = {
105 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01, 0x09, 0x05
108 static char ASN1_transId_oid_str
[] = {
110 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01, 0x09, 0x07
113 static const chunk_t ASN1_contentType_oid
=
114 chunk_from_buf(ASN1_contentType_oid_str
);
115 static const chunk_t ASN1_messageDigest_oid
=
116 chunk_from_buf(ASN1_messageDigest_oid_str
);
117 static const chunk_t ASN1_signingTime_oid
=
118 chunk_from_buf(ASN1_signingTime_oid_str
);
119 static const chunk_t ASN1_messageType_oid
=
120 chunk_from_buf(ASN1_messageType_oid_str
);
121 static const chunk_t ASN1_senderNonce_oid
=
122 chunk_from_buf(ASN1_senderNonce_oid_str
);
123 static const chunk_t ASN1_transId_oid
=
124 chunk_from_buf(ASN1_transId_oid_str
);
127 * return the ASN.1 encoded OID of a PKCS#9 attribute
129 static chunk_t
asn1_attributeIdentifier(int oid
)
133 case OID_PKCS9_CONTENT_TYPE
:
134 return ASN1_contentType_oid
;
135 case OID_PKCS9_MESSAGE_DIGEST
:
136 return ASN1_messageDigest_oid
;
137 case OID_PKCS9_SIGNING_TIME
:
138 return ASN1_signingTime_oid
;
139 case OID_PKI_MESSAGE_TYPE
:
140 return ASN1_messageType_oid
;
141 case OID_PKI_SENDER_NONCE
:
142 return ASN1_senderNonce_oid
;
143 case OID_PKI_TRANS_ID
:
144 return ASN1_transId_oid
;;
151 * return the ASN.1 encoding of a PKCS#9 attribute
153 static asn1_t
asn1_attributeType(int oid
)
159 case OID_PKCS9_CONTENT_TYPE
:
162 case OID_PKCS9_SIGNING_TIME
:
165 case OID_PKCS9_MESSAGE_DIGEST
:
166 type
= ASN1_OCTET_STRING
;
168 case OID_PKI_MESSAGE_TYPE
:
169 type
= ASN1_PRINTABLESTRING
;
172 type
= ASN1_PRINTABLESTRING
;
174 case OID_PKI_FAIL_INFO
:
175 type
= ASN1_PRINTABLESTRING
;
177 case OID_PKI_SENDER_NONCE
:
178 type
= ASN1_OCTET_STRING
;
180 case OID_PKI_RECIPIENT_NONCE
:
181 type
= ASN1_OCTET_STRING
;
183 case OID_PKI_TRANS_ID
:
184 type
= ASN1_PRINTABLESTRING
;
193 * Destroy an attribute_t object.
195 static void attribute_destroy(attribute_t
*this)
197 free(this->value
.ptr
);
198 free(this->encoding
.ptr
);
203 * Create an attribute_t object.
205 static attribute_t
*attribute_create(int oid
, chunk_t value
)
207 attribute_t
*this = malloc_thing(attribute_t
);
210 this->value
= chunk_clone(value
);
211 this->encoding
= asn1_wrap(ASN1_SEQUENCE
, "cm",
212 asn1_attributeIdentifier(oid
),
213 asn1_simple_object(ASN1_SET
, value
));
214 this->destroy
= (void (*) (attribute_t
*))attribute_destroy
;
219 * Implements pkcs9_t.build_encoding
221 static void build_encoding(private_pkcs9_t
*this)
223 iterator_t
*iterator
;
224 attribute_t
*attribute
;
225 u_int attributes_len
= 0;
227 if (this->encoding
.ptr
)
229 chunk_free(&this->encoding
);
231 if (this->attributes
->get_count(this->attributes
) == 0)
236 /* compute the total length of the encoded attributes */
237 iterator
= this->attributes
->create_iterator(this->attributes
, TRUE
);
239 while (iterator
->iterate(iterator
, (void**)&attribute
))
241 attributes_len
+= attribute
->encoding
.len
;
243 iterator
->destroy(iterator
);
245 /* allocate memory for the attributes and build the encoding */
247 u_char
*pos
= asn1_build_object(&this->encoding
, ASN1_SET
, attributes_len
);
249 iterator
= this->attributes
->create_iterator(this->attributes
, TRUE
);
251 while (iterator
->iterate(iterator
, (void**)&attribute
))
253 memcpy(pos
, attribute
->encoding
.ptr
, attribute
->encoding
.len
);
254 pos
+= attribute
->encoding
.len
;
256 iterator
->destroy(iterator
);
261 * Implements pkcs9_t.get_encoding
263 static chunk_t
get_encoding(private_pkcs9_t
*this)
265 if (this->encoding
.ptr
== NULL
)
267 build_encoding(this);
269 return this->encoding
;
273 * Implements pkcs9_t.get_attribute
275 static chunk_t
get_attribute(private_pkcs9_t
*this, int oid
)
277 iterator_t
*iterator
= this->attributes
->create_iterator(this->attributes
, TRUE
);
278 chunk_t value
= chunk_empty
;
279 attribute_t
*attribute
;
281 while (iterator
->iterate(iterator
, (void**)&attribute
))
283 if (attribute
->oid
== oid
)
285 value
= attribute
->value
;
289 iterator
->destroy(iterator
);
294 * Implements pkcs9_t.set_attribute
296 static void set_attribute(private_pkcs9_t
*this, int oid
, chunk_t value
)
298 attribute_t
*attribute
= attribute_create(oid
, value
);
300 this->attributes
->insert_last(this->attributes
, (void*)attribute
);
304 * Implements pkcs9_t.get_messageDigest
306 static chunk_t
get_messageDigest(private_pkcs9_t
*this)
308 const int oid
= OID_PKCS9_MESSAGE_DIGEST
;
309 chunk_t value
= get_attribute(this, oid
);
311 if (value
.ptr
== NULL
)
315 if (!asn1_parse_simple_object(&value
, asn1_attributeType(oid
), 0,
316 oid_names
[oid
].name
))
320 return chunk_clone(value
);
324 * Implements pkcs9_t.set_attribute
326 static void set_messageDigest(private_pkcs9_t
*this, chunk_t value
)
328 const int oid
= OID_PKCS9_MESSAGE_DIGEST
;
329 chunk_t messageDigest
= asn1_simple_object(asn1_attributeType(oid
), value
);
331 set_attribute(this, oid
, messageDigest
);
332 free(messageDigest
.ptr
);
336 * Implements pkcs9_t.destroy
338 static void destroy(private_pkcs9_t
*this)
340 this->attributes
->destroy_offset(this->attributes
, offsetof(attribute_t
, destroy
));
341 free(this->encoding
.ptr
);
346 * Generic private constructor
348 static private_pkcs9_t
*pkcs9_create_empty(void)
350 private_pkcs9_t
*this = malloc_thing(private_pkcs9_t
);
353 this->encoding
= chunk_empty
;
354 this->attributes
= linked_list_create();
356 /*public functions */
357 this->public.build_encoding
= (void (*) (pkcs9_t
*))build_encoding
;
358 this->public.get_encoding
= (chunk_t (*) (pkcs9_t
*))get_encoding
;
359 this->public.get_attribute
= (chunk_t (*) (pkcs9_t
*,int))get_attribute
;
360 this->public.set_attribute
= (void (*) (pkcs9_t
*,int,chunk_t
))set_attribute
;
361 this->public.get_messageDigest
= (chunk_t (*) (pkcs9_t
*))get_messageDigest
;
362 this->public.set_messageDigest
= (void (*) (pkcs9_t
*,chunk_t
))set_messageDigest
;
363 this->public.destroy
= (void (*) (pkcs9_t
*))destroy
;
369 * Described in header.
371 pkcs9_t
*pkcs9_create(void)
373 private_pkcs9_t
*this = pkcs9_create_empty();
375 return &this->public;
379 * ASN.1 definition of the X.501 atttribute type
381 static const asn1Object_t attributesObjects
[] = {
382 { 0, "attributes", ASN1_SET
, ASN1_LOOP
}, /* 0 */
383 { 1, "attribute", ASN1_SEQUENCE
, ASN1_NONE
}, /* 1 */
384 { 2, "type", ASN1_OID
, ASN1_BODY
}, /* 2 */
385 { 2, "values", ASN1_SET
, ASN1_LOOP
}, /* 3 */
386 { 3, "value", ASN1_EOC
, ASN1_RAW
}, /* 4 */
387 { 2, "end loop", ASN1_EOC
, ASN1_END
}, /* 5 */
388 { 0, "end loop", ASN1_EOC
, ASN1_END
}, /* 6 */
389 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
391 #define ATTRIBUTE_OBJ_TYPE 2
392 #define ATTRIBUTE_OBJ_VALUE 4
395 * Parse a PKCS#9 attribute list
397 static bool parse_attributes(chunk_t chunk
, int level0
, private_pkcs9_t
* this)
399 asn1_parser_t
*parser
;
402 int oid
= OID_UNKNOWN
;
403 bool success
= FALSE
;
405 parser
= asn1_parser_create(attributesObjects
, chunk
);
406 parser
->set_top_level(parser
, level0
);
408 while (parser
->iterate(parser
, &objectID
, &object
))
412 case ATTRIBUTE_OBJ_TYPE
:
413 oid
= asn1_known_oid(object
);
415 case ATTRIBUTE_OBJ_VALUE
:
416 if (oid
== OID_UNKNOWN
)
420 /* add the attribute to a linked list */
422 attribute_t
*attribute
= attribute_create(oid
, object
);
424 this->attributes
->insert_last(this->attributes
,
427 /* parse known attributes */
429 asn1_t type
= asn1_attributeType(oid
);
431 if (type
!= ASN1_EOC
)
433 if (!asn1_parse_simple_object(&object
, type
,
434 parser
->get_level(parser
)+1,
435 oid_names
[oid
].name
))
443 success
= parser
->success(parser
);
446 parser
->destroy(parser
);
452 * Described in header.
454 pkcs9_t
*pkcs9_create_from_chunk(chunk_t chunk
, u_int level
)
456 private_pkcs9_t
*this = pkcs9_create_empty();
458 this->encoding
= chunk_clone(chunk
);
460 if (!parse_attributes(chunk
, level
, this))
465 return &this->public;