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';
|
|
|
|
|
2017-09-06 15:10:34 +00:00
|
|
|
const {
|
|
|
|
assertCrypto,
|
|
|
|
deprecate
|
|
|
|
} = require('internal/util');
|
|
|
|
assertCrypto();
|
2010-05-04 20:49:00 +00:00
|
|
|
|
2018-08-21 06:54:02 +00:00
|
|
|
const { internalBinding } = require('internal/bootstrap/loaders');
|
2018-02-27 13:55:32 +00:00
|
|
|
const {
|
|
|
|
ERR_CRYPTO_FIPS_FORCED,
|
|
|
|
ERR_CRYPTO_FIPS_UNAVAILABLE
|
|
|
|
} = require('internal/errors').codes;
|
2016-05-02 17:27:12 +00:00
|
|
|
const constants = process.binding('constants').crypto;
|
2017-10-24 05:44:52 +00:00
|
|
|
const {
|
|
|
|
fipsMode,
|
|
|
|
fipsForced
|
|
|
|
} = process.binding('config');
|
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');
|
2017-09-06 15:10:34 +00:00
|
|
|
const {
|
|
|
|
DiffieHellman,
|
|
|
|
DiffieHellmanGroup,
|
|
|
|
ECDH
|
|
|
|
} = require('internal/crypto/diffiehellman');
|
|
|
|
const {
|
|
|
|
Cipher,
|
|
|
|
Cipheriv,
|
|
|
|
Decipher,
|
|
|
|
Decipheriv,
|
|
|
|
privateDecrypt,
|
|
|
|
privateEncrypt,
|
|
|
|
publicDecrypt,
|
|
|
|
publicEncrypt
|
|
|
|
} = require('internal/crypto/cipher');
|
|
|
|
const {
|
|
|
|
Sign,
|
|
|
|
Verify
|
|
|
|
} = require('internal/crypto/sig');
|
|
|
|
const {
|
|
|
|
Hash,
|
|
|
|
Hmac
|
|
|
|
} = require('internal/crypto/hash');
|
|
|
|
const {
|
|
|
|
getCiphers,
|
|
|
|
getCurves,
|
|
|
|
getDefaultEncoding,
|
|
|
|
getHashes,
|
|
|
|
setDefaultEncoding,
|
|
|
|
setEngine,
|
2017-10-24 17:04:25 +00:00
|
|
|
timingSafeEqual,
|
2017-09-06 15:10:34 +00:00
|
|
|
toBuf
|
|
|
|
} = 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);
|
|
|
|
}
|
|
|
|
|
2017-09-06 15:10:34 +00:00
|
|
|
module.exports = exports = {
|
|
|
|
// Methods
|
2017-10-07 18:24:21 +00:00
|
|
|
createCipheriv,
|
|
|
|
createDecipheriv,
|
|
|
|
createDiffieHellman,
|
|
|
|
createDiffieHellmanGroup,
|
|
|
|
createECDH,
|
|
|
|
createHash,
|
|
|
|
createHmac,
|
|
|
|
createSign,
|
|
|
|
createVerify,
|
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,
|
|
|
|
privateDecrypt,
|
|
|
|
privateEncrypt,
|
|
|
|
publicDecrypt,
|
|
|
|
publicEncrypt,
|
|
|
|
randomBytes,
|
|
|
|
randomFill,
|
|
|
|
randomFillSync,
|
2018-05-18 09:05:20 +00:00
|
|
|
scrypt,
|
|
|
|
scryptSync,
|
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,
|
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,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-09-06 15:10:34 +00:00
|
|
|
Object.defineProperties(exports, {
|
2018-08-27 11:44:19 +00:00
|
|
|
_toBuf: {
|
|
|
|
enumerable: false,
|
|
|
|
value: deprecate(toBuf, 'crypto._toBuf is deprecated.', 'DEP0114')
|
|
|
|
},
|
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: {
|
2014-02-18 01:57:08 +00:00
|
|
|
enumerable: true,
|
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.
|
|
|
|
// The ecosystem needs those to exist for backwards compatibility with
|
|
|
|
// ancient Node.js runtimes (0.10, 0.12).
|
|
|
|
prng: {
|
|
|
|
enumerable: false,
|
|
|
|
value: deprecate(randomBytes, 'crypto.prng is deprecated.', 'DEP0115')
|
|
|
|
},
|
|
|
|
pseudoRandomBytes: {
|
|
|
|
enumerable: false,
|
|
|
|
value: deprecate(randomBytes,
|
|
|
|
'crypto.pseudoRandomBytes is deprecated.', 'DEP0115')
|
|
|
|
},
|
|
|
|
rng: {
|
|
|
|
enumerable: false,
|
|
|
|
value: deprecate(randomBytes, 'crypto.rng is deprecated.', 'DEP0115')
|
2016-12-09 23:13:14 +00:00
|
|
|
}
|
2016-05-15 05:29:19 +00:00
|
|
|
});
|