Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: handle more webcrypto errors with OperationError #45320

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/internal/crypto/aes.js
Original file line number Diff line number Diff line change
@@ -124,7 +124,7 @@ function asyncAesCtrCipher(mode, key, data, { counter, length }) {
'OperationError');
}

return jobPromise(new AESCipherJob(
return jobPromise(() => new AESCipherJob(
kCryptoJobAsync,
mode,
key[kKeyObject][kHandle],
@@ -137,7 +137,7 @@ function asyncAesCtrCipher(mode, key, data, { counter, length }) {
function asyncAesCbcCipher(mode, key, data, { iv }) {
iv = getArrayBufferOrView(iv, 'algorithm.iv');
validateByteLength(iv, 'algorithm.iv', 16);
return jobPromise(new AESCipherJob(
return jobPromise(() => new AESCipherJob(
kCryptoJobAsync,
mode,
key[kKeyObject][kHandle],
@@ -147,7 +147,7 @@ function asyncAesCbcCipher(mode, key, data, { iv }) {
}

function asyncAesKwCipher(mode, key, data) {
return jobPromise(new AESCipherJob(
return jobPromise(() => new AESCipherJob(
kCryptoJobAsync,
mode,
key[kKeyObject][kHandle],
@@ -201,7 +201,7 @@ function asyncAesGcmCipher(
break;
}

return jobPromise(new AESCipherJob(
return jobPromise(() => new AESCipherJob(
kCryptoJobAsync,
mode,
key[kKeyObject][kHandle],
4 changes: 2 additions & 2 deletions lib/internal/crypto/cfrg.js
Original file line number Diff line number Diff line change
@@ -193,7 +193,7 @@ async function cfrgGenerateKey(algorithm, extractable, keyUsages) {

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

return jobPromise(new SignJob(
return jobPromise(() => new SignJob(
kCryptoJobAsync,
mode,
key[kKeyObject][kHandle],
2 changes: 1 addition & 1 deletion lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
@@ -357,7 +357,7 @@ async function ecdhDeriveBits(algorithm, baseKey, length) {
throw lazyDOMException('Named curve mismatch', 'InvalidAccessError');
}

const bits = await jobPromise(new ECDHBitsJob(
const bits = await jobPromise(() => new ECDHBitsJob(
kCryptoJobAsync,
key.algorithm.name === 'ECDH' ? baseKey.algorithm.namedCurve : baseKey.algorithm.name,
key[kKeyObject][kHandle],
4 changes: 2 additions & 2 deletions lib/internal/crypto/ec.js
Original file line number Diff line number Diff line change
@@ -150,7 +150,7 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {
}

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

return jobPromise(new SignJob(
return jobPromise(() => new SignJob(
kCryptoJobAsync,
mode,
key[kKeyObject][kHandle],
2 changes: 1 addition & 1 deletion lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
@@ -183,7 +183,7 @@ async function asyncDigest(algorithm, data) {
case 'SHA-384':
// Fall through
case 'SHA-512':
return jobPromise(new HashJob(
return jobPromise(() => new HashJob(
kCryptoJobAsync,
normalizeHashName(algorithm.name),
data,
2 changes: 1 addition & 1 deletion lib/internal/crypto/mac.js
Original file line number Diff line number Diff line change
@@ -183,7 +183,7 @@ async function hmacImportKey(

function hmacSignVerify(key, data, algorithm, signature) {
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
return jobPromise(new HmacJob(
return jobPromise(() => new HmacJob(
kCryptoJobAsync,
mode,
normalizeHashName(key.algorithm.hash.name),
6 changes: 3 additions & 3 deletions lib/internal/crypto/rsa.js
Original file line number Diff line number Diff line change
@@ -116,7 +116,7 @@ function rsaOaepCipher(mode, key, data, { label }) {
validateMaxBufferLength(label, 'algorithm.label');
}

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

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

return jobPromise(new SignJob(
return jobPromise(() => new SignJob(
kCryptoJobAsync,
signature === undefined ? kSignJobModeSign : kSignJobModeVerify,
key[kKeyObject][kHandle],
11 changes: 8 additions & 3 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
@@ -286,10 +286,15 @@ function onDone(resolve, reject, err, result) {
resolve(result);
}

function jobPromise(job) {
function jobPromise(getJob) {
return new Promise((resolve, reject) => {
job.ondone = FunctionPrototypeBind(onDone, job, resolve, reject);
job.run();
try {
const job = getJob();
job.ondone = FunctionPrototypeBind(onDone, job, resolve, reject);
job.run();
} catch (err) {
onDone(resolve, reject, err);
}
});
}

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

const kSourceData = {