forked from xvrh/chrome_extension.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenterprise_platform_keys.dart
456 lines (404 loc) · 17.9 KB
/
enterprise_platform_keys.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// ignore_for_file: unnecessary_parenthesis
library;
import 'dart:typed_data';
import 'enterprise.dart';
import 'src/internal_helpers.dart';
import 'src/js/enterprise_platform_keys.dart' as $js;
export 'enterprise.dart' show ChromeEnterprise, ChromeEnterpriseExtension;
export 'src/chrome.dart' show chrome, EventStream;
final _enterprisePlatformKeys = ChromeEnterprisePlatformKeys._();
extension ChromeEnterprisePlatformKeysExtension on ChromeEnterprise {
/// Use the `chrome.enterprise.platformKeys` API to generate keys and
/// install certificates for these keys. The certificates will be managed by
/// the
/// platform and can be used for TLS authentication, network access or by
/// other
/// extension through $(ref:platformKeys chrome.platformKeys).
ChromeEnterprisePlatformKeys get platformKeys => _enterprisePlatformKeys;
}
class ChromeEnterprisePlatformKeys {
ChromeEnterprisePlatformKeys._();
bool get isAvailable =>
$js.chrome.enterpriseNullable?.platformKeysNullable != null && alwaysTrue;
/// Returns the available Tokens. In a regular user's session the list will
/// always contain the user's token with `id` `"user"`.
/// If a system-wide TPM token is available, the returned list will also
/// contain the system-wide token with `id` `"system"`.
/// The system-wide token will be the same for all sessions on this device
/// (device in the sense of e.g. a Chromebook).
Future<List<Token>> getTokens() {
var $completer = Completer<List<Token>>();
$js.chrome.enterprise.platformKeys.getTokens((JSArray tokens) {
if (checkRuntimeLastError($completer)) {
$completer.complete(tokens.toDart
.cast<$js.Token>()
.map((e) => Token.fromJS(e))
.toList());
}
}.toJS);
return $completer.future;
}
/// Returns the list of all client certificates available from the given
/// token. Can be used to check for the existence and expiration of client
/// certificates that are usable for a certain authentication.
/// |tokenId|: The id of a Token returned by `getTokens`.
/// |callback|: Called back with the list of the available certificates.
Future<List<ByteBuffer>> getCertificates(String tokenId) {
var $completer = Completer<List<ByteBuffer>>();
$js.chrome.enterprise.platformKeys.getCertificates(
tokenId,
(JSArray certificates) {
if (checkRuntimeLastError($completer)) {
$completer.complete(certificates.toDart
.cast<JSArrayBuffer>()
.map((e) => e.toDart)
.toList());
}
}.toJS,
);
return $completer.future;
}
/// Imports `certificate` to the given token if the certified key
/// is already stored in this token.
/// After a successful certification request, this function should be used to
/// store the obtained certificate and to make it available to the operating
/// system and browser for authentication.
/// |tokenId|: The id of a Token returned by `getTokens`.
/// |certificate|: The DER encoding of a X.509 certificate.
/// |callback|: Called back when this operation is finished.
Future<void> importCertificate(
String tokenId,
ByteBuffer certificate,
) {
var $completer = Completer<void>();
$js.chrome.enterprise.platformKeys.importCertificate(
tokenId,
certificate.toJS,
() {
if (checkRuntimeLastError($completer)) {
$completer.complete(null);
}
}.toJS,
);
return $completer.future;
}
/// Removes `certificate` from the given token if present.
/// Should be used to remove obsolete certificates so that they are not
/// considered during authentication and do not clutter the certificate
/// choice. Should be used to free storage in the certificate store.
/// |tokenId|: The id of a Token returned by `getTokens`.
/// |certificate|: The DER encoding of a X.509 certificate.
/// |callback|: Called back when this operation is finished.
Future<void> removeCertificate(
String tokenId,
ByteBuffer certificate,
) {
var $completer = Completer<void>();
$js.chrome.enterprise.platformKeys.removeCertificate(
tokenId,
certificate.toJS,
() {
if (checkRuntimeLastError($completer)) {
$completer.complete(null);
}
}.toJS,
);
return $completer.future;
}
/// Similar to `challengeMachineKey` and
/// `challengeUserKey`, but allows specifying the algorithm of a
/// registered key. Challenges a hardware-backed Enterprise Machine Key and
/// emits the response as part of a remote attestation protocol. Only useful
/// on Chrome OS and in conjunction with the Verified Access Web API which
/// both issues challenges and verifies responses.
///
/// A successful verification by the Verified Access Web API is a strong
/// signal that the current device is a legitimate Chrome OS device, the
/// current device is managed by the domain specified during verification,
/// the current signed-in user is managed by the domain specified during
/// verification, and the current device state complies with enterprise
/// device policy. For example, a policy may specify that the device must not
/// be in developer mode. Any device identity emitted by the verification is
/// tightly bound to the hardware of the current device. If
/// `"user"` Scope is specified, the identity is also tighly bound
/// to the current signed-in user.
///
/// This function is highly restricted and will fail if the current device is
/// not managed, the current user is not managed, or if this operation has
/// not explicitly been enabled for the caller by enterprise device policy.
/// The challenged key does not reside in the `"system"` or
/// `"user"` token and is not accessible by any other API.
/// |options|: Object containing the fields defined in
/// [ChallengeKeyOptions].
/// |callback|: Called back with the challenge response.
Future<ByteBuffer> challengeKey(ChallengeKeyOptions options) {
var $completer = Completer<ByteBuffer>();
$js.chrome.enterprise.platformKeys.challengeKey(
options.toJS,
(JSArrayBuffer response) {
if (checkRuntimeLastError($completer)) {
$completer.complete(response.toDart);
}
}.toJS,
);
return $completer.future;
}
/// Challenges a hardware-backed Enterprise Machine Key and emits the
/// response as part of a remote attestation protocol. Only useful on Chrome
/// OS and in conjunction with the Verified Access Web API which both issues
/// challenges and verifies responses. A successful verification by the
/// Verified Access Web API is a strong signal of all of the following:
/// * The current device is a legitimate Chrome OS device.
/// * The current device is managed by the domain specified during
/// verification.
/// * The current signed-in user is managed by the domain specified during
/// verification.
/// * The current device state complies with enterprise device policy. For
/// example, a policy may specify that the device must not be in developer
/// mode.
/// * Any device identity emitted by the verification is tightly bound to the
/// hardware of the current device.
/// This function is highly restricted and will fail if the current device
/// is not managed, the current user is not managed, or if this operation
/// has not explicitly been enabled for the caller by enterprise device
/// policy. The Enterprise Machine Key does not reside in the
/// `"system"` token and is not accessible by any other API.
/// |challenge|: A challenge as emitted by the Verified Access Web API.
/// |registerKey|: If set, the current Enterprise Machine Key is registered
/// with the `"system"` token and relinquishes the
/// Enterprise Machine Key role. The key can then be
/// associated with a certificate and used like any other
/// signing key. This key is 2048-bit RSA. Subsequent calls
/// to this function will then generate a new Enterprise
/// Machine Key.
/// |callback|: Called back with the challenge response.
@Deprecated(r'Use $(ref:challengeKey) instead.')
Future<ByteBuffer> challengeMachineKey(
ByteBuffer challenge,
bool? registerKey,
) {
var $completer = Completer<ByteBuffer>();
$js.chrome.enterprise.platformKeys.challengeMachineKey(
challenge.toJS,
registerKey,
(JSArrayBuffer response) {
if (checkRuntimeLastError($completer)) {
$completer.complete(response.toDart);
}
}.toJS,
);
return $completer.future;
}
/// Challenges a hardware-backed Enterprise User Key and emits the response
/// as part of a remote attestation protocol. Only useful on Chrome OS and in
/// conjunction with the Verified Access Web API which both issues challenges
/// and verifies responses. A successful verification by the Verified Access
/// Web API is a strong signal of all of the following:
/// * The current device is a legitimate Chrome OS device.
/// * The current device is managed by the domain specified during
/// verification.
/// * The current signed-in user is managed by the domain specified during
/// verification.
/// * The current device state complies with enterprise user policy. For
/// example, a policy may specify that the device must not be in developer
/// mode.
/// * The public key emitted by the verification is tightly bound to the
/// hardware of the current device and to the current signed-in user.
/// This function is highly restricted and will fail if the current device is
/// not managed, the current user is not managed, or if this operation has
/// not explicitly been enabled for the caller by enterprise user policy.
/// The Enterprise User Key does not reside in the `"user"` token
/// and is not accessible by any other API.
/// |challenge|: A challenge as emitted by the Verified Access Web API.
/// |registerKey|: If set, the current Enterprise User Key is registered with
/// the `"user"` token and relinquishes the
/// Enterprise User Key role. The key can then be associated
/// with a certificate and used like any other signing key.
/// This key is 2048-bit RSA. Subsequent calls to this
/// function will then generate a new Enterprise User Key.
/// |callback|: Called back with the challenge response.
@Deprecated(r'Use $(ref:challengeKey) instead.')
Future<ByteBuffer> challengeUserKey(
ByteBuffer challenge,
bool registerKey,
) {
var $completer = Completer<ByteBuffer>();
$js.chrome.enterprise.platformKeys.challengeUserKey(
challenge.toJS,
registerKey,
(JSArrayBuffer response) {
if (checkRuntimeLastError($completer)) {
$completer.complete(response.toDart);
}
}.toJS,
);
return $completer.future;
}
}
/// Whether to use the Enterprise User Key or the Enterprise Machine Key.
enum Scope {
user('USER'),
machine('MACHINE');
const Scope(this.value);
final String value;
String get toJS => value;
static Scope fromJS(String value) =>
values.firstWhere((e) => e.value == value);
}
/// Type of key to generate.
enum Algorithm {
rsa('RSA'),
ecdsa('ECDSA');
const Algorithm(this.value);
final String value;
String get toJS => value;
static Algorithm fromJS(String value) =>
values.firstWhere((e) => e.value == value);
}
class Token {
Token.fromJS(this._wrapped);
Token({
/// Uniquely identifies this `Token`.
/// Static IDs are `"user"` and `"system"`,
/// referring to the platform's user-specific and the system-wide hardware
/// token, respectively. Any other tokens (with other identifiers) might be
/// returned by [enterprise.platformKeys.getTokens].
required String id,
/// Implements the WebCrypto's
/// [SubtleCrypto](http://www.w3.org/TR/WebCryptoAPI/#subtlecrypto-interface)
/// interface. The cryptographic operations, including key generation, are
/// hardware-backed.
/// Only non-extractable RSASSA-PKCS1-V1_5 keys with
/// `modulusLength` up to 2048 and ECDSA with
/// `namedCurve` P-256 can be generated. Each key can be
/// used for signing data at most once.
/// Keys generated on a specific `Token` cannot be used with
/// any other Tokens, nor can they be used with
/// `window.crypto.subtle`. Equally, `Key` objects
/// created with `window.crypto.subtle` cannot be used with this
/// interface.
required JSObject subtleCrypto,
/// Implements the WebCrypto's
/// [SubtleCrypto](http://www.w3.org/TR/WebCryptoAPI/#subtlecrypto-interface)
/// interface. The cryptographic operations, including key generation, are
/// software-backed. Protection of the keys, and thus implementation of the
/// non-extractable property, is done in software, so the keys are less
/// protected than hardware-backed keys.
/// Only non-extractable RSASSA-PKCS1-V1_5 keys with
/// `modulusLength` up to 2048 can be generated. Each key can be
/// used for signing data at most once.
/// Keys generated on a specific `Token` cannot be used with
/// any other Tokens, nor can they be used with
/// `window.crypto.subtle`. Equally, `Key` objects
/// created with `window.crypto.subtle` cannot be used with this
/// interface.
required JSObject softwareBackedSubtleCrypto,
}) : _wrapped = $js.Token(
id: id,
subtleCrypto: subtleCrypto,
softwareBackedSubtleCrypto: softwareBackedSubtleCrypto,
);
final $js.Token _wrapped;
$js.Token get toJS => _wrapped;
/// Uniquely identifies this `Token`.
/// Static IDs are `"user"` and `"system"`,
/// referring to the platform's user-specific and the system-wide hardware
/// token, respectively. Any other tokens (with other identifiers) might be
/// returned by [enterprise.platformKeys.getTokens].
String get id => _wrapped.id;
set id(String v) {
_wrapped.id = v;
}
/// Implements the WebCrypto's
/// [SubtleCrypto](http://www.w3.org/TR/WebCryptoAPI/#subtlecrypto-interface)
/// interface. The cryptographic operations, including key generation, are
/// hardware-backed.
/// Only non-extractable RSASSA-PKCS1-V1_5 keys with
/// `modulusLength` up to 2048 and ECDSA with
/// `namedCurve` P-256 can be generated. Each key can be
/// used for signing data at most once.
/// Keys generated on a specific `Token` cannot be used with
/// any other Tokens, nor can they be used with
/// `window.crypto.subtle`. Equally, `Key` objects
/// created with `window.crypto.subtle` cannot be used with this
/// interface.
JSObject get subtleCrypto => _wrapped.subtleCrypto;
set subtleCrypto(JSObject v) {
_wrapped.subtleCrypto = v;
}
/// Implements the WebCrypto's
/// [SubtleCrypto](http://www.w3.org/TR/WebCryptoAPI/#subtlecrypto-interface)
/// interface. The cryptographic operations, including key generation, are
/// software-backed. Protection of the keys, and thus implementation of the
/// non-extractable property, is done in software, so the keys are less
/// protected than hardware-backed keys.
/// Only non-extractable RSASSA-PKCS1-V1_5 keys with
/// `modulusLength` up to 2048 can be generated. Each key can be
/// used for signing data at most once.
/// Keys generated on a specific `Token` cannot be used with
/// any other Tokens, nor can they be used with
/// `window.crypto.subtle`. Equally, `Key` objects
/// created with `window.crypto.subtle` cannot be used with this
/// interface.
JSObject get softwareBackedSubtleCrypto =>
_wrapped.softwareBackedSubtleCrypto;
set softwareBackedSubtleCrypto(JSObject v) {
_wrapped.softwareBackedSubtleCrypto = v;
}
}
class RegisterKeyOptions {
RegisterKeyOptions.fromJS(this._wrapped);
RegisterKeyOptions(
{
/// Which algorithm the registered key should use.
required Algorithm algorithm})
: _wrapped = $js.RegisterKeyOptions(algorithm: algorithm.toJS);
final $js.RegisterKeyOptions _wrapped;
$js.RegisterKeyOptions get toJS => _wrapped;
/// Which algorithm the registered key should use.
Algorithm get algorithm => Algorithm.fromJS(_wrapped.algorithm);
set algorithm(Algorithm v) {
_wrapped.algorithm = v.toJS;
}
}
class ChallengeKeyOptions {
ChallengeKeyOptions.fromJS(this._wrapped);
ChallengeKeyOptions({
/// A challenge as emitted by the Verified Access Web API.
required ByteBuffer challenge,
/// If present, registers the challenged key with the specified
/// `scope`'s token. The key can then be associated with a
/// certificate and used like any other signing key. Subsequent calls to
/// this function will then generate a new Enterprise Key in the specified
/// `scope`.
RegisterKeyOptions? registerKey,
/// Which Enterprise Key to challenge.
required Scope scope,
}) : _wrapped = $js.ChallengeKeyOptions(
challenge: challenge.toJS,
registerKey: registerKey?.toJS,
scope: scope.toJS,
);
final $js.ChallengeKeyOptions _wrapped;
$js.ChallengeKeyOptions get toJS => _wrapped;
/// A challenge as emitted by the Verified Access Web API.
ByteBuffer get challenge => _wrapped.challenge.toDart;
set challenge(ByteBuffer v) {
_wrapped.challenge = v.toJS;
}
/// If present, registers the challenged key with the specified
/// `scope`'s token. The key can then be associated with a
/// certificate and used like any other signing key. Subsequent calls to
/// this function will then generate a new Enterprise Key in the specified
/// `scope`.
RegisterKeyOptions? get registerKey =>
_wrapped.registerKey?.let(RegisterKeyOptions.fromJS);
set registerKey(RegisterKeyOptions? v) {
_wrapped.registerKey = v?.toJS;
}
/// Which Enterprise Key to challenge.
Scope get scope => Scope.fromJS(_wrapped.scope);
set scope(Scope v) {
_wrapped.scope = v.toJS;
}
}