1 35d04818 2023-09-15 stsp /* $OpenBSD: a_time_tm.c,v 1.15 2018/04/25 11:48:21 tb Exp $ */
3 35d04818 2023-09-15 stsp * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
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.
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.
18 35d04818 2023-09-15 stsp #include "includes.h"
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>
26 35d04818 2023-09-15 stsp #define GENTIME_LENGTH 15
27 35d04818 2023-09-15 stsp #define UTCTIME_LENGTH 13
29 35d04818 2023-09-15 stsp #define V_ASN1_UTCTIME 23
30 35d04818 2023-09-15 stsp #define V_ASN1_GENERALIZEDTIME 24
33 35d04818 2023-09-15 stsp ASN1_time_tm_cmp(struct tm *tm1, struct tm *tm2)
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)
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)
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)
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)
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)
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)
63 35d04818 2023-09-15 stsp ASN1_time_tm_clamp_notafter(struct tm *tm)
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;
69 35d04818 2023-09-15 stsp if (gmtime_r(&broken_os_epoch_time, &broken_os_epoch_tm) == NULL)
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));
79 35d04818 2023-09-15 stsp * Parse an RFC 5280 format ASN.1 time string.
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.
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.
91 35d04818 2023-09-15 stsp * Fills in *tm with the corresponding time if tm is non NULL.
93 35d04818 2023-09-15 stsp #define ATOI2(ar) ((ar) += 2, ((ar)[-2] - '0') * 10 + ((ar)[-1] - '0'))
95 35d04818 2023-09-15 stsp ASN1_time_parse(const char *bytes, size_t len, struct tm *tm, int mode)
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;
103 35d04818 2023-09-15 stsp if (bytes == NULL)
104 35d04818 2023-09-15 stsp return (-1);
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);
111 35d04818 2023-09-15 stsp if (lt == NULL) {
112 35d04818 2023-09-15 stsp memset(<m, 0, sizeof(ltm));
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);
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]))
124 35d04818 2023-09-15 stsp return (-1);
128 35d04818 2023-09-15 stsp * Validate and convert the time
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;
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;
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);
167 35d04818 2023-09-15 stsp return (-1);
170 35d04818 2023-09-15 stsp return (type);