Coverage Report

Created: 2026-08-31 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
0
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
0
{
32
0
    if (50 <= year && year <= 149)
33
0
        return 1;
34
0
    return 0;
35
0
}
36
37
static int leap_year(const int year)
38
0
{
39
0
    if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
40
0
        return 1;
41
0
    return 0;
42
0
}
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
0
{
52
0
    static const int ydays[12] = {
53
0
        0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
54
0
    };
55
0
    int y = tm->tm_year + 1900;
56
0
    int m = tm->tm_mon;
57
0
    int d = tm->tm_mday;
58
0
    int c;
59
60
0
    tm->tm_yday = ydays[m] + d - 1;
61
0
    if (m >= 2) {
62
        /* March and onwards can be one day further into the year */
63
0
        tm->tm_yday += leap_year(y);
64
0
        m += 2;
65
0
    } else {
66
        /* Treat January and February as part of the previous year */
67
0
        m += 14;
68
0
        y--;
69
0
    }
70
0
    c = y / 100;
71
0
    y %= 100;
72
    /* Zeller's congruence */
73
0
    tm->tm_wday = (d + (13 * m) / 5 + y + y / 4 + c / 4 + 5 * c + 6) % 7;
74
0
}
75
76
int ossl_asn1_time_to_tm(struct tm *tm, const ASN1_TIME *d)
77
0
{
78
0
    static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
79
0
    static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
80
0
    static const int mdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
81
0
    char *a;
82
0
    int n, i, i2, l, o, min_l, end = 6, btz = 5, md;
83
0
    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
0
    const char upper_z = 'Z', num_zero = '0', period = '.', minus = '-', plus = '+';
88
0
#endif
89
90
0
    if (d->type == V_ASN1_UTCTIME) {
91
0
        min_l = 13;
92
0
    } else if (d->type == V_ASN1_GENERALIZEDTIME) {
93
0
        end = 7;
94
0
        btz = 6;
95
0
        min_l = 15;
96
0
    } else {
97
0
        return 0;
98
0
    }
99
100
0
    l = d->length;
101
0
    a = (char *)d->data;
102
0
    o = 0;
103
0
    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
0
    if (l < min_l)
112
0
        goto err;
113
0
    for (i = 0; i < end; i++) {
114
0
        if ((i == btz) && ((a[o] == upper_z) || (a[o] == plus) || (a[o] == minus))) {
115
0
            i++;
116
0
            break;
117
0
        }
118
0
        if (!ossl_ascii_isdigit(a[o]))
119
0
            goto err;
120
0
        n = a[o] - num_zero;
121
        /* incomplete 2-digital number */
122
0
        if (++o == l)
123
0
            goto err;
124
125
0
        if (!ossl_ascii_isdigit(a[o]))
126
0
            goto err;
127
0
        n = (n * 10) + a[o] - num_zero;
128
        /* no more bytes to read, but we haven't seen time-zone yet */
129
0
        if (++o == l)
130
0
            goto err;
131
132
0
        i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
133
134
0
        if ((n < min[i2]) || (n > max[i2]))
135
0
            goto err;
136
0
        switch (i2) {
137
0
        case 0:
138
            /* UTC will never be here */
139
0
            tmp.tm_year = n * 100 - 1900;
140
0
            break;
141
0
        case 1:
142
0
            if (d->type == V_ASN1_UTCTIME)
143
0
                tmp.tm_year = n < 50 ? n + 100 : n;
144
0
            else
145
0
                tmp.tm_year += n;
146
0
            break;
147
0
        case 2:
148
0
            tmp.tm_mon = n - 1;
149
0
            break;
150
0
        case 3:
151
            /* check if tm_mday is valid in tm_mon */
152
0
            if (tmp.tm_mon == 1) {
153
                /* it's February */
154
0
                md = mdays[1] + leap_year(tmp.tm_year + 1900);
155
0
            } else {
156
0
                md = mdays[tmp.tm_mon];
157
0
            }
158
0
            if (n > md)
159
0
                goto err;
160
0
            tmp.tm_mday = n;
161
0
            determine_days(&tmp);
162
0
            break;
163
0
        case 4:
164
0
            tmp.tm_hour = n;
165
0
            break;
166
0
        case 5:
167
0
            tmp.tm_min = n;
168
0
            break;
169
0
        case 6:
170
0
            tmp.tm_sec = n;
171
0
            break;
172
0
        }
173
0
    }
174
175
    /*
176
     * Optional fractional seconds: decimal point followed by one or more
177
     * digits.
178
     */
179
0
    if (d->type == V_ASN1_GENERALIZEDTIME && a[o] == period) {
180
0
        if (++o == l)
181
0
            goto err;
182
0
        i = o;
183
0
        while ((o < l) && ossl_ascii_isdigit(a[o]))
184
0
            o++;
185
        /* Must have at least one digit after decimal point */
186
0
        if (i == o)
187
0
            goto err;
188
        /* no more bytes to read, but we haven't seen time-zone yet */
189
0
        if (o == l)
190
0
            goto err;
191
0
    }
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
0
    if (a[o] == upper_z) {
199
0
        o++;
200
0
    } else if (((a[o] == plus) || (a[o] == minus))) {
201
0
        int offsign = a[o] == minus ? 1 : -1;
202
0
        int offset = 0;
203
204
0
        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
0
        if (o + 4 != l)
212
0
            goto err;
213
0
        for (i = end; i < end + 2; i++) {
214
0
            if (!ossl_ascii_isdigit(a[o]))
215
0
                goto err;
216
0
            n = a[o] - num_zero;
217
0
            o++;
218
0
            if (!ossl_ascii_isdigit(a[o]))
219
0
                goto err;
220
0
            n = (n * 10) + a[o] - num_zero;
221
0
            i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
222
0
            if ((n < min[i2]) || (n > max[i2]))
223
0
                goto err;
224
            /* if tm is NULL, no need to adjust */
225
0
            if (tm != NULL) {
226
0
                if (i == end)
227
0
                    offset = n * 3600;
228
0
                else if (i == end + 1)
229
0
                    offset += n * 60;
230
0
            }
231
0
            o++;
232
0
        }
233
0
        if (offset && !OPENSSL_gmtime_adj(&tmp, 0, offset * offsign))
234
0
            goto err;
235
0
    } else {
236
        /* not Z, or not +/- */
237
0
        goto err;
238
0
    }
239
0
    if (o == l) {
240
        /* success, check if tm should be filled */
241
0
        if (tm != NULL)
242
0
            *tm = tmp;
243
0
        return 1;
244
0
    }
245
0
err:
246
0
    return 0;
247
0
}
248
249
ASN1_TIME *ossl_asn1_time_from_tm(ASN1_TIME *s, struct tm *ts, int type)
250
0
{
251
0
    char *p;
252
0
    ASN1_TIME *tmps = NULL;
253
0
    const int len = 20;
254
0
    int ret;
255
256
0
    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
0
    } else if (type == V_ASN1_UTCTIME) {
262
0
        if (!is_utc(ts->tm_year))
263
0
            goto err;
264
0
    } else if (type != V_ASN1_GENERALIZEDTIME) {
265
0
        goto err;
266
0
    }
