Coverage Report

Created: 2025-07-01 06:25

/src/nss/lib/util/sectime.c
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "prtime.h"
6
#include "secder.h"
7
#include "secitem.h"
8
#include "secerr.h"
9
10
static char *DecodeUTCTime2FormattedAscii(SECItem *utcTimeDER, char *format);
11
static char *DecodeGeneralizedTime2FormattedAscii(SECItem *generalizedTimeDER, char *format);
12
13
/* convert DER utc time to ascii time string */
14
char *
15
DER_UTCTimeToAscii(SECItem *utcTime)
16
0
{
17
0
    return (DecodeUTCTime2FormattedAscii(utcTime, "%a %b %d %H:%M:%S %Y"));
18
0
}
19
20
/* convert DER utc time to ascii time string, only include day, not time */
21
char *
22
DER_UTCDayToAscii(SECItem *utctime)
23
0
{
24
0
    return (DecodeUTCTime2FormattedAscii(utctime, "%a %b %d, %Y"));
25
0
}
26
27
/* convert DER generalized time to ascii time string, only include day,
28
   not time */
29
char *
30
DER_GeneralizedDayToAscii(SECItem *gentime)
31
0
{
32
0
    return (DecodeGeneralizedTime2FormattedAscii(gentime, "%a %b %d, %Y"));
33
0
}
34
35
/* convert DER generalized or UTC time to ascii time string, only include
36
   day, not time */
37
char *
38
DER_TimeChoiceDayToAscii(SECItem *timechoice)
39
0
{
40
0
    switch (timechoice->type) {
41
42
0
        case siUTCTime:
43
0
            return DER_UTCDayToAscii(timechoice);
44
45
0
        case siGeneralizedTime:
46
0
            return DER_GeneralizedDayToAscii(timechoice);
47
48
0
        default:
49
0
            PORT_Assert(0);
50
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
51
0
            return NULL;
52
0
    }
53
0
}
54
55
char *
56
CERT_UTCTime2FormattedAscii(PRTime utcTime, char *format)
57
0
{
58
0
    PRExplodedTime printableTime;
59
0
    char *timeString;
60
61
    /* Converse time to local time and decompose it into components */
62
0
    PR_ExplodeTime(utcTime, PR_LocalTimeParameters, &printableTime);
63
64
0
    timeString = (char *)PORT_Alloc(256);
65
66
0
    if (timeString) {
67
0
        if (!PR_FormatTime(timeString, 256, format, &printableTime)) {
68
0
            PORT_Free(timeString);
69
0
            timeString = NULL;
70
0
        }
71
0
    }
72
73
0
    return (timeString);
74
0
}
75
76
char *
77
CERT_GenTime2FormattedAscii(PRTime genTime, char *format)
78
0
{
79
0
    PRExplodedTime printableTime;
80
0
    char *timeString;
81
82
    /* Decompose time into components */
83
0
    PR_ExplodeTime(genTime, PR_GMTParameters, &printableTime);
84
85
0
    timeString = (char *)PORT_Alloc(256);
86
87
0
    if (timeString) {
88
0
        if (!PR_FormatTime(timeString, 256, format, &printableTime)) {
89
0
            PORT_Free(timeString);
90
0
            timeString = NULL;
91
0
            PORT_SetError(SEC_ERROR_OUTPUT_LEN);
92
0
        }
93
0
    }
94
95
0
    return (timeString);
96
0
}
97
98
/* convert DER utc time to ascii time string, The format of the time string
99
   depends on the input "format"
100
 */
101
static char *
102
DecodeUTCTime2FormattedAscii(SECItem *utcTimeDER, char *format)
103
0
{
104
0
    PRTime utcTime;
105
0
    int rv;
106
107
0
    rv = DER_UTCTimeToTime(&utcTime, utcTimeDER);
108
0
    if (rv) {
109
0
        return (NULL);
110
0
    }
111
0
    return (CERT_UTCTime2FormattedAscii(utcTime, format));
112
0
}
113
114
/* convert DER utc time to ascii time string, The format of the time string
115
   depends on the input "format"
116
 */
117
static char *
118
DecodeGeneralizedTime2FormattedAscii(SECItem *generalizedTimeDER, char *format)
119
0
{
120
0
    PRTime generalizedTime;
121
0
    int rv;
122
123
0
    rv = DER_GeneralizedTimeToTime(&generalizedTime, generalizedTimeDER);
124
0
    if (rv) {
125
0
        return (NULL);
126
0
    }
127
0
    return (CERT_GeneralizedTime2FormattedAscii(generalizedTime, format));
128
0
}
129
130
/* decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME
131
   or a SEC_ASN1_UTC_TIME */
132
133
SECStatus
134
DER_DecodeTimeChoice(PRTime *output, const SECItem *input)
135
0
{
136
0
    switch (input->type) {
137
0
        case siGeneralizedTime:
138
0
            return DER_GeneralizedTimeToTime(output, input);
139
140
0
        case siUTCTime:
141
0
            return DER_UTCTimeToTime(output, input);
142
143
0
        default:
144
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
145
0
            PORT_Assert(0);
146
0
            return SECFailure;
147
0
    }
148
0
}
149
150
/* encode a PRTime to an ASN.1 DER SECItem containing either a
151
   SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */
152
153
SECStatus
154
DER_EncodeTimeChoice(PLArenaPool *arena, SECItem *output, PRTime input)
155
0
{
156
0
    SECStatus rv;
157
158
0
    rv = DER_TimeToUTCTimeArena(arena, output, input);
159
0
    if (rv == SECSuccess || PORT_GetError() != SEC_ERROR_INVALID_ARGS) {
160
0
        return rv;
161
0
    }
162
0
    return DER_TimeToGeneralizedTimeArena(arena, output, input);
163
0
}