Skip to content

Commit

Permalink
[WebCryptoAPI] Additional invalid cases for the JWK import tests (#49383
Browse files Browse the repository at this point in the history
)
  • Loading branch information
javifernandez authored Nov 30, 2024
1 parent 982d859 commit 76dfa54
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
22 changes: 22 additions & 0 deletions WebCryptoAPI/import_export/ec_importKey_failures_fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ function getMismatchedJWKKeyData(algorithm) {
return [];
}

function getMismatchedKtyField(algorithm) {
return mismatchedKtyField[algorithm.name];
}

function getMismatchedCrvField(algorithm) {
return mismatchedCrvField[algorithm.name];
}

var validKeyData = {
"P-521": [
{
Expand Down Expand Up @@ -201,3 +209,17 @@ var missingJWKFieldKeyData = {
}
]
};

// The 'kty' field doesn't match the key algorithm.
var mismatchedKtyField = {
"P-521": "OKP",
"P-256": "OKP",
"P-384": "OKP",
}

// The 'kty' field doesn't match the key algorithm.
var mismatchedCrvField = {
"P-521": "P-256",
"P-256": "P-384",
"P-384": "P-521",
}
60 changes: 60 additions & 0 deletions WebCryptoAPI/import_export/importKey_failures.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,64 @@ function run_test(algorithmNames) {
});
});

// The 'kty' field is not correct.
testVectors.forEach(function(vector) {
var name = vector.name;
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
getValidKeyData(algorithm).forEach(function(test) {
if (test.format === "jwk") {
var data = {crv: test.data.crv, kty: test.data.kty, d: test.data.d, x: test.data.x, d: test.data.d};
data.kty = getMismatchedKtyField(algorithm);
var usages = validUsages(vector, 'jwk', test.data);
testError('jwk', algorithm, data, name, usages, true, "DataError", "Invalid 'kty' field");
}
});
});
});

// The 'ext' field is not correct.
testVectors.forEach(function(vector) {
var name = vector.name;
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
getValidKeyData(algorithm).forEach(function(test) {
if (test.format === "jwk") {
var data = {crv: test.data.crv, kty: test.data.kty, d: test.data.d, x: test.data.x, d: test.data.d};
data.ext = false;
var usages = validUsages(vector, 'jwk', test.data);
testError('jwk', algorithm, data, name, usages, true, "DataError", "Import from a non-extractable");
}
});
});
});

// The 'use' field is incorrect.
testVectors.forEach(function(vector) {
var name = vector.name;
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
getValidKeyData(algorithm).forEach(function(test) {
if (test.format === "jwk") {
var data = {crv: test.data.crv, kty: test.data.kty, d: test.data.d, x: test.data.x, d: test.data.d};
data.use = "invalid";
var usages = validUsages(vector, 'jwk', test.data);
if (usages.length !== 0)
testError('jwk', algorithm, data, name, usages, true, "DataError", "Invalid 'use' field");
}
});
});
});

// The 'crv' field is incorrect.
testVectors.forEach(function(vector) {
var name = vector.name;
allAlgorithmSpecifiersFor(name).forEach(function(algorithm) {
getValidKeyData(algorithm).forEach(function(test) {
if (test.format === "jwk") {
var data = {crv: test.data.crv, kty: test.data.kty, d: test.data.d, x: test.data.x, d: test.data.d};
data.crv = getMismatchedCrvField(algorithm)
var usages = validUsages(vector, 'jwk', test.data);
testError('jwk', algorithm, data, name, usages, true, "DataError", "Invalid 'crv' field");
}
});
});
});
}
24 changes: 24 additions & 0 deletions WebCryptoAPI/import_export/okp_importKey_failures_fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ function getMismatchedJWKKeyData(algorithm) {
return mismatchedJWKKeyData[algorithm.name];
}

function getMismatchedKtyField(algorithm) {
return mismatchedKtyField[algorithm.name];
}

function getMismatchedCrvField(algorithm) {
return mismatchedCrvField[algorithm.name];
}

var validKeyData = {
"Ed25519": [
{
Expand Down Expand Up @@ -412,3 +420,19 @@ var mismatchedJWKKeyData = {
},
],
}

// The 'kty' field doesn't match the key algorithm.
var mismatchedKtyField = {
"Ed25519": "EC",
"X25519": "EC",
"Ed448": "EC",
"X448": "EC",
}

// The 'kty' field doesn't match the key algorithm.
var mismatchedCrvField = {
"Ed25519": "X25519",
"X25519": "Ed448",
"Ed448": "X25519",
"X448": "Ed25519",
}

0 comments on commit 76dfa54

Please sign in to comment.