|
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 "nsNSSCertValidity.h" |
|
6 #include "nsCOMPtr.h" |
|
7 #include "nsIDateTimeFormat.h" |
|
8 #include "nsDateTimeFormatCID.h" |
|
9 #include "nsComponentManagerUtils.h" |
|
10 #include "nsReadableUtils.h" |
|
11 #include "nsNSSShutDown.h" |
|
12 #include "cert.h" |
|
13 |
|
14 /* Implementation file */ |
|
15 NS_IMPL_ISUPPORTS(nsX509CertValidity, nsIX509CertValidity) |
|
16 |
|
17 nsX509CertValidity::nsX509CertValidity() : mTimesInitialized(false) |
|
18 { |
|
19 /* member initializers and constructor code */ |
|
20 } |
|
21 |
|
22 nsX509CertValidity::nsX509CertValidity(CERTCertificate *cert) : |
|
23 mTimesInitialized(false) |
|
24 { |
|
25 nsNSSShutDownPreventionLock locker; |
|
26 if (cert) { |
|
27 SECStatus rv = CERT_GetCertTimes(cert, &mNotBefore, &mNotAfter); |
|
28 if (rv == SECSuccess) |
|
29 mTimesInitialized = true; |
|
30 } |
|
31 } |
|
32 |
|
33 nsX509CertValidity::~nsX509CertValidity() |
|
34 { |
|
35 /* destructor code */ |
|
36 } |
|
37 |
|
38 /* readonly attribute PRTime notBefore; */ |
|
39 NS_IMETHODIMP nsX509CertValidity::GetNotBefore(PRTime *aNotBefore) |
|
40 { |
|
41 NS_ENSURE_ARG(aNotBefore); |
|
42 |
|
43 nsresult rv = NS_ERROR_FAILURE; |
|
44 if (mTimesInitialized) { |
|
45 *aNotBefore = mNotBefore; |
|
46 rv = NS_OK; |
|
47 } |
|
48 return rv; |
|
49 } |
|
50 |
|
51 NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalTime(nsAString &aNotBeforeLocalTime) |
|
52 { |
|
53 if (!mTimesInitialized) |
|
54 return NS_ERROR_FAILURE; |
|
55 |
|
56 nsresult rv; |
|
57 nsCOMPtr<nsIDateTimeFormat> dateFormatter = |
|
58 do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); |
|
59 if (NS_FAILED(rv)) return rv; |
|
60 |
|
61 nsAutoString date; |
|
62 PRExplodedTime explodedTime; |
|
63 PR_ExplodeTime(mNotBefore, PR_LocalTimeParameters, &explodedTime); |
|
64 dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatSecondsForce24Hour, |
|
65 &explodedTime, date); |
|
66 aNotBeforeLocalTime = date; |
|
67 return NS_OK; |
|
68 } |
|
69 |
|
70 NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalDay(nsAString &aNotBeforeLocalDay) |
|
71 { |
|
72 if (!mTimesInitialized) |
|
73 return NS_ERROR_FAILURE; |
|
74 |
|
75 nsresult rv; |
|
76 nsCOMPtr<nsIDateTimeFormat> dateFormatter = |
|
77 do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); |
|
78 if (NS_FAILED(rv)) return rv; |
|
79 |
|
80 nsAutoString date; |
|
81 PRExplodedTime explodedTime; |
|
82 PR_ExplodeTime(mNotBefore, PR_LocalTimeParameters, &explodedTime); |
|
83 dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatNone, |
|
84 &explodedTime, date); |
|
85 aNotBeforeLocalDay = date; |
|
86 return NS_OK; |
|
87 } |
|
88 |
|
89 |
|
90 NS_IMETHODIMP nsX509CertValidity::GetNotBeforeGMT(nsAString &aNotBeforeGMT) |
|
91 { |
|
92 if (!mTimesInitialized) |
|
93 return NS_ERROR_FAILURE; |
|
94 |
|
95 nsresult rv; |
|
96 nsCOMPtr<nsIDateTimeFormat> dateFormatter = |
|
97 do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); |
|
98 if (NS_FAILED(rv)) return rv; |
|
99 |
|
100 nsAutoString date; |
|
101 PRExplodedTime explodedTime; |
|
102 PR_ExplodeTime(mNotBefore, PR_GMTParameters, &explodedTime); |
|
103 dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatSecondsForce24Hour, |
|
104 &explodedTime, date); |
|
105 aNotBeforeGMT = date; |
|
106 return NS_OK; |
|
107 } |
|
108 |
|
109 /* readonly attribute PRTime notAfter; */ |
|
110 NS_IMETHODIMP nsX509CertValidity::GetNotAfter(PRTime *aNotAfter) |
|
111 { |
|
112 NS_ENSURE_ARG(aNotAfter); |
|
113 |
|
114 nsresult rv = NS_ERROR_FAILURE; |
|
115 if (mTimesInitialized) { |
|
116 *aNotAfter = mNotAfter; |
|
117 rv = NS_OK; |
|
118 } |
|
119 return rv; |
|
120 } |
|
121 |
|
122 NS_IMETHODIMP nsX509CertValidity::GetNotAfterLocalTime(nsAString &aNotAfterLocaltime) |
|
123 { |
|
124 if (!mTimesInitialized) |
|
125 return NS_ERROR_FAILURE; |
|
126 |
|
127 nsresult rv; |
|
128 nsCOMPtr<nsIDateTimeFormat> dateFormatter = |
|
129 do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); |
|
130 if (NS_FAILED(rv)) return rv; |
|
131 |
|
132 nsAutoString date; |
|
133 PRExplodedTime explodedTime; |
|
134 PR_ExplodeTime(mNotAfter, PR_LocalTimeParameters, &explodedTime); |
|
135 dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatSecondsForce24Hour, |
|
136 &explodedTime, date); |
|
137 aNotAfterLocaltime = date; |
|
138 return NS_OK; |
|
139 } |
|
140 |
|
141 NS_IMETHODIMP nsX509CertValidity::GetNotAfterLocalDay(nsAString &aNotAfterLocalDay) |
|
142 { |
|
143 if (!mTimesInitialized) |
|
144 return NS_ERROR_FAILURE; |
|
145 |
|
146 nsresult rv; |
|
147 nsCOMPtr<nsIDateTimeFormat> dateFormatter = |
|
148 do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); |
|
149 if (NS_FAILED(rv)) return rv; |
|
150 |
|
151 nsAutoString date; |
|
152 PRExplodedTime explodedTime; |
|
153 PR_ExplodeTime(mNotAfter, PR_LocalTimeParameters, &explodedTime); |
|
154 dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatNone, |
|
155 &explodedTime, date); |
|
156 aNotAfterLocalDay = date; |
|
157 return NS_OK; |
|
158 } |
|
159 |
|
160 NS_IMETHODIMP nsX509CertValidity::GetNotAfterGMT(nsAString &aNotAfterGMT) |
|
161 { |
|
162 if (!mTimesInitialized) |
|
163 return NS_ERROR_FAILURE; |
|
164 |
|
165 nsresult rv; |
|
166 nsCOMPtr<nsIDateTimeFormat> dateFormatter = |
|
167 do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); |
|
168 if (NS_FAILED(rv)) return rv; |
|
169 |
|
170 nsAutoString date; |
|
171 PRExplodedTime explodedTime; |
|
172 PR_ExplodeTime(mNotAfter, PR_GMTParameters, &explodedTime); |
|
173 dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatSecondsForce24Hour, |
|
174 &explodedTime, date); |
|
175 aNotAfterGMT = date; |
|
176 return NS_OK; |
|
177 } |