Source: security/v2/interest-validation-state.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/validation-state.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 SyncPromise = require('../../util/sync-promise.js').SyncPromise; /** @ignore */
  22. var Interest = require('../../interest.js').Interest; /** @ignore */
  23. var LOG = require('../../log.js').Log.LOG; /** @ignore */
  24. var ValidationError = require('./validation-error.js').ValidationError; /** @ignore */
  25. var ValidationState = require('./validation-state.js').ValidationState; /** @ignore */
  26. var VerificationHelpers = require('../verification-helpers.js').VerificationHelpers; /** @ignore */
  27. var NdnCommon = require('../../util/ndn-common.js').NdnCommon;
  28. /**
  29. * The InterestValidationState class extends ValidationState to hold the
  30. * validation state for an Interest packet.
  31. *
  32. * Create an InterestValidationState for the Interest packet. The caller must
  33. * ensure that the state instance is valid until the validation finishes (i.e.,
  34. * until validateCertificateChain() and validateOriginalPacket() have been
  35. * called).
  36. * @param {Interest} interest The Interest packet being validated, which is copied.
  37. * @param {function} successCallback This calls successCallback(interest) to
  38. * report a successful Interest validation.
  39. * @param {function} failureCallback This calls failureCallback(interest, error)
  40. * to report a failed Interest validation, where error is a ValidationError.
  41. * @constructor
  42. */
  43. var InterestValidationState = function InterestValidationState
  44. (interest, successCallback, failureCallback)
  45. {
  46. // Call the base constructor.
  47. ValidationState.call(this);
  48. // Make a copy.
  49. this.interest_ = new Interest(interest);
  50. this.successCallbacks_ = [successCallback]; // of SuccessCallback function
  51. this.failureCallback_ = failureCallback;
  52. if (successCallback == null)
  53. throw new Error("The successCallback is null");
  54. if (this.failureCallback_ == null)
  55. throw new Error("The failureCallback is null");
  56. };
  57. InterestValidationState.prototype = new ValidationState();
  58. InterestValidationState.prototype.name = "InterestValidationState";
  59. exports.InterestValidationState = InterestValidationState;
  60. /**
  61. * Call the failure callback.
  62. * @param {ValidationError} error
  63. */
  64. InterestValidationState.prototype.fail = function(error)
  65. {
  66. if (LOG > 3) console.log("" + error);
  67. try {
  68. this.failureCallback_(this.interest_, error);
  69. } catch (ex) {
  70. console.log("Error in failureCallback: " + NdnCommon.getErrorWithStackTrace(ex));
  71. }
  72. this.setOutcome(false);
  73. };
  74. /**
  75. * Get the original Interest packet being validated which was given to the
  76. * constructor.
  77. * @return {Interest} The original Interest packet.
  78. */
  79. InterestValidationState.prototype.getOriginalInterest = function()
  80. {
  81. return this.interest_;
  82. };
  83. /**
  84. * @param {function} successCallback This calls successCallback(interest).
  85. */
  86. InterestValidationState.prototype.addSuccessCallback = function(successCallback)
  87. {
  88. this.successCallbacks_.push(successCallback);
  89. };
  90. /**
  91. * Override to verify the Interest packet given to the constructor.
  92. * @param {CertificateV2} trustedCertificate The certificate that signs the
  93. * original packet.
  94. * @return {Promise|SyncPromise} A promise that resolves when the success or
  95. * failure callback has been called.
  96. */
  97. InterestValidationState.prototype.verifyOriginalPacketPromise_ = function
  98. (trustedCertificate)
  99. {
  100. var thisState = this;
  101. return VerificationHelpers.verifyInterestSignaturePromise
  102. (this.interest_, trustedCertificate)
  103. .then(function(verifySuccess) {
  104. if (verifySuccess) {
  105. if (LOG > 3) console.log("OK signature for interest `" +
  106. thisState.interest_.getName().toUri() + "`");
  107. for (var i = 0; i < thisState.successCallbacks_.length; ++i) {
  108. try {
  109. thisState.successCallbacks_[i](thisState.interest_);
  110. } catch (ex) {
  111. console.log("Error in successCallback: " + NdnCommon.getErrorWithStackTrace(ex));
  112. }
  113. }
  114. thisState.setOutcome(true);
  115. }
  116. else
  117. thisState.fail(new ValidationError(ValidationError.INVALID_SIGNATURE,
  118. "Invalid signature of interest `" +
  119. thisState.interest_.getName().toUri() + "`"));
  120. return SyncPromise.resolve();
  121. });
  122. };
  123. /**
  124. * Override to call the success callback using the Interest packet given to the
  125. * constructor.
  126. */
  127. InterestValidationState.prototype.bypassValidation_ = function()
  128. {
  129. if (LOG > 3) console.log("Signature verification bypassed for interest `" +
  130. this.interest_.getName().toUri() + "`");
  131. for (var i = 0; i < this.successCallbacks_.length; ++i) {
  132. try {
  133. this.successCallbacks_[i](this.interest_);
  134. } catch (ex) {
  135. console.log("Error in successCallback: " + NdnCommon.getErrorWithStackTrace(ex));
  136. }
  137. }
  138. this.setOutcome(true);
  139. };