Blob


1 /* $OpenBSD: a_time_tm.c,v 1.15 2018/04/25 11:48:21 tb Exp $ */
2 /*
3 * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
4 *
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.
8 *
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.
16 */
18 #include "config.h"
20 #include <ctype.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <time.h>
26 #define GENTIME_LENGTH 15
27 #define UTCTIME_LENGTH 13
29 #define V_ASN1_UTCTIME 23
30 #define V_ASN1_GENERALIZEDTIME 24
32 #ifndef HAVE_ASN1_TIME_TM_CMP
33 int
34 ASN1_time_tm_cmp(struct tm *tm1, struct tm *tm2)
35 {
36 if (tm1->tm_year < tm2->tm_year)
37 return (-1);
38 if (tm1->tm_year > tm2->tm_year)
39 return (1);
40 if (tm1->tm_mon < tm2->tm_mon)
41 return (-1);
42 if (tm1->tm_mon > tm2->tm_mon)
43 return (1);
44 if (tm1->tm_mday < tm2->tm_mday)
45 return (-1);
46 if (tm1->tm_mday > tm2->tm_mday)
47 return (1);
48 if (tm1->tm_hour < tm2->tm_hour)
49 return (-1);
50 if (tm1->tm_hour > tm2->tm_hour)
51 return (1);
52 if (tm1->tm_min < tm2->tm_min)
53 return (-1);
54 if (tm1->tm_min > tm2->tm_min)
55 return (1);
56 if (tm1->tm_sec < tm2->tm_sec)
57 return (-1);
58 if (tm1->tm_sec > tm2->tm_sec)
59 return (1);
60 return 0;
61 }
62 #endif
64 #ifndef HAVE_ASN1_TIME_TM_CLAMP_NOTAFTER
65 int
66 ASN1_time_tm_clamp_notafter(struct tm *tm)
67 {
68 #ifdef SMALL_TIME_T
69 struct tm broken_os_epoch_tm;
70 time_t broken_os_epoch_time = INT_MAX;
72 if (gmtime_r(&broken_os_epoch_time, &broken_os_epoch_tm) == NULL)
73 return 0;
75 if (ASN1_time_tm_cmp(tm, &broken_os_epoch_tm) == 1)
76 memcpy(tm, &broken_os_epoch_tm, sizeof(*tm));
77 #endif
78 return 1;
79 }
80 #endif
82 /*
83 * Parse an RFC 5280 format ASN.1 time string.
84 *
85 * mode must be:
86 * 0 if we expect to parse a time as specified in RFC 5280 for an X509 object.
87 * V_ASN1_UTCTIME if we wish to parse an RFC5280 format UTC time.
88 * V_ASN1_GENERALIZEDTIME if we wish to parse an RFC5280 format Generalized time.
89 *
90 * Returns:
91 * -1 if the string was invalid.
92 * V_ASN1_UTCTIME if the string validated as a UTC time string.
93 * V_ASN1_GENERALIZEDTIME if the string validated as a Generalized time string.
94 *
95 * Fills in *tm with the corresponding time if tm is non NULL.
96 */
97 #ifndef HAVE_ASN1_TIME_PARSE
98 #define ATOI2(ar) ((ar) += 2, ((ar)[-2] - '0') * 10 + ((ar)[-1] - '0'))
99 int
100 ASN1_time_parse(const char *bytes, size_t len, struct tm *tm, int mode)
102 size_t i;
103 int type = 0;
104 struct tm ltm;
105 struct tm *lt;
106 const char *p;
108 if (bytes == NULL)
109 return (-1);
111 /* Constrain to valid lengths. */
112 if (len != UTCTIME_LENGTH && len != GENTIME_LENGTH)
113 return (-1);
115 lt = tm;
116 if (lt == NULL) {
117 memset(&ltm, 0, sizeof(ltm));
118 lt = &ltm;
121 /* Timezone is required and must be GMT (Zulu). */
122 if (bytes[len - 1] != 'Z')
123 return (-1);
125 /* Make sure everything else is digits. */
126 for (i = 0; i < len - 1; i++) {
127 if (isdigit((unsigned char)bytes[i]))
128 continue;
129 return (-1);
132 /*
133 * Validate and convert the time
134 */
135 p = bytes;
136 switch (len) {
137 case GENTIME_LENGTH:
138 if (mode == V_ASN1_UTCTIME)
139 return (-1);
140 lt->tm_year = (ATOI2(p) * 100) - 1900; /* cc */
141 type = V_ASN1_GENERALIZEDTIME;
142 /* FALLTHROUGH */
143 case UTCTIME_LENGTH:
144 if (type == 0) {
145 if (mode == V_ASN1_GENERALIZEDTIME)
146 return (-1);
147 type = V_ASN1_UTCTIME;
149 lt->tm_year += ATOI2(p); /* yy */
150 if (type == V_ASN1_UTCTIME) {
151 if (lt->tm_year < 50)
152 lt->tm_year += 100;
154 lt->tm_mon = ATOI2(p) - 1; /* mm */
155 if (lt->tm_mon < 0 || lt->tm_mon > 11)
156 return (-1);
157 lt->tm_mday = ATOI2(p); /* dd */
158 if (lt->tm_mday < 1 || lt->tm_mday > 31)
159 return (-1);
160 lt->tm_hour = ATOI2(p); /* HH */
161 if (lt->tm_hour < 0 || lt->tm_hour > 23)
162 return (-1);
163 lt->tm_min = ATOI2(p); /* MM */
164 if (lt->tm_min < 0 || lt->tm_min > 59)
165 return (-1);
166 lt->tm_sec = ATOI2(p); /* SS */
167 /* Leap second 60 is not accepted. Reconsider later? */
168 if (lt->tm_sec < 0 || lt->tm_sec > 59)
169 return (-1);
170 break;
171 default:
172 return (-1);
175 return (type);
177 #endif