1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
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/
8 : *
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
12 : * License.
13 : *
14 : * The Original Code is the Netscape security libraries.
15 : *
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.
20 : *
21 : * Contributor(s):
22 : * Ian McGreer <mcgreer@netscape.com>
23 : * Javier Delgadillo <javi@netscape.com>
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsNSSCertValidity.h"
40 : #include "nsNSSCertHeader.h"
41 : #include "nsCOMPtr.h"
42 : #include "nsIDateTimeFormat.h"
43 : #include "nsDateTimeFormatCID.h"
44 : #include "nsComponentManagerUtils.h"
45 : #include "nsReadableUtils.h"
46 : #include "nsNSSShutDown.h"
47 :
48 : /* Implementation file */
49 0 : NS_IMPL_THREADSAFE_ISUPPORTS1(nsX509CertValidity, nsIX509CertValidity)
50 :
51 0 : nsX509CertValidity::nsX509CertValidity() : mTimesInitialized(false)
52 : {
53 : /* member initializers and constructor code */
54 0 : }
55 :
56 0 : nsX509CertValidity::nsX509CertValidity(CERTCertificate *cert) :
57 0 : mTimesInitialized(false)
58 : {
59 0 : nsNSSShutDownPreventionLock locker;
60 0 : if (cert) {
61 0 : SECStatus rv = CERT_GetCertTimes(cert, &mNotBefore, &mNotAfter);
62 0 : if (rv == SECSuccess)
63 0 : mTimesInitialized = true;
64 : }
65 0 : }
66 :
67 0 : nsX509CertValidity::~nsX509CertValidity()
68 : {
69 : /* destructor code */
70 0 : }
71 :
72 : /* readonly attribute PRTime notBefore; */
73 0 : NS_IMETHODIMP nsX509CertValidity::GetNotBefore(PRTime *aNotBefore)
74 : {
75 0 : NS_ENSURE_ARG(aNotBefore);
76 :
77 0 : nsresult rv = NS_ERROR_FAILURE;
78 0 : if (mTimesInitialized) {
79 0 : *aNotBefore = mNotBefore;
80 0 : rv = NS_OK;
81 : }
82 0 : return rv;
83 : }
84 :
85 0 : NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalTime(nsAString &aNotBeforeLocalTime)
86 : {
87 0 : if (!mTimesInitialized)
88 0 : return NS_ERROR_FAILURE;
89 :
90 : nsresult rv;
91 : nsCOMPtr<nsIDateTimeFormat> dateFormatter =
92 0 : do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
93 0 : if (NS_FAILED(rv)) return rv;
94 :
95 0 : nsAutoString date;
96 : PRExplodedTime explodedTime;
97 0 : PR_ExplodeTime(mNotBefore, PR_LocalTimeParameters, &explodedTime);
98 0 : dateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort, kTimeFormatSecondsForce24Hour,
99 0 : &explodedTime, date);
100 0 : aNotBeforeLocalTime = date;
101 0 : return NS_OK;
102 : }
103 :
104 0 : NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalDay(nsAString &aNotBeforeLocalDay)
105 : {
106 0 : if (!mTimesInitialized)
107 0 : return NS_ERROR_FAILURE;
108 :
109 : nsresult rv;
110 : nsCOMPtr<nsIDateTimeFormat> dateFormatter =
111 0 : do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
112 0 : if (NS_FAILED(rv)) return rv;
113 :
114 0 : nsAutoString date;
115 : PRExplodedTime explodedTime;
116 0 : PR_ExplodeTime(mNotBefore, PR_LocalTimeParameters, &explodedTime);
117 0 : dateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort, kTimeFormatNone,
118 0 : &explodedTime, date);
119 0 : aNotBeforeLocalDay = date;
120 0 : return NS_OK;
121 : }
122 :
123 :
124 0 : NS_IMETHODIMP nsX509CertValidity::GetNotBeforeGMT(nsAString &aNotBeforeGMT)
125 : {
126 0 : if (!mTimesInitialized)
127 0 : return NS_ERROR_FAILURE;
128 :
129 : nsresult rv;
130 : nsCOMPtr<nsIDateTimeFormat> dateFormatter =
131 0 : do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
132 0 : if (NS_FAILED(rv)) return rv;
133 :
134 0 : nsAutoString date;
135 : PRExplodedTime explodedTime;
136 0 : PR_ExplodeTime(mNotBefore, PR_GMTParameters, &explodedTime);
137 0 : dateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort, kTimeFormatSecondsForce24Hour,
138 0 : &explodedTime, date);
139 0 : aNotBeforeGMT = date;
140 0 : return NS_OK;
141 : }
142 :
143 : /* readonly attribute PRTime notAfter; */
144 0 : NS_IMETHODIMP nsX509CertValidity::GetNotAfter(PRTime *aNotAfter)
145 : {
146 0 : NS_ENSURE_ARG(aNotAfter);
147 :
148 0 : nsresult rv = NS_ERROR_FAILURE;
149 0 : if (mTimesInitialized) {
150 0 : *aNotAfter = mNotAfter;
151 0 : rv = NS_OK;
152 : }
153 0 : return rv;
154 : }
155 :
156 0 : NS_IMETHODIMP nsX509CertValidity::GetNotAfterLocalTime(nsAString &aNotAfterLocaltime)
157 : {
158 0 : if (!mTimesInitialized)
159 0 : return NS_ERROR_FAILURE;
160 :
161 : nsresult rv;
162 : nsCOMPtr<nsIDateTimeFormat> dateFormatter =
163 0 : do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
164 0 : if (NS_FAILED(rv)) return rv;
165 :
166 0 : nsAutoString date;
167 : PRExplodedTime explodedTime;
168 0 : PR_ExplodeTime(mNotAfter, PR_LocalTimeParameters, &explodedTime);
169 0 : dateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort, kTimeFormatSecondsForce24Hour,
170 0 : &explodedTime, date);
171 0 : aNotAfterLocaltime = date;
172 0 : return NS_OK;
173 : }
174 :
175 0 : NS_IMETHODIMP nsX509CertValidity::GetNotAfterLocalDay(nsAString &aNotAfterLocalDay)
176 : {
177 0 : if (!mTimesInitialized)
178 0 : return NS_ERROR_FAILURE;
179 :
180 : nsresult rv;
181 : nsCOMPtr<nsIDateTimeFormat> dateFormatter =
182 0 : do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
183 0 : if (NS_FAILED(rv)) return rv;
184 :
185 0 : nsAutoString date;
186 : PRExplodedTime explodedTime;
187 0 : PR_ExplodeTime(mNotAfter, PR_LocalTimeParameters, &explodedTime);
188 0 : dateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort, kTimeFormatNone,
189 0 : &explodedTime, date);
190 0 : aNotAfterLocalDay = date;
191 0 : return NS_OK;
192 : }
193 :
194 0 : NS_IMETHODIMP nsX509CertValidity::GetNotAfterGMT(nsAString &aNotAfterGMT)
195 : {
196 0 : if (!mTimesInitialized)
197 0 : return NS_ERROR_FAILURE;
198 :
199 : nsresult rv;
200 : nsCOMPtr<nsIDateTimeFormat> dateFormatter =
201 0 : do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
202 0 : if (NS_FAILED(rv)) return rv;
203 :
204 0 : nsAutoString date;
205 : PRExplodedTime explodedTime;
206 0 : PR_ExplodeTime(mNotAfter, PR_GMTParameters, &explodedTime);
207 0 : dateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort, kTimeFormatSecondsForce24Hour,
208 0 : &explodedTime, date);
209 0 : aNotAfterGMT = date;
210 0 : return NS_OK;
211 : }
|