Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/asn1/a_time.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2019 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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/ctype.h"
20
#include "internal/cryptlib.h"
21
#include <openssl/asn1t.h>
22
#include "asn1_local.h"
23
24
IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
25
26
IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
27
28
static int is_utc(const int year)
29
0
{
30
0
    if (50 <= year && year <= 149)
31
0
        return 1;
32
0
    return 0;
33
0
}
34
35
static int leap_year(const int year)
36
27.3k
{
37
27.3k
    if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
38
1.78k
        return 1;
39
25.5k
    return 0;
40
27.3k
}
41
42
/*
43
 * Compute the day of the week and the day of the year from the year, month
44
 * and day.  The day of the year is straightforward, the day of the week uses
45
 * a form of Zeller's congruence.  For this months start with March and are
46
 * numbered 4 through 15.
47
 */
48
static void determine_days(struct tm *tm)
49
36.5k
{
50
36.5k
    static const int ydays[12] = {
51
36.5k
        0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
52
36.5k
    };
53
36.5k
    int y = tm->tm_year + 1900;
54
36.5k
    int m = tm->tm_mon;
55
36.5k
    int d = tm->tm_mday;
56
36.5k
    int c;
57
58
36.5k
    tm->tm_yday = ydays[m] + d - 1;
59
36.5k
    if (m >= 2) {
60
        /* March and onwards can be one day further into the year */
61
25.8k
        tm->tm_yday += leap_year(y);
62
25.8k
        m += 2;
63
25.8k
    } else {
64
        /* Treat January and February as part of the previous year */
65
10.6k
        m += 14;
66
10.6k
        y--;
67
10.6k
    }
68
36.5k
    c = y / 100;
69
36.5k
    y %= 100;
70
    /* Zeller's congruence */
71
36.5k
    tm->tm_wday = (d + (13 * m) / 5 + y + y / 4 + c / 4 + 5 * c + 6) % 7;
72
36.5k
}
73
74
int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *d)
75
43.3k
{
76
43.3k
    static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
77
43.3k
    static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
78
43.3k
    static const int mdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
79
43.3k
    char *a;
80
43.3k
    int n, i, i2, l, o, min_l = 11, strict = 0, end = 6, btz = 5, md;
81
43.3k
    struct tm tmp;
82
#if defined(CHARSET_EBCDIC)
83
    const char upper_z = 0x5A, num_zero = 0x30, period = 0x2E, minus = 0x2D, plus = 0x2B;
84
#else
85
43.3k
    const char upper_z = 'Z', num_zero = '0', period = '.', minus = '-', plus = '+';
86
43.3k
#endif
87
    /*
88
     * ASN1_STRING_FLAG_X509_TIME is used to enforce RFC 5280
89
     * time string format, in which:
90
     *
91
     * 1. "seconds" is a 'MUST'
92
     * 2. "Zulu" timezone is a 'MUST'
93
     * 3. "+|-" is not allowed to indicate a time zone
94
     */
95
43.3k
    if (d->type == V_ASN1_UTCTIME) {
96
6.96k
        if (d->flags & ASN1_STRING_FLAG_X509_TIME) {
97
0
            min_l = 13;
98
0
            strict = 1;
99
0
        }
100
36.3k
    } else if (d->type == V_ASN1_GENERALIZEDTIME) {
101
36.3k
        end = 7;
102
36.3k
        btz = 6;
103
36.3k
        if (d->flags & ASN1_STRING_FLAG_X509_TIME) {
104
0
            min_l = 15;
105
0
            strict = 1;
106
36.3k
        } else {
107
36.3k
            min_l = 13;
108
36.3k
        }
109
36.3k
    } else {
110
0
        return 0;
111
0
    }
112
113
43.3k
    l = d->length;
114
43.3k
    a = (char *)d->data;
115
43.3k
    o = 0;
116
43.3k
    memset(&tmp, 0, sizeof(tmp));
117
118
    /*
119
     * GENERALIZEDTIME is similar to UTCTIME except the year is represented
120
     * as YYYY. This stuff treats everything as a two digit field so make
121
     * first two fields 00 to 99
122
     */
123
124
43.3k
    if (l < min_l)
125
4.49k
        goto err;
126
268k
    for (i = 0; i < end; i++) {
127
254k
        if (!strict && (i == btz) && ((a[o] == upper_z) || (a[o] == plus) || (a[o] == minus))) {
128
21.5k
            i++;
129
21.5k
            break;
130
21.5k
        }
131
233k
        if (!ascii_isdigit(a[o]))
132
893
            goto err;
133
232k
        n = a[o] - num_zero;
134
        /* incomplete 2-digital number */
135
232k
        if (++o == l)
136
241
            goto err;
137
138
232k
        if (!ascii_isdigit(a[o]))
139
1.56k
            goto err;
140
230k
        n = (n * 10) + a[o] - num_zero;
141
        /* no more bytes to read, but we haven't seen time-zone yet */
142
230k
        if (++o == l)
143
2
            goto err;
144
145
230k
        i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
146
147
230k
        if ((n < min[i2]) || (n > max[i2]))
148
745
            goto err;
149
229k
        switch (i2) {
150
31.9k
        case 0:
151
            /* UTC will never be here */
152
31.9k
            tmp.tm_year = n * 100 - 1900;
153
31.9k
            break;
154
38.2k
        case 1:
155
38.2k
            if (d->type == V_ASN1_UTCTIME)
156
6.78k
                tmp.tm_year = n < 50 ? n + 100 : n;
157
31.4k
            else
158
31.4k
                tmp.tm_year += n;
159
38.2k
            break;
160
37.2k
        case 2:
161
37.2k
            tmp.tm_mon = n - 1;
162
37.2k
            break;
163
37.0k
        case 3:
164
            /* check if tm_mday is valid in tm_mon */
165
37.0k
            if (tmp.tm_mon == 1) {
166
                /* it's February */
167
1.48k
                md = mdays[1] + leap_year(tmp.tm_year + 1900);
168
35.5k
            } else {
169
35.5k
                md = mdays[tmp.tm_mon];
170
35.5k
            }
171
37.0k
            if (n > md)
172
556
                goto err;
173
36.5k
            tmp.tm_mday = n;
174
36.5k
            determine_days(&tmp);
175
36.5k
            break;
176
36.2k
        case 4:
177
36.2k
            tmp.tm_hour = n;
178
36.2k
            break;
179
35.7k
        case 5:
180
35.7k
            tmp.tm_min = n;
181
35.7k
            break;
182
13.3k
        case 6:
183
13.3k
            tmp.tm_sec = n;
184
13.3k
            break;
185
229k
        }
186
229k
    }
187
188
    /*
189
     * Optional fractional seconds: decimal point followed by one or more
190
     * digits.
191
     */
192
34.8k
    if (d->type == V_ASN1_GENERALIZEDTIME && a[o] == period) {
193
6.26k
        if (strict)
194
            /* RFC 5280 forbids fractional seconds */
195
0
            goto err;
196
6.26k
        if (++o == l)
197
10
            goto err;
198
6.25k
        i = o;
199
23.8k
        while ((o < l) && ascii_isdigit(a[o]))
200
17.6k
            o++;
201
        /* Must have at least one digit after decimal point */
202
6.25k
        if (i == o)
203
307
            goto err;
204
        /* no more bytes to read, but we haven't seen time-zone yet */
205
5.95k
        if (o == l)
206
197
            goto err;
207
5.95k
    }
208
209
    /*
210
     * 'o' will never point to '\0' at this point, the only chance
211
     * 'o' can point to '\0' is either the subsequent if or the first
212
     * else if is true.
213
     */
214
34.3k
    if (a[o] == upper_z) {
215
13.7k
        o++;
216
20.6k
    } else if (!strict && ((a[o] == plus) || (a[o] == minus))) {
217
20.2k
        int offsign = a[o] == minus ? 1 : -1;
218
20.2k
        int offset = 0;
219
220
20.2k
        o++;
221
        /*
222
         * if not equal, no need to do subsequent checks
223
         * since the following for-loop will add 'o' by 4
224
         * and the final return statement will check if 'l'
225
         * and 'o' are equal.
226
         */
227
20.2k
        if (o + 4 != l)
228
291
            goto err;
229
57.0k
        for (i = end; i < end + 2; i++) {
230
38.9k
            if (!ascii_isdigit(a[o]))
231
694
                goto err;
232
38.2k
            n = a[o] - num_zero;
233
38.2k
            o++;
234
38.2k
            if (!ascii_isdigit(a[o]))
235
123
                goto err;
236
38.1k
            n = (n * 10) + a[o] - num_zero;
237
38.1k
            i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
238
38.1k
            if ((n < min[i2]) || (n > max[i2]))
239
1.07k
                goto err;
240
            /* if tm is NULL, no need to adjust */
241
37.0k
            if (tm != NULL) {
242
37.0k
                if (i == end)
243
18.9k
                    offset = n * 3600;
244
18.0k
                else if (i == end + 1)
245
18.0k
                    offset += n * 60;
246
37.0k
            }
247
37.0k
            o++;
248
37.0k
        }
249
18.0k
        if (offset && !OPENSSL_gmtime_adj(&tmp, 0, offset * offsign))
250
742
            goto err;
251
18.0k
    } else {
252
        /* not Z, or not +/- in non-strict mode */
253
349
        goto err;
254
349
    }
255
31.0k
    if (o == l) {
256
        /* success, check if tm should be filled */
257
30.3k
        if (tm != NULL)
258
27.5k
            *tm = tmp;
259
30.3k
        return 1;
260
30.3k
    }
261
12.9k
 err:
262
12.9k
    return 0;
263
31.0k
}
264
265
ASN1_TIME *asn1_time_from_tm(ASN1_TIME *s, struct tm *ts, int type)
266
2.85k
{
267
2.85k
    char* p;
268
2.85k
    ASN1_TIME *tmps = NULL;
269
2.85k
    const size_t len = 20;
270
271
2.85k
    if (type == V_ASN1_UNDEF) {
272
0
        if (is_utc(ts->tm_year))
273
0
            type = V_ASN1_UTCTIME;
274
0
        else
275
0
            type = V_ASN1_GENERALIZEDTIME;
276
2.85k
    } else if (type == V_ASN1_UTCTIME) {
277
0
        if (!is_utc(ts->tm_year))
278
0
            goto err;
279
2.85k
    } else if (type != V_ASN1_GENERALIZEDTIME) {
280
0
        goto err;
281
0
    }
282
283
2.85k
    if (s == NULL)
284
0
        tmps = ASN1_STRING_new();
285
2.85k
    else
286
2.85k
        tmps = s;
287
2.85k
    if (tmps == NULL)
288
0
        return NULL;
289
290
2.85k
    if (!ASN1_STRING_set(tmps, NULL, len))
291
0
        goto err;
292
293
2.85k
    tmps->type = type;
294
2.85k
    p = (char*)tmps->data;
295
296
2.85k
    if (type == V_ASN1_GENERALIZEDTIME)
297
2.85k
        tmps->length = BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ",
298
2.85k
                                    ts->tm_year + 1900, ts->tm_mon + 1,
299
2.85k
                                    ts->tm_mday, ts->tm_hour, ts->tm_min,
300
2.85k
                                    ts->tm_sec);
