mirror of
https://github.com/nodejs/node.git
synced 2025-12-28 07:50:41 +00:00
crypto: avoid calls to promise.catch()
This avoids explicit calls to the user-mutable `%Promise.prototype%.catch`, and by association, implicit calls to the user-mutable `%Promise.prototype%.then`. PR-URL: https://github.com/nodejs/node/pull/59841 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
This commit is contained in:
parent
e511344152
commit
33e3e423d9
@ -245,12 +245,15 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) {
|
||||
'SyntaxError');
|
||||
}
|
||||
|
||||
const key = await generateKey('aes', { length }).catch((err) => {
|
||||
let key;
|
||||
try {
|
||||
key = await generateKey('aes', { length });
|
||||
} catch (err) {
|
||||
throw lazyDOMException(
|
||||
'The operation failed for an operation-specific reason' +
|
||||
`[${err.message}]`,
|
||||
{ name: 'OperationError', cause: err });
|
||||
});
|
||||
}
|
||||
|
||||
return new InternalCryptoKey(
|
||||
key,
|
||||
|
||||
@ -149,11 +149,14 @@ async function cfrgGenerateKey(algorithm, extractable, keyUsages) {
|
||||
break;
|
||||
}
|
||||
|
||||
const keyPair = await generateKeyPair(genKeyType).catch((err) => {
|
||||
let keyPair;
|
||||
try {
|
||||
keyPair = await generateKeyPair(genKeyType);
|
||||
} catch (err) {
|
||||
throw lazyDOMException(
|
||||
'The operation failed for an operation-specific reason',
|
||||
{ name: 'OperationError', cause: err });
|
||||
});
|
||||
}
|
||||
|
||||
let publicUsages;
|
||||
let privateUsages;
|
||||
|
||||
@ -91,12 +91,15 @@ async function c20pGenerateKey(algorithm, extractable, keyUsages) {
|
||||
'SyntaxError');
|
||||
}
|
||||
|
||||
const keyData = await randomBytes(32).catch((err) => {
|
||||
let keyData;
|
||||
try {
|
||||
keyData = await randomBytes(32);
|
||||
} catch (err) {
|
||||
throw lazyDOMException(
|
||||
'The operation failed for an operation-specific reason' +
|
||||
`[${err.message}]`,
|
||||
{ name: 'OperationError', cause: err });
|
||||
});
|
||||
}
|
||||
|
||||
return new InternalCryptoKey(
|
||||
createSecretKey(keyData),
|
||||
|
||||
@ -97,11 +97,14 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {
|
||||
// Fall through
|
||||
}
|
||||
|
||||
const keypair = await generateKeyPair('ec', { namedCurve }).catch((err) => {
|
||||
let keyPair;
|
||||
try {
|
||||
keyPair = await generateKeyPair('ec', { namedCurve });
|
||||
} catch(err) {
|
||||
throw lazyDOMException(
|
||||
'The operation failed for an operation-specific reason',
|
||||
{ name: 'OperationError', cause: err });
|
||||
});
|
||||
}
|
||||
|
||||
let publicUsages;
|
||||
let privateUsages;
|
||||
@ -120,14 +123,14 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {
|
||||
|
||||
const publicKey =
|
||||
new InternalCryptoKey(
|
||||
keypair.publicKey,
|
||||
keyPair.publicKey,
|
||||
keyAlgorithm,
|
||||
publicUsages,
|
||||
true);
|
||||
|
||||
const privateKey =
|
||||
new InternalCryptoKey(
|
||||
keypair.privateKey,
|
||||
keyPair.privateKey,
|
||||
keyAlgorithm,
|
||||
privateUsages,
|
||||
extractable);
|
||||
|
||||
@ -64,11 +64,14 @@ async function hmacGenerateKey(algorithm, extractable, keyUsages) {
|
||||
'SyntaxError');
|
||||
}
|
||||
|
||||
const key = await generateKey('hmac', { length }).catch((err) => {
|
||||
let key;
|
||||
try {
|
||||
key = await generateKey('hmac', { length });
|
||||
} catch (err) {
|
||||
throw lazyDOMException(
|
||||
'The operation failed for an operation-specific reason',
|
||||
{ name: 'OperationError', cause: err });
|
||||
});
|
||||
}
|
||||
|
||||
return new InternalCryptoKey(
|
||||
key,
|
||||
@ -94,12 +97,15 @@ async function kmacGenerateKey(algorithm, extractable, keyUsages) {
|
||||
'SyntaxError');
|
||||
}
|
||||
|
||||
const keyData = await randomBytes(length / 8).catch((err) => {
|
||||
let keyData;
|
||||
try {
|
||||
keyData = await randomBytes(length / 8);
|
||||
} catch (err) {
|
||||
throw lazyDOMException(
|
||||
'The operation failed for an operation-specific reason' +
|
||||
`[${err.message}]`,
|
||||
{ name: 'OperationError', cause: err });
|
||||
});
|
||||
}
|
||||
|
||||
return new InternalCryptoKey(
|
||||
createSecretKey(keyData),
|
||||
|
||||
@ -88,11 +88,14 @@ async function mlDsaGenerateKey(algorithm, extractable, keyUsages) {
|
||||
'SyntaxError');
|
||||
}
|
||||
|
||||
const keyPair = await generateKeyPair(name.toLowerCase()).catch((err) => {
|
||||
let keyPair;
|
||||
try {
|
||||
keyPair = await generateKeyPair(name.toLowerCase());
|
||||
} catch (err) {
|
||||
throw lazyDOMException(
|
||||
'The operation failed for an operation-specific reason',
|
||||
{ name: 'OperationError', cause: err });
|
||||
});
|
||||
}
|
||||
|
||||
const publicUsages = getUsagesUnion(usageSet, 'verify');
|
||||
const privateUsages = getUsagesUnion(usageSet, 'sign');
|
||||
|
||||
@ -59,11 +59,14 @@ async function mlKemGenerateKey(algorithm, extractable, keyUsages) {
|
||||
'SyntaxError');
|
||||
}
|
||||
|
||||
const keyPair = await generateKeyPair(name.toLowerCase()).catch((err) => {
|
||||
let keyPair;
|
||||
try {
|
||||
keyPair = await generateKeyPair(name.toLowerCase());
|
||||
} catch(err) {
|
||||
throw lazyDOMException(
|
||||
'The operation failed for an operation-specific reason',
|
||||
{ name: 'OperationError', cause: err });
|
||||
});
|
||||
}
|
||||
|
||||
const publicUsages = getUsagesUnion(usageSet, 'encapsulateBits', 'encapsulateKey');
|
||||
const privateUsages = getUsagesUnion(usageSet, 'decapsulateBits', 'decapsulateKey');
|
||||
|
||||
@ -150,14 +150,17 @@ async function rsaKeyGenerate(
|
||||
}
|
||||
}
|
||||
|
||||
const keypair = await generateKeyPair('rsa', {
|
||||
modulusLength,
|
||||
publicExponent: publicExponentConverted,
|
||||
}).catch((err) => {
|
||||
let keyPair;
|
||||
try {
|
||||
keyPair = await generateKeyPair('rsa', {
|
||||
modulusLength,
|
||||
publicExponent: publicExponentConverted,
|
||||
});
|
||||
} catch (err) {
|
||||
throw lazyDOMException(
|
||||
'The operation failed for an operation-specific reason',
|
||||
{ name: 'OperationError', cause: err });
|
||||
});
|
||||
}
|
||||
|
||||
const keyAlgorithm = {
|
||||
name,
|
||||
@ -183,14 +186,14 @@ async function rsaKeyGenerate(
|
||||
|
||||
const publicKey =
|
||||
new InternalCryptoKey(
|
||||
keypair.publicKey,
|
||||
keyPair.publicKey,
|
||||
keyAlgorithm,
|
||||
publicUsages,
|
||||
true);
|
||||
|
||||
const privateKey =
|
||||
new InternalCryptoKey(
|
||||
keypair.privateKey,
|
||||
keyPair.privateKey,
|
||||
keyAlgorithm,
|
||||
privateUsages,
|
||||
extractable);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user