All files / lib/internal/crypto dsa.js

95.11% Statements 214/225
75.86% Branches 22/29
100% Functions 5/5
95.11% Lines 214/225

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 2264x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 44x 44x 44x 44x 22x 22x 44x 22x 22x 44x 44x         44x 4x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 4x 8x 8x 8x 8x 8x 8x 4x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 2x 2x 2x 2x 2x 2x 2x 2x 44x 21x 21x 21x 21x 21x 21x 21x 21x 44x 21x 21x 21x 21x 21x 21x 21x 21x 44x       44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 4x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 4x 4x 4x 4x 4x 4x 4x  
'use strict';
 
const {
  Promise,
  SafeSet,
} = primordials;
 
const {
  DSAKeyExportJob,
  SignJob,
  kCryptoJobAsync,
  kSigEncDER,
  kSignJobModeSign,
  kSignJobModeVerify,
} = internalBinding('crypto');
 
const {
  codes: {
    ERR_INVALID_ARG_TYPE,
    ERR_MISSING_OPTION,
  }
} = require('internal/errors');
 
const {
  validateUint32,
} = require('internal/validators');
 
const {
  InternalCryptoKey,
  createPrivateKey,
  createPublicKey,
  isKeyObject,
} = require('internal/crypto/keys');
 
const {
  generateKeyPair,
} = require('internal/crypto/keygen');
 
const {
  getUsagesUnion,
  hasAnyNotIn,
  jobPromise,
  normalizeHashName,
  kKeyObject,
  kHandle,
} = require('internal/crypto/util');
 
const {
  lazyDOMException,
} = require('internal/util');
 
function verifyAcceptableDsaKeyUse(name, type, usages) {
  let checkSet;
  switch (type) {
    case 'private':
      checkSet = ['sign'];
      break;
    case 'public':
      checkSet = ['verify'];
      break;
  }
  if (hasAnyNotIn(usages, checkSet)) {
    throw lazyDOMException(
      `Unsupported key usage for an ${name} key`,
      'SyntaxError');
  }
}
 
async function dsaGenerateKey(
  algorithm,
  extractable,
  keyUsages) {
  const {
    name,
    modulusLength,
    divisorLength,
    hash
  } = algorithm;
 
  if (hash === undefined)
    throw new ERR_MISSING_OPTION('algorithm.hash');
  validateUint32(modulusLength, 'algorithm.modulusLength');
 
  const usageSet = new SafeSet(keyUsages);
 
  if (hasAnyNotIn(usageSet, ['sign', 'verify'])) {
    throw lazyDOMException(
      'Unsupported key usage for a DSA key',
      'SyntaxError');
  }
 
  return new Promise((resolve, reject) => {
    generateKeyPair('dsa', {
      modulusLength,
      divisorLength,
    }, (err, pubKey, privKey) => {
      if (err) {
        return reject(lazyDOMException(
          'The operation failed for an operation-specific reason',
          'OperationError'));
      }
 
      const algorithm = {
        name,
        modulusLength,
        divisorLength,
        hash: { name: hash.name }
      };
 
      const publicKey =
        new InternalCryptoKey(
          pubKey,
          algorithm,
          getUsagesUnion(usageSet, 'verify'),
          true);
 
      const privateKey =
        new InternalCryptoKey(
          privKey,
          algorithm,
          getUsagesUnion(usageSet, 'sign'),
          extractable);
 
      resolve({ publicKey, privateKey });
    });
  });
}
 
function dsaExportKey(key, format) {
  return jobPromise(new DSAKeyExportJob(
    kCryptoJobAsync,
    format,
    key[kKeyObject][kHandle]));
}
 
async function dsaImportKey(
  format,
  keyData,
  algorithm,
  extractable,
  keyUsages) {
  const { hash } = algorithm;
  if (hash === undefined)
    throw new ERR_MISSING_OPTION('algorithm.hash');
 
  const usagesSet = new SafeSet(keyUsages);
  let keyObject;
  switch (format) {
    case 'node.keyObject': {
      if (!isKeyObject(keyData))
        throw new ERR_INVALID_ARG_TYPE('keyData', 'KeyObject', keyData);
      if (keyData.type === 'secret')
        throw lazyDOMException('Invalid key type', 'InvalidAccessException');
      verifyAcceptableDsaKeyUse(algorithm.name, keyData.type, usagesSet);
      keyObject = keyData;
      break;
    }
    case 'spki': {
      verifyAcceptableDsaKeyUse(algorithm.name, 'public', usagesSet);
      keyObject = createPublicKey({
        key: keyData,
        format: 'der',
        type: 'spki'
      });
      break;
    }
    case 'pkcs8': {
      verifyAcceptableDsaKeyUse(algorithm.name, 'private', usagesSet);
      keyObject = createPrivateKey({
        key: keyData,
        format: 'der',
        type: 'pkcs8'
      });
      break;
    }
    default:
      throw lazyDOMException(
        `Unable to import DSA key with format ${format}`,
        'NotSupportedError');
  }
 
  if (keyObject.asymmetricKeyType !== 'dsa')
    throw lazyDOMException('Invalid key type', 'DataError');
 
  const {
    modulusLength,
    divisorLength,
  } = keyObject[kHandle].keyDetail({});
 
  return new InternalCryptoKey(keyObject, {
    name: algorithm.name,
    modulusLength,
    divisorLength,
    hash: algorithm.hash
  }, keyUsages, extractable);
}
 
function dsaSignVerify(key, data, algorithm, signature) {
  const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
  const type = mode === kSignJobModeSign ? 'private' : 'public';
 
  if (key.type !== type)
    throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError');
 
  return jobPromise(new SignJob(
    kCryptoJobAsync,
    signature === undefined ? kSignJobModeSign : kSignJobModeVerify,
    key[kKeyObject][kHandle],
    undefined,
    undefined,
    undefined,
    data,
    normalizeHashName(key.algorithm.hash.name),
    undefined,  // Salt-length is not used in DSA
    undefined,  // Padding is not used in DSA
    kSigEncDER,
    signature));
}
 
module.exports = {
  dsaExportKey,
  dsaGenerateKey,
  dsaImportKey,
  dsaSignVerify,
};