Line data Source code
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 :
7 : #include "cert.h"
8 : #include "mozilla/Assertions.h"
9 : #include "nsCOMPtr.h"
10 : #include "nsComponentManagerUtils.h"
11 : #include "nsReadableUtils.h"
12 :
13 0 : NS_IMPL_ISUPPORTS(nsX509CertValidity, nsIX509CertValidity)
14 :
15 0 : nsX509CertValidity::nsX509CertValidity(const mozilla::UniqueCERTCertificate& cert)
16 0 : : mTimesInitialized(false)
17 : {
18 0 : MOZ_ASSERT(cert);
19 0 : if (!cert) {
20 0 : return;
21 : }
22 :
23 0 : nsNSSShutDownPreventionLock locker;
24 0 : if (isAlreadyShutDown()) {
25 0 : return;
26 : }
27 :
28 0 : if (CERT_GetCertTimes(cert.get(), &mNotBefore, &mNotAfter) == SECSuccess) {
29 0 : mTimesInitialized = true;
30 : }
31 : }
32 :
33 0 : nsX509CertValidity::~nsX509CertValidity()
34 : {
35 0 : nsNSSShutDownPreventionLock locker;
36 0 : if (isAlreadyShutDown()) {
37 0 : return;
38 : }
39 :
40 0 : shutdown(ShutdownCalledFrom::Object);
41 0 : }
42 :
43 : NS_IMETHODIMP
44 0 : nsX509CertValidity::GetNotBefore(PRTime* aNotBefore)
45 : {
46 0 : NS_ENSURE_ARG(aNotBefore);
47 :
48 0 : if (!mTimesInitialized) {
49 0 : return NS_ERROR_FAILURE;
50 : }
51 :
52 0 : *aNotBefore = mNotBefore;
53 0 : return NS_OK;
54 : }
55 :
56 : nsresult
57 0 : nsX509CertValidity::FormatTime(const PRTime& aTimeDate,
58 : PRTimeParamFn aParamFn,
59 : const nsTimeFormatSelector aTimeFormatSelector,
60 : nsAString& aFormattedTimeDate)
61 : {
62 0 : if (!mTimesInitialized)
63 0 : return NS_ERROR_FAILURE;
64 :
65 : PRExplodedTime explodedTime;
66 0 : PR_ExplodeTime(const_cast<PRTime&>(aTimeDate), aParamFn, &explodedTime);
67 : return mozilla::DateTimeFormat::FormatPRExplodedTime(kDateFormatLong,
68 : aTimeFormatSelector,
69 0 : &explodedTime, aFormattedTimeDate);
70 : }
71 :
72 : NS_IMETHODIMP
73 0 : nsX509CertValidity::GetNotBeforeLocalTime(nsAString& aNotBeforeLocalTime)
74 : {
75 0 : return FormatTime(mNotBefore, PR_LocalTimeParameters,
76 0 : kTimeFormatSeconds, aNotBeforeLocalTime);
77 : }
78 :
79 : NS_IMETHODIMP
80 0 : nsX509CertValidity::GetNotBeforeLocalDay(nsAString& aNotBeforeLocalDay)
81 : {
82 0 : return FormatTime(mNotBefore, PR_LocalTimeParameters,
83 0 : kTimeFormatNone, aNotBeforeLocalDay);
84 : }
85 :
86 : NS_IMETHODIMP
87 0 : nsX509CertValidity::GetNotBeforeGMT(nsAString& aNotBeforeGMT)
88 : {
89 0 : return FormatTime(mNotBefore, PR_GMTParameters,
90 0 : kTimeFormatSeconds, aNotBeforeGMT);
91 : }
92 :
93 : NS_IMETHODIMP
94 0 : nsX509CertValidity::GetNotAfter(PRTime* aNotAfter)
95 : {
96 0 : NS_ENSURE_ARG(aNotAfter);
97 :
98 0 : if (!mTimesInitialized) {
99 0 : return NS_ERROR_FAILURE;
100 : }
101 :
102 0 : *aNotAfter = mNotAfter;
103 0 : return NS_OK;
104 : }
105 :
106 : NS_IMETHODIMP
107 0 : nsX509CertValidity::GetNotAfterLocalTime(nsAString& aNotAfterLocaltime)
108 : {
109 0 : return FormatTime(mNotAfter, PR_LocalTimeParameters,
110 0 : kTimeFormatSeconds, aNotAfterLocaltime);
111 : }
112 :
113 : NS_IMETHODIMP
114 0 : nsX509CertValidity::GetNotAfterLocalDay(nsAString& aNotAfterLocalDay)
115 : {
116 0 : return FormatTime(mNotAfter, PR_LocalTimeParameters,
117 0 : kTimeFormatNone, aNotAfterLocalDay);
118 : }
119 :
120 : NS_IMETHODIMP
121 0 : nsX509CertValidity::GetNotAfterGMT(nsAString& aNotAfterGMT)
122 : {
123 0 : return FormatTime(mNotAfter, PR_GMTParameters,
124 0 : kTimeFormatSeconds, aNotAfterGMT);
125 : }
|