Source: util/command-interest-generator.js

  1. /**
  2. * Copyright (C) 2014-2018 Regents of the University of California.
  3. * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. * A copy of the GNU Lesser General Public License is in the file COPYING.
  18. */
  19. var WireFormat = require('../encoding/wire-format.js').WireFormat; /** @ignore */
  20. var CommandInterestPreparer = require('../security/command-interest-preparer.js').CommandInterestPreparer;
  21. /**
  22. * A CommandInterestGenerator keeps track of a timestamp and generates command
  23. * interests according to the NFD Signed Command Interests protocol:
  24. * http://redmine.named-data.net/projects/nfd/wiki/Command_Interests
  25. *
  26. * Create a new CommandInterestGenerator and initialize the timestamp to now.
  27. * @constructor
  28. */
  29. var CommandInterestGenerator = function CommandInterestGenerator()
  30. {
  31. // Call the base constructor.
  32. CommandInterestPreparer.call(this);
  33. };
  34. CommandInterestGenerator.prototype = new CommandInterestPreparer();
  35. CommandInterestGenerator.prototype.name = "CommandInterestGenerator";
  36. exports.CommandInterestGenerator = CommandInterestGenerator;
  37. /**
  38. * Append a timestamp component and a random value component to interest's name.
  39. * This ensures that the timestamp is greater than the timestamp used in the
  40. * previous call. Then use keyChain to sign the interest which appends a
  41. * SignatureInfo component and a component with the signature bits. If the
  42. * interest lifetime is not set, this sets it.
  43. * @param {Interest} interest The interest whose name is append with components.
  44. * @param {KeyChain} keyChain The KeyChain for calling sign.
  45. * @param {Name} certificateName The certificate name of the key to use for
  46. * signing.
  47. * @param {WireFormat} wireFormat (optional) A WireFormat object used to encode
  48. * the SignatureInfo and to encode interest name for signing. If omitted, use
  49. * WireFormat.getDefaultWireFormat().
  50. * @param {function} onComplete (optional) This calls onComplete() when complete.
  51. * (Some crypto/database libraries only use a callback, so onComplete is
  52. * required to use these.)
  53. */
  54. CommandInterestGenerator.prototype.generate = function
  55. (interest, keyChain, certificateName, wireFormat, onComplete)
  56. {
  57. onComplete = (typeof wireFormat === "function") ? wireFormat : onComplete;
  58. wireFormat = (typeof wireFormat === "function" || !wireFormat) ?
  59. WireFormat.getDefaultWireFormat() : wireFormat;
  60. this.prepareCommandInterestName(interest, wireFormat);
  61. keyChain.sign(interest, certificateName, wireFormat, function() {
  62. if (interest.getInterestLifetimeMilliseconds() == null ||
  63. interest.getInterestLifetimeMilliseconds() < 0)
  64. // The caller has not set the interest lifetime, so set it here.
  65. interest.setInterestLifetimeMilliseconds(1000.0);
  66. if (onComplete)
  67. onComplete();
  68. });
  69. };