2 * Copyright 1999-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 * This is an implementation of the ASN1 Time structure which is:
14 * generalTime GeneralizedTime }
19 #include "internal/cryptlib.h"
20 #include <openssl/asn1t.h>
21 #include "asn1_locl.h"
23 IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
25 IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
27 static int leap_year(const int year)
29 if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
35 * Compute the day of the week and the day of the year from the year, month
36 * and day. The day of the year is straightforward, the day of the week uses
37 * a form of Zeller's congruence. For this months start with March and are
38 * numbered 4 through 15.
40 static void determine_days(struct tm *tm)
42 static const int ydays[12] = {
43 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
45 int y = tm->tm_year + 1900;
50 tm->tm_yday = ydays[m] + d - 1;
52 /* March and onwards can be one day further into the year */
53 tm->tm_yday += leap_year(y);
56 /* Treat January and February as part of the previous year */
62 /* Zeller's congruance */
63 tm->tm_wday = (d + (13 * m) / 5 + y + y / 4 + c / 4 + 5 * c + 6) % 7;
66 int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *d)
68 static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
69 static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
70 static const int mdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
72 int n, i, i2, l, o, min_l = 11, strict = 0, end = 6, btz = 5, md;
76 * ASN1_STRING_FLAG_X509_TIME is used to enforce RFC 5280
77 * time string format, in which:
79 * 1. "seconds" is a 'MUST'
80 * 2. "Zulu" timezone is a 'MUST'
81 * 3. "+|-" is not allowed to indicate a time zone
83 if (d->type == V_ASN1_UTCTIME) {
84 if (d->flags & ASN1_STRING_FLAG_X509_TIME) {
88 } else if (d->type == V_ASN1_GENERALIZEDTIME) {
91 if (d->flags & ASN1_STRING_FLAG_X509_TIME) {
104 memset(&tmp, 0, sizeof(tmp));
107 * GENERALIZEDTIME is similar to UTCTIME except the year is represented
108 * as YYYY. This stuff treats everything as a two digit field so make
109 * first two fields 00 to 99
114 for (i = 0; i < end; i++) {
115 if (!strict && (i == btz) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
119 if ((a[o] < '0') || (a[o] > '9'))
122 /* incomplete 2-digital number */
126 if ((a[o] < '0') || (a[o] > '9'))
128 n = (n * 10) + a[o] - '0';
129 /* no more bytes to read, but we haven't seen time-zone yet */
133 i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
135 if ((n < min[i2]) || (n > max[i2]))
139 /* UTC will never be here */
140 tmp.tm_year = n * 100 - 1900;
143 if (d->type == V_ASN1_UTCTIME)
144 tmp.tm_year = n < 50 ? n + 100 : n;
152 /* check if tm_mday is valid in tm_mon */
153 if (tmp.tm_mon == 1) {
155 md = mdays[1] + leap_year(tmp.tm_year + 1900);
157 md = mdays[tmp.tm_mon];
162 determine_days(&tmp);
177 * Optional fractional seconds: decimal point followed by one or more
180 if (d->type == V_ASN1_GENERALIZEDTIME && a[o] == '.') {
182 /* RFC 5280 forbids fractional seconds */
187 while ((o < l) && (a[o] >= '0') && (a[o] <= '9'))
189 /* Must have at least one digit after decimal point */
192 /* no more bytes to read, but we haven't seen time-zone yet */
198 * 'o' will never point to '\0' at this point, the only chance
199 * 'o' can point to '\0' is either the subsequent if or the first
204 } else if (!strict && ((a[o] == '+') || (a[o] == '-'))) {
205 int offsign = a[o] == '-' ? 1 : -1;
210 * if not equal, no need to do subsequent checks
211 * since the following for-loop will add 'o' by 4
212 * and the final return statement will check if 'l'
217 for (i = end; i < end + 2; i++) {
218 if ((a[o] < '0') || (a[o] > '9'))
222 if ((a[o] < '0') || (a[o] > '9'))
224 n = (n * 10) + a[o] - '0';
225 i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
226 if ((n < min[i2]) || (n > max[i2]))
228 /* if tm is NULL, no need to adjust */
232 else if (i == end + 1)
237 if (offset && !OPENSSL_gmtime_adj(&tmp, 0, offset * offsign))
240 /* not Z, or not +/- in non-strict mode */
244 /* success, check if tm should be filled */
253 ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
255 return ASN1_TIME_adj(s, t, 0, 0);
258 ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
259 int offset_day, long offset_sec)
264 ts = OPENSSL_gmtime(&t, &data);
266 ASN1err(ASN1_F_ASN1_TIME_ADJ, ASN1_R_ERROR_GETTING_TIME);
269 if (offset_day || offset_sec) {
270 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
273 if ((ts->tm_year >= 50) && (ts->tm_year < 150))
274 return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
275 return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
278 int ASN1_TIME_check(const ASN1_TIME *t)
280 if (t->type == V_ASN1_GENERALIZEDTIME)
281 return ASN1_GENERALIZEDTIME_check(t);
282 else if (t->type == V_ASN1_UTCTIME)
283 return ASN1_UTCTIME_check(t);
287 /* Convert an ASN1_TIME structure to GeneralizedTime */
288 ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
289 ASN1_GENERALIZEDTIME **out)
291 ASN1_GENERALIZEDTIME *ret = NULL;
294 if (!ASN1_TIME_check(t))
297 if (out == NULL || *out == NULL) {
298 if ((ret = ASN1_GENERALIZEDTIME_new()) == NULL)
304 /* If already GeneralizedTime just copy across */
305 if (t->type == V_ASN1_GENERALIZEDTIME) {
306 if (!ASN1_STRING_set(ret, t->data, t->length))
312 * Grow the string by two bytes.
313 * The actual allocation is t->length + 3 to include a terminator byte.
315 if (!ASN1_STRING_set(ret, NULL, t->length + 2))
317 str = (char *)ret->data;
318 /* Work out the century and prepend */
319 memcpy(str, t->data[0] >= '5' ? "19" : "20", 2);
321 * t->length + 1 is the size of the data and the allocated buffer has
322 * this much space after the first two characters.
324 OPENSSL_strlcpy(str + 2, (const char *)t->data, t->length + 1);
327 if (out != NULL && *out == NULL)
332 if (out == NULL || *out != ret)
333 ASN1_GENERALIZEDTIME_free(ret);
337 int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
341 t.length = strlen(str);
342 t.data = (unsigned char *)str;
345 t.type = V_ASN1_UTCTIME;
347 if (!ASN1_TIME_check(&t)) {
348 t.type = V_ASN1_GENERALIZEDTIME;
349 if (!ASN1_TIME_check(&t))
353 if (s != NULL && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
359 int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str)
365 t.length = strlen(str);
366 t.data = (unsigned char *)str;
367 t.flags = ASN1_STRING_FLAG_X509_TIME;
369 t.type = V_ASN1_UTCTIME;
371 if (!ASN1_TIME_check(&t)) {
372 t.type = V_ASN1_GENERALIZEDTIME;
373 if (!ASN1_TIME_check(&t))
378 * Per RFC 5280 (section 4.1.2.5.), the valid input time
379 * strings should be encoded with the following rules:
381 * 1. UTC: YYMMDDHHMMSSZ, if YY < 50 (20YY) --> UTC: YYMMDDHHMMSSZ
382 * 2. UTC: YYMMDDHHMMSSZ, if YY >= 50 (19YY) --> UTC: YYMMDDHHMMSSZ
383 * 3. G'd: YYYYMMDDHHMMSSZ, if YYYY >= 2050 --> G'd: YYYYMMDDHHMMSSZ
384 * 4. G'd: YYYYMMDDHHMMSSZ, if YYYY < 2050 --> UTC: YYMMDDHHMMSSZ
386 * Only strings of the 4th rule should be reformatted, but since a
387 * UTC can only present [1950, 2050), so if the given time string
388 * is less than 1950 (e.g. 19230419000000Z), we do nothing...
391 if (s != NULL && t.type == V_ASN1_GENERALIZEDTIME) {
392 if (!asn1_time_to_tm(&tm, &t))
394 if (tm.tm_year >= 50 && tm.tm_year < 150) {
397 * it's OK to let original t.data go since that's assigned
398 * to a piece of memory allocated outside of this function.
399 * new t.data would be freed after ASN1_STRING_copy is done.
401 t.data = OPENSSL_zalloc(t.length + 1);
404 memcpy(t.data, str + 2, t.length);
405 t.type = V_ASN1_UTCTIME;
409 if (s == NULL || ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
412 if (t.data != (unsigned char *)str)
413 OPENSSL_free(t.data);
418 int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm)
424 memset(tm, 0, sizeof(*tm));
425 if (OPENSSL_gmtime(&now_t, tm) != NULL)
430 return asn1_time_to_tm(tm, s);
433 int ASN1_TIME_diff(int *pday, int *psec,
434 const ASN1_TIME *from, const ASN1_TIME *to)
436 struct tm tm_from, tm_to;
438 if (!ASN1_TIME_to_tm(from, &tm_from))
440 if (!ASN1_TIME_to_tm(to, &tm_to))
442 return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
445 static const char _asn1_mon[12][4] = {
446 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
447 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
450 int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
456 if (!asn1_time_to_tm(&stm, tm)) {
457 /* asn1_time_to_tm will check the time type */
462 v = (char *)tm->data;
466 if (tm->type == V_ASN1_GENERALIZEDTIME) {
471 * Try to parse fractional seconds. '14' is the place of
472 * 'fraction point' in a GeneralizedTime string.
474 if (tm->length > 15 && v[14] == '.') {
477 while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
481 return BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
482 _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
483 stm.tm_min, stm.tm_sec, f_len, f, stm.tm_year + 1900,
484 (gmt ? " GMT" : "")) > 0;
486 return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
487 _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
488 stm.tm_min, stm.tm_sec, stm.tm_year + 1900,
489 (gmt ? " GMT" : "")) > 0;
492 BIO_write(bp, "Bad time value", 14);