nsNSSCertValidity.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/. */
  4. #include "nsNSSCertValidity.h"
  5. #include "cert.h"
  6. #include "mozilla/Assertions.h"
  7. #include "nsCOMPtr.h"
  8. #include "nsComponentManagerUtils.h"
  9. #include "nsReadableUtils.h"
  10. NS_IMPL_ISUPPORTS(nsX509CertValidity, nsIX509CertValidity)
  11. nsX509CertValidity::nsX509CertValidity(const mozilla::UniqueCERTCertificate& cert)
  12. : mTimesInitialized(false)
  13. {
  14. MOZ_ASSERT(cert);
  15. if (!cert) {
  16. return;
  17. }
  18. nsNSSShutDownPreventionLock locker;
  19. if (isAlreadyShutDown()) {
  20. return;
  21. }
  22. if (CERT_GetCertTimes(cert.get(), &mNotBefore, &mNotAfter) == SECSuccess) {
  23. mTimesInitialized = true;
  24. }
  25. }
  26. nsX509CertValidity::~nsX509CertValidity()
  27. {
  28. nsNSSShutDownPreventionLock locker;
  29. if (isAlreadyShutDown()) {
  30. return;
  31. }
  32. shutdown(ShutdownCalledFrom::Object);
  33. }
  34. NS_IMETHODIMP
  35. nsX509CertValidity::GetNotBefore(PRTime* aNotBefore)
  36. {
  37. NS_ENSURE_ARG(aNotBefore);
  38. if (!mTimesInitialized) {
  39. return NS_ERROR_FAILURE;
  40. }
  41. *aNotBefore = mNotBefore;
  42. return NS_OK;
  43. }
  44. nsresult
  45. nsX509CertValidity::FormatTime(const PRTime& aTimeDate,
  46. PRTimeParamFn aParamFn,
  47. const nsTimeFormatSelector aTimeFormatSelector,
  48. nsAString& aFormattedTimeDate)
  49. {
  50. if (!mTimesInitialized)
  51. return NS_ERROR_FAILURE;
  52. nsCOMPtr<nsIDateTimeFormat> dateFormatter = nsIDateTimeFormat::Create();
  53. if (!dateFormatter) {
  54. return NS_ERROR_FAILURE;
  55. }
  56. PRExplodedTime explodedTime;
  57. PR_ExplodeTime(const_cast<PRTime&>(aTimeDate), aParamFn, &explodedTime);
  58. return dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatLong,
  59. aTimeFormatSelector,
  60. &explodedTime, aFormattedTimeDate);
  61. }
  62. NS_IMETHODIMP
  63. nsX509CertValidity::GetNotBeforeLocalTime(nsAString& aNotBeforeLocalTime)
  64. {
  65. return FormatTime(mNotBefore, PR_LocalTimeParameters,
  66. kTimeFormatSeconds, aNotBeforeLocalTime);
  67. }
  68. NS_IMETHODIMP
  69. nsX509CertValidity::GetNotBeforeLocalDay(nsAString& aNotBeforeLocalDay)
  70. {
  71. return FormatTime(mNotBefore, PR_LocalTimeParameters,
  72. kTimeFormatNone, aNotBeforeLocalDay);
  73. }
  74. NS_IMETHODIMP
  75. nsX509CertValidity::GetNotBeforeGMT(nsAString& aNotBeforeGMT)
  76. {
  77. return FormatTime(mNotBefore, PR_GMTParameters,
  78. kTimeFormatSeconds, aNotBeforeGMT);
  79. }
  80. NS_IMETHODIMP
  81. nsX509CertValidity::GetNotAfter(PRTime* aNotAfter)
  82. {
  83. NS_ENSURE_ARG(aNotAfter);
  84. if (!mTimesInitialized) {
  85. return NS_ERROR_FAILURE;
  86. }
  87. *aNotAfter = mNotAfter;
  88. return NS_OK;
  89. }
  90. NS_IMETHODIMP
  91. nsX509CertValidity::GetNotAfterLocalTime(nsAString& aNotAfterLocaltime)
  92. {
  93. return FormatTime(mNotAfter, PR_LocalTimeParameters,
  94. kTimeFormatSeconds, aNotAfterLocaltime);
  95. }
  96. NS_IMETHODIMP
  97. nsX509CertValidity::GetNotAfterLocalDay(nsAString& aNotAfterLocalDay)
  98. {
  99. return FormatTime(mNotAfter, PR_LocalTimeParameters,
  100. kTimeFormatNone, aNotAfterLocalDay);
  101. }
  102. NS_IMETHODIMP
  103. nsX509CertValidity::GetNotAfterGMT(nsAString& aNotAfterGMT)
  104. {
  105. return FormatTime(mNotAfter, PR_GMTParameters,
  106. kTimeFormatSeconds, aNotAfterGMT);
  107. }