Coverage Report

Created: 2026-01-09 07:00

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-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
 * 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
255
0
    if (type == V_ASN1_UNDEF) {
256
0
        if (is_utc(ts->tm_year))
257
0
            type = V_ASN1_UTCTIME;
258
0
        else
259
0
            type = V_ASN1_GENERALIZEDTIME;
260
0
    } else if (type == V_ASN1_UTCTIME) {
261
0
        if (!is_utc(ts->tm_year))
262
0
            goto err;
263
0
    } else if (type != V_ASN1_GENERALIZEDTIME) {
264
0
        goto err;
265
0
    }
266
267
0
    if (s == NULL)
268
0
        tmps = ASN1_STRING_new();
269
0
    else
270
0
        tmps = s;
271
0
    if (tmps == NULL)
272
0
        return NULL;
273
274
0
    if (!ASN1_STRING_set(tmps, NULL, len))
275
0
        goto err;
276
277
0
    tmps->type = type;
278
0
    p = (char *)tmps->data;
279
280
0
    if (ts->tm_mon > INT_MAX - 1)
281
0
        goto err;
282
283
0
    if (type == V_ASN1_GENERALIZEDTIME) {
284
0
        if (ts->tm_year > INT_MAX - 1900)
285
0
            goto err;
286
0
        tmps->length = BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ",
287
0
            ts->tm_year + 1900, ts->tm_mon + 1,
288
0
            ts->tm_mday, ts->tm_hour, ts->tm_min,
289
0
            ts->tm_sec);
290
0
    } else {
291
0
        tmps->length = BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ",
292
0
            ts->tm_year % 100, ts->tm_mon + 1,
293
0
            ts->tm_mday, ts->tm_hour, ts->tm_min,
294
0
            ts->tm_sec);
295
0
    }
296
297
#ifdef CHARSET_EBCDIC
298
    ebcdic2ascii(tmps->data, tmps->data, tmps->length);
299
#endif
300
0
    return tmps;
301
0
err:
302
0
    if (tmps != s)
303
0
        ASN1_STRING_free(tmps);
304
0
    return NULL;
305
0
}
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
0
{
315
0
    struct tm *ts;
316
0
    struct tm data;
317
318
0
    ts = OPENSSL_gmtime(&t, &data);
319
0
    if (ts == NULL) {
320
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_GETTING_TIME);
321
0
        return NULL;
322
0
    }
323
0
    if (offset_day || offset_sec) {
324
0
        if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
325
0
            return NULL;
326
0
    }
327
0
    return ossl_asn1_time_from_tm(s, ts, V_ASN1_UNDEF);
328
0
}
329
330
int ASN1_TIME_check(const ASN1_TIME *t)
331
0
{
332
0
    if (t->type == V_ASN1_GENERALIZEDTIME)
333
0
        return ASN1_GENERALIZEDTIME_check(t);
334
0
    else if (t->type == V_ASN1_UTCTIME)
335
0
        return ASN1_UTCTIME_check(t);
336
0
    return 0;
337
0
}
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
0
{
423
0
    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
0
    return ossl_asn1_time_to_tm(tm, s);
434
0
}
435
436
int ASN1_TIME_diff(int *pday, int *psec,
437
    const ASN1_TIME *from, const ASN1_TIME *to)
438
0
{
439
0
    struct tm tm_from, tm_to;
440
441
0
    if (!ASN1_TIME_to_tm(from, &tm_from))
442
0
        return 0;
443
0
    if (!ASN1_TIME_to_tm(to, &tm_to))
444
0
        return 0;
445
0
    return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
446
0
}
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
0
{
456
0
    return ASN1_TIME_print_ex(bp, tm, ASN1_DTFLGS_RFC822);
457
0
}
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
0
{
462
0
    return ossl_asn1_time_print_ex(bp, tm, flags) > 0;
463
0
}
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
0
{
469
0
    char *v;
470
0
    int l;
471
0
    struct tm stm;
472
0
    const char period = 0x2E;
473
474
    /* ossl_asn1_time_to_tm will check the time type */
475
0
    if (!ossl_asn1_time_to_tm(&stm, tm))
476
0
        return BIO_write(bp, "Bad time value", 14) ? -1 : 0;
477
478
0
    l = tm->length;
479
0
    v = (char *)tm->data;
480
481
0
    if (tm->type == V_ASN1_GENERALIZEDTIME) {
482
0
        char *f = NULL;
483
0
        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
0
        if (tm->length > 15 && v[14] == period) {
490
            /* exclude the . itself */
491
0
            f = &v[15];
492
0
            f_len = 0;
493
0
            while (15 + f_len < l && ossl_ascii_isdigit(f[f_len]))
494
0
                ++f_len;
495
0
        }
496
497
0
        if (f_len > 0) {
498
0
            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
0
            } else {
505
0
                return BIO_printf(bp, "%s %2d %02d:%02d:%02d.%.*s %d GMT",
506
0
                           _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
507
0
                           stm.tm_min, stm.tm_sec, f_len, f,
508
0
                           stm.tm_year + 1900)
509
0
                    > 0;
510
0
            }
511
0
        }
512
0
    }
513
0
    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
0
    } else {
520
0
        return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT",
521
0
                   _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
522
0
                   stm.tm_min, stm.tm_sec, stm.tm_year + 1900)
523
0
            > 0;
524
0
    }
525
0
}
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
0
{
560
0
    int day, sec;
561
562
0
    if (!ASN1_TIME_diff(&day, &sec, b, a))
563
0
        return -2;
564
0
    if (day > 0 || sec > 0)
565
0
        return 1;
566
0
    if (day < 0 || sec < 0)
567
0
        return -1;
568
0
    return 0;
569
0
}