Blame


1 35d04818 2023-09-15 stsp /* $OpenBSD: a_time_tm.c,v 1.15 2018/04/25 11:48:21 tb Exp $ */
2 35d04818 2023-09-15 stsp /*
3 35d04818 2023-09-15 stsp * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
4 35d04818 2023-09-15 stsp *
5 35d04818 2023-09-15 stsp * Permission to use, copy, modify, and distribute this software for any
6 35d04818 2023-09-15 stsp * purpose with or without fee is hereby granted, provided that the above
7 35d04818 2023-09-15 stsp * copyright notice and this permission notice appear in all copies.
8 35d04818 2023-09-15 stsp *
9 35d04818 2023-09-15 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 35d04818 2023-09-15 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 35d04818 2023-09-15 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 35d04818 2023-09-15 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 35d04818 2023-09-15 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 35d04818 2023-09-15 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 35d04818 2023-09-15 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 35d04818 2023-09-15 stsp */
17 35d04818 2023-09-15 stsp
18 35d04818 2023-09-15 stsp #include "includes.h"
19 35d04818 2023-09-15 stsp
20 35d04818 2023-09-15 stsp #include <ctype.h>
21 35d04818 2023-09-15 stsp #include <limits.h>
22 35d04818 2023-09-15 stsp #include <stdio.h>
23 35d04818 2023-09-15 stsp #include <string.h>
24 35d04818 2023-09-15 stsp #include <time.h>
25 35d04818 2023-09-15 stsp
26 35d04818 2023-09-15 stsp #define GENTIME_LENGTH 15
27 35d04818 2023-09-15 stsp #define UTCTIME_LENGTH 13
28 35d04818 2023-09-15 stsp
29 35d04818 2023-09-15 stsp #define V_ASN1_UTCTIME 23
30 35d04818 2023-09-15 stsp #define V_ASN1_GENERALIZEDTIME 24
31 35d04818 2023-09-15 stsp
32 35d04818 2023-09-15 stsp int
33 35d04818 2023-09-15 stsp ASN1_time_tm_cmp(struct tm *tm1, struct tm *tm2)
34 35d04818 2023-09-15 stsp {
35 35d04818 2023-09-15 stsp if (tm1->tm_year < tm2->tm_year)
36 35d04818 2023-09-15 stsp return (-1);
37 35d04818 2023-09-15 stsp if (tm1->tm_year > tm2->tm_year)
38 35d04818 2023-09-15 stsp return (1);
39 35d04818 2023-09-15 stsp if (tm1->tm_mon < tm2->tm_mon)
40 35d04818 2023-09-15 stsp return (-1);
41 35d04818 2023-09-15 stsp if (tm1->tm_mon > tm2->tm_mon)
42 35d04818 2023-09-15 stsp return (1);
43 35d04818 2023-09-15 stsp if (tm1->tm_mday < tm2->tm_mday)
44 35d04818 2023-09-15 stsp return (-1);
45 35d04818 2023-09-15 stsp if (tm1->tm_mday > tm2->tm_mday)
46 35d04818 2023-09-15 stsp return (1);
47 35d04818 2023-09-15 stsp if (tm1->tm_hour < tm2->tm_hour)
48 35d04818 2023-09-15 stsp return (-1);
49 35d04818 2023-09-15 stsp if (tm1->tm_hour > tm2->tm_hour)
50 35d04818 2023-09-15 stsp return (1);
51 35d04818 2023-09-15 stsp if (tm1->tm_min < tm2->tm_min)
52 35d04818 2023-09-15 stsp return (-1);
53 35d04818 2023-09-15 stsp if (tm1->tm_min > tm2->tm_min)
54 35d04818 2023-09-15 stsp return (1);
55 35d04818 2023-09-15 stsp if (tm1->tm_sec < tm2->tm_sec)
56 35d04818 2023-09-15 stsp return (-1);
57 35d04818 2023-09-15 stsp if (tm1->tm_sec > tm2->tm_sec)
58 35d04818 2023-09-15 stsp return (1);
59 35d04818 2023-09-15 stsp return 0;
60 35d04818 2023-09-15 stsp }
61 35d04818 2023-09-15 stsp
62 35d04818 2023-09-15 stsp int
63 35d04818 2023-09-15 stsp ASN1_time_tm_clamp_notafter(struct tm *tm)
64 35d04818 2023-09-15 stsp {
65 35d04818 2023-09-15 stsp #ifdef SMALL_TIME_T
66 35d04818 2023-09-15 stsp struct tm broken_os_epoch_tm;
67 35d04818 2023-09-15 stsp time_t broken_os_epoch_time = INT_MAX;
68 35d04818 2023-09-15 stsp
69 35d04818 2023-09-15 stsp if (gmtime_r(&broken_os_epoch_time, &broken_os_epoch_tm) == NULL)
70 35d04818 2023-09-15 stsp return 0;
71 35d04818 2023-09-15 stsp
72 35d04818 2023-09-15 stsp if (ASN1_time_tm_cmp(tm, &broken_os_epoch_tm) == 1)
73 35d04818 2023-09-15 stsp memcpy(tm, &broken_os_epoch_tm, sizeof(*tm));
74 35d04818 2023-09-15 stsp #endif
75 35d04818 2023-09-15 stsp return 1;
76 35d04818 2023-09-15 stsp }
77 35d04818 2023-09-15 stsp
78 35d04818 2023-09-15 stsp /*
79 35d04818 2023-09-15 stsp * Parse an RFC 5280 format ASN.1 time string.
80 35d04818 2023-09-15 stsp *
81 35d04818 2023-09-15 stsp * mode must be:
82 35d04818 2023-09-15 stsp * 0 if we expect to parse a time as specified in RFC 5280 for an X509 object.
83 35d04818 2023-09-15 stsp * V_ASN1_UTCTIME if we wish to parse an RFC5280 format UTC time.
84 35d04818 2023-09-15 stsp * V_ASN1_GENERALIZEDTIME if we wish to parse an RFC5280 format Generalized time.
85 35d04818 2023-09-15 stsp *
86 35d04818 2023-09-15 stsp * Returns:
87 35d04818 2023-09-15 stsp * -1 if the string was invalid.
88 35d04818 2023-09-15 stsp * V_ASN1_UTCTIME if the string validated as a UTC time string.
89 35d04818 2023-09-15 stsp * V_ASN1_GENERALIZEDTIME if the string validated as a Generalized time string.
90 35d04818 2023-09-15 stsp *
91 35d04818 2023-09-15 stsp * Fills in *tm with the corresponding time if tm is non NULL.
92 35d04818 2023-09-15 stsp */
93 35d04818 2023-09-15 stsp #define ATOI2(ar) ((ar) += 2, ((ar)[-2] - '0') * 10 + ((ar)[-1] - '0'))
94 35d04818 2023-09-15 stsp int
95 35d04818 2023-09-15 stsp ASN1_time_parse(const char *bytes, size_t len, struct tm *tm, int mode)
96 35d04818 2023-09-15 stsp {
97 35d04818 2023-09-15 stsp size_t i;
98 35d04818 2023-09-15 stsp int type = 0;
99 35d04818 2023-09-15 stsp struct tm ltm;
100 35d04818 2023-09-15 stsp struct tm *lt;
101 35d04818 2023-09-15 stsp const char *p;
102 35d04818 2023-09-15 stsp
103 35d04818 2023-09-15 stsp if (bytes == NULL)
104 35d04818 2023-09-15 stsp return (-1);
105 35d04818 2023-09-15 stsp
106 35d04818 2023-09-15 stsp /* Constrain to valid lengths. */
107 35d04818 2023-09-15 stsp if (len != UTCTIME_LENGTH && len != GENTIME_LENGTH)
108 35d04818 2023-09-15 stsp return (-1);
109 35d04818 2023-09-15 stsp
110 35d04818 2023-09-15 stsp lt = tm;
111 35d04818 2023-09-15 stsp if (lt == NULL) {
112 35d04818 2023-09-15 stsp memset(&ltm, 0, sizeof(ltm));
113 35d04818 2023-09-15 stsp lt = &ltm;
114 35d04818 2023-09-15 stsp }
115 35d04818 2023-09-15 stsp
116 35d04818 2023-09-15 stsp /* Timezone is required and must be GMT (Zulu). */
117 35d04818 2023-09-15 stsp if (bytes[len - 1] != 'Z')
118 35d04818 2023-09-15 stsp return (-1);
119 35d04818 2023-09-15 stsp
120 35d04818 2023-09-15 stsp /* Make sure everything else is digits. */
121 35d04818 2023-09-15 stsp for (i = 0; i < len - 1; i++) {
122 35d04818 2023-09-15 stsp if (isdigit((unsigned char)bytes[i]))
123 35d04818 2023-09-15 stsp continue;
124 35d04818 2023-09-15 stsp return (-1);
125 35d04818 2023-09-15 stsp }
126 35d04818 2023-09-15 stsp
127 35d04818 2023-09-15 stsp /*
128 35d04818 2023-09-15 stsp * Validate and convert the time
129 35d04818 2023-09-15 stsp */
130 35d04818 2023-09-15 stsp p = bytes;
131 35d04818 2023-09-15 stsp switch (len) {
132 35d04818 2023-09-15 stsp case GENTIME_LENGTH:
133 35d04818 2023-09-15 stsp if (mode == V_ASN1_UTCTIME)
134 35d04818 2023-09-15 stsp return (-1);
135 35d04818 2023-09-15 stsp lt->tm_year = (ATOI2(p) * 100) - 1900; /* cc */
136 35d04818 2023-09-15 stsp type = V_ASN1_GENERALIZEDTIME;
137 35d04818 2023-09-15 stsp /* FALLTHROUGH */
138 35d04818 2023-09-15 stsp case UTCTIME_LENGTH:
139 35d04818 2023-09-15 stsp if (type == 0) {
140 35d04818 2023-09-15 stsp if (mode == V_ASN1_GENERALIZEDTIME)
141 35d04818 2023-09-15 stsp return (-1);
142 35d04818 2023-09-15 stsp type = V_ASN1_UTCTIME;
143 35d04818 2023-09-15 stsp }
144 35d04818 2023-09-15 stsp lt->tm_year += ATOI2(p); /* yy */
145 35d04818 2023-09-15 stsp if (type == V_ASN1_UTCTIME) {
146 35d04818 2023-09-15 stsp if (lt->tm_year < 50)
147 35d04818 2023-09-15 stsp lt->tm_year += 100;
148 35d04818 2023-09-15 stsp }
149 35d04818 2023-09-15 stsp lt->tm_mon = ATOI2(p) - 1; /* mm */
150 35d04818 2023-09-15 stsp if (lt->tm_mon < 0 || lt->tm_mon > 11)
151 35d04818 2023-09-15 stsp return (-1);
152 35d04818 2023-09-15 stsp lt->tm_mday = ATOI2(p); /* dd */
153 35d04818 2023-09-15 stsp if (lt->tm_mday < 1 || lt->tm_mday > 31)
154 35d04818 2023-09-15 stsp return (-1);
155 35d04818 2023-09-15 stsp lt->tm_hour = ATOI2(p); /* HH */
156 35d04818 2023-09-15 stsp if (lt->tm_hour < 0 || lt->tm_hour > 23)
157 35d04818 2023-09-15 stsp return (-1);
158 35d04818 2023-09-15 stsp lt->tm_min = ATOI2(p); /* MM */
159 35d04818 2023-09-15 stsp if (lt->tm_min < 0 || lt->tm_min > 59)
160 35d04818 2023-09-15 stsp return (-1);
161 35d04818 2023-09-15 stsp lt->tm_sec = ATOI2(p); /* SS */
162 35d04818 2023-09-15 stsp /* Leap second 60 is not accepted. Reconsider later? */
163 35d04818 2023-09-15 stsp if (lt->tm_sec < 0 || lt->tm_sec > 59)
164 35d04818 2023-09-15 stsp return (-1);
165 35d04818 2023-09-15 stsp break;
166 35d04818 2023-09-15 stsp default:
167 35d04818 2023-09-15 stsp return (-1);
168 35d04818 2023-09-15 stsp }
169 35d04818 2023-09-15 stsp
170 35d04818 2023-09-15 stsp return (type);
171 35d04818 2023-09-15 stsp }