Skip to content

Commit c7c5011

Browse files
committed
crypto: handle more webcrypto errors with OperationError
PR-URL: nodejs#45320 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Backport-PR-URL: nodejs#47336
1 parent cca5940 commit c7c5011

9 files changed

Lines changed: 27 additions & 18 deletions

File tree

lib/internal/crypto/aes.js

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ async function cfrgGenerateKey(algorithm, extractable, keyUsages) {
194194

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

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

lib/internal/crypto/diffiehellman.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,11 @@ async function asyncDeriveBitsECDH(algorithm, baseKey, length) {
377377
key.algorithm.name === 'ECDH' ? baseKey.algorithm.namedCurve : baseKey.algorithm.name,
378378
key[kKeyObject][kHandle],
379379
baseKey[kKeyObject][kHandle], (err, bits) => {
380-
if (err) return reject(err);
380+
if (err) {
381+
return reject(lazyDOMException(
382+
'The operation failed for an operation-specific reason',
383+
'OperationError'));
384+
}
381385
resolve(bits);
382386
});
383387
});

lib/internal/crypto/ec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {
151151
}
152152

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

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

lib/internal/crypto/hash.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ async function hmacImportKey(
184184

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

lib/internal/crypto/rsa.js

Lines changed: 3 additions & 3 deletions
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],
@@ -224,7 +224,7 @@ async function rsaKeyGenerate(
224224
}
225225

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

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

lib/internal/crypto/util.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,15 @@ function onDone(resolve, reject, err, result) {
287287
resolve(result);
288288
}
289289

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

test/parallel/test-webcrypto-digest.js

Lines changed: 1 addition & 1 deletion
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)