1 /* $OpenBSD: a_time_tm.c,v 1.15 2018/04/25 11:48:21 tb Exp $ */
3 * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 #define GENTIME_LENGTH 15
27 #define UTCTIME_LENGTH 13
29 #define V_ASN1_UTCTIME 23
30 #define V_ASN1_GENERALIZEDTIME 24
33 ASN1_time_tm_cmp(struct tm *tm1, struct tm *tm2)
35 if (tm1->tm_year < tm2->tm_year)
37 if (tm1->tm_year > tm2->tm_year)
39 if (tm1->tm_mon < tm2->tm_mon)
41 if (tm1->tm_mon > tm2->tm_mon)
43 if (tm1->tm_mday < tm2->tm_mday)
45 if (tm1->tm_mday > tm2->tm_mday)
47 if (tm1->tm_hour < tm2->tm_hour)
49 if (tm1->tm_hour > tm2->tm_hour)
51 if (tm1->tm_min < tm2->tm_min)
53 if (tm1->tm_min > tm2->tm_min)
55 if (tm1->tm_sec < tm2->tm_sec)
57 if (tm1->tm_sec > tm2->tm_sec)
63 ASN1_time_tm_clamp_notafter(struct tm *tm)
66 struct tm broken_os_epoch_tm;
67 time_t broken_os_epoch_time = INT_MAX;
69 if (gmtime_r(&broken_os_epoch_time, &broken_os_epoch_tm) == NULL)
72 if (ASN1_time_tm_cmp(tm, &broken_os_epoch_tm) == 1)
73 memcpy(tm, &broken_os_epoch_tm, sizeof(*tm));
79 * Parse an RFC 5280 format ASN.1 time string.
82 * 0 if we expect to parse a time as specified in RFC 5280 for an X509 object.
83 * V_ASN1_UTCTIME if we wish to parse an RFC5280 format UTC time.
84 * V_ASN1_GENERALIZEDTIME if we wish to parse an RFC5280 format Generalized time.
87 * -1 if the string was invalid.
88 * V_ASN1_UTCTIME if the string validated as a UTC time string.
89 * V_ASN1_GENERALIZEDTIME if the string validated as a Generalized time string.
91 * Fills in *tm with the corresponding time if tm is non NULL.
93 #define ATOI2(ar) ((ar) += 2, ((ar)[-2] - '0') * 10 + ((ar)[-1] - '0'))
95 ASN1_time_parse(const char *bytes, size_t len, struct tm *tm, int mode)
106 /* Constrain to valid lengths. */
107 if (len != UTCTIME_LENGTH && len != GENTIME_LENGTH)
112 memset(<m, 0, sizeof(ltm));
116 /* Timezone is required and must be GMT (Zulu). */
117 if (bytes[len - 1] != 'Z')
120 /* Make sure everything else is digits. */
121 for (i = 0; i < len - 1; i++) {
122 if (isdigit((unsigned char)bytes[i]))
128 * Validate and convert the time
133 if (mode == V_ASN1_UTCTIME)
135 lt->tm_year = (ATOI2(p) * 100) - 1900; /* cc */
136 type = V_ASN1_GENERALIZEDTIME;
140 if (mode == V_ASN1_GENERALIZEDTIME)
142 type = V_ASN1_UTCTIME;
144 lt->tm_year += ATOI2(p); /* yy */
145 if (type == V_ASN1_UTCTIME) {
146 if (lt->tm_year < 50)
149 lt->tm_mon = ATOI2(p) - 1; /* mm */
150 if (lt->tm_mon < 0 || lt->tm_mon > 11)
152 lt->tm_mday = ATOI2(p); /* dd */
153 if (lt->tm_mday < 1 || lt->tm_mday > 31)
155 lt->tm_hour = ATOI2(p); /* HH */
156 if (lt->tm_hour < 0 || lt->tm_hour > 23)
158 lt->tm_min = ATOI2(p); /* MM */
159 if (lt->tm_min < 0 || lt->tm_min > 59)
161 lt->tm_sec = ATOI2(p); /* SS */
162 /* Leap second 60 is not accepted. Reconsider later? */
163 if (lt->tm_sec < 0 || lt->tm_sec > 59)