2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2012-10-22 17:37:20 +00:00
|
|
|
// Note: In 0.8 and before, crypto functions all defaulted to using
|
|
|
|
// binary-encoded strings rather than buffers.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
2020-04-10 11:04:10 +00:00
|
|
|
ObjectDefineProperty,
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperties,
|
|
|
|
} = primordials;
|
2019-04-09 07:55:53 +00:00
|
|
|
|
2017-09-06 15:10:34 +00:00
|
|
|
const {
|
|
|
|
assertCrypto,
|
|
|
|
deprecate
|
|
|
|
} = require('internal/util');
|
|
|
|
assertCrypto();
|
2010-05-04 20:49:00 +00:00
|
|
|
|
2018-02-27 13:55:32 +00:00
|
|
|
const {
|
|
|
|
ERR_CRYPTO_FIPS_FORCED,
|
|
|
|
ERR_CRYPTO_FIPS_UNAVAILABLE
|
|
|
|
} = require('internal/errors').codes;
|
2018-10-14 23:41:32 +00:00
|
|
|
const constants = internalBinding('constants').crypto;
|
2018-11-04 20:52:50 +00:00
|
|
|
const { getOptionValue } = require('internal/options');
|
|
|
|
const pendingDeprecation = getOptionValue('--pending-deprecation');
|
2019-02-18 05:53:12 +00:00
|
|
|
const { fipsMode } = internalBinding('config');
|
|
|
|
const fipsForced = getOptionValue('--force-fips');
|
2018-08-21 06:54:02 +00:00
|
|
|
const { getFipsCrypto, setFipsCrypto } = internalBinding('crypto');
|
2017-09-06 15:10:34 +00:00
|
|
|
const {
|
|
|
|
randomBytes,
|
|
|
|
randomFill,
|
|
|
|
randomFillSync
|
|
|
|
} = require('internal/crypto/random');
|
|
|
|
const {
|
|
|
|
pbkdf2,
|
|
|
|
pbkdf2Sync
|
|
|
|
} = require('internal/crypto/pbkdf2');
|
2018-05-18 09:05:20 +00:00
|
|
|
const {
|
|
|
|
scrypt,
|
|
|
|
scryptSync
|
|
|
|
} = require('internal/crypto/scrypt');
|
2018-09-02 15:00:01 +00:00
|
|
|
const {
|
|
|
|
generateKeyPair,
|
|
|
|
generateKeyPairSync
|
|
|
|
} = require('internal/crypto/keygen');
|
2018-09-20 17:53:44 +00:00
|
|
|
const {
|
|
|
|
createSecretKey,
|
|
|
|
createPublicKey,
|
2019-03-11 20:26:22 +00:00
|
|
|
createPrivateKey,
|
|
|
|
KeyObject,
|
2018-09-20 17:53:44 +00:00
|
|
|
} = require('internal/crypto/keys');
|
2017-09-06 15:10:34 +00:00
|
|
|
const {
|
|
|
|
DiffieHellman,
|
|
|
|
DiffieHellmanGroup,
|
2020-01-03 11:01:34 +00:00
|
|
|
ECDH,
|
|
|
|
diffieHellman
|
2017-09-06 15:10:34 +00:00
|
|
|
} = require('internal/crypto/diffiehellman');
|
|
|
|
const {
|
|
|
|
Cipher,
|
|
|
|
Cipheriv,
|
|
|
|
Decipher,
|
|
|
|
Decipheriv,
|
|
|
|
privateDecrypt,
|
|
|
|
privateEncrypt,
|
|
|
|
publicDecrypt,
|
|
|
|
publicEncrypt
|
|
|
|
} = require('internal/crypto/cipher');
|
|
|
|
const {
|
|
|
|
Sign,
|
2019-03-12 13:17:10 +00:00
|
|
|
signOneShot,
|
|
|
|
Verify,
|
|
|
|
verifyOneShot
|
2017-09-06 15:10:34 +00:00
|
|
|
} = require('internal/crypto/sig');
|
|
|
|
const {
|
|
|
|
Hash,
|
|
|
|
Hmac
|
|
|
|
} = require('internal/crypto/hash');
|
|
|
|
const {
|
|
|
|
getCiphers,
|
|
|
|
getCurves,
|
|
|
|
getDefaultEncoding,
|
|
|
|
getHashes,
|
|
|
|
setDefaultEncoding,
|
|
|
|
setEngine,
|
2019-01-04 12:35:00 +00:00
|
|
|
timingSafeEqual
|
2017-09-06 15:10:34 +00:00
|
|
|
} = require('internal/crypto/util');
|
|
|
|
const Certificate = require('internal/crypto/certificate');
|
|
|
|
|
2017-10-07 18:24:21 +00:00
|
|
|
// These helper functions are needed because the constructors can
|
|
|
|
// use new, in which case V8 cannot inline the recursive constructor call
|
|
|
|
function createHash(algorithm, options) {
|
|
|
|
return new Hash(algorithm, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createCipher(cipher, password, options) {
|
|
|
|
return new Cipher(cipher, password, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createCipheriv(cipher, key, iv, options) {
|
|
|
|
return new Cipheriv(cipher, key, iv, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createDecipher(cipher, password, options) {
|
|
|
|
return new Decipher(cipher, password, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createDecipheriv(cipher, key, iv, options) {
|
|
|
|
return new Decipheriv(cipher, key, iv, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createDiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
|
|
|
|
return new DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createDiffieHellmanGroup(name) {
|
|
|
|
return new DiffieHellmanGroup(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createECDH(curve) {
|
|
|
|
return new ECDH(curve);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createHmac(hmac, key, options) {
|
|
|
|
return new Hmac(hmac, key, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createSign(algorithm, options) {
|
|
|
|
return new Sign(algorithm, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createVerify(algorithm, options) {
|
|
|
|
return new Verify(algorithm, options);
|
|
|
|
}
|
|
|
|
|
2019-04-04 03:36:41 +00:00
|
|
|
module.exports = {
|
2017-09-06 15:10:34 +00:00
|
|
|
// Methods
|
2017-10-07 18:24:21 +00:00
|
|
|
createCipheriv,
|
|
|
|
createDecipheriv,
|
|
|
|
createDiffieHellman,
|
|
|
|
createDiffieHellmanGroup,
|
|
|
|
createECDH,
|
|
|
|
createHash,
|
|
|
|
createHmac,
|
2018-09-20 17:53:44 +00:00
|
|
|
createPrivateKey,
|
|
|
|
createPublicKey,
|
|
|
|
createSecretKey,
|
2017-10-07 18:24:21 +00:00
|
|
|
createSign,
|
|
|
|
createVerify,
|
2020-01-03 11:01:34 +00:00
|
|
|
diffieHellman,
|
2017-09-06 15:10:34 +00:00
|
|
|
getCiphers,
|
|
|
|
getCurves,
|
2017-10-07 18:24:21 +00:00
|
|
|
getDiffieHellman: createDiffieHellmanGroup,
|
2017-09-06 15:10:34 +00:00
|
|
|
getHashes,
|
|
|
|
pbkdf2,
|
|
|
|
pbkdf2Sync,
|
2018-09-02 15:00:01 +00:00
|
|
|
generateKeyPair,
|
|
|
|
generateKeyPairSync,
|
2017-09-06 15:10:34 +00:00
|
|
|
privateDecrypt,
|
|
|
|
privateEncrypt,
|
|
|
|
publicDecrypt,
|
|
|
|
publicEncrypt,
|
|
|
|
randomBytes,
|
|
|
|
randomFill,
|
|
|
|
randomFillSync,
|
2018-05-18 09:05:20 +00:00
|
|
|
scrypt,
|
|
|
|
scryptSync,
|
2019-03-12 13:17:10 +00:00
|
|
|
sign: signOneShot,
|
2017-09-06 15:10:34 +00:00
|
|
|
setEngine,
|
|
|
|
timingSafeEqual,
|
2018-01-24 00:32:19 +00:00
|
|
|
getFips: !fipsMode ? getFipsDisabled :
|
|
|
|
fipsForced ? getFipsForced : getFipsCrypto,
|
|
|
|
setFips: !fipsMode ? setFipsDisabled :
|
|
|
|
fipsForced ? setFipsForced : setFipsCrypto,
|
2019-03-12 13:17:10 +00:00
|
|
|
verify: verifyOneShot,
|
2017-09-06 15:10:34 +00:00
|
|
|
|
|
|
|
// Classes
|
|
|
|
Certificate,
|
|
|
|
Cipher,
|
|
|
|
Cipheriv,
|
|
|
|
Decipher,
|
|
|
|
Decipheriv,
|
|
|
|
DiffieHellman,
|
|
|
|
DiffieHellmanGroup,
|
2016-08-19 20:56:58 +00:00
|
|
|
ECDH,
|
2017-09-06 15:10:34 +00:00
|
|
|
Hash,
|
|
|
|
Hmac,
|
2019-03-11 20:26:22 +00:00
|
|
|
KeyObject,
|
2017-09-06 15:10:34 +00:00
|
|
|
Sign,
|
|
|
|
Verify
|
|
|
|
};
|
|
|
|
|
2017-10-24 05:44:52 +00:00
|
|
|
function setFipsDisabled() {
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_CRYPTO_FIPS_UNAVAILABLE();
|
2017-10-24 05:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setFipsForced(val) {
|
|
|
|
if (val) return;
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_CRYPTO_FIPS_FORCED();
|
2017-10-24 05:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getFipsDisabled() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFipsForced() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-04-10 11:04:10 +00:00
|
|
|
ObjectDefineProperty(constants, 'defaultCipherList', {
|
|
|
|
value: getOptionValue('--tls-cipher-list')
|
|
|
|
});
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperties(module.exports, {
|
2018-08-02 13:23:09 +00:00
|
|
|
createCipher: {
|
|
|
|
enumerable: false,
|
|
|
|
value: deprecate(createCipher,
|
|
|
|
'crypto.createCipher is deprecated.', 'DEP0106')
|
|
|
|
},
|
|
|
|
createDecipher: {
|
|
|
|
enumerable: false,
|
|
|
|
value: deprecate(createDecipher,
|
|
|
|
'crypto.createDecipher is deprecated.', 'DEP0106')
|
|
|
|
},
|
2018-06-19 07:32:28 +00:00
|
|
|
// crypto.fips is deprecated. DEP0093. Use crypto.getFips()/crypto.setFips()
|
2017-09-06 15:10:34 +00:00
|
|
|
fips: {
|
2017-10-24 05:44:52 +00:00
|
|
|
get: !fipsMode ? getFipsDisabled :
|
|
|
|
fipsForced ? getFipsForced : getFipsCrypto,
|
|
|
|
set: !fipsMode ? setFipsDisabled :
|
|
|
|
fipsForced ? setFipsForced : setFipsCrypto
|
2017-09-06 15:10:34 +00:00
|
|
|
},
|
|
|
|
DEFAULT_ENCODING: {
|
2018-10-02 17:12:32 +00:00
|
|
|
enumerable: false,
|
2017-09-06 15:10:34 +00:00
|
|
|
configurable: true,
|
2018-01-23 23:58:14 +00:00
|
|
|
get: deprecate(getDefaultEncoding,
|
2018-02-01 01:50:21 +00:00
|
|
|
'crypto.DEFAULT_ENCODING is deprecated.', 'DEP0091'),
|
2018-01-23 23:58:14 +00:00
|
|
|
set: deprecate(setDefaultEncoding,
|
2018-02-01 01:50:21 +00:00
|
|
|
'crypto.DEFAULT_ENCODING is deprecated.', 'DEP0091')
|
2017-09-06 15:10:34 +00:00
|
|
|
},
|
|
|
|
constants: {
|
|
|
|
configurable: false,
|
2014-02-18 01:57:08 +00:00
|
|
|
enumerable: true,
|
2017-09-06 15:10:34 +00:00
|
|
|
value: constants
|
2018-08-25 10:04:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Aliases for randomBytes are deprecated.
|
2018-09-22 09:45:42 +00:00
|
|
|
// The ecosystem needs those to exist for backwards compatibility.
|
2018-08-25 10:04:35 +00:00
|
|
|
prng: {
|
|
|
|
enumerable: false,
|
2018-11-05 16:07:48 +00:00
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
2018-09-22 09:45:42 +00:00
|
|
|
value: pendingDeprecation ?
|
|
|
|
deprecate(randomBytes, 'crypto.prng is deprecated.', 'DEP0115') :
|
|
|
|
randomBytes
|
2018-08-25 10:04:35 +00:00
|
|
|
},
|
|
|
|
pseudoRandomBytes: {
|
|
|
|
enumerable: false,
|
2018-11-05 16:07:48 +00:00
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
2018-09-22 09:45:42 +00:00
|
|
|
value: pendingDeprecation ?
|
|
|
|
deprecate(randomBytes,
|
|
|
|
'crypto.pseudoRandomBytes is deprecated.', 'DEP0115') :
|
|
|
|
randomBytes
|
2018-08-25 10:04:35 +00:00
|
|
|
},
|
|
|
|
rng: {
|
|
|
|
enumerable: false,
|
2018-11-05 16:07:48 +00:00
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
2018-09-22 09:45:42 +00:00
|
|
|
value: pendingDeprecation ?
|
|
|
|
deprecate(randomBytes, 'crypto.rng is deprecated.', 'DEP0115') :
|
|
|
|
randomBytes
|
2016-12-09 23:13:14 +00:00
|
|
|
}
|
2016-05-15 05:29:19 +00:00
|
|
|
});
|