Coverage Report

Created: 2026-08-31 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/x509/v3_timespec.c
Line
Count
Source
1
/*
2
 * Copyright 2024 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
#include <stdio.h>
11
#include <openssl/asn1t.h>
12
#include <openssl/x509v3.h>
13
#include <crypto/asn1.h>
14
#include "ext_dat.h"
15
16
static const char *const WEEKDAY_NAMES[7] = {
17
    "SUN",
18
    "MON",
19
    "TUE",
20
    "WED",
21
    "THU",
22
    "FRI",
23
    "SAT"
24
};
25
26
static const char *const WEEK_NAMES[5] = {
27
    "first",
28
    "second",
29
    "third",
30
    "fourth",
31
    "final"
32
};
33
34
static const char *const MONTH_NAMES[12] = {
35
    "JAN",
36
    "FEB",
37
    "MAR",
38
    "APR",
39
    "MAY",
40
    "JUN",
41
    "JUL",
42
    "AUG",
43
    "SEPT",
44
    "OCT",
45
    "NOV",
46
    "DEC"
47
};
48
49
ASN1_SEQUENCE(OSSL_TIME_SPEC_ABSOLUTE) = {
50
    ASN1_EXP_OPT(OSSL_TIME_SPEC_ABSOLUTE, startTime, ASN1_GENERALIZEDTIME, 0),
51
    ASN1_EXP_OPT(OSSL_TIME_SPEC_ABSOLUTE, endTime, ASN1_GENERALIZEDTIME, 1),
52
0
} ASN1_SEQUENCE_END(OSSL_TIME_SPEC_ABSOLUTE)
53
0
54
0
ASN1_SEQUENCE(OSSL_DAY_TIME) = {
55
0
    ASN1_EXP_OPT(OSSL_DAY_TIME, hour, ASN1_INTEGER, 0),
56
0
    ASN1_EXP_OPT(OSSL_DAY_TIME, minute, ASN1_INTEGER, 1),
57
0
    ASN1_EXP_OPT(OSSL_DAY_TIME, second, ASN1_INTEGER, 2),
58
0
} ASN1_SEQUENCE_END(OSSL_DAY_TIME)
59
0
60
0
ASN1_SEQUENCE(OSSL_DAY_TIME_BAND) = {
61
0
    ASN1_EXP_OPT(OSSL_DAY_TIME_BAND, startDayTime, OSSL_DAY_TIME, 0),
62
0
    ASN1_EXP_OPT(OSSL_DAY_TIME_BAND, endDayTime, OSSL_DAY_TIME, 1),
63
0
} ASN1_SEQUENCE_END(OSSL_DAY_TIME_BAND)
64
0
65
0
ASN1_CHOICE(OSSL_NAMED_DAY) = {
66
0
    ASN1_SET_OF(OSSL_NAMED_DAY, choice.intNamedDays, ASN1_ENUMERATED),
67
0
    ASN1_SIMPLE(OSSL_NAMED_DAY, choice.bitNamedDays, ASN1_BIT_STRING),
68
0
} ASN1_CHOICE_END(OSSL_NAMED_DAY)
69
0
70
0
ASN1_CHOICE(OSSL_TIME_SPEC_X_DAY_OF) = {
71
0
    ASN1_EXP(OSSL_TIME_SPEC_X_DAY_OF, choice.first, OSSL_NAMED_DAY, 1),
72
0
    ASN1_EXP(OSSL_TIME_SPEC_X_DAY_OF, choice.second, OSSL_NAMED_DAY, 2),
73
0
    ASN1_EXP(OSSL_TIME_SPEC_X_DAY_OF, choice.third, OSSL_NAMED_DAY, 3),
74
0
    ASN1_EXP(OSSL_TIME_SPEC_X_DAY_OF, choice.fourth, OSSL_NAMED_DAY, 4),
75
0
    ASN1_EXP(OSSL_TIME_SPEC_X_DAY_OF, choice.fifth, OSSL_NAMED_DAY, 5),
76
0
} ASN1_CHOICE_END(OSSL_TIME_SPEC_X_DAY_OF)
77
0
78
0
ASN1_CHOICE(OSSL_TIME_SPEC_DAY) = {
79
0
    ASN1_SET_OF(OSSL_TIME_SPEC_DAY, choice.intDay, ASN1_INTEGER),
80
0
    ASN1_SIMPLE(OSSL_TIME_SPEC_DAY, choice.bitDay, ASN1_BIT_STRING),
81
0
    ASN1_SIMPLE(OSSL_TIME_SPEC_DAY, choice.dayOf, OSSL_TIME_SPEC_X_DAY_OF),
82
0
} ASN1_CHOICE_END(OSSL_TIME_SPEC_DAY)
83
0
84
0
ASN1_CHOICE(OSSL_TIME_SPEC_WEEKS) = {
85
0
    ASN1_SIMPLE(OSSL_TIME_SPEC_WEEKS, choice.allWeeks, ASN1_NULL),
86
0
    ASN1_SET_OF(OSSL_TIME_SPEC_WEEKS, choice.intWeek, ASN1_INTEGER),
87
0
    ASN1_SIMPLE(OSSL_TIME_SPEC_WEEKS, choice.bitWeek, ASN1_BIT_STRING),
88
0
} ASN1_CHOICE_END(OSSL_TIME_SPEC_WEEKS)
89
0
90
0
ASN1_CHOICE(OSSL_TIME_SPEC_MONTH) = {
91
0
    ASN1_SIMPLE(OSSL_TIME_SPEC_MONTH, choice.allMonths, ASN1_NULL),
92
0
    ASN1_SET_OF(OSSL_TIME_SPEC_MONTH, choice.intMonth, ASN1_INTEGER),
93
0
    ASN1_SIMPLE(OSSL_TIME_SPEC_MONTH, choice.bitMonth, ASN1_BIT_STRING),
94
0
} ASN1_CHOICE_END(OSSL_TIME_SPEC_MONTH)
95
0
96
0
ASN1_SEQUENCE(OSSL_TIME_PERIOD) = {
97
0
    ASN1_EXP_SET_OF_OPT(OSSL_TIME_PERIOD, timesOfDay, OSSL_DAY_TIME_BAND, 0),
98
0
    ASN1_EXP_OPT(OSSL_TIME_PERIOD, days, OSSL_TIME_SPEC_DAY, 1),
99
0
    ASN1_EXP_OPT(OSSL_TIME_PERIOD, weeks, OSSL_TIME_SPEC_WEEKS, 2),
100
0
    ASN1_EXP_OPT(OSSL_TIME_PERIOD, months, OSSL_TIME_SPEC_MONTH, 3),
101
0
    ASN1_EXP_SET_OF_OPT(OSSL_TIME_PERIOD, years, ASN1_INTEGER, 4),
102
0
} ASN1_SEQUENCE_END(OSSL_TIME_PERIOD)
103
0
104
0
ASN1_CHOICE(OSSL_TIME_SPEC_TIME) = {
105
0
    ASN1_SIMPLE(OSSL_TIME_SPEC_TIME, choice.absolute, OSSL_TIME_SPEC_ABSOLUTE),
106
0
    ASN1_SET_OF(OSSL_TIME_SPEC_TIME, choice.periodic, OSSL_TIME_PERIOD)
107
0
} ASN1_CHOICE_END(OSSL_TIME_SPEC_TIME)
108
0
109
0
ASN1_SEQUENCE(OSSL_TIME_SPEC) = {
110
0
    ASN1_SIMPLE(OSSL_TIME_SPEC, time, OSSL_TIME_SPEC_TIME),
111
0
    ASN1_OPT(OSSL_TIME_SPEC, notThisTime, ASN1_FBOOLEAN),
112
0
    ASN1_OPT(OSSL_TIME_SPEC, timeZone, ASN1_INTEGER),
113
0
} ASN1_SEQUENCE_END(OSSL_TIME_SPEC)
114
0
115
0
IMPLEMENT_ASN1_FUNCTIONS(OSSL_DAY_TIME)
116
0
IMPLEMENT_ASN1_FUNCTIONS(OSSL_DAY_TIME_BAND)
117
0
IMPLEMENT_ASN1_FUNCTIONS(OSSL_TIME_SPEC_DAY)
118
0
IMPLEMENT_ASN1_FUNCTIONS(OSSL_TIME_SPEC_WEEKS)
119
0
IMPLEMENT_ASN1_FUNCTIONS(OSSL_TIME_SPEC_MONTH)
120
0
IMPLEMENT_ASN1_FUNCTIONS(OSSL_NAMED_DAY)
121
0
IMPLEMENT_ASN1_FUNCTIONS(OSSL_TIME_SPEC_X_DAY_OF)
122
0
IMPLEMENT_ASN1_FUNCTIONS(OSSL_TIME_SPEC_ABSOLUTE)
123
0
IMPLEMENT_ASN1_FUNCTIONS(OSSL_TIME_SPEC_TIME)
124
0
IMPLEMENT_ASN1_FUNCTIONS(OSSL_TIME_SPEC)
125
0
IMPLEMENT_ASN1_FUNCTIONS(OSSL_TIME_PERIOD)
126
0
127
0
static int i2r_OSSL_TIME_SPEC_ABSOLUTE(X509V3_EXT_METHOD *method,
128
0
    OSSL_TIME_SPEC_ABSOLUTE *time,
129
0
    BIO *out, int indent)
130
0
{
131
0
    if (time->startTime != NULL && time->endTime != NULL) {
132
0
        if (!BIO_puts(out, "Any time between "))
133
0
            return 0;
134
0
        if (!ossl_asn1_time_print_ex(out, time->startTime, 0))
135
0
            return 0;
136
0
        if (!BIO_puts(out, " and "))
137
0
            return 0;
138
0
        if (!ossl_asn1_time_print_ex(out, time->endTime, 0))
139
0
            return 0;
140
0
    } else if (time->startTime != NULL) {
141
0
        if (!BIO_puts(out, "Any time after "))
142
0
            return 0;
143
0
        if (!ossl_asn1_time_print_ex(out, time->startTime, 0))
144
0
            return 0;
145
0
        if (BIO_printf(out, "%.*s", time->startTime->length,
146
0
                time->startTime->length
147
0
                    ? time->startTime->data
148
0
                    : (const unsigned char *)"")
149
0
            <= 0)
150
0
            return 0;
151
0
    } else if (time->endTime != NULL) {
152
0
        if (!BIO_puts(out, "Any time until "))
153
0
            return 0;
154
0
        if (!ossl_asn1_time_print_ex(out, time->endTime, 0))
155
0
            return 0;
156
0
    } else { /* Invalid: there must be SOME time specified. */
