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,
|
2023-02-12 18:26:21 +00:00
|
|
|
deprecate,
|
2017-09-06 15:10:34 +00:00
|
|
|
} = 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,
|
2022-07-08 10:50:24 +00:00
|
|
|
ERR_WORKER_UNSUPPORTED_OPERATION,
|
2018-02-27 13:55:32 +00:00
|
|
|
} = 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');
|
2020-06-30 20:36:10 +00:00
|
|
|
const {
|
|
|
|
getFipsCrypto,
|
|
|
|
setFipsCrypto,
|
|
|
|
timingSafeEqual,
|
|
|
|
} = internalBinding('crypto');
|
2017-09-06 15:10:34 +00:00
|
|
|
const {
|
2021-01-19 07:03:58 +00:00
|
|
|
checkPrime,
|
|
|
|
checkPrimeSync,
|
|
|
|
generatePrime,
|
|
|
|
generatePrimeSync,
|
2017-09-06 15:10:34 +00:00
|
|
|
randomBytes,
|
|
|
|
randomFill,
|
2020-08-02 12:08:39 +00:00
|
|
|
randomFillSync,
|
2021-01-02 05:59:44 +00:00
|
|
|
randomInt,
|
|
|
|
randomUUID,
|
2017-09-06 15:10:34 +00:00
|
|
|
} = require('internal/crypto/random');
|
|
|
|
const {
|
|
|
|
pbkdf2,
|
2023-02-12 18:26:21 +00:00
|
|
|
pbkdf2Sync,
|
2017-09-06 15:10:34 +00:00
|
|
|
} = require('internal/crypto/pbkdf2');
|
2018-05-18 09:05:20 +00:00
|
|
|
const {
|
|
|
|
scrypt,
|
2023-02-12 18:26:21 +00:00
|
|
|
scryptSync,
|
2018-05-18 09:05:20 +00:00
|
|
|
} = require('internal/crypto/scrypt');
|
2020-08-25 17:05:51 +00:00
|
|
|
const {
|
|
|
|
hkdf,
|
2023-02-12 18:26:21 +00:00
|
|
|
hkdfSync,
|
2020-08-25 17:05:51 +00:00
|
|
|
} = require('internal/crypto/hkdf');
|
2018-09-02 15:00:01 +00:00
|
|
|
const {
|
|
|
|
generateKeyPair,
|
2020-08-25 17:05:51 +00:00
|
|
|
generateKeyPairSync,
|
|
|
|
generateKey,
|
|
|
|
generateKeySync,
|
2018-09-02 15:00:01 +00:00
|
|
|
} = 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,
|
2023-02-12 18:26:21 +00:00
|
|
|
diffieHellman,
|
2017-09-06 15:10:34 +00:00
|
|
|
} = require('internal/crypto/diffiehellman');
|
|
|
|
const {
|
|
|
|
Cipher,
|
|
|
|
Cipheriv,
|
|
|
|
Decipher,
|
|
|
|
Decipheriv,
|
|
|
|
privateDecrypt,
|
|
|
|
privateEncrypt,
|
|
|
|
publicDecrypt,
|
2020-09-27 02:20:03 +00:00
|
|
|
publicEncrypt,
|
|
|
|
getCipherInfo,
|
2017-09-06 15:10:34 +00:00
|
|
|
} = require('internal/crypto/cipher');
|
|
|
|
const {
|
|
|
|
Sign,
|
2019-03-12 13:17:10 +00:00
|
|
|
signOneShot,
|
|
|
|
Verify,
|
2023-02-12 18:26:21 +00:00
|
|
|
verifyOneShot,
|
2017-09-06 15:10:34 +00:00
|
|
|
} = require('internal/crypto/sig');
|
|
|
|
const {
|
|
|
|
Hash,
|
2023-02-12 18:26:21 +00:00
|
|
|
Hmac,
|
2017-09-06 15:10:34 +00:00
|
|
|
} = require('internal/crypto/hash');
|
2021-01-05 05:27:20 +00:00
|
|
|
const {
|
2023-02-12 18:26:21 +00:00
|
|
|
X509Certificate,
|
2021-01-05 05:27:20 +00:00
|
|
|
} = require('internal/crypto/x509');
|
2017-09-06 15:10:34 +00:00
|
|
|
const {
|
|
|
|
getCiphers,
|
|
|
|
getCurves,
|
|
|
|
getHashes,
|
|
|
|
setEngine,
|
2021-01-04 17:06:26 +00:00
|
|
|
secureHeapUsed,
|
2017-09-06 15:10:34 +00:00
|
|
|
} = require('internal/crypto/util');
|
|
|
|
const Certificate = require('internal/crypto/certificate');
|
|
|
|
|
2021-12-27 14:48:59 +00:00
|
|
|
let webcrypto;
|
|
|
|
function lazyWebCrypto() {
|
|
|
|
webcrypto ??= require('internal/crypto/webcrypto');
|
|
|
|
return webcrypto;
|
|
|
|
}
|
|
|
|
|
2022-07-08 10:50:24 +00:00
|
|
|
let ownsProcessState;
|
|
|
|
function lazyOwnsProcessState() {
|
|
|
|
ownsProcessState ??= require('internal/worker').ownsProcessState;
|
|
|
|
return ownsProcessState;
|
|
|
|
}
|
|
|
|
|
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 createCipheriv(cipher, key, iv, options) {
|
|
|
|
return new Cipheriv(cipher, key, iv, 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
|
2021-01-19 07:03:58 +00:00
|
|
|
checkPrime,
|
|
|
|
checkPrimeSync,
|
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,
|
2021-01-19 07:03:58 +00:00
|
|
|
generatePrime,
|
|
|
|
generatePrimeSync,
|
2017-09-06 15:10:34 +00:00
|
|
|
getCiphers,
|
2020-09-27 02:20:03 +00:00
|
|
|
getCipherInfo,
|
2017-09-06 15:10:34 +00:00
|
|
|
getCurves,
|
2017-10-07 18:24:21 +00:00
|
|
|
getDiffieHellman: createDiffieHellmanGroup,
|
2017-09-06 15:10:34 +00:00
|
|
|
getHashes,
|
2020-08-25 17:05:51 +00:00
|
|
|
hkdf,
|
|
|
|
hkdfSync,
|
2017-09-06 15:10:34 +00:00
|
|
|
pbkdf2,
|
|
|
|
pbkdf2Sync,
|
2018-09-02 15:00:01 +00:00
|
|
|
generateKeyPair,
|
|
|
|
generateKeyPairSync,
|
2020-08-25 17:05:51 +00:00
|
|
|
generateKey,
|
|
|
|
generateKeySync,
|
2017-09-06 15:10:34 +00:00
|
|
|
privateDecrypt,
|
|
|
|
privateEncrypt,
|
|
|
|
publicDecrypt,
|
|
|
|
publicEncrypt,
|
|
|
|
randomBytes,
|
|
|
|
randomFill,
|
|
|
|
randomFillSync,
|
2020-08-02 12:08:39 +00:00
|
|
|
randomInt,
|
2021-01-02 05:59:44 +00:00
|
|
|
randomUUID,
|
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,
|
2022-03-03 18:18:56 +00:00
|
|
|
getFips,
|
|
|
|
setFips,
|
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,
|
2021-01-05 05:27:20 +00:00
|
|
|
Verify,
|
|
|
|
X509Certificate,
|
2021-01-04 17:06:26 +00:00
|
|
|
secureHeapUsed,
|
2017-09-06 15:10:34 +00:00
|
|
|
};
|
|
|
|
|
2022-03-03 18:18:56 +00:00
|
|
|
function getFips() {
|
|
|
|
return getOptionValue('--force-fips') ? 1 : getFipsCrypto();
|
2017-10-24 05:44:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 18:18:56 +00:00
|
|
|
function setFips(val) {
|
|
|
|
if (getOptionValue('--force-fips')) {
|
|
|
|
if (val) return;
|
|
|
|
throw new ERR_CRYPTO_FIPS_FORCED();
|
|
|
|
} else {
|
2022-07-08 10:50:24 +00:00
|
|
|
if (!lazyOwnsProcessState()) {
|
|
|
|
throw new ERR_WORKER_UNSUPPORTED_OPERATION('Calling crypto.setFips()');
|
|
|
|
}
|
2022-03-03 18:18:56 +00:00
|
|
|
setFipsCrypto(val);
|
|
|
|
}
|
2017-10-24 05:44:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 16:51:50 +00:00
|
|
|
function getRandomValues(array) {
|
|
|
|
return lazyWebCrypto().crypto.getRandomValues(array);
|
|
|
|
}
|
|
|
|
|
2020-04-10 11:04:10 +00:00
|
|
|
ObjectDefineProperty(constants, 'defaultCipherList', {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2022-03-03 18:18:56 +00:00
|
|
|
get() {
|
|
|
|
const value = getOptionValue('--tls-cipher-list');
|
|
|
|
ObjectDefineProperty(this, 'defaultCipherList', {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2022-03-03 18:18:56 +00:00
|
|
|
writable: true,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
2023-02-12 18:26:21 +00:00
|
|
|
value,
|
2022-03-03 18:18:56 +00:00
|
|
|
});
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
ObjectDefineProperty(this, 'defaultCipherList', {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2022-03-03 18:18:56 +00:00
|
|
|
writable: true,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
2023-02-12 18:26:21 +00:00
|
|
|
value: val,
|
2022-03-03 18:18:56 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
2020-04-10 11:04:10 +00:00
|
|
|
});
|
|
|
|
|
2022-03-03 18:18:56 +00:00
|
|
|
function getRandomBytesAlias(key) {
|
|
|
|
return {
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
get() {
|
|
|
|
let value;
|
|
|
|
if (getOptionValue('--pending-deprecation')) {
|
|
|
|
value = deprecate(
|
|
|
|
randomBytes,
|
|
|
|
`crypto.${key} is deprecated.`,
|
|
|
|
'DEP0115');
|
|
|
|
} else {
|
|
|
|
value = randomBytes;
|
|
|
|
}
|
|
|
|
ObjectDefineProperty(
|
|
|
|
this,
|
|
|
|
key,
|
|
|
|
{
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2022-03-03 18:18:56 +00:00
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
2023-02-12 18:26:21 +00:00
|
|
|
value: value,
|
2023-02-14 17:45:16 +00:00
|
|
|
},
|
2022-03-03 18:18:56 +00:00
|
|
|
);
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
ObjectDefineProperty(
|
|
|
|
this,
|
|
|
|
key,
|
|
|
|
{
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2022-03-03 18:18:56 +00:00
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
2023-02-12 18:26:21 +00:00
|
|
|
value,
|
2023-02-14 17:45:16 +00:00
|
|
|
},
|
2022-03-03 18:18:56 +00:00
|
|
|
);
|
2023-02-12 18:26:21 +00:00
|
|
|
},
|
2022-03-03 18:18:56 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperties(module.exports, {
|
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: {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2022-03-03 18:18:56 +00:00
|
|
|
get: getFips,
|
|
|
|
set: setFips,
|
2017-09-06 15:10:34 +00:00
|
|
|
},
|
|
|
|
constants: {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2017-09-06 15:10:34 +00:00
|
|
|
configurable: false,
|
2014-02-18 01:57:08 +00:00
|
|
|
enumerable: true,
|
2023-02-12 18:26:21 +00:00
|
|
|
value: constants,
|
2018-08-25 10:04:35 +00:00
|
|
|
},
|
|
|
|
|
2020-08-25 17:05:51 +00:00
|
|
|
webcrypto: {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2020-08-25 17:05:51 +00:00
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
2021-12-27 14:48:59 +00:00
|
|
|
get() { return lazyWebCrypto().crypto; },
|
|
|
|
set: undefined,
|
|
|
|
},
|
|
|
|
|
|
|
|
subtle: {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2021-12-27 14:48:59 +00:00
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
|
|
|
get() { return lazyWebCrypto().crypto.subtle; },
|
|
|
|
set: undefined,
|
|
|
|
},
|
|
|
|
|
|
|
|
getRandomValues: {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2021-12-27 14:48:59 +00:00
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
2022-02-22 16:51:50 +00:00
|
|
|
get: () => getRandomValues,
|
2021-12-27 14:48:59 +00:00
|
|
|
set: undefined,
|
2020-08-25 17:05:51 +00:00
|
|
|
},
|
|
|
|
|
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.
|
2022-03-03 18:18:56 +00:00
|
|
|
prng: getRandomBytesAlias('prng'),
|
|
|
|
pseudoRandomBytes: getRandomBytesAlias('pseudoRandomBytes'),
|
2023-02-12 18:26:21 +00:00
|
|
|
rng: getRandomBytesAlias('rng'),
|
2016-05-15 05:29:19 +00:00
|
|
|
});
|