Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
5 #include "prtime.h"
6 #include "secder.h"
7 #include "secitem.h"
8 #include "secerr.h"
10 static char *DecodeUTCTime2FormattedAscii (SECItem *utcTimeDER, char *format);
11 static char *DecodeGeneralizedTime2FormattedAscii (SECItem *generalizedTimeDER, char *format);
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 }
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 }
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 }
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) {
42 case siUTCTime:
43 return DER_UTCDayToAscii(timechoice);
45 case siGeneralizedTime:
46 return DER_GeneralizedDayToAscii(timechoice);
48 default:
49 PORT_Assert(0);
50 PORT_SetError(SEC_ERROR_INVALID_ARGS);
51 return NULL;
52 }
53 }
55 char *
56 CERT_UTCTime2FormattedAscii(PRTime utcTime, char *format)
57 {
58 PRExplodedTime printableTime;
59 char *timeString;
61 /* Converse time to local time and decompose it into components */
62 PR_ExplodeTime(utcTime, PR_LocalTimeParameters, &printableTime);
64 timeString = (char *)PORT_Alloc(256);
66 if ( timeString ) {
67 if ( ! PR_FormatTime( timeString, 256, format, &printableTime )) {
68 PORT_Free(timeString);
69 timeString = NULL;
70 }
71 }
73 return (timeString);
74 }
76 char *CERT_GenTime2FormattedAscii(PRTime genTime, char *format)
77 {
78 PRExplodedTime printableTime;
79 char *timeString;
81 /* Decompose time into components */
82 PR_ExplodeTime(genTime, PR_GMTParameters, &printableTime);
84 timeString = (char *)PORT_Alloc(256);
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 }
94 return (timeString);
95 }
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;
107 rv = DER_UTCTimeToTime(&utcTime, utcTimeDER);
108 if (rv) {
109 return(NULL);
110 }
111 return (CERT_UTCTime2FormattedAscii (utcTime, format));
112 }
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;
123 rv = DER_GeneralizedTimeToTime(&generalizedTime, generalizedTimeDER);
124 if (rv) {
125 return(NULL);
126 }
127 return (CERT_GeneralizedTime2FormattedAscii (generalizedTime, format));
128 }
130 /* decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME
131 or a SEC_ASN1_UTC_TIME */
133 SECStatus DER_DecodeTimeChoice(PRTime* output, const SECItem* input)
134 {
135 switch (input->type) {
136 case siGeneralizedTime:
137 return DER_GeneralizedTimeToTime(output, input);
139 case siUTCTime:
140 return DER_UTCTimeToTime(output, input);
142 default:
143 PORT_SetError(SEC_ERROR_INVALID_ARGS);
144 PORT_Assert(0);
145 return SECFailure;
146 }
147 }
149 /* encode a PRTime to an ASN.1 DER SECItem containing either a
150 SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */
152 SECStatus DER_EncodeTimeChoice(PLArenaPool* arena, SECItem* output, PRTime input)
153 {
154 SECStatus rv;
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 }