Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: chrome/test/data/extensions/api_test/enterprise_platform_keys/basic.js

Issue 214863002: Extension API enterprise.platformKeys. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Asynchronous calls revisited. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Must be packed to ../enterprise_platform_keys.crx using the private key
6 // ../enterprise_platform_keys.pem .
7
8 var assertEq = chrome.test.assertEq;
9 var assertTrue = chrome.test.assertTrue;
10 var assertThrows = chrome.test.assertThrows;
11 var fail = chrome.test.fail;
12 var succeed = chrome.test.succeed;
13 var callbackPass = chrome.test.callbackPass;
14 var callbackFail= chrome.test.callbackFail;
15
16 // openssl req -new -x509 -key privkey.pem \
17 // -outform der -out cert.der -days 36500
18 // xxd -i cert.der
19 // based on privateKeyPkcs8
20 var cert1a = new Uint8Array([
21 0x30, 0x82, 0x01, 0xd5, 0x30, 0x82, 0x01, 0x7f, 0xa0, 0x03, 0x02, 0x01,
22 0x02, 0x02, 0x09, 0x00, 0xd2, 0xcc, 0x76, 0xeb, 0x19, 0xb9, 0x3a, 0x33,
23 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
24 0x05, 0x05, 0x00, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
25 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
26 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74,
27 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a,
28 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57,
29 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c,
30 0x74, 0x64, 0x30, 0x20, 0x17, 0x0d, 0x31, 0x34, 0x30, 0x34, 0x31, 0x35,
31 0x31, 0x34, 0x35, 0x32, 0x30, 0x33, 0x5a, 0x18, 0x0f, 0x32, 0x31, 0x31,
32 0x34, 0x30, 0x33, 0x32, 0x32, 0x31, 0x34, 0x35, 0x32, 0x30, 0x33, 0x5a,
33 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
34 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08,
35 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65,
36 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49,
37 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67,
38 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30,
39 0x5c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
40 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00, 0x30, 0x48, 0x02, 0x41, 0x00,
41 0xc7, 0xc1, 0x4d, 0xd5, 0xdc, 0x3a, 0x2e, 0x1f, 0x42, 0x30, 0x3d, 0x21,
42 0x1e, 0xa2, 0x1f, 0x60, 0xcb, 0x71, 0x11, 0x53, 0xb0, 0x75, 0xa0, 0x62,
43 0xfe, 0x5e, 0x0a, 0xde, 0xb0, 0x0f, 0x48, 0x97, 0x5e, 0x42, 0xa7, 0x3a,
44 0xd1, 0xca, 0x4c, 0xe3, 0xdb, 0x5f, 0x31, 0xc2, 0x99, 0x08, 0x89, 0xcd,
45 0x6d, 0x20, 0xaa, 0x75, 0xe6, 0x2b, 0x98, 0xd2, 0xf3, 0x7b, 0x4b, 0xe5,
46 0x9b, 0xfe, 0xe2, 0x6d, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x50, 0x30,
47 0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
48 0xbd, 0x85, 0x6b, 0xdd, 0x84, 0xd1, 0x54, 0x2e, 0xad, 0xb4, 0x5e, 0xdd,
49 0x24, 0x7e, 0x16, 0x9c, 0x84, 0x1e, 0x19, 0xf0, 0x30, 0x1f, 0x06, 0x03,
50 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xbd, 0x85, 0x6b,
51 0xdd, 0x84, 0xd1, 0x54, 0x2e, 0xad, 0xb4, 0x5e, 0xdd, 0x24, 0x7e, 0x16,
52 0x9c, 0x84, 0x1e, 0x19, 0xf0, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13,
53 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0d, 0x06, 0x09, 0x2a,
54 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x41,
55 0x00, 0x37, 0x23, 0x2f, 0x81, 0x24, 0xfc, 0xec, 0x2d, 0x0b, 0xd1, 0xa0,
56 0x74, 0xdf, 0x2e, 0x34, 0x9a, 0x92, 0x33, 0xae, 0x75, 0xd6, 0x60, 0xfc,
57 0x44, 0x1d, 0x65, 0x8c, 0xb7, 0xd9, 0x60, 0x3b, 0xc7, 0x20, 0x30, 0xdf,
58 0x17, 0x07, 0xd1, 0x87, 0xda, 0x2b, 0x7f, 0x84, 0xf3, 0xfc, 0xb0, 0x31,
59 0x42, 0x08, 0x17, 0x96, 0xd2, 0x1b, 0xdc, 0x28, 0xae, 0xf8, 0xbd, 0xf9,
60 0x4e, 0x78, 0xc3, 0xe8, 0x80
61 ]);
62
63 // based on privateKeyPkcs8, different from cert1a
64 var cert1b = new Uint8Array([
65 0x30, 0x82, 0x01, 0xd5, 0x30, 0x82, 0x01, 0x7f, 0xa0, 0x03, 0x02, 0x01,
66 0x02, 0x02, 0x09, 0x00, 0xe7, 0x1e, 0x6e, 0xb0, 0x12, 0x87, 0xf5, 0x09,
67 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
68 0x05, 0x05, 0x00, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
69 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
70 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74,
71 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a,
72 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57,
73 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c,
74 0x74, 0x64, 0x30, 0x20, 0x17, 0x0d, 0x31, 0x34, 0x30, 0x34, 0x31, 0x35,
75 0x31, 0x35, 0x31, 0x39, 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x32, 0x31, 0x31,
76 0x34, 0x30, 0x33, 0x32, 0x32, 0x31, 0x35, 0x31, 0x39, 0x30, 0x30, 0x5a,
77 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
78 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08,
79 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65,
80 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49,
81 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67,
82 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30,
83 0x5c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
84 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00, 0x30, 0x48, 0x02, 0x41, 0x00,
85 0xc7, 0xc1, 0x4d, 0xd5, 0xdc, 0x3a, 0x2e, 0x1f, 0x42, 0x30, 0x3d, 0x21,
86 0x1e, 0xa2, 0x1f, 0x60, 0xcb, 0x71, 0x11, 0x53, 0xb0, 0x75, 0xa0, 0x62,
87 0xfe, 0x5e, 0x0a, 0xde, 0xb0, 0x0f, 0x48, 0x97, 0x5e, 0x42, 0xa7, 0x3a,
88 0xd1, 0xca, 0x4c, 0xe3, 0xdb, 0x5f, 0x31, 0xc2, 0x99, 0x08, 0x89, 0xcd,
89 0x6d, 0x20, 0xaa, 0x75, 0xe6, 0x2b, 0x98, 0xd2, 0xf3, 0x7b, 0x4b, 0xe5,
90 0x9b, 0xfe, 0xe2, 0x6d, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x50, 0x30,
91 0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
92 0xbd, 0x85, 0x6b, 0xdd, 0x84, 0xd1, 0x54, 0x2e, 0xad, 0xb4, 0x5e, 0xdd,
93 0x24, 0x7e, 0x16, 0x9c, 0x84, 0x1e, 0x19, 0xf0, 0x30, 0x1f, 0x06, 0x03,
94 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xbd, 0x85, 0x6b,
95 0xdd, 0x84, 0xd1, 0x54, 0x2e, 0xad, 0xb4, 0x5e, 0xdd, 0x24, 0x7e, 0x16,
96 0x9c, 0x84, 0x1e, 0x19, 0xf0, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13,
97 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0d, 0x06, 0x09, 0x2a,
98 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x41,
99 0x00, 0x82, 0x95, 0xa7, 0x08, 0x6c, 0xbd, 0x49, 0xe6, 0x1e, 0xc1, 0xd9,
100 0x58, 0x54, 0x11, 0x11, 0x84, 0x77, 0x1e, 0xad, 0xe9, 0x73, 0x69, 0x1c,
101 0x5c, 0xaa, 0x26, 0x3e, 0x5f, 0x1d, 0x89, 0x20, 0xc3, 0x90, 0xa4, 0x67,
102 0xfa, 0x26, 0x20, 0xd7, 0x1f, 0xae, 0x42, 0x89, 0x30, 0x61, 0x43, 0x8a,
103 0x8c, 0xbe, 0xd4, 0x32, 0xf7, 0x96, 0x71, 0x2a, 0xcd, 0xeb, 0x26, 0xf6,
104 0xdb, 0x54, 0x95, 0xca, 0x5a
105 ]);
106
107 // based on a private key different than privateKeyPkcs8
108 var cert2 = new Uint8Array([
109 0x30, 0x82, 0x01, 0xd5, 0x30, 0x82, 0x01, 0x7f, 0xa0, 0x03, 0x02, 0x01,
110 0x02, 0x02, 0x09, 0x00, 0x9e, 0x11, 0x7e, 0xff, 0x43, 0x84, 0xd4, 0xe6,
111 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
112 0x05, 0x05, 0x00, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
113 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
114 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74,
115 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a,
116 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57,
117 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c,
118 0x74, 0x64, 0x30, 0x20, 0x17, 0x0d, 0x31, 0x34, 0x30, 0x34, 0x30, 0x37,
119 0x31, 0x35, 0x35, 0x30, 0x30, 0x38, 0x5a, 0x18, 0x0f, 0x32, 0x31, 0x31,
120 0x34, 0x30, 0x33, 0x31, 0x34, 0x31, 0x35, 0x35, 0x30, 0x30, 0x38, 0x5a,
121 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
122 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08,
123 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65,
124 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49,
125 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67,
126 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30,
127 0x5c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
128 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00, 0x30, 0x48, 0x02, 0x41, 0x00,
129 0xac, 0x6c, 0x72, 0x46, 0xa2, 0xde, 0x88, 0x30, 0x54, 0x06, 0xad, 0xc7,
130 0x2d, 0x64, 0x6e, 0xf6, 0x0f, 0x72, 0x3e, 0x92, 0x31, 0xcc, 0x0b, 0xa0,
131 0x18, 0x20, 0xb0, 0xdb, 0x86, 0xab, 0x11, 0xc6, 0xa5, 0x78, 0xea, 0x64,
132 0xe8, 0xeb, 0xa5, 0xb3, 0x78, 0x5d, 0xbb, 0x10, 0x57, 0xe6, 0x12, 0x23,
133 0x89, 0x92, 0x1d, 0xa0, 0xe5, 0x1e, 0xd1, 0xc9, 0x0e, 0x62, 0xcb, 0xc9,
134 0xaf, 0xde, 0x4e, 0x83, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x50, 0x30,
135 0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
136 0x75, 0x6c, 0x61, 0xfb, 0xb0, 0x6e, 0x37, 0x32, 0x41, 0x62, 0x3b, 0x55,
137 0xbd, 0x5f, 0x6b, 0xe0, 0xdb, 0xb9, 0xc7, 0xec, 0x30, 0x1f, 0x06, 0x03,
138 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x75, 0x6c, 0x61,
139 0xfb, 0xb0, 0x6e, 0x37, 0x32, 0x41, 0x62, 0x3b, 0x55, 0xbd, 0x5f, 0x6b,
140 0xe0, 0xdb, 0xb9, 0xc7, 0xec, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13,
141 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0d, 0x06, 0x09, 0x2a,
142 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x41,
143 0x00, 0xa5, 0xe8, 0x9d, 0x3d, 0xc4, 0x1a, 0x6e, 0xd2, 0x92, 0x42, 0x37,
144 0xb9, 0x3a, 0xb3, 0x8e, 0x2f, 0x55, 0xb5, 0xf2, 0xe4, 0x6e, 0x39, 0x0d,
145 0xa8, 0xba, 0x10, 0x43, 0x57, 0xdd, 0x4e, 0x4e, 0x52, 0xc6, 0xbe, 0x07,
146 0xdb, 0x83, 0x05, 0x97, 0x97, 0xc1, 0x7b, 0xd5, 0x5c, 0x50, 0x64, 0x0f,
147 0x96, 0xff, 0x3d, 0x83, 0x37, 0x8f, 0x3a, 0x85, 0x08, 0x62, 0x5c, 0xb1,
148 0x2f, 0x68, 0xb2, 0x4a, 0x4a
149 ]);
150
151 /**
152 * Runs an array of asynchronous functions [f1, f2, ...] of the form
153 * function(callback) {}
154 * by chaining, i.e. f1(f2(...)). Additionally, each callback is wrapped with
155 * callbackPass.
156 */
157 function runAsyncSequence(funcs) {
158 if (funcs.length == 0)
159 return;
160 function go(i) {
161 var current = funcs[i];
162 console.log('#' + (i + 1) + ' of ' + funcs.length);
163 if (i == funcs.length - 1) {
164 current(callbackPass());
165 } else {
166 current(callbackPass(go.bind(undefined, i + 1)));
167 }
168 };
169 go(0);
170 }
171
172 // Some array comparison. Note: not lexicographical!
173 function compareArrays(array1, array2) {
174 if (array1.length < array2.length)
175 return -1;
176 if (array1.length > array2.length)
177 return 1;
178 for (var i = 0; i < array1.length; i++) {
179 if (array1[i] < array2[i])
180 return -1;
181 if (array1[i] > array2[i])
182 return 1;
183 }
184 return 0;
185 }
186
187 /**
188 * @param {ArrayBufferView[]} certs
189 * @return {ArrayBufferView[]} |certs| sorted in some order.
190 */
191 function sortCerts(certs) {
192 return certs.sort(compareArrays);
193 }
194
195 /**
196 * Checks whether the certificates currently stored in |token| match
197 * |expectedCerts| by comparing to the result of platformKeys.getCertificates.
198 * The order of |expectedCerts| is ignored. Afterwards calls |callback|.
199 */
200 function assertCertsStored(token, expectedCerts, callback) {
201 chrome.enterprise.platformKeys.getCertificates(
202 token.id,
203 callbackPass(function(actualCerts) {
204 assertEq(expectedCerts.length,
205 actualCerts.length,
206 'Number of stored certs not as expected');
207 if (expectedCerts.length == actualCerts.length) {
208 actualCerts = actualCerts.map(
209 function(buffer) { return new Uint8Array(buffer); });
210 actualCerts = sortCerts(actualCerts);
211 expectedCerts = sortCerts(expectedCerts);
212 for (var i = 0; i < expectedCerts.length; i++) {
213 assertTrue(compareArrays(expectedCerts[i], actualCerts[i]) == 0,
214 'Certs at index ' + i + ' differ');
215 }
216 }
217 if (callback)
218 callback();
219 }));
220 }
221
222 /**
223 * Fetches all available tokens using platformKeys.getTokens and calls
224 * |callback| with the user token if available or with undefined otherwise.
225 */
226 function getUserToken(callback) {
227 chrome.enterprise.platformKeys.getTokens(function(tokens) {
228 for (var i = 0; i < tokens.length; i++) {
229 if (tokens[i].id == 'user') {
230 callback(tokens[i]);
231 return;
232 }
233 }
234 callback(undefined);
235 });
236 }
237
238 /**
239 * Runs preparations before the actual tests. Calls |callback| with |userToken|.
240 */
241 function beforeTests(callback) {
242 assertTrue(!!chrome.enterprise, "No enterprise namespace.");
243 assertTrue(!!chrome.enterprise.platformKeys, "No platformKeys namespace.");
244 assertTrue(!!chrome.enterprise.platformKeys.getTokens,
245 "No getTokens function.");
246 assertTrue(!!chrome.enterprise.platformKeys.importCertificate,
247 "No importCertificate function.");
248 assertTrue(!!chrome.enterprise.platformKeys.removeCertificate,
249 "No removeCertificate function.");
250
251 getUserToken(function(userToken) {
252 if (!userToken)
253 chrome.test.fail('no user token');
254 if (userToken.id != 'user')
255 chrome.test.fail('token is not named "user".');
256
257 callback(userToken);
258 });
259 }
260
261 function runTests(userToken) {
262 chrome.test.runTests([
263 function hasSubtleCryptoMethods() {
264 assertTrue(!!userToken.subtleCrypto.generateKey,
265 "user token has no generateKey method");
266 assertTrue(!!userToken.subtleCrypto.sign,
267 "user token has no sign method");
268 assertTrue(!!userToken.subtleCrypto.exportKey,
269 "user token has no exportKey method");
270 succeed();
271 },
272 function initiallyNoCerts() { assertCertsStored(userToken, []); },
273
274 // Generates a key and sign some data with it. Verifies the signature using
275 // WebCrypto.
276 function generateKeyAndSign() {
277 var algorithm = {
278 name: "RSASSA-PKCS1-v1_5",
279 // RsaHashedKeyGenParams
280 modulusLength: 512,
281 publicExponent:
282 new Uint8Array([0x01, 0x00, 0x01]), // Equivalent to 65537
283 hash: {
284 name: "SHA-1",
285 }
286 };
287 // Some random data to sign.
288 var data = new Uint8Array([0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]);
289 var cachedKeyPair;
290 var cachedSpki;
291 var cachedSignature;
292 userToken.subtleCrypto.generateKey(algorithm, false, ["sign"])
293 .then(callbackPass(function(keyPair) {
294 assertTrue(!!keyPair, "No key pair.");
295 cachedKeyPair = keyPair;
296 return userToken.subtleCrypto.exportKey('spki',
297 keyPair.publicKey);
298 }),
299 function(error) {
300 assertTrue(false, "GenerateKey failed: " + error);
301 })
302 .then(callbackPass(function(publicKeySpki) {
303 cachedSpki = publicKeySpki;
304 return userToken.subtleCrypto.sign(
305 {}, cachedKeyPair.privateKey, data);
306 }),
307 function(error) {
308 assertTrue(false, "Export failed: " + error);
309 })
310 .then(callbackPass(function(signature) {
311 assertTrue(!!signature, "No signature.");
312 assertTrue(signature.length != 0, "Signature is empty.");
313 cachedSignature = signature;
314 return window.crypto.subtle.importKey(
315 "spki", cachedSpki, algorithm, false, ["verify"]);
316 }),
317 function(error) { assertTrue(false, "Sign failed: " + error); })
318 .then(callbackPass(function(webCryptoPublicKey) {
319 assertTrue(!!webCryptoPublicKey);
320 assertEq(algorithm.modulusLength,
321 webCryptoPublicKey.algorithm.modulusLength);
322 return window.crypto.subtle.verify(
323 algorithm, webCryptoPublicKey, cachedSignature, data);
324 }),
325 function(error) {
326 assertTrue(false, "Import failed: " + error);
327 })
328 .then(callbackPass(function(success) {
329 assertEq(true, success, "Signature invalid.");
330 }),
331 function(error) {
332 assertTrue(false, "Verification failed: " + error);
333 });
334 },
335
336 // Imports and removes certificates for privateKeyPkcs8, which was imported
337 // by on C++'s side.
338 // Note: After this test, privateKeyPkcs8 is not stored anymore!
339 function importAndRemoveCerts() {
340 runAsyncSequence([
341 chrome.enterprise.platformKeys.importCertificate.bind(
342 null, userToken.id, cert1a.buffer),
343 assertCertsStored.bind(null, userToken, [cert1a]),
344 // Importing the same cert again shouldn't change anything.
345 chrome.enterprise.platformKeys.importCertificate.bind(
346 null, userToken.id, cert1a.buffer),
347 assertCertsStored.bind(null, userToken, [cert1a]),
348 // Importing another certificate should succeed.
349 chrome.enterprise.platformKeys.importCertificate.bind(
350 null, userToken.id, cert1b.buffer),
351 assertCertsStored.bind(null, userToken, [cert1a, cert1b]),
352 chrome.enterprise.platformKeys.removeCertificate.bind(
353 null, userToken.id, cert1a.buffer),
354 assertCertsStored.bind(null, userToken, [cert1b]),
355 chrome.enterprise.platformKeys.removeCertificate.bind(
356 null, userToken.id, cert1b.buffer),
357 assertCertsStored.bind(null, userToken, [])
358 ]);
359 },
360
361 // Imports a certificate for which now private key was imported/generated
362 // before.
363 function missingPrivateKey() {
364 chrome.enterprise.platformKeys.importCertificate(
365 userToken.id, cert2.buffer, callbackFail('Key not found.'));
366 },
367 function importInvalidCert() {
368 var invalidCert = new ArrayBuffer(16);
369 chrome.enterprise.platformKeys.importCertificate(
370 userToken.id,
371 invalidCert,
372 callbackFail('Certificate is not a valid X.509 certificate.'));
373 },
374 function removeUnknownCert() {
375 chrome.enterprise.platformKeys.removeCertificate(
376 userToken.id,
377 cert2.buffer,
378 callbackFail('Certificate could not be found.'));
379 },
380 function removeInvalidCert() {
381 var invalidCert = new ArrayBuffer(16);
382 chrome.enterprise.platformKeys.removeCertificate(
383 userToken.id,
384 invalidCert,
385 callbackFail('Certificate is not a valid X.509 certificate.'));
386 },
387 function getCertsInvalidToken() {
388 chrome.enterprise.platformKeys.getCertificates(
389 'invalid token id', callbackFail('The token is not valid.'));
390 }
391 ]);
392 }
393
394 beforeTests(runTests);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698