|
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 { |
|
17 return (DecodeUTCTime2FormattedAscii (utcTime, "%a %b %d %H:%M:%S %Y")); |
|
18 } |
|
19 |
|
20 /* convert DER utc time to ascii time string, only include day, not time */ |
|
21 char * |
|
22 DER_UTCDayToAscii(SECItem *utctime) |
|
23 { |
|
24 return (DecodeUTCTime2FormattedAscii (utctime, "%a %b %d, %Y")); |
|
25 } |
|
26 |
|
27 /* convert DER generalized time to ascii time string, only include day, |
|
28 not time */ |
|
29 char * |
|
30 DER_GeneralizedDayToAscii(SECItem *gentime) |
|
31 { |
|
32 return (DecodeGeneralizedTime2FormattedAscii (gentime, "%a %b %d, %Y")); |
|
33 } |
|
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 { |
|
40 switch (timechoice->type) { |
|
41 |
|
42 case siUTCTime: |
|
43 return DER_UTCDayToAscii(timechoice); |
|
44 |
|
45 case siGeneralizedTime: |
|
46 return DER_GeneralizedDayToAscii(timechoice); |
|
47 |
|
48 default: |
|
49 PORT_Assert(0); |
|
50 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
|
51 return NULL; |
|
52 } |
|
53 } |
|
54 |
|
55 char * |
|
56 CERT_UTCTime2FormattedAscii(PRTime utcTime, char *format) |
|
57 { |
|
58 PRExplodedTime printableTime; |
|
59 char *timeString; |
|
60 |
|
61 /* Converse time to local time and decompose it into components */ |
|
62 PR_ExplodeTime(utcTime, PR_LocalTimeParameters, &printableTime); |
|
63 |
|
64 timeString = (char *)PORT_Alloc(256); |
|
65 |
|
66 if ( timeString ) { |
|
67 if ( ! PR_FormatTime( timeString, 256, format, &printableTime )) { |
|
68 PORT_Free(timeString); |
|
69 timeString = NULL; |
|
70 } |
|
71 } |
|
72 |
|
73 return (timeString); |
|
74 } |
|
75 |
|
76 char *CERT_GenTime2FormattedAscii(PRTime genTime, char *format) |
|
77 { |
|
78 PRExplodedTime printableTime; |
|
79 char *timeString; |
|
80 |
|
81 /* Decompose time into components */ |
|
82 PR_ExplodeTime(genTime, PR_GMTParameters, &printableTime); |
|
83 |
|
84 timeString = (char *)PORT_Alloc(256); |
|
85 |
|
86 if ( timeString ) { |
|
87 if ( ! PR_FormatTime( timeString, 256, format, &printableTime )) { |
|
88 PORT_Free(timeString); |
|
89 timeString = NULL; |
|
90 PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
|
91 } |
|
92 } |
|
93 |
|
94 return (timeString); |
|
95 } |
|
96 |
|
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 { |
|
104 PRTime utcTime; |
|
105 int rv; |
|
106 |
|
107 rv = DER_UTCTimeToTime(&utcTime, utcTimeDER); |
|
108 if (rv) { |
|
109 return(NULL); |
|
110 } |
|
111 return (CERT_UTCTime2FormattedAscii (utcTime, format)); |
|
112 } |
|
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 { |
|
120 PRTime generalizedTime; |
|
121 int rv; |
|
122 |
|
123 rv = DER_GeneralizedTimeToTime(&generalizedTime, generalizedTimeDER); |
|
124 if (rv) { |
|
125 return(NULL); |
|
126 } |
|
127 return (CERT_GeneralizedTime2FormattedAscii (generalizedTime, format)); |
|
128 } |
|
129 |
|
130 /* decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME |
|
131 or a SEC_ASN1_UTC_TIME */ |
|
132 |
|
133 SECStatus DER_DecodeTimeChoice(PRTime* output, const SECItem* input) |
|
134 { |
|
135 switch (input->type) { |
|
136 case siGeneralizedTime: |
|
137 return DER_GeneralizedTimeToTime(output, input); |
|
138 |
|
139 case siUTCTime: |
|
140 return DER_UTCTimeToTime(output, input); |
|
141 |
|
142 default: |
|
143 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
|
144 PORT_Assert(0); |
|
145 return SECFailure; |
|
146 } |
|
147 } |
|
148 |
|
149 /* encode a PRTime to an ASN.1 DER SECItem containing either a |
|
150 SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */ |
|
151 |
|
152 SECStatus DER_EncodeTimeChoice(PLArenaPool* arena, SECItem* output, PRTime input) |
|
153 { |
|
154 SECStatus rv; |
|
155 |
|
156 rv = DER_TimeToUTCTimeArena(arena, output, input); |
|
157 if (rv == SECSuccess || PORT_GetError() != SEC_ERROR_INVALID_ARGS) { |
|
158 return rv; |
|
159 } |
|
160 return DER_TimeToGeneralizedTimeArena(arena, output, input); |
|
161 } |