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.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
73.1k
{
251
73.1k
    char *p;
252
73.1k
    ASN1_TIME *tmps = NULL;
253
73.1k
    const int len = 20;
254
255
73.1k
    if (type == V_ASN1_UNDEF) {
256
11.3k
        if (is_utc(ts->tm_year))
257
11.3k
            type = V_ASN1_UTCTIME;
258
0
        else
259
0
            type = V_ASN1_GENERALIZEDTIME;
260
61.8k
    } else if (type == V_ASN1_UTCTIME) {
261
0
        if (!is_utc(ts->tm_year))
262
0
            goto err;
263
61.8k
    } else if (type != V_ASN1_GENERALIZEDTIME) {
264
0
        goto err;
265
0
    }
266
267
73.1k
    if (s == NULL)
268
11.3k
        tmps = ASN1_STRING_new();
269
61.8k
    else
270
61.8k
        tmps = s;
271
73.1k
    if (tmps == NULL)
272
0
        return NULL;
273
274
73.1k
    if (!ASN1_STRING_set(tmps, NULL, len))
275
0
        goto err;
276
277
73.1k
    tmps->type = type;
278
73.1k
    p = (char *)tmps->data;
279
280
73.1k
    if (ts->tm_mon > INT_MAX - 1)
281
0
        goto err;
282
283
73.1k
    if (type == V_ASN1_GENERALIZEDTIME) {
284
61.8k
        if (ts->tm_year > INT_MAX - 1900)
285
0
            goto err;
286
61.8k
        tmps->length = BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ",
287
61.8k
            ts->tm_year + 1900, ts->tm_mon + 1,
288
61.8k
            ts->tm_mday, ts->tm_hour, ts->tm_min,
289
61.8k
            ts->tm_sec);
290
61.8k
    } else {
291
11.3k
        tmps->length = BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ",
292
11.3k
            ts->tm_year % 100, ts->tm_mon + 1,
293
11.3k
            ts->tm_mday, ts->tm_hour, ts->tm_min,
294
11.3k
            ts->tm_sec);
295
11.3k
    }
296
297
#ifdef CHARSET_EBCDIC
298
    ebcdic2ascii(tmps->data, tmps->data, tmps->length);
299
#endif
300
73.1k
    return tmps;
301
0
err:
302
0
    if (tmps != s)
303
0
        ASN1_STRING_free(tmps);
304
0
    return NULL;
305
73.1k
}
306
307
ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
308
0
{
309
0
    return ASN1_TIME_adj(s, t, 0, 0);
310
0
}
311
312
ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
313
    int offset_day, long offset_sec)
314
11.3k
{
315
11.3k
    struct tm *ts;
316
11.3k
    struct tm data;
317
318
11.3k
    ts = OPENSSL_gmtime(&t, &data);
319
11.3k
    if (ts == NULL) {
320
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_GETTING_TIME);
321
0
        return NULL;
322
0
    }
323
11.3k
    if (offset_day || offset_sec) {
324
0
        if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
325
0
            return NULL;
326
0
    }
327
11.3k
    return ossl_asn1_time_from_tm(s, ts, V_ASN1_UNDEF);
328
11.3k
}
329
330
int ASN1_TIME_check(const ASN1_TIME *t)
331
485
{
332
485
    if (t->type == V_ASN1_GENERALIZEDTIME)
333
129
        return ASN1_GENERALIZEDTIME_check(t);
334
356
    else if (t->type == V_ASN1_UTCTIME)
335
356
        return ASN1_UTCTIME_check(t);
336
0
    return 0;
337
485
}
338
339
/* Convert an ASN1_TIME structure to GeneralizedTime */
340
ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
341
    ASN1_GENERALIZEDTIME **out)