157
0
        return BIO_puts(out, "INVALID (EMPTY)");
158
0
    }
159
0
    return 1;
160
0
}
161
162
static int i2r_OSSL_DAY_TIME(X509V3_EXT_METHOD *method,
163
    OSSL_DAY_TIME *dt,
164
    BIO *out, int indent)
165
0
{
166
0
    int64_t h = 0;
167
0
    int64_t m = 0;
168
0
    int64_t s = 0;
169
170
0
    if (!dt->hour || !ASN1_INTEGER_get_int64(&h, dt->hour))
171
0
        return 0;
172
0
    if (dt->minute && !ASN1_INTEGER_get_int64(&m, dt->minute))
173
0
        return 0;
174
0
    if (dt->second && !ASN1_INTEGER_get_int64(&s, dt->second))
175
0
        return 0;
176
0
    return BIO_printf(out, "%02lld:%02lld:%02lld",
177
0
               (long long int)h, (long long int)m, (long long int)s)
178
0
        > 0;
179
0
}
180
181
static int i2r_OSSL_DAY_TIME_BAND(X509V3_EXT_METHOD *method,
182
    OSSL_DAY_TIME_BAND *band,
183
    BIO *out, int indent)
184
0
{
185
0
    if (band->startDayTime) {
186
0
        if (!i2r_OSSL_DAY_TIME(method, band->startDayTime, out, indent))
187
0
            return 0;
188
0
    } else if (!BIO_puts(out, "00:00:00")) {
189
0
        return 0;
190
0
    }
191
0
    if (!BIO_puts(out, " - "))
192
0
        return 0;
193
0
    if (band->endDayTime) {
194
0
        if (!i2r_OSSL_DAY_TIME(method, band->endDayTime, out, indent))
195
0
            return 0;
196
0
    } else if (!BIO_puts(out, "23:59:59")) {
197
0
        return 0;
198
0
    }
199
0
    return 1;
200
0
}
201
202
static int print_int_month(BIO *out, int64_t month)
203
0
{
204
0
    switch (month) {
205
0
    case (OSSL_TIME_SPEC_INT_MONTH_JAN):
206
0
        return BIO_puts(out, "JAN");
207
0
    case (OSSL_TIME_SPEC_INT_MONTH_FEB):
208
0
        return BIO_puts(out, "FEB");
209
0
    case (OSSL_TIME_SPEC_INT_MONTH_MAR):
210
0
        return BIO_puts(out, "MAR");
211
0
    case (OSSL_TIME_SPEC_INT_MONTH_APR):
212
0
        return BIO_puts(out, "APR");
213
0
    case (OSSL_TIME_SPEC_INT_MONTH_MAY):
214
0
        return BIO_puts(out, "MAY");
215
0
    case (OSSL_TIME_SPEC_INT_MONTH_JUN):
216
0
        return BIO_puts(out, "JUN");
217
0
    case (OSSL_TIME_SPEC_INT_MONTH_JUL):
218
0
        return BIO_puts(out, "JUL");
219
0
    case (OSSL_TIME_SPEC_INT_MONTH_AUG):
220
0
        return BIO_puts(out, "AUG");
221
0
    case (OSSL_TIME_SPEC_INT_MONTH_SEP):
222
0
        return BIO_puts(out, "SEP");
223
0
    case (OSSL_TIME_SPEC_INT_MONTH_OCT):
224
0
        return BIO_puts(out, "OCT");
225
0
    case (OSSL_TIME_SPEC_INT_MONTH_NOV):
226
0
        return BIO_puts(out, "NOV");
227
0
    case (OSSL_TIME_SPEC_INT_MONTH_DEC):
228
0
        return BIO_puts(out, "DEC");
229
0
    default:
230
0
        return 0;
231
0
    }
232
0
    return 0;
233
0
}
234
235
static int print_bit_month(BIO *out, ASN1_BIT_STRING *bs)
236
0
{
237
0
    int i = OSSL_TIME_SPEC_BIT_MONTH_JAN;
238
0
    int j = 0;
239
240
0
    for (; i <= OSSL_TIME_SPEC_BIT_MONTH_DEC; i++) {
241
0
        if (ASN1_BIT_STRING_get_bit(bs, i)) {
242
0
            if (j > 0 && !BIO_puts(out, ", "))
243
0
                return 0;
244
0
            j++;
245
0
            if (!BIO_puts(out, MONTH_NAMES[i]))
246
0
                return 0;
247
0
        }
248
0
    }
249
0
    return 1;
250
0
}
251
252
/*
253
 * It might seem like you could just print the bits of the string numerically,
254
 * but the fifth bit has the special meaning of "the final week" imputed to it
255
 * by the text of ITU-T Recommendation X.520.
256
 */
