moved parse_time() from x509.c to asn1.c
authorAndreas Steffen <andreas.steffen@strongswan.org>
Thu, 12 Apr 2007 18:58:27 +0000 (18:58 -0000)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Thu, 12 Apr 2007 18:58:27 +0000 (18:58 -0000)
src/libstrongswan/asn1/asn1.c
src/libstrongswan/asn1/asn1.h

index 91a6621..2a0aa4f 100644 (file)
@@ -731,3 +731,43 @@ chunk_t timetoasn1(const time_t *time, asn1_t type)
        formatted_time.len = strlen(buf);
        return asn1_simple_object(type, formatted_time);
 }
+
+/**
+ * ASN.1 definition of time
+ */
+static const asn1Object_t timeObjects[] = {
+       { 0,   "utcTime",               ASN1_UTCTIME,                   ASN1_OPT|ASN1_BODY      }, /*  0 */
+       { 0,   "end opt",               ASN1_EOC,                               ASN1_END                        }, /*  1 */
+       { 0,   "generalizeTime",ASN1_GENERALIZEDTIME,   ASN1_OPT|ASN1_BODY      }, /*  2 */
+       { 0,   "end opt",               ASN1_EOC,                               ASN1_END                        }  /*  3 */
+};
+#define TIME_UTC                       0
+#define TIME_GENERALIZED       2
+#define TIME_ROOF                      4
+
+/**
+ * extracts and converts a UTCTIME or GENERALIZEDTIME object
+ */
+time_t parse_time(chunk_t blob, int level0)
+{
+       asn1_ctx_t ctx;
+       chunk_t object;
+       u_int level;
+       int objectID = 0;
+       
+       asn1_init(&ctx, blob, level0, FALSE, FALSE);
+       
+       while (objectID < TIME_ROOF)
+       {
+               if (!extract_object(timeObjects, &objectID, &object, &level, &ctx))
+                       return 0;
+               
+               if (objectID == TIME_UTC || objectID == TIME_GENERALIZED)
+               {
+                       return asn1totime(&object, (objectID == TIME_UTC)
+                                       ? ASN1_UTCTIME : ASN1_GENERALIZEDTIME);
+               }
+               objectID++;
+       }
+       return 0;
+}
index 5ab519e..365ccb4 100644 (file)
@@ -124,6 +124,8 @@ extern void asn1_init(asn1_ctx_t *ctx, chunk_t blob, u_int level0, bool implicit
 extern bool extract_object(asn1Object_t const *objects, u_int *objectID, chunk_t *object, u_int *level, asn1_ctx_t *ctx);
 extern bool parse_asn1_simple_object(chunk_t *object, asn1_t type, u_int level, const char* name);
 extern int parse_algorithmIdentifier(chunk_t blob, int level0, chunk_t *parameters);
+extern time_t parse_time(chunk_t blob, int level0);
+
 extern bool is_asn1(chunk_t blob);
 
 extern void code_asn1_length(size_t length, chunk_t *code);