summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/a_time_posix.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/a_time_posix.c')
-rw-r--r--src/lib/libcrypto/asn1/a_time_posix.c106
1 files changed, 56 insertions, 50 deletions
diff --git a/src/lib/libcrypto/asn1/a_time_posix.c b/src/lib/libcrypto/asn1/a_time_posix.c
index 5d10d21d3c..d4439b4701 100644
--- a/src/lib/libcrypto/asn1/a_time_posix.c
+++ b/src/lib/libcrypto/asn1/a_time_posix.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_time_posix.c,v 1.4 2023/11/13 12:46:07 beck Exp $ */ 1/* $OpenBSD: a_time_posix.c,v 1.5 2024/02/18 16:28:38 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2022, Google Inc. 3 * Copyright (c) 2022, Google Inc.
4 * Copyright (c) 2022, Bob Beck <beck@obtuse.com> 4 * Copyright (c) 2022, Bob Beck <beck@obtuse.com>
@@ -23,10 +23,14 @@
23 23
24#include <inttypes.h> 24#include <inttypes.h>
25#include <limits.h> 25#include <limits.h>
26#include <stdint.h>
26#include <string.h> 27#include <string.h>
27#include <time.h> 28#include <time.h>
28 29
29#include <openssl/asn1.h> 30#include <openssl/asn1.h>
31#include <openssl/posix_time.h>
32
33#include "crypto_internal.h"
30 34
31#define SECS_PER_HOUR (int64_t)(60 * 60) 35#define SECS_PER_HOUR (int64_t)(60 * 60)
32#define SECS_PER_DAY (int64_t)(24 * SECS_PER_HOUR) 36#define SECS_PER_DAY (int64_t)(24 * SECS_PER_HOUR)
@@ -36,7 +40,7 @@
36 * to 9999? 40 * to 9999?
37 */ 41 */
38static int 42static int
39is_valid_date(int year, int month, int day) 43is_valid_date(int64_t year, int64_t month, int64_t day)
40{ 44{
41 int days_in_month; 45 int days_in_month;
42 if (day < 1 || month < 1 || year < 0 || year > 9999) 46 if (day < 1 || month < 1 || year < 0 || year > 9999)
@@ -80,13 +84,16 @@ is_valid_time(int hours, int minutes, int seconds)
80 minutes <= 59 && seconds <= 59; 84 minutes <= 59 && seconds <= 59;
81} 85}
82 86
87/* 0000-01-01 00:00:00 UTC */
88#define MIN_POSIX_TIME INT64_C(-62167219200)
89/* 9999-12-31 23:59:59 UTC */
90#define MAX_POSIX_TIME INT64_C(253402300799)
91
83/* Is a int64 time representing a time within our expected range? */ 92/* Is a int64 time representing a time within our expected range? */
84static int 93static int
85is_valid_epoch_time(int64_t time) 94is_valid_posix_time(int64_t time)
86{ 95{
87 /* 0000-01-01 00:00:00 UTC to 9999-12-31 23:59:59 UTC */ 96 return MIN_POSIX_TIME <= time && time <= MAX_POSIX_TIME;
88 return (int64_t)-62167219200LL <= time &&
89 time <= (int64_t)253402300799LL;
90} 97}
91 98
92/* 99/*
@@ -95,8 +102,8 @@ is_valid_epoch_time(int64_t time)
95 * (Public Domain) 102 * (Public Domain)
96 */ 103 */
97static int 104static int
98posix_time_from_utc(int year, int month, int day, int hours, int minutes, 105posix_time_from_utc(int64_t year, int64_t month, int64_t day, int64_t hours,
99 int seconds, int64_t *out_time) 106 int64_t minutes, int64_t seconds, int64_t *out_time)
100{ 107{
101 int64_t era, year_of_era, day_of_year, day_of_era, posix_days; 108 int64_t era, year_of_era, day_of_year, day_of_era, posix_days;
102 109
@@ -132,7 +139,7 @@ utc_from_posix_time(int64_t time, int *out_year, int *out_month, int *out_day,
132 int64_t days, leftover_seconds, era, day_of_era, year_of_era, 139 int64_t days, leftover_seconds, era, day_of_era, year_of_era,
133 day_of_year, month_of_year; 140 day_of_year, month_of_year;
134 141
135 if (!is_valid_epoch_time(time)) 142 if (!is_valid_posix_time(time))
136 return 0; 143 return 0;
137 144
138 days = time / SECS_PER_DAY; 145 days = time / SECS_PER_DAY;
@@ -167,40 +174,41 @@ utc_from_posix_time(int64_t time, int *out_year, int *out_month, int *out_day,
167 return 1; 174 return 1;
168} 175}
169 176
170static int 177int
171asn1_time_tm_to_posix(const struct tm *tm, int64_t *out) 178OPENSSL_tm_to_posix(const struct tm *tm, int64_t *out)
172{ 179{
173 /* Ensure additions below do not overflow */ 180 return posix_time_from_utc(tm->tm_year + (int64_t)1900,
174 if (tm->tm_year > 9999) 181 tm->tm_mon + (int64_t)1, tm->tm_mday, tm->tm_hour, tm->tm_min,
175 return 0; 182 tm->tm_sec, out);
176 if (tm->tm_mon > 12)
177 return 0;
178
179 return posix_time_from_utc(tm->tm_year + 1900, tm->tm_mon + 1,
180 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, out);
181} 183}
184LCRYPTO_ALIAS(OPENSSL_tm_to_posix);
182 185
183static int 186int
184asn1_time_posix_to_tm(int64_t time, struct tm *out_tm) 187OPENSSL_posix_to_tm(int64_t time, struct tm *out_tm)
185{ 188{
186 memset(out_tm, 0, sizeof(struct tm)); 189 struct tm tmp_tm = {0};
187 if (!utc_from_posix_time(time, &out_tm->tm_year, &out_tm->tm_mon, 190
188 &out_tm->tm_mday, &out_tm->tm_hour, &out_tm->tm_min, 191 memset(out_tm, 0, sizeof(*out_tm));
189 &out_tm->tm_sec)) 192
193 if (!utc_from_posix_time(time, &tmp_tm.tm_year, &tmp_tm.tm_mon,
194 &tmp_tm.tm_mday, &tmp_tm.tm_hour, &tmp_tm.tm_min, &tmp_tm.tm_sec))
190 return 0; 195 return 0;
191 196
192 out_tm->tm_year -= 1900; 197 tmp_tm.tm_year -= 1900;
193 out_tm->tm_mon -= 1; 198 tmp_tm.tm_mon -= 1;
199
200 *out_tm = tmp_tm;
194 201
195 return 1; 202 return 1;
196} 203}
204LCRYPTO_ALIAS(OPENSSL_posix_to_tm);
197 205
198int 206int
199asn1_time_tm_to_time_t(const struct tm *tm, time_t *out) 207asn1_time_tm_to_time_t(const struct tm *tm, time_t *out)
200{ 208{
201 int64_t posix_time; 209 int64_t posix_time;
202 210
203 if (!asn1_time_tm_to_posix(tm, &posix_time)) 211 if (!OPENSSL_tm_to_posix(tm, &posix_time))
204 return 0; 212 return 0;
205 213
206#ifdef SMALL_TIME_T 214#ifdef SMALL_TIME_T
@@ -219,7 +227,7 @@ asn1_time_time_t_to_tm(const time_t *time, struct tm *out_tm)
219{ 227{
220 int64_t posix_time = *time; 228 int64_t posix_time = *time;
221 229
222 return asn1_time_posix_to_tm(posix_time, out_tm); 230 return OPENSSL_posix_to_tm(posix_time, out_tm);
223} 231}
224 232
225int 233int
@@ -236,28 +244,29 @@ OPENSSL_gmtime(const time_t *time, struct tm *out_tm) {
236} 244}
237LCRYPTO_ALIAS(OPENSSL_gmtime); 245LCRYPTO_ALIAS(OPENSSL_gmtime);
238 246
247/* Public API in OpenSSL. BoringSSL uses int64_t instead of long. */
239int 248int
240OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec) 249OPENSSL_gmtime_adj(struct tm *tm, int offset_day, int64_t offset_sec)
241{ 250{
242 int64_t posix_time; 251 int64_t posix_time;
243 252
244 /* Ensure additions below do not overflow */ 253 if (!OPENSSL_tm_to_posix(tm, &posix_time))
245 if (tm->tm_year > 9999)
246 return 0;
247 if (tm->tm_mon > 12)
248 return 0; 254 return 0;
249 255
250 if (!posix_time_from_utc(tm->tm_year + 1900, tm->tm_mon + 1, 256 CTASSERT(INT_MAX <= INT64_MAX / SECS_PER_DAY);
251 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, &posix_time)) 257 CTASSERT(MAX_POSIX_TIME <= INT64_MAX - INT_MAX * SECS_PER_DAY);
252 return 0; 258 CTASSERT(MIN_POSIX_TIME >= INT64_MIN - INT_MIN * SECS_PER_DAY);
259
260 posix_time += offset_day * SECS_PER_DAY;
253 261
254 if (!utc_from_posix_time(posix_time + off_day * SECS_PER_DAY + 262 if (posix_time > 0 && offset_sec > INT64_MAX - posix_time)
255 offset_sec, &tm->tm_year, &tm->tm_mon, &tm->tm_mday, &tm->tm_hour,
256 &tm->tm_min, &tm->tm_sec))
257 return 0; 263 return 0;
264 if (posix_time < 0 && offset_sec < INT64_MIN - posix_time)
265 return 0;
266 posix_time += offset_sec;
258 267
259 tm->tm_year -= 1900; 268 if (!OPENSSL_posix_to_tm(posix_time, tm))
260 tm->tm_mon -= 1; 269 return 0;
261 270
262 return 1; 271 return 1;
263} 272}
@@ -268,20 +277,17 @@ OPENSSL_gmtime_diff(int *out_days, int *out_secs, const struct tm *from,
268{ 277{
269 int64_t time_to, time_from, timediff, daydiff; 278 int64_t time_to, time_from, timediff, daydiff;
270 279
271 if (!posix_time_from_utc(to->tm_year + 1900, to->tm_mon + 1, 280 if (!OPENSSL_tm_to_posix(to, &time_to) ||
272 to->tm_mday, to->tm_hour, to->tm_min, to->tm_sec, &time_to)) 281 !OPENSSL_tm_to_posix(from, &time_from))
273 return 0; 282 return 0;
274 283
275 if (!posix_time_from_utc(from->tm_year + 1900, from->tm_mon + 1, 284 /* Times are in range, so these calculations cannot overflow. */
276 from->tm_mday, from->tm_hour, from->tm_min, 285 CTASSERT(SECS_PER_DAY <= INT_MAX);
277 from->tm_sec, &time_from)) 286 CTASSERT((MAX_POSIX_TIME - MIN_POSIX_TIME) / SECS_PER_DAY <= INT_MAX);
278 return 0;
279 287
280 timediff = time_to - time_from; 288 timediff = time_to - time_from;
281 daydiff = timediff / SECS_PER_DAY; 289 daydiff = timediff / SECS_PER_DAY;
282 timediff %= SECS_PER_DAY; 290 timediff %= SECS_PER_DAY;
283 if (daydiff > INT_MAX || daydiff < INT_MIN)
284 return 0;
285 291
286 *out_secs = timediff; 292 *out_secs = timediff;
287 *out_days = daydiff; 293 *out_days = daydiff;