257
static int print_bit_week(BIO *out, ASN1_BIT_STRING *bs)
258
0
{
259
0
    int i = OSSL_TIME_SPEC_BIT_WEEKS_1;
260
0
    int j = 0;
261
262
0
    for (; i <= OSSL_TIME_SPEC_BIT_WEEKS_5; i++) {
263
0
        if (ASN1_BIT_STRING_get_bit(bs, i)) {
264
0
            if (j > 0 && !BIO_puts(out, ", "))
265
0
                return 0;
266
0
            j++;
267
0
            if (!BIO_puts(out, WEEK_NAMES[i]))
268
0
                return 0;
269
0
        }
270
0
    }
271
0
    return 1;
272
0
}
273
274
static int print_day_of_week(BIO *out, ASN1_BIT_STRING *bs)
275
0
{
276
0
    int i = OSSL_TIME_SPEC_DAY_BIT_SUN;
277
0
    int j = 0;
278
279
0
    for (; i <= OSSL_TIME_SPEC_DAY_BIT_SAT; i++) {
280
0
        if (ASN1_BIT_STRING_get_bit(bs, i)) {
281
0
            if (j > 0 && !BIO_puts(out, ", "))
282
0
                return 0;
283
0
            j++;
284
0
            if (!BIO_puts(out, WEEKDAY_NAMES[i]))
285
0
                return 0;
286
0
        }
287
0
    }
288
0
    return 1;
289
0
}
290
291
static int print_int_day_of_week(BIO *out, int64_t dow)
292
0
{
293
0
    switch (dow) {
294
0
    case (OSSL_TIME_SPEC_DAY_INT_SUN):
295
0
        return BIO_puts(out, "SUN");
296
0
    case (OSSL_TIME_SPEC_DAY_INT_MON):
297
0
        return BIO_puts(out, "MON");
298
0
    case (OSSL_TIME_SPEC_DAY_INT_TUE):
299
0
        return BIO_puts(out, "TUE");
300
0
    case (OSSL_TIME_SPEC_DAY_INT_WED):
301
0
        return BIO_puts(out, "WED");
302
0
    case (OSSL_TIME_SPEC_DAY_INT_THU):
303
0
        return BIO_puts(out, "THU");
304
0
    case (OSSL_TIME_SPEC_DAY_INT_FRI):
305
0
        return BIO_puts(out, "FRI");
306
0
    case (OSSL_TIME_SPEC_DAY_INT_SAT):
307
0
        return BIO_puts(out, "SAT");
308
0
    default:
309
0
        return 0;
310
0
    }
311
0
    return 0;
312
0
}
313
314
static int print_int_named_day(BIO *out, int64_t nd)
315
0
{
316
0
    switch (nd) {
317
0
    case (OSSL_NAMED_DAY_INT_SUN):
318
0
        return BIO_puts(out, "SUN");
319
0
    case (OSSL_NAMED_DAY_INT_MON):
320
0
        return BIO_puts(out, "MON");
321
0
    case (OSSL_NAMED_DAY_INT_TUE):
322
0
        return BIO_puts(out, "TUE");
323
0
    case (OSSL_NAMED_DAY_INT_WED):
324
0
        return BIO_puts(out, "WED");
325
0
    case (OSSL_NAMED_DAY_INT_THU):
326
0
        return BIO_puts(out, "THU");
327
0
    case (OSSL_NAMED_DAY_INT_FRI):
328
0
        return BIO_puts(out, "FRI");
329
0
    case (OSSL_NAMED_DAY_INT_SAT):
330
0
        return BIO_puts(out, "SAT");
331
0
    default:
332
0
        return 0;
333
0
    }
334
0
    return 0;
335
0
}
336
337
static int print_bit_named_day(BIO *out, ASN1_BIT_STRING *bs)
338
0
{
339
0
    return print_day_of_week(out, bs);
340
0
}
341
342
static int i2r_OSSL_PERIOD(X509V3_EXT_METHOD *method,
343
    OSSL_TIME_PERIOD *p,
344
    BIO *out, int indent)
