Source: security/v2/dynamic-trust-anchor-group.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/trust-anchor-group.cpp
  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 fs = require('fs'); /** @ignore */
  22. var path = require('path'); /** @ignore */
  23. var TrustAnchorGroup = require('./trust-anchor-group.js').TrustAnchorGroup; /** @ignore */
  24. var Name = require('../../name.js').Name; /** @ignore */
  25. var LOG = require('../../log.js').Log.LOG;
  26. /**
  27. * The DynamicTrustAnchorGroup class extends TrustAnchorGroup to implement a
  28. * dynamic trust anchor group.
  29. *
  30. * Create a DynamicTrustAnchorGroup to use an existing container.
  31. * @param {CertificateContainer} certificateContainer The existing certificate
  32. * container which implements the CertificateContainer interface.
  33. * @param {string} id The group ID.
  34. * @param {string} path The file path for trust anchor(s), which could be a
  35. * directory or a file. If it is a directory, all the certificates in the
  36. * directory will be loaded.
  37. * @param {number} refreshPeriod The refresh time in milliseconds for the
  38. * anchors under path. This must be positive.
  39. * @param {boolean} isDirectory If true, then path is a directory. If false, it
  40. * is a single file.
  41. * @throws Error If refreshPeriod is not positive.
  42. * @constructor
  43. */
  44. var DynamicTrustAnchorGroup = function DynamicTrustAnchorGroup
  45. (certificateContainer, id, path, refreshPeriod, isDirectory)
  46. {
  47. // Call the base constructor.
  48. TrustAnchorGroup.call(this, certificateContainer, id);
  49. this.isDirectory_ = isDirectory;
  50. this.path_ = path;
  51. this.refreshPeriod_ = refreshPeriod;
  52. this.expireTime_ = 0;
  53. if (refreshPeriod <= 0)
  54. throw new Error("Refresh period for the dynamic group must be positive");
  55. if (LOG > 0)
  56. console.log("Create a dynamic trust anchor group " + id + " for file/dir " +
  57. path + " with refresh time " + refreshPeriod);
  58. this.refresh();
  59. };
  60. DynamicTrustAnchorGroup.prototype = new TrustAnchorGroup();
  61. DynamicTrustAnchorGroup.prototype.name = "DynamicTrustAnchorGroup";
  62. exports.DynamicTrustAnchorGroup = DynamicTrustAnchorGroup;
  63. /**
  64. * Request a certificate refresh.
  65. */
  66. DynamicTrustAnchorGroup.prototype.refresh = function()
  67. {
  68. var now = new Date().getTime();
  69. if (this.expireTime_ > now)
  70. return;
  71. this.expireTime_ = now + this.refreshPeriod_;
  72. if (LOG > 0)
  73. console.log("Reloading the dynamic trust anchor group");
  74. // Save a copy of anchorNameUris_ .
  75. var oldAnchorNameUris = {};
  76. for (var uri in this.anchorNameUris_)
  77. oldAnchorNameUris[uri] = true;
  78. if (!this.isDirectory_)
  79. this.loadCertificate_(this.path_, oldAnchorNameUris);
  80. else {
  81. var allFiles;
  82. try {
  83. allFiles = fs.readdirSync(this.path_);
  84. }
  85. catch (e) {
  86. throw new Error("Cannot list files in directory " + this.path_);
  87. }
  88. for (var i = 0; i < allFiles.length; ++i)
  89. this.loadCertificate_(path.join(this.path_, allFiles[i]), oldAnchorNameUris);
  90. }
  91. // Remove old certificates.
  92. for (var uri in oldAnchorNameUris) {
  93. delete this.anchorNameUris_[uri];
  94. this.certificates_.remove(new Name(uri));
  95. }
  96. };
  97. /**
  98. * @param {string} file
  99. * @param {object} oldAnchorNameUris The keys are the set of anchor name URIs,
  100. * and each value is true.
  101. */
  102. DynamicTrustAnchorGroup.prototype.loadCertificate_ = function
  103. (file, oldAnchorNameUris)
  104. {
  105. var certificate = TrustAnchorGroup.readCertificate(file);
  106. if (certificate != null) {
  107. var certificateNameUri = certificate.getName().toUri();
  108. if (!this.anchorNameUris_[certificateNameUri]) {
  109. this.anchorNameUris_[certificateNameUri] = true;
  110. this.certificates_.add(certificate);
  111. }
  112. else
  113. delete oldAnchorNameUris[certificateNameUri];
  114. }
  115. };