267
268
0
    if (s == NULL)
269
0
        tmps = ASN1_STRING_new();
270
0
    else
271
0
        tmps = s;
272
0
    if (tmps == NULL)
273
0
        return NULL;
274
275
0
    if (!ASN1_STRING_set1_data(tmps, NULL, len))
276
0
        goto err;
277
278
0
    tmps->type = type;
279
0
    p = (char *)tmps->data;
280
281
0
    if (ts->tm_mon > INT_MAX - 1)
282
0
        goto err;
283
284
0
    if (type == V_ASN1_GENERALIZEDTIME) {
285
0
        if (ts->tm_year > INT_MAX - 1900)
286
0
            goto err;
287
0
        ret = snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ",
288
0
            ts->tm_year + 1900, ts->tm_mon + 1,
289
0
            ts->tm_mday, ts->tm_hour, ts->tm_min,
290
0
            ts->tm_sec);
291
0
    } 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
0
    if (ret < 0 || ret >= len)
298
0
        goto err;
299
0
    tmps->length = ret;
300
301
#ifdef CHARSET_EBCDIC
302
    ebcdic2ascii(tmps->data, tmps->data, tmps->length);
303
#endif
304
0
    return tmps;
305
0
err:
306
0
    if (tmps != s)
307
0
        ASN1_STRING_free(tmps);