301
0
    else
302
0
        tmps->length = BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ",
303
0
                                    ts->tm_year % 100, ts->tm_mon + 1,
304
0
                                    ts->tm_mday, ts->tm_hour, ts->tm_min,
305
0
                                    ts->tm_sec);
306
307
#ifdef CHARSET_EBCDIC
308
    ebcdic2ascii(tmps->data, tmps->data, tmps->length);
309
#endif
310
2.85k
    return tmps;
311
0
 err:
312
0
    if (tmps != s)
313
0
        ASN1_STRING_free(tmps);
314
0
    return NULL;
315
2.85k
}
316
317
ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
318
0
{
319
0
    return ASN1_TIME_adj(s, t, 0, 0);
320
0
}
321
322
ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
323
                         int offset_day, long offset_sec)
324
0
{
325
0
    struct tm *ts;
326
0
    struct tm data;
327
328
0
    ts = OPENSSL_gmtime(&t, &data);
329
0
    if (ts == NULL) {
330
0
        ASN1err(ASN1_F_ASN1_TIME_ADJ, ASN1_R_ERROR_GETTING_TIME);
331
0
        return NULL;
332
0
    }
333
0
    if (offset_day || offset_sec) {
334
0
        if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
335
0
            return NULL;
336
0
    }
337
0
    return asn1_time_from_tm(s, ts, V_ASN1_UNDEF);
338
0
}
339
340
int ASN1_TIME_check(const ASN1_TIME *t)
341
0
{
342
0
    if (t->type == V_ASN1_GENERALIZEDTIME)
343
0
        return ASN1_GENERALIZEDTIME_check(t);
344
0
    else if (t->type == V_ASN1_UTCTIME)
345
0
        return ASN1_UTCTIME_check(t);
346
0
    return 0;
347
0
}
348
349
/* Convert an ASN1_TIME structure to GeneralizedTime */
350
ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
351
                                                   ASN1_GENERALIZEDTIME **out)
