From ea8fd2dfe0a234c6ea1ad245cc05dfe833f94adc Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Wed, 10 May 2023 18:42:06 +0200 Subject: [PATCH] test,crypto: update WebCryptoAPI WPT PR-URL: https://github.com/nodejs/node/pull/47921 Refs: https://github.com/nodejs/node/issues/47864 Reviewed-By: Yagiz Nizipli Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- test/fixtures/wpt/README.md | 2 +- .../wpt/WebCryptoAPI/generateKey/failures.js | 2 +- .../import_export/ec_importKey.https.any.js | 25 +++++++++++--- .../import_export/okp_importKey_failures.js | 13 ++++++++ .../import_export/rsa_importKey.https.any.js | 24 +++++++++++--- .../symmetric_importKey.https.any.js | 33 ++++++++++++++----- test/fixtures/wpt/versions.json | 2 +- 7 files changed, 79 insertions(+), 22 deletions(-) diff --git a/test/fixtures/wpt/README.md b/test/fixtures/wpt/README.md index 3f4c7dc79e134d..49506a6cd2a86d 100644 --- a/test/fixtures/wpt/README.md +++ b/test/fixtures/wpt/README.md @@ -31,7 +31,7 @@ Last update: - user-timing: https://github.com/web-platform-tests/wpt/tree/df24fb604e/user-timing - wasm/jsapi: https://github.com/web-platform-tests/wpt/tree/cde25e7e3c/wasm/jsapi - wasm/webapi: https://github.com/web-platform-tests/wpt/tree/fd1b23eeaa/wasm/webapi -- WebCryptoAPI: https://github.com/web-platform-tests/wpt/tree/188993d46b/WebCryptoAPI +- WebCryptoAPI: https://github.com/web-platform-tests/wpt/tree/17b7ca10fd/WebCryptoAPI - webidl/ecmascript-binding/es-exceptions: https://github.com/web-platform-tests/wpt/tree/a370aad338/webidl/ecmascript-binding/es-exceptions - webmessaging/broadcastchannel: https://github.com/web-platform-tests/wpt/tree/e97fac4791/webmessaging/broadcastchannel diff --git a/test/fixtures/wpt/WebCryptoAPI/generateKey/failures.js b/test/fixtures/wpt/WebCryptoAPI/generateKey/failures.js index c39e4d211cbdf4..e0f0279a69bb88 100644 --- a/test/fixtures/wpt/WebCryptoAPI/generateKey/failures.js +++ b/test/fixtures/wpt/WebCryptoAPI/generateKey/failures.js @@ -204,7 +204,7 @@ function run_test(algorithmNames) { }); - // The last thing that should be checked is an empty usages (for secret keys). + // The last thing that should be checked is empty usages (disallowed for secret and private keys). testVectors.forEach(function(vector) { var name = vector.name; diff --git a/test/fixtures/wpt/WebCryptoAPI/import_export/ec_importKey.https.any.js b/test/fixtures/wpt/WebCryptoAPI/import_export/ec_importKey.https.any.js index c70583bf12eba9..25defa369c1d31 100644 --- a/test/fixtures/wpt/WebCryptoAPI/import_export/ec_importKey.https.any.js +++ b/test/fixtures/wpt/WebCryptoAPI/import_export/ec_importKey.https.any.js @@ -85,13 +85,13 @@ }); // Next, test private keys - allValidUsages(vector.privateUsages, []).forEach(function(usages) { - ['pkcs8', 'jwk'].forEach(function(format) { - var algorithm = {name: vector.name, namedCurve: curve}; - var data = keyData[curve]; - + ['pkcs8', 'jwk'].forEach(function(format) { + var algorithm = {name: vector.name, namedCurve: curve}; + var data = keyData[curve]; + allValidUsages(vector.privateUsages, []).forEach(function(usages) { testFormat(format, algorithm, data, curve, usages, extractable); }); + testEmptyUsages(format, algorithm, data, curve, extractable); }); }); @@ -136,6 +136,21 @@ }, "Good parameters: " + keySize.toString() + " bits " + parameterString(format, compressed, keyData, algorithm, extractable, usages)); } + // Test importKey with a given key format and other parameters but with empty usages. + // Should fail with SyntaxError + function testEmptyUsages(format, algorithm, data, keySize, extractable) { + const keyData = data[format]; + const usages = []; + promise_test(function(test) { + return subtle.importKey(format, keyData, algorithm, extractable, usages). + then(function(key) { + assert_unreached("importKey succeeded but should have failed with SyntaxError"); + }, function(err) { + assert_equals(err.name, "SyntaxError", "Should throw correct error, not " + err.name + ": " + err.message); + }); + }, "Empty Usages: " + keySize.toString() + " bits " + parameterString(format, false, keyData, algorithm, extractable, usages)); + } + // Helper methods follow: diff --git a/test/fixtures/wpt/WebCryptoAPI/import_export/okp_importKey_failures.js b/test/fixtures/wpt/WebCryptoAPI/import_export/okp_importKey_failures.js index a5cc08a01e9fc1..ebdb73616d6581 100644 --- a/test/fixtures/wpt/WebCryptoAPI/import_export/okp_importKey_failures.js +++ b/test/fixtures/wpt/WebCryptoAPI/import_export/okp_importKey_failures.js @@ -132,6 +132,19 @@ function run_test(algorithmNames) { }); }); + // Algorithms normalize okay, but usages bad (empty). + // Should fail due to SyntaxError + testVectors.forEach(function(vector) { + var name = vector.name; + validKeyData.filter((test) => test.format === 'pkcs8' || (test.format === 'jwk' && test.data.d)).forEach(function(test) { + allAlgorithmSpecifiersFor(name).forEach(function(algorithm) { + [true, false].forEach(function(extractable) { + testError(test.format, algorithm, test.data, name, [/* Empty usages */], extractable, "SyntaxError", "Empty usages"); + }); + }); + }); + }); + // Algorithms normalize okay, usages ok. The length of the key must thouw a DataError exception. testVectors.forEach(function(vector) { var name = vector.name; diff --git a/test/fixtures/wpt/WebCryptoAPI/import_export/rsa_importKey.https.any.js b/test/fixtures/wpt/WebCryptoAPI/import_export/rsa_importKey.https.any.js index 41d25da89c6505..5582b2f506b067 100644 --- a/test/fixtures/wpt/WebCryptoAPI/import_export/rsa_importKey.https.any.js +++ b/test/fixtures/wpt/WebCryptoAPI/import_export/rsa_importKey.https.any.js @@ -92,13 +92,13 @@ }); // Next, test private keys - allValidUsages(vector.privateUsages, []).forEach(function(usages) { - ['pkcs8', 'jwk'].forEach(function(format) { - var algorithm = {name: vector.name, hash: hash}; - var data = keyData[size]; - + ['pkcs8', 'jwk'].forEach(function(format) { + var algorithm = {name: vector.name, hash: hash}; + var data = keyData[size]; + allValidUsages(vector.privateUsages, []).forEach(function(usages) { testFormat(format, algorithm, data, size, usages, extractable); }); + testEmptyUsages(format, algorithm, data, size, extractable); }); }); }); @@ -135,6 +135,20 @@ }, "Good parameters: " + keySize.toString() + " bits " + parameterString(format, keyData[format], algorithm, extractable, usages)); } + // Test importKey with a given key format and other parameters but with empty usages. + // Should fail with SyntaxError + function testEmptyUsages(format, algorithm, keyData, keySize, extractable) { + const usages = []; + promise_test(function(test) { + return subtle.importKey(format, keyData[format], algorithm, extractable, usages). + then(function(key) { + assert_unreached("importKey succeeded but should have failed with SyntaxError"); + }, function(err) { + assert_equals(err.name, "SyntaxError", "Should throw correct error, not " + err.name + ": " + err.message); + }); + }, "Empty Usages: " + keySize.toString() + " bits " + parameterString(format, keyData, algorithm, extractable, usages)); + } + // Helper methods follow: diff --git a/test/fixtures/wpt/WebCryptoAPI/import_export/symmetric_importKey.https.any.js b/test/fixtures/wpt/WebCryptoAPI/import_export/symmetric_importKey.https.any.js index 404b66ac0022ac..a9ce9be0a1becf 100644 --- a/test/fixtures/wpt/WebCryptoAPI/import_export/symmetric_importKey.https.any.js +++ b/test/fixtures/wpt/WebCryptoAPI/import_export/symmetric_importKey.https.any.js @@ -41,17 +41,18 @@ } rawKeyData.forEach(function(keyData) { - // Generate all combinations of valid usages for testing - allValidUsages(vector.legalUsages, []).forEach(function(usages) { - // Try each legal value of the extractable parameter - vector.extractable.forEach(function(extractable) { - vector.formats.forEach(function(format) { - var data = keyData; - if (format === "jwk") { - data = jwkData(keyData, algorithm); - } + // Try each legal value of the extractable parameter + vector.extractable.forEach(function(extractable) { + vector.formats.forEach(function(format) { + var data = keyData; + if (format === "jwk") { + data = jwkData(keyData, algorithm); + } + // Generate all combinations of valid usages for testing + allValidUsages(vector.legalUsages, []).forEach(function(usages) { testFormat(format, algorithm, data, keyData.length * 8, usages, extractable); }); + testEmptyUsages(format, algorithm, data, keyData.length * 8, extractable); }); }); @@ -90,6 +91,20 @@ }, "Good parameters: " + keySize.toString() + " bits " + parameterString(format, keyData, algorithm, extractable, usages)); } + // Test importKey with a given key format and other parameters but with empty usages. + // Should fail with SyntaxError + function testEmptyUsages(format, algorithm, keyData, keySize, extractable) { + const usages = []; + promise_test(function(test) { + return subtle.importKey(format, keyData, algorithm, extractable, usages). + then(function(key) { + assert_unreached("importKey succeeded but should have failed with SyntaxError"); + }, function(err) { + assert_equals(err.name, "SyntaxError", "Should throw correct error, not " + err.name + ": " + err.message); + }); + }, "Empty Usages: " + keySize.toString() + " bits " + parameterString(format, keyData, algorithm, extractable, usages)); + } + // Helper methods follow: diff --git a/test/fixtures/wpt/versions.json b/test/fixtures/wpt/versions.json index 6b9c6cb80a4659..d6d29dc92ed2cb 100644 --- a/test/fixtures/wpt/versions.json +++ b/test/fixtures/wpt/versions.json @@ -84,7 +84,7 @@ "path": "wasm/webapi" }, "WebCryptoAPI": { - "commit": "188993d46b95c9c0414ba2cef8751f5e19d3d498", + "commit": "17b7ca10fd17ab22e60d62da6bc6e7424ea52740", "path": "WebCryptoAPI" }, "webidl/ecmascript-binding/es-exceptions": {