/src/openssl/crypto/asn1/a_time_posix.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | /* |
11 | | * Time conversion to/from POSIX time_t and struct tm, with no support |
12 | | * for time zones other than UTC |
13 | | */ |
14 | | |
15 | | #include <inttypes.h> |
16 | | #include <limits.h> |
17 | | #include <stdint.h> |
18 | | #include <string.h> |
19 | | #include <time.h> |
20 | | |
21 | | #include <openssl/asn1.h> |
22 | | #include <openssl/posix_time.h> |
23 | | |
24 | | #include <crypto/x509.h> |
25 | | #include "asn1_local.h" |
26 | | |
27 | 190k | #define SECS_PER_HOUR (int64_t)(60 * 60) |
28 | 104k | #define SECS_PER_DAY (int64_t)(24 * SECS_PER_HOUR) |
29 | | |
30 | | /* |
31 | | * Is a year/month/day combination valid, in the range from year 0000 |
32 | | * to 9999? |
33 | | */ |
34 | | static int is_valid_date(int64_t year, int64_t month, int64_t day) |
35 | 20.4k | { |
36 | 20.4k | int days_in_month; |
37 | | |
38 | 20.4k | if (day < 1 || year < 0 || year > 9999) |
39 | 0 | return 0; |
40 | 20.4k | switch (month) { |
41 | 12.7k | case 1: |
42 | 13.4k | case 3: |
43 | 13.8k | case 5: |
44 | 14.0k | case 7: |
45 | 14.1k | case 8: |
46 | 14.5k | case 10: |
47 | 15.2k | case 12: |
48 | 15.2k | days_in_month = 31; |
49 | 15.2k | break; |
50 | 1.14k | case 4: |
51 | 1.52k | case 6: |
52 | 1.64k | case 9: |
53 | 2.52k | case 11: |
54 | 2.52k | days_in_month = 30; |
55 | 2.52k | break; |
56 | 2.67k | case 2: |
57 | 2.67k | if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) |
58 | 2.15k | days_in_month = 29; |
59 | 519 | else |
60 | 519 | days_in_month = 28; |
61 | 2.67k | break; |
62 | 0 | default: |
63 | 0 | return 0; |
64 | 20.4k | } |
65 | 20.4k | return day <= days_in_month; |
66 | 20.4k | } |
67 | | |
68 | | /* |
69 | | * Is a time valid? Leap seconds of 60 are not considered valid, as |
70 | | * the POSIX time in seconds does not include them. |
71 | | */ |
72 | | static int is_valid_time(int64_t hours, int64_t minutes, int64_t seconds) |
73 | 20.4k | { |
74 | 20.4k | return hours >= 0 && minutes >= 0 && seconds >= 0 && hours <= 23 && |
75 | 20.4k | minutes <= 59 && seconds <= 59; |
76 | 20.4k | } |
77 | | |
78 | | /* 0000-01-01 00:00:00 UTC */ |
79 | 37.2k | #define MIN_POSIX_TIME INT64_C(-62167219200) |
80 | | /* 9999-12-31 23:59:59 UTC */ |
81 | 34.9k | #define MAX_POSIX_TIME INT64_C(253402300799) |
82 | | |
83 | | /* Is a int64 time representing a time within our expected range? */ |
84 | | static int is_valid_posix_time(int64_t time) |
85 | 37.2k | { |
86 | 37.2k | return MIN_POSIX_TIME <= time && time <= MAX_POSIX_TIME; |
87 | 37.2k | } |
88 | | |
89 | | /* |
90 | | * Inspired by algorithms presented in |
91 | | * https://howardhinnant.github.io/date_algorithms.html |
92 | | * (Public Domain) |
93 | | */ |
94 | | static int posix_time_from_utc(int64_t year, int64_t month, int64_t day, |
95 | | int64_t hours, int64_t minutes, int64_t seconds, |
96 | | int64_t *out_time) |
97 | 20.4k | { |
98 | 20.4k | int64_t era, year_of_era, day_of_year, day_of_era, posix_days; |
99 | | |
100 | 20.4k | if (!is_valid_date(year, month, day) || |
101 | 20.4k | !is_valid_time(hours, minutes, seconds)) |
102 | 0 | return 0; |
103 | 20.4k | if (month <= 2) |
104 | 15.3k | year--; /* Start years on Mar 1, so leap days end a year. */ |
105 | | |
106 | | /* At this point year will be in the range -1 and 9999. */ |
107 | 20.4k | era = (year >= 0 ? year : year - 399) / 400; |
108 | 20.4k | year_of_era = year - era * 400; |
109 | 20.4k | day_of_year = (153 * (month > 2 ? month - 3 : month + 9) + 2) / |
110 | 20.4k | 5 + day - 1; |
111 | 20.4k | day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / |
112 | 20.4k | 100 + day_of_year; |
113 | 20.4k | posix_days = era * 146097 + day_of_era - 719468; |
114 | 20.4k | *out_time = posix_days * SECS_PER_DAY + hours * SECS_PER_HOUR + |
115 | 20.4k | minutes * 60 + seconds; |
116 | | |
117 | 20.4k | return 1; |
118 | 20.4k | } |
119 | | |
120 | | /* |
121 | | * Inspired by algorithms presented in |
122 | | * https://howardhinnant.github.io/date_algorithms.html |
123 | | * (Public Domain) |
124 | | */ |
125 | | static int utc_from_posix_time(int64_t time, int *out_year, int *out_month, |
126 | | int *out_day, int *out_hours, int *out_minutes, |
127 | | int *out_seconds) |
128 | 37.2k | { |
129 | 37.2k | int64_t days, leftover_seconds, era, day_of_era, year_of_era, day_of_year; |
130 | 37.2k | int64_t month_of_year; |
131 | | |
132 | 37.2k | if (!is_valid_posix_time(time)) |
133 | 4.33k | return 0; |
134 | | |
135 | 32.8k | days = time / SECS_PER_DAY; |
136 | 32.8k | leftover_seconds = time % SECS_PER_DAY; |
137 | 32.8k | if (leftover_seconds < 0) { |
138 | 1.17k | days--; |
139 | 1.17k | leftover_seconds += SECS_PER_DAY; |
140 | 1.17k | } |
141 | 32.8k | days += 719468; /* Shift to starting epoch of Mar 1 0000. */ |
142 | | |
143 | | /* At this point, days will be in the range -61 and 3652364. */ |
144 | 32.8k | era = (days > 0 ? days : days - 146096) / 146097; |
145 | 32.8k | day_of_era = days - era * 146097; |
146 | 32.8k | year_of_era = (day_of_era - day_of_era / 1460 + day_of_era / 36524 - |
147 | 32.8k | day_of_era / 146096) / 365; |
148 | 32.8k | *out_year = (int) (year_of_era + era * 400); /* Year starts on Mar 1 */ |
149 | 32.8k | day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - |
150 | 32.8k | year_of_era / 100); |
151 | 32.8k | month_of_year = (5 * day_of_year + 2) / 153; |
152 | 32.8k | *out_month = (int) (month_of_year < 10 ? month_of_year + 3 : |
153 | 32.8k | month_of_year - 9); |
154 | 32.8k | if (*out_month <= 2) |
155 | 20.0k | (*out_year)++; /* Adjust year back to Jan 1 start of year. */ |
156 | | |
157 | 32.8k | *out_day = (int) (day_of_year - (153 * month_of_year + 2) / 5 + 1); |
158 | 32.8k | *out_hours = (int) leftover_seconds / SECS_PER_HOUR; |
159 | 32.8k | leftover_seconds %= SECS_PER_HOUR; |
160 | 32.8k | *out_minutes = (int) leftover_seconds / 60; |
161 | 32.8k | *out_seconds = (int) leftover_seconds % 60; |
162 | | |
163 | 32.8k | return 1; |
164 | 37.2k | } |
165 | | |
166 | | int OPENSSL_tm_to_posix(const struct tm *tm, int64_t *out) |
167 | 20.4k | { |
168 | 20.4k | return posix_time_from_utc(tm->tm_year + (int64_t)1900, |
169 | 20.4k | tm->tm_mon + (int64_t)1, tm->tm_mday, |
170 | 20.4k | tm->tm_hour, tm->tm_min, tm->tm_sec, out); |
171 | 20.4k | } |
172 | | |
173 | | int OPENSSL_posix_to_tm(int64_t time, struct tm *out_tm) |
174 | 37.2k | { |
175 | 37.2k | struct tm tmp_tm = {0}; |
176 | | |
177 | 37.2k | memset(out_tm, 0, sizeof(*out_tm)); |
178 | | |
179 | 37.2k | if (!utc_from_posix_time(time, &tmp_tm.tm_year, &tmp_tm.tm_mon, |
180 | 37.2k | &tmp_tm.tm_mday, &tmp_tm.tm_hour, |
181 | 37.2k | &tmp_tm.tm_min, &tmp_tm.tm_sec)) |
182 | 4.33k | return 0; |
183 | | |
184 | 32.8k | tmp_tm.tm_year -= 1900; |
185 | 32.8k | tmp_tm.tm_mon -= 1; |
186 | | |
187 | 32.8k | *out_tm = tmp_tm; |
188 | | |
189 | 32.8k | return 1; |
190 | 37.2k | } |
191 | | |
192 | | int ossl_asn1_time_tm_to_time_t(const struct tm *tm, time_t *out) |
193 | 0 | { |
194 | 0 | int64_t posix_time; |
195 | 0 | time_t test_t = -1; |
196 | 0 | int bad_idea_bears = (test_t > 0); /* time_t is unsigned */ |
197 | |
|
198 | 0 | if (!OPENSSL_tm_to_posix(tm, &posix_time)) |
199 | 0 | return 0; |
200 | | |
201 | 0 | if (sizeof(time_t) == sizeof(int32_t) |
202 | 0 | && ((!bad_idea_bears && (posix_time > INT32_MAX |
203 | 0 | || posix_time < INT32_MIN)) |
204 | 0 | || (bad_idea_bears && (posix_time > UINT32_MAX |
205 | 0 | || posix_time < 0)))) |
206 | 0 | return 0; |
207 | | |
208 | 0 | *out = posix_time; |
209 | 0 | return 1; |
210 | 0 | } |
211 | | |
212 | | int ossl_asn1_time_time_t_to_tm(const time_t *time, struct tm *out_tm) |
213 | 20.2k | { |
214 | 20.2k | int64_t posix_time = *time; |
215 | | |
216 | 20.2k | return OPENSSL_posix_to_tm(posix_time, out_tm); |
217 | 20.2k | } |
218 | | |
219 | | int OPENSSL_timegm(const struct tm *tm, time_t *out) |
220 | 0 | { |
221 | 0 | return ossl_asn1_time_tm_to_time_t(tm, out); |
222 | 0 | } |
223 | | |
224 | | struct tm * OPENSSL_gmtime(const time_t *time, struct tm *out_tm) |
225 | 20.2k | { |
226 | 20.2k | if (!ossl_asn1_time_time_t_to_tm(time, out_tm)) |
227 | 0 | return NULL; |
228 | 20.2k | return out_tm; |
229 | 20.2k | } |
230 | | |
231 | | /* LibreSSL and BoringSSL use int64_t instead of long. */ |
232 | | int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, long offset_sec) |
233 | 16.9k | { |
234 | 16.9k | int64_t posix_time; |
235 | | |
236 | 16.9k | if (!OPENSSL_tm_to_posix(tm, &posix_time)) |
237 | 0 | return 0; |
238 | | |
239 | 16.9k | OPENSSL_assert(INT_MAX <= INT64_MAX / SECS_PER_DAY); |
240 | 16.9k | OPENSSL_assert(MAX_POSIX_TIME <= INT64_MAX - INT_MAX * SECS_PER_DAY); |
241 | 16.9k | OPENSSL_assert(MIN_POSIX_TIME >= INT64_MIN - INT_MIN * SECS_PER_DAY); |
242 | | |
243 | 16.9k | posix_time += offset_day * SECS_PER_DAY; |
244 | | |
245 | 16.9k | if (posix_time > 0 && offset_sec > INT64_MAX - posix_time) |
246 | 0 | return 0; |
247 | 16.9k | if (posix_time < 0 && offset_sec < INT64_MIN - posix_time) |
248 | 0 | return 0; |
249 | 16.9k | posix_time += offset_sec; |
250 | | |
251 | 16.9k | if (!OPENSSL_posix_to_tm(posix_time, tm)) |
252 | 4.33k | return 0; |
253 | | |
254 | 12.6k | return 1; |
255 | 16.9k | } |
256 | | |
257 | | int OPENSSL_gmtime_diff(int *out_days, int *out_secs, const struct tm *from, |
258 | | const struct tm *to) |
259 | 33 | { |
260 | 33 | int64_t time_to, time_from, timediff, daydiff; |
261 | | |
262 | 33 | if (!OPENSSL_tm_to_posix(to, &time_to) || |
263 | 33 | !OPENSSL_tm_to_posix(from, &time_from)) |
264 | 0 | return 0; |
265 | | |
266 | | /* Times are in range, so these calculations cannot overflow. */ |
267 | 33 | OPENSSL_assert(SECS_PER_DAY <= INT_MAX); |
268 | 33 | OPENSSL_assert((MAX_POSIX_TIME - MIN_POSIX_TIME) / SECS_PER_DAY <= INT_MAX); |
269 | | |
270 | 33 | timediff = time_to - time_from; |
271 | 33 | daydiff = timediff / SECS_PER_DAY; |
272 | 33 | timediff %= SECS_PER_DAY; |
273 | | |
274 | 33 | *out_secs = (int) timediff; |
275 | 33 | *out_days = (int) daydiff; |
276 | | |
277 | 33 | return 1; |
278 | 33 | } |
279 | | |
280 | | int ossl_posix_to_asn1_time(int64_t posix_time, ASN1_TIME **out_time) |
281 | 0 | { |
282 | 0 | struct tm ts; |
283 | |
|
284 | 0 | if (!OPENSSL_posix_to_tm(posix_time, &ts)) |
285 | 0 | return 0; |
286 | | |
287 | 0 | if ((*out_time = ossl_asn1_time_from_tm(*out_time, &ts, V_ASN1_UNDEF)) == NULL) |
288 | 0 | return 0; |
289 | | |
290 | 0 | return 1; |
291 | 0 | } |