2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 "crypto/ctype.h"
20 #include "internal/cryptlib.h"
21 #include <openssl/asn1t.h>
22 #include "asn1_local.h"
24 IMPLEMENT_ASN1_MSTRING(ASN1_TIME
, B_ASN1_TIME
)
26 IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME
)
27 IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_TIME
)
29 static int is_utc(const int year
)
31 if (50 <= year
&& year
<= 149)
36 static int leap_year(const int year
)
38 if (year
% 400 == 0 || (year
% 100 != 0 && year
% 4 == 0))
44 * Compute the day of the week and the day of the year from the year, month
45 * and day. The day of the year is straightforward, the day of the week uses
46 * a form of Zeller's congruence. For this months start with March and are
47 * numbered 4 through 15.
49 static void determine_days(struct tm
*tm
)
51 static const int ydays
[12] = {
52 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
54 int y
= tm
->tm_year
+ 1900;
59 tm
->tm_yday
= ydays
[m
] + d
- 1;
61 /* March and onwards can be one day further into the year */
62 tm
->tm_yday
+= leap_year(y
);
65 /* Treat January and February as part of the previous year */
71 /* Zeller's congruence */
72 tm
->tm_wday
= (d
+ (13 * m
) / 5 + y
+ y
/ 4 + c
/ 4 + 5 * c
+ 6) % 7;
75 int asn1_time_to_tm(struct tm
*tm
, const ASN1_TIME
*d
)
77 static const int min
[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
78 static const int max
[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
79 static const int mdays
[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
81 int n
, i
, i2
, l
, o
, min_l
= 11, strict
= 0, end
= 6, btz
= 5, md
;
83 #if defined(CHARSET_EBCDIC)
84 const char upper_z
= 0x5A, num_zero
= 0x30, period
= 0x2E, minus
= 0x2D, plus
= 0x2B;
86 const char upper_z
= 'Z', num_zero
= '0', period
= '.', minus
= '-', plus
= '+';
89 * ASN1_STRING_FLAG_X509_TIME is used to enforce RFC 5280
90 * time string format, in which:
92 * 1. "seconds" is a 'MUST'
93 * 2. "Zulu" timezone is a 'MUST'
94 * 3. "+|-" is not allowed to indicate a time zone
96 if (d
->type
== V_ASN1_UTCTIME
) {
97 if (d
->flags
& ASN1_STRING_FLAG_X509_TIME
) {
101 } else if (d
->type
== V_ASN1_GENERALIZEDTIME
) {
104 if (d
->flags
& ASN1_STRING_FLAG_X509_TIME
) {
117 memset(&tmp
, 0, sizeof(tmp
));
120 * GENERALIZEDTIME is similar to UTCTIME except the year is represented
121 * as YYYY. This stuff treats everything as a two digit field so make
122 * first two fields 00 to 99
127 for (i
= 0; i
< end
; i
++) {
128 if (!strict
&& (i
== btz
) && ((a
[o
] == upper_z
) || (a
[o
] == plus
) || (a
[o
] == minus
))) {
132 if (!ascii_isdigit(a
[o
]))
135 /* incomplete 2-digital number */
139 if (!ascii_isdigit(a
[o
]))
141 n
= (n
* 10) + a
[o
] - num_zero
;
142 /* no more bytes to read, but we haven't seen time-zone yet */
146 i2
= (d
->type
== V_ASN1_UTCTIME
) ? i
+ 1 : i
;
148 if ((n
< min
[i2
]) || (n
> max
[i2
]))
152 /* UTC will never be here */
153 tmp
.tm_year
= n
* 100 - 1900;
156 if (d
->type
== V_ASN1_UTCTIME
)
157 tmp
.tm_year
= n
< 50 ? n
+ 100 : n
;
165 /* check if tm_mday is valid in tm_mon */
166 if (tmp
.tm_mon
== 1) {
168 md
= mdays
[1] + leap_year(tmp
.tm_year
+ 1900);
170 md
= mdays
[tmp
.tm_mon
];
175 determine_days(&tmp
);
190 * Optional fractional seconds: decimal point followed by one or more
193 if (d
->type
== V_ASN1_GENERALIZEDTIME
&& a
[o
] == period
) {
195 /* RFC 5280 forbids fractional seconds */
200 while ((o
< l
) && ascii_isdigit(a
[o
]))
202 /* Must have at least one digit after decimal point */
205 /* no more bytes to read, but we haven't seen time-zone yet */
211 * 'o' will never point to '\0' at this point, the only chance
212 * 'o' can point to '\0' is either the subsequent if or the first
215 if (a
[o
] == upper_z
) {
217 } else if (!strict
&& ((a
[o
] == plus
) || (a
[o
] == minus
))) {
218 int offsign
= a
[o
] == minus
? 1 : -1;
223 * if not equal, no need to do subsequent checks
224 * since the following for-loop will add 'o' by 4
225 * and the final return statement will check if 'l'
230 for (i
= end
; i
< end
+ 2; i
++) {
231 if (!ascii_isdigit(a
[o
]))
235 if (!ascii_isdigit(a
[o
]))
237 n
= (n
* 10) + a
[o
] - num_zero
;
238 i2
= (d
->type
== V_ASN1_UTCTIME
) ? i
+ 1 : i
;
239 if ((n
< min
[i2
]) || (n
> max
[i2
]))
241 /* if tm is NULL, no need to adjust */
245 else if (i
== end
+ 1)
250 if (offset
&& !OPENSSL_gmtime_adj(&tmp
, 0, offset
* offsign
))
253 /* not Z, or not +/- in non-strict mode */
257 /* success, check if tm should be filled */
266 ASN1_TIME
*asn1_time_from_tm(ASN1_TIME
*s
, struct tm
*ts
, int type
)
269 ASN1_TIME
*tmps
= NULL
;
270 const size_t len
= 20;
272 if (type
== V_ASN1_UNDEF
) {
273 if (is_utc(ts
->tm_year
))
274 type
= V_ASN1_UTCTIME
;
276 type
= V_ASN1_GENERALIZEDTIME
;
277 } else if (type
== V_ASN1_UTCTIME
) {
278 if (!is_utc(ts
->tm_year
))
280 } else if (type
!= V_ASN1_GENERALIZEDTIME
) {
285 tmps
= ASN1_STRING_new();
291 if (!ASN1_STRING_set(tmps
, NULL
, len
))
295 p
= (char*)tmps
->data
;
297 if (type
== V_ASN1_GENERALIZEDTIME
)
298 tmps
->length
= BIO_snprintf(p
, len
, "%04d%02d%02d%02d%02d%02dZ",
299 ts
->tm_year
+ 1900, ts
->tm_mon
+ 1,
300 ts
->tm_mday
, ts
->tm_hour
, ts
->tm_min
,
303 tmps
->length
= BIO_snprintf(p
, len
, "%02d%02d%02d%02d%02d%02dZ",
304 ts
->tm_year
% 100, ts
->tm_mon
+ 1,
305 ts
->tm_mday
, ts
->tm_hour
, ts
->tm_min
,
308 #ifdef CHARSET_EBCDIC
309 ebcdic2ascii(tmps
->data
, tmps
->data
, tmps
->length
);
314 ASN1_STRING_free(tmps
);
318 ASN1_TIME
*ASN1_TIME_set(ASN1_TIME
*s
, time_t t
)
320 return ASN1_TIME_adj(s
, t
, 0, 0);
323 ASN1_TIME
*ASN1_TIME_adj(ASN1_TIME
*s
, time_t t
,
324 int offset_day
, long offset_sec
)
329 ts
= OPENSSL_gmtime(&t
, &data
);
331 ASN1err(ASN1_F_ASN1_TIME_ADJ
, ASN1_R_ERROR_GETTING_TIME
);
334 if (offset_day
|| offset_sec
) {
335 if (!OPENSSL_gmtime_adj(ts
, offset_day
, offset_sec
))
338 return asn1_time_from_tm(s
, ts
, V_ASN1_UNDEF
);
341 int ASN1_TIME_check(const ASN1_TIME
*t
)
343 if (t
->type
== V_ASN1_GENERALIZEDTIME
)
344 return ASN1_GENERALIZEDTIME_check(t
);
345 else if (t
->type
== V_ASN1_UTCTIME
)
346 return ASN1_UTCTIME_check(t
);
350 /* Convert an ASN1_TIME structure to GeneralizedTime */
351 ASN1_GENERALIZEDTIME
*ASN1_TIME_to_generalizedtime(const ASN1_TIME
*t
,
352 ASN1_GENERALIZEDTIME
**out
)
354 ASN1_GENERALIZEDTIME
*ret
= NULL
;
357 if (!ASN1_TIME_to_tm(t
, &tm
))
363 ret
= asn1_time_from_tm(ret
, &tm
, V_ASN1_GENERALIZEDTIME
);
365 if (out
!= NULL
&& ret
!= NULL
)
371 int ASN1_TIME_set_string(ASN1_TIME
*s
, const char *str
)
373 /* Try UTC, if that fails, try GENERALIZED */
374 if (ASN1_UTCTIME_set_string(s
, str
))
376 return ASN1_GENERALIZEDTIME_set_string(s
, str
);
379 int ASN1_TIME_set_string_X509(ASN1_TIME
*s
, const char *str
)
385 t
.length
= strlen(str
);
386 t
.data
= (unsigned char *)str
;
387 t
.flags
= ASN1_STRING_FLAG_X509_TIME
;
389 t
.type
= V_ASN1_UTCTIME
;
391 if (!ASN1_TIME_check(&t
)) {
392 t
.type
= V_ASN1_GENERALIZEDTIME
;
393 if (!ASN1_TIME_check(&t
))
398 * Per RFC 5280 (section 4.1.2.5.), the valid input time
399 * strings should be encoded with the following rules:
401 * 1. UTC: YYMMDDHHMMSSZ, if YY < 50 (20YY) --> UTC: YYMMDDHHMMSSZ
402 * 2. UTC: YYMMDDHHMMSSZ, if YY >= 50 (19YY) --> UTC: YYMMDDHHMMSSZ
403 * 3. G'd: YYYYMMDDHHMMSSZ, if YYYY >= 2050 --> G'd: YYYYMMDDHHMMSSZ
404 * 4. G'd: YYYYMMDDHHMMSSZ, if YYYY < 2050 --> UTC: YYMMDDHHMMSSZ
406 * Only strings of the 4th rule should be reformatted, but since a
407 * UTC can only present [1950, 2050), so if the given time string
408 * is less than 1950 (e.g. 19230419000000Z), we do nothing...
411 if (s
!= NULL
&& t
.type
== V_ASN1_GENERALIZEDTIME
) {
412 if (!asn1_time_to_tm(&tm
, &t
))
414 if (is_utc(tm
.tm_year
)) {
417 * it's OK to let original t.data go since that's assigned
418 * to a piece of memory allocated outside of this function.
419 * new t.data would be freed after ASN1_STRING_copy is done.
421 t
.data
= OPENSSL_zalloc(t
.length
+ 1);
424 memcpy(t
.data
, str
+ 2, t
.length
);
425 t
.type
= V_ASN1_UTCTIME
;
429 if (s
== NULL
|| ASN1_STRING_copy((ASN1_STRING
*)s
, (ASN1_STRING
*)&t
))
432 if (t
.data
!= (unsigned char *)str
)
433 OPENSSL_free(t
.data
);
438 int ASN1_TIME_to_tm(const ASN1_TIME
*s
, struct tm
*tm
)
444 memset(tm
, 0, sizeof(*tm
));
445 if (OPENSSL_gmtime(&now_t
, tm
) != NULL
)
450 return asn1_time_to_tm(tm
, s
);
453 int ASN1_TIME_diff(int *pday
, int *psec
,
454 const ASN1_TIME
*from
, const ASN1_TIME
*to
)
456 struct tm tm_from
, tm_to
;
458 if (!ASN1_TIME_to_tm(from
, &tm_from
))
460 if (!ASN1_TIME_to_tm(to
, &tm_to
))
462 return OPENSSL_gmtime_diff(pday
, psec
, &tm_from
, &tm_to
);
465 static const char _asn1_mon
[12][4] = {
466 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
467 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
470 int ASN1_TIME_print(BIO
*bp
, const ASN1_TIME
*tm
)
475 const char upper_z
= 0x5A, period
= 0x2E;
477 if (!asn1_time_to_tm(&stm
, tm
)) {
478 /* asn1_time_to_tm will check the time type */
483 v
= (char *)tm
->data
;
484 if (v
[l
- 1] == upper_z
)
487 if (tm
->type
== V_ASN1_GENERALIZEDTIME
) {
492 * Try to parse fractional seconds. '14' is the place of
493 * 'fraction point' in a GeneralizedTime string.
495 if (tm
->length
> 15 && v
[14] == period
) {
498 while (14 + f_len
< l
&& ascii_isdigit(f
[f_len
]))
502 return BIO_printf(bp
, "%s %2d %02d:%02d:%02d%.*s %d%s",
503 _asn1_mon
[stm
.tm_mon
], stm
.tm_mday
, stm
.tm_hour
,
504 stm
.tm_min
, stm
.tm_sec
, f_len
, f
, stm
.tm_year
+ 1900,
505 (gmt
? " GMT" : "")) > 0;
507 return BIO_printf(bp
, "%s %2d %02d:%02d:%02d %d%s",
508 _asn1_mon
[stm
.tm_mon
], stm
.tm_mday
, stm
.tm_hour
,
509 stm
.tm_min
, stm
.tm_sec
, stm
.tm_year
+ 1900,
510 (gmt
? " GMT" : "")) > 0;
513 BIO_write(bp
, "Bad time value", 14);
517 int ASN1_TIME_cmp_time_t(const ASN1_TIME
*s
, time_t t
)
522 if (!ASN1_TIME_to_tm(s
, &stm
))
525 if (!OPENSSL_gmtime(&t
, &ttm
))
528 if (!OPENSSL_gmtime_diff(&day
, &sec
, &ttm
, &stm
))
531 if (day
> 0 || sec
> 0)
533 if (day
< 0 || sec
< 0)
538 int ASN1_TIME_normalize(ASN1_TIME
*t
)
542 if (!ASN1_TIME_to_tm(t
, &tm
))
545 return asn1_time_from_tm(t
, &tm
, V_ASN1_UNDEF
) != NULL
;
548 int ASN1_TIME_compare(const ASN1_TIME
*a
, const ASN1_TIME
*b
)
552 if (!ASN1_TIME_diff(&day
, &sec
, b
, a
))
554 if (day
> 0 || sec
> 0)
556 if (day
< 0 || sec
< 0)