示例#1
0
function read(fd, buffer, offset, length, position, callback) {
  validateUint32(fd, 'fd');
  validateBuffer(buffer);

  offset |= 0;
  length |= 0;

  if (length === 0) {
    return process.nextTick(function tick() {
      callback && callback(null, 0, buffer);
    });
  }

  validateOffsetLengthRead(offset, length, buffer.length);

  if (!isUint32(position))
    position = -1;

  function wrapper(err, bytesRead) {
    // Retain a reference to buffer so that it can't be GC'ed too soon.
    callback && callback(err, bytesRead || 0, buffer);
  }

  const req = new FSReqWrap();
  req.oncomplete = wrapper;

  binding.read(fd, buffer, offset, length, position, req);
}
示例#2
0
function readSync(fd, buffer, offset, length, position) {
  validateUint32(fd, 'fd');
  validateBuffer(buffer);

  offset |= 0;
  length |= 0;

  if (length === 0) {
    return 0;
  }

  validateOffsetLengthRead(offset, length, buffer.length);

  if (!isUint32(position))
    position = -1;

  const ctx = {};
  const result = binding.read(fd, buffer, offset, length, position,
                              undefined, ctx);
  handleErrorFromBinding(ctx);
  return result;
}
示例#3
0
文件: keygen.js 项目: reneweb/node
function check(type, options, callback) {
  if (typeof type !== 'string')
    throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
  if (options == null || typeof options !== 'object')
    throw new ERR_INVALID_ARG_TYPE('options', 'object', options);

  // These will be set after parsing the type and type-specific options to make
  // the order a bit more intuitive.
  let cipher, passphrase, publicType, publicFormat, privateType, privateFormat;

  let impl;
  switch (type) {
    case 'rsa':
      {
        const { modulusLength } = options;
        if (!isUint32(modulusLength))
          throw new ERR_INVALID_OPT_VALUE('modulusLength', modulusLength);

        let { publicExponent } = options;
        if (publicExponent == null) {
          publicExponent = 0x10001;
        } else if (!isUint32(publicExponent)) {
          throw new ERR_INVALID_OPT_VALUE('publicExponent', publicExponent);
        }

        impl = (wrap) => generateKeyPairRSA(modulusLength, publicExponent,
                                            publicType, publicFormat,
                                            privateType, privateFormat,
                                            cipher, passphrase, wrap);
      }
      break;
    case 'dsa':
      {
        const { modulusLength } = options;
        if (!isUint32(modulusLength))
          throw new ERR_INVALID_OPT_VALUE('modulusLength', modulusLength);

        let { divisorLength } = options;
        if (divisorLength == null) {
          divisorLength = -1;
        } else if (!isUint32(divisorLength)) {
          throw new ERR_INVALID_OPT_VALUE('divisorLength', divisorLength);
        }

        impl = (wrap) => generateKeyPairDSA(modulusLength, divisorLength,
                                            publicType, publicFormat,
                                            privateType, privateFormat,
                                            cipher, passphrase, wrap);
      }
      break;
    case 'ec':
      {
        const { namedCurve } = options;
        if (typeof namedCurve !== 'string')
          throw new ERR_INVALID_OPT_VALUE('namedCurve', namedCurve);
        let { paramEncoding } = options;
        if (paramEncoding == null || paramEncoding === 'named')
          paramEncoding = OPENSSL_EC_NAMED_CURVE;
        else if (paramEncoding === 'explicit')
          paramEncoding = OPENSSL_EC_EXPLICIT_CURVE;
        else
          throw new ERR_INVALID_OPT_VALUE('paramEncoding', paramEncoding);

        impl = (wrap) => generateKeyPairEC(namedCurve, paramEncoding,
                                           publicType, publicFormat,
                                           privateType, privateFormat,
                                           cipher, passphrase, wrap);
      }
      break;
    default:
      throw new ERR_INVALID_ARG_VALUE('type', type,
                                      "must be one of 'rsa', 'dsa', 'ec'");
  }

  ({
    cipher,
    passphrase,
    publicType,
    publicFormat,
    privateType,
    privateFormat
  } = parseKeyEncoding(type, options));

  return impl;
}
示例#4
0
function check(type, options, callback) {
  validateString(type, 'type');

  // These will be set after parsing the type and type-specific options to make
  // the order a bit more intuitive.
  let cipher, passphrase, publicType, publicFormat, privateType, privateFormat;

  if (options !== undefined && typeof options !== 'object')
    throw new ERR_INVALID_ARG_TYPE('options', 'object', options);

  function needOptions() {
    if (options == null)
      throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
    return options;
  }

  let impl;
  switch (type) {
    case 'rsa':
    case 'rsa-pss':
      {
        const { modulusLength } = needOptions();
        if (!isUint32(modulusLength))
          throw new ERR_INVALID_OPT_VALUE('modulusLength', modulusLength);

        let { publicExponent } = options;
        if (publicExponent == null) {
          publicExponent = 0x10001;
        } else if (!isUint32(publicExponent)) {
          throw new ERR_INVALID_OPT_VALUE('publicExponent', publicExponent);
        }

        if (type === 'rsa') {
          impl = (wrap) => generateKeyPairRSA(modulusLength, publicExponent,
                                              publicFormat, publicType,
                                              privateFormat, privateType,
                                              cipher, passphrase, wrap);
          break;
        }

        const { hash, mgf1Hash, saltLength } = options;
        if (hash !== undefined && typeof hash !== 'string')
          throw new ERR_INVALID_OPT_VALUE('hash', hash);
        if (mgf1Hash !== undefined && typeof mgf1Hash !== 'string')
          throw new ERR_INVALID_OPT_VALUE('mgf1Hash', mgf1Hash);
        if (saltLength !== undefined && !isUint32(saltLength))
          throw new ERR_INVALID_OPT_VALUE('saltLength', saltLength);

        impl = (wrap) => generateKeyPairRSAPSS(modulusLength, publicExponent,
                                               hash, mgf1Hash, saltLength,
                                               publicFormat, publicType,
                                               privateFormat, privateType,
                                               cipher, passphrase, wrap);
      }
      break;
    case 'dsa':
      {
        const { modulusLength } = needOptions();
        if (!isUint32(modulusLength))
          throw new ERR_INVALID_OPT_VALUE('modulusLength', modulusLength);

        let { divisorLength } = options;
        if (divisorLength == null) {
          divisorLength = -1;
        } else if (!isUint32(divisorLength)) {
          throw new ERR_INVALID_OPT_VALUE('divisorLength', divisorLength);
        }

        impl = (wrap) => generateKeyPairDSA(modulusLength, divisorLength,
                                            publicFormat, publicType,
                                            privateFormat, privateType,
                                            cipher, passphrase, wrap);
      }
      break;
    case 'ec':
      {
        const { namedCurve } = needOptions();
        if (typeof namedCurve !== 'string')
          throw new ERR_INVALID_OPT_VALUE('namedCurve', namedCurve);
        let { paramEncoding } = options;
        if (paramEncoding == null || paramEncoding === 'named')
          paramEncoding = OPENSSL_EC_NAMED_CURVE;
        else if (paramEncoding === 'explicit')
          paramEncoding = OPENSSL_EC_EXPLICIT_CURVE;
        else
          throw new ERR_INVALID_OPT_VALUE('paramEncoding', paramEncoding);

        impl = (wrap) => generateKeyPairEC(namedCurve, paramEncoding,
                                           publicFormat, publicType,
                                           privateFormat, privateType,
                                           cipher, passphrase, wrap);
      }
      break;
    case 'ed25519':
    case 'ed448':
    case 'x25519':
    case 'x448':
      {
        let id;
        switch (type) {
          case 'ed25519':
            id = EVP_PKEY_ED25519;
            break;
          case 'ed448':
            id = EVP_PKEY_ED448;
            break;
          case 'x25519':
            id = EVP_PKEY_X25519;
            break;
          case 'x448':
            id = EVP_PKEY_X448;
            break;
        }
        impl = (wrap) => generateKeyPairNid(id,
                                            publicFormat, publicType,
                                            privateFormat, privateType,
                                            cipher, passphrase, wrap);
      }
      break;
    default:
      throw new ERR_INVALID_ARG_VALUE('type', type,
                                      'must be a supported key type');
  }

  if (options) {
    ({
      cipher,
      passphrase,
      publicType,
      publicFormat,
      privateType,
      privateFormat
    } = parseKeyEncoding(type, options));
  }

  return impl;
}