1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
43 static const PRTime January1st2050
= LL_INIT(0x0008f81e, 0x1b098000);
45 static char *DecodeUTCTime2FormattedAscii (SECItem
*utcTimeDER
, char *format
);
46 static char *DecodeGeneralizedTime2FormattedAscii (SECItem
*generalizedTimeDER
, char *format
);
48 /* convert DER utc time to ascii time string */
50 DER_UTCTimeToAscii(SECItem
*utcTime
)
52 return (DecodeUTCTime2FormattedAscii (utcTime
, "%a %b %d %H:%M:%S %Y"));
55 /* convert DER utc time to ascii time string, only include day, not time */
57 DER_UTCDayToAscii(SECItem
*utctime
)
59 return (DecodeUTCTime2FormattedAscii (utctime
, "%a %b %d, %Y"));
62 /* convert DER generalized time to ascii time string, only include day,
65 DER_GeneralizedDayToAscii(SECItem
*gentime
)
67 return (DecodeGeneralizedTime2FormattedAscii (gentime
, "%a %b %d, %Y"));
70 /* convert DER generalized or UTC time to ascii time string, only include
73 DER_TimeChoiceDayToAscii(SECItem
*timechoice
)
75 switch (timechoice
->type
) {
78 return DER_UTCDayToAscii(timechoice
);
80 case siGeneralizedTime
:
81 return DER_GeneralizedDayToAscii(timechoice
);
85 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
91 CERT_UTCTime2FormattedAscii (int64 utcTime
, char *format
)
93 PRExplodedTime printableTime
;
96 /* Converse time to local time and decompose it into components */
97 PR_ExplodeTime(utcTime
, PR_LocalTimeParameters
, &printableTime
);
99 timeString
= (char *)PORT_Alloc(256);
102 if ( ! PR_FormatTime( timeString
, 256, format
, &printableTime
)) {
103 PORT_Free(timeString
);
111 char *CERT_GenTime2FormattedAscii (int64 genTime
, char *format
)
113 PRExplodedTime printableTime
;
116 /* Decompose time into components */
117 PR_ExplodeTime(genTime
, PR_GMTParameters
, &printableTime
);
119 timeString
= (char *)PORT_Alloc(256);
122 if ( ! PR_FormatTime( timeString
, 256, format
, &printableTime
)) {
123 PORT_Free(timeString
);
125 PORT_SetError(SEC_ERROR_OUTPUT_LEN
);
133 /* convert DER utc time to ascii time string, The format of the time string
134 depends on the input "format"
137 DecodeUTCTime2FormattedAscii (SECItem
*utcTimeDER
, char *format
)
142 rv
= DER_UTCTimeToTime(&utcTime
, utcTimeDER
);
146 return (CERT_UTCTime2FormattedAscii (utcTime
, format
));
149 /* convert DER utc time to ascii time string, The format of the time string
150 depends on the input "format"
153 DecodeGeneralizedTime2FormattedAscii (SECItem
*generalizedTimeDER
, char *format
)
155 PRTime generalizedTime
;
158 rv
= DER_GeneralizedTimeToTime(&generalizedTime
, generalizedTimeDER
);
162 return (CERT_GeneralizedTime2FormattedAscii (generalizedTime
, format
));
165 /* decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME
166 or a SEC_ASN1_UTC_TIME */
168 SECStatus
DER_DecodeTimeChoice(PRTime
* output
, const SECItem
* input
)
170 switch (input
->type
) {
171 case siGeneralizedTime
:
172 return DER_GeneralizedTimeToTime(output
, input
);
175 return DER_UTCTimeToTime(output
, input
);
178 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
184 /* encode a PRTime to an ASN.1 DER SECItem containing either a
185 SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */
187 SECStatus
DER_EncodeTimeChoice(PRArenaPool
* arena
, SECItem
* output
, PRTime input
)
189 if (LL_CMP(input
, >, January1st2050
)) {
190 return DER_TimeToGeneralizedTimeArena(arena
, output
, input
);
192 return DER_TimeToUTCTimeArena(arena
, output
, input
);