security/manager/ssl/src/nsNSSCertValidity.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/manager/ssl/src/nsNSSCertValidity.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,177 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "nsNSSCertValidity.h"
     1.9 +#include "nsCOMPtr.h"
    1.10 +#include "nsIDateTimeFormat.h"
    1.11 +#include "nsDateTimeFormatCID.h"
    1.12 +#include "nsComponentManagerUtils.h"
    1.13 +#include "nsReadableUtils.h"
    1.14 +#include "nsNSSShutDown.h"
    1.15 +#include "cert.h"
    1.16 +
    1.17 +/* Implementation file */
    1.18 +NS_IMPL_ISUPPORTS(nsX509CertValidity, nsIX509CertValidity)
    1.19 +
    1.20 +nsX509CertValidity::nsX509CertValidity() : mTimesInitialized(false)
    1.21 +{
    1.22 +  /* member initializers and constructor code */
    1.23 +}
    1.24 +
    1.25 +nsX509CertValidity::nsX509CertValidity(CERTCertificate *cert) : 
    1.26 +                                           mTimesInitialized(false)
    1.27 +{
    1.28 +  nsNSSShutDownPreventionLock locker;
    1.29 +  if (cert) {
    1.30 +    SECStatus rv = CERT_GetCertTimes(cert, &mNotBefore, &mNotAfter);
    1.31 +    if (rv == SECSuccess)
    1.32 +      mTimesInitialized = true;
    1.33 +  }
    1.34 +}
    1.35 +
    1.36 +nsX509CertValidity::~nsX509CertValidity()
    1.37 +{
    1.38 +  /* destructor code */
    1.39 +}
    1.40 +
    1.41 +/* readonly attribute PRTime notBefore; */
    1.42 +NS_IMETHODIMP nsX509CertValidity::GetNotBefore(PRTime *aNotBefore)
    1.43 +{
    1.44 +  NS_ENSURE_ARG(aNotBefore);
    1.45 +
    1.46 +  nsresult rv = NS_ERROR_FAILURE;
    1.47 +  if (mTimesInitialized) {
    1.48 +    *aNotBefore = mNotBefore;
    1.49 +    rv = NS_OK;
    1.50 +  }
    1.51 +  return rv;
    1.52 +}
    1.53 +
    1.54 +NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalTime(nsAString &aNotBeforeLocalTime)
    1.55 +{
    1.56 +  if (!mTimesInitialized)
    1.57 +    return NS_ERROR_FAILURE;
    1.58 +
    1.59 +  nsresult rv;
    1.60 +  nsCOMPtr<nsIDateTimeFormat> dateFormatter =
    1.61 +     do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
    1.62 +  if (NS_FAILED(rv)) return rv;
    1.63 +
    1.64 +  nsAutoString date;
    1.65 +  PRExplodedTime explodedTime;
    1.66 +  PR_ExplodeTime(mNotBefore, PR_LocalTimeParameters, &explodedTime);
    1.67 +  dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatSecondsForce24Hour,
    1.68 +                                      &explodedTime, date);
    1.69 +  aNotBeforeLocalTime = date;
    1.70 +  return NS_OK;
    1.71 +}
    1.72 +
    1.73 +NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalDay(nsAString &aNotBeforeLocalDay)
    1.74 +{
    1.75 +  if (!mTimesInitialized)
    1.76 +    return NS_ERROR_FAILURE;
    1.77 +
    1.78 +  nsresult rv;
    1.79 +  nsCOMPtr<nsIDateTimeFormat> dateFormatter =
    1.80 +     do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
    1.81 +  if (NS_FAILED(rv)) return rv;
    1.82 +
    1.83 +  nsAutoString date;
    1.84 +  PRExplodedTime explodedTime;
    1.85 +  PR_ExplodeTime(mNotBefore, PR_LocalTimeParameters, &explodedTime);
    1.86 +  dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatNone,
    1.87 +                                      &explodedTime, date);
    1.88 +  aNotBeforeLocalDay = date;
    1.89 +  return NS_OK;
    1.90 +}
    1.91 +
    1.92 +
    1.93 +NS_IMETHODIMP nsX509CertValidity::GetNotBeforeGMT(nsAString &aNotBeforeGMT)
    1.94 +{
    1.95 +  if (!mTimesInitialized)
    1.96 +    return NS_ERROR_FAILURE;
    1.97 +
    1.98 +  nsresult rv;
    1.99 +  nsCOMPtr<nsIDateTimeFormat> dateFormatter =
   1.100 +     do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
   1.101 +  if (NS_FAILED(rv)) return rv;
   1.102 +
   1.103 +  nsAutoString date;
   1.104 +  PRExplodedTime explodedTime;
   1.105 +  PR_ExplodeTime(mNotBefore, PR_GMTParameters, &explodedTime);
   1.106 +  dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatSecondsForce24Hour,
   1.107 +                                      &explodedTime, date);
   1.108 +  aNotBeforeGMT = date;
   1.109 +  return NS_OK;
   1.110 +}
   1.111 +
   1.112 +/* readonly attribute PRTime notAfter; */
   1.113 +NS_IMETHODIMP nsX509CertValidity::GetNotAfter(PRTime *aNotAfter)
   1.114 +{
   1.115 +  NS_ENSURE_ARG(aNotAfter);
   1.116 +
   1.117 +  nsresult rv = NS_ERROR_FAILURE;
   1.118 +  if (mTimesInitialized) {
   1.119 +    *aNotAfter = mNotAfter;
   1.120 +    rv = NS_OK;
   1.121 +  }
   1.122 +  return rv;
   1.123 +}
   1.124 +
   1.125 +NS_IMETHODIMP nsX509CertValidity::GetNotAfterLocalTime(nsAString &aNotAfterLocaltime)
   1.126 +{
   1.127 +  if (!mTimesInitialized)
   1.128 +    return NS_ERROR_FAILURE;
   1.129 +
   1.130 +  nsresult rv;
   1.131 +  nsCOMPtr<nsIDateTimeFormat> dateFormatter =
   1.132 +     do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
   1.133 +  if (NS_FAILED(rv)) return rv;
   1.134 +
   1.135 +  nsAutoString date;
   1.136 +  PRExplodedTime explodedTime;
   1.137 +  PR_ExplodeTime(mNotAfter, PR_LocalTimeParameters, &explodedTime);
   1.138 +  dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatSecondsForce24Hour,
   1.139 +                                      &explodedTime, date);
   1.140 +  aNotAfterLocaltime = date;
   1.141 +  return NS_OK;
   1.142 +}
   1.143 +
   1.144 +NS_IMETHODIMP nsX509CertValidity::GetNotAfterLocalDay(nsAString &aNotAfterLocalDay)
   1.145 +{
   1.146 +  if (!mTimesInitialized)
   1.147 +    return NS_ERROR_FAILURE;
   1.148 +
   1.149 +  nsresult rv;
   1.150 +  nsCOMPtr<nsIDateTimeFormat> dateFormatter =
   1.151 +     do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
   1.152 +  if (NS_FAILED(rv)) return rv;
   1.153 +
   1.154 +  nsAutoString date;
   1.155 +  PRExplodedTime explodedTime;
   1.156 +  PR_ExplodeTime(mNotAfter, PR_LocalTimeParameters, &explodedTime);
   1.157 +  dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatNone,
   1.158 +                                      &explodedTime, date);
   1.159 +  aNotAfterLocalDay = date;
   1.160 +  return NS_OK;
   1.161 +}
   1.162 +
   1.163 +NS_IMETHODIMP nsX509CertValidity::GetNotAfterGMT(nsAString &aNotAfterGMT)
   1.164 +{
   1.165 +  if (!mTimesInitialized)
   1.166 +    return NS_ERROR_FAILURE;
   1.167 +
   1.168 +  nsresult rv;
   1.169 +  nsCOMPtr<nsIDateTimeFormat> dateFormatter =
   1.170 +     do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
   1.171 +  if (NS_FAILED(rv)) return rv;
   1.172 +
   1.173 +  nsAutoString date;
   1.174 +  PRExplodedTime explodedTime;
   1.175 +  PR_ExplodeTime(mNotAfter, PR_GMTParameters, &explodedTime);
   1.176 +  dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatSecondsForce24Hour,
   1.177 +                                      &explodedTime, date);
   1.178 +  aNotAfterGMT = date;
   1.179 +  return NS_OK;
   1.180 +}

mercurial