summaryrefslogtreecommitdiffstats
path: root/security/manager/ssl/X509CertValidity.cpp
blob: 8c09326bb62da66a0fce7f6bdc2aaea6d6ba8d14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "X509CertValidity.h"

#include "mozilla/Assertions.h"
#include "mozpkix/pkixder.h"
#include "mozpkix/pkixutil.h"
#include "nsCOMPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsReadableUtils.h"
#include "seccomon.h"
#include "secder.h"

NS_IMPL_ISUPPORTS(X509CertValidity, nsIX509CertValidity)

using namespace mozilla;
using namespace mozilla::pkix;
using namespace mozilla::pkix::der;

X509CertValidity::X509CertValidity(Input certDER)
    : mNotBefore(0), mNotAfter(0), mTimesInitialized(false) {
  // We're not building a verified certificate chain, so the EndEntityOrCA
  // parameter doesn't matter.
  BackCert cert(certDER, EndEntityOrCA::MustBeEndEntity, nullptr);
  pkix::Result rv = cert.Init();
  if (rv != Success) {
    return;
  }
  // Validity ::= SEQUENCE {
  //    notBefore      Time,
  //    notAfter       Time  }
  //
  // Time ::= CHOICE {
  //    utcTime        UTCTime,
  //    generalTime    GeneralizedTime }
  //
  // NB: BackCert::GetValidity returns the value of the Validity of the
  // certificate (i.e. notBefore and notAfter, without the enclosing SEQUENCE
  // and length)
  Reader reader(cert.GetValidity());
  uint8_t expectedTag = reader.Peek(UTCTime) ? UTCTime : GENERALIZED_TIME;
  Input notBefore;
  pkix::Result result = ExpectTagAndGetValue(reader, expectedTag, notBefore);
  if (result != Success) {
    return;
  }
  SECItemType notBeforeType =
      expectedTag == UTCTime ? siUTCTime : siGeneralizedTime;
  SECItem notBeforeItem = {
      notBeforeType, const_cast<unsigned char*>(notBefore.UnsafeGetData()),
      notBefore.GetLength()};
  SECStatus srv = DER_DecodeTimeChoice(&mNotBefore, &notBeforeItem);
  if (srv != SECSuccess) {
    return;
  }
  expectedTag = reader.Peek(UTCTime) ? UTCTime : GENERALIZED_TIME;
  Input notAfter;
  result = ExpectTagAndGetValue(reader, expectedTag, notAfter);
  if (result != Success) {
    return;
  }
  SECItemType notAfterType =
      expectedTag == UTCTime ? siUTCTime : siGeneralizedTime;
  SECItem notAfterItem = {notAfterType,
                          const_cast<unsigned char*>(notAfter.UnsafeGetData()),
                          notAfter.GetLength()};
  srv = DER_DecodeTimeChoice(&mNotAfter, &notAfterItem);
  if (srv != SECSuccess) {
    return;
  }

  mTimesInitialized = true;
}

NS_IMETHODIMP
X509CertValidity::GetNotBefore(PRTime* aNotBefore) {
  NS_ENSURE_ARG(aNotBefore);

  if (!mTimesInitialized) {
    return NS_ERROR_FAILURE;
  }

  *aNotBefore = mNotBefore;
  return NS_OK;
}

nsresult X509CertValidity::FormatTime(
    const PRTime& aTimeDate, PRTimeParamFn aParamFn,
    const nsTimeFormatSelector aTimeFormatSelector,
    nsAString& aFormattedTimeDate) {
  if (!mTimesInitialized) return NS_ERROR_FAILURE;

  PRExplodedTime explodedTime;
  PR_ExplodeTime(const_cast<PRTime&>(aTimeDate), aParamFn, &explodedTime);
  return mozilla::DateTimeFormat::FormatPRExplodedTime(
      kDateFormatLong, aTimeFormatSelector, &explodedTime, aFormattedTimeDate);
}

NS_IMETHODIMP
X509CertValidity::GetNotBeforeLocalTime(nsAString& aNotBeforeLocalTime) {
  return FormatTime(mNotBefore, PR_LocalTimeParameters, kTimeFormatLong,
                    aNotBeforeLocalTime);
}

NS_IMETHODIMP
X509CertValidity::GetNotBeforeLocalDay(nsAString& aNotBeforeLocalDay) {
  return FormatTime(mNotBefore, PR_LocalTimeParameters, kTimeFormatNone,
                    aNotBeforeLocalDay);
}

NS_IMETHODIMP
X509CertValidity::GetNotBeforeGMT(nsAString& aNotBeforeGMT) {
  return FormatTime(mNotBefore, PR_GMTParameters, kTimeFormatLong,
                    aNotBeforeGMT);
}

NS_IMETHODIMP
X509CertValidity::GetNotAfter(PRTime* aNotAfter) {
  NS_ENSURE_ARG(aNotAfter);

  if (!mTimesInitialized) {
    return NS_ERROR_FAILURE;
  }

  *aNotAfter = mNotAfter;
  return NS_OK;
}

NS_IMETHODIMP
X509CertValidity::GetNotAfterLocalTime(nsAString& aNotAfterLocaltime) {
  return FormatTime(mNotAfter, PR_LocalTimeParameters, kTimeFormatLong,
                    aNotAfterLocaltime);
}

NS_IMETHODIMP
X509CertValidity::GetNotAfterLocalDay(nsAString& aNotAfterLocalDay) {
  return FormatTime(mNotAfter, PR_LocalTimeParameters, kTimeFormatNone,
                    aNotAfterLocalDay);
}

NS_IMETHODIMP
X509CertValidity::GetNotAfterGMT(nsAString& aNotAfterGMT) {
  return FormatTime(mNotAfter, PR_GMTParameters, kTimeFormatLong, aNotAfterGMT);
}