Skip to content

Commit ecbdbcb

Browse files
committed
Implement failure tests cases for the EC import operation
This PR implements new tests for the import operation of ECDH and ECDSA algorithms, defining failure test cases. Additionally, it applies some refactoring to the code already used for OPK keys.
1 parent 5be647d commit ecbdbcb

9 files changed

Lines changed: 663 additions & 431 deletions
Lines changed: 10 additions & 0 deletions
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"]);
Lines changed: 10 additions & 0 deletions
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"]);

WebCryptoAPI/import_export/ec_importKey_failures_fixtures.js

Lines changed: 203 additions & 0 deletions
Large diffs are not rendered by default.

WebCryptoAPI/import_export/okp_importKey_failures.js renamed to WebCryptoAPI/import_export/importKey_failures.js

Lines changed: 18 additions & 12 deletions
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");
Lines changed: 2 additions & 105 deletions
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"]);
Lines changed: 2 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,8 @@
11
// META: title=WebCryptoAPI: importKey() for Failures
22
// META: timeout=long
33
// META: script=../util/helpers.js
4-
// META: script=okp_importKey_failures.js
4+
// META: script=okp_importKey_failures_fixtures.js
5+
// META: script=importKey_failures.js
56

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, 67, 48, 5, 6, 3, 43, 101, 113, 3, 58, 0, 171, 75, 184, 133, 253, 125, 44, 90, 242, 78, 131, 113, 12, 255, 160, 199, 74, 87, 226, 116, 128, 29, 178, 5, 123, 11, 220, 94, 160, 50, 182, 254, 107, 199, 139, 128, 69, 54, 90, 235, 38, 232, 110, 31, 20, 253, 52, 157, 7, 196, 132, 149, 245, 164, 106, 90, 128]),
13-
},
14-
{
15-
format: "pkcs8",
16-
data: new Uint8Array([48, 71, 2, 1, 0, 48, 5, 6, 3, 43, 101, 113, 4, 59, 4, 57, 14, 255, 3, 69, 140, 40, 224, 23, 156, 82, 29, 227, 18, 201, 105, 183, 131, 67, 72, 236, 171, 153, 26, 96, 227, 178, 233, 167, 158, 76, 217, 228, 128, 239, 41, 23, 18, 210, 200, 61, 4, 114, 114, 213, 201, 244, 40, 102, 79, 105, 109, 38, 112, 69, 143, 29, 46]),
17-
},
18-
{
19-
format: "raw",
20-
data: new Uint8Array([171, 75, 184, 133, 253, 125, 44, 90, 242, 78, 131, 113, 12, 255, 160, 199, 74, 87, 226, 116, 128, 29, 178, 5, 123, 11, 220, 94, 160, 50, 182, 254, 107, 199, 139, 128, 69, 54, 90, 235, 38, 232, 110, 31, 20, 253, 52, 157, 7, 196, 132, 149, 245, 164, 106, 90, 128]),
21-
},
22-
{
23-
format: "jwk",
24-
data: {
25-
crv: "Ed448",
26-
d: "Dv8DRYwo4BecUh3jEslpt4NDSOyrmRpg47Lpp55M2eSA7ykXEtLIPQRyctXJ9ChmT2ltJnBFjx0u",
27-
x: "q0u4hf19LFryToNxDP-gx0pX4nSAHbIFewvcXqAytv5rx4uARTZa6ybobh8U_TSdB8SElfWkalqA",
28-
kty: "OKP"
29-
},
30-
},
31-
{
32-
format: "jwk",
33-
data: {
34-
crv: "Ed448",
35-
x: "q0u4hf19LFryToNxDP-gx0pX4nSAHbIFewvcXqAytv5rx4uARTZa6ybobh8U_TSdB8SElfWkalqA",
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, 67, 48, 5, 6, 3, 43, 101, 113, 3, 58, 0, 171, 75, 184, 133, 253, 125, 44, 90, 242, 78, 131, 113, 12, 255, 160, 199, 74, 87, 226, 116, 128, 29, 178, 5, 123, 11, 220, 94, 160, 50, 182, 254, 107, 199, 139, 128, 69, 54, 90, 235, 38, 232, 110, 31, 20, 253, 52, 157, 7, 196, 132, 149, 245, 164, 106, 90]),
46-
},
47-
{
48-
format: "pkcs8",
49-
data: new Uint8Array([48, 71, 2, 1, 0, 48, 5, 6, 3, 43, 101, 113, 4, 59, 4, 57, 14, 255, 3, 69, 140, 40, 224, 23, 156, 82, 29, 227, 18, 201, 105, 183, 131, 67, 72, 236, 171, 153, 26, 96, 227, 178, 233, 167, 158, 76, 217, 228, 128, 239, 41, 23, 18, 210, 200, 61, 4, 114, 114, 213, 201, 244, 40, 102, 79, 105, 109, 38, 112, 69, 143, 29]),
50-
},
51-
{
52-
format: "raw",
53-
data: new Uint8Array([171, 75, 184, 133, 253, 125, 44, 90, 242, 78, 131, 113, 12, 255, 160, 199, 74, 87, 226, 116, 128, 29, 178, 5, 123, 11, 220, 94, 160, 50, 182, 254, 107, 199, 139, 128, 69, 54, 90, 235, 38, 232, 110, 31, 20, 253, 52, 157, 7, 196, 132, 149, 245, 164, 106, 90]),
54-
},
55-
{
56-
format: "jwk",
57-
data: {
58-
crv: "Ed448",
59-
d: "Dv8DRYwo4BecUh3jEslpt4NDSOyrmRpg47Lpp55M2eSA7ykXEtLIPQRyctXJ9ChmT2ltJnBFjx0",
60-
x: "q0u4hf19LFryToNxDP-gx0pX4nSAHbIFewvcXqAytv5rx4uARTZa6ybobh8U_TSdB8SElfWkalqA",
61-
kty: "OKP"
62-
},
63-
},
64-
{
65-
format: "jwk",
66-
data: {
67-
crv: "Ed448",
68-
x: "q0u4hf19LFryToNxDP-gx0pX4nSAHbIFewvcXqAytv5rx4uARTZa6ybobh8U_TSdB8SElfWkalq",
69-
kty: "OKP"
70-
},
71-
},
72-
];
73-
74-
var missingJWKFieldKeyData = [
75-
{
76-
param: "x",
77-
data: {
78-
crv: "Ed448",
79-
d: "Dv8DRYwo4BecUh3jEslpt4NDSOyrmRpg47Lpp55M2eSA7ykXEtLIPQRyctXJ9ChmT2ltJnBFjx0u",
80-
kty: "OKP"
81-
}
82-
},
83-
{
84-
param: "kty",
85-
data: {
86-
crv: "Ed448",
87-
d: "Dv8DRYwo4BecUh3jEslpt4NDSOyrmRpg47Lpp55M2eSA7ykXEtLIPQRyctXJ9ChmT2ltJnBFjx0u",
88-
x: "q0u4hf19LFryToNxDP-gx0pX4nSAHbIFewvcXqAytv5rx4uARTZa6ybobh8U_TSdB8SElfWkalqA",
89-
}
90-
},
91-
{
92-
param: "crv",
93-
data: {
94-
d: "Dv8DRYwo4BecUh3jEslpt4NDSOyrmRpg47Lpp55M2eSA7ykXEtLIPQRyctXJ9ChmT2ltJnBFjx0u",
95-
x: "q0u4hf19LFryToNxDP-gx0pX4nSAHbIFewvcXqAytv5rx4uARTZa6ybobh8U_TSdB8SElfWkalqA",
96-
kty: "OKP"
97-
}
98-
}
99-
];
100-
101-
// The public key doesn't match the private key.
102-
var invalidJWKKeyData = [
103-
{
104-
crv: "Ed448",
105-
d: "Dv8DRYwo4BecUh3jEslpt4NDSOyrmRpg47Lpp55M2eSA7ykXEtLIPQRyctXJ9ChmT2ltJnBFjx0u",
106-
x: "X9dEm1m0Yf0s54fsYWrUah2hNCSFpw4fig6nXYDpZ3jt8SR2m0bHBhvWeD3x5Q9s0foavq_oJWGA",
107-
kty: "OKP"
108-
},
109-
];
1107

1118
run_test(["Ed448"]);

0 commit comments

Comments
 (0)