-
Notifications
You must be signed in to change notification settings - Fork 43
/
crypto_subtle.dart
681 lines (616 loc) · 17 KB
/
crypto_subtle.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// This library attempts to expose the definitions necessary to use the
/// browsers `window.crypto.subtle` APIs.
library common;
import 'dart:js_interop';
import 'dart:typed_data';
import 'package:meta/meta.dart';
import 'jsonwebkey.dart' show JsonWebKey, RsaOtherPrimesInfo;
export 'jsonwebkey.dart' show JsonWebKey;
/// Convert [BigInt] to [Uint8List] formatted as [BigInteger][1] following
/// the Web Cryptography specification.
///
/// [1]: https://www.w3.org/TR/WebCryptoAPI/#big-integer
Uint8List bigIntToUint8ListBigInteger(BigInt integer) {
if (integer == BigInt.from(65537)) {
return Uint8List.fromList([0x01, 0x00, 0x01]); // 65537
}
if (integer == BigInt.from(3)) {
return Uint8List.fromList([0x03]); // 3
}
// TODO: Implement bigIntToUint8ListBigInteger for all positive integers
// There is no rush as this is only used for public exponent, and chrome only
// supports 3 and 65537, so supporting other numbers is a low priority.
// https://chromium.googlesource.com/chromium/src/+/43d62c50b705f88c67b14539e91fd8fd017f70c4/components/webcrypto/algorithms/rsa.cc#286
throw UnimplementedError('Only supports 65537 and 3 for now');
}
/// The `window` object.
@JS()
external JSWindow get window;
/// https://developer.mozilla.org/en-US/docs/Web/API/Window
extension type JSWindow(JSObject _) implements JSObject {
/// https://developer.mozilla.org/en-US/docs/Web/API/crypto_property
external JSCrypto get crypto;
}
/// The `window.crypto` object.
///
/// https://www.w3.org/TR/WebCryptoAPI/#crypto-interface
/// https://developer.mozilla.org/en-US/docs/Web/API/Crypto
extension type JSCrypto(JSObject _) implements JSObject {
/// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/subtle
external JSSubtleCrypto get subtle;
/// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
external JSTypedArray getRandomValues(JSTypedArray array);
}
/// The `window.crypto.subtle` object.
///
/// https://www.w3.org/TR/WebCryptoAPI/#subtlecrypto-interface
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto
extension type JSSubtleCrypto(JSObject _) implements JSObject {
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
external JSPromise<JSArrayBuffer> encrypt(
JSAny algorithm,
JSCryptoKey key,
JSTypedArray data,
);
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
external JSPromise<JSArrayBuffer> decrypt(
JSAny algorithm,
JSAny key,
JSTypedArray data,
);
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign
external JSPromise<JSArrayBuffer> sign(
JSAny algorithm,
JSCryptoKey key,
JSTypedArray data,
);
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/verify
external JSPromise<JSBoolean> verify(
JSAny algorithm,
JSCryptoKey key,
JSTypedArray signature,
JSTypedArray data,
);
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
external JSPromise<JSArrayBuffer> digest(
String algorithm,
JSTypedArray data,
);
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits
external JSPromise<JSArrayBuffer> deriveBits(
JSAny algorithm,
JSCryptoKey baseKey,
int length,
);
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey
@JS('generateKey')
external JSPromise<JSCryptoKey> generateCryptoKey(
JSAny algorithm,
bool extractable,
JSArray<JSString> keyUsages,
);
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey
@JS('generateKey')
external JSPromise<JSCryptoKeyPair> generateCryptoKeyPair(
JSAny algorithm,
bool extractable,
JSArray<JSString> keyUsages,
);
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
@JS('importKey')
external JSPromise<JSCryptoKey> importKey(
String format,
JSTypedArray keyData,
JSAny algorithm,
bool extractable,
JSArray<JSString> keyUsages,
);
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
@JS('importKey')
external JSPromise<JSCryptoKey> importJsonWebKey(
String format,
JSAny keyData,
JSAny algorithm,
bool extractable,
JSArray<JSString> keyUsages,
);
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey
@JS('exportKey')
external JSPromise<JSArrayBuffer> exportKey(
String format,
JSCryptoKey key,
);
/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey
@JS('exportKey')
external JSPromise<JSJsonWebKey> exportJsonWebKey(
String format,
JSCryptoKey key,
);
}
/// Wrapper for the [CryptoKey][1] type.
///
/// [1]: https://www.w3.org/TR/WebCryptoAPI/#cryptokey-interface
/// https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey
extension type JSCryptoKey(JSObject _) implements JSObject {
/// Returns the _type_ of this key, as one of:
/// * `'private'`
/// * `'public'`
/// * `'secret'`
///
/// https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey/type
external String get type;
/// True, if this key can be extracted.
///
/// https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey/extractable
external bool get extractable;
/// Ways in which this key can be used, list of one or more of:
/// * `'encrypt'`,
/// * `'decrypt'`,
/// * `'sign'`,
/// * `'verify'`,
/// * `'deriveKey'`,
/// * `'deriveBits'`,
/// * `'wrapKey'`,
/// * `'unwrapKey'`.
///
/// https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey/usages
external JSArray<JSString> get usages;
}
/// Wrapper for the [CryptoKeyPair][1] type.
///
/// [1]: https://www.w3.org/TR/WebCryptoAPI/#keypair
/// https://developer.mozilla.org/en-US/docs/Web/API/CryptoKeyPair
extension type JSCryptoKeyPair(JSObject _) implements JSObject {
/// https://developer.mozilla.org/en-US/docs/Web/API/CryptoKeyPair#cryptokeypair.privatekey
external JSCryptoKey get privateKey;
/// https://developer.mozilla.org/en-US/docs/Web/API/CryptoKeyPair#cryptokeypair.publickey
external JSCryptoKey get publicKey;
}
/// Wrapper for the [DOMException][1] type.
///
/// [1]: https://webidl.spec.whatwg.org/#idl-DOMException
@JS('DOMException')
extension type JSDomException(JSObject _) implements JSObject {
external String get name;
external String get message;
}
/// Anonymous object to be used for constructing the `algorithm` parameter in
/// `subtle.crypto` methods.
///
/// Note this only works because [WebIDL specification][1] for converting
/// dictionaries say to ignore properties whose values are `null` or
/// `undefined`. Otherwise, this object would define a lot of properties that
/// are not permitted. If two parameters for any algorithms in the Web
/// Cryptography specification has conflicting types in the future, we might
/// have to split this into multiple types. But so long as they don't have
/// conflicting parameters there is no reason to make a type per algorithm.
///
/// [1]: https://www.w3.org/TR/WebIDL-1/#es-dictionary
class Algorithm {
const Algorithm({
this.name,
this.modulusLength,
this.publicExponent,
this.hash,
this.saltLength,
this.label,
this.namedCurve,
this.public,
this.counter,
this.length,
this.iv,
this.additionalData,
this.tagLength,
this.salt,
this.info,
this.iterations,
});
final String? name;
final int? modulusLength;
final Uint8List? publicExponent;
final String? hash;
final int? saltLength;
final TypedData? label;
final String? namedCurve;
final JSCryptoKey? public;
final TypedData? counter;
final int? length;
final TypedData? iv;
final TypedData? additionalData;
final int? tagLength;
final TypedData? salt;
final TypedData? info;
final int? iterations;
Algorithm update({
String? name,
int? modulusLength,
Uint8List? publicExponent,
String? hash,
int? saltLength,
TypedData? label,
String? namedCurve,
JSCryptoKey? public,
TypedData? counter,
int? length,
TypedData? iv,
TypedData? additionalData,
int? tagLength,
TypedData? salt,
TypedData? info,
int? iterations,
}) =>
Algorithm(
name: this.name ?? name,
modulusLength: this.modulusLength ?? modulusLength,
publicExponent: this.publicExponent ?? publicExponent,
hash: this.hash ?? hash,
saltLength: this.saltLength ?? saltLength,
label: this.label ?? label,
namedCurve: this.namedCurve ?? namedCurve,
public: this.public ?? public,
counter: this.counter ?? counter,
length: this.length ?? length,
iv: this.iv ?? iv,
additionalData: this.additionalData ?? additionalData,
tagLength: this.tagLength ?? tagLength,
salt: this.salt ?? salt,
info: this.info ?? info,
iterations: this.iterations ?? iterations,
);
}
extension type JSJsonWebKey(JSObject _) implements JSObject {
external String? get kty;
external String? get use;
@JS('key_ops')
external JSArray<JSString>? get keyOps;
external String? get alg;
external bool? get ext;
external String? get crv;
external String? get x;
external String? get y;
external String? get d;
external String? get n;
external String? get e;
external String? get p;
external String? get q;
external String? get dp;
external String? get dq;
external String? get qi;
external JSArray<JSRsaOtherPrimesInfo>? get oth;
external String? get k;
}
extension type JSRsaOtherPrimesInfo(JSObject _) implements JSObject {
external String get r;
external String get d;
external String get t;
}
TypedData getRandomValues(TypedData array) {
if (array is Uint8List) {
window.crypto.getRandomValues(array.toJS);
return array;
} else if (array is Uint16List) {
window.crypto.getRandomValues(array.toJS);
return array;
} else if (array is Uint32List) {
window.crypto.getRandomValues(array.toJS);
return array;
} else if (array is Int8List) {
window.crypto.getRandomValues(array.toJS);
return array;
} else if (array is Int16List) {
window.crypto.getRandomValues(array.toJS);
return array;
} else if (array is Int32List) {
window.crypto.getRandomValues(array.toJS);
return array;
} else {
throw ArgumentError.value(
array,
'array',
'Unsupported TypedData type',
);
}
}
Future<ByteBuffer> decrypt(
Algorithm algorithm,
JSCryptoKey key,
Uint8List data,
) async {
final value = await window.crypto.subtle
.decrypt(
algorithm.toJS,
key,
data.toJS,
)
.toDart;
return value.toDart;
}
Future<ByteBuffer> encrypt(
Algorithm algorithm,
JSCryptoKey key,
Uint8List data,
) async {
final value = await window.crypto.subtle
.encrypt(
algorithm.toJS,
key,
data.toJS,
)
.toDart;
return value.toDart;
}
Future<ByteBuffer> exportKey(
String format,
JSCryptoKey key,
) async {
final value = await window.crypto.subtle
.exportKey(
format,
key,
)
.toDart;
return value.toDart;
}
Future<JsonWebKey> exportJsonWebKey(
String format,
JSCryptoKey key,
) async {
final value = await window.crypto.subtle
.exportJsonWebKey(
format,
key,
)
.toDart;
return value.toDart;
}
Future<JSCryptoKey> generateKey(
Algorithm algorithm,
bool extractable,
List<String> usages,
) async {
final value = await window.crypto.subtle
.generateCryptoKey(
algorithm.toJS,
extractable,
usages.toJS,
)
.toDart;
return value;
}
Future<JSCryptoKeyPair> generateKeyPair(
Algorithm algorithm,
bool extractable,
List<String> usages,
) async {
final value = await window.crypto.subtle
.generateCryptoKeyPair(
algorithm.toJS,
extractable,
usages.toJS,
)
.toDart;
return value;
}
Future<ByteBuffer> digest(
String algorithm,
Uint8List data,
) async {
final value = await window.crypto.subtle
.digest(
algorithm,
data.toJS,
)
.toDart;
return value.toDart;
}
Future<JSCryptoKey> importKey(
String format,
Uint8List keyData,
Algorithm algorithm,
bool extractable,
List<String> usages,
) async {
final value = await window.crypto.subtle
.importKey(
format,
keyData.toJS,
algorithm.toJS,
extractable,
usages.toJS,
)
.toDart;
return value;
}
Future<JSCryptoKey> importJsonWebKey(
String format,
JsonWebKey jwk,
Algorithm algorithm,
bool extractable,
List<String> usages,
) async {
final value = await window.crypto.subtle
.importJsonWebKey(
format,
jwk.toJS,
algorithm.toJS,
extractable,
usages.toJS,
)
.toDart;
return value;
}
Future<ByteBuffer> sign(
Algorithm algorithm,
JSCryptoKey key,
Uint8List data,
) async {
final value = await window.crypto.subtle
.sign(
algorithm.toJS,
key,
data.toJS,
)
.toDart;
return value.toDart;
}
Future<bool> verify(
Algorithm algorithm,
JSCryptoKey key,
Uint8List signature,
Uint8List data,
) async {
final value = await window.crypto.subtle
.verify(
algorithm.toJS,
key,
signature.toJS,
data.toJS,
)
.toDart;
return value.toDart;
}
Future<ByteBuffer> deriveBits(
Algorithm algorithm,
JSCryptoKey key,
int length,
) async {
final value = await window.crypto.subtle
.deriveBits(
algorithm.toJS,
key,
length,
)
.toDart;
return value.toDart;
}
extension ListExtension on List<String> {
@visibleForTesting
JSArray<JSString> get toJS => <JSString>[
for (final value in this) value.toJS,
].toJS;
}
extension AlgorithmExtension on Algorithm {
/// Create JSON from [Algorithm].
/// To avoid null properties for keys, eliminate keys whose value is null.
@visibleForTesting
JSAny get toJS {
final json = <String, Object>{};
final name_ = name;
if (name_ != null) {
json['name'] = name_;
}
final modulusLength_ = modulusLength;
if (modulusLength_ != null) {
json['modulusLength'] = modulusLength_;
}
final publicExponent_ = publicExponent;
if (publicExponent_ != null) {
json['publicExponent'] = publicExponent_;
}
final hash_ = hash;
if (hash_ != null) {
json['hash'] = hash_;
}
final saltLength_ = saltLength;
if (saltLength_ != null) {
json['saltLength'] = saltLength_;
}
final label_ = label;
if (label_ != null) {
json['label'] = label_.buffer;
}
final namedCurve_ = namedCurve;
if (namedCurve_ != null) {
json['namedCurve'] = namedCurve_;
}
final public_ = public;
if (public_ != null) {
json['public'] = public_;
}
final counter_ = counter;
if (counter_ != null) {
json['counter'] = counter_;
}
final length_ = length;
if (length_ != null) {
json['length'] = length_;
}
final iv_ = iv;
if (iv_ != null) {
json['iv'] = iv_.buffer;
}
final additionalData_ = additionalData;
if (additionalData_ != null) {
json['additionalData'] = additionalData_.buffer;
}
final tagLength_ = tagLength;
if (tagLength_ != null) {
json['tagLength'] = tagLength_;
}
final salt_ = salt;
if (salt_ != null) {
json['salt'] = salt_.buffer;
}
final info_ = info;
if (info_ != null) {
json['info'] = info_.buffer;
}
final iterations_ = iterations;
if (iterations_ != null) {
json['iterations'] = iterations_;
}
return json.jsify()!;
}
}
extension on JsonWebKey {
/// Create JSON from [JsonWebKey].
/// Convert the map created by JsonWebKey.toJson(),
/// to avoid null properties for keys.
JSAny get toJS => toJson().jsify()!;
}
extension on JSJsonWebKey {
JsonWebKey get toDart => JsonWebKey(
kty: kty,
use: use,
key_ops: keyOps?.toDart.map((e) => e.toDart).toList(),
alg: alg,
ext: ext,
crv: crv,
x: x,
y: y,
d: d,
n: n,
e: e,
p: p,
q: q,
dp: dp,
dq: dq,
qi: qi,
oth: oth?.toDart
.map(
(e) => RsaOtherPrimesInfo(
r: e.r,
d: e.d,
t: e.t,
),
)
.toList(),
k: k,
);
}
// TODO: crypto.subtle.unwrapKey
// TODO: crypto.subtle.wrapKey
// TODO: crypto.subtle.deriveKey