Commit 71f9af72 authored by eroman@chromium.org's avatar eroman@chromium.org

[refactor] Split importKey.html into smaller files, and delete algoritm-specific tests.

 * importKey-badParameters.html
 * importKey-normalize-usages.html

The reason for deleting the successful key import tests, is that there are much more exhaustive key import tests as part of the clone-* layout tests (which import a large combination of keys in order to serialize them).

Lastly this change fixes a typo in an error message "HmacImporParams" --> "HmacImportParams".

BUG=245025

Review URL: https://codereview.chromium.org/211933003

git-svn-id: svn://svn.chromium.org/blink/trunk@170019 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 2b42bd91
CONSOLE ERROR: Invalid keyFormat argument
CONSOLE ERROR: Invalid keyUsages argument
CONSOLE ERROR: Invalid keyFormat argument
CONSOLE ERROR: Algorithm: HMAC: HmacImportParams: hash: Missing or not a dictionary
CONSOLE ERROR: Algorithm: SHA-1: Unsupported operation
Tests calling cypto.subtle.importKey with bad parameters
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
PASS crypto.subtle.importKey('raw', [], aesCbc, extractable, ['encrypt']) threw exception TypeError: Failed to execute 'importKey' on 'SubtleCrypto': Invalid keyData argument.
PASS crypto.subtle.importKey('raw', null, aesCbc, extractable, ['encrypt']) threw exception TypeError: Failed to execute 'importKey' on 'SubtleCrypto': Invalid keyData argument.
PASS crypto.subtle.importKey('raw', aesCbc, null, extractable, ['encrypt']) threw exception TypeError: Failed to execute 'importKey' on 'SubtleCrypto': Invalid keyData argument.
PASS error is null
PASS error is null
PASS error is null
PASS error is null
PASS error is null
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Tests calling cypto.subtle.importKey with bad parameters");
jsTestIsAsync = true;
var aesCbc = {name: 'aes-cbc'};
var aesKeyBytes = new Uint8Array(16);
var extractable = true;
// Undefined key usage.
// FIXME: http://crbug.com/262383
//shouldThrow("crypto.subtle.importKey('raw', aesKeyBytes, aesCbc, extractable, undefined)");
// Invalid data
shouldThrow("crypto.subtle.importKey('raw', [], aesCbc, extractable, ['encrypt'])");
shouldThrow("crypto.subtle.importKey('raw', null, aesCbc, extractable, ['encrypt'])");
// Invalid algorithm
shouldThrow("crypto.subtle.importKey('raw', aesCbc, null, extractable, ['encrypt'])");
Promise.resolve(null).then(function() {
// Invalid format.
return crypto.subtle.importKey('invalid format', aesKeyBytes, aesCbc, extractable, ['encrypt']);
}).then(failAndFinishJSTest, function(result) {
error = result;
shouldBeNull("error");
// Invalid key usage (case sensitive).
return crypto.subtle.importKey('raw', aesKeyBytes, aesCbc, extractable, ['ENCRYPT']);
}).then(failAndFinishJSTest, function(result) {
error = result;
shouldBeNull("error");
// If both the format and key usage are bogus, should complain about the
// format first.
return crypto.subtle.importKey('invalid format', aesKeyBytes, aesCbc, extractable, ['ENCRYPT']);
}).then(failAndFinishJSTest, function(result) {
error = result;
shouldBeNull("error");
// Missing hash parameter for HMAC.
return crypto.subtle.importKey('raw', new Uint8Array(20), {name: 'hmac'}, extractable, ['sign']);
}).then(failAndFinishJSTest, function(result) {
error = result;
shouldBeNull("error");
// SHA-1 doesn't support the importKey operation.
return crypto.subtle.importKey('raw', new Uint8Array(20), {name: 'sha-1'}, extractable, ['sign']);
}).then(failAndFinishJSTest, function(result) {
error = result;
shouldBeNull("error");
}).then(finishJSTest, failAndFinishJSTest);
</script>
</body>
</html>
CONSOLE ERROR: Invalid keyFormat argument
CONSOLE ERROR: Invalid keyUsages argument
CONSOLE ERROR: Invalid keyFormat argument
CONSOLE ERROR: Algorithm: HMAC: HmacImporParams: hash: Missing or not a dictionary
CONSOLE ERROR: Algorithm: SHA-1: Unsupported operation
Tests cypto.subtle.importKey.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
PASS key.type is 'secret'
PASS key.extractable is true
PASS key.algorithm.name is 'HMAC'
PASS key.algorithm.hash.name is 'SHA-256'
PASS key.usages.join(',') is 'encrypt,sign'
PASS key.type is 'secret'
PASS key.extractable is true
PASS key.algorithm.name is 'AES-CBC'
PASS key.algorithm.length is 128
PASS key.usages.join(',') is ''
PASS key.type is 'secret'
PASS key.extractable is false
PASS key.algorithm.name is 'AES-CBC'
PASS key.algorithm.length is 128
PASS key.usages.join(',') is ''
rejected with null
PASS key.type is 'public'
PASS key.algorithm.name is 'RSASSA-PKCS1-v1_5'
PASS key.algorithm.hash.name is "SHA-1"
PASS key.algorithm.modulusLength is 1024
PASS: key.algorithm.publicExponent should be [010001] and was
PASS key.usages.join(',') is ''
PASS key.type is 'private'
PASS key.extractable is false
PASS key.algorithm.name is 'RSASSA-PKCS1-v1_5'
PASS key.algorithm.hash.name is "SHA-1"
PASS key.algorithm.modulusLength is 1024
PASS: key.algorithm.publicExponent should be [010001] and was
PASS key.usages.join(',') is ''
PASS crypto.subtle.importKey(keyFormat, [], algorithm, extractable, keyUsages) threw exception TypeError: Failed to execute 'importKey' on 'SubtleCrypto': Invalid keyData argument.
PASS crypto.subtle.importKey(keyFormat, null, algorithm, extractable, keyUsages) threw exception TypeError: Failed to execute 'importKey' on 'SubtleCrypto': Invalid keyData argument.
PASS crypto.subtle.importKey(keyFormat, data, null, extractable, keyUsages) threw exception TypeError: Failed to execute 'importKey' on 'SubtleCrypto': Algorithm: Not an object.
PASS: 'crypto.subtle.importKey('invalid format', data, algorithm, extractable, keyUsages)' rejected with null
PASS: 'crypto.subtle.importKey(keyFormat, data, algorithm, extractable, ['SIGN'])' rejected with null
PASS: 'crypto.subtle.importKey('invalid format', data, algorithm, extractable, ['SIGN'])' rejected with null
PASS: 'crypto.subtle.importKey(keyFormat, data, {name: 'hmac'}, extractable, keyUsages)' rejected with null
PASS: 'crypto.subtle.importKey(keyFormat, data, {name: 'sha-1'}, extractable, keyUsages)' rejected with null
PASS successfullyParsed is true
TEST COMPLETE
Tests that the reflected Key.usages are normalized.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
PASS key.usages.join(',') is "encrypt,decrypt,wrapKey"
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Tests that the reflected Key.usages are normalized.");
jsTestIsAsync = true;
var aesCbc = {name: 'aes-cbc'};
var aesKeyBytes = new Uint8Array(16);
var extractable = true;
// The usages includes several duplicates.
crypto.subtle.importKey('raw', aesKeyBytes, aesCbc, extractable, ['decrypt','decrypt','encrypt','wrapKey','encrypt', 'encrypt']).then(function(result) {
key = result;
shouldEvaluateAs("key.usages.join(',')", "encrypt,decrypt,wrapKey");
}).then(finishJSTest, failAndFinishJSTest);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="resources/common.js"></script>
<script src="resources/keys.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Tests cypto.subtle.importKey.");
jsTestIsAsync = true;
aesCbc = {name: 'aes-cbc'};
Promise.resolve(null).then(function() {
keyFormat = "raw";
data = asciiToUint8Array("raw bytes for key");
algorithm = { name: 'hmac', hash: { name: 'sha-256' } };
extractable = true;
// Note there are duplicates
keyUsages = ['encrypt', 'encrypt', 'encrypt', 'sign'];
return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(function(result) {
key = result;
shouldBe("key.type", "'secret'");
shouldBe("key.extractable", "true");
shouldBe("key.algorithm.name", "'HMAC'");
shouldBe("key.algorithm.hash.name", "'SHA-256'");
shouldBe("key.usages.join(',')", "'encrypt,sign'");
// Same test as above, but with an keyUsages, and AES-CBC.
keyFormat = "raw";
data = asciiToUint8Array("16 bytes of key!");
algorithm = aesCbc;
extractable = true;
keyUsages = [];
return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(function(result) {
key = result;
shouldBe("key.type", "'secret'");
shouldBe("key.extractable", "true");
shouldBe("key.algorithm.name", "'AES-CBC'");
shouldBe("key.algorithm.length", "128");
shouldBe("key.usages.join(',')", "''");
// Same test as above, but with extractable = false.
keyFormat = "raw";
data = asciiToUint8Array("16 bytes of key!");
algorithm = aesCbc;
extractable = false;
keyUsages = [];
return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(function(result) {
key = result;
shouldBe("key.type", "'secret'");
shouldBe("key.extractable", "false");
shouldBe("key.algorithm.name", "'AES-CBC'");
shouldBe("key.algorithm.length", "128");
shouldBe("key.usages.join(',')", "''");
// Same test as above, but with keyFormat = spki
keyFormat = "spki";
data = asciiToUint8Array("16 bytes of key!");
algorithm = aesCbc;
extractable = false;
keyUsages = [];
return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(undefined, function(result) {
debug("rejected with " + result);
// Import an spki formatted public key
keyFormat = "spki";
data = hexStringToUint8Array(kKeyData.rsa1.spki);
algorithm = {name: 'RsasSA-pKCS1-v1_5', hash: {name: 'sha-1'}};
extractable = false;
keyUsages = [];
return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(function(result) {
key = result;
shouldBe("key.type", "'public'");
// FIXME: Enable this once updated.
//shouldBe("key.extractable", "true");
shouldBe("key.algorithm.name", "'RSASSA-PKCS1-v1_5'");
shouldBe("key.algorithm.hash.name", '"SHA-1"');
shouldEvaluateAs("key.algorithm.modulusLength", kKeyData.rsa1.modulusLengthBits);
bytesShouldMatchHexString("key.algorithm.publicExponent", kKeyData.rsa1.publicExponent, key.algorithm.publicExponent);
shouldBe("key.usages.join(',')", "''");
// Import a pkcs8 formatted private key
keyFormat = "pkcs8";
data = hexStringToUint8Array(kKeyData.rsa1.pkcs8);
algorithm = {name: 'RsasSA-pKCS1-v1_5', hash: {name: 'sha-1'}};
extractable = false;
keyUsages = [];
return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(function(result) {
key = result;
shouldBe("key.type", "'private'");
shouldBe("key.extractable", "false");
shouldBe("key.algorithm.name", "'RSASSA-PKCS1-v1_5'");
shouldBe("key.algorithm.hash.name", '"SHA-1"');
shouldEvaluateAs("key.algorithm.modulusLength", kKeyData.rsa1.modulusLengthBits);
bytesShouldMatchHexString("key.algorithm.publicExponent", kKeyData.rsa1.publicExponent, key.algorithm.publicExponent);
shouldBe("key.usages.join(',')", "''")
keyFormat = "raw";
data = asciiToUint8Array("");
algorithm = aesCbc;
extractable = true;
keyUsages = [];
// Invalid format.
shouldRejectPromiseWithNull("crypto.subtle.importKey('invalid format', data, algorithm, extractable, keyUsages)");
// Invalid key usage.
shouldRejectPromiseWithNull("crypto.subtle.importKey(keyFormat, data, algorithm, extractable, ['SIGN'])");
// If both the format and key usage are bogus, should complain about the
// format first.
shouldRejectPromiseWithNull("crypto.subtle.importKey('invalid format', data, algorithm, extractable, ['SIGN'])");
// Undefined key usage.
// FIXME: http://crbug.com/262383
//shouldThrow("crypto.subtle.importKey(keyFormat, data, algorithm, extractable, undefined)");
// Invalid data
shouldThrow("crypto.subtle.importKey(keyFormat, [], algorithm, extractable, keyUsages)");
shouldThrow("crypto.subtle.importKey(keyFormat, null, algorithm, extractable, keyUsages)");
// Invalid algorithm
shouldThrow("crypto.subtle.importKey(keyFormat, data, null, extractable, keyUsages)");
// Missing hash parameter for HMAC.
shouldRejectPromiseWithNull("crypto.subtle.importKey(keyFormat, data, {name: 'hmac'}, extractable, keyUsages)");
// SHA-1 doesn't support the importKey operation.
shouldRejectPromiseWithNull("crypto.subtle.importKey(keyFormat, data, {name: 'sha-1'}, extractable, keyUsages)");
}).then(finishJSTest, failAndFinishJSTest);
</script>
</body>
</html>
......@@ -623,7 +623,7 @@ bool parseAlgorithmParams(const Dictionary& raw, blink::WebCryptoAlgorithmParams
context.add("AesKeyGenParams");
return parseAesKeyGenParams(raw, params, context, errorDetails);
case blink::WebCryptoAlgorithmParamsTypeHmacImportParams:
context.add("HmacImporParams");
context.add("HmacImportParams");
return parseHmacImportParams(raw, params, context, errorDetails);
case blink::WebCryptoAlgorithmParamsTypeHmacKeyGenParams:
context.add("HmacKeyGenParams");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment