Skip to content

Commit 90b3f7e

Browse files
committed
revert changes.
1 parent 0dad409 commit 90b3f7e

6 files changed

Lines changed: 155 additions & 154 deletions

File tree

web/e2ee.data_packet_cryptor.dart

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ class E2EEDataPacketCryptor {
5050
}
5151

5252
Uint8List makeIv({required int timestamp}) {
53-
var iv = ByteData(IV_LENGTH);
53+
final iv = ByteData(IV_LENGTH);
5454

5555
// having to keep our own send count (similar to a picture id) is not ideal.
5656
if (sendCount_ == -1) {
5757
// Initialize with a random offset, similar to the RTP sequence number.
5858
sendCount_ = Random.secure().nextInt(0xffff);
5959
}
6060

61-
var sendCount = sendCount_;
61+
final sendCount = sendCount_;
6262
final randomBytes =
6363
Random.secure().nextInt(max(0, 0xffffffff)).toUnsigned(32);
6464

@@ -81,23 +81,23 @@ class E2EEDataPacketCryptor {
8181
) async {
8282
logger.fine('encodeFunction: buffer ${data.length}');
8383

84-
var secretKey = keyHandler.getKeySet(currentKeyIndex)?.encryptionKey;
85-
var keyIndex = currentKeyIndex;
84+
final secretKey = keyHandler.getKeySet(currentKeyIndex)?.encryptionKey;
85+
final keyIndex = currentKeyIndex;
8686

8787
if (secretKey == null) {
8888
logger.warning(
8989
'encodeFunction: no secretKey for index $keyIndex, cannot encrypt');
9090
return null;
9191
}
9292

93-
var iv = makeIv(timestamp: DateTime.timestamp().millisecondsSinceEpoch);
93+
final iv = makeIv(timestamp: DateTime.timestamp().millisecondsSinceEpoch);
9494

95-
var frameTrailer = ByteData(2);
95+
final frameTrailer = ByteData(2);
9696
frameTrailer.setInt8(0, IV_LENGTH);
9797
frameTrailer.setInt8(1, keyIndex);
9898

9999
try {
100-
var cipherText = await worker.crypto.subtle
100+
final cipherText = await worker.crypto.subtle
101101
.encrypt(
102102
{
103103
'name': 'AES-GCM',
@@ -133,13 +133,13 @@ class E2EEDataPacketCryptor {
133133

134134
ByteBuffer? decrypted;
135135
KeySet? initialKeySet;
136-
var initialKeyIndex = currentKeyIndex;
136+
final initialKeyIndex = currentKeyIndex;
137137

138138
try {
139-
var ivLength = encryptedPacket.iv.length;
140-
var keyIndex = encryptedPacket.keyIndex;
141-
var iv = encryptedPacket.iv;
142-
var payload = encryptedPacket.data;
139+
final ivLength = encryptedPacket.iv.length;
140+
final keyIndex = encryptedPacket.keyIndex;
141+
final iv = encryptedPacket.iv;
142+
final payload = encryptedPacket.data;
143143
initialKeySet = keyHandler.getKeySet(initialKeyIndex);
144144

145145
logger.finer(
@@ -190,9 +190,9 @@ class E2EEDataPacketCryptor {
190190
throw Exception('[ratchedKeyInternal] cannot ratchet anymore');
191191
}
192192

193-
var newKeyBuffer = await keyHandler.ratchet(
193+
final newKeyBuffer = await keyHandler.ratchet(
194194
currentkeySet.material, keyOptions.ratchetSalt);
195-
var newMaterial = await keyHandler.ratchetMaterial(
195+
final newMaterial = await keyHandler.ratchetMaterial(
196196
currentkeySet.material, newKeyBuffer.buffer);
197197
currentkeySet =
198198
await keyHandler.deriveKeys(newMaterial, keyOptions.ratchetSalt);

web/e2ee.frame_cryptor.dart

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import 'dart:js_interop_unsafe';
44
import 'dart:math';
55
import 'dart:typed_data';
66

7-
// ignore: deprecated_member_use
8-
import 'package:js/js.dart';
97
import 'package:web/web.dart' as web;
108
import 'e2ee.keyhandler.dart';
119
import 'e2ee.logger.dart';
@@ -75,7 +73,7 @@ const SLICE_LAYER_EXT = 21;
7573
// 22, 23 reserved
7674

7775
List<int> findNALUIndices(Uint8List stream) {
78-
var result = <int>[];
76+
final result = <int>[];
7977
var start = 0, pos = 0, searchLength = stream.length - 2;
8078
while (pos < searchLength) {
8179
// skip until end of current NALU
@@ -210,15 +208,15 @@ class FrameCryptor {
210208

211209
Uint8List makeIv(
212210
{required int synchronizationSource, required int timestamp}) {
213-
var iv = ByteData(IV_LENGTH);
211+
final iv = ByteData(IV_LENGTH);
214212

215213
// having to keep our own send count (similar to a picture id) is not ideal.
216214
if (sendCounts[synchronizationSource] == null) {
217215
// Initialize with a random offset, similar to the RTP sequence number.
218216
sendCounts[synchronizationSource] = Random.secure().nextInt(0xffff);
219217
}
220218

221-
var sendCount = sendCounts[synchronizationSource] ?? 0;
219+
final sendCount = sendCounts[synchronizationSource] ?? 0;
222220

223221
iv.setUint32(0, synchronizationSource);
224222
iv.setUint32(4, timestamp);
@@ -247,9 +245,9 @@ class FrameCryptor {
247245
logger.info('setting codec on cryptor to $codec');
248246
this.codec = codec;
249247
}
250-
var transformer = web.TransformStream({
248+
final transformer = web.TransformStream({
251249
'transform':
252-
allowInterop(operation == 'encode' ? encodeFunction : decodeFunction)
250+
(operation == 'encode' ? encodeFunction.toJS : decodeFunction.toJS)
253251
}.jsify() as JSObject);
254252
try {
255253
readable
@@ -283,9 +281,9 @@ class FrameCryptor {
283281
}
284282

285283
if (codec != null && codec.toLowerCase() == 'h264') {
286-
var naluIndices = findNALUIndices(data);
284+
final naluIndices = findNALUIndices(data);
287285
for (var index in naluIndices) {
288-
var type = parseNALUType(data[index]);
286+
final type = parseNALUType(data[index]);
289287
switch (type) {
290288
case SLICE_IDR:
291289
case SLICE_NON_IDR:
@@ -364,7 +362,7 @@ class FrameCryptor {
364362
controller.enqueue(frameObj);
365363
}
366364

367-
Future<void> encodeFunction(
365+
void encodeFunction(
368366
JSObject frameObj,
369367
web.TransformStreamDefaultController controller,
370368
) async {
@@ -382,13 +380,13 @@ class FrameCryptor {
382380
return;
383381
}
384382

385-
var srcFrame = readFrameInfo(frameObj);
383+
final srcFrame = readFrameInfo(frameObj);
386384

387385
logger.fine(
388386
'encodeFunction: buffer ${srcFrame.buffer.length}, synchronizationSource ${srcFrame.ssrc} frameType ${srcFrame.frameType}');
389387

390-
var secretKey = keyHandler.getKeySet(currentKeyIndex)?.encryptionKey;
391-
var keyIndex = currentKeyIndex;
388+
final secretKey = keyHandler.getKeySet(currentKeyIndex)?.encryptionKey;
389+
final keyIndex = currentKeyIndex;
392390

393391
if (secretKey == null) {
394392
if (lastError != CryptorError.kMissingKey) {
@@ -406,17 +404,17 @@ class FrameCryptor {
406404
return;
407405
}
408406

409-
var headerLength =
407+
final headerLength =
410408
kind == 'video' ? getUnencryptedBytes(frameObj, codec) : 1;
411409

412-
var iv = makeIv(
410+
final iv = makeIv(
413411
synchronizationSource: srcFrame.ssrc, timestamp: srcFrame.timestamp);
414412

415-
var frameTrailer = ByteData(2);
413+
final frameTrailer = ByteData(2);
416414
frameTrailer.setInt8(0, IV_LENGTH);
417415
frameTrailer.setInt8(1, keyIndex);
418416

419-
var cipherText = await worker.crypto.subtle
417+
final cipherText = await worker.crypto.subtle
420418
.encrypt(
421419
{
422420
'name': 'AES-GCM',
@@ -430,7 +428,7 @@ class FrameCryptor {
430428

431429
logger.finer(
432430
'encodeFunction: encrypted buffer: ${srcFrame.buffer.length}, cipherText: ${cipherText.toDart.asUint8List().length}');
433-
var finalBuffer = BytesBuilder();
431+
final finalBuffer = BytesBuilder();
434432

435433
finalBuffer
436434
.add(Uint8List.fromList(srcFrame.buffer.sublist(0, headerLength)));
@@ -472,11 +470,11 @@ class FrameCryptor {
472470
}
473471
}
474472

475-
Future<void> decodeFunction(
473+
void decodeFunction(
476474
JSObject frameObj,
477475
web.TransformStreamDefaultController controller,
478476
) async {
479-
var srcFrame = readFrameInfo(frameObj);
477+
final srcFrame = readFrameInfo(frameObj);
480478
var ratchetCount = 0;
481479

482480
logger.fine('decodeFunction: frame lenght ${srcFrame.buffer.length}');
@@ -497,21 +495,21 @@ class FrameCryptor {
497495
}
498496

499497
if (keyOptions.uncryptedMagicBytes != null) {
500-
var magicBytes = keyOptions.uncryptedMagicBytes!;
498+
final magicBytes = keyOptions.uncryptedMagicBytes!;
501499
if (srcFrame.buffer.length > magicBytes.length + 1) {
502-
var magicBytesBuffer = srcFrame.buffer.sublist(
500+
final magicBytesBuffer = srcFrame.buffer.sublist(
503501
srcFrame.buffer.length - magicBytes.length - 1,
504502
srcFrame.buffer.length - 1);
505503
logger.finer(
506504
'magicBytesBuffer $magicBytesBuffer, magicBytes $magicBytes');
507505
if (magicBytesBuffer.toString() == magicBytes.toString()) {
508506
sifGuard.recordSif();
509507
if (sifGuard.isSifAllowed()) {
510-
var frameType =
508+
final frameType =
511509
srcFrame.buffer.sublist(srcFrame.buffer.length - 1)[0];
512510
logger
513511
.finer('ecodeFunction: skip uncrypted frame, type $frameType');
514-
var finalBuffer = BytesBuilder();
512+
final finalBuffer = BytesBuilder();
515513
finalBuffer.add(Uint8List.fromList(srcFrame.buffer
516514
.sublist(0, srcFrame.buffer.length - (magicBytes.length + 1))));
517515
enqueueFrame(frameObj, controller, finalBuffer);
@@ -530,13 +528,13 @@ class FrameCryptor {
530528
}
531529

532530
try {
533-
var headerLength =
531+
final headerLength =
534532
kind == 'video' ? getUnencryptedBytes(frameObj, codec) : 1;
535533

536-
var frameTrailer = srcFrame.buffer.sublist(srcFrame.buffer.length - 2);
537-
var ivLength = frameTrailer[0];
538-
var keyIndex = frameTrailer[1];
539-
var iv = srcFrame.buffer.sublist(
534+
final frameTrailer = srcFrame.buffer.sublist(srcFrame.buffer.length - 2);
535+
final ivLength = frameTrailer[0];
536+
final keyIndex = frameTrailer[1];
537+
final iv = srcFrame.buffer.sublist(
540538
srcFrame.buffer.length - ivLength - 2, srcFrame.buffer.length - 2);
541539

542540
initialKeySet = keyHandler.getKeySet(keyIndex);
@@ -627,9 +625,9 @@ class FrameCryptor {
627625
throw Exception('[ratchedKeyInternal] cannot ratchet anymore');
628626
}
629627

630-
var newKeyBuffer = await keyHandler.ratchet(
628+
final newKeyBuffer = await keyHandler.ratchet(
631629
currentkeySet.material, keyOptions.ratchetSalt);
632-
var newMaterial = await keyHandler.ratchetMaterial(
630+
final newMaterial = await keyHandler.ratchetMaterial(
633631
currentkeySet.material, newKeyBuffer.buffer);
634632
currentkeySet =
635633
await keyHandler.deriveKeys(newMaterial, keyOptions.ratchetSalt);
@@ -659,7 +657,7 @@ class FrameCryptor {
659657
logger.finer(
660658
'decodeFunction: decryption success, buffer length ${srcFrame.buffer.length}, decrypted: ${decrypted!.asUint8List().length}');
661659

662-
var finalBuffer = BytesBuilder();
660+
final finalBuffer = BytesBuilder();
663661

664662
finalBuffer
665663
.add(Uint8List.fromList(srcFrame.buffer.sublist(0, headerLength)));

web/e2ee.keyhandler.dart

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ class ParticipantKeyHandler {
141141
}
142142

143143
Future<Uint8List?> exportKey(int? keyIndex) async {
144-
var currentMaterial = getKeySet(keyIndex)?.material;
144+
final currentMaterial = getKeySet(keyIndex)?.material;
145145
if (currentMaterial == null) {
146146
return null;
147147
}
148148
try {
149-
var key = await worker.crypto.subtle
149+
final key = await worker.crypto.subtle
150150
.exportKey('raw', currentMaterial)
151151
.toDart as JSArrayBuffer;
152152
return key.toDart.asUint8List();
@@ -157,20 +157,20 @@ class ParticipantKeyHandler {
157157
}
158158

159159
Future<Uint8List?> ratchetKey(int? keyIndex) async {
160-
var currentMaterial = getKeySet(keyIndex)?.material;
160+
final currentMaterial = getKeySet(keyIndex)?.material;
161161
if (currentMaterial == null) {
162162
return null;
163163
}
164-
var newKey = await ratchet(currentMaterial, keyOptions.ratchetSalt);
165-
var newMaterial = await ratchetMaterial(currentMaterial, newKey.buffer);
166-
var newKeySet = await deriveKeys(newMaterial, keyOptions.ratchetSalt);
164+
final newKey = await ratchet(currentMaterial, keyOptions.ratchetSalt);
165+
final newMaterial = await ratchetMaterial(currentMaterial, newKey.buffer);
166+
final newKeySet = await deriveKeys(newMaterial, keyOptions.ratchetSalt);
167167
await setKeySetFromMaterial(newKeySet, keyIndex ?? currentKeyIndex);
168168
return newKey;
169169
}
170170

171171
Future<web.CryptoKey> ratchetMaterial(
172172
web.CryptoKey currentMaterial, ByteBuffer newKeyBuffer) async {
173-
var newMaterial = await worker.crypto.subtle
173+
final newMaterial = await worker.crypto.subtle
174174
.importKey(
175175
'raw',
176176
newKeyBuffer.toJS,
@@ -187,12 +187,12 @@ class ParticipantKeyHandler {
187187
}
188188

189189
Future<void> setKey(Uint8List key, {int keyIndex = 0}) async {
190-
var keyMaterial = await worker.crypto.subtle
190+
final keyMaterial = await worker.crypto.subtle
191191
.importKey('raw', key.toJS, {'name': 'PBKDF2'.toJS}.jsify() as JSAny,
192192
false, ['deriveBits', 'deriveKey'].jsify() as JSArray<JSString>)
193193
.toDart;
194194

195-
var keySet = await deriveKeys(
195+
final keySet = await deriveKeys(
196196
keyMaterial,
197197
keyOptions.ratchetSalt,
198198
);
@@ -211,11 +211,12 @@ class ParticipantKeyHandler {
211211
/// Derives a set of keys from the master key.
212212
/// See https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.1
213213
Future<KeySet> deriveKeys(web.CryptoKey material, Uint8List salt) async {
214-
var algorithmName = material.algorithm.getProperty('name'.toJS) as JSString;
215-
var algorithmOptions = getAlgoOptions(algorithmName.toDart, salt);
214+
final algorithmName =
215+
material.algorithm.getProperty('name'.toJS) as JSString;
216+
final algorithmOptions = getAlgoOptions(algorithmName.toDart, salt);
216217
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#HKDF
217218
// https://developer.mozilla.org/en-US/docs/Web/API/HkdfParams
218-
var encryptionKey = await worker.crypto.subtle
219+
final encryptionKey = await worker.crypto.subtle
219220
.deriveKey(
220221
algorithmOptions.jsify() as web.AlgorithmIdentifier,
221222
material,
@@ -232,10 +233,10 @@ class ParticipantKeyHandler {
232233
/// https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.5.1
233234
234235
Future<Uint8List> ratchet(web.CryptoKey material, Uint8List salt) async {
235-
var algorithmOptions = getAlgoOptions('PBKDF2', salt);
236+
final algorithmOptions = getAlgoOptions('PBKDF2', salt);
236237

237238
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits
238-
var newKey = await worker.crypto.subtle
239+
final newKey = await worker.crypto.subtle
239240
.deriveBits(
240241
algorithmOptions.jsify() as web.AlgorithmIdentifier, material, 256)
241242
.toDart;

web/e2ee.sfi_guard.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class SifGuard {
1212

1313
void recordSif() {
1414
consecutiveSifCount += 1;
15-
sifSequenceStartedAt ??= DateTime.now().millisecondsSinceEpoch;
16-
lastSifReceivedAt = DateTime.now().millisecondsSinceEpoch;
15+
sifSequenceStartedAt ??= DateTime.timestamp().millisecondsSinceEpoch;
16+
lastSifReceivedAt = DateTime.timestamp().millisecondsSinceEpoch;
1717
}
1818

1919
void recordUserFrame() {
@@ -26,7 +26,7 @@ class SifGuard {
2626
// reset if we received more user frames than SIFs
2727
userFramesSinceSif > consecutiveSifCount ||
2828
// also reset if we got a new user frame and the latest SIF frame hasn't been updated in a while
29-
DateTime.now().millisecondsSinceEpoch - lastSifReceivedAt >
29+
DateTime.timestamp().millisecondsSinceEpoch - lastSifReceivedAt >
3030
MAX_SIF_DURATION) {
3131
reset();
3232
}

0 commit comments

Comments
 (0)