Skip to content

Commit 1ba1809

Browse files
panvadanielleadams
authored andcommitted
crypto: handle more webcrypto errors with OperationError
PR-URL: #45320 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent b54f876 commit 1ba1809

File tree

9 files changed

+23
-18
lines changed

9 files changed

+23
-18
lines changed

lib/internal/crypto/aes.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function asyncAesCtrCipher(mode, key, data, { counter, length }) {
124124
'OperationError');
125125
}
126126

127-
return jobPromise(new AESCipherJob(
127+
return jobPromise(() => new AESCipherJob(
128128
kCryptoJobAsync,
129129
mode,
130130
key[kKeyObject][kHandle],
@@ -137,7 +137,7 @@ function asyncAesCtrCipher(mode, key, data, { counter, length }) {
137137
function asyncAesCbcCipher(mode, key, data, { iv }) {
138138
iv = getArrayBufferOrView(iv, 'algorithm.iv');
139139
validateByteLength(iv, 'algorithm.iv', 16);
140-
return jobPromise(new AESCipherJob(
140+
return jobPromise(() => new AESCipherJob(
141141
kCryptoJobAsync,
142142
mode,
143143
key[kKeyObject][kHandle],
@@ -147,7 +147,7 @@ function asyncAesCbcCipher(mode, key, data, { iv }) {
147147
}
148148

149149
function asyncAesKwCipher(mode, key, data) {
150-
return jobPromise(new AESCipherJob(
150+
return jobPromise(() => new AESCipherJob(
151151
kCryptoJobAsync,
152152
mode,
153153
key[kKeyObject][kHandle],
@@ -201,7 +201,7 @@ function asyncAesGcmCipher(
201201
break;
202202
}
203203

204-
return jobPromise(new AESCipherJob(
204+
return jobPromise(() => new AESCipherJob(
205205
kCryptoJobAsync,
206206
mode,
207207
key[kKeyObject][kHandle],

lib/internal/crypto/cfrg.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ async function cfrgGenerateKey(algorithm, extractable, keyUsages) {
193193

194194
function cfrgExportKey(key, format) {
195195
emitExperimentalWarning(`The ${key.algorithm.name} Web Crypto API algorithm`);
196-
return jobPromise(new ECKeyExportJob(
196+
return jobPromise(() => new ECKeyExportJob(
197197
kCryptoJobAsync,
198198
format,
199199
key[kKeyObject][kHandle]));
@@ -322,7 +322,7 @@ function eddsaSignVerify(key, data, { name, context }, signature) {
322322
}
323323
}
324324

325-
return jobPromise(new SignJob(
325+
return jobPromise(() => new SignJob(
326326
kCryptoJobAsync,
327327
mode,
328328
key[kKeyObject][kHandle],

lib/internal/crypto/diffiehellman.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ async function ecdhDeriveBits(algorithm, baseKey, length) {
357357
throw lazyDOMException('Named curve mismatch', 'InvalidAccessError');
358358
}
359359

360-
const bits = await jobPromise(new ECDHBitsJob(
360+
const bits = await jobPromise(() => new ECDHBitsJob(
361361
kCryptoJobAsync,
362362
key.algorithm.name === 'ECDH' ? baseKey.algorithm.namedCurve : baseKey.algorithm.name,
363363
key[kKeyObject][kHandle],

lib/internal/crypto/ec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {
150150
}
151151

152152
function ecExportKey(key, format) {
153-
return jobPromise(new ECKeyExportJob(
153+
return jobPromise(() => new ECKeyExportJob(
154154
kCryptoJobAsync,
155155
format,
156156
key[kKeyObject][kHandle]));
@@ -285,7 +285,7 @@ function ecdsaSignVerify(key, data, { name, hash }, signature) {
285285
throw new ERR_MISSING_OPTION('algorithm.hash');
286286
const hashname = normalizeHashName(hash.name);
287287

288-
return jobPromise(new SignJob(
288+
return jobPromise(() => new SignJob(
289289
kCryptoJobAsync,
290290
mode,
291291
key[kKeyObject][kHandle],

lib/internal/crypto/hash.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ async function asyncDigest(algorithm, data) {
183183
case 'SHA-384':
184184
// Fall through
185185
case 'SHA-512':
186-
return jobPromise(new HashJob(
186+
return jobPromise(() => new HashJob(
187187
kCryptoJobAsync,
188188
normalizeHashName(algorithm.name),
189189
data,

lib/internal/crypto/mac.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ async function hmacImportKey(
183183

184184
function hmacSignVerify(key, data, algorithm, signature) {
185185
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
186-
return jobPromise(new HmacJob(
186+
return jobPromise(() => new HmacJob(
187187
kCryptoJobAsync,
188188
mode,
189189
normalizeHashName(key.algorithm.hash.name),

lib/internal/crypto/rsa.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function rsaOaepCipher(mode, key, data, { label }) {
116116
validateMaxBufferLength(label, 'algorithm.label');
117117
}
118118

119-
return jobPromise(new RSACipherJob(
119+
return jobPromise(() => new RSACipherJob(
120120
kCryptoJobAsync,
121121
mode,
122122
key[kKeyObject][kHandle],
@@ -223,7 +223,7 @@ async function rsaKeyGenerate(
223223
}
224224

225225
function rsaExportKey(key, format) {
226-
return jobPromise(new RSAKeyExportJob(
226+
return jobPromise(() => new RSAKeyExportJob(
227227
kCryptoJobAsync,
228228
format,
229229
key[kKeyObject][kHandle],
@@ -346,7 +346,7 @@ function rsaSignVerify(key, data, { saltLength }, signature) {
346346
if (key.type !== type)
347347
throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError');
348348

349-
return jobPromise(new SignJob(
349+
return jobPromise(() => new SignJob(
350350
kCryptoJobAsync,
351351
signature === undefined ? kSignJobModeSign : kSignJobModeVerify,
352352
key[kKeyObject][kHandle],

lib/internal/crypto/util.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,15 @@ function onDone(resolve, reject, err, result) {
286286
resolve(result);
287287
}
288288

289-
function jobPromise(job) {
289+
function jobPromise(getJob) {
290290
return new Promise((resolve, reject) => {
291-
job.ondone = FunctionPrototypeBind(onDone, job, resolve, reject);
292-
job.run();
291+
try {
292+
const job = getJob();
293+
job.ondone = FunctionPrototypeBind(onDone, job, resolve, reject);
294+
job.run();
295+
} catch (err) {
296+
onDone(resolve, reject, err);
297+
}
293298
});
294299
}
295300

test/parallel/test-webcrypto-digest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Promise.all([1, [], {}, null, undefined].map((i) =>
8383
// addition to the API, and is added as a support for future additional
8484
// hash algorithms that support variable digest output lengths.
8585
assert.rejects(subtle.digest({ name: 'SHA-512', length: 510 }, kData), {
86-
message: /Digest method not supported/
86+
name: 'OperationError',
8787
}).then(common.mustCall());
8888

8989
const kSourceData = {

0 commit comments

Comments
 (0)