342
0
{
343
0
    ASN1_GENERALIZEDTIME *ret = NULL;
344
0
    struct tm tm;
345
346
0
    if (!ASN1_TIME_to_tm(t, &tm))
347
0
        return NULL;
348
349
0
    if (out != NULL)
350
0
        ret = *out;
351
352
0
    ret = ossl_asn1_time_from_tm(ret, &tm, V_ASN1_GENERALIZEDTIME);
353
354
0
    if (out != NULL && ret != NULL)
355
0
        *out = ret;
356
357
0
    return ret;
358
0
}
359
360
int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
361
0
{
362
    /* Try UTC, if that fails, try GENERALIZED */
363
0
    if (ASN1_UTCTIME_set_string(s, str))
364
0
        return 1;
365
0
    return ASN1_GENERALIZEDTIME_set_string(s, str);
366
0
}
367
368
int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str)
369
0
{
370
0
    ASN1_TIME t;
371
0
    struct tm tm;
372
0
    size_t len;
373
374
    /* RFC 5280 4.1.2.5: Valid RFC5280 times must be either length 13 or 15. */
375
0
    len = strlen(str);
376
0
    switch (len) {
377
0
    case 13:
378
0
        t.type = V_ASN1_UTCTIME;
379
0
        break;
380
0
    case 15:
381
0
        t.type = V_ASN1_GENERALIZEDTIME;
382
0
        break;
383
0
    default:
384
0
        return 0;
385
0
    }
386
387
    /* RFC 5280 4.1.2.5 Valid RFC5280 times must end in 'Z'. */
388
0
    if (str[len - 1] != 0x5A)
389
0
        return 0;
390
391
0
    t.length = (int)len;
392
0
    t.data = (unsigned char *)str;
393
394
    /*
395
     * RFC 5280 Section 4.1.2.5 The following function is permissive
396
     * and allows time zone offsets and time zones not Z, etc. As we
397
     * have already failed and excluded anything not the correct length
398
     * for the type, and anything not ending in a 'Z', Our time may
399
     * not be any of these other cases, and still parse as a time.
400
     */
401
0
    if (!ossl_asn1_time_to_tm(&tm, &t))
402
0
        return 0;
403
404
0
    if (s != NULL) {
405
        /*
406
         * Unlike every other type of nonconforming to RFC5280 time
407
         * string, which causes this function to fail, a 15 character
408
         * input string that ends up being in the UTC time range is not
409
         * rejected. Instead, two digits of the year is removed from
410
         * start of the input string so the result is a UTC time.
411
         */
412
0
        if (is_utc(tm.tm_year) && len == 15)
413
0
            return ASN1_TIME_set_string(s, str + 2);
414
415
0
        return ASN1_TIME_set_string(s, str);
416
0
    }
417
418
0
    return 1;
419
0
}
420
421
int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm)
422
30.2k
{
423
30.2k
    if (s == NULL) {
424
0
        time_t now_t;
425
426
0
        time(&now_t);
427
0
        memset(tm, 0, sizeof(*tm));
428
0
        if (OPENSSL_gmtime(&now_t, tm) != NULL)
429
0
            return 1;
430
0
        return 0;
431
0
    }
432
433
30.2k
    return ossl_asn1_time_to_tm(tm, s);
434
30.2k
}
435
436
int ASN1_TIME_diff(int *pday, int *psec,
437
    const ASN1_TIME *from, const ASN1_TIME *to)
