Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/asn1/a_time.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2026 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
 * This is an implementation of the ASN1 Time structure which is:
12
 *    Time ::= CHOICE {
13
 *      utcTime        UTCTime,
14
 *      generalTime    GeneralizedTime }
15
 */
16
17
#include <stdio.h>
18
#include <time.h>
19
#include "crypto/asn1.h"
20
#include "crypto/ctype.h"
21
#include "internal/cryptlib.h"
22
#include <openssl/asn1t.h>
23
#include "asn1_local.h"
24
25
7.92M
IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
26
27
IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
28
IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_TIME)
29
30
static int is_utc(const int year)
31
11.3k
{
32
11.3k
    if (50 <= year && year <= 149)
33
11.3k
        return 1;
34
0
    return 0;
35
11.3k
}
36
37
static int leap_year(const int year)
38
202k
{
39
202k
    if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
40
58.2k
        return 1;
41
144k
    return 0;
42
202k
}
43
44
/*
45
 * Compute the day of the week and the day of the year from the year, month
46
 * and day.  The day of the year is straightforward, the day of the week uses
47
 * a form of Zeller's congruence.  For this months start with March and are
48
 * numbered 4 through 15.
49
 */
50
static void determine_days(struct tm *tm)
51
271k
{
52
271k
    static const int ydays[12] = {
53
271k
        0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
54
271k
    };
55
271k
    int y = tm->tm_year + 1900;
56
271k
    int m = tm->tm_mon;
57
271k
    int d = tm->tm_mday;
58
271k
    int c;
59
60
271k
    tm->tm_yday = ydays[m] + d - 1;
61
271k
    if (m >= 2) {
62
        /* March and onwards can be one day further into the year */
63
154k
        tm->tm_yday += leap_year(y);
64
154k
        m += 2;
65
154k
    } else {
66
        /* Treat January and February as part of the previous year */
67
116k
        m += 14;
68
116k
        y--;
69
116k
    }
70
271k
    c = y / 100;
71
271k
    y %= 100;
72
    /* Zeller's congruence */
73
271k
    tm->tm_wday = (d + (13 * m) / 5 + y + y / 4 + c / 4 + 5 * c + 6) % 7;
74
271k
}
75
76
int ossl_asn1_time_to_tm(struct tm *tm, const ASN1_TIME *d)
77
138k
{
78
138k
    static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
79
138k
    static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
80
138k
    static const int mdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
81
138k
    char *a;
82
138k
    int n, i, i2, l, o, min_l, end = 6, btz = 5, md;
83
138k
    struct tm tmp;
84
#if defined(CHARSET_EBCDIC)
85
    const char upper_z = 0x5A, num_zero = 0x30, period = 0x2E, minus = 0x2D, plus = 0x2B;
86
#else
87
138k
    const char upper_z = 'Z', num_zero = '0', period = '.', minus = '-', plus = '+';
88
138k
#endif
89
90
138k
    if (d->type == V_ASN1_UTCTIME) {
91
63.5k
        min_l = 13;
92
75.3k
    } else if (d->type == V_ASN1_GENERALIZEDTIME) {
93
75.3k
        end = 7;
94
75.3k
        btz = 6;
95
75.3k
        min_l = 15;
96
75.3k
    } else {
97
0
        return 0;
98
0
    }
99
100
138k
    l = d->length;
101
138k
    a = (char *)d->data;
102
138k
    o = 0;
103
138k
    memset(&tmp, 0, sizeof(tmp));
104
105
    /*
106
     * GENERALIZEDTIME is similar to UTCTIME except the year is represented
107
     * as YYYY. This stuff treats everything as a two digit field so make
108
     * first two fields 00 to 99
109
     */
110
111
138k
    if (l < min_l)
112
5.48k
        goto err;
113
724k
    for (i = 0; i < end; i++) {
114
657k
        if ((i == btz) && ((a[o] == upper_z) || (a[o] == plus) || (a[o] == minus))) {
115
14.6k
            i++;
116
14.6k
            break;
117
14.6k
        }
118
642k
        if (!ossl_ascii_isdigit(a[o]))
119
25.9k
            goto err;
120
616k
        n = a[o] - num_zero;
121
        /* incomplete 2-digital number */
122
616k
        if (++o == l)
123
0
            goto err;
124
125
616k
        if (!ossl_ascii_isdigit(a[o]))
126
15.2k
            goto err;
127
601k
        n = (n * 10) + a[o] - num_zero;
128
        /* no more bytes to read, but we haven't seen time-zone yet */
129
601k
        if (++o == l)
130
0
            goto err;
131
132
601k
        i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
133
134
601k
        if ((n < min[i2]) || (n > max[i2]))
135
8.86k
            goto err;
136
592k
        switch (i2) {
137
60.4k
        case 0:
138
            /* UTC will never be here */
139
60.4k
            tmp.tm_year = n * 100 - 1900;
140
60.4k
            break;
141
106k
        case 1:
142
106k
            if (d->type == V_ASN1_UTCTIME)
143
48.5k
                tmp.tm_year = n < 50 ? n + 100 : n;
144
58.3k
            else
145
58.3k
                tmp.tm_year += n;
146
106k
            break;
147
95.7k
        case 2:
148
95.7k
            tmp.tm_mon = n - 1;
149
95.7k
            break;
150
91.4k
        case 3:
151
            /* check if tm_mday is valid in tm_mon */
152
91.4k
            if (tmp.tm_mon == 1) {
153
                /* it's February */
154
17.2k
                md = mdays[1] + leap_year(tmp.tm_year + 1900);
155
74.1k
            } else {
156
74.1k
                md = mdays[tmp.tm_mon];
157
74.1k
            }
158
91.4k
            if (n > md)
159
1.65k
                goto err;
160
89.8k
            tmp.tm_mday = n;
161
89.8k
            determine_days(&tmp);
162
89.8k
            break;
163
86.7k
        case 4:
164
86.7k
            tmp.tm_hour = n;
165
86.7k
            break;
166
83.9k
        case 5:
167
83.9k
            tmp.tm_min = n;
168
83.9k
            break;
169
67.1k
        case 6:
170
67.1k
            tmp.tm_sec = n;
171
67.1k
            break;
172
592k
        }
173
592k
    }
174
175
    /*
176
     * Optional fractional seconds: decimal point followed by one or more
177
     * digits.
178
     */
179
81.7k
    if (d->type == V_ASN1_GENERALIZEDTIME && a[o] == period) {
180
23.0k
        if (++o == l)
181
1.35k
            goto err;
182
21.7k
        i = o;
183
96.2k
        while ((o < l) && ossl_ascii_isdigit(a[o]))
184
74.4k
            o++;
185
        /* Must have at least one digit after decimal point */
186
21.7k
        if (i == o)
187
1.21k
            goto err;
188
        /* no more bytes to read, but we haven't seen time-zone yet */
189
20.5k
        if (o == l)
190
176
            goto err;
191
20.5k
    }
192
193
    /*
194
     * 'o' will never point to '\0' at this point, the only chance
195
     * 'o' can point to '\0' is either the subsequent if or the first
196
     * else if is true.
197
     */
198
79.0k
    if (a[o] == upper_z) {
199
58.7k
        o++;
200
58.7k
    } else if (((a[o] == plus) || (a[o] == minus))) {
201
16.1k
        int offsign = a[o] == minus ? 1 : -1;
202
16.1k
        int offset = 0;
203
204
16.1k
        o++;
205
        /*
206
         * if not equal, no need to do subsequent checks
207
         * since the following for-loop will add 'o' by 4
208
         * and the final return statement will check if 'l'
209
         * and 'o' are equal.
210
         */
211
16.1k
        if (o + 4 != l)
212
1.80k
            goto err;
213
36.9k
        for (i = end; i < end + 2; i++) {
214
26.9k
            if (!ossl_ascii_isdigit(a[o]))
215
1.14k
                goto err;
216
25.7k
            n = a[o] - num_zero;
217
25.7k
            o++;
218
25.7k
            if (!ossl_ascii_isdigit(a[o]))
219
2.05k
                goto err;
220
23.7k
            n = (n * 10) + a[o] - num_zero;
221
23.7k
            i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
222
23.7k
            if ((n < min[i2]) || (n > max[i2]))
223
1.14k
                goto err;
224
            /* if tm is NULL, no need to adjust */
225
22.5k
            if (tm != NULL) {
226
22.5k
                if (i == end)
227
12.5k
                    offset = n * 3600;
228
9.99k
                else if (i == end + 1)
229
9.99k
                    offset += n * 60;
230
22.5k
            }
231
22.5k
            o++;
232
22.5k
        }
233
10.0k
        if (offset && !OPENSSL_gmtime_adj(&tmp, 0, offset * offsign))
234
85
            goto err;
235
10.0k
    } else {
236
        /* not Z, or not +/- */
237
4.11k
        goto err;
238
4.11k
    }
239
68.6k
    if (o == l) {
240
        /* success, check if tm should be filled */
241
67.5k
        if (tm != NULL)
242
58.0k
            *tm = tmp;
243
67.5k
        return 1;
244
67.5k
    }
245
71.3k
err:
246
71.3k
    return 0;
247
68.6k
}
248
249
ASN1_TIME *ossl_asn1_time_from_tm(ASN1_TIME *s, struct tm *ts, int type)
250
12.1k
{
251
12.1k
    char *p;
252
12.1k
    ASN1_TIME *tmps = NULL;
253
12.1k
    const int len = 20;
254
12.1k
    int ret;
255
256
12.1k
    if (type == V_ASN1_UNDEF) {
257
0
        if (is_utc(ts->tm_year))
258
0
            type = V_ASN1_UTCTIME;
259
0
        else
260
0
            type = V_ASN1_GENERALIZEDTIME;
261
12.1k
    } else if (type == V_ASN1_UTCTIME) {
262
0
        if (!is_utc(ts->tm_year))
263
0
            goto err;
264
12.1k
    } else if (type != V_ASN1_GENERALIZEDTIME) {
265
0
        goto err;
266
0
    }
267
268
12.1k
    if (s == NULL)
269
0
        tmps = ASN1_STRING_new();
270
12.1k
    else
271
12.1k
        tmps = s;
272
12.1k
    if (tmps == NULL)
273
0
        return NULL;
274
275
12.1k
    if (!ASN1_STRING_set1_data(tmps, NULL, len))
276
0
        goto err;
277
278
12.1k
    tmps->type = type;
279
12.1k
    p = (char *)tmps->data;
280
281
12.1k
    if (ts->tm_mon > INT_MAX - 1)
282
0
        goto err;
283
284
12.1k
    if (type == V_ASN1_GENERALIZEDTIME) {
285
12.1k
        if (ts->tm_year > INT_MAX - 1900)
286
0
            goto err;
287
12.1k
        ret = snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ",
288
12.1k
            ts->tm_year + 1900, ts->tm_mon + 1,
289
12.1k
            ts->tm_mday, ts->tm_hour, ts->tm_min,
290
12.1k
            ts->tm_sec);
291
12.1k
    } else {
292
0
        ret = snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ",
293
0
            ts->tm_year % 100, ts->tm_mon + 1,
294
0
            ts->tm_mday, ts->tm_hour, ts->tm_min,
295
0
            ts->tm_sec);
296
0
    }
297
12.1k
    if (ret < 0 || ret >= len)
298
0
        goto err;
299
12.1k
    tmps->length = ret;
300
301
#ifdef CHARSET_EBCDIC
302
    ebcdic2ascii(tmps->data, tmps->data, tmps->length);
303
#endif
304
12.1k
    return tmps;
305
0
err:
306
0
    if (tmps != s)
307
0
        ASN1_STRING_free(tmps);
308
0
    return NULL;
309
12.1k
}
310
311
ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
312
0
{
313
0
    return ASN1_TIME_adj(s, t, 0, 0);
314
0
}
315
316
ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
317
    int offset_day, long offset_sec)
318
11.3k
{
319
11.3k
    struct tm *ts;
320
11.3k
    struct tm data;
321
322
11.3k
    ts = OPENSSL_gmtime(&t, &data);
323
11.3k
    if (ts == NULL) {
324
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_GETTING_TIME);
325
0
        return NULL;
326
0
    }
327
11.3k
    if (offset_day || offset_sec) {
328
0
        if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
329
0
            return NULL;
330
0
    }
331
11.3k
    return ossl_asn1_time_from_tm(s, ts, V_ASN1_UNDEF);
332
11.3k
}
333
334
int ASN1_TIME_check(const ASN1_TIME *t)
335
485
{
336
485
    if (t->type == V_ASN1_GENERALIZEDTIME)
337
129
        return ASN1_GENERALIZEDTIME_check(t);
338
356
    else if (t->type == V_ASN1_UTCTIME)
339
356
        return ASN1_UTCTIME_check(t);
340
0
    return 0;
341
485
}
342
343
/* Convert an ASN1_TIME structure to GeneralizedTime */
344
ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
345
    ASN1_GENERALIZEDTIME **out)
