Back to index

lightning-sunbird  0.9+nobinonly
nsNSSCertValidity.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Ian McGreer <mcgreer@netscape.com>
00023  *   Javier Delgadillo <javi@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsNSSCertValidity.h"
00040 #include "nsNSSCertHeader.h"
00041 #include "nsCOMPtr.h"
00042 #include "nsIDateTimeFormat.h"
00043 #include "nsDateTimeFormatCID.h"
00044 #include "nsComponentManagerUtils.h"
00045 #include "nsReadableUtils.h"
00046 #include "nsNSSShutDown.h"
00047 
00048 static NS_DEFINE_CID(kDateTimeFormatCID, NS_DATETIMEFORMAT_CID);
00049 
00050 /* Implementation file */
00051 NS_IMPL_THREADSAFE_ISUPPORTS1(nsX509CertValidity, nsIX509CertValidity)
00052 
00053 nsX509CertValidity::nsX509CertValidity() : mTimesInitialized(PR_FALSE)
00054 {
00055   /* member initializers and constructor code */
00056 }
00057 
00058 nsX509CertValidity::nsX509CertValidity(CERTCertificate *cert) : 
00059                                            mTimesInitialized(PR_FALSE)
00060 {
00061   nsNSSShutDownPreventionLock locker;
00062   if (cert) {
00063     SECStatus rv = CERT_GetCertTimes(cert, &mNotBefore, &mNotAfter);
00064     if (rv == SECSuccess)
00065       mTimesInitialized = PR_TRUE;
00066   }
00067 }
00068 
00069 nsX509CertValidity::~nsX509CertValidity()
00070 {
00071   /* destructor code */
00072 }
00073 
00074 /* readonly attribute PRTime notBefore; */
00075 NS_IMETHODIMP nsX509CertValidity::GetNotBefore(PRTime *aNotBefore)
00076 {
00077   NS_ENSURE_ARG(aNotBefore);
00078 
00079   nsresult rv = NS_ERROR_FAILURE;
00080   if (mTimesInitialized) {
00081     *aNotBefore = mNotBefore;
00082     rv = NS_OK;
00083   }
00084   return rv;
00085 }
00086 
00087 NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalTime(nsAString &aNotBeforeLocalTime)
00088 {
00089   if (!mTimesInitialized)
00090     return NS_ERROR_FAILURE;
00091 
00092   nsresult rv;
00093   nsCOMPtr<nsIDateTimeFormat> dateFormatter =
00094      do_CreateInstance(kDateTimeFormatCID, &rv);
00095   if (NS_FAILED(rv)) return rv;
00096 
00097   nsAutoString date;
00098   PRExplodedTime explodedTime;
00099   PR_ExplodeTime(mNotBefore, PR_LocalTimeParameters, &explodedTime);
00100   dateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort, kTimeFormatSecondsForce24Hour,
00101                                       &explodedTime, date);
00102   aNotBeforeLocalTime = date;
00103   return NS_OK;
00104 }
00105 
00106 NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalDay(nsAString &aNotBeforeLocalDay)
00107 {
00108   if (!mTimesInitialized)
00109     return NS_ERROR_FAILURE;
00110 
00111   nsresult rv;
00112   nsCOMPtr<nsIDateTimeFormat> dateFormatter =
00113      do_CreateInstance(kDateTimeFormatCID, &rv);
00114   if (NS_FAILED(rv)) return rv;
00115 
00116   nsAutoString date;
00117   PRExplodedTime explodedTime;
00118   PR_ExplodeTime(mNotBefore, PR_LocalTimeParameters, &explodedTime);
00119   dateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort, kTimeFormatNone,
00120                                       &explodedTime, date);
00121   aNotBeforeLocalDay = date;
00122   return NS_OK;
00123 }
00124 
00125 
00126 NS_IMETHODIMP nsX509CertValidity::GetNotBeforeGMT(nsAString &aNotBeforeGMT)
00127 {
00128   if (!mTimesInitialized)
00129     return NS_ERROR_FAILURE;
00130 
00131   nsresult rv;
00132   nsCOMPtr<nsIDateTimeFormat> dateFormatter =
00133      do_CreateInstance(kDateTimeFormatCID, &rv);
00134   if (NS_FAILED(rv)) return rv;
00135 
00136   nsAutoString date;
00137   PRExplodedTime explodedTime;
00138   PR_ExplodeTime(mNotBefore, PR_GMTParameters, &explodedTime);
00139   dateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort, kTimeFormatSecondsForce24Hour,
00140                                       &explodedTime, date);
00141   aNotBeforeGMT = date;
00142   return NS_OK;
00143 }
00144 
00145 /* readonly attribute PRTime notAfter; */
00146 NS_IMETHODIMP nsX509CertValidity::GetNotAfter(PRTime *aNotAfter)
00147 {
00148   NS_ENSURE_ARG(aNotAfter);
00149 
00150   nsresult rv = NS_ERROR_FAILURE;
00151   if (mTimesInitialized) {
00152     *aNotAfter = mNotAfter;
00153     rv = NS_OK;
00154   }
00155   return rv;
00156 }
00157 
00158 NS_IMETHODIMP nsX509CertValidity::GetNotAfterLocalTime(nsAString &aNotAfterLocaltime)
00159 {
00160   if (!mTimesInitialized)
00161     return NS_ERROR_FAILURE;
00162 
00163   nsresult rv;
00164   nsCOMPtr<nsIDateTimeFormat> dateFormatter =
00165      do_CreateInstance(kDateTimeFormatCID, &rv);
00166   if (NS_FAILED(rv)) return rv;
00167 
00168   nsAutoString date;
00169   PRExplodedTime explodedTime;
00170   PR_ExplodeTime(mNotAfter, PR_LocalTimeParameters, &explodedTime);
00171   dateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort, kTimeFormatSecondsForce24Hour,
00172                                       &explodedTime, date);
00173   aNotAfterLocaltime = date;
00174   return NS_OK;
00175 }
00176 
00177 NS_IMETHODIMP nsX509CertValidity::GetNotAfterLocalDay(nsAString &aNotAfterLocalDay)
00178 {
00179   if (!mTimesInitialized)
00180     return NS_ERROR_FAILURE;
00181 
00182   nsresult rv;
00183   nsCOMPtr<nsIDateTimeFormat> dateFormatter =
00184      do_CreateInstance(kDateTimeFormatCID, &rv);
00185   if (NS_FAILED(rv)) return rv;
00186 
00187   nsAutoString date;
00188   PRExplodedTime explodedTime;
00189   PR_ExplodeTime(mNotAfter, PR_LocalTimeParameters, &explodedTime);
00190   dateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort, kTimeFormatNone,
00191                                       &explodedTime, date);
00192   aNotAfterLocalDay = date;
00193   return NS_OK;
00194 }
00195 
00196 NS_IMETHODIMP nsX509CertValidity::GetNotAfterGMT(nsAString &aNotAfterGMT)
00197 {
00198   if (!mTimesInitialized)
00199     return NS_ERROR_FAILURE;
00200 
00201   nsresult rv;
00202   nsCOMPtr<nsIDateTimeFormat> dateFormatter =
00203      do_CreateInstance(kDateTimeFormatCID, &rv);
00204   if (NS_FAILED(rv)) return rv;
00205 
00206   nsAutoString date;
00207   PRExplodedTime explodedTime;
00208   PR_ExplodeTime(mNotAfter, PR_GMTParameters, &explodedTime);
00209   dateFormatter->FormatPRExplodedTime(nsnull, kDateFormatShort, kTimeFormatSecondsForce24Hour,
00210                                       &explodedTime, date);
00211   aNotAfterGMT = date;
00212   return NS_OK;
00213 }