blob: cb5c542dd75efcf48ee75e6c106326430007cbfb [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="../resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Test generating, importing and exporting RSA keys for RSA-OAEP. Test that they can't be used with another algorithm.");
jsTestIsAsync = true;
var algorithmKeyGen = {
name: "RSA-OAEP",
// RsaKeyGenParams
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // Equivalent to 65537
};
var extractable = true;
debug("Generating a key pair...");
crypto.webkitSubtle.generateKey(algorithmKeyGen, extractable, ["encrypt", "decrypt", "wrapKey", "unwrapKey"]).then(function(result) {
keyPair = result;
shouldBe("keyPair.toString()", "'[object Object]'");
shouldBe("keyPair.publicKey.type", "'public'");
shouldBe("keyPair.publicKey.algorithm.name", "'RSA-OAEP'");
shouldBe("keyPair.publicKey.algorithm.modulusLength", "2048");
shouldBe("bytesToHexString(keyPair.publicKey.algorithm.publicExponent)", "'010001'");
shouldBeUndefined("keyPair.publicKey.algorithm.hash");
shouldBe("keyPair.privateKey.type", "'private'");
shouldBe("keyPair.privateKey.algorithm.name", "'RSA-OAEP'");
shouldBe("keyPair.privateKey.algorithm.modulusLength", "2048");
shouldBe("bytesToHexString(keyPair.privateKey.algorithm.publicExponent)", "'010001'");
shouldBeUndefined("keyPair.privateKey.algorithm.hash");
debug("\nTesting that the keys can't be used with different algorithms...");
iv = hexStringToUint8Array("000102030405060708090a0b0c0d0e0f");
shouldThrow('crypto.webkitSubtle.encrypt({name: "aes-cbc", iv: iv}, keyPair.privateKey, hexStringToUint8Array("00"))');
shouldThrow('crypto.webkitSubtle.encrypt({name: "aes-cbc", iv: iv}, keyPair.publicKey, hexStringToUint8Array("00"))');
debug("\nTrying to export keys to raw...");
return crypto.webkitSubtle.exportKey('raw', keyPair.publicKey);
}).then(undefined, function(result) {
testPassed("Promise rejected for exporting public key");
return crypto.webkitSubtle.exportKey('raw', keyPair.privateKey);
}).then(undefined, function(result) {
testPassed("Promise rejected for exporting private key");
debug("\nExporting public key to JWK...");
return crypto.webkitSubtle.exportKey("jwk", keyPair.publicKey);
}).then(function(result) {
jwkPublicKeyArray = result;
jwkPublicKey = JSON.parse(bytesToASCIIString(jwkPublicKeyArray));
shouldBe("jwkPublicKey.alg", "'RSA-OAEP'");
shouldBe("jwkPublicKey.ext", "true");
shouldBe("jwkPublicKey.key_ops", "['encrypt', 'decrypt', 'wrapKey', 'unwrapKey']");
shouldBe("jwkPublicKey.use", "undefined");
shouldBe("jwkPublicKey.kty", "'RSA'");
shouldBe("bytesToHexString(Base64URL.parse(jwkPublicKey.e))", "'010001'");
debug("\nImporting it back...");
return crypto.webkitSubtle.importKey("jwk", jwkPublicKeyArray, null, extractable, ["encrypt", "decrypt", "wrapKey", "unwrapKey"]);
}).then(function(result) {
exportedPublicKey = result;
shouldBe("exportedPublicKey.type", "'public'");
shouldBe("exportedPublicKey.algorithm.name", "'RSA-OAEP'");
shouldBe("exportedPublicKey.algorithm.modulusLength", "2048");
shouldBe("bytesToHexString(exportedPublicKey.algorithm.publicExponent)", "'010001'");
shouldBe("exportedPublicKey.algorithm.hash.name", "'SHA-1'");
shouldBe("exportedPublicKey.extractable", "true");
shouldBe("exportedPublicKey.usages", "['decrypt','encrypt','unwrapKey','wrapKey']");
finishJSTest();
});
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>