346
0
{
347
0
    ASN1_GENERALIZEDTIME *ret = NULL;
348
0
    struct tm tm;
349
350
0
    if (!ASN1_TIME_to_tm(t, &tm))
351
0
        return NULL;
352
353
0
    if (out != NULL)
354
0
        ret = *out;
355
356
0
    ret = ossl_asn1_time_from_tm(ret, &tm, V_ASN1_GENERALIZEDTIME);
357
358
0
    if (out != NULL && ret != NULL)
359
0
        *out = ret;
360
361
0
    return ret;
362
0
}
363
364
int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
365
0
{
366
    /* Try UTC, if that fails, try GENERALIZED */
367
0
    if (ASN1_UTCTIME_set_string(s, str))
368
0
        return 1;
369
0
    return ASN1_GENERALIZEDTIME_set_string(s, str);
370
0
}
371
372
int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str)
373
0
{
374
0
    ASN1_TIME t;
375
0
    struct tm tm;
376
0
    size_t len;
377
378
    /* RFC 5280 4.1.2.5: Valid RFC5280 times must be either length 13 or 15. */
379
0
    len = strlen(str);
380
0
    switch (len) {
381
0
    case 13:
382
0
        t.type = V_ASN1_UTCTIME;
383
0
        break;
384
0
    case 15:
385
0
        t.type = V_ASN1_GENERALIZEDTIME;
386
0
        break;
387
0
    default:
388
0
        return 0;
389
0
    }
390
391
    /* RFC 5280 4.1.2.5 Valid RFC5280 times must end in 'Z'. */
392
0
    if (str[len - 1] != 0x5A)
393
0
        return 0;
394
395
0
    t.length = (int)len;
396
0
    t.data = (unsigned char *)str;
397
398
    /*
399
     * RFC 5280 Section 4.1.2.5 The following function is permissive
400
     * and allows time zone offsets and time zones not Z, etc. As we
401
     * have already failed and excluded anything not the correct length
402
     * for the type, and anything not ending in a 'Z', Our time may
403
     * not be any of these other cases, and still parse as a time.
404
     */
405
0
    if (!ossl_asn1_time_to_tm(&tm, &t))
406
0
        return 0;
407
408
0
    if (s != NULL) {
409
        /*
410
         * Unlike every other type of nonconforming to RFC5280 time
411
         * string, which causes this function to fail, a 15 character
412
         * input string that ends up being in the UTC time range is not
413
         * rejected. Instead, two digits of the year is removed from
414
         * start of the input string so the result is a UTC time.
415
         */
416
0
        if (is_utc(tm.tm_year) && len == 15)
417
0
            return ASN1_TIME_set_string(s, str + 2);
418
419
0
        return ASN1_TIME_set_string(s, str);
420
0
    }
421
422
0
    return 1;
423
0
}
424
425
int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm)
426
30.2k
{
427
30.2k
    if (s == NULL) {
428
0
        time_t now_t;
429
430
0
        time(&now_t);
431
0
        memset(tm, 0, sizeof(*tm));
432
0
        if (OPENSSL_gmtime(&now_t, tm) != NULL)
433
0
            return 1;
434
0
        return 0;
435
0
    }
436
437
30.2k
    return ossl_asn1_time_to_tm(tm, s);
438
30.2k
}
439
440
int ASN1_TIME_diff(int *pday, int *psec,
441
    const ASN1_TIME *from, const ASN1_TIME *to)
