Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/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
264k
#define SECS_PER_HOUR (int64_t)(60 * 60)
28
145k
#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
31.1k
{
36
31.1k
    int days_in_month;
37
38
31.1k
    if (day < 1 || year < 0 || year > 9999)
39
0
        return 0;
40
31.1k
    switch (month) {
41
15.4k
    case 1:
42
16.6k
    case 3:
43
18.1k
    case 5:
44
19.5k
    case 7:
45
20.2k
    case 8:
46
21.3k
    case 10:
47
23.1k
    case 12:
48
23.1k
        days_in_month = 31;
49
23.1k
        break;
50
2.02k
    case 4:
51
2.99k
    case 6:
52
3.26k
    case 9:
53
4.86k
    case 11:
54
4.86k
        days_in_month = 30;
55
4.86k
        break;
56
3.12k
    case 2:
57
3.12k
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
58
2.21k
            days_in_month = 29;
59
909
        else
60
909
            days_in_month = 28;
61
3.12k
        break;
62
0
    default:
63
0
        return 0;
64
31.1k
    }
65
31.1k
    return day <= days_in_month;
66
31.1k
}
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
31.1k
{
74
31.1k
    return hours >= 0 && minutes >= 0 && seconds >= 0 && hours <= 23 && minutes <= 59 && seconds <= 59;
75
31.1k
}
76
77
/* 0000-01-01 00:00:00 UTC */
78
49.5k
#define MIN_POSIX_TIME INT64_C(-62167219200)
79
/* 9999-12-31 23:59:59 UTC */
80
46.6k
#define MAX_POSIX_TIME INT64_C(253402300799)
81
82
/* Is a int64 time representing a time within our expected range? */
83
static int is_valid_posix_time(int64_t time)
84
49.5k
{
85
49.5k
    return MIN_POSIX_TIME <= time && time <= MAX_POSIX_TIME;
86
49.5k
}
87
88
/*
89
 * Inspired by algorithms presented in
90
 * https://howardhinnant.github.io/date_algorithms.html
91
 * (Public Domain)
92
 */
93
static int posix_time_from_utc(int64_t year, int64_t month, int64_t day,
94
    int64_t hours, int64_t minutes, int64_t seconds,
95
    int64_t *out_time)
96
31.1k
{
97
31.1k
    int64_t era, year_of_era, day_of_year, day_of_era, posix_days;
98
99
31.1k
    if (!is_valid_date(year, month, day) || !is_valid_time(hours, minutes, seconds))
100
0
        return 0;
101
31.1k
    if (month <= 2)
102
18.5k
        year--; /* Start years on Mar 1, so leap days end a year. */
103
104
    /* At this point year will be in the range -1 and 9999. */
105
31.1k
    era = (year >= 0 ? year : year - 399) / 400;
106
31.1k
    year_of_era = year - era * 400;
107
31.1k
    day_of_year = (153 * (month > 2 ? month - 3 : month + 9) + 2) / 5 + day - 1;
108
31.1k
    day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
109
31.1k
    posix_days = era * 146097 + day_of_era - 719468;
110
31.1k
    *out_time = posix_days * SECS_PER_DAY + hours * SECS_PER_HOUR + minutes * 60 + seconds;
111
112
31.1k
    return 1;
113
31.1k
}
114
115
/*
116
 * Inspired by algorithms presented in
117
 * https://howardhinnant.github.io/date_algorithms.html
118
 * (Public Domain)
119
 */
120
static int utc_from_posix_time(int64_t time, int *out_year, int *out_month,
121
    int *out_day, int *out_hours, int *out_minutes,
122
    int *out_seconds)
