-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathe2ee_manager.dart
More file actions
274 lines (254 loc) · 10.5 KB
/
e2ee_manager.dart
File metadata and controls
274 lines (254 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2024 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:typed_data' show Uint8List;
import 'package:flutter/foundation.dart' show kDebugMode, kIsWeb;
import 'package:flutter_webrtc/flutter_webrtc.dart';
import '../core/room.dart';
import '../events.dart';
import '../extensions.dart';
import '../logger.dart';
import '../managers/event.dart';
import '../utils.dart';
import 'events.dart';
import 'key_provider.dart';
import 'options.dart';
class E2EEManager {
Room? _room;
final Map<Map<String, String>, FrameCryptor> _frameCryptors = {};
final E2EEOptions _options;
BaseKeyProvider? _keyProvider;
final Algorithm _algorithm = Algorithm.kAesGcm;
DataPacketCryptor? _dataPacketCryptor;
bool _enabled = true;
bool _encryptionEnabled = false;
EventsListener<RoomEvent>? _listener;
E2EEManager({
required E2EEOptions options,
bool dcEncryptionEnabled = false,
}) : _options = options {
_encryptionEnabled = dcEncryptionEnabled;
}
static Future<BaseKeyProvider> _buildSharedKeyProvider(String key) async {
final provider = await BaseKeyProvider.create();
await provider.setSharedKey(key);
return provider;
}
Future<void> setup(Room room) async {
_keyProvider ??= _options.keyProviderOrNull ?? await _buildSharedKeyProvider(_options.sharedKey!);
if (_room != room) {
await _cleanUp();
_room = room;
_listener = _room!.createListener();
_listener!
..on<LocalTrackPublishedEvent>((event) async {
if (event.publication.encryptionType == EncryptionType.kNone ||
isAV1Codec(event.publication.track?.codec ?? '')) {
// no need to setup frame cryptor
return;
}
final frameCryptor = await _addRtpSender(
sender: event.publication.track!.sender!,
identity: event.participant.identity,
sid: event.publication.sid);
if (kIsWeb && event.publication.track!.codec != null) {
await frameCryptor.updateCodec(event.publication.track!.codec!);
}
frameCryptor.onFrameCryptorStateChanged = (trackId, state) {
if (kDebugMode) {
print('Sender::onFrameCryptorStateChanged: $state, trackId: $trackId');
}
final participant = event.participant;
[event.participant.events, participant.room.events].emit(TrackE2EEStateEvent(
participant: participant,
publication: event.publication,
state: _e2eeStateFromFrameCryptoState(state),
));
};
})
..on<LocalTrackUnpublishedEvent>((event) async {
for (var key in _frameCryptors.keys.toList()) {
if (key.keys.first == event.participant.identity && key.values.first == event.publication.sid) {
final frameCryptor = _frameCryptors.remove(key);
await frameCryptor?.setEnabled(false);
await frameCryptor?.dispose();
}
}
})
..on<TrackSubscribedEvent>((event) async {
final codec = event.publication.mimeType.split('/')[1];
if (event.publication.encryptionType == EncryptionType.kNone || isAV1Codec(codec)) {
// no need to setup frame cryptor
return;
}
final frameCryptor = await _addRtpReceiver(
receiver: event.track.receiver!,
identity: event.participant.identity,
sid: event.publication.sid,
);
if (kIsWeb) {
await frameCryptor.updateCodec(codec.toLowerCase());
}
frameCryptor.onFrameCryptorStateChanged = (trackId, state) {
if (kDebugMode) {
print('Receiver::onFrameCryptorStateChanged: $state, trackId: $trackId');
}
final participant = event.participant;
[event.participant.events, participant.room.events].emit(TrackE2EEStateEvent(
participant: participant,
publication: event.publication,
state: _e2eeStateFromFrameCryptoState(state),
));
};
})
..on<TrackUnsubscribedEvent>((event) async {
for (var key in _frameCryptors.keys.toList()) {
if (key.keys.first == event.participant.identity && key.values.first == event.publication.sid) {
final frameCryptor = _frameCryptors.remove(key);
await frameCryptor?.setEnabled(false);
await frameCryptor?.dispose();
}
}
});
_dataPacketCryptor ??= await dataPacketCryptorFactory.createDataPacketCryptor(
algorithm: _algorithm, keyProvider: _keyProvider!.keyProvider);
}
}
BaseKeyProvider get keyProvider => _keyProvider!;
Future<void> ratchetKey({String? participantId, int? keyIndex}) async {
if (participantId != null) {
final newKey = await _keyProvider!.ratchetKey(participantId, keyIndex);
if (kDebugMode) {
print('newKey: $newKey');
}
} else {
final newKey = await _keyProvider!.ratchetSharedKey(keyIndex: keyIndex);
if (kDebugMode) {
print('newKey: $newKey');
}
}
}
Future<void> _cleanUp() async {
await _listener?.cancelAll();
await _listener?.dispose();
_listener = null;
for (var frameCryptor in _frameCryptors.values.toList()) {
await frameCryptor.dispose();
}
_frameCryptors.clear();
await _dataPacketCryptor?.dispose();
_dataPacketCryptor = null;
}
Future<FrameCryptor> _addRtpSender(
{required RTCRtpSender sender, required String identity, required String sid}) async {
final frameCryptor = await frameCryptorFactory.createFrameCryptorForRtpSender(
participantId: identity, sender: sender, algorithm: _algorithm, keyProvider: _keyProvider!.keyProvider);
_frameCryptors[{identity: sid}] = frameCryptor;
await frameCryptor.setEnabled(_enabled);
logger.info('_addRtpSender, setKeyIndex: ${_keyProvider!.getLatestIndex(identity)}');
await frameCryptor.setKeyIndex(_keyProvider!.getLatestIndex(identity));
return frameCryptor;
}
Future<FrameCryptor> _addRtpReceiver(
{required RTCRtpReceiver receiver, required String identity, required String sid}) async {
final frameCryptor = await frameCryptorFactory.createFrameCryptorForRtpReceiver(
participantId: identity, receiver: receiver, algorithm: _algorithm, keyProvider: _keyProvider!.keyProvider);
_frameCryptors[{identity: sid}] = frameCryptor;
await frameCryptor.setEnabled(_enabled);
logger.info('_addRtpReceiver, setKeyIndex: ${_keyProvider!.getLatestIndex(identity)}');
await frameCryptor.setKeyIndex(_keyProvider!.getLatestIndex(identity));
return frameCryptor;
}
/// Enable/Disable frame crypto for the sender and receiver.
/// @param enabled true to enable, false to disable
/// if false, the frame cryptor will pass through frames and
/// without encryption/decryption
Future<void> setEnabled(bool enabled) async {
_enabled = enabled;
for (var frameCryptor in _frameCryptors.entries.toList()) {
await frameCryptor.value.setEnabled(enabled);
}
}
/// Sets the key index for the encryptors of the participant.
/// @param keyIndex the key index to set
/// @param participantIdentity the identity of the participant,
/// if null, use local participant.
Future<void> setKeyIndex(int keyIndex, {String? participantIdentity}) async {
participantIdentity ??= _room?.localParticipant?.identity;
for (var item in _frameCryptors.entries.toList()) {
if (item.key.keys.first == participantIdentity) {
await item.value.setKeyIndex(keyIndex);
}
}
}
/// Get the key index for the encryptors of the participant.
/// @param identity the identity of the participant,
/// if null, use local participant.
/// @return the key index and -1 if not found
Future<int> getKeyIndex(String? participantIdentity) async {
participantIdentity ??= _room?.localParticipant?.identity;
for (var item in _frameCryptors.entries.toList()) {
if (item.key.keys.first == participantIdentity) {
return await item.value.keyIndex;
}
}
return -1;
}
E2EEState _e2eeStateFromFrameCryptoState(FrameCryptorState state) {
switch (state) {
case FrameCryptorState.FrameCryptorStateNew:
return E2EEState.kNew;
case FrameCryptorState.FrameCryptorStateOk:
return E2EEState.kOk;
case FrameCryptorState.FrameCryptorStateMissingKey:
return E2EEState.kMissingKey;
case FrameCryptorState.FrameCryptorStateEncryptionFailed:
return E2EEState.kEncryptionFailed;
case FrameCryptorState.FrameCryptorStateDecryptionFailed:
return E2EEState.kDecryptionFailed;
case FrameCryptorState.FrameCryptorStateInternalError:
return E2EEState.kInternalError;
case FrameCryptorState.FrameCryptorStateKeyRatcheted:
return E2EEState.kKeyRatcheted;
}
}
bool get isDataChannelEncryptionEnabled => _enabled && _encryptionEnabled;
Future<Uint8List?> handleEncryptedData({
required Uint8List data,
required Uint8List iv,
required String participantIdentity,
required int keyIndex,
}) async {
if (_dataPacketCryptor == null) {
throw Exception('DataPacketCryptor is not initialized');
}
try {
final decryptedData = await _dataPacketCryptor!.decrypt(
participantId: participantIdentity,
encryptedPacket: EncryptedPacket(data: data, keyIndex: keyIndex, iv: iv),
);
return decryptedData;
} catch (e) {
logger.warning('handleEncryptedData error: $e');
return null;
}
}
Future<EncryptedPacket> encryptData({required Uint8List data}) async {
final participantId = _room?.localParticipant?.identity;
if (participantId == null || _dataPacketCryptor == null) {
throw Exception('DataPacketCryptor is not initialized');
}
return await _dataPacketCryptor!
.encrypt(participantId: participantId, keyIndex: _keyProvider!.getLatestIndex(participantId), data: data);
}
}