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/ctype.h"
20 #include "internal/cryptlib.h"
21 #include <openssl/asn1t.h>
22 #include "asn1_locl.h"
24 IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
26 IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
28 static int is_utc(const int year)
30 if (50 <= year && year <= 149)
35 static int leap_year(const int year)
37 if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
43 * Compute the day of the week and the day of the year from the year, month
44 * and day. The day of the year is straightforward, the day of the week uses
45 * a form of Zeller's congruence. For this months start with March and are
46 * numbered 4 through 15.
48 static void determine_days(struct tm *tm)
50 static const int ydays[12] = {
51 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
53 int y = tm->tm_year + 1900;
58 tm->tm_yday = ydays[m] + d - 1;
60 /* March and onwards can be one day further into the year */
61 tm->tm_yday += leap_year(y);
64 /* Treat January and February as part of the previous year */
70 /* Zeller's congruance */
71 tm->tm_wday = (d + (13 * m) / 5 + y + y / 4 + c / 4 + 5 * c + 6) % 7;
74 int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *d)
76 static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
77 static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
78 static const int mdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
80 int n, i, i2, l, o, min_l = 11, strict = 0, end = 6, btz = 5, md;
84 * ASN1_STRING_FLAG_X509_TIME is used to enforce RFC 5280
85 * time string format, in which:
87 * 1. "seconds" is a 'MUST'
88 * 2. "Zulu" timezone is a 'MUST'
89 * 3. "+|-" is not allowed to indicate a time zone
91 if (d->type == V_ASN1_UTCTIME) {
92 if (d->flags & ASN1_STRING_FLAG_X509_TIME) {
96 } else if (d->type == V_ASN1_GENERALIZEDTIME) {
99 if (d->flags & ASN1_STRING_FLAG_X509_TIME) {
112 memset(&tmp, 0, sizeof(tmp));
115 * GENERALIZEDTIME is similar to UTCTIME except the year is represented
116 * as YYYY. This stuff treats everything as a two digit field so make
117 * first two fields 00 to 99
122 for (i = 0; i < end; i++) {
123 if (!strict && (i == btz) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
127 if (!ossl_isdigit(a[o]))
130 /* incomplete 2-digital number */
134 if (!ossl_isdigit(a[o]))
136 n = (n * 10) + a[o] - '0';
137 /* no more bytes to read, but we haven't seen time-zone yet */
141 i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
143 if ((n < min[i2]) || (n > max[i2]))
147 /* UTC will never be here */
148 tmp.tm_year = n * 100 - 1900;
151 if (d->type == V_ASN1_UTCTIME)
152 tmp.tm_year = n < 50 ? n + 100 : n;
160 /* check if tm_mday is valid in tm_mon */
161 if (tmp.tm_mon == 1) {
163 md = mdays[1] + leap_year(tmp.tm_year + 1900);
165 md = mdays[tmp.tm_mon];
170 determine_days(&tmp);
185 * Optional fractional seconds: decimal point followed by one or more
188 if (d->type == V_ASN1_GENERALIZEDTIME && a[o] == '.') {
190 /* RFC 5280 forbids fractional seconds */
195 while ((o < l) && ossl_isdigit(a[o]))
197 /* Must have at least one digit after decimal point */
200 /* no more bytes to read, but we haven't seen time-zone yet */
206 * 'o' will never point to '\0' at this point, the only chance
207 * 'o' can point to '\0' is either the subsequent if or the first
212 } else if (!strict && ((a[o] == '+') || (a[o] == '-'))) {
213 int offsign = a[o] == '-' ? 1 : -1;
218 * if not equal, no need to do subsequent checks
219 * since the following for-loop will add 'o' by 4
220 * and the final return statement will check if 'l'
225 for (i = end; i < end + 2; i++) {
226 if (!ossl_isdigit(a[o]))
230 if (!ossl_isdigit(a[o]))
232 n = (n * 10) + a[o] - '0';
233 i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
234 if ((n < min[i2]) || (n > max[i2]))
236 /* if tm is NULL, no need to adjust */
240 else if (i == end + 1)
245 if (offset && !OPENSSL_gmtime_adj(&tmp, 0, offset * offsign))
248 /* not Z, or not +/- in non-strict mode */
252 /* success, check if tm should be filled */
261 ASN1_TIME *asn1_time_from_tm(ASN1_TIME *s, struct tm *ts, int type)
264 ASN1_TIME *tmps = NULL;
265 const size_t len = 20;
267 if (type == V_ASN1_UNDEF) {
268 if (is_utc(ts->tm_year))
269 type = V_ASN1_UTCTIME;
271 type = V_ASN1_GENERALIZEDTIME;
272 } else if (type == V_ASN1_UTCTIME) {
273 if (!is_utc(ts->tm_year))
275 } else if (type != V_ASN1_GENERALIZEDTIME) {
280 tmps = ASN1_STRING_new();
286 if (!ASN1_STRING_set(tmps, NULL, len))
290 p = (char*)tmps->data;
292 if (type == V_ASN1_GENERALIZEDTIME)
293 tmps->length = BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ",
294 ts->tm_year + 1900, ts->tm_mon + 1,
295 ts->tm_mday, ts->tm_hour, ts->tm_min,
298 tmps->length = BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ",
299 ts->tm_year % 100, ts->tm_mon + 1,
300 ts->tm_mday, ts->tm_hour, ts->tm_min,
303 #ifdef CHARSET_EBCDIC_not
304 ebcdic2ascii(tmps->data, tmps->data, tmps->length);
309 ASN1_STRING_free(tmps);
313 ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
315 return ASN1_TIME_adj(s, t, 0, 0);
318 ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
319 int offset_day, long offset_sec)
324 ts = OPENSSL_gmtime(&t, &data);
326 ASN1err(ASN1_F_ASN1_TIME_ADJ, ASN1_R_ERROR_GETTING_TIME);
329 if (offset_day || offset_sec) {
330 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
333 return asn1_time_from_tm(s, ts, V_ASN1_UNDEF);
336 int ASN1_TIME_check(const ASN1_TIME *t)
338 if (t->type == V_ASN1_GENERALIZEDTIME)
339 return ASN1_GENERALIZEDTIME_check(t);
340 else if (t->type == V_ASN1_UTCTIME)
341 return ASN1_UTCTIME_check(t);
345 /* Convert an ASN1_TIME structure to GeneralizedTime */
346 ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
347 ASN1_GENERALIZEDTIME **out)
349 ASN1_GENERALIZEDTIME *ret = NULL;
352 if (!ASN1_TIME_to_tm(t, &tm))
358 ret = asn1_time_from_tm(ret, &tm, V_ASN1_GENERALIZEDTIME);
360 if (out != NULL && ret != NULL)
366 int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
368 /* Try UTC, if that fails, try GENERALIZED */
369 if (ASN1_UTCTIME_set_string(s, str))
371 return ASN1_GENERALIZEDTIME_set_string(s, str);
374 int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str)
380 t.length = strlen(str);
381 t.data = (unsigned char *)str;
382 t.flags = ASN1_STRING_FLAG_X509_TIME;
384 t.type = V_ASN1_UTCTIME;
386 if (!ASN1_TIME_check(&t)) {
387 t.type = V_ASN1_GENERALIZEDTIME;
388 if (!ASN1_TIME_check(&t))
393 * Per RFC 5280 (section 4.1.2.5.), the valid input time
394 * strings should be encoded with the following rules:
396 * 1. UTC: YYMMDDHHMMSSZ, if YY < 50 (20YY) --> UTC: YYMMDDHHMMSSZ
397 * 2. UTC: YYMMDDHHMMSSZ, if YY >= 50 (19YY) --> UTC: YYMMDDHHMMSSZ
398 * 3. G'd: YYYYMMDDHHMMSSZ, if YYYY >= 2050 --> G'd: YYYYMMDDHHMMSSZ
399 * 4. G'd: YYYYMMDDHHMMSSZ, if YYYY < 2050 --> UTC: YYMMDDHHMMSSZ
401 * Only strings of the 4th rule should be reformatted, but since a
402 * UTC can only present [1950, 2050), so if the given time string
403 * is less than 1950 (e.g. 19230419000000Z), we do nothing...
406 if (s != NULL && t.type == V_ASN1_GENERALIZEDTIME) {
407 if (!asn1_time_to_tm(&tm, &t))
409 if (is_utc(tm.tm_year)) {
412 * it's OK to let original t.data go since that's assigned
413 * to a piece of memory allocated outside of this function.
414 * new t.data would be freed after ASN1_STRING_copy is done.
416 t.data = OPENSSL_zalloc(t.length + 1);
419 memcpy(t.data, str + 2, t.length);
420 t.type = V_ASN1_UTCTIME;
424 if (s == NULL || ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
427 if (t.data != (unsigned char *)str)
428 OPENSSL_free(t.data);
433 int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm)
439 memset(tm, 0, sizeof(*tm));
440 if (OPENSSL_gmtime(&now_t, tm) != NULL)
445 return asn1_time_to_tm(tm, s);
448 int ASN1_TIME_diff(int *pday, int *psec,
449 const ASN1_TIME *from, const ASN1_TIME *to)
451 struct tm tm_from, tm_to;
453 if (!ASN1_TIME_to_tm(from, &tm_from))
455 if (!ASN1_TIME_to_tm(to, &tm_to))
457 return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
460 static const char _asn1_mon[12][4] = {
461 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
462 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
465 int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
471 if (!asn1_time_to_tm(&stm, tm)) {
472 /* asn1_time_to_tm will check the time type */
477 v = (char *)tm->data;
481 if (tm->type == V_ASN1_GENERALIZEDTIME) {
486 * Try to parse fractional seconds. '14' is the place of
487 * 'fraction point' in a GeneralizedTime string.
489 if (tm->length > 15 && v[14] == '.') {
492 while (14 + f_len < l && ossl_isdigit(f[f_len]))
496 return BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
497 _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
498 stm.tm_min, stm.tm_sec, f_len, f, stm.tm_year + 1900,
499 (gmt ? " GMT" : "")) > 0;
501 return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
502 _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
503 stm.tm_min, stm.tm_sec, stm.tm_year + 1900,
504 (gmt ? " GMT" : "")) > 0;
507 BIO_write(bp, "Bad time value", 14);
511 int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t)
516 if (!ASN1_TIME_to_tm(s, &stm))
519 if (!OPENSSL_gmtime(&t, &ttm))
522 if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
525 if (day > 0 || sec > 0)
527 if (day < 0 || sec < 0)
532 int ASN1_TIME_normalize(ASN1_TIME *t)
536 if (!ASN1_TIME_to_tm(t, &tm))
539 return asn1_time_from_tm(t, &tm, V_ASN1_UNDEF) != NULL;
542 int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b)
546 if (!ASN1_TIME_diff(&day, &sec, b, a))
548 if (day > 0 || sec > 0)
550 if (day < 0 || sec < 0)