|
| 1 | +package io.getstream.webrtc.flutter; |
| 2 | + |
| 3 | +import androidx.annotation.NonNull; |
| 4 | + |
| 5 | +import io.getstream.webrtc.flutter.utils.ConstraintsMap; |
| 6 | + |
| 7 | +import org.webrtc.DataPacketCryptor; |
| 8 | +import org.webrtc.DataPacketCryptorFactory; |
| 9 | +import org.webrtc.FrameCryptor; |
| 10 | +import org.webrtc.FrameCryptorAlgorithm; |
| 11 | +import org.webrtc.FrameCryptorKeyProvider; |
| 12 | + |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.UUID; |
| 16 | + |
| 17 | +import io.flutter.plugin.common.MethodCall; |
| 18 | +import io.flutter.plugin.common.MethodChannel; |
| 19 | + |
| 20 | +class FlutterDataPacketCryptor { |
| 21 | + private static final String TAG = "FlutterDataPacketCryptor"; |
| 22 | + private final Map<String, DataPacketCryptor> dataCryptos = new HashMap<>(); |
| 23 | + |
| 24 | + private final FlutterRTCFrameCryptor frameCryptor; |
| 25 | + |
| 26 | + public FlutterDataPacketCryptor(FlutterRTCFrameCryptor frameCryptor) { |
| 27 | + this.frameCryptor = frameCryptor; |
| 28 | + } |
| 29 | + |
| 30 | + public boolean handleMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { |
| 31 | + String method_name = call.method; |
| 32 | + Map<String, Object> params = (Map<String, Object>) call.arguments; |
| 33 | + if (method_name.equals("createDataPacketCryptor")) { |
| 34 | + createDataPacketCryptor(params, result); |
| 35 | + } else if (method_name.equals("dataPacketCryptorEncrypt")) { |
| 36 | + dataPacketCryptorEncrypt(params, result); |
| 37 | + } else if (method_name.equals("dataPacketCryptorDecrypt")) { |
| 38 | + dataPacketCryptorDecrypt(params, result); |
| 39 | + } else if (method_name.equals("dataPacketCryptorDispose")) { |
| 40 | + dataPacketCryptorDispose(params, result); |
| 41 | + } else { |
| 42 | + return false; |
| 43 | + } |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + private void createDataPacketCryptor(@NonNull Map<String, Object> params, @NonNull MethodChannel.Result result) { |
| 48 | + String keyProviderId = (String) params.get("keyProviderId"); |
| 49 | + FrameCryptorKeyProvider keyProvider = frameCryptor.getKeyProvider(keyProviderId); |
| 50 | + if (keyProvider == null) { |
| 51 | + result.error("createDataPacketCryptorFailed", "keyProvider not found", null); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if (params.get("algorithm") == null) { |
| 56 | + result.error("createDataPacketCryptorFailed", "algorithm is null", null); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + int algorithm = (int) params.get("algorithm"); |
| 61 | + |
| 62 | + DataPacketCryptor dataPacketCryptor = DataPacketCryptorFactory.createDataPacketCryptor( |
| 63 | + frameCryptor.frameCryptorAlgorithmFromInt(algorithm), |
| 64 | + keyProvider); |
| 65 | + if (dataPacketCryptor == null) { |
| 66 | + result.error("createDataPacketCryptorFailed", "createDataPacketCryptor failed", null); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + String dataCryptorId = UUID.randomUUID().toString(); |
| 71 | + dataCryptos.put(dataCryptorId, dataPacketCryptor); |
| 72 | + |
| 73 | + ConstraintsMap paramsResult = new ConstraintsMap(); |
| 74 | + paramsResult.putString("dataCryptorId", dataCryptorId); |
| 75 | + result.success(paramsResult.toMap()); |
| 76 | + } |
| 77 | + |
| 78 | + private void dataPacketCryptorEncrypt(@NonNull Map<String, Object> params, @NonNull MethodChannel.Result result) { |
| 79 | + String dataCryptorId = (String) params.get("dataCryptorId"); |
| 80 | + if (dataCryptorId == null) { |
| 81 | + result.error("dataPacketCryptorEncryptFailed", "dataCryptorId is null", null); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + DataPacketCryptor dataPacketCryptor = dataCryptos.get(dataCryptorId); |
| 86 | + if (dataPacketCryptor == null) { |
| 87 | + result.error("dataPacketCryptorEncryptFailed", "dataPacketCryptor not found", null); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + String participantId = (String) params.get("participantId"); |
| 92 | + if (participantId == null) { |
| 93 | + result.error("dataPacketCryptorEncryptFailed", "participantId is null", null); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + byte[] data = (byte[]) params.get("data"); |
| 98 | + if (data == null) { |
| 99 | + result.error("dataPacketCryptorEncryptFailed", "data is null", null); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + int keyIndex = (int) params.get("keyIndex"); |
| 104 | + if (keyIndex < 0) { |
| 105 | + result.error("dataPacketCryptorEncryptFailed", "keyIndex is invalid", null); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + DataPacketCryptor.EncryptedPacket packet = dataPacketCryptor.encrypt(participantId, keyIndex, data); |
| 110 | + ConstraintsMap paramsResult = new ConstraintsMap(); |
| 111 | + paramsResult.putInt("keyIndex", packet.keyIndex); |
| 112 | + paramsResult.putByte("data", packet.payload); |
| 113 | + paramsResult.putByte("iv", packet.iv); |
| 114 | + result.success(paramsResult.toMap()); |
| 115 | + } |
| 116 | + |
| 117 | + private void dataPacketCryptorDecrypt(@NonNull Map<String, Object> params, @NonNull MethodChannel.Result result) { |
| 118 | + String dataCryptorId = (String) params.get("dataCryptorId"); |
| 119 | + if (dataCryptorId == null) { |
| 120 | + result.error("dataPacketCryptorEncryptFailed", "dataCryptorId is null", null); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + DataPacketCryptor dataPacketCryptor = dataCryptos.get(dataCryptorId); |
| 125 | + if (dataPacketCryptor == null) { |
| 126 | + result.error("dataPacketCryptorEncryptFailed", "dataPacketCryptor not found", null); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + String participantId = (String) params.get("participantId"); |
| 131 | + if (participantId == null) { |
| 132 | + result.error("dataPacketCryptorEncryptFailed", "participantId is null", null); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + byte[] data = (byte[]) params.get("data"); |
| 137 | + if (data == null) { |
| 138 | + result.error("dataPacketCryptorEncryptFailed", "data is null", null); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + byte[] iv = (byte[]) params.get("iv"); |
| 143 | + if (iv == null) { |
| 144 | + result.error("dataPacketCryptorEncryptFailed", "iv is null", null); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + int keyIndex = (int) params.get("keyIndex"); |
| 149 | + if (keyIndex < 0) { |
| 150 | + result.error("dataPacketCryptorEncryptFailed", "keyIndex is invalid", null); |
| 151 | + return; |
| 152 | + } |
| 153 | + DataPacketCryptor.EncryptedPacket encryptedPacket = new DataPacketCryptor.EncryptedPacket(data, iv, keyIndex); |
| 154 | + byte[] decrypted = dataPacketCryptor.decrypt(participantId, encryptedPacket); |
| 155 | + if (decrypted == null) { |
| 156 | + result.error("dataPacketCryptorDecryptFailed", "decrypt failed", null); |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + ConstraintsMap paramsResult = new ConstraintsMap(); |
| 161 | + paramsResult.putByte("data", decrypted); |
| 162 | + result.success(paramsResult.toMap()); |
| 163 | + } |
| 164 | + |
| 165 | + private void dataPacketCryptorDispose(@NonNull Map<String, Object> params, @NonNull MethodChannel.Result result) { |
| 166 | + String dataCryptorId = (String) params.get("dataCryptorId"); |
| 167 | + if (dataCryptorId == null) { |
| 168 | + result.error("dataPacketCryptorDisposeFailed", "dataCryptorId is null", null); |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + DataPacketCryptor dataPacketCryptor = dataCryptos.remove(dataCryptorId); |
| 173 | + if (dataPacketCryptor == null) { |
| 174 | + result.error("dataPacketCryptorDisposeFailed", "dataPacketCryptor not found", null); |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + if (dataPacketCryptor != null) { |
| 179 | + dataPacketCryptor.dispose(); |
| 180 | + } |
| 181 | + result.success(null); |
| 182 | + } |
| 183 | +} |
0 commit comments