345
0
{
346
0
    int i;
347
0
    OSSL_DAY_TIME_BAND *band;
348
0
    ASN1_INTEGER *big_val;
349
0
    int64_t small_val;
350
0
    OSSL_NAMED_DAY *nd;
351
352
0
    if (BIO_printf(out, "%*sPeriod:\n", indent, "") <= 0)
353
0
        return 0;
354
0
    if (p->timesOfDay) {
355
0
        if (BIO_printf(out, "%*sDaytime bands:\n", indent + 4, "") <= 0)
356
0
            return 0;
357
0
        for (i = 0; i < sk_OSSL_DAY_TIME_BAND_num(p->timesOfDay); i++) {
358
0
            band = sk_OSSL_DAY_TIME_BAND_value(p->timesOfDay, i);
359
0
            if (BIO_printf(out, "%*s", indent + 8, "") <= 0)
360
0
                return 0;
361
0
            if (!i2r_OSSL_DAY_TIME_BAND(method, band, out, indent + 8))
362
0
                return 0;
363
0
            if (!BIO_puts(out, "\n"))
364
0
                return 0;
365
0
        }
366
0
    }
367
0
    if (p->days) {
368
0
        if (p->days->type == OSSL_TIME_SPEC_DAY_TYPE_INT) {
369
0
            if (p->weeks != NULL) {
370
0
                if (BIO_printf(out, "%*sDays of the week: ", indent + 4, "") <= 0)
371
0
                    return 0;
372
0
            } else if (p->months != NULL) {
373
0
                if (BIO_printf(out, "%*sDays of the month: ", indent + 4, "") <= 0)
374
0
                    return 0;
375
0
            } else if (p->years != NULL) {
376
0
                if (BIO_printf(out, "%*sDays of the year: ", indent + 4, "") <= 0)
377
0
                    return 0;
378
0
            }
379
0
        } else {
380
0
            if (BIO_printf(out, "%*sDays: ", indent + 4, "") <= 0)
381
0
                return 0;
382
0
        }
383
384
0
        switch (p->days->type) {
385
0
        case (OSSL_TIME_SPEC_DAY_TYPE_INT):
386
0
            for (i = 0; i < sk_ASN1_INTEGER_num(p->days->choice.intDay); i++) {
387
0
                big_val = sk_ASN1_INTEGER_value(p->days->choice.intDay, i);
388
0
                if (!ASN1_INTEGER_get_int64(&small_val, big_val))
389
0
                    return 0;
390
0
                if (i > 0 && !BIO_puts(out, ", "))
391
0
                    return 0;
392
                /* If weeks is defined, then print day of week by name. */
393
0
                if (p->weeks != NULL) {
394
0
                    if (!print_int_day_of_week(out, small_val))
395
0
                        return 0;
396
0
                } else if (BIO_printf(out, "%lld", (long long int)small_val) <= 0) {
397
0
                    return 0;
398
0
                }
399
0
            }
400
0
            break;
401
0
        case (OSSL_TIME_SPEC_DAY_TYPE_BIT):
402
0
            if (!print_day_of_week(out, p->days->choice.bitDay))
403
0
                return 0;
404
0
            break;
405
0
        case (OSSL_TIME_SPEC_DAY_TYPE_DAY_OF):
406
0
            switch (p->days->choice.dayOf->type) {
407
0
            case (OSSL_TIME_SPEC_X_DAY_OF_FIRST):
408
0
                if (!BIO_puts(out, "FIRST "))
409
0
                    return 0;
410
0
                nd = p->days->choice.dayOf->choice.first;
411
0
                break;
412
0
            case (OSSL_TIME_SPEC_X_DAY_OF_SECOND):
413
0
                if (!BIO_puts(out, "SECOND "))
414
0
                    return 0;
415
0
                nd = p->days->choice.dayOf->choice.second;
416
0
                break;
417
0
            case (OSSL_TIME_SPEC_X_DAY_OF_THIRD):
418
0
                if (!BIO_puts(out, "THIRD "))
419
0
                    return 0;
420
0
                nd = p->days->choice.dayOf->choice.third;
421
0
                break;
422
0
            case (OSSL_TIME_SPEC_X_DAY_OF_FOURTH):
423
0
                if (!BIO_puts(out, "FOURTH "))
424
0
                    return 0;
425
0
                nd = p->days->choice.dayOf->choice.fourth;
426
0
                break;
427
0
            case (OSSL_TIME_SPEC_X_DAY_OF_FIFTH):
428
0
                if (!BIO_puts(out, "FIFTH "))
429
0
                    return 0;
430
0
                nd = p->days->choice.dayOf->choice.fifth;
431
0
                break;
432
0
            default:
433
0
                return 0;
434
0
            }
435
0
            switch (nd->type) {
436
0
            case (OSSL_NAMED_DAY_TYPE_INT):
437
0
                if (!ASN1_INTEGER_get_int64(&small_val, nd->choice.intNamedDays))
438
0
                    return 0;
439
0
                if (!print_int_named_day(out, small_val))
440
0
                    return 0;
441
0
                break;
442
0
            case (OSSL_NAMED_DAY_TYPE_BIT):
443
0
                if (!print_bit_named_day(out, nd->choice.bitNamedDays))
444
0
                    return 0;
445
0
                break;
446
0
            default:
447
0
                return 0;
448
0
            }
449
0
            break;
450
0
        default:
451
0
            return 0;
452
0
        }
453
0
        if (!BIO_puts(out, "\n"))
454
0
            return 0;
455
0
    }
456
0
    if (p->weeks) {
457
0
        if (p->weeks->type == OSSL_TIME_SPEC_WEEKS_TYPE_INT) {
458
0
            if (p->months != NULL) {
459
0
                if (BIO_printf(out, "%*sWeeks of the month: ", indent + 4, "") <= 0)
460
0
                    return 0;
461
0
            } else if (p->years != NULL) {
462
0
                if (BIO_printf(out, "%*sWeeks of the year: ", indent + 4, "") <= 0)
463
0
                    return 0;
464
0
            }
465
0
        } else {
466
0
            if (BIO_printf(out, "%*sWeeks: ", indent + 4, "") <= 0)
467
0
                return 0;
468
0
        }
469
470
0
        switch (p->weeks->type) {
471
0
        case (OSSL_TIME_SPEC_WEEKS_TYPE_ALL):
472
0
            if (!BIO_puts(out, "ALL"))
473
0
                return 0;
474
0
            break;
475
0
        case (OSSL_TIME_SPEC_WEEKS_TYPE_INT):
476
0
            for (i = 0; i < sk_ASN1_INTEGER_num(p->weeks->choice.intWeek); i++) {
477
0
                big_val = sk_ASN1_INTEGER_value(p->weeks->choice.intWeek, i);
478
0
                if (!ASN1_INTEGER_get_int64(&small_val, big_val))
479
0
                    return 0;
480
0
                if (i > 0 && !BIO_puts(out, ", "))
481
0
                    return 0;
482
0
                if (!BIO_printf(out, "%lld", (long long int)small_val))
483
0
                    return 0;
484
0
            }
485
0
            break;
486
0
        case (OSSL_TIME_SPEC_WEEKS_TYPE_BIT):
487
0
            if (!print_bit_week(out, p->weeks->choice.bitWeek))
488
0
                return 0;
489
0
            break;
490
0
        default:
491
0
            return 0;
492
0
        }
493
0
        if (!BIO_puts(out, "\n"))
494
0
            return 0;
495
0
    }
496
0
    if (p->months) {
497
0
        if (BIO_printf(out, "%*sMonths: ", indent + 4, "") <= 0)
498
0
            return 0;
499
0
        switch (p->months->type) {
500
0
        case (OSSL_TIME_SPEC_MONTH_TYPE_ALL):
501
0
            if (!BIO_puts(out, "ALL"))
502
0
                return 0;
503
0
            break;
504
0
        case (OSSL_TIME_SPEC_MONTH_TYPE_INT):
505
0
            for (i = 0; i < sk_ASN1_INTEGER_num(p->months->choice.intMonth); i++) {
506
0
                big_val = sk_ASN1_INTEGER_value(p->months->choice.intMonth, i);
507
0
                if (!ASN1_INTEGER_get_int64(&small_val, big_val))
508
0
                    return 0;
509
0
                if (i > 0 && !BIO_puts(out, ", "))
510
0
                    return 0;
511
0
                if (!print_int_month(out, small_val))
512
0
                    return 0;
513
0
            }
514
0
            break;
515
0
        case (OSSL_TIME_SPEC_MONTH_TYPE_BIT):
516
0
            if (!print_bit_month(out, p->months->choice.bitMonth))
517
0
                return 0;
518
0
            break;
519
0
        default:
520
0
            return 0;
521
0
        }
522
0
        if (!BIO_puts(out, "\n"))
523
0
            return 0;
524
0
    }
525
0
    if (p->years) {
526
0
        if (BIO_printf(out, "%*sYears: ", indent + 4, "") <= 0)
527
0
            return 0;
528
0
        for (i = 0; i < sk_ASN1_INTEGER_num(p->years); i++) {
529
0
            big_val = sk_ASN1_INTEGER_value(p->years, i);
530
0
            if (!ASN1_INTEGER_get_int64(&small_val, big_val))
531
0
                return 0;
532
0
            if (i > 0 && !BIO_puts(out, ", "))
533
0
                return 0;
534
0
            if (BIO_printf(out, "%04lld", (long long int)small_val) <= 0)
535
0
                return 0;
536
0
        }
537
0
    }
538
0
    return 1;
539
0
}
540
541
static int i2r_OSSL_TIME_SPEC_TIME(X509V3_EXT_METHOD *method,
542
    OSSL_TIME_SPEC_TIME *time,
543
    BIO *out, int indent)
