OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
4 <script src="../resources/js-test.js"></script> | 4 <script src="../resources/js-test.js"></script> |
5 <script src="resources/common.js"></script> | 5 <script src="resources/common.js"></script> |
6 </head> | 6 </head> |
7 <body> | 7 <body> |
8 <p id="description"></p> | 8 <p id="description"></p> |
9 <div id="console"></div> | 9 <div id="console"></div> |
10 | 10 |
11 <script> | 11 <script> |
12 description("Tests cypto.subtle.importKey."); | 12 description("Tests cypto.subtle.importKey."); |
13 | 13 |
14 jsTestIsAsync = true; | 14 jsTestIsAsync = true; |
15 | 15 |
16 aesCbc = {name: 'aes-cbc'}; | 16 aesCbc = {name: 'aes-cbc'}; |
17 | 17 |
18 Promise.resolve(null).then(function() { | 18 Promise.resolve(null).then(function() { |
19 keyFormat = "raw"; | 19 keyFormat = "raw"; |
20 data = asciiToUint8Array("raw bytes for key"); | 20 data = asciiToUint8Array("raw bytes for key"); |
21 algorithm = { name: 'hmac', hash: { name: 'sha-256' } }; | 21 algorithm = { name: 'hmac', hash: { name: 'sha-256' } }; |
22 extractable = true; | 22 extractable = true; |
23 // Note there are duplicates | 23 // Note there are duplicates |
24 keyUsages = ['encrypt', 'encrypt', 'encrypt', 'sign']; | 24 keyUsages = ['encrypt', 'encrypt', 'encrypt', 'sign']; |
25 | 25 |
26 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | 26 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
27 }).then(function(result) { | 27 }).then(function(result) { |
28 key = result; | 28 key = result; |
29 shouldBe("key.type", "'secret'") | 29 shouldBe("key.type", "'secret'"); |
30 shouldBe("key.extractable", "true") | 30 shouldBe("key.extractable", "true"); |
31 shouldBe("key.algorithm.name", "'HMAC'") | 31 shouldBe("key.algorithm.name", "'HMAC'"); |
32 shouldBe("key.usages.join(',')", "'encrypt,sign'") | 32 shouldBe("key.algorithm.hash.name", "'SHA-256'"); |
| 33 shouldBe("key.usages.join(',')", "'encrypt,sign'"); |
33 | 34 |
34 // Same test as above, but with an keyUsages, and AES-CBC. | 35 // Same test as above, but with an keyUsages, and AES-CBC. |
35 keyFormat = "raw"; | 36 keyFormat = "raw"; |
36 data = asciiToUint8Array("16 bytes of key!"); | 37 data = asciiToUint8Array("16 bytes of key!"); |
37 algorithm = aesCbc; | 38 algorithm = aesCbc; |
38 extractable = true; | 39 extractable = true; |
39 keyUsages = []; | 40 keyUsages = []; |
40 | 41 |
41 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | 42 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
42 }).then(function(result) { | 43 }).then(function(result) { |
43 key = result; | 44 key = result; |
44 shouldBe("key.type", "'secret'") | 45 shouldBe("key.type", "'secret'"); |
45 shouldBe("key.extractable", "true") | 46 shouldBe("key.extractable", "true"); |
46 shouldBe("key.algorithm.name", "'AES-CBC'") | 47 shouldBe("key.algorithm.name", "'AES-CBC'"); |
47 shouldBe("key.usages.join(',')", "''") | 48 shouldBe("key.algorithm.length", "128"); |
| 49 shouldBe("key.usages.join(',')", "''"); |
48 | 50 |
49 // Same test as above, but with extractable = false. | 51 // Same test as above, but with extractable = false. |
50 keyFormat = "raw"; | 52 keyFormat = "raw"; |
51 data = asciiToUint8Array("16 bytes of key!"); | 53 data = asciiToUint8Array("16 bytes of key!"); |
52 algorithm = aesCbc; | 54 algorithm = aesCbc; |
53 extractable = false; | 55 extractable = false; |
54 keyUsages = []; | 56 keyUsages = []; |
55 | 57 |
56 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | 58 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
57 }).then(function(result) { | 59 }).then(function(result) { |
58 key = result; | 60 key = result; |
59 shouldBe("key.type", "'secret'") | 61 shouldBe("key.type", "'secret'"); |
60 shouldBe("key.extractable", "false") | 62 shouldBe("key.extractable", "false"); |
61 shouldBe("key.algorithm.name", "'AES-CBC'") | 63 shouldBe("key.algorithm.name", "'AES-CBC'"); |
62 shouldBe("key.usages.join(',')", "''") | 64 shouldBe("key.algorithm.length", "128"); |
| 65 shouldBe("key.usages.join(',')", "''"); |
63 | 66 |
64 // Same test as above, but with keyFormat = spki | 67 // Same test as above, but with keyFormat = spki |
65 keyFormat = "spki"; | 68 keyFormat = "spki"; |
66 data = asciiToUint8Array("16 bytes of key!"); | 69 data = asciiToUint8Array("16 bytes of key!"); |
67 algorithm = aesCbc; | 70 algorithm = aesCbc; |
68 extractable = false; | 71 extractable = false; |
69 keyUsages = []; | 72 keyUsages = []; |
70 | 73 |
71 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | 74 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
72 }).then(undefined, function(result) { | 75 }).then(undefined, function(result) { |
73 // TODO(eroman): Only "raw" key format is supported at the moment. | |
74 debug("rejected with " + result); | 76 debug("rejected with " + result); |
75 | 77 |
| 78 // Import an spki formatted public key |
| 79 keyFormat = "spki"; |
| 80 data = hexStringToUint8Array(kPublicKeySpkiDerHex); |
| 81 algorithm = {name: 'RsasSA-pKCS1-v1_5', hash: {name: 'sha-1'}}; |
| 82 extractable = false; |
| 83 keyUsages = []; |
| 84 |
| 85 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
| 86 }).then(function(result) { |
| 87 key = result; |
| 88 shouldBe("key.type", "'public'"); |
| 89 shouldBe("key.extractable", "true"); |
| 90 shouldBe("key.algorithm.name", "'RSASSA-PKCS1-v1_5'"); |
| 91 shouldBe("key.algorithm.hash.name", '"SHA-1"'); |
| 92 shouldBe("key.algorithm.modulusLength", '1024'); |
| 93 bytesShouldMatchHexString("key.algorithm.publicExponent", "010001", key.algo
rithm.publicExponent); |
| 94 shouldBe("key.usages.join(',')", "''"); |
| 95 |
| 96 // Import a pkcs8 formatted private key |
| 97 keyFormat = "pkcs8"; |
| 98 data = hexStringToUint8Array(kPrivateKeyPkcs8DerHex); |
| 99 algorithm = {name: 'RsasSA-pKCS1-v1_5', hash: {name: 'sha-1'}}; |
| 100 extractable = false; |
| 101 keyUsages = []; |
| 102 |
| 103 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
| 104 }).then(function(result) { |
| 105 key = result; |
| 106 shouldBe("key.type", "'private'"); |
| 107 shouldBe("key.extractable", "false"); |
| 108 shouldBe("key.algorithm.name", "'RSASSA-PKCS1-v1_5'"); |
| 109 shouldBe("key.algorithm.hash.name", '"SHA-1"'); |
| 110 shouldBe("key.algorithm.modulusLength", '1024'); |
| 111 bytesShouldMatchHexString("key.algorithm.publicExponent", "010001", key.algo
rithm.publicExponent); |
| 112 shouldBe("key.usages.join(',')", "''") |
| 113 |
76 // Import a "raw" key without specifying the algorithm. | 114 // Import a "raw" key without specifying the algorithm. |
77 keyFormat = "raw"; | 115 keyFormat = "raw"; |
78 data = asciiToUint8Array("16 bytes of key!"); | 116 data = asciiToUint8Array("16 bytes of key!"); |
79 algorithm = null; | 117 algorithm = null; |
80 extractable = false; | 118 extractable = false; |
81 keyUsages = []; | 119 keyUsages = []; |
82 | 120 |
83 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | 121 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
84 }).then(undefined, function(result) { | 122 }).then(undefined, function(result) { |
85 debug("rejected with " + result); | 123 debug("rejected with " + result); |
(...skipping 26 matching lines...) Expand all Loading... |
112 shouldRejectPromiseWithNull("crypto.subtle.importKey(keyFormat, data, {name:
'hmac'}, extractable, keyUsages)"); | 150 shouldRejectPromiseWithNull("crypto.subtle.importKey(keyFormat, data, {name:
'hmac'}, extractable, keyUsages)"); |
113 | 151 |
114 // SHA-1 doesn't support the importKey operation. | 152 // SHA-1 doesn't support the importKey operation. |
115 shouldRejectPromiseWithNull("crypto.subtle.importKey(keyFormat, data, {name:
'sha-1'}, extractable, keyUsages)"); | 153 shouldRejectPromiseWithNull("crypto.subtle.importKey(keyFormat, data, {name:
'sha-1'}, extractable, keyUsages)"); |
116 }).then(finishJSTest, failAndFinishJSTest); | 154 }).then(finishJSTest, failAndFinishJSTest); |
117 | 155 |
118 </script> | 156 </script> |
119 | 157 |
120 </body> | 158 </body> |
121 </html> | 159 </html> |
OLD | NEW |