@@ -159,7 +159,7 @@ class FrameCryptor {
159
159
CryptorError lastError = CryptorError .kNew;
160
160
final DedicatedWorkerGlobalScope worker;
161
161
int currentKeyIndex = 0 ;
162
-
162
+ bool hasValidKey = false ;
163
163
Completer ? _ratchetCompleter;
164
164
165
165
List <KeySet ?> cryptoKeyRing = List .filled (KEYRING_SIZE , null );
@@ -242,6 +242,7 @@ class FrameCryptor {
242
242
keyOptions.ratchetSalt,
243
243
);
244
244
await setKeySetFromMaterial (keySet, keyIndex);
245
+ hasValidKey = true ;
245
246
}
246
247
247
248
Future <void > setKeySetFromMaterial (KeySet keySet, int keyIndex) async {
@@ -499,10 +500,13 @@ class FrameCryptor {
499
500
500
501
if (keyOptions.uncryptedMagicBytes != null ) {
501
502
var magicBytes = keyOptions.uncryptedMagicBytes! ;
502
- if (buffer.length >= magicBytes.length + 1 ) {
503
+ if (buffer.length > magicBytes.length + 1 ) {
503
504
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, ');
505
507
if (magicBytesBuffer.toString () == magicBytes.toString ()) {
508
+ var frameType = buffer.sublist (buffer.length - 1 )[0 ];
509
+ print ('skip uncrypted frame, type $frameType ' );
506
510
var finalBuffer = BytesBuilder ();
507
511
finalBuffer.add (Uint8List .fromList (
508
512
buffer.sublist (0 , buffer.length - (magicBytes.length + 1 ))));
@@ -526,7 +530,7 @@ class FrameCryptor {
526
530
var initialKeySet = getKeySet (keyIndex);
527
531
initialKeyIndex = keyIndex;
528
532
529
- if (initialKeySet == null ) {
533
+ if (initialKeySet == null || ! hasValidKey ) {
530
534
if (lastError != CryptorError .kMissingKey) {
531
535
lastError = CryptorError .kMissingKey;
532
536
postMessage ({
@@ -635,6 +639,7 @@ class FrameCryptor {
635
639
/// yet and ratcheting, of course, did not solve the problem. So if we fail RATCHET_WINDOW_SIZE times,
636
640
/// we come back to the initial key.
637
641
await setKeySetFromMaterial (initialKeySet! , initialKeyIndex);
642
+ hasValidKey = false ;
638
643
}
639
644
}
640
645
}
0 commit comments