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

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

Powered by Google App Engine
This is Rietveld 408576698