All files / src/classes/MFKDFDerivedKey envelope.js

100% Statements 59/59
100% Branches 36/36
100% Functions 9/9
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245                    1x                                                       17x 16x 15x 14x 13x   13x   13x           1x                                                             34x 33x 39x   1x                                                           4x 3x 4x   1x                                                   9x 8x   7x                 7x 4x 4x 3x 1x 1x 2x 1x   1x     6x   6x   1x                                                     15x 14x 25x 13x 13x   1x                                                   11x 10x   10x           1x  
/**
 * @file Multi-Factor Derived Key Enveloped Secret Functions
 * @copyright Multifactor 2022 All Rights Reserved
 *
 * @description
 * Enveloped secret operations using a multi-factor derived key
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
 
const crypto = require('crypto')
 
/**
 * Add enveloped secret to a multi-factor derived key
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 *
 * // add enveloped secret to key
 * await key.addEnvelopedSecret('mySecret', Buffer.from('hello world'))
 *
 * // later... derive key
 * const derived = await mfkdf.derive.key(key.policy, { password: mfkdf.derive.factors.password('password') })
 *
 * // retrieve secret
 * const secret = await derived.getEnvelopedSecret('mySecret')
 * secret.toString() // -> hello world
 *
 * @param {string} id - String which uniquely identifies the enveloped secret to add
 * @param {Buffer} value - The plaintext secret value to be encrypted with this key
 * @param {string} [type='raw'] - The type of the enveloped secret to add
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.20.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function addEnvelopedSecret (id, value, type = 'raw') {
  if (typeof id !== 'string') throw new TypeError('id must be a string')
  if (!Buffer.isBuffer(value)) throw new TypeError('value must be a buffer')
  if (typeof type !== 'string') throw new TypeError('type must be a string')
  if (this.hasEnvelopedSecret(id)) throw new RangeError('id must be unique')
  if (!Array.isArray(this.policy.secrets)) this.policy.secrets = []
 
  const ct = await this.encrypt(value)
 
  this.policy.secrets.push({
    id,
    value: ct.toString('base64'),
    type
  })
}
module.exports.addEnvelopedSecret = addEnvelopedSecret
 
/**
 * Check if multi-factor derived key has enveloped secret with id
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 *
 * // add enveloped secret to key
 * await key.addEnvelopedSecret('mySecret', Buffer.from('hello world'))
 *
 * // later... derive key
 * const derived = await mfkdf.derive.key(key.policy, { password: mfkdf.derive.factors.password('password') })
 *
 * // check secret
 * const check1 = derived.hasEnvelopedSecret('mySecret') // -> true
 *
 * // remove secret
 * derived.removeEnvelopedSecret('mySecret')
 *
 * // check secret
 * const check2 = derived.hasEnvelopedSecret('mySecret') // -> false
 *
 * @param {string} id - String which uniquely identifies the enveloped secret
 * @returns {boolean} - Whether the key has enveloped secret with given id
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.20.0
 * @memberOf MFKDFDerivedKey
 */
function hasEnvelopedSecret (id) {
  if (typeof id !== 'string') throw new TypeError('id must be a string')
  if (!Array.isArray(this.policy.secrets)) return false
  return this.policy.secrets.some(x => x.id === id)
}
module.exports.hasEnvelopedSecret = hasEnvelopedSecret
 
/**
 * Remove enveloped secret from a multi-factor derived key
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 *
 * // add enveloped secret to key
 * await key.addEnvelopedSecret('mySecret', Buffer.from('hello world'))
 *
 * // later... derive key
 * const derived = await mfkdf.derive.key(key.policy, { password: mfkdf.derive.factors.password('password') })
 *
 * // check secret
 * const check1 = derived.hasEnvelopedSecret('mySecret') // -> true
 *
 * // remove secret
 * derived.removeEnvelopedSecret('mySecret')
 *
 * // check secret
 * const check2 = derived.hasEnvelopedSecret('mySecret') // -> false
 *
 * @param {string} id - ID of the enveloped secret to remove
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.20.0
 * @memberOf MFKDFDerivedKey
 */