123
49.5k
{
124
49.5k
    int64_t days, leftover_seconds, era, day_of_era, year_of_era, day_of_year;
125
49.5k
    int64_t month_of_year;
126
127
49.5k
    if (!is_valid_posix_time(time))
128
5.54k
        return 0;
129
130
44.0k
    days = time / SECS_PER_DAY;
131
44.0k
    leftover_seconds = time % SECS_PER_DAY;
132
44.0k
    if (leftover_seconds < 0) {
133
3.74k
        days--;
134
3.74k
        leftover_seconds += SECS_PER_DAY;
135
3.74k
    }
136
44.0k
    days += 719468; /*  Shift to starting epoch of Mar 1 0000. */
137
138
    /* At this point, days will be in the range -61 and 3652364. */
139
44.0k
    era = (days > 0 ? days : days - 146096) / 146097;
140
44.0k
    day_of_era = days - era * 146097;
141
44.0k
    year_of_era = (day_of_era - day_of_era / 1460 + day_of_era / 36524 - day_of_era / 146096) / 365;
142
44.0k
    *out_year = (int)(year_of_era + era * 400); /* Year starts on Mar 1 */
143
44.0k
    day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
144
44.0k
    month_of_year = (5 * day_of_year + 2) / 153;
145
44.0k
    *out_month = (int)(month_of_year < 10 ? month_of_year + 3 : month_of_year - 9);
146
44.0k
    if (*out_month <= 2)
147
21.1k
        (*out_year)++; /* Adjust year back to Jan 1 start of year. */
148
149
44.0k
    *out_day = (int)(day_of_year - (153 * month_of_year + 2) / 5 + 1);
150
44.0k
    *out_hours = (int)leftover_seconds / SECS_PER_HOUR;
151
44.0k
    leftover_seconds %= SECS_PER_HOUR;
152
44.0k
    *out_minutes = (int)leftover_seconds / 60;
153
44.0k
    *out_seconds = (int)leftover_seconds % 60;
154
155
44.0k
    return 1;
156
49.5k
}
157
158
int OPENSSL_tm_to_posix(const struct tm *tm, int64_t *out)
159
31.1k
{
160
31.1k
    return posix_time_from_utc(tm->tm_year + (int64_t)1900,
161
31.1k
        tm->tm_mon + (int64_t)1, tm->tm_mday,
162
31.1k
        tm->tm_hour, tm->tm_min, tm->tm_sec, out);
163
31.1k
}
164
165
int OPENSSL_posix_to_tm(int64_t time, struct tm *out_tm)
166
49.5k
{
167
49.5k
    struct tm tmp_tm = { 0 };
168
169
49.5k
    memset(out_tm, 0, sizeof(*out_tm));
170
171
49.5k
    if (!utc_from_posix_time(time, &tmp_tm.tm_year, &tmp_tm.tm_mon,
172
49.5k
            &tmp_tm.tm_mday, &tmp_tm.tm_hour,
173
49.5k
            &tmp_tm.tm_min, &tmp_tm.tm_sec))
174
5.54k
        return 0;
175
176
44.0k
    tmp_tm.tm_year -= 1900;
177
44.0k
    tmp_tm.tm_mon -= 1;
178
179
44.0k
    *out_tm = tmp_tm;
180
181
44.0k
    return 1;
182
49.5k
}
183
184
int ossl_asn1_time_tm_to_time_t(const struct tm *tm, time_t *out)
185
0
{
186
0
    int64_t posix_time;
187
0
    time_t test_t = -1;
188
0
    int bad_idea_bears = (test_t > 0); /* time_t is unsigned */
189
190
0
    if (!OPENSSL_tm_to_posix(tm, &posix_time))
191
0
        return 0;
192
193
0
    if (sizeof(time_t) == sizeof(int32_t)
194
0
        && ((!bad_idea_bears && (posix_time > INT32_MAX || posix_time < INT32_MIN))
195
0
            || (bad_idea_bears && (posix_time > UINT32_MAX || posix_time < 0))))
196
0
        return 0;
197
198
0
    *out = posix_time;
199
0
    return 1;
200
0
}
201
202
int ossl_asn1_time_time_t_to_tm(const time_t *time, struct tm *out_tm)
203
27.1k
{
204
27.1k
    int64_t posix_time = *time;
205
206
27.1k
    return OPENSSL_posix_to_tm(posix_time, out_tm);
207
27.1k
}
208
209
int OPENSSL_timegm(const struct tm *tm, time_t *out)
210
0
{
211
0
    return ossl_asn1_time_tm_to_time_t(tm, out);
212
0
}
213
214
struct tm *OPENSSL_gmtime(const time_t *time, struct tm *out_tm)
215
27.1k
{
216
27.1k
    if (!ossl_asn1_time_time_t_to_tm(time, out_tm))
217
0
        return NULL;
218
27.1k
    return out_tm;
219
27.1k
}
220
221
/* LibreSSL and BoringSSL use int64_t instead of long. */
222
int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, long offset_sec)
223
22.4k
{
224
22.4k
    int64_t posix_time;
225
226
22.4k
    if (!OPENSSL_tm_to_posix(tm, &posix_time))
227
0
        return 0;
228
229
22.4k
    OPENSSL_assert(INT_MAX <= INT64_MAX / SECS_PER_DAY);
230
22.4k
    OPENSSL_assert(MAX_POSIX_TIME <= INT64_MAX - INT_MAX * SECS_PER_DAY);
231
22.4k
    OPENSSL_assert(MIN_POSIX_TIME >= INT64_MIN - INT_MIN * SECS_PER_DAY);
232
233
22.4k
    posix_time += offset_day * SECS_PER_DAY;
234
235
22.4k
    if (posix_time > 0 && offset_sec > INT64_MAX - posix_time)
236
0
        return 0;
237
22.4k
    if (posix_time < 0 && offset_sec < INT64_MIN - posix_time)
238
0
        return 0;
239
22.4k
    posix_time += offset_sec;
240
241
22.4k
    if (!OPENSSL_posix_to_tm(posix_time, tm))
242
5.54k
        return 0;
243
244
16.9k
    return 1;
245
22.4k
}
246
247
int OPENSSL_gmtime_diff(int *out_days, int *out_secs, const struct tm *from,
248
    const struct tm *to)
249
53
{
250
53
    int64_t time_to, time_from, timediff, daydiff;
251
252
53
    if (!OPENSSL_tm_to_posix(to, &time_to) || !OPENSSL_tm_to_posix(from, &time_from))
253
0
        return 0;
254
255
    /* Times are in range, so these calculations cannot overflow. */
256
53
    OPENSSL_assert(SECS_PER_DAY <= INT_MAX);
257
53
    OPENSSL_assert((MAX_POSIX_TIME - MIN_POSIX_TIME) / SECS_PER_DAY <= INT_MAX);
258
259
53
    timediff = time_to - time_from;
260
53
    daydiff = timediff / SECS_PER_DAY;
261
53
    timediff %= SECS_PER_DAY;
262
263
53
    if (out_secs != NULL)
264
53
        *out_secs = (int)timediff;
265
53
    if (out_days != NULL)
266
53
        *out_days = (int)daydiff;
267
268
53
    return 1;
269
53
}
270
271
int ossl_posix_to_asn1_time(int64_t posix_time, ASN1_TIME **out_time)
272
0
{
273
0
    struct tm ts;
274
275
0
    if (!OPENSSL_posix_to_tm(posix_time, &ts))
276
0
        return 0;
277
278
0
    if ((*out_time = ossl_asn1_time_from_tm(*out_time, &ts, V_ASN1_UNDEF)) == NULL)
279
0
        return 0;
280
281
0
    return 1;
282
0
}