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 <script src="resources/keys.js"></script> | 6 <script src="resources/keys.js"></script> |
7 </head> | 7 </head> |
8 <body> | 8 <body> |
9 <p id="description"></p> | 9 <p id="description"></p> |
10 <div id="console"></div> | 10 <div id="console"></div> |
11 | 11 |
12 <script> | 12 <script> |
13 description("Tests structured cloning of RSA keys (without a hash)"); | 13 description("Tests structured cloning of RSA private keys (without a hash)"); |
14 | 14 |
15 jsTestIsAsync = true; | 15 jsTestIsAsync = true; |
16 | 16 |
17 // Tests the 16 permutations of keys generated by: | 17 // Tests the 16 permutations of keys generated by: |
18 // kPossibleAlgorithms x kPossibleExtractable x kPossibleKeyUsages x kPossible
KeyData | 18 // kPossibleAlgorithms x kPossibleExtractable x kPossibleKeyUsages x kPossible
KeyData |
19 // | 19 // |
20 // For practical reasons these tests are not exhaustive. | 20 // For practical reasons these tests are not exhaustive. |
21 | 21 |
22 var kPossibleAlgorithms = ['RSAES-PKCS1-v1_5']; | 22 var kPossibleAlgorithms = ['RSAES-PKCS1-v1_5']; |
23 var kPossibleExtractable = [true, false]; | 23 var kPossibleExtractable = [true, false]; |
24 var kPossibleKeyUsages = [[], ['encrypt'], ['decrypt', 'wrapKey'], ['encrypt', '
wrapKey', 'unwrapKey']]; | 24 var kPossibleKeyUsages = [[], ['encrypt'], ['decrypt', 'wrapKey'], ['encrypt', '
wrapKey', 'unwrapKey']]; |
25 | 25 |
26 var kPossibleKeyData = [ | 26 var kPossibleKeyData = [ |
27 kKeyData.rsa2, | 27 kKeyData.rsa1, |
28 kKeyData.rsa3 | 28 kKeyData.rsa4 |
29 ]; | 29 ]; |
30 | 30 |
31 function runTest(algorithmName, extractable, keyUsages, keyData) | 31 function runTest(algorithmName, extractable, keyUsages, keyData) |
32 { | 32 { |
33 var importData = hexStringToUint8Array(keyData.spki); | 33 var importData = hexStringToUint8Array(keyData.pkcs8); |
34 var importAlgorithm = { name: algorithmName }; | 34 var importAlgorithm = { name: algorithmName }; |
35 | 35 |
36 var results = {}; | 36 var results = {}; |
37 | 37 |
38 return crypto.subtle.importKey('spki', importData, importAlgorithm, extracta
ble, keyUsages).then(function(importedKey) { | 38 return crypto.subtle.importKey('pkcs8', importData, importAlgorithm, extract
able, keyUsages).then(function(importedKey) { |
39 results.importedKey = importedKey; | 39 results.importedKey = importedKey; |
40 importedKey.extraProperty = 'hi'; | 40 importedKey.extraProperty = 'hi'; |
41 return cloneKey(importedKey); | 41 return cloneKey(importedKey); |
42 }).then(function(clonedKey) { | 42 }).then(function(clonedKey) { |
43 results.clonedKey = clonedKey; | 43 results.clonedKey = clonedKey; |
44 if (extractable) | 44 if (extractable) |
45 return crypto.subtle.exportKey('spki', clonedKey); | 45 return crypto.subtle.exportKey('pkcs8', clonedKey); |
46 return null; | 46 return null; |
47 }).then(function(clonedKeyData) { | 47 }).then(function(clonedKeyData) { |
48 importedKey = results.importedKey; | 48 importedKey = results.importedKey; |
49 clonedKey = results.clonedKey; | 49 clonedKey = results.clonedKey; |
50 | 50 |
51 shouldEvaluateAs("importedKey.extraProperty", "hi"); | 51 shouldEvaluateAs("importedKey.extraProperty", "hi"); |
52 shouldEvaluateAs("importedKey.type", "public"); | 52 shouldEvaluateAs("importedKey.type", "private"); |
53 shouldEvaluateAs("importedKey.extractable", extractable); | 53 shouldEvaluateAs("importedKey.extractable", extractable); |
54 shouldEvaluateAs("importedKey.algorithm.name", algorithmName); | 54 shouldEvaluateAs("importedKey.algorithm.name", algorithmName); |
55 shouldEvaluateAs("importedKey.algorithm.modulusLength", keyData.modulusL
engthBits); | 55 shouldEvaluateAs("importedKey.algorithm.modulusLength", keyData.modulusL
engthBits); |
56 bytesShouldMatchHexString("importedKey.algorithm.publicExponent", keyDat
a.publicExponent, importedKey.algorithm.publicExponent); | 56 bytesShouldMatchHexString("importedKey.algorithm.publicExponent", keyDat
a.publicExponent, importedKey.algorithm.publicExponent); |
57 shouldBeUndefined("importedKey.algorithm.hash"); | 57 shouldBeUndefined("importedKey.algorithm.hash"); |
58 shouldEvaluateAs("importedKey.usages.join(',')", keyUsages.join(",")); | 58 shouldEvaluateAs("importedKey.usages.join(',')", keyUsages.join(",")); |
59 | 59 |
60 shouldBeTrue("importedKey != clonedKey"); | 60 shouldBeTrue("importedKey != clonedKey"); |
61 | 61 |
62 shouldBeUndefined("clonedKey.extraProperty"); | 62 shouldBeUndefined("clonedKey.extraProperty"); |
63 shouldEvaluateAs("clonedKey.type", "public"); | 63 shouldEvaluateAs("clonedKey.type", "private"); |
64 shouldEvaluateAs("clonedKey.extractable", extractable); | 64 shouldEvaluateAs("clonedKey.extractable", extractable); |
65 shouldEvaluateAs("clonedKey.algorithm.name", algorithmName); | 65 shouldEvaluateAs("clonedKey.algorithm.name", algorithmName); |
66 shouldEvaluateAs("clonedKey.algorithm.modulusLength", keyData.modulusLen
gthBits); | 66 shouldEvaluateAs("clonedKey.algorithm.modulusLength", keyData.modulusLen
gthBits); |
67 bytesShouldMatchHexString("clonedKey.algorithm.publicExponent", keyData.
publicExponent, clonedKey.algorithm.publicExponent); | 67 bytesShouldMatchHexString("clonedKey.algorithm.publicExponent", keyData.
publicExponent, clonedKey.algorithm.publicExponent); |
68 shouldBeUndefined("clonedKey.algorithm.hash"); | 68 shouldBeUndefined("clonedKey.algorithm.hash"); |
69 shouldEvaluateAs("clonedKey.usages.join(',')", keyUsages.join(",")); | 69 shouldEvaluateAs("clonedKey.usages.join(',')", keyUsages.join(",")); |
70 | 70 |
71 logSerializedKey(importedKey); | 71 logSerializedKey(importedKey); |
72 | 72 |
73 if (extractable) | 73 if (extractable) |
74 bytesShouldMatchHexString("Cloned key exported data", keyData.spki,
clonedKeyData); | 74 bytesShouldMatchHexString("Cloned key exported data", keyData.pkcs8,
clonedKeyData); |
75 | 75 |
76 debug(""); | 76 debug(""); |
77 }); | 77 }); |
78 } | 78 } |
79 | 79 |
80 var lastPromise = Promise.resolve(null); | 80 var lastPromise = Promise.resolve(null); |
81 | 81 |
82 kPossibleAlgorithms.forEach(function(algorithmName) { | 82 kPossibleAlgorithms.forEach(function(algorithmName) { |
83 kPossibleExtractable.forEach(function(extractable) { | 83 kPossibleExtractable.forEach(function(extractable) { |
84 kPossibleKeyUsages.forEach(function(keyUsages) { | 84 kPossibleKeyUsages.forEach(function(keyUsages) { |
85 kPossibleKeyData.forEach(function(keyData) { | 85 kPossibleKeyData.forEach(function(keyData) { |
86 lastPromise = lastPromise.then(runTest.bind(null, algorithmNam
e, extractable, keyUsages, keyData)); | 86 lastPromise = lastPromise.then(runTest.bind(null, algorithmNam
e, extractable, keyUsages, keyData)); |
87 }); | 87 }); |
88 }); | 88 }); |
89 }); | 89 }); |
90 }); | 90 }); |
91 | 91 |
92 lastPromise.then(finishJSTest, failAndFinishJSTest); | 92 lastPromise.then(finishJSTest, failAndFinishJSTest); |
93 | 93 |
94 </script> | 94 </script> |
95 | 95 |
96 </body> | 96 </body> |
97 </html> | 97 </html> |
OLD | NEW |