Skip to content

Commit 2c938d7

Browse files
panvajuanarbol
authored andcommitted
crypto: fix webcrypto operation errors to be OperationError
PR-URL: #44171 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Backport-PR-URL: #44872
1 parent a6e2cb4 commit 2c938d7

9 files changed

Lines changed: 185 additions & 205 deletions

File tree

lib/internal/crypto/aes.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const {
77
ArrayPrototypeIncludes,
88
ArrayPrototypePush,
99
MathFloor,
10-
Promise,
1110
SafeSet,
1211
TypedArrayPrototypeSlice,
1312
} = primordials;
@@ -46,6 +45,7 @@ const {
4645

4746
const {
4847
lazyDOMException,
48+
promisify,
4949
} = require('internal/util');
5050

5151
const { PromiseReject } = primordials;
@@ -57,11 +57,12 @@ const {
5757
} = require('internal/crypto/keys');
5858

5959
const {
60-
generateKey,
60+
generateKey: _generateKey,
6161
} = require('internal/crypto/keygen');
6262

6363
const kMaxCounterLength = 128;
6464
const kTagLengths = [32, 64, 96, 104, 112, 120, 128];
65+
const generateKey = promisify(_generateKey);
6566

6667
function getAlgorithmName(name, length) {
6768
switch (name) {
@@ -239,22 +240,19 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) {
239240
'SyntaxError');
240241
}
241242

242-
return new Promise((resolve, reject) => {
243-
generateKey('aes', { length }, (err, key) => {
244-
if (err) {
245-
return reject(lazyDOMException(
246-
'The operation failed for an operation-specific reason ' +
247-
`[${err.message}]`,
248-
'OperationError'));
249-
}
250-
251-
resolve(new InternalCryptoKey(
252-
key,
253-
{ name, length },
254-
ArrayFrom(usagesSet),
255-
extractable));
256-
});
243+
const key = await generateKey('aes', { length }).catch((err) => {
244+
// TODO(@panva): add err as cause to DOMException
245+
throw lazyDOMException(
246+
'The operation failed for an operation-specific reason' +
247+
`[${err.message}]`,
248+
'OperationError');
257249
});
250+
251+
return new InternalCryptoKey(
252+
key,
253+
{ name, length },
254+
ArrayFrom(usagesSet),
255+
extractable);
258256
}
259257

260258
async function aesImportKey(

lib/internal/crypto/cfrg.js

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {
4-
Promise,
54
SafeSet,
65
} = primordials;
76

@@ -31,10 +30,11 @@ const {
3130
const {
3231
emitExperimentalWarning,
3332
lazyDOMException,
33+
promisify,
3434
} = require('internal/util');
3535

3636
const {
37-
generateKeyPair,
37+
generateKeyPair: _generateKeyPair,
3838
} = require('internal/crypto/keygen');
3939

4040
const {
@@ -45,6 +45,8 @@ const {
4545
create