Source: security/v2/certificate-storage.js

  1. /**
  2. * Copyright (C) 2018 Regents of the University of California.
  3. * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  4. * @author: From ndn-cxx security https://github.com/named-data/ndn-cxx/blob/master/src/security/v2/certificate-storage.hpp
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * A copy of the GNU Lesser General Public License is in the file COPYING.
  19. */
  20. /** @ignore */
  21. var Name = require('../../name.js').Name; /** @ignore */
  22. var TrustAnchorContainer = require('./trust-anchor-container.js').TrustAnchorContainer; /** @ignore */
  23. var CertificateV2 = require('./certificate-v2.js').CertificateV2; /** @ignore */
  24. var CertificateCacheV2 = require('./certificate-cache-v2.js').CertificateCacheV2;
  25. /**
  26. * The CertificateStorage class stores trusted anchors and has a verified
  27. * certificate cache, and an unverified certificate cache.
  28. *
  29. * @constructor
  30. */
  31. var CertificateStorage = function CertificateStorage()
  32. {
  33. this.trustAnchors_ = new TrustAnchorContainer();
  34. this.verifiedCertificateCache_ = new CertificateCacheV2(3600 * 1000.0);
  35. this.unverifiedCertificateCache_ = new CertificateCacheV2(300 * 1000.0);
  36. };
  37. exports.CertificateStorage = CertificateStorage;
  38. /**
  39. * Find a trusted certificate in the trust anchor container or in the
  40. * verified cache.
  41. * @param {Interest} interestForCertificate The Interest for the certificate.
  42. * @return {CertificateV2} The found certificate, or null if not found.
  43. */
  44. CertificateStorage.prototype.findTrustedCertificate = function
  45. (interestForCertificate)
  46. {
  47. var certificate = this.trustAnchors_.find(interestForCertificate);
  48. if (certificate != null)
  49. return certificate;
  50. certificate = this.verifiedCertificateCache_.find(interestForCertificate);
  51. return certificate;
  52. };
  53. /**
  54. * Check if the certificate with the given name prefix exists in the verified
  55. * cache, the unverified cache, or in the set of trust anchors.
  56. * @param {Name} certificatePrefix The certificate name prefix.
  57. * @return {boolean} True if the certificate is known.
  58. */
  59. CertificateStorage.prototype.isCertificateKnown = function(certificatePrefix)
  60. {
  61. return this.trustAnchors_.find(certificatePrefix) != null ||
  62. this.verifiedCertificateCache_.find(certificatePrefix) != null ||
  63. this.unverifiedCertificateCache_.find(certificatePrefix) != null;
  64. };
  65. /**
  66. * Cache the unverified certificate for a period of time (5 minutes).
  67. * @param {CertificateV2} certificate The certificate packet, which is copied.
  68. */
  69. CertificateStorage.prototype.cacheUnverifiedCertificate = function(certificate)
  70. {
  71. this.unverifiedCertificateCache_.insert(certificate);
  72. };
  73. /**
  74. * Get the trust anchor container.
  75. * @return {TrustAnchorContainer} The trust anchor container.
  76. */
  77. CertificateStorage.prototype.getTrustAnchors = function()
  78. {
  79. return this.trustAnchors_;
  80. };
  81. /**
  82. * Get the verified certificate cache.
  83. * @return {CertificateCacheV2} The verified certificate cache.
  84. */
  85. CertificateStorage.prototype.getVerifiedCertificateCache = function()
  86. {
  87. return this.verifiedCertificateCache_;
  88. };
  89. /**
  90. * Get the unverified certificate cache.
  91. * @return {CertificateCacheV2} The unverified certificate cache.
  92. */
  93. CertificateStorage.prototype.getUnverifiedCertificateCache = function()
  94. {
  95. return this.unverifiedCertificateCache_;
  96. };
  97. /**
  98. * There are two forms of loadAnchor:
  99. * loadAnchor(groupId, certificate) - Load a static trust anchor. Static trust
  100. * anchors are permanently associated with the validator and never expire.
  101. * loadAnchor(groupId, path, refreshPeriod, isDirectory) - Load dynamic trust
  102. * anchors. Dynamic trust anchors are associated with the validator for as long
  103. * as the underlying trust anchor file (or set of files) exists.
  104. * @param {String} groupId The certificate group id.
  105. * @param {CertificateV2} certificate The certificate to load as a trust anchor,
  106. * which is copied.
  107. * @param {String} path The path to load the trust anchors.
  108. * @param {number} refreshPeriod The refresh time in milliseconds for the
  109. * anchors under path. This must be positive. The relevant trust anchors will
  110. * only be updated when find is called.
  111. * @param {boolean} isDirectory (optional) If true, then path is a directory.
  112. * If false or omitted, it is a single file.
  113. */
  114. CertificateStorage.prototype.loadAnchor = function
  115. (groupId, certificateOrPath, refreshPeriod, isDirectory)
  116. {
  117. this.trustAnchors_.insert
  118. (groupId, certificateOrPath, refreshPeriod, isDirectory);
  119. };
  120. /**
  121. * Remove any previously loaded static or dynamic trust anchors.
  122. */
  123. CertificateStorage.prototype.resetAnchors = function()
  124. {
  125. this.trustAnchors_.clear();
  126. };
  127. /**
  128. * Cache the verified certificate a period of time (1 hour).
  129. * @param {CertificateV2} certificate The certificate object, which is copied.
  130. */
  131. CertificateStorage.prototype.cacheVerifiedCertificate = function(certificate)
  132. {
  133. this.verifiedCertificateCache_.insert(certificate);
  134. };
  135. /**
  136. * Remove any cached verified certificates.
  137. */
  138. CertificateStorage.prototype.resetVerifiedCertificates = function()
  139. {
  140. this.verifiedCertificateCache_.clear();
  141. };
  142. /**
  143. * Set the offset when the cache insert() and refresh() get the current time,
  144. * which should only be used for testing.
  145. * @param {number} nowOffsetMilliseconds The offset in milliseconds.
  146. */
  147. CertificateStorage.prototype.setCacheNowOffsetMilliseconds_ = function
  148. (nowOffsetMilliseconds)
  149. {
  150. this.verifiedCertificateCache_.setNowOffsetMilliseconds_(nowOffsetMilliseconds);
  151. this.unverifiedCertificateCache_.setNowOffsetMilliseconds_(nowOffsetMilliseconds);
  152. };