352
0
{
353
0
    ASN1_GENERALIZEDTIME *ret = NULL;
354
0
    struct tm tm;
355
356
0
    if (!ASN1_TIME_to_tm(t, &tm))
357
0
        return NULL;
358
359
0
    if (out != NULL)
360
0
        ret = *out;
361
362
0
    ret = asn1_time_from_tm(ret, &tm, V_ASN1_GENERALIZEDTIME);
363
364
0
    if (out != NULL && ret != NULL)
365
0
        *out = ret;
366
367
0
    return ret;
368
0
}
369
370
int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
371
0
{
372
    /* Try UTC, if that fails, try GENERALIZED */
373
0
    if (ASN1_UTCTIME_set_string(s, str))
374
0
        return 1;
375
0
    return ASN1_GENERALIZEDTIME_set_string(s, str);
376
0
}
377
378
int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str)
379
0
{
380
0
    ASN1_TIME t;
381
0
    struct tm tm;
382
0
    int rv = 0;
383
384
0
    t.length = strlen(str);
385
0
    t.data = (unsigned char *)str;
386
0
    t.flags = ASN1_STRING_FLAG_X509_TIME;
387
388
0
    t.type = V_ASN1_UTCTIME;
389
390
0
    if (!ASN1_TIME_check(&t)) {
391
0
        t.type = V_ASN1_GENERALIZEDTIME;
392
0
        if (!ASN1_TIME_check(&t))
393
0
            goto out;
394
0
    }
395
396
    /*
397
     * Per RFC 5280 (section 4.1.2.5.), the valid input time
398
     * strings should be encoded with the following rules:
399
     *
400
     * 1. UTC: YYMMDDHHMMSSZ, if YY < 50 (20YY) --> UTC: YYMMDDHHMMSSZ
401
     * 2. UTC: YYMMDDHHMMSSZ, if YY >= 50 (19YY) --> UTC: YYMMDDHHMMSSZ
402
     * 3. G'd: YYYYMMDDHHMMSSZ, if YYYY >= 2050 --> G'd: YYYYMMDDHHMMSSZ
403
     * 4. G'd: YYYYMMDDHHMMSSZ, if YYYY < 2050 --> UTC: YYMMDDHHMMSSZ
404
     *
405
     * Only strings of the 4th rule should be reformatted, but since a
406
     * UTC can only present [1950, 2050), so if the given time string
407
     * is less than 1950 (e.g. 19230419000000Z), we do nothing...
408
     */
409
410
0
    if (s != NULL && t.type == V_ASN1_GENERALIZEDTIME) {
411
0
        if (!asn1_time_to_tm(&tm, &t))
412
0
            goto out;
413
0
        if (is_utc(tm.tm_year)) {
414
0
            t.length -= 2;
415
            /*
416
             * it's OK to let original t.data go since that's assigned
417
             * to a piece of memory allocated outside of this function.
418
             * new t.data would be freed after ASN1_STRING_copy is done.
419
             */
420
0
            t.data = OPENSSL_zalloc(t.length + 1);
421
0
            if (t.data == NULL)
422
0
                goto out;
423
0
            memcpy(t.data, str + 2, t.length);
424
0
            t.type = V_ASN1_UTCTIME;
425
0
        }
426
0
    }
427
428
0
    if (s == NULL || ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
429
0
        rv = 1;
430
431
0
    if (t.data != (unsigned char *)str)
432
0
        OPENSSL_free(t.data);
433
0
out:
434
0
    return rv;
435
0
}
436
437
int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm)
438
0
{
439
0
    if (s == NULL) {
440
0
        time_t now_t;
441
442
0
        time(&now_t);
443
0
        memset(tm, 0, sizeof(*tm));
444
0
        if (OPENSSL_gmtime(&now_t, tm) != NULL)
445
0
            return 1;
446
0
        return 0;
447
0
    }
448
449
0
    return asn1_time_to_tm(tm, s);
450
0
}
451
452
int ASN1_TIME_diff(int *pday, int *psec,
453
                   const ASN1_TIME *from, const ASN1_TIME *to)
