All files / lib/internal/crypto sig.js

98.04% Statements 301/307
88.88% Branches 48/54
100% Functions 14/14
98.04% Lines 301/307

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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 146x 146x 146x 146x 146x 146x 146x 146x 146x 101x 101x 101x 101x 101x 22x 22x 101x 101x 101x 752x 752x 752x 218x 752x 28x 28x 28x 752x 752x 752x 101x 101x 1710x 1710x 1710x 101x 1702x 1702x 1702x 101x 1694x 1694x 1537x 1537x 1537x 74x 74x 6x 6x 1694x 1694x 1694x 101x 3412x 3412x 3412x 1929x 1913x 1913x 1929x 1929x 3412x 3412x 101x 101x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 133x 133x 101x 101x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 197x 131x 131x 131x 131x 131x 131x 197x 197x 42x 42x 197x 197x 197x 101x 597x 597x 597x 597x 597x 597x 597x 597x 597x 101x 101x 101x 101x 101x 101x 101x 101x 592x 592x 592x 592x 592x 592x 592x 592x 592x 592x 592x 592x 592x 592x 592x 592x 592x 592x 592x 592x 101x 101x 806x 806x 806x 806x 806x 806x 806x 806x 806x 806x             806x 806x 806x 806x 806x 806x 806x 806x 806x 5x 5x 5x 5x 5x 5x 796x 796x 796x 796x 796x 796x 796x 796x 796x 806x 806x 806x 806x 806x 806x 806x 806x 806x 806x 806x 806x 806x 806x 632x 632x 632x 632x 632x 632x 159x 159x 159x 159x 159x 159x 806x 101x 101x 101x 101x 101x 101x 101x  
'use strict';
 
const {
  FunctionPrototypeCall,
  ObjectSetPrototypeOf,
  ReflectApply,
} = primordials;
 
const {
  codes: {
    ERR_CRYPTO_SIGN_KEY_REQUIRED,
    ERR_INVALID_ARG_TYPE,
    ERR_INVALID_ARG_VALUE,
  }
} = require('internal/errors');
 
const {
  validateFunction,
  validateEncoding,
  validateString,
} = require('internal/validators');
 
const {
  Sign: _Sign,
  SignJob,
  Verify: _Verify,
  kCryptoJobAsync,
  kCryptoJobSync,
  kSigEncDER,
  kSigEncP1363,
  kSignJobModeSign,
  kSignJobModeVerify,
} = internalBinding('crypto');
 
const {
  getArrayBufferOrView,
  getDefaultEncoding,
  kHandle,
} = require('internal/crypto/util');
 
const {
  preparePrivateKey,
  preparePublicOrPrivateKey,
} = require('internal/crypto/keys');
 
const { Writable } = require('stream');
 
const { Buffer } = require('buffer');
 
const {
  isArrayBufferView,
} = require('internal/util/types');
 
function Sign(algorithm, options) {
  if (!(this instanceof Sign))
    return new Sign(algorithm, options);
  validateString(algorithm, 'algorithm');
  this[kHandle] = new _Sign();
  this[kHandle].init(algorithm);
 
  ReflectApply(Writable, this, [options]);
}
 
ObjectSetPrototypeOf(Sign.prototype, Writable.prototype);
ObjectSetPrototypeOf(Sign, Writable);
 
Sign.prototype._write = function _write(chunk, encoding, callback) {
  this.update(chunk, encoding);
  callback();
};
 
Sign.prototype.update = function update(data, encoding) {
  encoding = encoding || getDefaultEncoding();
 
  if (typeof data === 'string') {
    validateEncoding(data, encoding);
  } else if (!isArrayBufferView(data)) {
    throw new ERR_INVALID_ARG_TYPE(
      'data', ['string', 'Buffer', 'TypedArray', 'DataView'], data);
  }
 
  this[kHandle].update(data, encoding);
  return this;
};
 
function getPadding(options) {
  return getIntOption('padding', options);
}
 
function getSaltLength(options) {
  return getIntOption('saltLength', options);
}
 
function getDSASignatureEncoding(options) {
  if (typeof options === 'object') {
    const { dsaEncoding = 'der' } = options;
    if (dsaEncoding === 'der')
      return kSigEncDER;
    else if (dsaEncoding === 'ieee-p1363')
      return kSigEncP1363;
    throw new ERR_INVALID_ARG_VALUE('options.dsaEncoding', dsaEncoding);
  }
 
  return kSigEncDER;
}
 
function getIntOption(name, options) {
  const value = options[name];
  if (value !== undefined) {
    if (value === value >> 0) {
      return value;
    }
    throw new ERR_INVALID_ARG_VALUE(`options.${name}`, value);
  }
  return undefined;
}
 
