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) 2000
19 * the Initial Developer. All Rights Reserved.
22 * Ian McGreer <mcgreer@netscape.com>
23 * Javier Delgadillo <javi@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsNSSCertValidity.h"
40 #include "nsNSSCertHeader.h"
42 #include "nsIDateTimeFormat.h"
43 #include "nsDateTimeFormatCID.h"
44 #include "nsComponentManagerUtils.h"
45 #include "nsReadableUtils.h"
46 #include "nsNSSShutDown.h"
48 /* Implementation file */
49 NS_IMPL_THREADSAFE_ISUPPORTS1(nsX509CertValidity
, nsIX509CertValidity
)
51 nsX509CertValidity::nsX509CertValidity() : mTimesInitialized(PR_FALSE
)
53 /* member initializers and constructor code */
56 nsX509CertValidity::nsX509CertValidity(CERTCertificate
*cert
) :
57 mTimesInitialized(PR_FALSE
)
59 nsNSSShutDownPreventionLock locker
;
61 SECStatus rv
= CERT_GetCertTimes(cert
, &mNotBefore
, &mNotAfter
);
63 mTimesInitialized
= PR_TRUE
;
67 nsX509CertValidity::~nsX509CertValidity()
72 /* readonly attribute PRTime notBefore; */
73 NS_IMETHODIMP
nsX509CertValidity::GetNotBefore(PRTime
*aNotBefore
)
75 NS_ENSURE_ARG(aNotBefore
);
77 nsresult rv
= NS_ERROR_FAILURE
;
78 if (mTimesInitialized
) {
79 *aNotBefore
= mNotBefore
;
85 NS_IMETHODIMP
nsX509CertValidity::GetNotBeforeLocalTime(nsAString
&aNotBeforeLocalTime
)
87 if (!mTimesInitialized
)
88 return NS_ERROR_FAILURE
;
91 nsCOMPtr
<nsIDateTimeFormat
> dateFormatter
=
92 do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID
, &rv
);
93 if (NS_FAILED(rv
)) return rv
;
96 PRExplodedTime explodedTime
;
97 PR_ExplodeTime(mNotBefore
, PR_LocalTimeParameters
, &explodedTime
);
98 dateFormatter
->FormatPRExplodedTime(nsnull
, kDateFormatShort
, kTimeFormatSecondsForce24Hour
,
100 aNotBeforeLocalTime
= date
;
104 NS_IMETHODIMP
nsX509CertValidity::GetNotBeforeLocalDay(nsAString
&aNotBeforeLocalDay
)
106 if (!mTimesInitialized
)
107 return NS_ERROR_FAILURE
;
110 nsCOMPtr
<nsIDateTimeFormat
> dateFormatter
=
111 do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID
, &rv
);
112 if (NS_FAILED(rv
)) return rv
;
115 PRExplodedTime explodedTime
;
116 PR_ExplodeTime(mNotBefore
, PR_LocalTimeParameters
, &explodedTime
);
117 dateFormatter
->FormatPRExplodedTime(nsnull
, kDateFormatShort
, kTimeFormatNone
,
118 &explodedTime
, date
);
119 aNotBeforeLocalDay
= date
;
124 NS_IMETHODIMP
nsX509CertValidity::GetNotBeforeGMT(nsAString
&aNotBeforeGMT
)
126 if (!mTimesInitialized
)
127 return NS_ERROR_FAILURE
;
130 nsCOMPtr
<nsIDateTimeFormat
> dateFormatter
=
131 do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID
, &rv
);
132 if (NS_FAILED(rv
)) return rv
;
135 PRExplodedTime explodedTime
;
136 PR_ExplodeTime(mNotBefore
, PR_GMTParameters
, &explodedTime
);
137 dateFormatter
->FormatPRExplodedTime(nsnull
, kDateFormatShort
, kTimeFormatSecondsForce24Hour
,
138 &explodedTime
, date
);
139 aNotBeforeGMT
= date
;
143 /* readonly attribute PRTime notAfter; */
144 NS_IMETHODIMP
nsX509CertValidity::GetNotAfter(PRTime
*aNotAfter
)
146 NS_ENSURE_ARG(aNotAfter
);
148 nsresult rv
= NS_ERROR_FAILURE
;
149 if (mTimesInitialized
) {
150 *aNotAfter
= mNotAfter
;
156 NS_IMETHODIMP
nsX509CertValidity::GetNotAfterLocalTime(nsAString
&aNotAfterLocaltime
)
158 if (!mTimesInitialized
)
159 return NS_ERROR_FAILURE
;
162 nsCOMPtr
<nsIDateTimeFormat
> dateFormatter
=
163 do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID
, &rv
);
164 if (NS_FAILED(rv
)) return rv
;
167 PRExplodedTime explodedTime
;
168 PR_ExplodeTime(mNotAfter
, PR_LocalTimeParameters
, &explodedTime
);
169 dateFormatter
->FormatPRExplodedTime(nsnull
, kDateFormatShort
, kTimeFormatSecondsForce24Hour
,
170 &explodedTime
, date
);
171 aNotAfterLocaltime
= date
;
175 NS_IMETHODIMP
nsX509CertValidity::GetNotAfterLocalDay(nsAString
&aNotAfterLocalDay
)
177 if (!mTimesInitialized
)
178 return NS_ERROR_FAILURE
;
181 nsCOMPtr
<nsIDateTimeFormat
> dateFormatter
=
182 do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID
, &rv
);
183 if (NS_FAILED(rv
)) return rv
;
186 PRExplodedTime explodedTime
;
187 PR_ExplodeTime(mNotAfter
, PR_LocalTimeParameters
, &explodedTime
);
188 dateFormatter
->FormatPRExplodedTime(nsnull
, kDateFormatShort
, kTimeFormatNone
,
189 &explodedTime
, date
);
190 aNotAfterLocalDay
= date
;
194 NS_IMETHODIMP
nsX509CertValidity::GetNotAfterGMT(nsAString
&aNotAfterGMT
)
196 if (!mTimesInitialized
)
197 return NS_ERROR_FAILURE
;
200 nsCOMPtr
<nsIDateTimeFormat
> dateFormatter
=
201 do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID
, &rv
);
202 if (NS_FAILED(rv
)) return rv
;
205 PRExplodedTime explodedTime
;
206 PR_ExplodeTime(mNotAfter
, PR_GMTParameters
, &explodedTime
);
207 dateFormatter
->FormatPRExplodedTime(nsnull
, kDateFormatShort
, kTimeFormatSecondsForce24Hour
,
208 &explodedTime
, date
);