Skip to content

Commit 8980b05

Browse files
move to package:web
1 parent d7945ee commit 8980b05

19 files changed

+983
-804
lines changed

lib/src/e2ee.worker/crypto.dart

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
/*
12
import 'dart:async';
2-
import 'dart:html' as html;
33
import 'dart:js_util' as jsutil;
44
import 'dart:typed_data';
55
66
import 'package:js/js.dart';
7+
import 'package:web/web.dart' as web;
8+
79
810
@JS('Promise')
911
class Promise<T> {
@@ -18,14 +20,14 @@ class Algorithm {
1820
@JS('crypto.subtle.encrypt')
1921
external Promise<ByteBuffer> encrypt(
2022
dynamic algorithm,
21-
html.CryptoKey key,
23+
web.CryptoKey key,
2224
ByteBuffer data,
2325
);
2426
2527
@JS('crypto.subtle.decrypt')
2628
external Promise<ByteBuffer> decrypt(
2729
dynamic algorithm,
28-
html.CryptoKey key,
30+
web.CryptoKey key,
2931
ByteBuffer data,
3032
);
3133
@@ -52,7 +54,7 @@ ByteBuffer jsArrayBufferFrom(List<int> data) {
5254
}
5355
5456
@JS('crypto.subtle.importKey')
55-
external Promise<html.CryptoKey> importKey(
57+
external Promise<web.CryptoKey> importKey(
5658
String format,
5759
ByteBuffer keyData,
5860
dynamic algorithm,
@@ -63,32 +65,34 @@ external Promise<html.CryptoKey> importKey(
6365
@JS('crypto.subtle.exportKey')
6466
external Promise<ByteBuffer> exportKey(
6567
String format,
66-
html.CryptoKey key,
68+
web.CryptoKey key,
6769
);
6870
6971
@JS('crypto.subtle.deriveKey')
70-
external Promise<html.CryptoKey> deriveKey(
72+
external Promise<web.CryptoKey> deriveKey(
7173
dynamic algorithm,
72-
html.CryptoKey baseKey,
74+
web.CryptoKey baseKey,
7375
dynamic derivedKeyAlgorithm,
7476
bool extractable,
7577
List<String> keyUsages);
7678
7779
@JS('crypto.subtle.deriveBits')
7880
external Promise<ByteBuffer> deriveBits(
7981
dynamic algorithm,
80-
html.CryptoKey baseKey,
82+
web.CryptoKey baseKey,
8183
int length,
8284
);
8385
84-
Future<html.CryptoKey> impportKeyFromRawData(List<int> secretKeyData,
86+
Future<web.CryptoKey> impportKeyFromRawData(List<int> secretKeyData,
8587
{required String webCryptoAlgorithm,
8688
required List<String> keyUsages}) async {
87-
return jsutil.promiseToFuture<html.CryptoKey>(importKey(
89+
return jsutil.promiseToFuture<web.CryptoKey>(importKey(
8890
'raw',
8991
jsArrayBufferFrom(secretKeyData),
9092
jsutil.jsify({'name': webCryptoAlgorithm}),
9193
false,
9294
keyUsages,
9395
));
9496
}
97+
98+
*/

lib/src/e2ee.worker/e2ee.cryptor.dart

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import 'dart:async';
2-
import 'dart:html';
2+
33
import 'dart:js';
4-
import 'dart:js_util' as jsutil;
4+
import 'dart:js_interop';
55
import 'dart:math';
66
import 'dart:typed_data';
77

88
import 'package:dart_webrtc/src/rtc_transform_stream.dart';
9-
import 'crypto.dart' as crypto;
9+
import 'package:js/js_util.dart' as jsutil;
10+
import 'package:web/web.dart' as web;
11+
1012
import 'e2ee.keyhandler.dart';
1113
import 'e2ee.logger.dart';
1214
import 'e2ee.sfi_guard.dart';
1315

16+
final crypto = web.window.crypto.subtle;
17+
1418
const IV_LENGTH = 12;
1519

