KeyProvider.kt
2.4 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
package io.livekit.android.e2ee
import io.livekit.android.util.LKLog
import org.webrtc.FrameCryptorFactory
import org.webrtc.FrameCryptorKeyProvider
class KeyInfo
constructor(var participantId: String, var keyIndex: Int, var key: String ) {
override fun toString(): String {
return "KeyInfo(participantId='$participantId', keyIndex=$keyIndex)"
}
}
public interface KeyProvider {
fun setKey(key: String, participantId: String?, keyIndex: Int? = 0)
fun ratchetKey(participantId: String, index: Int): ByteArray
val rtcKeyProvider: FrameCryptorKeyProvider
var sharedKey: ByteArray?
var enableSharedKey: Boolean
}
class BaseKeyProvider
constructor(private var ratchetSalt: String, private var uncryptedMagicBytes: String, private var ratchetWindowSize: Int, override var enableSharedKey: Boolean = true) :
KeyProvider {
override var sharedKey: ByteArray? = null
private var keys: MutableMap<String, MutableMap<Int, String>> = mutableMapOf()
/**
* Set a key for a participant
* @param key
* @param participantId
* @param keyIndex
*/
override fun setKey(key: String, participantId: String?, keyIndex: Int?) {
if (enableSharedKey) {
sharedKey = key.toByteArray()
return
}
if(participantId == null) {
LKLog.d{ "Please provide valid participantId for non-SharedKey mode." }
return
}
var keyInfo = KeyInfo(participantId, keyIndex ?: 0, key)
if (!keys.containsKey(keyInfo.participantId)) {
keys[keyInfo.participantId] = mutableMapOf()
}
keys[keyInfo.participantId]!![keyInfo.keyIndex] = keyInfo.key
rtcKeyProvider.setKey(participantId, keyInfo.keyIndex, key.toByteArray())
}
override fun ratchetKey(participantId: String, index: Int): ByteArray {
return rtcKeyProvider.ratchetKey(participantId, index)
}
override val rtcKeyProvider: FrameCryptorKeyProvider
init {
this.ratchetSalt = ratchetSalt
this.uncryptedMagicBytes = uncryptedMagicBytes
this.ratchetWindowSize = ratchetWindowSize
this.enableSharedKey = enableSharedKey
this.rtcKeyProvider = FrameCryptorFactory.createFrameCryptorKeyProvider(
enableSharedKey,
ratchetSalt.toByteArray(),
ratchetWindowSize,
uncryptedMagicBytes.toByteArray(),
)
}
}