Source: security/tpm/tpm-key-handle-memory.js

  1. /**
  2. * Copyright (C) 2017-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/tpm/key-handle-mem.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 Blob = require('../../util/blob.js').Blob; /** @ignore */
  22. var SyncPromise = require('../../util/sync-promise.js').SyncPromise; /** @ignore */
  23. var DigestAlgorithm = require('../security-types.js').DigestAlgorithm; /** @ignore */
  24. var TpmPrivateKey = require('./tpm-private-key.js').TpmPrivateKey; /** @ignore */
  25. var TpmBackEnd = require('./tpm-back-end.js').TpmBackEnd; /** @ignore */
  26. var TpmKeyHandle = require('./tpm-key-handle.js').TpmKeyHandle;
  27. /**
  28. * TpmKeyHandleMemory extends TpmKeyHandle to implement a TPM key handle that
  29. * keeps the private key in memory.
  30. *
  31. * Create a TpmKeyHandleMemory to use the given in-memory key.
  32. * @param {TpmPrivateKey} key The in-memory key.
  33. * @constructor
  34. */
  35. var TpmKeyHandleMemory = function TpmKeyHandleMemory(key)
  36. {
  37. // Call the base constructor.
  38. TpmKeyHandle.call(this);
  39. if (key == null)
  40. throw new Error("The key is null");
  41. this.key_ = key;
  42. };
  43. TpmKeyHandleMemory.prototype = new TpmKeyHandle();
  44. TpmKeyHandleMemory.prototype.name = "TpmKeyHandleMemory";
  45. exports.TpmKeyHandleMemory = TpmKeyHandleMemory;
  46. /**
  47. * A protected method to do the work of sign().
  48. * @param {number} digestAlgorithm The digest algorithm as an int from the
  49. * DigestAlgorithm enum.
  50. * @param {Buffer} data The input byte buffer.
  51. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  52. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  53. * an async Promise.
  54. * @return {Promise|SyncPromise} A promise which returns the signature Blob (or
  55. * an isNull Blob for an unrecognized digestAlgorithm), or a promise rejected
  56. * with TpmBackEnd.Error for an error in signing.
  57. */
  58. TpmKeyHandleMemory.prototype.doSignPromise_ = function
  59. (digestAlgorithm, data, useSync)
  60. {
  61. if (digestAlgorithm == DigestAlgorithm.SHA256) {
  62. return this.key_.signPromise(data, digestAlgorithm, useSync)
  63. .catch(function(err) {
  64. return SyncPromise.reject(new TpmBackEnd.Error(new Error
  65. ("Error in TpmPrivateKey.sign: " + err)));
  66. });
  67. }
  68. else
  69. return SyncPromise.resolve(new Blob());
  70. };
  71. /**
  72. * A protected method to do the work of decrypt().
  73. * @param {Buffer} cipherText The cipher text byte buffer.
  74. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  75. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  76. * an async Promise.
  77. * @return {Promise|SyncPromise} A promise which returns the decrypted data Blob,
  78. * or a promise rejected with TpmPrivateKey.Error for error decrypting.
  79. */
  80. TpmKeyHandleMemory.prototype.doDecryptPromise_ = function(cipherText, useSync)
  81. {
  82. return this.key_.decryptPromise(cipherText, useSync)
  83. .catch(function(err) {
  84. return SyncPromise.reject(new TpmBackEnd.Error(new Error
  85. ("Error in TpmPrivateKey.decrypt: " + err)));
  86. });
  87. };
  88. /**
  89. * A protected method to do the work of derivePublicKey().
  90. * @return {Blob} The public key encoding Blob.
  91. */
  92. TpmKeyHandle.prototype.doDerivePublicKey_ = function()
  93. {
  94. try {
  95. return this.key_.derivePublicKey();
  96. } catch (ex) {
  97. throw new TpmBackEnd.Error(new Error
  98. ("Error in TpmPrivateKey.derivePublicKey: " + ex));
  99. }
  100. };