Sign.prototype.sign = function sign(options, encoding) {
  if (!options)
    throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
 
  const { data, format, type, passphrase } = preparePrivateKey(options, true);
 
  // Options specific to RSA
  const rsaPadding = getPadding(options);
  const pssSaltLength = getSaltLength(options);
 
  // Options specific to (EC)DSA
  const dsaSigEnc = getDSASignatureEncoding(options);
 
  const ret = this[kHandle].sign(data, format, type, passphrase, rsaPadding,
                                 pssSaltLength, dsaSigEnc);
 
  encoding = encoding || getDefaultEncoding();
  if (encoding && encoding !== 'buffer')
    return ret.toString(encoding);
 
  return ret;
};
 
function signOneShot(algorithm, data, key, callback) {
  if (algorithm != null)
    validateString(algorithm, 'algorithm');
 
  if (callback !== undefined)
    validateFunction(callback, 'callback');
 
  data = getArrayBufferOrView(data, 'data');
 
  if (!key)
    throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
 
  // Options specific to RSA
  const rsaPadding = getPadding(key);
  const pssSaltLength = getSaltLength(key);
 
  // Options specific to (EC)DSA
  const dsaSigEnc = getDSASignatureEncoding(key);
 
  const {
    data: keyData,
    format: keyFormat,
    type: keyType,
    passphrase: keyPassphrase
  } = preparePrivateKey(key);
 
  const job = new SignJob(
    callback ? kCryptoJobAsync : kCryptoJobSync,
    kSignJobModeSign,
    keyData,
    keyFormat,
    keyType,
    keyPassphrase,
    data,
    algorithm,
    pssSaltLength,
    rsaPadding,
    dsaSigEnc);
 
  if (!callback) {
    const { 0: err, 1: signature } = job.run();
    if (err !== undefined)
      throw err;
 
    return Buffer.from(signature);
  }
 
  job.ondone = (error, signature) => {
    if (error) return FunctionPrototypeCall(callback, job, error);
    FunctionPrototypeCall(callback, job, null, Buffer.from(signature));
  };
  job.run();
}
 
function Verify(algorithm, options) {
  if (!(this instanceof Verify))
    return new Verify(algorithm, options);
  validateString(algorithm, 'algorithm');
  this[kHandle] = new _Verify();
  this[kHandle].init(algorithm);
 
  ReflectApply(Writable, this, [options]);
}
 
ObjectSetPrototypeOf(Verify.prototype, Writable.prototype);
ObjectSetPrototypeOf(Verify, Writable);
 
Verify.prototype._write = Sign.prototype._write;
Verify.prototype.update = Sign.prototype.update;
 
Verify.prototype.verify = function verify(options, signature, sigEncoding) {
  const {
    data,
    format,
    type,
    passphrase
  } = preparePublicOrPrivateKey(options, true);
 
  sigEncoding = sigEncoding || getDefaultEncoding();
 
  // Options specific to RSA
  const rsaPadding = getPadding(options);
  const pssSaltLength = getSaltLength(options);
 
  // Options specific to (EC)DSA
  const dsaSigEnc = getDSASignatureEncoding(options);
 
  signature = getArrayBufferOrView(signature, 'signature', sigEncoding);
 
  return this[kHandle].verify(data, format, type, passphrase, signature,
                              rsaPadding, pssSaltLength, dsaSigEnc);
};
 
function verifyOneShot(algorithm, data, key, signature, callback) {
  if (algorithm != null)
    validateString(algorithm, 'algorithm');
 
  if (callback !== undefined)
    validateFunction(callback, 'callback');
 
  data = getArrayBufferOrView(data, 'data');
 
  if (!isArrayBufferView(data)) {
    throw new ERR_INVALID_ARG_TYPE(
      'data',
      ['Buffer', 'TypedArray', 'DataView'],
      data
    );
  }
 
  // Options specific to RSA
  const rsaPadding = getPadding(key);
  const pssSaltLength = getSaltLength(key);
 
  // Options specific to (EC)DSA
  const dsaSigEnc = getDSASignatureEncoding(key);
 
  if (!isArrayBufferView(signature)) {
    throw new ERR_INVALID_ARG_TYPE(
      'signature',
      ['Buffer', 'TypedArray', 'DataView'],
      signature
    );
  }
 
  const {
    data: keyData,
    format: keyFormat,
    type: keyType,
    passphrase: keyPassphrase
  } = preparePublicOrPrivateKey(key);
 
  const job = new SignJob(
    callback ? kCryptoJobAsync : kCryptoJobSync,
    kSignJobModeVerify,
    keyData,
    keyFormat,
    keyType,
    keyPassphrase,
    data,
    algorithm,
    pssSaltLength,
    rsaPadding,
    dsaSigEnc,
    signature);
 
  if (!callback) {
    const { 0: err, 1: result } = job.run();
    if (err !== undefined)
      throw err;
 
    return result;
  }
 
  job.ondone = (error, result) => {
    if (error) return FunctionPrototypeCall(callback, job, error);
    FunctionPrototypeCall(callback, job, null, result);
  };
  job.run();
}
 
module.exports = {
  Sign,
  signOneShot,
  Verify,
  verifyOneShot,
};