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: fix webcrypto WPT expected failures #43423

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
25 changes: 20 additions & 5 deletions lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const {
prepareSecretKey,
} = require('internal/crypto/keys');

const {
lazyDOMException,
} = require('internal/util');

const {
Buffer,
} = require('buffer');
Expand Down Expand Up @@ -171,11 +175,22 @@ async function asyncDigest(algorithm, data) {
if (algorithm.length !== undefined)
validateUint32(algorithm.length, 'algorithm.length');

return jobPromise(new HashJob(
kCryptoJobAsync,
normalizeHashName(algorithm.name),
data,
algorithm.length));
switch (algorithm.name) {
case 'SHA-1':
// Fall through
case 'SHA-256':
// Fall through
case 'SHA-384':
// Fall through
case 'SHA-512':
return jobPromise(new HashJob(
kCryptoJobAsync,
normalizeHashName(algorithm.name),
data,
algorithm.length));
}

throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ async function rsaKeyGenerate(
return new Promise((resolve, reject) => {
generateKeyPair('rsa', {
modulusLength,
publicExponentConverted,
publicExponent: publicExponentConverted,
}, (err, pubKey, privKey) => {
if (err) {
return reject(lazyDOMException(
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ async function generateKey(
algorithm = normalizeAlgorithm(algorithm);
validateBoolean(extractable, 'extractable');
validateArray(keyUsages, 'keyUsages');
if (keyUsages.length === 0) {
throw lazyDOMException(
'Usages cannot be empty when creating a key',
'SyntaxError');
}
switch (algorithm.name) {
case 'RSASSA-PKCS1-v1_5':
// Fall through
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-webcrypto-digest.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,9 @@ async function testDigest(size, name) {

await Promise.all(variations);
})().then(common.mustCall());

(async () => {
assert.rejects(subtle.digest('RSA-OAEP', Buffer.alloc(1)), {
name: 'NotSupportedError',
});
})().then(common.mustCall());
19 changes: 19 additions & 0 deletions test/parallel/test-webcrypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ const vectors = {
if (!vectors[name].usages.includes(usage))
invalidUsages.push(usage);
});
await assert.rejects(
subtle.generateKey(
{
name, ...vectors[name].algorithm
},
true,
[]),
{ message: /Usages cannot be empty/ });
return assert.rejects(
subtle.generateKey(
{
Expand Down Expand Up @@ -342,6 +350,17 @@ const vectors = {
code: 'ERR_INVALID_ARG_TYPE'
});
}));

await Promise.all([[1], [1, 0, 0]].map((publicExponent) => {
return assert.rejects(subtle.generateKey({
name,
modulusLength,
publicExponent: new Uint8Array(publicExponent),
hash
}, true, usages), {
name: 'OperationError',
});
}));
}

const kTests = [
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-webcrypto-sign-verify-hmac.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async function testSign({ hash,
}

await assert.rejects(
subtle.generateKey({ name }, false, []), {
subtle.generateKey({ name }, false, ['sign', 'verify']), {
name: 'TypeError',
code: 'ERR_MISSING_OPTION',
message: 'algorithm.hash is required'
Expand Down