function removeEnvelopedSecret (id) {
  if (typeof id !== 'string') throw new TypeError('id must be a string')
  if (!this.hasEnvelopedSecret(id)) throw new RangeError('secret with id does not exist')
  this.policy.secrets = this.policy.secrets.filter(x => x.id !== id)
}
module.exports.removeEnvelopedSecret = removeEnvelopedSecret
 
/**
 * Add enveloped key to a multi-factor derived key
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 *
 * // add enveloped rsa1024 key
 * await key.addEnvelopedKey('myKey', 'rsa1024')
 *
 * // later... derive key
 * const derived = await mfkdf.derive.key(key.policy, { password: mfkdf.derive.factors.password('password') })
 *
 * // retrieve enveloped key
 * const enveloped = await derived.getEnvelopedKey('myKey') // -> PrivateKeyObject
 *
 * @param {string} id - String which uniquely identifies the enveloped key to add
 * @param {string} [type='rsa1024'] - The type of the enveloped key to add; rsa1024, rsa2048, or ed25519
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.20.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function addEnvelopedKey (id, type = 'rsa1024') {
  if (typeof id !== 'string') throw new TypeError('id must be a string')
  if (typeof type !== 'string') throw new TypeError('type must be a string')
 
  const options = {
    privateKeyEncoding: {
      type: 'pkcs8',
      format: 'der'
    }
  }
 
  let myType
 
  if (type === 'rsa1024') {
    myType = 'rsa'
    options.modulusLength = 1024
  } else if (type === 'rsa2048') {
    myType = 'rsa'
    options.modulusLength = 2048
  } else if (type === 'ed25519') {
    myType = 'ed25519'
  } else {
    throw new RangeError('invalid key type')
  }
 
  const keyPair = await crypto.generateKeyPairSync(myType, options)
 
  return await this.addEnvelopedSecret(id, keyPair.privateKey, type)
}
module.exports.addEnvelopedKey = addEnvelopedKey
 
/**
 * Get enveloped secret from a multi-factor derived key
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 *
 * // add enveloped secret to key
 * await key.addEnvelopedSecret('mySecret', Buffer.from('hello world'))
 *
 * // later... derive key
 * const derived = await mfkdf.derive.key(key.policy, { password: mfkdf.derive.factors.password('password') })
 *
 * // retrieve secret
 * const secret = await derived.getEnvelopedSecret('mySecret')
 * secret.toString() // -> hello world
 *
 * @param {string} id - ID of the enveloped secret to get
 * @returns {Buffer} The retrieved plaintext secret value
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.20.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function getEnvelopedSecret (id) {
  if (typeof id !== 'string') throw new TypeError('id must be a string')
  if (!this.hasEnvelopedSecret(id)) throw new RangeError('secret with id does not exist')
  const secret = this.policy.secrets.find(x => x.id === id)
  const ct = Buffer.from(secret.value, 'base64')
  return await this.decrypt(ct)
}
module.exports.getEnvelopedSecret = getEnvelopedSecret
 
/**
 * Get enveloped secret from a multi-factor derived key
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 *
 * // add enveloped rsa1024 key
 * await key.addEnvelopedKey('myKey', 'rsa1024')
 *
 * // later... derive key
 * const derived = await mfkdf.derive.key(key.policy, { password: mfkdf.derive.factors.password('password') })
 *
 * // retrieve enveloped key
 * const enveloped = await derived.getEnvelopedKey('myKey') // -> PrivateKeyObject
 *
 * @param {string} id - ID of the enveloped key to get
 * @returns {PrivateKeyObject} The retrieved enveloped key
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.20.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function getEnvelopedKey (id) {
  if (typeof id !== 'string') throw new TypeError('id must be a string')
  const privateKey = await this.getEnvelopedSecret(id)
 
  return await crypto.createPrivateKey({
    key: privateKey,
    format: 'der',
    type: 'pkcs8'
  })
}
module.exports.getEnvelopedKey = getEnvelopedKey