442
11.6k
{
443
11.6k
    struct tm tm_from, tm_to;
444
445
11.6k
    if (!ASN1_TIME_to_tm(from, &tm_from))
446
2.60k
        return 0;
447
9.00k
    if (!ASN1_TIME_to_tm(to, &tm_to))
448
51
        return 0;
449
8.95k
    return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
450
9.00k
}
451
452
static const char _asn1_mon[12][4] = {
453
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
454
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
455
};
456
457
/* prints the time with the default date format (RFC 822) */
458
int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
459
259k
{
460
259k
    return ASN1_TIME_print_ex(bp, tm, ASN1_DTFLGS_RFC822);
461
259k
}
462
463
/* returns 1 on success, 0 on BIO write error or parse failure */
464
int ASN1_TIME_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags)
465
259k
{
466
259k
    return ossl_asn1_time_print_ex(bp, tm, flags) > 0;
467
259k
}
468
469
/* prints the time with the date format of ISO 8601 */
470
/* returns 0 on BIO write error, else -1 in case of parse failure, else 1 */
471
int ossl_asn1_time_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags)
472
326k
{
473
326k
    char *v;
474
326k
    int l;
475
326k
    struct tm stm;
476
326k
    const char period = 0x2E;
477
478
    /* ossl_asn1_time_to_tm will check the time type */
479
326k
    if (!ossl_asn1_time_to_tm(&stm, tm))
480
188k
        return BIO_write(bp, "Bad time value", 14) ? -1 : 0;
481
482
137k
    l = tm->length;
483
137k
    v = (char *)tm->data;
484
485
137k
    if (tm->type == V_ASN1_GENERALIZEDTIME) {
486
84.4k
        char *f = NULL;
487
84.4k
        int f_len = 0;
488
489
        /*
490
         * Try to parse fractional seconds. '14' is the place of
491
         * 'fraction point' in a GeneralizedTime string.
492
         */
493
84.4k
        if (tm->length > 15 && v[14] == period) {
494
            /* exclude the . itself */
495
44.3k
            f = &v[15];
496
44.3k
            f_len = 0;
497
259k
            while (15 + f_len < l && ossl_ascii_isdigit(f[f_len]))
498
215k
                ++f_len;
499
44.3k
        }
500
501
84.4k
        if (f_len > 0) {
502
44.3k
            if ((flags & ASN1_DTFLGS_TYPE_MASK) == ASN1_DTFLGS_ISO8601) {
503
0
                return BIO_printf(bp, "%4d-%02d-%02d %02d:%02d:%02d.%.*sZ",
504
0
                           stm.tm_year + 1900, stm.tm_mon + 1,
505
0
                           stm.tm_mday, stm.tm_hour,
506
0
                           stm.tm_min, stm.tm_sec, f_len, f)
507
0
                    > 0;
508
44.3k
            } else {
509
44.3k
                return BIO_printf(bp, "%s %2d %02d:%02d:%02d.%.*s %d GMT",
510
44.3k
                           _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
511
44.3k
                           stm.tm_min, stm.tm_sec, f_len, f,
512
44.3k
                           stm.tm_year + 1900)
513
44.3k
                    > 0;
514
44.3k
            }
515
44.3k
        }
516
84.4k
    }
517
93.4k
    if ((flags & ASN1_DTFLGS_TYPE_MASK) == ASN1_DTFLGS_ISO8601) {
518
0
        return BIO_printf(bp, "%4d-%02d-%02d %02d:%02d:%02dZ",
519
0
                   stm.tm_year + 1900, stm.tm_mon + 1,
520
0
                   stm.tm_mday, stm.tm_hour,
521
0
                   stm.tm_min, stm.tm_sec)
522
0
            > 0;
523
93.4k
    } else {
524
93.4k
        return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT",
525
93.4k
                   _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
526
93.4k
                   stm.tm_min, stm.tm_sec, stm.tm_year + 1900)
527
93.4k
            > 0;
528
93.4k
    }
