OCSPCache.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This code is made available to you under your choice of the following sets
  3. * of licensing terms:
  4. */
  5. /* This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. */
  9. /* Copyright 2013 Mozilla Contributors
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #ifndef mozilla_psm_OCSPCache_h
  24. #define mozilla_psm_OCSPCache_h
  25. #include "hasht.h"
  26. #include "mozilla/Mutex.h"
  27. #include "mozilla/Vector.h"
  28. #include "pkix/Result.h"
  29. #include "pkix/Time.h"
  30. #include "prerror.h"
  31. #include "seccomon.h"
  32. namespace mozilla {
  33. class NeckoOriginAttributes;
  34. }
  35. namespace mozilla { namespace pkix {
  36. struct CertID;
  37. } } // namespace mozilla::pkix
  38. namespace mozilla { namespace psm {
  39. // make SHA384Buffer be of type "array of uint8_t of length SHA384_LENGTH"
  40. typedef uint8_t SHA384Buffer[SHA384_LENGTH];
  41. // OCSPCache can store and retrieve OCSP response verification results. Each
  42. // result is keyed on the certificate that purportedly corresponds to it (where
  43. // certificates are distinguished based on serial number, issuer, and
  44. // issuer public key, much like in an encoded OCSP response itself). A maximum
  45. // of 1024 distinct entries can be stored.
  46. // OCSPCache is thread-safe.
  47. class OCSPCache
  48. {
  49. public:
  50. OCSPCache();
  51. ~OCSPCache();
  52. // Returns true if the status of the given certificate (issued by the given
  53. // issuer) is in the cache, and false otherwise.
  54. // If it is in the cache, returns by reference the error code of the cached
  55. // status and the time through which the status is considered trustworthy.
  56. // The passed in origin attributes are used to isolate the OCSP cache.
  57. // We currently only use the first party domain portion of the attributes, and
  58. // it is non-empty only when "privacy.firstParty.isolate" is enabled.
  59. bool Get(const mozilla::pkix::CertID& aCertID,
  60. const NeckoOriginAttributes& aOriginAttributes,
  61. /*out*/ mozilla::pkix::Result& aResult,
  62. /*out*/ mozilla::pkix::Time& aValidThrough);
  63. // Caches the status of the given certificate (issued by the given issuer).
  64. // The status is considered trustworthy through the given time.
  65. // A status with an error code of SEC_ERROR_REVOKED_CERTIFICATE will not
  66. // be replaced or evicted.
  67. // A status with an error code of SEC_ERROR_OCSP_UNKNOWN_CERT will not
  68. // be evicted when the cache is full.
  69. // A status with a more recent thisUpdate will not be replaced with a
  70. // status with a less recent thisUpdate unless the less recent status
  71. // indicates the certificate is revoked.
  72. // The passed in origin attributes are used to isolate the OCSP cache.
  73. // We currently only use the first party domain portion of the attributes, and
  74. // it is non-empty only when "privacy.firstParty.isolate" is enabled.
  75. mozilla::pkix::Result Put(const mozilla::pkix::CertID& aCertID,
  76. const NeckoOriginAttributes& aOriginAttributes,
  77. mozilla::pkix::Result aResult,
  78. mozilla::pkix::Time aThisUpdate,
  79. mozilla::pkix::Time aValidThrough);
  80. // Removes everything from the cache.
  81. void Clear();
  82. private:
  83. class Entry
  84. {
  85. public:
  86. Entry(mozilla::pkix::Result aResult,
  87. mozilla::pkix::Time aThisUpdate,
  88. mozilla::pkix::Time aValidThrough)
  89. : mResult(aResult)
  90. , mThisUpdate(aThisUpdate)
  91. , mValidThrough(aValidThrough)
  92. {
  93. }
  94. mozilla::pkix::Result Init(const mozilla::pkix::CertID& aCertID,
  95. const NeckoOriginAttributes& aOriginAttributes);
  96. mozilla::pkix::Result mResult;
  97. mozilla::pkix::Time mThisUpdate;
  98. mozilla::pkix::Time mValidThrough;
  99. // The SHA-384 hash of the concatenation of the DER encodings of the
  100. // issuer name and issuer key, followed by the length of the serial number,
  101. // the serial number, the length of the first party domain, and the first
  102. // party domain (if "privacy.firstparty.isolate" is enabled).
  103. // See the documentation for CertIDHash in OCSPCache.cpp.
  104. SHA384Buffer mIDHash;
  105. };
  106. bool FindInternal(const mozilla::pkix::CertID& aCertID,
  107. const NeckoOriginAttributes& aOriginAttributes,
  108. /*out*/ size_t& index,
  109. const MutexAutoLock& aProofOfLock);
  110. void MakeMostRecentlyUsed(size_t aIndex, const MutexAutoLock& aProofOfLock);
  111. Mutex mMutex;
  112. static const size_t MaxEntries = 1024;
  113. // Sorted with the most-recently-used entry at the end.
  114. // Using 256 here reserves as much possible inline storage as the vector
  115. // implementation will give us. 1024 bytes is the maximum it allows,
  116. // which results in 256 Entry pointers or 128 Entry pointers, depending
  117. // on the size of a pointer.
  118. Vector<Entry*, 256> mEntries;
  119. };
  120. } } // namespace mozilla::psm
  121. #endif // mozilla_psm_OCSPCache_h