increased debug level for errors to DBG1
[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 * Initializes the internal context of the ASN.1 parser
315 */
316 void asn1_init(asn1_ctx_t *ctx, chunk_t blob, u_int level0,
317 bool implicit, bool private)
318 {
319 ctx->blobs[0] = blob;
320 ctx->level0 = level0;
321 ctx->implicit = implicit;
322 ctx->private = private;
323 memset(ctx->loopAddr, '\0', sizeof(ctx->loopAddr));
324 }
325
326 /**
327 * print the value of an ASN.1 simple object
328 */
329 static void debug_asn1_simple_object(chunk_t object, asn1_t type, bool private)
330 {
331 int oid;
332
333 switch (type)
334 {
335 case ASN1_OID:
336 oid = known_oid(object);
337 if (oid != OID_UNKNOWN)
338 {
339 DBG2(" '%s'", oid_names[oid].name);
340 return;
341 }
342 break;
343 case ASN1_UTF8STRING:
344 case ASN1_IA5STRING:
345 case ASN1_PRINTABLESTRING:
346 case ASN1_T61STRING:
347 case ASN1_VISIBLESTRING:
348 DBG2(" '%.*s'", (int)object.len, object.ptr);
349 return;
350 case ASN1_UTCTIME:
351 case ASN1_GENERALIZEDTIME:
352 {
353 time_t time = asn1totime(&object, type);
354
355 DBG2(" '%T'", &time);
356 }
357 return;
358 default:
359 break;
360 }
361 if (private)
362 {
363 DBG4("%B", &object);
364 }
365 else
366 {
367 DBG3("%B", &object);
368 }
369 }
370
371 /**
372 * Parses and extracts the next ASN.1 object
373 */
374 bool extract_object(asn1Object_t const *objects, u_int *objectID, chunk_t *object, u_int *level, asn1_ctx_t *ctx)
375 {
376 asn1Object_t obj = objects[*objectID];
377 chunk_t *blob;
378 chunk_t *blob1;
379 u_char *start_ptr;
380
381 *object = chunk_empty;
382
383 if (obj.flags & ASN1_END) /* end of loop or option found */
384 {
385 if (ctx->loopAddr[obj.level] && ctx->blobs[obj.level+1].len > 0)
386 {
387 *objectID = ctx->loopAddr[obj.level]; /* another iteration */
388 obj = objects[*objectID];
389 }
390 else
391 {
392 ctx->loopAddr[obj.level] = 0; /* exit loop or option*/
393 return TRUE;
394 }
395 }
396
397 *level = ctx->level0 + obj.level;
398 blob = ctx->blobs + obj.level;
399 blob1 = blob + 1;
400 start_ptr = blob->ptr;
401
402 /* handle ASN.1 defaults values */
403 if ((obj.flags & ASN1_DEF) && (blob->len == 0 || *start_ptr != obj.type) )
404 {
405 /* field is missing */
406 DBG2("L%d - %s:", *level, obj.name);
407 if (obj.type & ASN1_CONSTRUCTED)
408 {
409 (*objectID)++ ; /* skip context-specific tag */
410 }
411 return TRUE;
412 }
413
414 /* handle ASN.1 options */
415
416 if ((obj.flags & ASN1_OPT)
417 && (blob->len == 0 || *start_ptr != obj.type))
418 {
419 /* advance to end of missing option field */
420 do
421 (*objectID)++;
422 while (!((objects[*objectID].flags & ASN1_END)
423 && (objects[*objectID].level == obj.level)));
424 return TRUE;
425 }
426
427 /* an ASN.1 object must possess at least a tag and length field */
428
429 if (blob->len < 2)
430 {
431 DBG1("L%d - %s: ASN.1 object smaller than 2 octets",
432 *level, obj.name);
433 return FALSE;
434 }
435
436 blob1->len = asn1_length(blob);
437
438 if (blob1->len == ASN1_INVALID_LENGTH || blob->len < blob1->len)
439 {
440 DBG1("L%d - %s: length of ASN.1 object invalid or too large",
441 *level, obj.name);
442 return FALSE;
443 }
444
445 blob1->ptr = blob->ptr;
446 blob->ptr += blob1->len;
447 blob->len -= blob1->len;
448
449 /* return raw ASN.1 object without prior type checking */
450
451 if (obj.flags & ASN1_RAW)
452 {
453 DBG2("L%d - %s:", *level, obj.name);
454 object->ptr = start_ptr;
455 object->len = (size_t)(blob->ptr - start_ptr);
456 return TRUE;
457 }
458
459 if (*start_ptr != obj.type && !(ctx->implicit && *objectID == 0))
460 {
461 DBG1("L%d - %s: ASN1 tag 0x%02x expected, but is 0x%02x",
462 *level, obj.name, obj.type, *start_ptr);
463 DBG3("%b", start_ptr, (u_int)(blob->ptr - start_ptr));
464 return FALSE;
465 }
466
467 DBG2("L%d - %s:", ctx->level0+obj.level, obj.name);
468
469 /* In case of "SEQUENCE OF" or "SET OF" start a loop */
470 if (obj.flags & ASN1_LOOP)
471 {
472 if (blob1->len > 0)
473 {
474 /* at least one item, start the loop */
475 ctx->loopAddr[obj.level] = *objectID + 1;
476 }
477 else
478 {
479 /* no items, advance directly to end of loop */
480 do
481 (*objectID)++;
482 while (!((objects[*objectID].flags & ASN1_END)
483 && (objects[*objectID].level == obj.level)));
484 return TRUE;
485 }
486 }
487
488 if (obj.flags & ASN1_OBJ)
489 {
490 object->ptr = start_ptr;
491 object->len = (size_t)(blob->ptr - start_ptr);
492 if (ctx->private)
493 {
494 DBG4("%B", object);
495 }
496 else
497 {
498 DBG3("%B", object);
499 }
500 }
501 else if (obj.flags & ASN1_BODY)
502 {
503 *object = *blob1;
504 debug_asn1_simple_object(*object, obj.type, ctx->private);
505 }
506 return TRUE;
507 }
508
509 /**
510 * parse an ASN.1 simple type
511 */
512 bool parse_asn1_simple_object(chunk_t *object, asn1_t type, u_int level, const char* name)
513 {
514 size_t len;
515
516 /* an ASN.1 object must possess at least a tag and length field */
517 if (object->len < 2)
518 {
519 DBG2("L%d - %s: ASN.1 object smaller than 2 octets", level, name);
520 return FALSE;
521 }
522
523 if (*object->ptr != type)
524 {
525 DBG2("L%d - %s: ASN1 tag 0x%02x expected, but is 0x%02x",
526 level, name, type, *object->ptr);
527 return FALSE;
528 }
529
530 len = asn1_length(object);
531
532 if (len == ASN1_INVALID_LENGTH || object->len < len)
533 {
534 DBG2("L%d - %s: length of ASN.1 object invalid or too large",
535 level, name);
536 return FALSE;
537 }
538
539 DBG2("L%d - %s:", level, name);
540 debug_asn1_simple_object(*object, type, FALSE);
541 return TRUE;
542 }
543
544 /**
545 * extracts an algorithmIdentifier
546 */
547 int parse_algorithmIdentifier(chunk_t blob, int level0, chunk_t *parameters)
548 {
549 asn1_ctx_t ctx;
550 chunk_t object;
551 u_int level;
552 int alg = OID_UNKNOWN;
553 int objectID = 0;
554
555 asn1_init(&ctx, blob, level0, FALSE, FALSE);
556
557 while (objectID < ALGORITHM_ID_ROOF)
558 {
559 if (!extract_object(algorithmIdentifierObjects, &objectID, &object, &level, &ctx))
560 return OID_UNKNOWN;
561
562 switch (objectID)
563 {
564 case ALGORITHM_ID_ALG:
565 alg = known_oid(object);
566 break;
567 case ALGORITHM_ID_PARAMETERS:
568 if (parameters != NULL)
569 *parameters = object;
570 break;
571 default:
572 break;
573 }
574 objectID++;
575 }
576 return alg;
577 }
578
579 /*
580 * tests if a blob contains a valid ASN.1 set or sequence
581 */
582 bool is_asn1(chunk_t blob)
583 {
584 u_int len;
585 u_char tag = *blob.ptr;
586
587 if (tag != ASN1_SEQUENCE && tag != ASN1_SET)
588 {
589 DBG2(" file content is not binary ASN.1");
590 return FALSE;
591 }
592 len = asn1_length(&blob);
593 if (len != blob.len)
594 {
595 DBG2(" file size does not match ASN.1 coded length");
596 return FALSE;
597 }
598 return TRUE;
599 }
600
601 /**
602 * codes ASN.1 lengths up to a size of 16'777'215 bytes
603 */
604 void code_asn1_length(size_t length, chunk_t *code)
605 {
606 if (length < 128)
607 {
608 code->ptr[0] = length;
609 code->len = 1;
610 }
611 else if (length < 256)
612 {
613 code->ptr[0] = 0x81;
614 code->ptr[1] = (u_char) length;
615 code->len = 2;
616 }
617 else if (length < 65536)
618 {
619 code->ptr[0] = 0x82;
620 code->ptr[1] = length >> 8;
621 code->ptr[2] = length & 0x00ff;
622 code->len = 3;
623 }
624 else
625 {
626 code->ptr[0] = 0x83;
627 code->ptr[1] = length >> 16;
628 code->ptr[2] = (length >> 8) & 0x00ff;
629 code->ptr[3] = length & 0x0000ff;
630 code->len = 4;
631 }
632 }
633
634 /**
635 * build an empty asn.1 object with tag and length fields already filled in
636 */
637 u_char* build_asn1_object(chunk_t *object, asn1_t type, size_t datalen)
638 {
639 u_char length_buf[4];
640 chunk_t length = { length_buf, 0 };
641 u_char *pos;
642
643 /* code the asn.1 length field */
644 code_asn1_length(datalen, &length);
645
646 /* allocate memory for the asn.1 TLV object */
647 object->len = 1 + length.len + datalen;
648 object->ptr = malloc(object->len);
649
650 /* set position pointer at the start of the object */
651 pos = object->ptr;
652
653 /* copy the asn.1 tag field and advance the pointer */
654 *pos++ = type;
655
656 /* copy the asn.1 length field and advance the pointer */
657 memcpy(pos, length.ptr, length.len);
658 pos += length.len;
659
660 return pos;
661 }
662
663 /**
664 * build a simple ASN.1 object
665 */
666 chunk_t asn1_simple_object(asn1_t tag, chunk_t content)
667 {
668 chunk_t object;
669
670 u_char *pos = build_asn1_object(&object, tag, content.len);
671 memcpy(pos, content.ptr, content.len);
672 pos += content.len;
673
674 return object;
675 }
676
677 /**
678 * Build an ASN.1 object from a variable number of individual chunks.
679 * Depending on the mode, chunks either are moved ('m') or copied ('c').
680 */
681 chunk_t asn1_wrap(asn1_t type, const char *mode, ...)
682 {
683 chunk_t construct;
684 va_list chunks;
685 u_char *pos;
686 int i;
687 int count = strlen(mode);
688
689 /* sum up lengths of individual chunks */
690 va_start(chunks, mode);
691 construct.len = 0;
692 for (i = 0; i < count; i++)
693 {
694 chunk_t ch = va_arg(chunks, chunk_t);
695 construct.len += ch.len;
696 }
697 va_end(chunks);
698
699 /* allocate needed memory for construct */
700 pos = build_asn1_object(&construct, type, construct.len);
701
702 /* copy or move the chunks */
703 va_start(chunks, mode);
704 for (i = 0; i < count; i++)
705 {
706 chunk_t ch = va_arg(chunks, chunk_t);
707
708 switch (*mode++)
709 {
710 case 'm':
711 memcpy(pos, ch.ptr, ch.len);
712 pos += ch.len;
713 free(ch.ptr);
714 break;
715 case 'c':
716 default:
717 memcpy(pos, ch.ptr, ch.len);
718 pos += ch.len;
719 }
720 }
721 va_end(chunks);
722
723 return construct;
724 }
725
726 /**
727 * convert a MP integer into a DER coded ASN.1 object
728 */
729 chunk_t asn1_integer_from_mpz(const mpz_t value)
730 {
731 size_t bits = mpz_sizeinbase(value, 2); /* size in bits */
732 chunk_t n;
733 n.len = 1 + bits / 8; /* size in bytes */
734 n.ptr = mpz_export(NULL, NULL, 1, n.len, 1, 0, value);
735
736 return asn1_wrap(ASN1_INTEGER, "m", n);
737 }
738
739 /**
740 * convert a date into ASN.1 UTCTIME or GENERALIZEDTIME format
741 */
742 chunk_t timetoasn1(const time_t *time, asn1_t type)
743 {
744 int offset;
745 const char *format;
746 char buf[32];
747 chunk_t formatted_time;
748 struct tm *t = gmtime(time);
749
750 if (type == ASN1_GENERALIZEDTIME)
751 {
752 format = "%04d%02d%02d%02d%02d%02dZ";
753 offset = 1900;
754 }
755 else /* ASN1_UTCTIME */
756 {
757 format = "%02d%02d%02d%02d%02d%02dZ";
758 offset = (t->tm_year < 100)? 0 : -100;
759 }
760 snprintf(buf, sizeof(buf), format, t->tm_year + offset,
761 t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
762 formatted_time.ptr = buf;
763 formatted_time.len = strlen(buf);
764 return asn1_simple_object(type, formatted_time);
765 }
766
767 /**
768 * ASN.1 definition of time
769 */
770 static const asn1Object_t timeObjects[] = {
771 { 0, "utcTime", ASN1_UTCTIME, ASN1_OPT|ASN1_BODY }, /* 0 */
772 { 0, "end opt", ASN1_EOC, ASN1_END }, /* 1 */
773 { 0, "generalizeTime",ASN1_GENERALIZEDTIME, ASN1_OPT|ASN1_BODY }, /* 2 */
774 { 0, "end opt", ASN1_EOC, ASN1_END } /* 3 */
775 };
776 #define TIME_UTC 0
777 #define TIME_GENERALIZED 2
778 #define TIME_ROOF 4
779
780 /**
781 * extracts and converts a UTCTIME or GENERALIZEDTIME object
782 */
783 time_t parse_time(chunk_t blob, int level0)
784 {
785 asn1_ctx_t ctx;
786 chunk_t object;
787 u_int level;
788 int objectID = 0;
789
790 asn1_init(&ctx, blob, level0, FALSE, FALSE);
791
792 while (objectID < TIME_ROOF)
793 {
794 if (!extract_object(timeObjects, &objectID, &object, &level, &ctx))
795 return 0;
796
797 if (objectID == TIME_UTC || objectID == TIME_GENERALIZED)
798 {
799 return asn1totime(&object, (objectID == TIME_UTC)
800 ? ASN1_UTCTIME : ASN1_GENERALIZEDTIME);
801 }
802 objectID++;
803 }
804 return 0;
805 }