Skip to content

Commit

Permalink
fix: use latest key index for new rtp nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
td-famedly committed Mar 4, 2024
1 parent 79271a5 commit b978670
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
15 changes: 7 additions & 8 deletions lib/src/e2ee/e2ee_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ import 'package:flutter/foundation.dart';

import 'package:flutter_webrtc/flutter_webrtc.dart';

import '../core/room.dart';
import '../e2ee/events.dart';
import '../e2ee/options.dart';
import '../events.dart';
import 'package:livekit_client/livekit_client.dart';
import '../extensions.dart';
import '../managers/event.dart';
import '../utils.dart';
import 'key_provider.dart';

class E2EEManager {
Room? _room;
Expand Down Expand Up @@ -156,7 +151,9 @@ class E2EEManager {
keyProvider: _keyProvider.keyProvider);
_frameCryptors[{identity: sid}] = frameCryptor;
await frameCryptor.setEnabled(_enabled);
await frameCryptor.setKeyIndex(0);
logger.info(
'_addRtpSender, setKeyIndex: ${_keyProvider.getLatestIndex(identity)}');
await frameCryptor.setKeyIndex(_keyProvider.getLatestIndex(identity));
return frameCryptor;
}

Expand All @@ -172,7 +169,9 @@ class E2EEManager {
keyProvider: _keyProvider.keyProvider);
_frameCryptors[{identity: sid}] = frameCryptor;
await frameCryptor.setEnabled(_enabled);
await frameCryptor.setKeyIndex(0);
logger.info(
'_addRtpReceiver, setKeyIndex: ${_keyProvider.getLatestIndex(identity)}');
await frameCryptor.setKeyIndex(_keyProvider.getLatestIndex(identity));
return frameCryptor;
}

Expand Down
10 changes: 10 additions & 0 deletions lib/src/e2ee/key_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;

import 'package:livekit_client/livekit_client.dart';

const defaultRatchetSalt = 'LKFrameEncryptionKey';
const defaultMagicBytes = 'LK-ROCKS';
const defaultRatchetWindowSize = 16;
Expand Down Expand Up @@ -45,6 +48,11 @@ abstract class KeyProvider {

class BaseKeyProvider implements KeyProvider {
final Map<String, Map<int, Uint8List>> _keys = {};

int getLatestIndex(String participantId) {
return _keys[participantId]?.keys.last ?? 0;
}

Uint8List? _sharedKey;
final rtc.KeyProviderOptions options;
final rtc.KeyProvider _keyProvider;
Expand Down Expand Up @@ -127,6 +135,8 @@ class BaseKeyProvider implements KeyProvider {
if (!_keys.containsKey(keyInfo.participantId)) {
_keys[keyInfo.participantId] = {};
}
logger.info(
'_setKey for ${keyInfo.participantId}, idx: ${keyInfo.keyIndex}, key: ${base64Encode(keyInfo.key)}');
_keys[keyInfo.participantId]![keyInfo.keyIndex] = keyInfo.key;
await _keyProvider.setKey(
participantId: keyInfo.participantId,
Expand Down
27 changes: 21 additions & 6 deletions web/e2ee.cryptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// ignore_for_file: constant_identifier_names

import 'dart:async';
import 'dart:html';
import 'dart:js';
Expand Down Expand Up @@ -331,6 +333,8 @@ class FrameCryptor {
'kind': kind,
'state': 'missingKey',
'error': 'Missing key for track $trackId',
'currentKeyIndex': currentKeyIndex,
'secretKey': secretKey.toString()
});
}
return;
Expand Down Expand Up @@ -380,7 +384,10 @@ class FrameCryptor {
'trackId': trackId,
'kind': kind,
'state': 'ok',
'error': 'encryption ok'
'error': 'encryption ok',
'frameTrailer': frameTrailer.buffer.asUint8List(),
'currentKeyIndex': currentKeyIndex,
'secretKey': secretKey.toString(),
});
}

Expand Down Expand Up @@ -471,7 +478,10 @@ class FrameCryptor {
'trackId': trackId,
'kind': kind,
'state': 'missingKey',
'error': 'Missing key for track $trackId'
'error': 'Missing key for track $trackId',
'frameTrailer': frameTrailer.buffer.asUint8List(),
'currentKeyIndex': keyIndex,
'secretKey': initialKeySet?.encryptionKey.toString()
});
}
controller.enqueue(frame);
Expand Down Expand Up @@ -518,7 +528,10 @@ class FrameCryptor {
'trackId': trackId,
'kind': kind,
'state': 'keyRatcheted',
'error': 'Key ratcheted ok'
'error': 'Key ratcheted ok',
'frameTrailer': frameTrailer.buffer.asUint8List(),
'currentKeyIndex': currentKeyIndex,
'secretKey': currentkeySet.encryptionKey.toString()
});
}
} catch (e) {
Expand All @@ -539,9 +552,8 @@ class FrameCryptor {
}

logger.finer(
'buffer: ${buffer.length}, decrypted: ${decrypted?.asUint8List()?.length ?? 0}');
'buffer: ${buffer.length}, decrypted: ${decrypted?.asUint8List().length ?? 0}');
var finalBuffer = BytesBuilder();

finalBuffer.add(Uint8List.fromList(buffer.sublist(0, headerLength)));
finalBuffer.add(decrypted!.asUint8List());
frame.data = crypto.jsArrayBufferFrom(finalBuffer.toBytes());
Expand All @@ -556,7 +568,10 @@ class FrameCryptor {
'trackId': trackId,
'kind': kind,
'state': 'ok',
'error': 'decryption ok'
'error': 'decryption ok',
'frameTrailer': frameTrailer.buffer.asUint8List(),
'currentKeyIndex': currentKeyIndex,
'secretKey': currentkeySet.encryptionKey.toString()
});
}

Expand Down
1 change: 1 addition & 0 deletions web/e2ee.keyhandler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class ParticipantKeyHandler {
currentKeyIndex = keyIndex % cryptoKeyRing.length;
}
cryptoKeyRing[currentKeyIndex] = keySet;
logger.config('setKeySetFromMaterial: currentIndex: $currentKeyIndex');
}

/// Derives a set of keys from the master key.
Expand Down

0 comments on commit b978670

Please sign in to comment.