Skip to content

Commit c3b95f9

Browse files
authored
Merge pull request #26 from flutter-webrtc/fix/skip-decryption-when-ratchet-exceeded
fix: Skip decryption when ratchet exceeded.
2 parents 2f8cdc3 + 014f147 commit c3b95f9

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class FrameCryptor {
159159
CryptorError lastError = CryptorError.kNew;
160160
final DedicatedWorkerGlobalScope worker;
161161
int currentKeyIndex = 0;
162-
162+
bool hasValidKey = false;
163163
Completer? _ratchetCompleter;
164164

165165
List<KeySet?> cryptoKeyRing = List.filled(KEYRING_SIZE, null);
@@ -242,6 +242,7 @@ class FrameCryptor {
242242
keyOptions.ratchetSalt,
243243
);
244244
await setKeySetFromMaterial(keySet, keyIndex);
245+
hasValidKey = true;
245246
}
246247

247248
Future<void> setKeySetFromMaterial(KeySet keySet, int keyIndex) async {
@@ -499,10 +500,13 @@ class FrameCryptor {
499500

500501
if (keyOptions.uncryptedMagicBytes != null) {
501502
var magicBytes = keyOptions.uncryptedMagicBytes!;
502-
if (buffer.length >= magicBytes.length + 1) {
503+
if (buffer.length > magicBytes.length + 1) {
503504
var magicBytesBuffer = buffer.sublist(
504-
buffer.length - (magicBytes.length + 1), magicBytes.length);
505+
buffer.length - magicBytes.length - 1, buffer.length - 1);
506+
//print('magicBytesBuffer $magicBytesBuffer, magicBytes $magicBytes, ');
505507
if (magicBytesBuffer.toString() == magicBytes.toString()) {
508+
var frameType = buffer.sublist(buffer.length - 1)[0];
509+
print('skip uncrypted frame, type $frameType');
506510
var finalBuffer = BytesBuilder();
507511
finalBuffer.add(Uint8List.fromList(
508512
buffer.sublist(0, buffer.length - (magicBytes.length + 1))));
@@ -526,7 +530,7 @@ class FrameCryptor {
526530
var initialKeySet = getKeySet(keyIndex);
527531
initialKeyIndex = keyIndex;
528532

529-
if (initialKeySet == null) {
533+
if (initialKeySet == null || !hasValidKey) {
530534
if (lastError != CryptorError.kMissingKey) {
531535
lastError = CryptorError.kMissingKey;
532536
postMessage({
@@ -635,6 +639,7 @@ class FrameCryptor {
635639
/// yet and ratcheting, of course, did not solve the problem. So if we fail RATCHET_WINDOW_SIZE times,
636640
/// we come back to the initial key.
637641
await setKeySetFromMaterial(initialKeySet!, initialKeyIndex);
642+
hasValidKey = false;
638643
}
639644
}
640645
}

0 commit comments

Comments
 (0)