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