454
0
{
455
0
    struct tm tm_from, tm_to;
456
457
0
    if (!ASN1_TIME_to_tm(from, &tm_from))
458
0
        return 0;
459
0
    if (!ASN1_TIME_to_tm(to, &tm_to))
460
0
        return 0;
461
0
    return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
462
0
}
463
464
static const char _asn1_mon[12][4] = {
465
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
466
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
467
};
468
469
int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
470
39.6k
{
471
39.6k
    char *v;
472
39.6k
    int gmt = 0, l;
473
39.6k
    struct tm stm;
474
39.6k
    const char upper_z = 0x5A, period = 0x2E;
475
476
39.6k
    if (!asn1_time_to_tm(&stm, tm)) {
477
        /* asn1_time_to_tm will check the time type */
478
12.1k
        goto err;
479
12.1k
    }
480
481
27.5k
    l = tm->length;
482
27.5k
    v = (char *)tm->data;
483
27.5k
    if (v[l - 1] == upper_z)
484
10.1k
        gmt = 1;
485
486
27.5k
    if (tm->type == V_ASN1_GENERALIZEDTIME) {
487
20.8k
        char *f = NULL;
488
20.8k
        int f_len = 0;
489
490
        /*
491
         * Try to parse fractional seconds. '14' is the place of
492
         * 'fraction point' in a GeneralizedTime string.
493
         */
494
20.8k
        if (tm->length > 15 && v[14] == period) {
495
2.87k
            f = &v[14];
496
2.87k
            f_len = 1;
497
11.4k
            while (14 + f_len < l && ascii_isdigit(f[f_len]))
498
8.61k
                ++f_len;
499
2.87k
        }
500
501
20.8k
        return BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
502
20.8k
                          _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
503
20.8k
                          stm.tm_min, stm.tm_sec, f_len, f, stm.tm_year + 1900,
504
20.8k
                          (gmt ? " GMT" : "")) > 0;
505
20.8k
    } else {
506
6.71k
        return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
507
6.71k
                          _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
508
6.71k
                          stm.tm_min, stm.tm_sec, stm.tm_year + 1900,
509
6.71k
                          (gmt ? " GMT" : "")) > 0;
