OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <html> | |
3 <head> | |
4 <script src="../fast/js/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("Tests algorithm normalization."); | |
13 | |
14 jsTestIsAsync = true; | |
15 | |
16 // FIXME: Rename this to crypto-operation.html, since it tests the basic | |
17 // construction of CryptoOperations. | |
18 | |
19 // ------------------------------- | |
20 // Helpers to return a normalized algorithm identifier. | |
21 // ------------------------------- | |
22 | |
23 aesCbcKey = null; | |
24 hmacSha1Key = null; | |
25 rsaSsaKey = null; | |
26 | |
27 function normalizeDigest(algorithmIdentifier) | |
28 { | |
29 return crypto.subtle.digest(algorithmIdentifier).algorithm; | |
30 } | |
31 | |
32 function normalizeEncrypt(algorithmIdentifier, key) | |
33 { | |
34 return crypto.subtle.encrypt(algorithmIdentifier, key).algorithm; | |
35 } | |
36 | |
37 function normalizeSign(algorithmIdentifier, key) | |
38 { | |
39 return crypto.subtle.sign(algorithmIdentifier, key).algorithm; | |
40 } | |
41 | |
42 function runTests() | |
43 { | |
44 // ------------------------------- | |
45 // Case insensitivity of "name" | |
46 // ------------------------------- | |
47 algorithm = normalizeDigest({name: "SHA-1"}); | |
48 shouldBe("algorithm.name", "'SHA-1'"); | |
49 algorithm = normalizeDigest({name: "sHa-256"}); | |
50 shouldBe("algorithm.name", "'SHA-256'"); | |
51 | |
52 // ------------------------------- | |
53 // Failures if "name" is invalid | |
54 // ------------------------------- | |
55 shouldThrow("normalizeDigest({})"); | |
56 shouldThrow("normalizeDigest({name: null})"); | |
57 shouldThrow("normalizeDigest({name: -1})"); | |
58 shouldThrow("normalizeDigest({name: ''})"); | |
59 shouldThrow("normalizeDigest({name: 'nosuchalgorithm'})"); | |
60 shouldThrow("normalizeDigest({name: '\\u0189'})"); | |
61 | |
62 // ------------------------------- | |
63 // Failures if the algorithm identifier is not an object | |
64 // ------------------------------- | |
65 shouldThrow("normalizeDigest(null)"); | |
66 shouldThrow("normalizeDigest(0)"); | |
67 shouldThrow("normalizeDigest(undefined)"); | |
68 shouldThrow("normalizeDigest('')"); | |
69 | |
70 // ------------------------------- | |
71 // Skip unrecognized parameters. | |
72 // ------------------------------- | |
73 algorithm = normalizeDigest({name: "sHa-1", noSuchParam: 3}); | |
74 shouldBeUndefined("algorithm.noSuchParam"); | |
75 | |
76 // ------------------------------- | |
77 // Normalized algorithm COPIES all data | |
78 // ------------------------------- | |
79 originalIv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1
4, 15]); | |
80 algorithm = normalizeEncrypt({ name: "aes-cbc", iv: originalIv }, aesCbcKey)
; | |
81 | |
82 // Make sure it constructed the normalized result. | |
83 shouldBe("algorithm.name", "'AES-CBC'"); | |
84 shouldBe("algorithm.iv.length", "16"); | |
85 shouldBe("algorithm.iv[3]", "3"); | |
86 | |
87 // Mutate the original (un-normalized) algorithm. Verify that this doesn't a
ffect the normalized output. | |
88 originalIv[3] = 0; | |
89 shouldBe("algorithm.iv[3]", "3"); | |
90 | |
91 // ------------------------------- | |
92 // AES-CBC normalization failures | |
93 // ------------------------------- | |
94 | |
95 // The "iv" MUST be 16 bytes long. | |
96 rawAlgorithm = { | |
97 name: "AES-CBC", | |
98 iv: new Uint8Array([1, 2, 3]) | |
99 }; | |
100 shouldThrow("normalizeEncrypt(rawAlgorithm, aesCbcKey)"); | |
101 | |
102 // ------------------------------- | |
103 // Normalize a normalized algorithm (SHA-384) | |
104 // ------------------------------- | |
105 algorithm = normalizeDigest({name: "sHa-384"}); | |
106 shouldBe("algorithm.name", "'SHA-384'"); | |
107 algorithm = normalizeDigest(algorithm); | |
108 shouldBe("algorithm.name", "'SHA-384'"); | |
109 | |
110 // ------------------------------- | |
111 // Normalize a normalized algorithm (AES-CBC, encrypt) | |
112 // ------------------------------- | |
113 algorithm = normalizeEncrypt({ name: "aEs-cbc", iv: originalIv }, aesCbcKey)
; | |
114 // Make sure it constructed the normalized result. | |
115 shouldBe("algorithm.name", "'AES-CBC'"); | |
116 shouldBe("algorithm.iv.length", "16"); | |
117 shouldBe("algorithm.iv[1]", "1"); | |
118 algorithm = normalizeEncrypt(algorithm, aesCbcKey); | |
119 shouldBe("algorithm.name", "'AES-CBC'"); | |
120 shouldBe("algorithm.iv.length", "16"); | |
121 shouldBe("algorithm.iv[1]", "1"); | |
122 | |
123 // ------------------------------- | |
124 // Unsupported operation on algorithm | |
125 // ------------------------------- | |
126 shouldThrow("normalizeEncrypt({ name: 'SHA-1' }, aesCbcKey)"); | |
127 shouldThrow("normalizeDigest({ name: 'AES-CBC', iv: originalIv })"); | |
128 | |
129 // ------------------------------- | |
130 // Normalize HMAC | |
131 // ------------------------------- | |
132 shouldThrow("normalizeSign({name: 'hmac'}, hmacSha1Key)"); // Missing "hash" | |
133 shouldThrow("normalizeSign({name: 'hmac', hash: 'foo'}, hmacSha1Key)"); // N
ot a valid "hash" | |
134 shouldThrow("normalizeSign({name: 'hmac', hash: { name: 'AES-CBC', iv: origi
nalIv }}, hmacSha1Key)"); // Not a valid "hash" | |
135 | |
136 validHmacSha1 = {name: 'hmac', hash: {name: 'Sha-1'}}; | |
137 algorithm = normalizeSign(validHmacSha1, hmacSha1Key); | |
138 shouldBe("algorithm.name", "'HMAC'"); | |
139 shouldBe("algorithm.hash.name", "'SHA-1'"); | |
140 | |
141 shouldThrow("normalizeEncrypt(validHmacSha1, hmacSha1Key)"); // Not defined
for encrypt() | |
142 | |
143 // ------------------------------- | |
144 // Normalize RSASSA-PKCS1-v1_5 | |
145 // ------------------------------- | |
146 shouldThrow("normalizeSign({name: 'RSASSA-PKCS1-v1_5'}, rsaSsaKey)"); // Mis
sing "hash" | |
147 shouldThrow("normalizeSign({name: 'RSASSA-PKCS1-v1_5', hash: 'foo'}, rsaSsaK
ey)"); // Not a valid "hash" | |
148 shouldThrow("normalizeSign({name: 'RSASSA-PKCS1-v1_5', hash: { name: 'AES-CB
C', iv: originalIv }}, rsaSsaKey)"); // Not a valid "hash" | |
149 | |
150 validRsaSsa = {name: 'RsaSsa-PKCS1-v1_5', hash: {name: 'Sha-256'}}; | |
151 algorithm = normalizeSign(validRsaSsa, rsaSsaKey); | |
152 shouldBe("algorithm.name", "'RSASSA-PKCS1-v1_5'"); | |
153 shouldBe("algorithm.hash.name", "'SHA-256'"); | |
154 | |
155 shouldThrow("normalizeEncrypt(validRsaSsa, rsaSsaKey)"); // Not defined for
encrypt() | |
156 | |
157 // FIXME: Test the normalization of RsaSsaKeyGen parameters. | |
158 | |
159 // ------------------------------- | |
160 // Try using a key for an unsupported operation. | |
161 // ------------------------------- | |
162 algorithmIdentifier = { name: "aes-cbc", iv: originalIv }; | |
163 shouldThrow("crypto.subtle.encrypt(algorithmIdentifier, aesCbcKeyNoEncrypt)"
); | |
164 | |
165 // ------------------------------- | |
166 // Try using an HMAC-SHA1 key for encrypting AES-CBC | |
167 // ------------------------------- | |
168 algorithmIdentifier = { name: "aes-cbc", iv: originalIv }; | |
169 shouldThrow("crypto.subtle.encrypt(algorithmIdentifier, hmacSha1Key)"); | |
170 | |
171 // ------------------------------- | |
172 // Try using an HMAC-SHA1 key for signing HMAC-SHA256 | |
173 // ------------------------------- | |
174 algorithmIdentifier = {name: 'hmac', hash: {name: 'sha-256'}}; | |
175 shouldThrow("crypto.subtle.sign(algorithmIdentifier, hmacSha1Key)"); | |
176 } | |
177 | |
178 function importAesCbcKey(keyUsages) | |
179 { | |
180 var keyFormat = "spki"; | |
181 var data = new Uint8Array([]); | |
182 var algorithm = {name: "aes-cbc"}; | |
183 var extractable = false; | |
184 | |
185 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | |
186 } | |
187 | |
188 function importRsaSsaKey() | |
189 { | |
190 var keyFormat = "spki"; | |
191 var data = new Uint8Array([]); | |
192 var algorithm = {name: "RSASSA-PKCS1-v1_5"}; | |
193 var extractable = false; | |
194 var keyUsages = ["encrypt", "decrypt", "verify", "sign"]; | |
195 | |
196 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); | |
197 } | |
198 | |
199 function failedKeyImport(value) | |
200 { | |
201 debug("Failed importing key: " + value); | |
202 finishJSTest(); | |
203 } | |
204 | |
205 // Import two keys before starting the tests: one for AES-CBC, and one for | |
206 // HMAC SHA1. | |
207 Promise.every(importAesCbcKey(['encrypt', 'decrypt', 'sign', 'verify']), importA
esCbcKey(['decrypt', 'sign', 'verify']), importHmacSha1Key(), importRsaSsaKey())
.then(function(keys) | |
208 { | |
209 aesCbcKey = keys[0]; | |
210 aesCbcKeyNoEncrypt = keys[1]; | |
211 hmacSha1Key = keys[2]; | |
212 rsaSsaKey = keys[3]; | |
213 | |
214 runTests(); | |
215 finishJSTest(); | |
216 | |
217 }, failedKeyImport); | |
218 | |
219 </script> | |
220 | |
221 <script src="../fast/js/resources/js-test-post.js"></script> | |
222 </body> | |
223 </html> | |
OLD | NEW |