Skip to content

Commit 457567f

Browse files
joyeecheungnodejs-github-bot
authored andcommitted
src: include crypto in the bootstrap snapshot
To lazy load the run time options, the following properties are updated from value properties to accessor properties whose getter would turn them back to a value properties upon the initial access. - crypto.constants.defaultCipherList - crypto.pseudoRandomBytes - crypto.prng - crypto.rng PR-URL: #42203 Refs: #37476 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent de163d5 commit 457567f

File tree

7 files changed

+110
-41
lines changed

7 files changed

+110
-41
lines changed

lib/crypto.js

+77-37
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ const {
4040
} = require('internal/errors').codes;
4141
const constants = internalBinding('constants').crypto;
4242
const { getOptionValue } = require('internal/options');
43-
const pendingDeprecation = getOptionValue('--pending-deprecation');
44-
const fipsForced = getOptionValue('--force-fips');
4543
const {
4644
getFipsCrypto,
4745
setFipsCrypto,
@@ -221,8 +219,8 @@ module.exports = {
221219
sign: signOneShot,
222220
setEngine,
223221
timingSafeEqual,
224-
getFips: fipsForced ? getFipsForced : getFipsCrypto,
225-
setFips: fipsForced ? setFipsForced : setFipsCrypto,
222+
getFips,
223+
setFips,
226224
verify: verifyOneShot,
227225

228226
// Classes
@@ -243,23 +241,87 @@ module.exports = {
243241
secureHeapUsed,
244242
};
245243

246-
function setFipsForced(val) {
247-
if (val) return;
248-
throw new ERR_CRYPTO_FIPS_FORCED();
244+
function getFips() {
245+
return getOptionValue('--force-fips') ? 1 : getFipsCrypto();
249246
}
250247

251-
function getFipsForced() {
252-
return 1;
248+
function setFips(val) {
249+
if (getOptionValue('--force-fips')) {
250+
if (val) return;
251+
throw new ERR_CRYPTO_FIPS_FORCED();
252+
} else {
253+
setFipsCrypto(val);
254+
}
253255
}
254256

255257
function getRandomValues(array) {
256258
return lazyWebCrypto().crypto.getRandomValues(array);
257259
}
258260

259261
ObjectDefineProperty(constants, 'defaultCipherList', {
260-
value: getOptionValue('--tls-cipher-list')
262+
get() {
263+
const value = getOptionValue('--tls-cipher-list');
264+
ObjectDefineProperty(this, 'defaultCipherList', {
265+
writable: true,
266+
configurable: true,
267+
enumerable: true,
268+
value
269+
});
270+
return value;
271+
},
272+
set(val) {
273+
ObjectDefineProperty(this, 'defaultCipherList', {
274+
writable: true,
275+
configurable: true,
276+
enumerable: true,
277+
value: val
278+
});
279+
},
280+
configurable: true,
281+
enumerable: true,
261282
});
262283

284+
function getRandomBytesAlias(key) {
285+
return {
286+
enumerable: false,
287+
configurable: true,
288+
get() {
289+
let value;
290+
if (getOptionValue('--pending-deprecation')) {
291+
value = deprecate(
292+
randomBytes,
293+
`crypto.${key} is deprecated.`,
294+
'DEP0115');
295+
} else {
296+
value = randomBytes;
297+
}
298+
ObjectDefineProperty(
299+
this,
300+
key,
301+
{
302+
enumerable: false,
303+
configurable: true,
304+
writable: true,
305+
value: value
306+
}
307+
);
308+
return value;
309+
},
310+
set(value) {
311+
ObjectDefineProperty(
312+
this,
313+
key,
314+
{
315+
enumerable: true,
316+
configurable: true,
317+
writable: true,
318+
value
319+
}
320+
);
321+
}
322+
};
323+
}
324+
263325
ObjectDefineProperties(module.exports, {
264326
createCipher: {
265327
enumerable: false,
@@ -273,8 +335,8 @@ ObjectDefineProperties(module.exports, {
273335
},
274336
// crypto.fips is deprecated. DEP0093. Use crypto.getFips()/crypto.setFips()
275337
fips: {
276-
get: fipsForced ? getFipsForced : getFipsCrypto,
277-
set: fipsForced ? setFipsForced : setFipsCrypto
338+
get: getFips,
339+
set: setFips,
278340
},
279341
DEFAULT_ENCODING: {
280342
enumerable: false,
@@ -313,29 +375,7 @@ ObjectDefineProperties(module.exports, {
313375

314376
// Aliases for randomBytes are deprecated.
315377
// The ecosystem needs those to exist for backwards compatibility.
316-
prng: {
317-
enumerable: false,
318-
configurable: true,
319-
writable: true,
320-
value: pendingDeprecation ?
321-
deprecate(randomBytes, 'crypto.prng is deprecated.', 'DEP0115') :
322-
randomBytes
323-
},
324-
pseudoRandomBytes: {
325-
enumerable: false,
326-
configurable: true,
327-
writable: true,
328-
value: pendingDeprecation ?
329-
deprecate(randomBytes,
330-
'crypto.pseudoRandomBytes is deprecated.', 'DEP0115') :
331-
randomBytes
332-
},
333-
rng: {
334-
enumerable: false,
335-
configurable: true,
336-
writable: true,
337-
value: pendingDeprecation ?
338-
deprecate(randomBytes, 'crypto.rng is deprecated.', 'DEP0115') :
339-
randomBytes
340-
}
378+
prng: getRandomBytesAlias('prng'),
379+
pseudoRandomBytes: getRandomBytesAlias('pseudoRandomBytes'),
380+
rng: getRandomBytesAlias('rng')
341381
});

lib/internal/bootstrap/node.js

+3
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ require('v8');
339339
require('vm');
340340
require('url');
341341
require('internal/options');
342+
if (config.hasOpenSSL) {
343+
require('crypto');
344+
}
342345

343346
function setupPrepareStackTrace() {
344347
const {

lib/internal/crypto/keygen.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ const {
6161
const { isArrayBufferView } = require('internal/util/types');
6262

6363
const { getOptionValue } = require('internal/options');
64-
const pendingDeprecation = getOptionValue('--pending-deprecation');
6564

6665
function wrapKey(key, ctor) {
6766
if (typeof key === 'string' ||
@@ -199,6 +198,9 @@ function createJob(mode, type, options) {
199198
const {
200199
hash, mgf1Hash, hashAlgorithm, mgf1HashAlgorithm, saltLength
201200
} = options;
201+
202+
const pendingDeprecation = getOptionValue('--pending-deprecation');
203+
202204
if (saltLength !== undefined && (!isInt32(saltLength) || saltLength < 0))
203205
throw new ERR_INVALID_ARG_VALUE('options.saltLength', saltLength);
204206
if (hashAlgorithm !== undefined && typeof hashAlgorithm !== 'string')

src/node_crypto.cc

-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ void Initialize(Local<Object> target,
7575
void* priv) {
7676
Environment* env = Environment::GetCurrent(context);
7777

78-
// TODO(joyeecheung): this needs to be called again if the instance is
79-
// deserialized from a snapshot with the crypto bindings.
8078
if (!InitCryptoOnce(env->isolate())) {
8179
return;
8280
}

src/node_main_instance.cc

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "node_main_instance.h"
22
#include <memory>
3+
#if HAVE_OPENSSL
4+
#include "crypto/crypto_util.h"
5+
#endif // HAVE_OPENSSL
36
#include "debug_utils-inl.h"
47
#include "node_external_reference.h"
58
#include "node_internals.h"
@@ -205,6 +208,10 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code,
205208
env->InitializeInspector({});
206209
#endif
207210
env->DoneBootstrapping();
211+
212+
#if HAVE_OPENSSL
213+
crypto::InitCryptoOnce(isolate_);
214+
#endif // HAVE_OPENSSL
208215
} else {
209216
context = NewContext(isolate_);
210217
CHECK(!context.IsEmpty());

test/parallel/test-bootstrap-modules.js

+20
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,26 @@ if (process.env.NODE_V8_COVERAGE) {
206206
expectedModules.add('Internal Binding profiler');
207207
}
208208

209+
if (common.hasCrypto) {
210+
expectedModules.add('Internal Binding crypto');
211+
expectedModules.add('NativeModule crypto');
212+
expectedModules.add('NativeModule internal/crypto/certificate');
213+
expectedModules.add('NativeModule internal/crypto/cipher');
214+
expectedModules.add('NativeModule internal/crypto/diffiehellman');
215+
expectedModules.add('NativeModule internal/crypto/hash');
216+
expectedModules.add('NativeModule internal/crypto/hashnames');
217+
expectedModules.add('NativeModule internal/crypto/hkdf');
218+
expectedModules.add('NativeModule internal/crypto/keygen');
219+
expectedModules.add('NativeModule internal/crypto/keys');
220+
expectedModules.add('NativeModule internal/crypto/pbkdf2');
221+
expectedModules.add('NativeModule internal/crypto/random');
222+
expectedModules.add('NativeModule internal/crypto/scrypt');
223+
expectedModules.add('NativeModule internal/crypto/sig');
224+
expectedModules.add('NativeModule internal/crypto/util');
225+
expectedModules.add('NativeModule internal/crypto/x509');
226+
expectedModules.add('NativeModule internal/streams/lazy_transform');
227+
}
228+
209229
const { internalBinding } = require('internal/test/binding');
210230
if (internalBinding('config').hasDtrace) {
211231
expectedModules.add('Internal Binding dtrace');

test/parallel/test-crypto-random.js

-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ assert.throws(
338338
const desc = Object.getOwnPropertyDescriptor(crypto, f);
339339
assert.ok(desc);
340340
assert.strictEqual(desc.configurable, true);
341-
assert.strictEqual(desc.writable, true);
342341
assert.strictEqual(desc.enumerable, false);
343342
});
344343

0 commit comments

Comments
 (0)