d2078cbbc6bc61c4a2dfae020fa6ecbe62fe4950
[strongswan.git] / src / libstrongswan / asn1 / asn1.c
1 /*
2 * Copyright (C) 2006 Martin Will
3 * Copyright (C) 2000-2008 Andreas Steffen
4 *
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <time.h>
21 #include <pthread.h>
22
23 #include <utils.h>
24 #include <debug.h>
25
26 #include "oid.h"
27 #include "asn1.h"
28 #include "asn1_parser.h"
29
30 /**
31 * some common prefabricated ASN.1 constants
32 */
33 static u_char ASN1_INTEGER_0_str[] = { 0x02, 0x00 };
34 static u_char ASN1_INTEGER_1_str[] = { 0x02, 0x01, 0x01 };
35 static u_char ASN1_INTEGER_2_str[] = { 0x02, 0x01, 0x02 };
36
37 const chunk_t ASN1_INTEGER_0 = chunk_from_buf(ASN1_INTEGER_0_str);
38 const chunk_t ASN1_INTEGER_1 = chunk_from_buf(ASN1_INTEGER_1_str);
39 const chunk_t ASN1_INTEGER_2 = chunk_from_buf(ASN1_INTEGER_2_str);
40
41 /**
42 * some popular algorithmIdentifiers
43 */
44
45 static u_char ASN1_md2_id_str[] = {
46 0x30, 0x0c,
47 0x06, 0x08,
48 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x02,
49 0x05,0x00,
50 };
51
52 static u_char ASN1_md5_id_str[] = {
53 0x30, 0x0C,
54 0x06, 0x08,
55 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05,
56 0x05, 0x00
57 };
58
59 static u_char ASN1_sha1_id_str[] = {
60 0x30, 0x09,
61 0x06, 0x05,
62 0x2B, 0x0E,0x03, 0x02, 0x1A,
63 0x05, 0x00
64 };
65
66 static u_char ASN1_sha256_id_str[] = {
67 0x30, 0x0d,
68 0x06, 0x09,
69 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
70 0x05, 0x00
71 };
72
73 static u_char ASN1_sha384_id_str[] = {
74 0x30, 0x0d,
75 0x06, 0x09,
76 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
77 0x05, 0x00
78 };
79
80 static u_char ASN1_sha512_id_str[] = {
81 0x30, 0x0d,
82 0x06, 0x09,
83 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
84 0x05,0x00
85 };
86
87 static u_char ASN1_md2WithRSA_id_str[] = {
88 0x30, 0x0D,
89 0x06, 0x09,
90 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x02,
91 0x05, 0x00
92 };
93
94 static u_char ASN1_md5WithRSA_id_str[] = {
95 0x30, 0x0D,
96 0x06, 0x09,
97 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x04,
98 0x05, 0x00
99 };
100
101 static u_char ASN1_sha1WithRSA_id_str[] = {
102 0x30, 0x0D,
103 0x06, 0x09,
104 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
105 0x05, 0x00
106 };
107
108 static u_char ASN1_sha256WithRSA_id_str[] = {
109 0x30, 0x0D,
110 0x06, 0x09,
111 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
112 0x05, 0x00
113 };
114
115 static u_char ASN1_sha384WithRSA_id_str[] = {
116 0x30, 0x0D,
117 0x06, 0x09,
118 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0C,
119 0x05, 0x00
120 };
121
122 static u_char ASN1_sha512WithRSA_id_str[] = {
123 0x30, 0x0D,
124 0x06, 0x09,
125 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0D,
126 0x05, 0x00
127 };
128
129 static u_char ASN1_rsaEncryption_id_str[] = {
130 0x30, 0x0D,
131 0x06, 0x09,
132 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01,
133 0x05, 0x00
134 };
135
136 static const chunk_t ASN1_md2_id = chunk_from_buf(ASN1_md2_id_str);
137 static const chunk_t ASN1_md5_id = chunk_from_buf(ASN1_md5_id_str);
138 static const chunk_t ASN1_sha1_id = chunk_from_buf(ASN1_sha1_id_str);
139 static const chunk_t ASN1_sha256_id = chunk_from_buf(ASN1_sha256_id_str);
140 static const chunk_t ASN1_sha384_id = chunk_from_buf(ASN1_sha384_id_str);
141 static const chunk_t ASN1_sha512_id = chunk_from_buf(ASN1_sha512_id_str);
142 static const chunk_t ASN1_rsaEncryption_id = chunk_from_buf(ASN1_rsaEncryption_id_str);
143 static const chunk_t ASN1_md2WithRSA_id = chunk_from_buf(ASN1_md2WithRSA_id_str);
144 static const chunk_t ASN1_md5WithRSA_id = chunk_from_buf(ASN1_md5WithRSA_id_str);
145 static const chunk_t ASN1_sha1WithRSA_id = chunk_from_buf(ASN1_sha1WithRSA_id_str);
146 static const chunk_t ASN1_sha256WithRSA_id = chunk_from_buf(ASN1_sha256WithRSA_id_str);
147 static const chunk_t ASN1_sha384WithRSA_id = chunk_from_buf(ASN1_sha384WithRSA_id_str);
148 static const chunk_t ASN1_sha512WithRSA_id = chunk_from_buf(ASN1_sha512WithRSA_id_str);
149
150 /*
151 * Defined in header.
152 */
153 chunk_t asn1_algorithmIdentifier(int oid)
154 {
155 switch (oid)
156 {
157 case OID_RSA_ENCRYPTION:
158 return ASN1_rsaEncryption_id;
159 case OID_MD2_WITH_RSA:
160 return ASN1_md2WithRSA_id;
161 case OID_MD5_WITH_RSA:
162 return ASN1_md5WithRSA_id;
163 case OID_SHA1_WITH_RSA:
164 return ASN1_sha1WithRSA_id;
165 case OID_SHA256_WITH_RSA:
166 return ASN1_sha256WithRSA_id;
167 case OID_SHA384_WITH_RSA:
168 return ASN1_sha384WithRSA_id;
169 case OID_SHA512_WITH_RSA:
170 return ASN1_sha512WithRSA_id;
171 case OID_MD2:
172 return ASN1_md2_id;
173 case OID_MD5:
174 return ASN1_md5_id;
175 case OID_SHA1:
176 return ASN1_sha1_id;
177 case OID_SHA256:
178 return ASN1_sha256_id;
179 case OID_SHA384:
180 return ASN1_sha384_id;
181 case OID_SHA512:
182 return ASN1_sha512_id;
183 default:
184 return chunk_empty;
185 }
186 }
187
188 /*
189 * Defined in header.
190 */
191 int asn1_known_oid(chunk_t object)
192 {
193 int oid = 0;
194
195 while (object.len)
196 {
197 if (oid_names[oid].octet == *object.ptr)
198 {
199 if (--object.len == 0 || oid_names[oid].down == 0)
200 {
201 return oid; /* found terminal symbol */
202 }
203 else
204 {
205 object.ptr++; oid++; /* advance to next hex octet */
206 }
207 }
208 else
209 {
210 if (oid_names[oid].next)
211 {
212 oid = oid_names[oid].next;
213 }
214 else
215 {
216 return OID_UNKNOWN;
217 }
218 }
219 }
220 return -1;
221 }
222
223 /*
224 * Defined in header.
225 */
226 chunk_t asn1_build_known_oid(int n)
227 {
228 chunk_t oid;
229 int i;
230
231 if (n < 0 || n >= OID_MAX)
232 {
233 return chunk_empty;
234 }
235
236 i = oid_names[n].level + 1;
237 oid = chunk_alloc(2 + i);
238 oid.ptr[0] = ASN1_OID;
239 oid.ptr[1] = i;
240
241 do
242 {
243 if (oid_names[n].level >= i)
244 {
245 n--;
246 continue;
247 }
248 oid.ptr[--i + 2] = oid_names[n--].octet;
249 }
250 while (i > 0);
251
252 return oid;
253 }
254
255 /*
256 * Defined in header.
257 */
258 size_t asn1_length(chunk_t *blob)
259 {
260 u_char n;
261 size_t len;
262
263 /* advance from tag field on to length field */
264 blob->ptr++;
265 blob->len--;
266
267 /* read first octet of length field */
268 n = *blob->ptr++;
269 blob->len--;
270
271 if ((n & 0x80) == 0)
272 {/* single length octet */
273 return n;
274 }
275
276 /* composite length, determine number of length octets */
277 n &= 0x7f;
278
279 if (n > blob->len)
280 {
281 DBG2("number of length octets is larger than ASN.1 object");
282 return ASN1_INVALID_LENGTH;
283 }
284
285 if (n > sizeof(len))
286 {
287 DBG2("number of length octets is larger than limit of %d octets",
288 (int)sizeof(len));
289 return ASN1_INVALID_LENGTH;
290 }
291
292 len = 0;
293
294 while (n-- > 0)
295 {
296 len = 256*len + *blob->ptr++;
297 blob->len--;
298 }
299 if (len > blob->len)
300 {
301 DBG2("length is larger than remaining blob size");
302 return ASN1_INVALID_LENGTH;
303 }
304 return len;
305 }
306
307 #define TIME_MAX 0x7fffffff
308
309 static const int days[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
310 static const int tm_leap_1970 = 477;
311
312 /**
313 * Converts ASN.1 UTCTIME or GENERALIZEDTIME into calender time
314 */
315 time_t asn1_to_time(const chunk_t *utctime, asn1_t type)
316 {
317 int tm_year, tm_mon, tm_day, tm_days, tm_hour, tm_min, tm_sec;
318 int tm_leap_4, tm_leap_100, tm_leap_400, tm_leap;
319 int tz_hour, tz_min, tz_offset;
320 time_t tm_secs;
321 u_char *eot = NULL;
322
323 if ((eot = memchr(utctime->ptr, 'Z', utctime->len)) != NULL)
324 {
325 tz_offset = 0; /* Zulu time with a zero time zone offset */
326 }
327 else if ((eot = memchr(utctime->ptr, '+', utctime->len)) != NULL)
328 {
329 if (sscanf(eot+1, "%2d%2d", &tz_hour, &tz_min) != 2)
330 {
331 return 0; /* error in positive timezone offset format */
332 }
333 tz_offset = 3600*tz_hour + 60*tz_min; /* positive time zone offset */
334 }
335 else if ((eot = memchr(utctime->ptr, '-', utctime->len)) != NULL)
336 {
337 if (sscanf(eot+1, "%2d%2d", &tz_hour, &tz_min) != 2)
338 {
339 return 0; /* error in negative timezone offset format */
340 }
341 tz_offset = -3600*tz_hour - 60*tz_min; /* negative time zone offset */
342 }
343 else
344 {
345 return 0; /* error in time format */
346 }
347
348 /* parse ASN.1 time string */
349 {
350 const char* format = (type == ASN1_UTCTIME)? "%2d%2d%2d%2d%2d":
351 "%4d%2d%2d%2d%2d";
352
353 if (sscanf(utctime->ptr, format, &tm_year, &tm_mon, &tm_day,
354 &tm_hour, &tm_min) != 5)
355 {
356 return 0; /* error in [yy]yymmddhhmm time format */
357 }
358 }
359
360 /* is there a seconds field? */
361 if ((eot - utctime->ptr) == ((type == ASN1_UTCTIME)?12:14))
362 {
363 if (sscanf(eot-2, "%2d", &tm_sec) != 1)
364 {
365 return 0; /* error in ss seconds field format */
366 }
367 }
368 else
369 {
370 tm_sec = 0;
371 }
372
373 /* representation of two-digit years */
374 if (type == ASN1_UTCTIME)
375 {
376 tm_year += (tm_year < 50) ? 2000 : 1900;
377 }
378
379 /* prevent large 32 bit integer overflows */
380 if (sizeof(time_t) == 4 && tm_year > 2038)
381 {
382 return TIME_MAX;
383 }
384
385 /* representation of months as 0..11*/
386 if (tm_mon < 1 || tm_mon > 12)
387 {
388 return 0; /* error in month format */
389 }
390 tm_mon--;
391
392 /* representation of days as 0..30 */
393 tm_day--;
394
395 /* number of leap years between last year and 1970? */
396 tm_leap_4 = (tm_year - 1) / 4;
397 tm_leap_100 = tm_leap_4 / 25;
398 tm_leap_400 = tm_leap_100 / 4;
399 tm_leap = tm_leap_4 - tm_leap_100 + tm_leap_400 - tm_leap_1970;
400
401 /* if date later then February, is the current year a leap year? */
402 if (tm_mon > 1 && (tm_year % 4 == 0) &&
403 (tm_year % 100 != 0 || tm_year % 400 == 0))
404 {
405 tm_leap++;
406 }
407 tm_days = 365 * (tm_year - 1970) + days[tm_mon] + tm_day + tm_leap;
408 tm_secs = 60 * (60 * (24 * tm_days + tm_hour) + tm_min) + tm_sec - tz_offset;
409
410 /* has a 32 bit overflow occurred? */
411 return (tm_secs < 0) ? TIME_MAX : tm_secs;
412 }
413
414 /**
415 * Convert a date into ASN.1 UTCTIME or GENERALIZEDTIME format
416 */
417 chunk_t asn1_from_time(const time_t *time, asn1_t type)
418 {
419 int offset;
420 const char *format;
421 char buf[BUF_LEN];
422 chunk_t formatted_time;
423 struct tm t;
424
425 gmtime_r(time, &t);
426 if (type == ASN1_GENERALIZEDTIME)
427 {
428 format = "%04d%02d%02d%02d%02d%02dZ";
429 offset = 1900;
430 }
431 else /* ASN1_UTCTIME */
432 {
433 format = "%02d%02d%02d%02d%02d%02dZ";
434 offset = (t.tm_year < 100)? 0 : -100;
435 }
436 snprintf(buf, BUF_LEN, format, t.tm_year + offset,
437 t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
438 formatted_time.ptr = buf;
439 formatted_time.len = strlen(buf);
440 return asn1_simple_object(type, formatted_time);
441 }
442
443 /*
444 * Defined in header.
445 */
446 void asn1_debug_simple_object(chunk_t object, asn1_t type, bool private)
447 {
448 int oid;
449
450 switch (type)
451 {
452 case ASN1_OID:
453 oid = asn1_known_oid(object);
454 if (oid != OID_UNKNOWN)
455 {
456 DBG2(" '%s'", oid_names[oid].name);
457 return;
458 }
459 break;
460 case ASN1_UTF8STRING:
461 case ASN1_IA5STRING:
462 case ASN1_PRINTABLESTRING:
463 case ASN1_T61STRING:
464 case ASN1_VISIBLESTRING:
465 DBG2(" '%.*s'", (int)object.len, object.ptr);
466 return;
467 case ASN1_UTCTIME:
468 case ASN1_GENERALIZEDTIME:
469 {
470 time_t time = asn1_to_time(&object, type);
471
472 DBG2(" '%T'", &time, TRUE);
473 }
474 return;
475 default:
476 break;
477 }
478 if (private)
479 {
480 DBG4("%B", &object);
481 }
482 else
483 {
484 DBG3("%B", &object);
485 }
486 }
487
488 /**
489 * parse an ASN.1 simple type
490 */
491 bool asn1_parse_simple_object(chunk_t *object, asn1_t type, u_int level, const char* name)
492 {
493 size_t len;
494
495 /* an ASN.1 object must possess at least a tag and length field */
496 if (object->len < 2)
497 {
498 DBG2("L%d - %s: ASN.1 object smaller than 2 octets", level, name);
499 return FALSE;
500 }
501
502 if (*object->ptr != type)
503 {
504 DBG2("L%d - %s: ASN1 tag 0x%02x expected, but is 0x%02x",
505 level, name, type, *object->ptr);
506 return FALSE;
507 }
508
509 len = asn1_length(object);
510
511 if (len == ASN1_INVALID_LENGTH || object->len < len)
512 {
513 DBG2("L%d - %s: length of ASN.1 object invalid or too large",
514 level, name);
515 return FALSE;
516 }
517
518 DBG2("L%d - %s:", level, name);
519 asn1_debug_simple_object(*object, type, FALSE);
520 return TRUE;
521 }
522
523 /**
524 * ASN.1 definition of an algorithmIdentifier
525 */
526 static const asn1Object_t algorithmIdentifierObjects[] = {
527 { 0, "algorithmIdentifier", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
528 { 1, "algorithm", ASN1_OID, ASN1_BODY }, /* 1 */
529 { 1, "parameters", ASN1_EOC, ASN1_RAW|ASN1_OPT }, /* 2 */
530 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 3 */
531 { 0, "exit", ASN1_EOC, ASN1_EXIT }
532 };
533 #define ALGORITHM_ID_ALG 1
534 #define ALGORITHM_ID_PARAMETERS 2
535
536 /*
537 * Defined in header
538 */
539 int asn1_parse_algorithmIdentifier(chunk_t blob, int level0, chunk_t *parameters)
540 {
541 asn1_parser_t *parser;
542 chunk_t object;
543 int objectID;
544 int alg = OID_UNKNOWN;
545
546 parser = asn1_parser_create(algorithmIdentifierObjects, blob);
547 parser->set_top_level(parser, level0);
548
549 while (parser->iterate(parser, &objectID, &object))
550 {
551 switch (objectID)
552 {
553 case ALGORITHM_ID_ALG:
554 alg = asn1_known_oid(object);
555 break;
556 case ALGORITHM_ID_PARAMETERS:
557 if (parameters != NULL)
558 {
559 *parameters = object;
560 }
561 break;
562 default:
563 break;
564 }
565 }
566 parser->destroy(parser);
567 return alg;
568 }
569
570 /*
571 * tests if a blob contains a valid ASN.1 set or sequence
572 */
573 bool is_asn1(chunk_t blob)
574 {
575 u_int len;
576 u_char tag = *blob.ptr;
577
578 if (tag != ASN1_SEQUENCE && tag != ASN1_SET)
579 {
580 DBG2(" file content is not binary ASN.1");
581 return FALSE;
582 }
583
584 len = asn1_length(&blob);
585
586 /* exact match */
587 if (len == blob.len)
588 {
589 return TRUE;
590 }
591
592 /* some websites append a surplus newline character to the blob */
593 if (len + 1 == blob.len && *(blob.ptr + len) == '\n')
594 {
595 return TRUE;
596 }
597
598 DBG2(" file size does not match ASN.1 coded length");
599 return FALSE;
600 }
601
602 /*
603 * Defined in header.
604 */
605 bool asn1_is_printablestring(chunk_t str)
606 {
607 const char printablestring_charset[] =
608 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '()+,-./:=?";
609 u_int i;
610
611 for (i = 0; i < str.len; i++)
612 {
613 if (strchr(printablestring_charset, str.ptr[i]) == NULL)
614 return FALSE;
615 }
616 return TRUE;
617 }
618
619 /**
620 * codes ASN.1 lengths up to a size of 16'777'215 bytes
621 */
622 static void asn1_code_length(size_t length, chunk_t *code)
623 {
624 if (length < 128)
625 {
626 code->ptr[0] = length;
627 code->len = 1;
628 }
629 else if (length < 256)
630 {
631 code->ptr[0] = 0x81;
632 code->ptr[1] = (u_char) length;
633 code->len = 2;
634 }
635 else if (length < 65536)
636 {
637 code->ptr[0] = 0x82;
638 code->ptr[1] = length >> 8;
639 code->ptr[2] = length & 0x00ff;
640 code->len = 3;
641 }
642 else
643 {
644 code->ptr[0] = 0x83;
645 code->ptr[1] = length >> 16;
646 code->ptr[2] = (length >> 8) & 0x00ff;
647 code->ptr[3] = length & 0x0000ff;
648 code->len = 4;
649 }
650 }
651
652 /**
653 * build an empty asn.1 object with tag and length fields already filled in
654 */
655 u_char* asn1_build_object(chunk_t *object, asn1_t type, size_t datalen)
656 {
657 u_char length_buf[4];
658 chunk_t length = { length_buf, 0 };
659 u_char *pos;
660
661 /* code the asn.1 length field */
662 asn1_code_length(datalen, &length);
663
664 /* allocate memory for the asn.1 TLV object */
665 object->len = 1 + length.len + datalen;
666 object->ptr = malloc(object->len);
667
668 /* set position pointer at the start of the object */
669 pos = object->ptr;
670
671 /* copy the asn.1 tag field and advance the pointer */
672 *pos++ = type;
673
674 /* copy the asn.1 length field and advance the pointer */
675 memcpy(pos, length.ptr, length.len);
676 pos += length.len;
677
678 return pos;
679 }
680
681 /**
682 * Build a simple ASN.1 object
683 */
684 chunk_t asn1_simple_object(asn1_t tag, chunk_t content)
685 {
686 chunk_t object;
687
688 u_char *pos = asn1_build_object(&object, tag, content.len);
689 memcpy(pos, content.ptr, content.len);
690 pos += content.len;
691
692 return object;
693 }
694
695 /**
696 * Build an ASN.1 BIT_STRING object
697 */
698 chunk_t asn1_bitstring(const char *mode, chunk_t content)
699 {
700 chunk_t object;
701 u_char *pos = asn1_build_object(&object, ASN1_BIT_STRING, 1 + content.len);
702
703 *pos++ = 0x00;
704 memcpy(pos, content.ptr, content.len);
705 if (*mode == 'm')
706 {
707 free(content.ptr);
708 }
709 return object;
710 }
711
712 /**
713 * Build an ASN.1 INTEGER object
714 */
715 chunk_t asn1_integer(const char *mode, chunk_t content)
716 {
717 chunk_t object;
718 size_t len;
719 u_char *pos;
720
721 if (content.len == 0 || (content.len == 1 && *content.ptr == 0x00))
722 {
723 /* a zero ASN.1 integer does not have a value field */
724 len = 0;
725 }
726 else
727 {
728 /* ASN.1 integers must be positive numbers in two's complement */
729 len = content.len + ((*content.ptr & 0x80) ? 1 : 0);
730 }
731 pos = asn1_build_object(&object, ASN1_INTEGER, len);
732 if (len > content.len)
733 {
734 *pos++ = 0x00;
735 }
736 if (len)
737 {
738 memcpy(pos, content.ptr, content.len);
739 }
740 if (*mode == 'm')
741 {
742 free(content.ptr);
743 }
744 return object;
745 }
746
747 /**
748 * Build an ASN.1 object from a variable number of individual chunks.
749 * Depending on the mode, chunks either are moved ('m') or copied ('c').
750 */
751 chunk_t asn1_wrap(asn1_t type, const char *mode, ...)
752 {
753 chunk_t construct;
754 va_list chunks;
755 u_char *pos;
756 int i;
757 int count = strlen(mode);
758
759 /* sum up lengths of individual chunks */
760 va_start(chunks, mode);
761 construct.len = 0;
762 for (i = 0; i < count; i++)
763 {
764 chunk_t ch = va_arg(chunks, chunk_t);
765 construct.len += ch.len;
766 }
767 va_end(chunks);
768
769 /* allocate needed memory for construct */
770 pos = asn1_build_object(&construct, type, construct.len);
771
772 /* copy or move the chunks */
773 va_start(chunks, mode);
774 for (i = 0; i < count; i++)
775 {
776 chunk_t ch = va_arg(chunks, chunk_t);
777
778 memcpy(pos, ch.ptr, ch.len);
779 pos += ch.len;
780
781 if (*mode++ == 'm')
782 {
783 free(ch.ptr);
784 }
785 }
786 va_end(chunks);
787
788 return construct;
789 }
790
791 /**
792 * ASN.1 definition of time
793 */
794 static const asn1Object_t timeObjects[] = {
795 { 0, "utcTime", ASN1_UTCTIME, ASN1_OPT|ASN1_BODY }, /* 0 */
796 { 0, "end opt", ASN1_EOC, ASN1_END }, /* 1 */
797 { 0, "generalizeTime", ASN1_GENERALIZEDTIME, ASN1_OPT|ASN1_BODY }, /* 2 */
798 { 0, "end opt", ASN1_EOC, ASN1_END }, /* 3 */
799 { 0, "exit", ASN1_EOC, ASN1_EXIT }
800 };
801 #define TIME_UTC 0
802 #define TIME_GENERALIZED 2
803
804 /**
805 * extracts and converts a UTCTIME or GENERALIZEDTIME object
806 */
807 time_t asn1_parse_time(chunk_t blob, int level0)
808 {
809 asn1_parser_t *parser;
810 chunk_t object;
811 int objectID;
812 time_t utc_time = 0;
813
814 parser= asn1_parser_create(timeObjects, blob);
815 parser->set_top_level(parser, level0);
816
817 while (parser->iterate(parser, &objectID, &object))
818 {
819 if (objectID == TIME_UTC || objectID == TIME_GENERALIZED)
820 {
821 utc_time = asn1_to_time(&object, (objectID == TIME_UTC)
822 ? ASN1_UTCTIME : ASN1_GENERALIZEDTIME);
823 }
824 }
825 parser->destroy(parser);
826 return utc_time;
827 }