Source: key-locator.js

  1. /**
  2. * This class represents an NDN KeyLocator object.
  3. * Copyright (C) 2014-2016 Regents of the University of California.
  4. * @author: Meki Cheraoui
  5. * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. * A copy of the GNU Lesser General Public License is in the file COPYING.
  20. */
  21. /** @ignore */
  22. var Blob = require('./util/blob.js').Blob; /** @ignore */
  23. var ChangeCounter = require('./util/change-counter.js').ChangeCounter; /** @ignore */
  24. var Name = require('./name.js').Name;
  25. /**
  26. * KeyLocator
  27. */
  28. var KeyLocatorType = {
  29. KEYNAME: 1,
  30. KEY_LOCATOR_DIGEST: 2
  31. };
  32. exports.KeyLocatorType = KeyLocatorType;
  33. /**
  34. * @constructor
  35. */
  36. var KeyLocator = function KeyLocator(input, type)
  37. {
  38. if (typeof input === 'object' && input instanceof KeyLocator) {
  39. // Copy from the input KeyLocator.
  40. this.type_ = input.type_;
  41. this.keyName_ = new ChangeCounter(new Name(input.getKeyName()));
  42. this.keyData_ = input.keyData_;
  43. }
  44. else {
  45. this.type_ = type;
  46. this.keyName_ = new ChangeCounter(new Name());
  47. this.keyData_ = new Blob();
  48. if (type == KeyLocatorType.KEYNAME)
  49. this.keyName_.set(typeof input === 'object' && input instanceof Name ?
  50. new Name(input) : new Name());
  51. else if (type == KeyLocatorType.KEY_LOCATOR_DIGEST)
  52. this.keyData_ = new Blob(input);
  53. }
  54. this.changeCount_ = 0;
  55. };
  56. exports.KeyLocator = KeyLocator;
  57. /**
  58. * Get the key locator type. If KeyLocatorType.KEYNAME, you may also
  59. * getKeyName(). If KeyLocatorType.KEY_LOCATOR_DIGEST, you may also
  60. * getKeyData() to get the digest.
  61. * @returns {number} The key locator type, or null if not specified.
  62. */
  63. KeyLocator.prototype.getType = function() { return this.type_; };
  64. /**
  65. * Get the key name. This is meaningful if getType() is KeyLocatorType.KEYNAME.
  66. * @returns {Name} The key name. If not specified, the Name is empty.
  67. */
  68. KeyLocator.prototype.getKeyName = function()
  69. {
  70. return this.keyName_.get();
  71. };
  72. /**
  73. * Get the key data. If getType() is KeyLocatorType.KEY_LOCATOR_DIGEST, this is
  74. * the digest bytes.
  75. * @returns {Blob} The key data, or null if not specified.
  76. */
  77. KeyLocator.prototype.getKeyData = function()
  78. {
  79. return this.keyData_;
  80. };
  81. /**
  82. * @deprecated Use getKeyData. This method returns a Buffer which is the former
  83. * behavior of getKeyData, and should only be used while updating your code.
  84. */
  85. KeyLocator.prototype.getKeyDataAsBuffer = function()
  86. {
  87. return this.getKeyData().buf();
  88. };
  89. /**
  90. * Set the key locator type. If KeyLocatorType.KEYNAME, you must also
  91. * setKeyName(). If KeyLocatorType.KEY_LOCATOR_DIGEST, you must also
  92. * setKeyData() to the digest.
  93. * @param {number} type The key locator type. If null, the type is unspecified.
  94. */
  95. KeyLocator.prototype.setType = function(type)
  96. {
  97. this.type_ = type;
  98. ++this.changeCount_;
  99. };
  100. /**
  101. * Set key name to a copy of the given Name. This is the name if getType()
  102. * is KeyLocatorType.KEYNAME.
  103. * @param {Name} name The key name which is copied.
  104. */
  105. KeyLocator.prototype.setKeyName = function(name)
  106. {
  107. this.keyName_.set(typeof name === 'object' && name instanceof Name ?
  108. new Name(name) : new Name());
  109. ++this.changeCount_;
  110. };
  111. /**
  112. * Set the key data to the given value. This is the digest bytes if getType() is
  113. * KeyLocatorType.KEY_LOCATOR_DIGEST.
  114. * @param {Blob} keyData A Blob with the key data bytes.
  115. */
  116. KeyLocator.prototype.setKeyData = function(keyData)
  117. {
  118. this.keyData_ = typeof keyData === 'object' && keyData instanceof Blob ?
  119. keyData : new Blob(keyData);
  120. ++this.changeCount_;
  121. };
  122. /**
  123. * Clear the keyData and set the type to not specified.
  124. */
  125. KeyLocator.prototype.clear = function()
  126. {
  127. this.type_ = null;
  128. this.keyName_.set(new Name());
  129. this.keyData_ = new Blob();
  130. ++this.changeCount_;
  131. };
  132. /**
  133. * If the signature is a type that has a KeyLocator (so that,
  134. * getFromSignature will succeed), return true.
  135. * Note: This is a static method of KeyLocator instead of a method of
  136. * Signature so that the Signature base class does not need to be overloaded
  137. * with all the different kinds of information that various signature
  138. * algorithms may use.
  139. * @param {Signature} signature An object of a subclass of Signature.
  140. * @returns {boolean} True if the signature is a type that has a KeyLocator,
  141. * otherwise false.
  142. */
  143. KeyLocator.canGetFromSignature = function(signature)
  144. {
  145. return signature instanceof Sha256WithRsaSignature ||
  146. signature instanceof HmacWithSha256Signature;
  147. }
  148. /**
  149. * If the signature is a type that has a KeyLocator, then return it. Otherwise
  150. * throw an error.
  151. * @param {Signature} signature An object of a subclass of Signature.
  152. * @returns {KeyLocator} The signature's KeyLocator. It is an error if signature
  153. * doesn't have a KeyLocator.
  154. */
  155. KeyLocator.getFromSignature = function(signature)
  156. {
  157. if (signature instanceof Sha256WithRsaSignature ||
  158. signature instanceof HmacWithSha256Signature)
  159. return signature.getKeyLocator();
  160. else
  161. throw new Error
  162. ("KeyLocator.getFromSignature: Signature type does not have a KeyLocator");
  163. }
  164. /**
  165. * Get the change count, which is incremented each time this object (or a child
  166. * object) is changed.
  167. * @returns {number} The change count.
  168. */
  169. KeyLocator.prototype.getChangeCount = function()
  170. {
  171. // Make sure each of the checkChanged is called.
  172. var changed = this.keyName_.checkChanged();
  173. if (changed)
  174. // A child object has changed, so update the change count.
  175. ++this.changeCount_;
  176. return this.changeCount_;
  177. };
  178. // Define properties so we can change member variable types and implement changeCount_.
  179. Object.defineProperty(KeyLocator.prototype, "type",
  180. { get: function() { return this.getType(); },
  181. set: function(val) { this.setType(val); } });
  182. /**
  183. * @@deprecated Use getKeyData and setKeyData.
  184. */
  185. Object.defineProperty(KeyLocator.prototype, "keyData",
  186. { get: function() { return this.getKeyDataAsBuffer(); },
  187. set: function(val) { this.setKeyData(val); } });
  188. // Put this last to avoid a require loop.
  189. var Sha256WithRsaSignature = require('./sha256-with-rsa-signature.js').Sha256WithRsaSignature;
  190. var HmacWithSha256Signature = require('./hmac-with-sha256-signature.js').HmacWithSha256Signature;