4 <script src="../resources/js-test.js"></script>
5 <script src="resources/common.js"></script>
8 <p id="description"></p>
9 <div id="console"></div>
12 description("Test importing keys with various uses from JWK.");
16 var extractable = true;
22 "k": "jnOw99oOZFLIEPMrgJB55Q"
29 "k": "ahjkn-_387fgnsibf23qsvahjkn-_387fgnsibf23qs"
32 function testWithAESCBC(expectedUsages, jwkUsages, importUsages)
34 if (jwkUsages.key_ops) {
35 aesKeyAsJSON.key_ops = jwkUsages.key_ops;
36 delete aesKeyAsJSON.use;
38 delete aesKeyAsJSON.key_ops;
39 aesKeyAsJSON.use = jwkUsages.use;
42 return crypto.subtle.importKey("jwk", aesKeyAsJSON, {name: "AES-CBC"}, extractable, importUsages).then(function(result) {
44 debug(JSON.stringify(jwkUsages) + ":");
45 shouldBe("key.usages", JSON.stringify(expectedUsages));
48 debug(JSON.stringify(jwkUsages) + ":");
49 debug("Failed importing with " + importUsages + ": " + result);
53 function testWithHMAC(expectedUsages, jwkUsages, importUsages)
55 if (jwkUsages.key_ops) {
56 hmacKeyAsJSON.key_ops = jwkUsages.key_ops;
57 delete hmacKeyAsJSON.use;
59 delete hmacKeyAsJSON.key_ops;
60 hmacKeyAsJSON.use = jwkUsages.use;
63 return crypto.subtle.importKey("jwk", hmacKeyAsJSON, {name: 'hmac', hash: {name: 'sha-256'}}, extractable, importUsages).then(function(result) {
65 debug(JSON.stringify(jwkUsages) + ":");
66 shouldBe("key.usages", JSON.stringify(expectedUsages));
69 debug(JSON.stringify(jwkUsages) + ":");
70 debug("Failed importing with " + importUsages + ": " + result);
78 // Duplicates are not allowed.
79 testWithAESCBC(null, {key_ops: ["encrypt", "encrypt"]}, ["encrypt"]),
81 testWithAESCBC(["encrypt"], {key_ops: ["encrypt"]}, ["encrypt"]),
82 testWithAESCBC(null, {key_ops: ["encrypt"]}, ["decrypt"]),
84 testWithAESCBC(["decrypt"], {key_ops: ["decrypt"]}, ["decrypt"]),
85 testWithAESCBC(null, {key_ops: ["decrypt"]}, ["encrypt"]),
87 testWithAESCBC(["encrypt", "decrypt"], {key_ops: ["encrypt", "decrypt"]}, ["encrypt", "decrypt"]),
88 testWithAESCBC(["encrypt"], {key_ops: ["encrypt", "decrypt"]}, ["encrypt"]),
89 testWithAESCBC(null, {key_ops: ["encrypt", "decrypt"]}, ["unwrapKey"]),
91 testWithAESCBC(["wrapKey"], {key_ops: ["wrapKey"]}, ["wrapKey"]),
92 testWithAESCBC(null, {key_ops: ["wrapKey"]}, ["unwrapKey"]),
94 testWithAESCBC(["unwrapKey"], {key_ops: ["unwrapKey"]}, ["unwrapKey"]),
95 testWithAESCBC(["wrapKey", "unwrapKey"], {key_ops: ["wrapKey", "unwrapKey"]}, ["unwrapKey", "wrapKey"]),
96 testWithAESCBC(["encrypt", "decrypt", "wrapKey"], {key_ops: ["encrypt", "decrypt", "wrapKey"]}, ["decrypt", "encrypt", "wrapKey"]),
98 testWithAESCBC(["encrypt", "decrypt", "wrapKey", "unwrapKey"], {use: "enc"}, ["decrypt", "encrypt", "unwrapKey", "wrapKey"]),
99 testWithAESCBC(["encrypt", "decrypt", "unwrapKey"], {use: "enc"}, ["decrypt", "encrypt", "unwrapKey"]),
100 testWithAESCBC(["encrypt", "decrypt", "unwrapKey"], {use: "enc"}, ["decrypt", "encrypt", "unwrapKey"]),
102 testWithHMAC(["sign"], {key_ops: ["sign"]}, ["sign"]),
103 testWithHMAC(null, {key_ops: ["sign"]}, ["verify"]),
105 testWithHMAC(["verify"], {key_ops: ["verify"]}, ["verify"]),
106 testWithHMAC(null, {key_ops: ["verify"]}, ["sign"]),
108 testWithHMAC(["sign", "verify"], {use: "sig"}, ["sign", "verify"]),
109 testWithHMAC(["sign"], {use: "sig"}, ["sign"]),
111 // Unknown key_ops strings are ignored.
112 testWithAESCBC(["decrypt"], {key_ops: ["'encrypt'", "decrypt"]}, ["decrypt"]),
113 testWithAESCBC(["decrypt"], {key_ops: ["encrypt ", "foo", "decrypt"]}, ["decrypt"]),
114 testWithAESCBC(["decrypt"], {key_ops: ["Encrypt", "decrypt"]}, ["decrypt"]),
115 testWithAESCBC(null, {key_ops: ["'encrypt'", "decrypt"]}, ["encrypt"]),
116 testWithAESCBC(null, {key_ops: ["encrypt "]}, ["encrypt"]),
117 testWithAESCBC(null, {key_ops: ["Encrypt"]}, ["encrypt"]),
119 ]).then(finishJSTest, failAndFinishJSTest);