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