Bug 1928997: Update tabs icon in Unified Search popup r=desktop-theme-reviewers,daleh...
[gecko.git] / security / manager / ssl / X509CertValidity.cpp
blob1d5dc54aa6d507ee09386a419fe44c1c6f5df458
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"
10 #include "secder.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();
25 if (rv != Success) {
26 return;
28 // Validity ::= SEQUENCE {
29 // notBefore Time,
30 // notAfter Time }
32 // Time ::= CHOICE {
33 // utcTime UTCTime,
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
38 // and length)
39 Reader reader(cert.GetValidity());
40 uint8_t expectedTag = reader.Peek(UTCTime) ? UTCTime : GENERALIZED_TIME;
41 Input notBefore;
42 pkix::Result result = ExpectTagAndGetValue(reader, expectedTag, notBefore);
43 if (result != Success) {
44 return;
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, &notBeforeItem);
52 if (srv != SECSuccess) {
53 return;
55 expectedTag = reader.Peek(UTCTime) ? UTCTime : GENERALIZED_TIME;
56 Input notAfter;
57 result = ExpectTagAndGetValue(reader, expectedTag, notAfter);
58 if (result != Success) {
59 return;
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, &notAfterItem);
67 if (srv != SECSuccess) {
68 return;
71 mTimesInitialized = true;
74 NS_IMETHODIMP
75 X509CertValidity::GetNotBefore(PRTime* aNotBefore) {
76 NS_ENSURE_ARG(aNotBefore);
78 if (!mTimesInitialized) {
79 return NS_ERROR_FAILURE;
82 *aNotBefore = mNotBefore;
83 return NS_OK;
86 NS_IMETHODIMP
87 X509CertValidity::GetNotAfter(PRTime* aNotAfter) {
88 NS_ENSURE_ARG(aNotAfter);
90 if (!mTimesInitialized) {
91 return NS_ERROR_FAILURE;
94 *aNotAfter = mNotAfter;
95 return NS_OK;