308
0
    return NULL;
309
0
}
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
0
{
319
0
    struct tm *ts;
320
0
    struct tm data;
321
322
0
    ts = OPENSSL_gmtime(&t, &data);
323
0
    if (ts == NULL) {
324
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_GETTING_TIME);
325
0
        return NULL;
326
0
    }
327
0
    if (offset_day || offset_sec) {
328
0
        if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
329
0
            return NULL;
330
0
    }
331
0
    return ossl_asn1_time_from_tm(s, ts, V_ASN1_UNDEF);
332
0
}
333
334
int ASN1_TIME_check(const ASN1_TIME *t)
335
0
{
336
0
    if (t->type == V_ASN1_GENERALIZEDTIME)
337
0
        return ASN1_GENERALIZEDTIME_check(t);
338
0
    else if (t->type == V_ASN1_UTCTIME)
339
0
        return ASN1_UTCTIME_check(t);
340
0
    return 0;
341
0
}
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
0
{
427
0
    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
0
    return ossl_asn1_time_to_tm(tm, s);
438
0
}
439
440
int ASN1_TIME_diff(int *pday, int *psec,
441
    const ASN1_TIME *from, const ASN1_TIME *to)
442
0
{
443
0
    struct tm tm_from, tm_to;
444
445
0
    if (!ASN1_TIME_to_tm(from, &tm_from))
446
0
        return 0;
447
0
    if (!ASN1_TIME_to_tm(to, &tm_to))
448
0
        return 0;
449
0
    return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
450
0
}
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
0
{
460
0
    return ASN1_TIME_print_ex(bp, tm, ASN1_DTFLGS_RFC822);
461
0
}
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
0
{
466
0
    return ossl_asn1_time_print_ex(bp, tm, flags) > 0;
467
0
}
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
0
{
473
0
    char *v;
474
0
    int l;
475
0
    struct tm stm;
476
0
    const char period = 0x2E;
477
478
    /* ossl_asn1_time_to_tm will check the time type */
479
0
    if (!ossl_asn1_time_to_tm(&stm, tm))
480
0
        return BIO_write(bp, "Bad time value", 14) ? -1 : 0;
481
482
0
    l = tm->length;
483
0
    v = (char *)tm->data;
484
485
0
    if (tm->type == V_ASN1_GENERALIZEDTIME) {
486
0
        char *f = NULL;
487
0
        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
0
        if (tm->length > 15 && v[14] == period) {
494
            /* exclude the . itself */
495
0
            f = &v[15];
496
0
            f_len = 0;
497
0
            while (15 + f_len < l && ossl_ascii_isdigit(f[f_len]))
498
0
                ++f_len;
499
0
        }
500
501
0
        if (f_len > 0) {
502
0
            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
0
            } else {
509
0
                return BIO_printf(bp, "%s %2d %02d:%02d:%02d.%.*s %d GMT",
510
0
                           _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
511
0
                           stm.tm_min, stm.tm_sec, f_len, f,
512
0
                           stm.tm_year + 1900)
513
0
                    > 0;
514
0
            }
515
0
        }
516
0
    }
517
0
    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
0
    } else {
524
0
        return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT",
525
0
                   _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
526
0
                   stm.tm_min, stm.tm_sec, stm.tm_year + 1900)
527
0
            > 0;
528
0
    }
529
0
}
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
0
{
564
0
    int day, sec;
565
566
0
    if (!ASN1_TIME_diff(&day, &sec, b, a))
567
0
        return -2;
568
0
    if (day > 0 || sec > 0)
569
0
        return 1;
570
0
    if (day < 0 || sec < 0)
571
0
        return -1;
572
0
    return 0;
573
0
}