544
0
{
545
0
    OSSL_TIME_PERIOD *tp;
546
0
    int i;
547
548
0
    switch (time->type) {
549
0
    case (OSSL_TIME_SPEC_TIME_TYPE_ABSOLUTE):
550
0
        if (BIO_printf(out, "%*sAbsolute: ", indent, "") <= 0)
551
0
            return 0;
552
0
        if (i2r_OSSL_TIME_SPEC_ABSOLUTE(method, time->choice.absolute, out, indent + 4) <= 0)
553
0
            return 0;
554
0
        return BIO_puts(out, "\n");
555
0
    case (OSSL_TIME_SPEC_TIME_TYPE_PERIODIC):
556
0
        if (BIO_printf(out, "%*sPeriodic:\n", indent, "") <= 0)
557
0
            return 0;
558
0
        for (i = 0; i < sk_OSSL_TIME_PERIOD_num(time->choice.periodic); i++) {
559
0
            if (i > 0 && !BIO_puts(out, "\n"))
560
0
                return 0;
561
0
            tp = sk_OSSL_TIME_PERIOD_value(time->choice.periodic, i);
562
0
            if (!i2r_OSSL_PERIOD(method, tp, out, indent + 4))
563
0
                return 0;
564
0
        }
565
0
        return BIO_puts(out, "\n");
566
0
    default:
567
0
        return 0;
568
0
    }
569
0
    return 0;
570
0
}
571
572
static int i2r_OSSL_TIME_SPEC(X509V3_EXT_METHOD *method,
573
    OSSL_TIME_SPEC *time,
574
    BIO *out, int indent)
