Source: encrypt/encrypted-content.js

  1. /**
  2. * Copyright (C) 2015-2018 Regents of the University of California.
  3. * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  4. * @author: From ndn-group-encrypt src/encrypted-content https://github.com/named-data/ndn-group-encrypt
  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 KeyLocator = require('../key-locator.js').KeyLocator; /** @ignore */
  22. var WireFormat = require('../encoding/wire-format.js').WireFormat; /** @ignore */
  23. var Blob = require('../util/blob.js').Blob;
  24. /**
  25. * An EncryptedContent holds an encryption type, a payload and other fields
  26. * representing encrypted content.
  27. * @param {EncryptedContent} (optional) If value is another EncryptedContent
  28. * then copy it. If value is omitted then create an EncryptedContent with
  29. * unspecified values.
  30. * @note This class is an experimental feature. The API may change.
  31. * @constructor
  32. */
  33. var EncryptedContent = function EncryptedContent(value)
  34. {
  35. if (typeof value === 'object' && value instanceof EncryptedContent) {
  36. // Make a deep copy.
  37. this.algorithmType_ = value.algorithmType_;
  38. this.keyLocator_ = new KeyLocator(value.keyLocator_);
  39. this.initialVector_ = value.initialVector_;
  40. this.payload_ = value.payload_;
  41. }
  42. else {
  43. this.algorithmType_ = null;
  44. this.keyLocator_ = new KeyLocator();
  45. this.initialVector_ = new Blob();
  46. this.payload_ = new Blob();
  47. }
  48. };
  49. exports.EncryptedContent = EncryptedContent;
  50. /**
  51. * Get the algorithm type from EncryptAlgorithmType.
  52. * @return {number} The algorithm type from EncryptAlgorithmType, or null if
  53. * not specified.
  54. */
  55. EncryptedContent.prototype.getAlgorithmType = function()
  56. {
  57. return this.algorithmType_;
  58. };
  59. /**
  60. * Get the key locator.
  61. * @return {KeyLocator} The key locator. If not specified, getType() is null.
  62. */
  63. EncryptedContent.prototype.getKeyLocator = function()
  64. {
  65. return this.keyLocator_;
  66. };
  67. /**
  68. * Get the initial vector.
  69. * @return {Blob} The initial vector. If not specified, isNull() is true.
  70. */
  71. EncryptedContent.prototype.getInitialVector = function()
  72. {
  73. return this.initialVector_;
  74. };
  75. /**
  76. * Get the payload.
  77. * @return {Blob} The payload. If not specified, isNull() is true.
  78. */
  79. EncryptedContent.prototype.getPayload = function()
  80. {
  81. return this.payload_;
  82. };
  83. /**
  84. * Set the algorithm type.
  85. * @param {number} algorithmType The algorithm type from EncryptAlgorithmType.
  86. * If not specified, set to null.
  87. * @return {EncryptedContent} This EncryptedContent so that you can chain calls
  88. * to update values.
  89. */
  90. EncryptedContent.prototype.setAlgorithmType = function(algorithmType)
  91. {
  92. this.algorithmType_ = algorithmType;
  93. return this;
  94. };
  95. /**
  96. * Set the key locator.
  97. * @param {KeyLocator} keyLocator The key locator. This makes a copy of the
  98. * object. If not specified, set to the default KeyLocator().
  99. * @return {EncryptedContent} This EncryptedContent so that you can chain calls
  100. * to update values.
  101. */
  102. EncryptedContent.prototype.setKeyLocator = function(keyLocator)
  103. {
  104. this.keyLocator_ = typeof keyLocator === 'object' &&
  105. keyLocator instanceof KeyLocator ?
  106. new KeyLocator(keyLocator) : new KeyLocator();
  107. return this;
  108. };
  109. /**
  110. * Set the initial vector.
  111. * @param {Blob} initialVector The initial vector. If not specified, set to the
  112. * default Blob() where isNull() is true.
  113. * @return {EncryptedContent} This EncryptedContent so that you can chain calls
  114. * to update values.
  115. */
  116. EncryptedContent.prototype.setInitialVector = function(initialVector)
  117. {
  118. this.initialVector_ =
  119. typeof initialVector === 'object' && initialVector instanceof Blob ?
  120. initialVector : new Blob(initialVector);
  121. return this;
  122. };
  123. /**
  124. * Set the encrypted payload.
  125. * @param {Blob} payload The payload. If not specified, set to the default Blob()
  126. * where isNull() is true.
  127. * @return {EncryptedContent} This EncryptedContent so that you can chain calls
  128. * to update values.
  129. */
  130. EncryptedContent.prototype.setPayload = function(payload)
  131. {
  132. this.payload_ = typeof payload === 'object' && payload instanceof Blob ?
  133. payload : new Blob(payload);
  134. return this;
  135. };
  136. /**
  137. * Encode this EncryptedContent for a particular wire format.
  138. * @param {WireFormat} wireFormat (optional) A WireFormat object used to encode
  139. * this object. If omitted, use WireFormat.getDefaultWireFormat().
  140. * @return {Blob} The encoded buffer in a Blob object.
  141. */
  142. EncryptedContent.prototype.wireEncode = function(wireFormat)
  143. {
  144. wireFormat = (wireFormat || WireFormat.getDefaultWireFormat());
  145. return wireFormat.encodeEncryptedContent(this);
  146. };
  147. /**
  148. * Decode the input using a particular wire format and update this
  149. * EncryptedContent.
  150. * @param {Blob|Buffer} input The buffer with the bytes to decode.
  151. * @param {WireFormat} wireFormat (optional) A WireFormat object used to decode
  152. * this object. If omitted, use WireFormat.getDefaultWireFormat().
  153. */
  154. EncryptedContent.prototype.wireDecode = function(input, wireFormat)
  155. {
  156. wireFormat = (wireFormat || WireFormat.getDefaultWireFormat());
  157. if (typeof input === 'object' && input instanceof Blob)
  158. // Input is a blob, so get its buf() and set copy false.
  159. wireFormat.decodeEncryptedContent(this, input.buf(), false);
  160. else
  161. wireFormat.decodeEncryptedContent(this, input, true);
  162. };