crypto: avoid calls to `promise.catch()` · nodejs/node@bb051c5

8 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -245,12 +245,15 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) {

245245

'SyntaxError');

246246

}

247247
248-

const key = await generateKey('aes', { length }).catch((err) => {

248+

let key;

249+

try {

250+

key = await generateKey('aes', { length });

251+

} catch (err) {

249252

throw lazyDOMException(

250253

'The operation failed for an operation-specific reason' +

251254

`[${err.message}]`,

252255

{ name: 'OperationError', cause: err });

253-

});

256+

}

254257
255258

return new InternalCryptoKey(

256259

key,

Original file line numberDiff line numberDiff line change

@@ -149,11 +149,14 @@ async function cfrgGenerateKey(algorithm, extractable, keyUsages) {

149149

break;

150150

}

151151
152-

const keyPair = await generateKeyPair(genKeyType).catch((err) => {

152+

let keyPair;

153+

try {

154+

keyPair = await generateKeyPair(genKeyType);

155+

} catch (err) {

153156

throw lazyDOMException(

154157

'The operation failed for an operation-specific reason',

155158

{ name: 'OperationError', cause: err });

156-

});

159+

}

157160
158161

let publicUsages;

159162

let privateUsages;

Original file line numberDiff line numberDiff line change

@@ -91,12 +91,15 @@ async function c20pGenerateKey(algorithm, extractable, keyUsages) {

9191

'SyntaxError');

9292

}

9393
94-

const keyData = await randomBytes(32).catch((err) => {

94+

let keyData;

95+

try {

96+

keyData = await randomBytes(32);

97+

} catch (err) {

9598

throw lazyDOMException(

9699

'The operation failed for an operation-specific reason' +

97100

`[${err.message}]`,

98101

{ name: 'OperationError', cause: err });

99-

});

102+

}

100103
101104

return new InternalCryptoKey(

102105

createSecretKey(keyData),

Original file line numberDiff line numberDiff line change

@@ -97,11 +97,14 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {

9797

// Fall through

9898

}

9999
100-

const keypair = await generateKeyPair('ec', { namedCurve }).catch((err) => {

100+

let keyPair;

101+

try {

102+

keyPair = await generateKeyPair('ec', { namedCurve });

103+

} catch(err) {

101104

throw lazyDOMException(

102105

'The operation failed for an operation-specific reason',

103106

{ name: 'OperationError', cause: err });

104-

});

107+

}

105108
106109

let publicUsages;

107110

let privateUsages;

@@ -120,14 +123,14 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {

120123
121124

const publicKey =

122125

new InternalCryptoKey(

123-

keypair.publicKey,

126+

keyPair.publicKey,

124127

keyAlgorithm,

125128

publicUsages,

126129

true);

127130
128131

const privateKey =

129132

new InternalCryptoKey(

130-

keypair.privateKey,

133+

keyPair.privateKey,

131134

keyAlgorithm,

132135

privateUsages,

133136

extractable);

Original file line numberDiff line numberDiff line change

@@ -64,11 +64,14 @@ async function hmacGenerateKey(algorithm, extractable, keyUsages) {

6464

'SyntaxError');

6565

}

6666
67-

const key = await generateKey('hmac', { length }).catch((err) => {

67+

let key;

68+

try {

69+

key = await generateKey('hmac', { length });

70+

} catch (err) {

6871

throw lazyDOMException(

6972

'The operation failed for an operation-specific reason',

7073

{ name: 'OperationError', cause: err });

71-

});

74+

}

7275
7376

return new InternalCryptoKey(

7477

key,

@@ -94,12 +97,15 @@ async function kmacGenerateKey(algorithm, extractable, keyUsages) {

9497

'SyntaxError');

9598

}

9699
97-

const keyData = await randomBytes(length / 8).catch((err) => {

100+

let keyData;

101+

try {

102+

keyData = await randomBytes(length / 8);

103+

} catch (err) {

98104

throw lazyDOMException(

99105

'The operation failed for an operation-specific reason' +

100106

`[${err.message}]`,

101107

{ name: 'OperationError', cause: err });

102-

});

108+

}

103109
104110

return new InternalCryptoKey(

105111

createSecretKey(keyData),

Original file line numberDiff line numberDiff line change

@@ -88,11 +88,14 @@ async function mlDsaGenerateKey(algorithm, extractable, keyUsages) {

8888

'SyntaxError');

8989

}

9090
91-

const keyPair = await generateKeyPair(name.toLowerCase()).catch((err) => {

91+

let keyPair;

92+

try {

93+

keyPair = await generateKeyPair(name.toLowerCase());

94+

} catch (err) {

9295

throw lazyDOMException(

9396

'The operation failed for an operation-specific reason',

9497

{ name: 'OperationError', cause: err });

95-

});

98+

}

9699
97100

const publicUsages = getUsagesUnion(usageSet, 'verify');

98101

const privateUsages = getUsagesUnion(usageSet, 'sign');

Original file line numberDiff line numberDiff line change

@@ -59,11 +59,14 @@ async function mlKemGenerateKey(algorithm, extractable, keyUsages) {

5959

'SyntaxError');

6060

}

6161
62-

const keyPair = await generateKeyPair(name.toLowerCase()).catch((err) => {

62+

let keyPair;

63+

try {

64+

keyPair = await generateKeyPair(name.toLowerCase());

65+

} catch(err) {

6366

throw lazyDOMException(

6467

'The operation failed for an operation-specific reason',

6568

{ name: 'OperationError', cause: err });

66-

});

69+

}

6770
6871

const publicUsages = getUsagesUnion(usageSet, 'encapsulateBits', 'encapsulateKey');

6972

const privateUsages = getUsagesUnion(usageSet, 'decapsulateBits', 'decapsulateKey');

Original file line numberDiff line numberDiff line change

@@ -150,14 +150,17 @@ async function rsaKeyGenerate(

150150

}

151151

}

152152
153-

const keypair = await generateKeyPair('rsa', {

154-

modulusLength,

155-

publicExponent: publicExponentConverted,

156-

}).catch((err) => {

153+

let keyPair;

154+

try {

155+

keyPair = await generateKeyPair('rsa', {

156+

modulusLength,

157+

publicExponent: publicExponentConverted,

158+

});

159+

} catch (err) {

157160

throw lazyDOMException(

158161

'The operation failed for an operation-specific reason',

159162

{ name: 'OperationError', cause: err });

160-

});

163+

}

161164
162165

const keyAlgorithm = {

163166

name,

@@ -183,14 +186,14 @@ async function rsaKeyGenerate(

183186
184187

const publicKey =

185188

new InternalCryptoKey(

186-

keypair.publicKey,

189+

keyPair.publicKey,

187190

keyAlgorithm,

188191

publicUsages,

189192

true);

190193
191194

const privateKey =

192195

new InternalCryptoKey(

193-

keypair.privateKey,

196+

keyPair.privateKey,

194197

keyAlgorithm,

195198

privateUsages,

196199

extractable);