michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsNSSCertValidity.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIDateTimeFormat.h" michael@0: #include "nsDateTimeFormatCID.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsNSSShutDown.h" michael@0: #include "cert.h" michael@0: michael@0: /* Implementation file */ michael@0: NS_IMPL_ISUPPORTS(nsX509CertValidity, nsIX509CertValidity) michael@0: michael@0: nsX509CertValidity::nsX509CertValidity() : mTimesInitialized(false) michael@0: { michael@0: /* member initializers and constructor code */ michael@0: } michael@0: michael@0: nsX509CertValidity::nsX509CertValidity(CERTCertificate *cert) : michael@0: mTimesInitialized(false) michael@0: { michael@0: nsNSSShutDownPreventionLock locker; michael@0: if (cert) { michael@0: SECStatus rv = CERT_GetCertTimes(cert, &mNotBefore, &mNotAfter); michael@0: if (rv == SECSuccess) michael@0: mTimesInitialized = true; michael@0: } michael@0: } michael@0: michael@0: nsX509CertValidity::~nsX509CertValidity() michael@0: { michael@0: /* destructor code */ michael@0: } michael@0: michael@0: /* readonly attribute PRTime notBefore; */ michael@0: NS_IMETHODIMP nsX509CertValidity::GetNotBefore(PRTime *aNotBefore) michael@0: { michael@0: NS_ENSURE_ARG(aNotBefore); michael@0: michael@0: nsresult rv = NS_ERROR_FAILURE; michael@0: if (mTimesInitialized) { michael@0: *aNotBefore = mNotBefore; michael@0: rv = NS_OK; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalTime(nsAString &aNotBeforeLocalTime) michael@0: { michael@0: if (!mTimesInitialized) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr dateFormatter = michael@0: do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsAutoString date; michael@0: PRExplodedTime explodedTime; michael@0: PR_ExplodeTime(mNotBefore, PR_LocalTimeParameters, &explodedTime); michael@0: dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatSecondsForce24Hour, michael@0: &explodedTime, date); michael@0: aNotBeforeLocalTime = date; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalDay(nsAString &aNotBeforeLocalDay) michael@0: { michael@0: if (!mTimesInitialized) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr dateFormatter = michael@0: do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsAutoString date; michael@0: PRExplodedTime explodedTime; michael@0: PR_ExplodeTime(mNotBefore, PR_LocalTimeParameters, &explodedTime); michael@0: dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatNone, michael@0: &explodedTime, date); michael@0: aNotBeforeLocalDay = date; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP nsX509CertValidity::GetNotBeforeGMT(nsAString &aNotBeforeGMT) michael@0: { michael@0: if (!mTimesInitialized) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr dateFormatter = michael@0: do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsAutoString date; michael@0: PRExplodedTime explodedTime; michael@0: PR_ExplodeTime(mNotBefore, PR_GMTParameters, &explodedTime); michael@0: dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatSecondsForce24Hour, michael@0: &explodedTime, date); michael@0: aNotBeforeGMT = date; michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute PRTime notAfter; */ michael@0: NS_IMETHODIMP nsX509CertValidity::GetNotAfter(PRTime *aNotAfter) michael@0: { michael@0: NS_ENSURE_ARG(aNotAfter); michael@0: michael@0: nsresult rv = NS_ERROR_FAILURE; michael@0: if (mTimesInitialized) { michael@0: *aNotAfter = mNotAfter; michael@0: rv = NS_OK; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsX509CertValidity::GetNotAfterLocalTime(nsAString &aNotAfterLocaltime) michael@0: { michael@0: if (!mTimesInitialized) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr dateFormatter = michael@0: do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsAutoString date; michael@0: PRExplodedTime explodedTime; michael@0: PR_ExplodeTime(mNotAfter, PR_LocalTimeParameters, &explodedTime); michael@0: dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatSecondsForce24Hour, michael@0: &explodedTime, date); michael@0: aNotAfterLocaltime = date; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsX509CertValidity::GetNotAfterLocalDay(nsAString &aNotAfterLocalDay) michael@0: { michael@0: if (!mTimesInitialized) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr dateFormatter = michael@0: do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsAutoString date; michael@0: PRExplodedTime explodedTime; michael@0: PR_ExplodeTime(mNotAfter, PR_LocalTimeParameters, &explodedTime); michael@0: dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatNone, michael@0: &explodedTime, date); michael@0: aNotAfterLocalDay = date; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsX509CertValidity::GetNotAfterGMT(nsAString &aNotAfterGMT) michael@0: { michael@0: if (!mTimesInitialized) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr dateFormatter = michael@0: do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsAutoString date; michael@0: PRExplodedTime explodedTime; michael@0: PR_ExplodeTime(mNotAfter, PR_GMTParameters, &explodedTime); michael@0: dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatShort, kTimeFormatSecondsForce24Hour, michael@0: &explodedTime, date); michael@0: aNotAfterGMT = date; michael@0: return NS_OK; michael@0: }