510
6.71k
    }
511
12.1k
 err:
512
12.1k
    BIO_write(bp, "Bad time value", 14);
513
12.1k
    return 0;
514
27.5k
}
515
516
int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t)
517
0
{
518
0
    struct tm stm, ttm;
519
0
    int day, sec;
520
521
0
    if (!ASN1_TIME_to_tm(s, &stm))
522
0
        return -2;
523
524
0
    if (!OPENSSL_gmtime(&t, &ttm))
525
0
        return -2;
526
527
0
    if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
528
0
        return -2;
529
530
0
    if (day > 0 || sec > 0)
531
0
        return 1;
532
0
    if (day < 0 || sec < 0)
533
0
        return -1;
534
0
    return 0;
535
0
}
536
537
int ASN1_TIME_normalize(ASN1_TIME *t)
538
0
{
539
0
    struct tm tm;
540
541
0
    if (!ASN1_TIME_to_tm(t, &tm))
542
0
        return 0;
543
544
0
    return asn1_time_from_tm(t, &tm, V_ASN1_UNDEF) != NULL;
545
0
}
546
547
int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b)
548
0
{
549
0
    int day, sec;
550
551
0
    if (!ASN1_TIME_diff(&day, &sec, b, a))
552
0
        return -2;
553
0
    if (day > 0 || sec > 0)
554
0
        return 1;
555
0
    if (day < 0 || sec < 0)
556
0
        return -1;
557
0
    return 0;
558
0
}