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/. */
5 #include "X509CertValidity.h"
7 #include "mozpkix/pkixder.h"
8 #include "mozpkix/pkixutil.h"
9 #include "nsComponentManagerUtils.h"
12 NS_IMPL_ISUPPORTS(X509CertValidity
, nsIX509CertValidity
)
14 using namespace mozilla
;
15 using namespace mozilla::pkix
;
17 X509CertValidity::X509CertValidity(Input certDER
)
18 : mNotBefore(0), mNotAfter(0), mTimesInitialized(false) {
19 using namespace mozilla::pkix::der
;
21 // We're not building a verified certificate chain, so the EndEntityOrCA
22 // parameter doesn't matter.
23 BackCert
cert(certDER
, EndEntityOrCA::MustBeEndEntity
, nullptr);
24 pkix::Result rv
= cert
.Init();
28 // Validity ::= SEQUENCE {
34 // generalTime GeneralizedTime }
36 // NB: BackCert::GetValidity returns the value of the Validity of the
37 // certificate (i.e. notBefore and notAfter, without the enclosing SEQUENCE
39 Reader
reader(cert
.GetValidity());
40 uint8_t expectedTag
= reader
.Peek(UTCTime
) ? UTCTime
: GENERALIZED_TIME
;
42 pkix::Result result
= ExpectTagAndGetValue(reader
, expectedTag
, notBefore
);
43 if (result
!= Success
) {
46 SECItemType notBeforeType
=
47 expectedTag
== UTCTime
? siUTCTime
: siGeneralizedTime
;
48 SECItem notBeforeItem
= {
49 notBeforeType
, const_cast<unsigned char*>(notBefore
.UnsafeGetData()),
50 notBefore
.GetLength()};
51 SECStatus srv
= DER_DecodeTimeChoice(&mNotBefore
, ¬BeforeItem
);
52 if (srv
!= SECSuccess
) {
55 expectedTag
= reader
.Peek(UTCTime
) ? UTCTime
: GENERALIZED_TIME
;
57 result
= ExpectTagAndGetValue(reader
, expectedTag
, notAfter
);
58 if (result
!= Success
) {
61 SECItemType notAfterType
=
62 expectedTag
== UTCTime
? siUTCTime
: siGeneralizedTime
;
63 SECItem notAfterItem
= {notAfterType
,
64 const_cast<unsigned char*>(notAfter
.UnsafeGetData()),
65 notAfter
.GetLength()};
66 srv
= DER_DecodeTimeChoice(&mNotAfter
, ¬AfterItem
);
67 if (srv
!= SECSuccess
) {
71 mTimesInitialized
= true;
75 X509CertValidity::GetNotBefore(PRTime
* aNotBefore
) {
76 NS_ENSURE_ARG(aNotBefore
);
78 if (!mTimesInitialized
) {
79 return NS_ERROR_FAILURE
;
82 *aNotBefore
= mNotBefore
;
87 X509CertValidity::GetNotAfter(PRTime
* aNotAfter
) {
88 NS_ENSURE_ARG(aNotAfter
);
90 if (!mTimesInitialized
) {
91 return NS_ERROR_FAILURE
;
94 *aNotAfter
= mNotAfter
;