6d07801138bd3683d428bf223d954ed3cf461019
[WebKit-https.git] / LayoutTests / crypto / subtle / rsaes-pkcs1-v1_5-wrap-unwrap-aes.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../resources/js-test-pre.js"></script>
5 <script src="resources/common.js"></script>
6 </head>
7 <body>
8 <p id="description"></p>
9 <div id="console"></div>
10
11 <script>
12 description("Test wrapping and unwrapping an AES key with RSAES-PKCS1-v1_5.");
13
14 jsTestIsAsync = true;
15
16 var publicKeyJSON = {
17     kty: "RSA",
18     alg: "RSA1_5",
19     n: Base64URL.stringify(hexStringToUint8Array("dcfa10ffa74665aeef870974ea99b2ce54547c67f42aaa6dd01a2ed31fd2c242af5d960b1f896efba3543d6554b7b12687a5c688568f32e026c532d25993b97a7c2842ec2b8e1235eee2414d25806c6fbae438954eba9d2755dffeeb1b47700957815a8a233f97b1a2c714b3e2be2e42d8be30b1961582ea9948910e0c797c50fc4bb455f0fc45e5e34e6396ac5b2d46239365c7f3daaf0909400d61cf9e0ca8083eaf335a6fceb6863c1cc0cf5a171aff35d97ecb60ef251c7ec2c8a588361dc41266a4b7ed38b026ce0d53786449dbb11a06ea33ccf1eca575201ed1aa473ed1187ec1d8a744ea345bed7ea00ee4e81bba4648601dd537dc91015d31f0c2c1")),
20     e: Base64URL.stringify(hexStringToUint8Array("010001"))
21 };
22
23 var privateKeyJSON = {
24     kty: "RSA",
25     alg: "RSA1_5",
26     n: Base64URL.stringify(hexStringToUint8Array("dcfa10ffa74665aeef870974ea99b2ce54547c67f42aaa6dd01a2ed31fd2c242af5d960b1f896efba3543d6554b7b12687a5c688568f32e026c532d25993b97a7c2842ec2b8e1235eee2414d25806c6fbae438954eba9d2755dffeeb1b47700957815a8a233f97b1a2c714b3e2be2e42d8be30b1961582ea9948910e0c797c50fc4bb455f0fc45e5e34e6396ac5b2d46239365c7f3daaf0909400d61cf9e0ca8083eaf335a6fceb6863c1cc0cf5a171aff35d97ecb60ef251c7ec2c8a588361dc41266a4b7ed38b026ce0d53786449dbb11a06ea33ccf1eca575201ed1aa473ed1187ec1d8a744ea345bed7ea00ee4e81bba4648601dd537dc91015d31f0c2c1")),
27     e: Base64URL.stringify(hexStringToUint8Array("010001")),
28     d: Base64URL.stringify(hexStringToUint8Array("21950851cdf25320318b305afa0f371f07ae5a44b314ebd729f5dcb15da7fa3947acdd915daed574bd16df88bf85f61060b387172fae6e01262b3864c2d3c22f94e04a8159422b4ed279c48a4c9d767d4966071a5bbf5d043e16ff46ec1ba0716f00bbc97bff5d5693e214e99c9721f12b3ec6282ae2a485721b96ddcf7403fa037d0c57ab463c448de5cc12265add886d311ea8d8a5903fa56c5f1c9cf2eb11cb657a1a7d3e41352dc3e686898c4ce4305e8b638e1b08a2a86cc9eb9866f3499ac77b6136b81cb276d614cfeb7b6ed3f3bc775e46c00066ebeee2cff7166b57520598947ff6210320b288fb4f2c3f8fe97b279414ebf7203000a19fc0424875")),
29     p: Base64URL.stringify(hexStringToUint8Array("f123bfe53de97a569d91adcf556fa625ad30f3fd3d811f9e91e6af44b6e780cb0f327829fb21190ae2806646d728cd9b6531132b1ebfef1272993060f1ce70b124393091ee8593b727367edbba009ec5be17c4acee120c841267d47631a16c36a6d1c99973c1b0b5a835bf39feafe8f6421fd9c2a90bc27976659e67bc83124d")),
30     q: Base64URL.stringify(hexStringToUint8Array("ea9839b7e37ea89bbda27e4c93471cb4fd92189a0a96bcb4d75693f18a5c2f742af9e36fde679fbd9eae345fa269527b6965021c4bdf54d685bf08960cc976f68dca21cebf44f268a59dab8d1a25e519f5147e1f45fe287d74cf725bec1326d34212c56cf4fffa202f57b68ee8cca943f3c138c4cde33bdf2c9440df65322445")),
31     dp: Base64URL.stringify(hexStringToUint8Array("ca0c9b60b8e4a6066756c65d2088419df6253b7b688a85f4f6e964d85dad52a45262867f1e9618069fccd865e9289e46e39e2022944c5c4487d345cf252d460d977d77edfefedbcbae46a23af7fa470f077da0e50942044cb1a360497cc2760ac0f2ad4a2fcd0e84d7a1d94dfdd2658fd9ce18475c1fa75ee0cebad0cf0ac04d")),
32     dq: Base64URL.stringify(hexStringToUint8Array("528171233c4e4a6c63b86764f51338846afddbcb2958344c01c4004a1dd828145a1d02a1507def4f58247a64fc10c0a288c1ae895721d78b8f044db7c00d86da55a9b654292ecd768270be69e4bd5922d4effd1f70955f9627e3e19b749e93b40ef3dd1d61d93915e2b09d930b4b1768bfacc0136f39b0cfdfb4d050011e2e65")),
33     qi: Base64URL.stringify(hexStringToUint8Array("df2eb2322cc2daabf4d1465508f41521cda7ceff23ebe61d00d441ee728dda5d16c7bf920cd95f34beb4fe32ee817ef3362e0bcd1d1245f7b07793eaa190dc5a37fdaf4c68e2ca13972d7f5148b796b6fb6d7adda07bd2cd13be98cebed1edc6ca412e395350c59a1d842bc4aa2f3c0b243fde7dfd95356f2439251a1172c45e"))
34 };
35 var wrapAlgorithm = {name: "RSAES-PKCS1-v1_5"};
36 var extractable = true;
37 var nonExtractable = false;
38
39 debug("Importing RSA keys...");
40 crypto.subtle.importKey("jwk", asciiToUint8Array(JSON.stringify(publicKeyJSON)), null, extractable, ["wrapKey", "unwrapKey"]).then(function(result) {
41     publicKey = result;
42     return crypto.subtle.importKey("jwk", asciiToUint8Array(JSON.stringify(privateKeyJSON)), null, extractable, ["wrapKey", "unwrapKey"]);
43 }).then(function(result) {
44     privateKey = result;
45
46     debug("Importing an AES key...");
47     aesKeyData = hexStringToUint8Array("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
48     return crypto.subtle.importKey("raw", aesKeyData, "aes-cbc", extractable, ["encrypt", "decrypt", "wrapKey", "unwrapKey"])
49 }).then(function(result) {
50     aesKey = result;
51     debug("Wrapping it...");
52
53     return crypto.subtle.wrapKey("raw", aesKey, publicKey, wrapAlgorithm);
54 }).then(function(result) {
55     wrappedKey = result;
56     shouldBe("wrappedKey.toString()", "'[object ArrayBuffer]'");
57
58     debug("Unwrapping it...");
59     return crypto.subtle.unwrapKey("raw", wrappedKey, privateKey, wrapAlgorithm, "aes-cbc", extractable, ["encrypt", "decrypt"]);
60 }).then(function(result) {
61     unwrappedKey = result;
62     shouldBe("unwrappedKey.toString()", "'[object Key]'");
63     shouldBe("unwrappedKey.type", "'secret'");
64     shouldBe("unwrappedKey.extractable", "true");
65     shouldBe("unwrappedKey.algorithm.name", "'AES-CBC'");
66     shouldBe("unwrappedKey.algorithm.length", "256");
67     shouldBe("unwrappedKey.usages", "['encrypt', 'decrypt']");
68
69     debug("Exporting it...");
70     return crypto.subtle.exportKey("raw", unwrappedKey);
71 }).then(function(result) {
72     unwrappedKeyData = result;
73     shouldBe("bytesToHexString(unwrappedKeyData)", "bytesToHexString(aesKeyData)");
74
75     debug("\nWrapping the same key as JWK...");
76     return crypto.subtle.wrapKey("jwk", aesKey, publicKey, wrapAlgorithm);
77 }).then(function(result) {
78     wrappedKey = result;
79     shouldBe("wrappedKey.toString()", "'[object ArrayBuffer]'");
80
81     debug("Unwrapping it...");
82     return crypto.subtle.unwrapKey("jwk", wrappedKey, privateKey, wrapAlgorithm, "aes-cbc", extractable, ["encrypt", "decrypt"]);
83 }).then(function(result) {
84     unwrappedKey = result;
85     shouldBe("unwrappedKey.toString()", "'[object Key]'");
86     shouldBe("unwrappedKey.type", "'secret'");
87     shouldBe("unwrappedKey.extractable", "true");
88     shouldBe("unwrappedKey.algorithm.name", "'AES-CBC'");
89     shouldBe("unwrappedKey.algorithm.length", "256");
90     shouldBe("unwrappedKey.usages", "['encrypt', 'decrypt']");
91
92     debug("Exporting it...");
93     return crypto.subtle.exportKey("raw", unwrappedKey);
94 }).then(function(result) {
95     unwrappedKeyData = result;
96     shouldBe("bytesToHexString(unwrappedKeyData)", "bytesToHexString(aesKeyData)");
97     finishJSTest();
98 });
99 </script>
100
101 <script src="../../resources/js-test-post.js"></script>
102 </body>
103 </html>