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 const char *_asn1_mon[12] = {
112 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
113 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
116 int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
121 int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
126 v = (char *)tm->data;
132 for (i = 0; i < 12; i++)
133 if ((v[i] > '9') || (v[i] < '0'))
135 y = (v[0] - '0') * 1000 + (v[1] - '0') * 100
136 + (v[2] - '0') * 10 + (v[3] - '0');
137 M = (v[4] - '0') * 10 + (v[5] - '0');
138 if ((M > 12) || (M < 1))
140 d = (v[6] - '0') * 10 + (v[7] - '0');
141 h = (v[8] - '0') * 10 + (v[9] - '0');
142 m = (v[10] - '0') * 10 + (v[11] - '0');
143 if (tm->length >= 14 &&
144 (v[12] >= '0') && (v[12] <= '9') &&
145 (v[13] >= '0') && (v[13] <= '9')) {
146 s = (v[12] - '0') * 10 + (v[13] - '0');
147 /* Check for fractions of seconds. */
148 if (tm->length >= 15 && v[14] == '.') {
150 f = &v[14]; /* The decimal point. */
152 while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
157 if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
158 _asn1_mon[M - 1], d, h, m, s, f_len, f, y,
159 (gmt) ? " GMT" : "") <= 0)
164 BIO_write(bp, "Bad time value", 14);