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> | |
7 </head> | 6 </head> |
8 <body> | 7 <body> |
9 <p id="description"></p> | 8 <p id="description"></p> |
10 <div id="console"></div> | 9 <div id="console"></div> |
11 | 10 |
12 <script> | 11 <script> |
13 description("Tests structured cloning of RSA private keys (with a hash)"); | 12 description("Tests structured cloning of EC private keys"); |
14 | 13 |
15 jsTestIsAsync = true; | 14 jsTestIsAsync = true; |
16 | 15 |
17 // Tests the 12 permutations of keys generated by: | 16 // Tests the 12 permutations of keys generated by: |
18 // kPossibleAlgorithms x kPossibleExtractable x kPossibleKeyUsages x kPossible
KeyData x kPossibleHashAlgorithms | 17 // kPossibleAlgorithms x kPossibleExtractable x kPossibleKeyUsages x kPossible
Curves |
19 // | 18 // |
20 // For practical reasons these tests are not exhaustive. | 19 // For practical reasons these tests are not exhaustive. |
21 | 20 |
22 var kPossibleAlgorithms = ['RSASSA-PKCS1-v1_5']; | 21 var kPossibleAlgorithms = ['ECDSA']; |
23 var kPossibleExtractable = [true, false]; | 22 var kPossibleExtractable = [true, false]; |
24 var kPossibleKeyUsages = [[], ['sign']]; | 23 var kPossibleKeyUsages = [[], ['sign']]; |
25 var kPossibleHashAlgorithms = ['SHA-1', 'SHA-256', 'SHA-512']; | 24 var kPossibleNamedCurves = ['P-256', 'P-384', 'P-521']; |
26 | 25 |
27 var kPossibleKeyData = [ | 26 // A mapping from curve name, to PKCS8 data (hex-encoded) for a valid private ke
y. |
28 kKeyData.rsa1, | 27 var kKeyDataForCurve = { |
29 kKeyData.rsa4 | 28 "P-256": "308187020100301306072A8648CE3D020106082A8648CE3D030107046D306B0201
0104201FE33950C5F461124AE992C2BDFDF1C73B1615F571BD567E60D19AA1F48CDF42A144034200
047C110C66DCFDA807F6E69E45DDB3C74F69A1484D203E8DC5ADA8E9A9DD7CB3C70DF448986E51BD
E5D1576F99901F9C2C6A806A47FD907643A72B835597EFC8C6", |
30 ]; | 29 "P-384": "3081B6020100301006072A8648CE3D020106052B8104002204819E30819B020101
0430A492CE8FA90084C227E1A32F7974D39E9FF67A7E8705EC3419B35FB607582BEBD461E0B1520A
C76EC2DD4E9B63EBAE71A16403620004E55FEE6C49D8D523F5CE7BF9C0425CE4FF650708B7DE5CFB
095901523979A7F042602DB30854735369813B5C3F5EF86828F59CC5DC509892A988D38A8E2519DE
3D0C4FD0FBDB0993E38F18506C17606C5E24249246F1CE94983A5361C5BE983E", |
| 30 "P-521": "3081EE020100301006072A8648CE3D020106052B810400230481D63081D3020101
044201BD56BD106118EDA246155BD43B42B8E13F0A6E25DD3BB376026FAB4DC92B6157BC6DFEC2D1
5DD3D0CF2A39AA68494042AF48BA9601118DA82C6F2108A3A203AD74A181890381860004012FBCAE
FFA6A51F3EE4D3D2B51C5DEC6D7C726CA353FC014EA2BF7CFBB9B910D32CBFA6A00FE39B6CDB8946
F22775398B2E233C0CF144D78C8A7742B5C7A3BB5D23009CDEF823DD7BF9A79E8CCEACD2E4527C23
1D0AE5967AF0958E931D7DDCCF2805A3E618DC3039FEC9FEBBD33052FE4C0FEE98F033106064982D
88F4E03549D4A64D" |
| 31 }; |
31 | 32 |
32 function runTest(algorithmName, hashName, extractable, keyUsages, keyData) | 33 function runTest(algorithmName, namedCurve, extractable, keyUsages) |
33 { | 34 { |
34 var importData = hexStringToUint8Array(keyData.pkcs8); | 35 var keyDataHex = kKeyDataForCurve[namedCurve]; |
35 var importAlgorithm = { name: algorithmName, hash: {name: hashName} }; | 36 var importData = hexStringToUint8Array(keyDataHex); |
| 37 var importAlgorithm = { name: algorithmName, namedCurve: namedCurve }; |
36 | 38 |
37 var results = {}; | 39 var results = {}; |
38 | 40 |
39 return crypto.subtle.importKey('pkcs8', importData, importAlgorithm, extract
able, keyUsages).then(function(importedKey) { | 41 return crypto.subtle.importKey('pkcs8', importData, importAlgorithm, extract
able, keyUsages).then(function(importedKey) { |
40 results.importedKey = importedKey; | 42 results.importedKey = importedKey; |
41 importedKey.extraProperty = 'hi'; | 43 importedKey.extraProperty = 'hi'; |
42 return cloneKey(importedKey); | 44 return cloneKey(importedKey); |
43 }).then(function(clonedKey) { | 45 }).then(function(clonedKey) { |
44 results.clonedKey = clonedKey; | 46 results.clonedKey = clonedKey; |
45 if (extractable) | 47 if (extractable) |
46 return crypto.subtle.exportKey('pkcs8', clonedKey); | 48 return crypto.subtle.exportKey('pkcs8', clonedKey); |
47 return null; | 49 return null; |
48 }).then(function(clonedKeyData) { | 50 }).then(function(clonedKeyData) { |
49 importedKey = results.importedKey; | 51 importedKey = results.importedKey; |
50 clonedKey = results.clonedKey; | 52 clonedKey = results.clonedKey; |
51 | 53 |
52 shouldEvaluateAs("importedKey.extraProperty", "hi"); | 54 shouldEvaluateAs("importedKey.extraProperty", "hi"); |
53 shouldEvaluateAs("importedKey.type", "private"); | 55 shouldEvaluateAs("importedKey.type", "private"); |
54 shouldEvaluateAs("importedKey.extractable", extractable); | 56 shouldEvaluateAs("importedKey.extractable", extractable); |
55 shouldEvaluateAs("importedKey.algorithm.name", algorithmName); | 57 shouldEvaluateAs("importedKey.algorithm.name", algorithmName); |
56 shouldEvaluateAs("importedKey.algorithm.modulusLength", keyData.modulusL
engthBits); | 58 shouldEvaluateAs("importedKey.algorithm.namedCurve", namedCurve); |
57 bytesShouldMatchHexString("importedKey.algorithm.publicExponent", keyDat
a.publicExponent, importedKey.algorithm.publicExponent); | |
58 shouldEvaluateAs("importedKey.algorithm.hash.name", hashName); | |
59 shouldEvaluateAs("importedKey.usages.join(',')", keyUsages.join(",")); | 59 shouldEvaluateAs("importedKey.usages.join(',')", keyUsages.join(",")); |
60 | 60 |
61 shouldNotBe("importedKey", "clonedKey"); | 61 shouldNotBe("importedKey", "clonedKey"); |
62 | 62 |
63 shouldBeUndefined("clonedKey.extraProperty"); | 63 shouldBeUndefined("clonedKey.extraProperty"); |
64 shouldEvaluateAs("clonedKey.type", "private"); | 64 shouldEvaluateAs("clonedKey.type", "private"); |
65 shouldEvaluateAs("clonedKey.extractable", extractable); | 65 shouldEvaluateAs("clonedKey.extractable", extractable); |
66 shouldEvaluateAs("clonedKey.algorithm.name", algorithmName); | 66 shouldEvaluateAs("clonedKey.algorithm.name", algorithmName); |
67 shouldEvaluateAs("clonedKey.algorithm.modulusLength", keyData.modulusLen
gthBits); | 67 shouldEvaluateAs("clonedKey.algorithm.namedCurve", namedCurve); |
68 bytesShouldMatchHexString("clonedKey.algorithm.publicExponent", keyData.
publicExponent, clonedKey.algorithm.publicExponent); | |
69 shouldEvaluateAs("clonedKey.algorithm.hash.name", hashName); | |
70 shouldEvaluateAs("clonedKey.usages.join(',')", keyUsages.join(",")); | 68 shouldEvaluateAs("clonedKey.usages.join(',')", keyUsages.join(",")); |
71 | 69 |
72 logSerializedKey(importedKey); | 70 logSerializedKey(importedKey); |
73 | 71 |
74 if (extractable) | 72 if (extractable) |
75 bytesShouldMatchHexString("Cloned key exported data", keyData.pkcs8,
clonedKeyData); | 73 bytesShouldMatchHexString("Cloned key exported data", keyDataHex, cl
onedKeyData); |
76 | 74 |
77 debug(""); | 75 debug(""); |
78 }); | 76 }); |
79 } | 77 } |
80 | 78 |
81 var lastPromise = Promise.resolve(null); | 79 var lastPromise = Promise.resolve(null); |
82 | 80 |
83 kPossibleAlgorithms.forEach(function(algorithmName) { | 81 kPossibleAlgorithms.forEach(function(algorithmName) { |
84 kPossibleExtractable.forEach(function(extractable) { | 82 kPossibleExtractable.forEach(function(extractable) { |
85 kPossibleKeyUsages.forEach(function(keyUsages) { | 83 kPossibleKeyUsages.forEach(function(keyUsages) { |
86 kPossibleKeyData.forEach(function(keyData) { | 84 kPossibleNamedCurves.forEach(function(namedCurve) { |
87 kPossibleHashAlgorithms.forEach(function(hashName) { | 85 lastPromise = lastPromise.then(runTest.bind(null, algorithmNam
e, namedCurve, extractable, keyUsages)); |
88 lastPromise = lastPromise.then(runTest.bind(null, algorithmN
ame, hashName, extractable, keyUsages, keyData)); | 86 }); |
89 }); | |
90 }); | |
91 }); | 87 }); |
92 }); | 88 }); |
93 }); | 89 }); |
94 | 90 |
95 lastPromise.then(finishJSTest, failAndFinishJSTest); | 91 lastPromise.then(finishJSTest, failAndFinishJSTest); |
96 | 92 |
97 </script> | 93 </script> |
98 | 94 |
99 </body> | 95 </body> |
100 </html> | 96 </html> |
OLD | NEW |