Various OCSP responder utility functions.
[openssl.git] / crypto / asn1 / a_time.c
index edc4929c361c0512ec4c9eaff19a9939a51fb050..03788a7d62a67ccba0709d306b0295c26efc78ba 100644 (file)
@@ -112,3 +112,49 @@ ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
                                        return ASN1_UTCTIME_set(s, t);
        return ASN1_GENERALIZEDTIME_set(s,t);
        }
+
+int ASN1_TIME_check(ASN1_TIME *t)
+       {
+       if (t->type == V_ASN1_GENERALIZEDTIME)
+               return ASN1_GENERALIZEDTIME_check(t);
+       else if (t->type == V_ASN1_UTCTIME)
+               return ASN1_UTCTIME_check(t);
+       return 0;
+       }
+
+/* Convert an ASN1_TIME structure to GeneralizedTime */
+ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t, ASN1_GENERALIZEDTIME **out)
+       {
+       ASN1_GENERALIZEDTIME *ret;
+       char *str;
+
+       if (!ASN1_TIME_check(t)) return NULL;
+
+       if (!out || !*out)
+               {
+               if (!(ret = ASN1_GENERALIZEDTIME_new ()))
+                       return NULL;
+               if (out) *out = ret;
+               }
+       else ret = *out;
+
+       /* If already GeneralizedTime just copy across */
+       if (t->type == V_ASN1_GENERALIZEDTIME)
+               {
+               if(!ASN1_STRING_set(ret, t->data, t->length))
+                       return NULL;
+               return ret;
+               }
+
+       /* grow the string */
+       if (!ASN1_STRING_set(ret, NULL, t->length + 2))
+               return NULL;
+       /* Work out the century and prepend */
+       str = (char *)t->data;
+       if (*str >= '5') strcpy(str, "19");
+       else strcpy(str, "20");
+
+       strcat(str, (char *)t->data);
+
+       return ret;
+       }