OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <html> | |
3 <head> | |
4 <script src="../resources/js-test.js"></script> | |
5 <script src="resources/common.js"></script> | |
6 <script src="resources/keys.js"></script> | |
7 </head> | |
8 <body> | |
9 <p id="description"></p> | |
10 <div id="console"></div> | |
11 | |
12 <script> | |
13 description("Tests cypto.subtle.importKey."); | |
14 | |
15 jsTestIsAsync = true; | |
16 | |
17 aesCbc = {name: 'aes-cbc'}; | |
18 | |
19 Promise.resolve(null).then(function() { | |
20 keyFormat = "raw"; | |
21 data = asciiToUint8Array("raw bytes for key"); | |
22 algorithm = { name: 'hmac', hash: { name: 'sha-256' } }; | |
23 extractable = true; | |
24 // Note there are duplicates | |
25 keyUsages = ['encrypt', 'encrypt', 'encrypt', 'sign']; | |
26 | |
27 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | |
28 }).then(function(result) { | |
29 key = result; | |
30 shouldBe("key.type", "'secret'"); | |
31 shouldBe("key.extractable", "true"); | |
32 shouldBe("key.algorithm.name", "'HMAC'"); | |
33 shouldBe("key.algorithm.hash.name", "'SHA-256'"); | |
34 shouldBe("key.usages.join(',')", "'encrypt,sign'"); | |
35 | |
36 // Same test as above, but with an keyUsages, and AES-CBC. | |
37 keyFormat = "raw"; | |
38 data = asciiToUint8Array("16 bytes of key!"); | |
39 algorithm = aesCbc; | |
40 extractable = true; | |
41 keyUsages = []; | |
42 | |
43 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | |
44 }).then(function(result) { | |
45 key = result; | |
46 shouldBe("key.type", "'secret'"); | |
47 shouldBe("key.extractable", "true"); | |
48 shouldBe("key.algorithm.name", "'AES-CBC'"); | |
49 shouldBe("key.algorithm.length", "128"); | |
50 shouldBe("key.usages.join(',')", "''"); | |
51 | |
52 // Same test as above, but with extractable = false. | |
53 keyFormat = "raw"; | |
54 data = asciiToUint8Array("16 bytes of key!"); | |
55 algorithm = aesCbc; | |
56 extractable = false; | |
57 keyUsages = []; | |
58 | |
59 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | |
60 }).then(function(result) { | |
61 key = result; | |
62 shouldBe("key.type", "'secret'"); | |
63 shouldBe("key.extractable", "false"); | |
64 shouldBe("key.algorithm.name", "'AES-CBC'"); | |
65 shouldBe("key.algorithm.length", "128"); | |
66 shouldBe("key.usages.join(',')", "''"); | |
67 | |
68 // Same test as above, but with keyFormat = spki | |
69 keyFormat = "spki"; | |
70 data = asciiToUint8Array("16 bytes of key!"); | |
71 algorithm = aesCbc; | |
72 extractable = false; | |
73 keyUsages = []; | |
74 | |
75 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | |
76 }).then(undefined, function(result) { | |
77 debug("rejected with " + result); | |
78 | |
79 // Import an spki formatted public key | |
80 keyFormat = "spki"; | |
81 data = hexStringToUint8Array(kKeyData.rsa1.spki); | |
82 algorithm = {name: 'RsasSA-pKCS1-v1_5', hash: {name: 'sha-1'}}; | |
83 extractable = false; | |
84 keyUsages = []; | |
85 | |
86 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | |
87 }).then(function(result) { | |
88 key = result; | |
89 shouldBe("key.type", "'public'"); | |
90 // FIXME: Enable this once updated. | |
91 //shouldBe("key.extractable", "true"); | |
92 shouldBe("key.algorithm.name", "'RSASSA-PKCS1-v1_5'"); | |
93 shouldBe("key.algorithm.hash.name", '"SHA-1"'); | |
94 shouldEvaluateAs("key.algorithm.modulusLength", kKeyData.rsa1.modulusLengthB
its); | |
95 bytesShouldMatchHexString("key.algorithm.publicExponent", kKeyData.rsa1.publ
icExponent, key.algorithm.publicExponent); | |
96 shouldBe("key.usages.join(',')", "''"); | |
97 | |
98 // Import a pkcs8 formatted private key | |
99 keyFormat = "pkcs8"; | |
100 data = hexStringToUint8Array(kKeyData.rsa1.pkcs8); | |
101 algorithm = {name: 'RsasSA-pKCS1-v1_5', hash: {name: 'sha-1'}}; | |
102 extractable = false; | |
103 keyUsages = []; | |
104 | |
105 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | |
106 }).then(function(result) { | |
107 key = result; | |
108 shouldBe("key.type", "'private'"); | |
109 shouldBe("key.extractable", "false"); | |
110 shouldBe("key.algorithm.name", "'RSASSA-PKCS1-v1_5'"); | |
111 shouldBe("key.algorithm.hash.name", '"SHA-1"'); | |
112 shouldEvaluateAs("key.algorithm.modulusLength", kKeyData.rsa1.modulusLengthB
its); | |
113 bytesShouldMatchHexString("key.algorithm.publicExponent", kKeyData.rsa1.publ
icExponent, key.algorithm.publicExponent); | |
114 shouldBe("key.usages.join(',')", "''") | |
115 | |
116 keyFormat = "raw"; | |
117 data = asciiToUint8Array(""); | |
118 algorithm = aesCbc; | |
119 extractable = true; | |
120 keyUsages = []; | |
121 | |
122 // Invalid format. | |
123 shouldRejectPromiseWithNull("crypto.subtle.importKey('invalid format', data,
algorithm, extractable, keyUsages)"); | |
124 | |
125 // Invalid key usage. | |
126 shouldRejectPromiseWithNull("crypto.subtle.importKey(keyFormat, data, algori
thm, extractable, ['SIGN'])"); | |
127 | |
128 // If both the format and key usage are bogus, should complain about the | |
129 // format first. | |
130 shouldRejectPromiseWithNull("crypto.subtle.importKey('invalid format', data,
algorithm, extractable, ['SIGN'])"); | |
131 | |
132 // Undefined key usage. | |
133 // FIXME: http://crbug.com/262383 | |
134 //shouldThrow("crypto.subtle.importKey(keyFormat, data, algorithm, extractab
le, undefined)"); | |
135 | |
136 // Invalid data | |
137 shouldThrow("crypto.subtle.importKey(keyFormat, [], algorithm, extractable,
keyUsages)"); | |
138 shouldThrow("crypto.subtle.importKey(keyFormat, null, algorithm, extractable
, keyUsages)"); | |
139 | |
140 // Invalid algorithm | |
141 shouldThrow("crypto.subtle.importKey(keyFormat, data, null, extractable, key
Usages)"); | |
142 | |
143 // Missing hash parameter for HMAC. | |
144 shouldRejectPromiseWithNull("crypto.subtle.importKey(keyFormat, data, {name:
'hmac'}, extractable, keyUsages)"); | |
145 | |
146 // SHA-1 doesn't support the importKey operation. | |
147 shouldRejectPromiseWithNull("crypto.subtle.importKey(keyFormat, data, {name:
'sha-1'}, extractable, keyUsages)"); | |
148 }).then(finishJSTest, failAndFinishJSTest); | |
149 | |
150 </script> | |
151 | |
152 </body> | |
153 </html> | |
OLD | NEW |