ba2724005fa6e2fa40cb8022cc697744cfbba83b
[strongswan.git] / src / libstrongswan / crypto / pkcs9.c
1 /*
2 * Copyright (C)2008 Andreas Steffen
3 * Hochschule fuer Technik Rapperswil, Switzerland
4 *
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>.
9 *
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
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include <library.h>
19 #include <debug.h>
20
21 #include <asn1/oid.h>
22 #include <asn1/asn1.h>
23 #include <utils/linked_list.h>
24
25 #include "pkcs9.h"
26
27 typedef struct private_pkcs9_t private_pkcs9_t;
28
29 /**
30 * Private data of a pkcs9_t attribute list.
31 */
32 struct private_pkcs9_t {
33 /**
34 * Public interface
35 */
36 pkcs9_t public;
37
38 /**
39 * DER encoding of PKCS#9 attributes
40 */
41 chunk_t encoding;
42
43 /**
44 * Linked list of PKCS#9 attributes
45 */
46 linked_list_t *attributes;
47 };
48
49 typedef struct attribute_t attribute_t;
50
51 /**
52 * Definition of an attribute_t object.
53 */
54 struct attribute_t {
55 /**
56 * Object Identifier (OID)
57 */
58 int oid;
59
60 /**
61 * Attribute value
62 */
63 chunk_t value;
64
65 /**
66 * ASN.1 encoding
67 */
68 chunk_t encoding;
69
70 /**
71 * Destroys the attribute.
72 *
73 * @param this attribute to destroy
74 */
75 void (*destroy) (attribute_t *this);
76
77 };
78
79 /* ASN.1 definition of the X.501 atttribute type */
80
81 static const asn1Object_t attributesObjects[] = {
82 { 0, "attributes", ASN1_SET, ASN1_LOOP }, /* 0 */
83 { 1, "attribute", ASN1_SEQUENCE, ASN1_NONE }, /* 1 */
84 { 2, "type", ASN1_OID, ASN1_BODY }, /* 2 */
85 { 2, "values", ASN1_SET, ASN1_LOOP }, /* 3 */
86 { 3, "value", ASN1_EOC, ASN1_RAW }, /* 4 */
87 { 2, "end loop", ASN1_EOC, ASN1_END }, /* 5 */
88 { 0, "end loop", ASN1_EOC, ASN1_END }, /* 6 */
89 };
90
91 #define ATTRIBUTE_OBJ_TYPE 2
92 #define ATTRIBUTE_OBJ_VALUE 4
93 #define ATTRIBUTE_OBJ_ROOF 7
94
95 /**
96 * PKCS#9 attribute type OIDs
97 */
98 static u_char ASN1_contentType_oid_str[] = {
99 0x06, 0x09,
100 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x03
101 };
102
103 static u_char ASN1_messageDigest_oid_str[] = {
104 0x06, 0x09,
105 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x04
106 };
107
108 static u_char ASN1_signingTime_oid_str[] = {
109 0x06, 0x09,
110 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x05
111 };
112
113 static char ASN1_messageType_oid_str[] = {
114 0x06, 0x0A,
115 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01, 0x09, 0x02
116 };
117
118 static char ASN1_senderNonce_oid_str[] = {
119 0x06, 0x0A,
120 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01, 0x09, 0x05
121 };
122
123 static char ASN1_transId_oid_str[] = {
124 0x06, 0x0A,
125 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01, 0x09, 0x07
126 };
127
128 static const chunk_t ASN1_contentType_oid =
129 chunk_from_buf(ASN1_contentType_oid_str);
130 static const chunk_t ASN1_messageDigest_oid =
131 chunk_from_buf(ASN1_messageDigest_oid_str);
132 static const chunk_t ASN1_signingTime_oid =
133 chunk_from_buf(ASN1_signingTime_oid_str);
134 static const chunk_t ASN1_messageType_oid =
135 chunk_from_buf(ASN1_messageType_oid_str);
136 static const chunk_t ASN1_senderNonce_oid =
137 chunk_from_buf(ASN1_senderNonce_oid_str);
138 static const chunk_t ASN1_transId_oid =
139 chunk_from_buf(ASN1_transId_oid_str);
140
141 /**
142 * return the ASN.1 encoded OID of a PKCS#9 attribute
143 */
144 static chunk_t asn1_attributeIdentifier(int oid)
145 {
146 switch (oid)
147 {
148 case OID_PKCS9_CONTENT_TYPE:
149 return ASN1_contentType_oid;
150 case OID_PKCS9_MESSAGE_DIGEST:
151 return ASN1_messageDigest_oid;
152 case OID_PKCS9_SIGNING_TIME:
153 return ASN1_signingTime_oid;
154 case OID_PKI_MESSAGE_TYPE:
155 return ASN1_messageType_oid;
156 case OID_PKI_SENDER_NONCE:
157 return ASN1_senderNonce_oid;
158 case OID_PKI_TRANS_ID:
159 return ASN1_transId_oid;;
160 default:
161 return chunk_empty;
162 }
163 }
164
165 /**
166 * return the ASN.1 encoding of a PKCS#9 attribute
167 */
168 static asn1_t asn1_attributeType(int oid)
169 {
170 asn1_t type;
171
172 switch (oid)
173 {
174 case OID_PKCS9_CONTENT_TYPE:
175 type = ASN1_OID;
176 break;
177 case OID_PKCS9_SIGNING_TIME:
178 type = ASN1_UTCTIME;
179 break;
180 case OID_PKCS9_MESSAGE_DIGEST:
181 type = ASN1_OCTET_STRING;
182 break;
183 case OID_PKI_MESSAGE_TYPE:
184 type = ASN1_PRINTABLESTRING;
185 break;
186 case OID_PKI_STATUS:
187 type = ASN1_PRINTABLESTRING;
188 break;
189 case OID_PKI_FAIL_INFO:
190 type = ASN1_PRINTABLESTRING;
191 break;
192 case OID_PKI_SENDER_NONCE:
193 type = ASN1_OCTET_STRING;
194 break;
195 case OID_PKI_RECIPIENT_NONCE:
196 type = ASN1_OCTET_STRING;
197 break;
198 case OID_PKI_TRANS_ID:
199 type = ASN1_PRINTABLESTRING;
200 break;
201 default:
202 type = ASN1_EOC;
203 }
204 return type;
205 }
206
207 /**
208 * Destroy an attribute_t object.
209 */
210 static void attribute_destroy(attribute_t *this)
211 {
212 free(this->value.ptr);
213 free(this->encoding.ptr);
214 free(this);
215 }
216
217 /**
218 * Create an attribute_t object.
219 */
220 static attribute_t *attribute_create(int oid, chunk_t value)
221 {
222 attribute_t *this = malloc_thing(attribute_t);
223
224 this->oid = oid;
225 this->value = chunk_clone(value);
226 this->encoding = asn1_wrap(ASN1_SEQUENCE, "cm",
227 asn1_attributeIdentifier(oid),
228 asn1_simple_object(ASN1_SET, value));
229 this->destroy = (void (*) (attribute_t*))attribute_destroy;
230 return this;
231 }
232
233 /**
234 * Implements pkcs9_t.build_encoding
235 */
236 static void build_encoding(private_pkcs9_t *this)
237 {
238 iterator_t *iterator;
239 attribute_t *attribute;
240 u_int attributes_len = 0;
241
242 if (this->encoding.ptr)
243 {
244 chunk_free(&this->encoding);
245 }
246 if (this->attributes->get_count(this->attributes) == 0)
247 {
248 return;
249 }
250
251 /* compute the total length of the encoded attributes */
252 iterator = this->attributes->create_iterator(this->attributes, TRUE);
253
254 while (iterator->iterate(iterator, (void**)&attribute))
255 {
256 attributes_len += attribute->encoding.len;
257 }
258 iterator->destroy(iterator);
259
260 /* allocate memory for the attributes and build the encoding */
261 {
262 u_char *pos = build_asn1_object(&this->encoding, ASN1_SET, attributes_len);
263
264 iterator = this->attributes->create_iterator(this->attributes, TRUE);
265
266 while (iterator->iterate(iterator, (void**)&attribute))
267 {
268 memcpy(pos, attribute->encoding.ptr, attribute->encoding.len);
269 pos += attribute->encoding.len;
270 }
271 iterator->destroy(iterator);
272 }
273 }
274
275 /**
276 * Implements pkcs9_t.get_encoding
277 */
278 static chunk_t get_encoding(private_pkcs9_t *this)
279 {
280 if (this->encoding.ptr == NULL)
281 {
282 build_encoding(this);
283 }
284 return this->encoding;
285 }
286
287 /**
288 * Implements pkcs9_t.get_attribute
289 */
290 static chunk_t get_attribute(private_pkcs9_t *this, int oid)
291 {
292 iterator_t *iterator = this->attributes->create_iterator(this->attributes, TRUE);
293 chunk_t value = chunk_empty;
294 attribute_t *attribute;
295
296 while (iterator->iterate(iterator, (void**)&attribute))
297 {
298 if (attribute->oid == oid)
299 {
300 value = attribute->value;
301 break;
302 }
303 }
304 iterator->destroy(iterator);
305 return value;
306 }
307
308 /**
309 * Implements pkcs9_t.set_attribute
310 */
311 static void set_attribute(private_pkcs9_t *this, int oid, chunk_t value)
312 {
313 attribute_t *attribute = attribute_create(oid, value);
314
315 this->attributes->insert_last(this->attributes, (void*)attribute);
316 }
317
318 /**
319 * Implements pkcs9_t.get_messageDigest
320 */
321 static chunk_t get_messageDigest(private_pkcs9_t *this)
322 {
323 const int oid = OID_PKCS9_MESSAGE_DIGEST;
324 chunk_t value = get_attribute(this, oid);
325
326 if (value.ptr == NULL)
327 {
328 return chunk_empty;
329 }
330 if (!parse_asn1_simple_object(&value, asn1_attributeType(oid), 0, oid_names[oid].name))
331 {
332 return chunk_empty;
333 }
334 return chunk_clone(value);
335 }
336
337 /**
338 * Implements pkcs9_t.set_attribute
339 */
340 static void set_messageDigest(private_pkcs9_t *this, chunk_t value)
341 {
342 const int oid = OID_PKCS9_MESSAGE_DIGEST;
343 chunk_t messageDigest = asn1_simple_object(asn1_attributeType(oid), value);
344
345 set_attribute(this, oid, messageDigest);
346 free(messageDigest.ptr);
347 }
348
349 /**
350 * Implements pkcs9_t.destroy
351 */
352 static void destroy(private_pkcs9_t *this)
353 {
354 this->attributes->destroy_offset(this->attributes, offsetof(attribute_t, destroy));
355 free(this->encoding.ptr);
356 free(this);
357 }
358
359 /**
360 * Generic private constructor
361 */
362 static private_pkcs9_t *pkcs9_create_empty(void)
363 {
364 private_pkcs9_t *this = malloc_thing(private_pkcs9_t);
365
366 /* initialize */
367 this->encoding = chunk_empty;
368 this->attributes = linked_list_create();
369
370 /*public functions */
371 this->public.build_encoding = (void (*) (pkcs9_t*))build_encoding;
372 this->public.get_encoding = (chunk_t (*) (pkcs9_t*))get_encoding;
373 this->public.get_attribute = (chunk_t (*) (pkcs9_t*,int))get_attribute;
374 this->public.set_attribute = (void (*) (pkcs9_t*,int,chunk_t))set_attribute;
375 this->public.get_messageDigest = (chunk_t (*) (pkcs9_t*))get_messageDigest;
376 this->public.set_messageDigest = (void (*) (pkcs9_t*,chunk_t))set_messageDigest;
377 this->public.destroy = (void (*) (pkcs9_t*))destroy;
378
379 return this;
380 }
381
382 /*
383 * Described in header.
384 */
385 pkcs9_t *pkcs9_create(void)
386 {
387 private_pkcs9_t *this = pkcs9_create_empty();
388
389 return &this->public;
390 }
391
392 /**
393 * Parse a PKCS#9 attribute list
394 */
395 static bool parse_attributes(chunk_t chunk, int level0, private_pkcs9_t* this)
396 {
397 asn1_ctx_t ctx;
398 chunk_t object;
399 u_int level;
400 int oid = OID_UNKNOWN;
401 int objectID = 0;
402
403 asn1_init(&ctx, chunk, level0, FALSE, FALSE);
404
405 while (objectID < ATTRIBUTE_OBJ_ROOF)
406 {
407 if (!extract_object(attributesObjects, &objectID, &object, &level, &ctx))
408 {
409 return FALSE;
410 }
411
412 switch (objectID)
413 {
414 case ATTRIBUTE_OBJ_TYPE:
415 oid = known_oid(object);
416 break;
417 case ATTRIBUTE_OBJ_VALUE:
418 if (oid == OID_UNKNOWN)
419 {
420 break;
421 }
422 /* add the attribute to a linked list */
423 {
424 attribute_t *attribute = attribute_create(oid, object);
425
426 this->attributes->insert_last(this->attributes, (void*)attribute);
427 }
428 /* parse known attributes */
429 {
430 asn1_t type = asn1_attributeType(oid);
431
432 if (type != ASN1_EOC)
433 {
434 if (!parse_asn1_simple_object(&object, type, level+1, oid_names[oid].name))
435 {
436 return FALSE;
437 }
438 }
439 }
440 }
441 objectID++;
442 }
443 return TRUE;
444 }
445
446
447 /*
448 * Described in header.
449 */
450 pkcs9_t *pkcs9_create_from_chunk(chunk_t chunk, u_int level)
451 {
452 private_pkcs9_t *this = pkcs9_create_empty();
453
454 this->encoding = chunk_clone(chunk);
455
456 if (!parse_attributes(chunk, level, this))
457 {
458 destroy(this);
459 return NULL;
460 }
461 return &this->public;
462 }