crypto: Notify the rest of the code about received room keys
This commit is contained in:
parent
3ba29b4ea9
commit
2805772d0a
@ -68,9 +68,11 @@ import org.matrix.android.sdk.internal.crypto.model.ImportRoomKeysResult
|
|||||||
import org.matrix.android.sdk.internal.crypto.model.MXDeviceInfo
|
import org.matrix.android.sdk.internal.crypto.model.MXDeviceInfo
|
||||||
import org.matrix.android.sdk.internal.crypto.model.MXEncryptEventContentResult
|
import org.matrix.android.sdk.internal.crypto.model.MXEncryptEventContentResult
|
||||||
import org.matrix.android.sdk.internal.crypto.model.MXUsersDevicesMap
|
import org.matrix.android.sdk.internal.crypto.model.MXUsersDevicesMap
|
||||||
|
import org.matrix.android.sdk.internal.crypto.model.event.RoomKeyContent
|
||||||
import org.matrix.android.sdk.internal.crypto.model.event.RoomKeyWithHeldContent
|
import org.matrix.android.sdk.internal.crypto.model.event.RoomKeyWithHeldContent
|
||||||
import org.matrix.android.sdk.internal.crypto.model.rest.DeviceInfo
|
import org.matrix.android.sdk.internal.crypto.model.rest.DeviceInfo
|
||||||
import org.matrix.android.sdk.internal.crypto.model.rest.DevicesListResponse
|
import org.matrix.android.sdk.internal.crypto.model.rest.DevicesListResponse
|
||||||
|
import org.matrix.android.sdk.internal.crypto.model.rest.ForwardedRoomKeyContent
|
||||||
import org.matrix.android.sdk.internal.crypto.model.rest.KeysUploadResponse
|
import org.matrix.android.sdk.internal.crypto.model.rest.KeysUploadResponse
|
||||||
import org.matrix.android.sdk.internal.crypto.model.rest.KeysClaimResponse
|
import org.matrix.android.sdk.internal.crypto.model.rest.KeysClaimResponse
|
||||||
import org.matrix.android.sdk.internal.crypto.model.rest.KeysQueryResponse
|
import org.matrix.android.sdk.internal.crypto.model.rest.KeysQueryResponse
|
||||||
@ -161,6 +163,9 @@ internal class DefaultCryptoService @Inject constructor(
|
|||||||
private val outgointRequestsLock: Mutex = Mutex()
|
private val outgointRequestsLock: Mutex = Mutex()
|
||||||
private val roomKeyShareLocks: ConcurrentHashMap<String, Mutex> = ConcurrentHashMap()
|
private val roomKeyShareLocks: ConcurrentHashMap<String, Mutex> = ConcurrentHashMap()
|
||||||
|
|
||||||
|
// TODO does this need to be concurrent?
|
||||||
|
private val newSessionListeners = ArrayList<NewSessionListener>()
|
||||||
|
|
||||||
suspend fun onStateEvent(roomId: String, event: Event) {
|
suspend fun onStateEvent(roomId: String, event: Event) {
|
||||||
when (event.getClearType()) {
|
when (event.getClearType()) {
|
||||||
EventType.STATE_ROOM_ENCRYPTION -> onRoomEncryptionEvent(roomId, event)
|
EventType.STATE_ROOM_ENCRYPTION -> onRoomEncryptionEvent(roomId, event)
|
||||||
@ -643,11 +648,50 @@ internal class DefaultCryptoService @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun notifyRoomKeyReceival(
|
||||||
|
roomId: String,
|
||||||
|
sessionId: String,
|
||||||
|
) {
|
||||||
|
// The sender key is actually unused since it's unimportant for megolm
|
||||||
|
// Our events don't contain the info so pass an empty string until we
|
||||||
|
// change the listener definition
|
||||||
|
val senderKey = ""
|
||||||
|
|
||||||
|
newSessionListeners.forEach {
|
||||||
|
try {
|
||||||
|
it.onNewSession(roomId, senderKey, sessionId)
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun receiveSyncChanges(
|
suspend fun receiveSyncChanges(
|
||||||
toDevice: ToDeviceSyncResponse?,
|
toDevice: ToDeviceSyncResponse?,
|
||||||
deviceChanges: DeviceListResponse?,
|
deviceChanges: DeviceListResponse?,
|
||||||
keyCounts: DeviceOneTimeKeysCountSyncResponse?) {
|
keyCounts: DeviceOneTimeKeysCountSyncResponse?) {
|
||||||
olmMachine!!.receiveSyncChanges(toDevice, deviceChanges, keyCounts)
|
// Decrypt and handle our to-device events
|
||||||
|
val toDeviceEvents = olmMachine!!.receiveSyncChanges(toDevice, deviceChanges, keyCounts)
|
||||||
|
|
||||||
|
// Notify the our listeners about room keys so decryption is retried.
|
||||||
|
if (toDeviceEvents.events != null) {
|
||||||
|
for (event in toDeviceEvents.events) {
|
||||||
|
if (event.type == "m.room_key") {
|
||||||
|
val content = event.getClearContent().toModel<RoomKeyContent>() ?: continue
|
||||||
|
|
||||||
|
val roomId = content.sessionId ?: continue
|
||||||
|
val sessionId = content.sessionId
|
||||||
|
|
||||||
|
notifyRoomKeyReceival(roomId, sessionId)
|
||||||
|
} else if (event.type == "m.forwarded_room_key") {
|
||||||
|
val content = event.getClearContent().toModel<ForwardedRoomKeyContent>() ?: continue
|
||||||
|
|
||||||
|
val roomId = content.sessionId ?: continue
|
||||||
|
val sessionId = content.sessionId
|
||||||
|
|
||||||
|
notifyRoomKeyReceival(roomId, sessionId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun preshareGroupSession(roomId: String, roomMembers: List<String>) {
|
private suspend fun preshareGroupSession(roomId: String, roomMembers: List<String>) {
|
||||||
@ -946,12 +990,11 @@ internal class DefaultCryptoService @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun addNewSessionListener(newSessionListener: NewSessionListener) {
|
override fun addNewSessionListener(newSessionListener: NewSessionListener) {
|
||||||
// TODO we need to notify the listener when we receive a new inbound
|
if (!newSessionListeners.contains(newSessionListener)) newSessionListeners.add(newSessionListener)
|
||||||
// group session
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun removeSessionListener(listener: NewSessionListener) {
|
override fun removeSessionListener(listener: NewSessionListener) {
|
||||||
// TODO
|
newSessionListeners.remove(listener)
|
||||||
}
|
}
|
||||||
/* ==========================================================================================
|
/* ==========================================================================================
|
||||||
* DEBUG INFO
|
* DEBUG INFO
|
||||||
|
Loading…
Reference in New Issue
Block a user