2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * GENERALIZEDTIME implementation. Based on UTCTIME
16 #include "internal/cryptlib.h"
17 #include <openssl/asn1.h>
18 #include "asn1_locl.h"
20 int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
22 /* wrapper around asn1_time_to_tm */
23 if (d->type != V_ASN1_GENERALIZEDTIME)
25 return asn1_time_to_tm(tm, d);
28 int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
30 return asn1_generalizedtime_to_tm(NULL, d);
33 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
35 ASN1_GENERALIZEDTIME t;
37 t.type = V_ASN1_GENERALIZEDTIME;
38 t.length = strlen(str);
39 t.data = (unsigned char *)str;
42 if (ASN1_GENERALIZEDTIME_check(&t)) {
44 if (!ASN1_STRING_set((ASN1_STRING *)s, str, t.length))
46 s->type = V_ASN1_GENERALIZEDTIME;
53 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
56 return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
59 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
60 time_t t, int offset_day,
66 const size_t len = 20;
67 ASN1_GENERALIZEDTIME *tmps = NULL;
70 tmps = ASN1_GENERALIZEDTIME_new();
76 ts = OPENSSL_gmtime(&t, &data);
80 if (offset_day || offset_sec) {
81 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
85 p = (char *)tmps->data;
86 if ((p == NULL) || ((size_t)tmps->length < len)) {
87 p = OPENSSL_malloc(len);
89 ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE);
92 OPENSSL_free(tmps->data);
93 tmps->data = (unsigned char *)p;
96 tmps->length = BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ",
97 ts->tm_year + 1900, ts->tm_mon + 1,
98 ts->tm_mday, ts->tm_hour, ts->tm_min,
100 tmps->type = V_ASN1_GENERALIZEDTIME;
101 #ifdef CHARSET_EBCDIC_not
102 ebcdic2ascii(tmps->data, tmps->data, tmps->length);
107 ASN1_GENERALIZEDTIME_free(tmps);
111 int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
113 if (tm->type != V_ASN1_GENERALIZEDTIME)
115 return ASN1_TIME_print(bp, tm);