|
| 1 | +import 'dart:typed_data'; |
| 2 | + |
| 3 | +import 'rtc_rtp_receiver.dart'; |
| 4 | +import 'rtc_rtp_sender.dart'; |
| 5 | + |
| 6 | +/// Built-in Algorithm. |
| 7 | +enum Algorithm { |
| 8 | + kAesGcm, |
| 9 | + kAesCbc, |
| 10 | +} |
| 11 | + |
| 12 | +class KeyProviderOptions { |
| 13 | + KeyProviderOptions({ |
| 14 | + required this.sharedKey, |
| 15 | + required this.ratchetSalt, |
| 16 | + required this.ratchetWindowSize, |
| 17 | + this.uncryptedMagicBytes, |
| 18 | + }); |
| 19 | + bool sharedKey; |
| 20 | + Uint8List ratchetSalt; |
| 21 | + Uint8List? uncryptedMagicBytes; |
| 22 | + int ratchetWindowSize; |
| 23 | + Map<String, dynamic> toJson() { |
| 24 | + return { |
| 25 | + 'sharedKey': sharedKey, |
| 26 | + 'ratchetSalt': ratchetSalt, |
| 27 | + if (uncryptedMagicBytes != null) |
| 28 | + 'uncryptedMagicBytes': uncryptedMagicBytes, |
| 29 | + 'ratchetWindowSize': ratchetWindowSize, |
| 30 | + }; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/// Shared secret key for frame encryption. |
| 35 | +abstract class KeyProvider { |
| 36 | + /// The unique identifier of the key provider. |
| 37 | + String get id; |
| 38 | + |
| 39 | + /// Set the raw key at the given index. |
| 40 | + Future<bool> setKey({ |
| 41 | + required String participantId, |
| 42 | + required int index, |
| 43 | + required Uint8List key, |
| 44 | + }); |
| 45 | + |
| 46 | + /// ratchet the key at the given index. |
| 47 | + Future<Uint8List> ratchetKey({ |
| 48 | + required String participantId, |
| 49 | + required int index, |
| 50 | + }); |
| 51 | + |
| 52 | + /// Dispose the key manager. |
| 53 | + Future<void> dispose(); |
| 54 | +} |
| 55 | + |
| 56 | +enum FrameCryptorState { |
| 57 | + FrameCryptorStateNew, |
| 58 | + FrameCryptorStateOk, |
| 59 | + FrameCryptorStateEncryptionFailed, |
| 60 | + FrameCryptorStateDecryptionFailed, |
| 61 | + FrameCryptorStateMissingKey, |
| 62 | + FrameCryptorStateKeyRatcheted, |
| 63 | + FrameCryptorStateInternalError, |
| 64 | +} |
| 65 | + |
| 66 | +/// Frame encryption/decryption. |
| 67 | +/// |
| 68 | +abstract class FrameCryptor { |
| 69 | + FrameCryptor(); |
| 70 | + |
| 71 | + Function(String participantId, FrameCryptorState state)? |
| 72 | + onFrameCryptorStateChanged; |
| 73 | + |
| 74 | + /// The unique identifier of the frame cryptor. |
| 75 | + String get participantId; |
| 76 | + |
| 77 | + /// Enable/Disable frame crypto for the sender or receiver. |
| 78 | + Future<bool> setEnabled(bool enabled); |
| 79 | + |
| 80 | + /// Get the enabled state for the sender or receiver. |
| 81 | + Future<bool> get enabled; |
| 82 | + |
| 83 | + /// Set the key index for the sender or receiver. |
| 84 | + /// If the key index is not set, the key index will be set to 0. |
| 85 | + Future<bool> setKeyIndex(int index); |
| 86 | + |
| 87 | + /// Get the key index for the sender or receiver. |
| 88 | + Future<int> get keyIndex; |
| 89 | + |
| 90 | + Future<void> updateCodec(String codec); |
| 91 | + |
| 92 | + /// Dispose the frame cryptor. |
| 93 | + Future<void> dispose(); |
| 94 | +} |
| 95 | + |
| 96 | +/// Factory for creating frame Cryptors. |
| 97 | +/// For End 2 End Encryption, you need to create a [KeyProvider] for each peer. |
| 98 | +/// And set your key in keyProvider. |
| 99 | +abstract class FrameCryptorFactory { |
| 100 | + /// Shared key manager. |
| 101 | + Future<KeyProvider> createDefaultKeyProvider(KeyProviderOptions options); |
| 102 | + |
| 103 | + /// Create a frame Cryptor from a [RTCRtpSender]. |
| 104 | + Future<FrameCryptor> createFrameCryptorForRtpSender({ |
| 105 | + required String participantId, |
| 106 | + required RTCRtpSender sender, |
| 107 | + required Algorithm algorithm, |
| 108 | + required KeyProvider keyProvider, |
| 109 | + }); |
| 110 | + |
| 111 | + /// Create a frame Cryptor from a [RTCRtpReceiver]. |
| 112 | + Future<FrameCryptor> createFrameCryptorForRtpReceiver({ |
| 113 | + required String participantId, |
| 114 | + required RTCRtpReceiver receiver, |
| 115 | + required Algorithm algorithm, |
| 116 | + required KeyProvider keyProvider, |
| 117 | + }); |
| 118 | +} |
0 commit comments