1620
const kNaluTypeMask = 0x1f;
@@ -136,7 +140,7 @@ class FrameCryptor {
136140
bool _enabled = false;
137141
CryptorError lastError = CryptorError.kNew;
138142
int currentKeyIndex = 0;
139-
final DedicatedWorkerGlobalScope worker;
143+
final web.DedicatedWorkerGlobalScope worker;
140144
SifGuard sifGuard = SifGuard();
141145

142146
void setParticipant(String identity, ParticipantKeyHandler keys) {
@@ -219,7 +223,7 @@ class FrameCryptor {
219223
}
220224

221225
void postMessage(Object message) {
222-
worker.postMessage(message);
226+
worker.postMessage(jsutil.jsify(message));
223227
}
224228

225229
Future<void> setupTransform({
@@ -335,14 +339,13 @@ class FrameCryptor {
335339
frameTrailer.setInt8(1, keyIndex);
336340

337341
var cipherText = await jsutil.promiseToFuture<ByteBuffer>(crypto.encrypt(
338-
crypto.AesGcmParams(
339-
name: 'AES-GCM',
340-
iv: crypto.jsArrayBufferFrom(iv),
341-
additionalData:
342-
crypto.jsArrayBufferFrom(buffer.sublist(0, headerLength)),
342+
web.AesGcmParams(
343+
//name: 'AES-GCM',
344+
iv: iv.toJS,
345+
additionalData: buffer.sublist(0, headerLength).toJS,
343346
),
344347
secretKey,
345-
crypto.jsArrayBufferFrom(buffer.sublist(headerLength, buffer.length)),
348+
buffer.sublist(headerLength, buffer.length).toJS,
346349
));
347350

348351
logger.finer(
@@ -353,7 +356,7 @@ class FrameCryptor {
353356
finalBuffer.add(cipherText.asUint8List());
354357
finalBuffer.add(iv);
355358
finalBuffer.add(frameTrailer.buffer.asUint8List());
356-
frame.data = crypto.jsArrayBufferFrom(finalBuffer.toBytes());
359+
frame.data = finalBuffer.toBytes().buffer;
357360

358361
controller.enqueue(frame);
359362

@@ -422,7 +425,7 @@ class FrameCryptor {
422425
var finalBuffer = BytesBuilder();
423426
finalBuffer.add(Uint8List.fromList(
424427
buffer.sublist(0, buffer.length - (magicBytes.length + 1))));
425-
frame.data = crypto.jsArrayBufferFrom(finalBuffer.toBytes());
428+
frame.data = finalBuffer.toBytes().buffer;
426429
controller.enqueue(frame);
427430
} else {
428431
logger.finer('SIF limit reached, dropping frame');
@@ -468,15 +471,13 @@ class FrameCryptor {
468471
while (!endDecLoop) {
469472
try {
470473
decrypted = await jsutil.promiseToFuture<ByteBuffer>(crypto.decrypt(
471-
crypto.AesGcmParams(
472-
name: 'AES-GCM',
473-
iv: crypto.jsArrayBufferFrom(iv),
474-
additionalData:
475-
crypto.jsArrayBufferFrom(buffer.sublist(0, headerLength)),
474+
web.AesGcmParams(
475+
//name: 'AES-GCM',
476+
iv: iv.toJS,
477+
additionalData: buffer.sublist(0, headerLength).toJS,
476478
),
477479
currentkeySet.encryptionKey,
478-
crypto.jsArrayBufferFrom(
479-
buffer.sublist(headerLength, buffer.length - ivLength - 2)),
480+
buffer.sublist(headerLength, buffer.length - ivLength - 2).toJS,
480481
));
481482

482483
if (currentkeySet != initialKeySet) {
@@ -514,10 +515,10 @@ class FrameCryptor {
514515
if (endDecLoop) {
515516
rethrow;
516517
}
517-
var newKeyBuffer = crypto.jsArrayBufferFrom(await keyHandler.ratchet(
518-
currentkeySet.material, keyOptions.ratchetSalt));
518+
var newKeyBuffer = await keyHandler.ratchet(
519+
currentkeySet.material, keyOptions.ratchetSalt);
519520
var newMaterial = await keyHandler.ratchetMaterial(
520-
currentkeySet.material, newKeyBuffer);
521+
currentkeySet.material, newKeyBuffer.buffer);
521522
currentkeySet =
522523
await keyHandler.deriveKeys(newMaterial, keyOptions.ratchetSalt);
523524
ratchetCount++;
@@ -530,7 +531,7 @@ class FrameCryptor {
530531

531532
finalBuffer.add(Uint8List.fromList(buffer.sublist(0, headerLength)));
532533
finalBuffer.add(decrypted!.asUint8List());
533-
frame.data = crypto.jsArrayBufferFrom(finalBuffer.toBytes());
534+
frame.data = finalBuffer.toBytes().buffer;
534535
controller.enqueue(frame);
535536

536537
if (lastError != CryptorError.kOk) {

lib/src/e2ee.worker/e2ee.keyhandler.dart

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import 'dart:async';
2-
import 'dart:html';
2+
import 'dart:js_interop';
33
import 'dart:js_util' as jsutil;
44
import 'dart:typed_data';
55

6-
import 'crypto.dart' as crypto;
6+
import 'package:web/web.dart' as web;
7+
78
import 'e2ee.logger.dart';
89
import 'e2ee.utils.dart';
910

11+
final crypto = web.window.crypto.subtle;
12+
1013
class KeyOptions {
1114
KeyOptions({
1215
required this.sharedKey,
@@ -29,7 +32,7 @@ class KeyOptions {
2932

3033
class KeyProvider {
3134
KeyProvider(this.worker, this.id, this.keyProviderOptions);
32-
final DedicatedWorkerGlobalScope worker;
35+
final web.DedicatedWorkerGlobalScope worker;
3336
final String id;
3437
final KeyOptions keyProviderOptions;
3538
var participantKeys = <String, ParticipantKeyHandler>{};
@@ -80,8 +83,8 @@ const KEYRING_SIZE = 16;
8083

8184
class KeySet {
8285
KeySet(this.material, this.encryptionKey);
83-
CryptoKey material;
84-
CryptoKey encryptionKey;
86+
web.CryptoKey material;
87+
web.CryptoKey encryptionKey;
8588
}
8689

8790
class ParticipantKeyHandler {
@@ -100,7 +103,7 @@ class ParticipantKeyHandler {
100103

101104
final KeyOptions keyOptions;
102105

103-
final DedicatedWorkerGlobalScope worker;
106+
final web.DedicatedWorkerGlobalScope worker;
104107

105108
final String participantIdentity;
106109

@@ -150,22 +153,23 @@ class ParticipantKeyHandler {
150153
return null;
151154
}
152155
var newKey = await ratchet(currentMaterial, keyOptions.ratchetSalt);
153-
var newMaterial = await ratchetMaterial(
154-
currentMaterial, crypto.jsArrayBufferFrom(newKey));
156+
var newMaterial = await ratchetMaterial(currentMaterial, newKey.buffer);
155157
var newKeySet = await deriveKeys(newMaterial, keyOptions.ratchetSalt);
156158
await setKeySetFromMaterial(newKeySet, keyIndex ?? currentKeyIndex);
157159
return newKey;
158160
}
159161

160-
Future<CryptoKey> ratchetMaterial(
161-
CryptoKey currentMaterial, ByteBuffer newKeyBuffer) async {
162-
var newMaterial = await jsutil.promiseToFuture(crypto.importKey(
163-
'raw',
164-
newKeyBuffer,
165-
(currentMaterial.algorithm as crypto.Algorithm).name,
166-
false,
167-
['deriveBits', 'deriveKey'],
168-
));
162+
Future<web.CryptoKey> ratchetMaterial(
163+
web.CryptoKey currentMaterial, ByteBuffer newKeyBuffer) async {
164+
var newMaterial = await crypto
165+
.importKey(
166+
'raw',
167+
newKeyBuffer.toJS,
168+
(currentMaterial.algorithm as web.Algorithm).name.toJS,
169+
false,
170+
<JSString>['deriveBits'.toJS, 'deriveKey'.toJS].toJS,
171+
)
172+
.toDart;
169173
return newMaterial;
170174
}
171175

@@ -174,8 +178,11 @@ class ParticipantKeyHandler {
174178
}
175179

176180
Future<void> setKey(Uint8List key, {int keyIndex = 0}) async {
177-
var keyMaterial = await crypto.impportKeyFromRawData(key,
178-
webCryptoAlgorithm: 'PBKDF2', keyUsages: ['deriveBits', 'deriveKey']);
181+
var keyMaterial = await crypto
182+
.importKey('raw', key.toJS, 'PBKDF2'.toJS, false,
183+
<JSString>['deriveBits'.toJS, 'deriveKey'.toJS].toJS)
184+
.toDart;
185+
179186
var keySet = await deriveKeys(
180187
keyMaterial,
181188
keyOptions.ratchetSalt,
@@ -194,19 +201,19 @@ class ParticipantKeyHandler {
194201

195202
/// Derives a set of keys from the master key.
196203
/// See https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.1
197-
Future<KeySet> deriveKeys(CryptoKey material, Uint8List salt) async {
204+
Future<KeySet> deriveKeys(web.CryptoKey material, Uint8List salt) async {
198205
var algorithmOptions =
199-
getAlgoOptions((material.algorithm as crypto.Algorithm).name, salt);
206+
getAlgoOptions((material.algorithm as web.Algorithm).name, salt);
200207

201208
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#HKDF
202209
// https://developer.mozilla.org/en-US/docs/Web/API/HkdfParams
203210
var encryptionKey =
204-
await jsutil.promiseToFuture<CryptoKey>(crypto.deriveKey(
211+
await jsutil.promiseToFuture<web.CryptoKey>(crypto.deriveKey(
205212
jsutil.jsify(algorithmOptions),
206213
material,
207214
jsutil.jsify({'name': 'AES-GCM', 'length': 128}),
208215
false,
209-
['encrypt', 'decrypt'],
216+
<JSString>['encrypt'.toJS, 'decrypt'.toJS].toJS,
210217
));
211218

212219
return KeySet(material, encryptionKey);
@@ -215,7 +222,7 @@ class ParticipantKeyHandler {
215222
/// Ratchets a key. See
216223
/// https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.5.1
217224
218-
Future<Uint8List> ratchet(CryptoKey material, Uint8List salt) async {
225+
Future<Uint8List> ratchet(web.CryptoKey material, Uint8List salt) async {
219226
var algorithmOptions = getAlgoOptions('PBKDF2', salt);
220227

221228
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits

0 commit comments

Comments
 (0)