575
0
{
576
0
    int64_t tz;
577
578
0
    if (time->timeZone) {
579
0
        if (ASN1_INTEGER_get_int64(&tz, time->timeZone) != 1)
580
0
            return 0;
581
0
        if (BIO_printf(out, "%*sTimezone: UTC%+03lld:00\n", indent, "", (long long int)tz) <= 0)
582
0
            return 0;
583
0
    }
584
0
    if (time->notThisTime > 0) {
585
0
        if (BIO_printf(out, "%*sNOT this time:\n", indent, "") <= 0)
586
0
            return 0;
587
0
    } else if (BIO_printf(out, "%*sTime:\n", indent, "") <= 0) {
588
0
        return 0;
589
0
    }
590
0
    return i2r_OSSL_TIME_SPEC_TIME(method, time->time, out, indent + 4);
591
0
}
592
593
const X509V3_EXT_METHOD ossl_v3_time_specification = {
594
    NID_time_specification, X509V3_EXT_MULTILINE,
595
    ASN1_ITEM_ref(OSSL_TIME_SPEC),
596
    0, 0, 0, 0,
597
    0, 0,
598
    0,
599
    0,
600
    (X509V3_EXT_I2R)i2r_OSSL_TIME_SPEC,
601
    NULL,
602
    NULL
603
};