438
11.6k
{
439
11.6k
    struct tm tm_from, tm_to;
440
441
11.6k
    if (!ASN1_TIME_to_tm(from, &tm_from))
442
2.60k
        return 0;
443
9.00k
    if (!ASN1_TIME_to_tm(to, &tm_to))
444
51
        return 0;
445
8.95k
    return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
446
9.00k
}
447
448
static const char _asn1_mon[12][4] = {
449
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
450
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
451
};
452
453
/* prints the time with the default date format (RFC 822) */
454
int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
455
259k
{
456
259k
    return ASN1_TIME_print_ex(bp, tm, ASN1_DTFLGS_RFC822);
457
259k
}
458
459
/* returns 1 on success, 0 on BIO write error or parse failure */
460
int ASN1_TIME_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags)
461
259k
{
462
259k
    return ossl_asn1_time_print_ex(bp, tm, flags) > 0;
463
259k
}
464
465
/* prints the time with the date format of ISO 8601 */
466
/* returns 0 on BIO write error, else -1 in case of parse failure, else 1 */
467
int ossl_asn1_time_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags)
468
326k
{
469
326k
    char *v;
470
326k
    int l;
471
326k
    struct tm stm;
472
326k
    const char period = 0x2E;
473
474
    /* ossl_asn1_time_to_tm will check the time type */
475
326k
    if (!ossl_asn1_time_to_tm(&stm, tm))
476
188k
        return BIO_write(bp, "Bad time value", 14) ? -1 : 0;
477
478
137k
    l = tm->length;
479
137k
    v = (char *)tm->data;
480
481
137k
    if (tm->type == V_ASN1_GENERALIZEDTIME) {
482
84.4k
        char *f = NULL;
483
84.4k
        int f_len = 0;
484
485
        /*
486
         * Try to parse fractional seconds. '14' is the place of
487
         * 'fraction point' in a GeneralizedTime string.
488
         */
489
84.4k
        if (tm->length > 15 && v[14] == period) {
490
            /* exclude the . itself */
491
44.3k
            f = &v[15];
492
44.3k
            f_len = 0;
493
259k
            while (15 + f_len < l && ossl_ascii_isdigit(f[f_len]))
494
215k
                ++f_len;
495
44.3k
        }
496
497
84.4k
        if (f_len > 0) {
498
44.3k
            if ((flags & ASN1_DTFLGS_TYPE_MASK) == ASN1_DTFLGS_ISO8601) {
499
0
                return BIO_printf(bp, "%4d-%02d-%02d %02d:%02d:%02d.%.*sZ",
500
0
                           stm.tm_year + 1900, stm.tm_mon + 1,
501
0
                           stm.tm_mday, stm.tm_hour,
502
0
                           stm.tm_min, stm.tm_sec, f_len, f)
503
0
                    > 0;
504
44.3k
            } else {
505
44.3k
                return BIO_printf(bp, "%s %2d %02d:%02d:%02d.%.*s %d GMT",
506
44.3k
                           _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
507
44.3k
                           stm.tm_min, stm.tm_sec, f_len, f,
508
44.3k
                           stm.tm_year + 1900)
509
44.3k
                    > 0;
510
44.3k
            }
511
44.3k
        }
512
84.4k
    }
513
93.4k
    if ((flags & ASN1_DTFLGS_TYPE_MASK) == ASN1_DTFLGS_ISO8601) {
514
0
        return BIO_printf(bp, "%4d-%02d-%02d %02d:%02d:%02dZ",
515
0
                   stm.tm_year + 1900, stm.tm_mon + 1,
516
0
                   stm.tm_mday, stm.tm_hour,
517
0
                   stm.tm_min, stm.tm_sec)
518
0
            > 0;
519
93.4k
    } else {
520
93.4k
        return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT",
521
93.4k
                   _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
522
93.4k
                   stm.tm_min, stm.tm_sec, stm.tm_year + 1900)
523
93.4k
            > 0;
524
93.4k
    }
525
93.4k
}
526
527
int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t)
528
0
{
529
0
    struct tm stm, ttm;
530
0
    int day, sec;
531
532
0
    if (!ASN1_TIME_to_tm(s, &stm))
533
0
        return -2;
534
535
0
    if (!OPENSSL_gmtime(&t, &ttm))
536
0
        return -2;
537
538
0
    if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
539
0
        return -2;
540
541
0
    if (day > 0 || sec > 0)
542
0
        return 1;
543
0
    if (day < 0 || sec < 0)
544
0
        return -1;
545
0
    return 0;
546
0
}
547
548
int ASN1_TIME_normalize(ASN1_TIME *t)
549
0
{
550
0
    struct tm tm;
551
552
0
    if (t == NULL || !ASN1_TIME_to_tm(t, &tm))
553
0
        return 0;
554
555
0
    return ossl_asn1_time_from_tm(t, &tm, V_ASN1_UNDEF) != NULL;
556
0
}
557
558
int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b)
559
305
{
560
305
    int day, sec;
561
562
305
    if (!ASN1_TIME_diff(&day, &sec, b, a))
563
161
        return -2;
564
144
    if (day > 0 || sec > 0)
565
35
        return 1;
566
109
    if (day < 0 || sec < 0)
567
104
        return -1;
568
5
    return 0;
569
109
}