529
93.4k
}
530
531
int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t)
532
0
{
533
0
    struct tm stm, ttm;
534
0
    int day, sec;
535
536
0
    if (!ASN1_TIME_to_tm(s, &stm))
537
0
        return -2;
538
539
0
    if (!OPENSSL_gmtime(&t, &ttm))
540
0
        return -2;
541
542
0
    if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
543
0
        return -2;
544
545
0
    if (day > 0 || sec > 0)
546
0
        return 1;
547
0
    if (day < 0 || sec < 0)
548
0
        return -1;
549
0
    return 0;
550
0
}
551
552
int ASN1_TIME_normalize(ASN1_TIME *t)
553
0
{
554
0
    struct tm tm;
555
556
0
    if (t == NULL || !ASN1_TIME_to_tm(t, &tm))
557
0
        return 0;
558
559
0
    return ossl_asn1_time_from_tm(t, &tm, V_ASN1_UNDEF) != NULL;
560
0
}
561
562
int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b)
563
305
{
564
305
    int day, sec;
565
566
305
    if (!ASN1_TIME_diff(&day, &sec, b, a))
567
161
        return -2;
568
144
    if (day > 0 || sec > 0)
569
35
        return 1;
570
109
    if (day < 0 || sec < 0)
571
104
        return -1;
572
5
    return 0;
573
109
}