2 * Copyright 1995-2016 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 static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
23 static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
27 if (d->type != V_ASN1_GENERALIZEDTIME)
33 * GENERALIZEDTIME is similar to UTCTIME except the year is represented
34 * as YYYY. This stuff treats everything as a two digit field so make
35 * first two fields 00 to 99
39 for (i = 0; i < 7; i++) {
40 if ((i == 6) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
46 if ((a[o] < '0') || (a[o] > '9'))
52 if ((a[o] < '0') || (a[o] > '9'))
54 n = (n * 10) + a[o] - '0';
58 if ((n < min[i]) || (n > max[i]))
63 tm->tm_year = n * 100 - 1900;
87 * Optional fractional seconds: decimal point followed by one or more
94 while ((a[o] >= '0') && (a[o] <= '9') && (o <= l))
96 /* Must have at least one digit after decimal point */
103 else if ((a[o] == '+') || (a[o] == '-')) {
104 int offsign = a[o] == '-' ? 1 : -1, offset = 0;
108 for (i = 7; i < 9; i++) {
109 if ((a[o] < '0') || (a[o] > '9'))
113 if ((a[o] < '0') || (a[o] > '9'))
115 n = (n * 10) + a[o] - '0';
116 if ((n < min[i]) || (n > max[i]))
126 if (offset && !OPENSSL_gmtime_adj(tm, 0, offset * offsign))
129 /* Missing time zone information. */
137 int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
139 return asn1_generalizedtime_to_tm(NULL, d);
142 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
144 ASN1_GENERALIZEDTIME t;
146 t.type = V_ASN1_GENERALIZEDTIME;
147 t.length = strlen(str);
148 t.data = (unsigned char *)str;
149 if (ASN1_GENERALIZEDTIME_check(&t)) {
151 if (!ASN1_STRING_set((ASN1_STRING *)s, str, t.length))
153 s->type = V_ASN1_GENERALIZEDTIME;
160 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
163 return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
166 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
167 time_t t, int offset_day,
174 ASN1_GENERALIZEDTIME *tmps = NULL;
177 tmps = ASN1_GENERALIZEDTIME_new();
183 ts = OPENSSL_gmtime(&t, &data);
187 if (offset_day || offset_sec) {
188 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
192 p = (char *)tmps->data;
193 if ((p == NULL) || ((size_t)tmps->length < len)) {
194 p = OPENSSL_malloc(len);
196 ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE);
199 OPENSSL_free(tmps->data);
200 tmps->data = (unsigned char *)p;
203 BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900,
204 ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
206 tmps->length = strlen(p);
207 tmps->type = V_ASN1_GENERALIZEDTIME;
208 #ifdef CHARSET_EBCDIC_not
209 ebcdic2ascii(tmps->data, tmps->data, tmps->length);
214 ASN1_GENERALIZEDTIME_free(tmps);
218 const char *_asn1_mon[12] = {
219 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
220 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
223 int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
228 int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
233 v = (char *)tm->data;
239 for (i = 0; i < 12; i++)
240 if ((v[i] > '9') || (v[i] < '0'))
242 y = (v[0] - '0') * 1000 + (v[1] - '0') * 100
243 + (v[2] - '0') * 10 + (v[3] - '0');
244 M = (v[4] - '0') * 10 + (v[5] - '0');
245 if ((M > 12) || (M < 1))
247 d = (v[6] - '0') * 10 + (v[7] - '0');
248 h = (v[8] - '0') * 10 + (v[9] - '0');
249 m = (v[10] - '0') * 10 + (v[11] - '0');
250 if (tm->length >= 14 &&
251 (v[12] >= '0') && (v[12] <= '9') &&
252 (v[13] >= '0') && (v[13] <= '9')) {
253 s = (v[12] - '0') * 10 + (v[13] - '0');
254 /* Check for fractions of seconds. */
255 if (tm->length >= 15 && v[14] == '.') {
257 f = &v[14]; /* The decimal point. */
259 while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
264 if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
265 _asn1_mon[M - 1], d, h, m, s, f_len, f, y,
266 (gmt) ? " GMT" : "") <= 0)
271 BIO_write(bp, "Bad time value", 14);