Skip to content

Commit 1b63a82

Browse files
panvaaduh95
authored andcommittedOct 23, 2024
test,crypto: update WebCryptoAPI WPT
PR-URL: #55427 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent adead26 commit 1b63a82

12 files changed

+707
-471
lines changed
 

‎test/fixtures/wpt/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Last update:
3232
- user-timing: https://github.com/web-platform-tests/wpt/tree/5ae85bf826/user-timing
3333
- wasm/jsapi: https://github.com/web-platform-tests/wpt/tree/cde25e7e3c/wasm/jsapi
3434
- wasm/webapi: https://github.com/web-platform-tests/wpt/tree/fd1b23eeaa/wasm/webapi
35-
- WebCryptoAPI: https://github.com/web-platform-tests/wpt/tree/203d2ac459/WebCryptoAPI
35+
- WebCryptoAPI: https://github.com/web-platform-tests/wpt/tree/bef6fa3566/WebCryptoAPI
3636
- webidl/ecmascript-binding/es-exceptions: https://github.com/web-platform-tests/wpt/tree/a370aad338/webidl/ecmascript-binding/es-exceptions
3737
- webmessaging/broadcastchannel: https://github.com/web-platform-tests/wpt/tree/e97fac4791/webmessaging/broadcastchannel
3838
- webstorage: https://github.com/web-platform-tests/wpt/tree/9dafa89214/webstorage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// META: title=WebCryptoAPI: importKey() for Failures
2+
// META: timeout=long
3+
// META: script=../util/helpers.js
4+
// META: script=ec_importKey_failures_fixtures.js
5+
// META: script=importKey_failures.js
6+
7+
// Setup: define the correct behaviors that should be sought, and create
8+
// helper functions that generate all possible test parameters for
9+
// different situations.
10+
run_test(["ECDH"]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// META: title=WebCryptoAPI: importKey() for Failures
2+
// META: timeout=long
3+
// META: script=../util/helpers.js
4+
// META: script=ec_importKey_failures_fixtures.js
5+
// META: script=importKey_failures.js
6+
7+
// Setup: define the correct behaviors that should be sought, and create
8+
// helper functions that generate all possible test parameters for
9+
// different situations.
10+
run_test(["ECDSA"]);

‎test/fixtures/wpt/WebCryptoAPI/import_export/ec_importKey_failures_fixtures.js

+203
Large diffs are not rendered by default.

‎test/fixtures/wpt/WebCryptoAPI/import_export/okp_importKey_failures.js ‎test/fixtures/wpt/WebCryptoAPI/import_export/importKey_failures.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ function run_test(algorithmNames) {
2020
var allTestVectors = [ // Parameters that should work for importKey / exportKey
2121
{name: "Ed25519", privateUsages: ["sign"], publicUsages: ["verify"]},
2222
{name: "Ed448", privateUsages: ["sign"], publicUsages: ["verify"]},
23+
{name: "ECDSA", privateUsages: ["sign"], publicUsages: ["verify"]},
2324
{name: "X25519", privateUsages: ["deriveKey", "deriveBits"], publicUsages: []},
2425
{name: "X448", privateUsages: ["deriveKey", "deriveBits"], publicUsages: []},
26+
{name: "ECDH", privateUsages: ["deriveKey", "deriveBits"], publicUsages: []}
2527
];
2628

2729
var testVectors = [];
@@ -109,6 +111,10 @@ function run_test(algorithmNames) {
109111
return [];
110112
}
111113

114+
function isPrivateKey(data) {
115+
return data.d !== undefined;
116+
}
117+
112118
// Now test for properly handling errors
113119
// - Unsupported algorithm
114120
// - Bad usages for algorithm
@@ -121,8 +127,8 @@ function run_test(algorithmNames) {
121127
// due to SyntaxError
122128
testVectors.forEach(function(vector) {
123129
var name = vector.name;
124-
validKeyData.forEach(function(test) {
125-
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
130+
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
131+
getValidKeyData(algorithm).forEach(function(test) {
126132
invalidUsages(validUsages(vector, test.format, test.data)).forEach(function(usages) {
127133
[true, false].forEach(function(extractable) {
128134
testError(test.format, algorithm, test.data, name, usages, extractable, "SyntaxError", "Bad usages");
@@ -136,20 +142,20 @@ function run_test(algorithmNames) {
136142
// Should fail due to SyntaxError
137143
testVectors.forEach(function(vector) {
138144
var name = vector.name;
139-
validKeyData.filter((test) => test.format === 'pkcs8' || (test.format === 'jwk' && test.data.d)).forEach(function(test) {
140-
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
145+
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
146+
getValidKeyData(algorithm).filter((test) => test.format === 'pkcs8' || (test.format === 'jwk' && isPrivateKey(test.data))).forEach(function(test) {
141147
[true, false].forEach(function(extractable) {
142148
testError(test.format, algorithm, test.data, name, [/* Empty usages */], extractable, "SyntaxError", "Empty usages");
143149
});
144150
});
145151
});
146152
});
147153

148-
// Algorithms normalize okay, usages ok. The length of the key must thouw a DataError exception.
154+
// Algorithms normalize okay, usages ok. The length of the key must throw a DataError exception.
149155
testVectors.forEach(function(vector) {
150156
var name = vector.name;
151-
badKeyLengthData.forEach(function(test) {
152-
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
157+
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
158+
getBadKeyLengthData(algorithm).forEach(function(test) {
153159
allValidUsages(validUsages(vector, test.format, test.data)).forEach(function(usages) {
154160
[true, false].forEach(function(extractable) {
155161
testError(test.format, algorithm, test.data, name, usages, extractable, "DataError", "Bad key length");
@@ -159,11 +165,11 @@ function run_test(algorithmNames) {
159165
});
160166
});
161167

162-
// Algorithms normalize okay, usages ok and valid key. The lack of the mandatory JWK parameter must throw a syntax error.
168+
// Algorithms normalize okay, usages ok and valid key. The lack of the mandatory JWK parameter must throw a DataError exception.
163169
testVectors.forEach(function(vector) {
164170
var name = vector.name;
165-
missingJWKFieldKeyData.forEach(function(test) {
166-
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
171+
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
172+
getMissingJWKFieldKeyData(algorithm).forEach(function(test) {
167173
allValidUsages(validUsages(vector, 'jwk', test.data)).forEach(function(usages) {
168174
[true, false].forEach(function(extractable) {
169175
testError('jwk', algorithm, test.data, name, usages, extractable, "DataError", "Missing JWK '" + test.param + "' parameter");
@@ -176,8 +182,8 @@ function run_test(algorithmNames) {
176182
// Algorithms normalize okay, usages ok and valid key. The public key is not compatible with the private key.
177183
testVectors.forEach(function(vector) {
178184
var name = vector.name;
179-
invalidJWKKeyData.forEach(function(data) {
180-
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
185+
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
186+
getMismatchedJWKKeyData(algorithm).forEach(function(data) {
181187
allValidUsages(vector.privateUsages).forEach(function(usages) {
182188
[true].forEach(function(extractable) {
183189
testError('jwk', algorithm, data, name, usages, extractable, "DataError", "Invalid key pair");

‎test/fixtures/wpt/WebCryptoAPI/import_export/okp_importKey.js

+42-38
Original file line numberDiff line numberDiff line change
@@ -40,51 +40,55 @@ function runTests(algorithmName) {
4040
// Test importKey with a given key format and other parameters. If
4141
// extrable is true, export the key and verify that it matches the input.
4242
function testFormat(format, algorithm, keyData, keySize, usages, extractable) {
43-
promise_test(function(test) {
44-
return subtle.importKey(format, keyData[format], algorithm, extractable, usages).
45-
then(function(key) {
46-
assert_equals(key.constructor, CryptoKey, "Imported a CryptoKey object");
47-
assert_goodCryptoKey(key, algorithm, extractable, usages, (format === 'pkcs8' || (format === 'jwk' && keyData[format].d)) ? 'private' : 'public');
48-
if (!extractable) {
49-
return;
50-
}
43+
[algorithm, algorithm.name].forEach((alg) => {
44+
promise_test(function(test) {
45+
return subtle.importKey(format, keyData[format], alg, extractable, usages).
46+
then(function(key) {
47+
assert_equals(key.constructor, CryptoKey, "Imported a CryptoKey object");
48+
assert_goodCryptoKey(key, algorithm, extractable, usages, (format === 'pkcs8' || (format === 'jwk' && keyData[format].d)) ? 'private' : 'public');
49+
if (!extractable) {
50+
return;
51+
}
5152

52-
return subtle.exportKey(format, key).
53-
then(function(result) {
54-
if (format !== "jwk") {
55-
assert_true(equalBuffers(keyData[format], result), "Round trip works");
56-
} else {
57-
assert_true(equalJwk(keyData[format], result), "Round trip works");
58-
}
59-
}, function(err) {
60-
assert_unreached("Threw an unexpected error: " + err.toString());
61-
});
62-
}, function(err) {
63-
assert_unreached("Threw an unexpected error: " + err.toString());
64-
});
65-
}, "Good parameters: " + keySize.toString() + " bits " + parameterString(format, keyData[format], algorithm, extractable, usages));
53+
return subtle.exportKey(format, key).
54+
then(function(result) {
55+
if (format !== "jwk") {
56+
assert_true(equalBuffers(keyData[format], result), "Round trip works");
57+
} else {
58+
assert_true(equalJwk(keyData[format], result), "Round trip works");
59+
}
60+
}, function(err) {
61+
assert_unreached("Threw an unexpected error: " + err.toString());
62+
});
63+
}, function(err) {
64+
assert_unreached("Threw an unexpected error: " + err.toString());
65+
});
66+
}, "Good parameters: " + keySize.toString() + " bits " + parameterString(format, keyData[format], alg, extractable, usages));
67+
});
6668
}
6769

6870
// Test importKey/exportKey "alg" behaviours, alg is ignored upon import and alg is missing for Ed25519 and Ed448 JWK export
6971
// https://github.com/WICG/webcrypto-secure-curves/pull/24
7072
function testJwkAlgBehaviours(algorithm, keyData, crv, usages) {
71-
promise_test(function(test) {
72-
return subtle.importKey('jwk', { ...keyData, alg: 'this is ignored' }, algorithm, true, usages).
73-
then(function(key) {
74-
assert_equals(key.constructor, CryptoKey, "Imported a CryptoKey object");
75-
76-
return subtle.exportKey('jwk', key).
77-
then(function(result) {
78-
assert_equals(Object.keys(result).length, keyData.d ? 6 : 5, "Correct number of JWK members");
79-
assert_equals(result.alg, undefined, 'No JWK "alg" member is present');
80-
assert_true(equalJwk(keyData, result), "Round trip works");
81-
}, function(err) {
73+
[algorithm, algorithm.name].forEach((alg) => {
74+
promise_test(function(test) {
75+
return subtle.importKey('jwk', { ...keyData, alg: 'this is ignored' }, alg, true, usages).
76+
then(function(key) {
77+
assert_equals(key.constructor, CryptoKey, "Imported a CryptoKey object");
78+
79+
return subtle.exportKey('jwk', key).
80+
then(function(result) {
81+
assert_equals(Object.keys(result).length, keyData.d ? 6 : 5, "Correct number of JWK members");
82+
assert_equals(result.alg, undefined, 'No JWK "alg" member is present');
83+
assert_true(equalJwk(keyData, result), "Round trip works");
84+
}, function(err) {
8285
assert_unreached("Threw an unexpected error: " + err.toString());
83-
});
84-
}, function(err) {
85-
assert_unreached("Threw an unexpected error: " + err.toString());
86-
});
87-
}, "Good parameters with ignored JWK alg: " + crv.toString() + " " + parameterString('jwk', keyData, algorithm, true, usages));
86+
});
87+
}, function(err) {
88+
assert_unreached("Threw an unexpected error: " + err.toString());
89+
});
90+
}, "Good parameters with ignored JWK alg: " + crv.toString() + " " + parameterString('jwk', keyData, alg, true, usages));
91+
});
8892
}
8993

9094

Original file line numberDiff line numberDiff line change
@@ -1,110 +1,7 @@
11
// META: title=WebCryptoAPI: importKey() for Failures
22
// META: timeout=long
33
// META: script=../util/helpers.js
4-
// META: script=okp_importKey_failures.js
5-
6-
// Setup: define the correct behaviors that should be sought, and create
7-
// helper functions that generate all possible test parameters for
8-
// different situations.
9-
var validKeyData = [
10-
{
11-
format: "spki",
12-
data: new Uint8Array([48, 42, 48, 5, 6, 3, 43, 101, 112, 3, 33, 0, 216, 225, 137, 99, 216, 9, 212, 135, 217, 84, 154, 204, 174, 198, 116, 46, 126, 235, 162, 77, 138, 13, 59, 20, 183, 227, 202, 234, 6, 137, 61, 204])
13-
},
14-
{
15-
format: "pkcs8",
16-
data: new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32, 243, 200, 244, 196, 141, 248, 120, 20, 110, 140, 211, 191, 109, 244, 229, 14, 56, 155, 167, 7, 78, 21, 194, 53, 45, 205, 93, 48, 141, 76, 168, 31])
17-
},
18-
{
19-
format: "raw",
20-
data: new Uint8Array([216, 225, 137, 99, 216, 9, 212, 135, 217, 84, 154, 204, 174, 198, 116, 46, 126, 235, 162, 77, 138, 13, 59, 20, 183, 227, 202, 234, 6, 137, 61, 204])
21-
},
22-
{
23-
format: "jwk",
24-
data: {
25-
crv: "Ed25519",
26-
d: "88j0xI34eBRujNO_bfTlDjibpwdOFcI1Lc1dMI1MqB8",
27-
x: "2OGJY9gJ1IfZVJrMrsZ0Ln7rok2KDTsUt-PK6gaJPcw",
28-
kty: "OKP"
29-
},
30-
},
31-
{
32-
format: "jwk",
33-
data: {
34-
crv: "Ed25519",
35-
x: "2OGJY9gJ1IfZVJrMrsZ0Ln7rok2KDTsUt-PK6gaJPcw",
36-
kty: "OKP"
37-
},
38-
},
39-
];
40-
41-
// Removed just the last byte.
42-
var badKeyLengthData = [
43-
{
44-
format: "spki",
45-
data: new Uint8Array([48, 42, 48, 5, 6, 3, 43, 101, 112, 3, 33, 0, 216, 225, 137, 99, 216, 9, 212, 135, 217, 84, 154, 204, 174, 198, 116, 46, 126, 235, 162, 77, 138, 13, 59, 20, 183, 227, 202, 234, 6, 137, 61])
46-
},
47-
{
48-
format: "pkcs8",
49-
data: new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32, 243, 200, 244, 196, 141, 248, 120, 20, 110, 140, 211, 191, 109, 244, 229, 14, 56, 155, 167, 7, 78, 21, 194, 53, 45, 205, 93, 48, 141, 76, 168])
50-
},
51-
{
52-
format: "raw",
53-
data: new Uint8Array([216, 225, 137, 99, 216, 9, 212, 135, 217, 84, 154, 204, 174, 198, 116, 46, 126, 235, 162, 77, 138, 13, 59, 20, 183, 227, 202, 234, 6, 137, 61])
54-
},
55-
{
56-
format: "jwk",
57-
data: {
58-
crv: "Ed25519",
59-
d: "88j0xI34eBRujNO_bfTlDjibpwdOFcI1Lc1dMI1MqB",
60-
x: "2OGJY9gJ1IfZVJrMrsZ0Ln7rok2KDTsUt-PK6gaJPcw",
61-
kty: "OKP"
62-
}
63-
},
64-
{
65-
format: "jwk",
66-
data: {
67-
crv: "Ed25519",
68-
x: "2OGJY9gJ1IfZVJrMrsZ0Ln7rok2KDTsUt-PK6gaJPc",
69-
kty: "OKP"
70-
}
71-
},
72-
];
73-
74-
var missingJWKFieldKeyData = [
75-
{
76-
param: "x",
77-
data: {
78-
crv: "Ed25519",
79-
d: "88j0xI34eBRujNO_bfTlDjibpwdOFcI1Lc1dMI1MqB8",
80-
kty: "OKP"
81-
},
82-
},
83-
{
84-
param: "kty",
85-
data: {
86-
crv: "Ed25519",
87-
x: "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo",
88-
d: "88j0xI34eBRujNO_bfTlDjibpwdOFcI1Lc1dMI1MqB8",
89-
},
90-
},
91-
{
92-
param: "crv",
93-
data: {
94-
x: "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo",
95-
kty: "OKP"
96-
},
97-
}
98-
];
99-
100-
// The public key doesn't match the private key.
101-
var invalidJWKKeyData = [
102-
{
103-
crv: "Ed25519",
104-
d: "88j0xI34eBRujNO_bfTlDjibpwdOFcI1Lc1dMI1MqB8",
105-
x: "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo",
106-
kty: "OKP"
107-
},
108-
];
4+
// META: script=okp_importKey_failures_fixtures.js
5+
// META: script=importKey_failures.js
1096

1107
run_test(["Ed25519"]);

0 commit comments

Comments
 (0)