crypto: Back up room keys when we create or receive new ones

This commit is contained in:
Damir Jelić 2021-11-03 12:24:58 +01:00
parent f9476f12af
commit 5c7b248ed2
2 changed files with 15 additions and 3 deletions

View File

@ -379,7 +379,6 @@ internal class DefaultCryptoService @Inject constructor(
private val setDeviceNameTask: SetDeviceNameTask,
private val loadRoomMembersTask: LoadRoomMembersTask,
private val cryptoSessionInfoProvider: CryptoSessionInfoProvider,
private val createKeysBackupVersionTask: CreateKeysBackupVersionTask,
private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val taskExecutor: TaskExecutor,
private val cryptoCoroutineScope: CoroutineScope,
@ -662,6 +661,8 @@ internal class DefaultCryptoService @Inject constructor(
// keys claim request to be sent out.
// This could be omitted but then devices might be waiting for the next
sendOutgoingRequests()
keysBackupService?.maybeBackupKeys()
}
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
@ -996,12 +997,14 @@ internal class DefaultCryptoService @Inject constructor(
}
val keyShareLock = roomKeyShareLocks.getOrPut(roomId, { Mutex() })
var sharedKey = false
keyShareLock.withLock {
coroutineScope {
this@DefaultCryptoService.olmMachine!!.shareRoomKey(roomId, roomMembers).map {
when (it) {
is Request.ToDevice -> {
sharedKey = true
async {
sendToDevice(it)
}
@ -1016,6 +1019,12 @@ internal class DefaultCryptoService @Inject constructor(
}.joinAll()
}
}
// If we sent out a room key over to-device messages it's likely that we created a new one
// Try to back the key up
if (sharedKey) {
keysBackupService?.maybeBackupKeys()
}
}
private suspend fun encrypt(roomId: String, eventType: String, content: Content): Content {
@ -1120,7 +1129,10 @@ internal class DefaultCryptoService @Inject constructor(
override suspend fun importRoomKeys(roomKeysAsArray: ByteArray,
password: String,
progressListener: ProgressListener?): ImportRoomKeysResult {
return olmMachine!!.importKeys(roomKeysAsArray, password, progressListener)
val result = olmMachine!!.importKeys(roomKeysAsArray, password, progressListener)
keysBackupService?.maybeBackupKeys()
return result
}
/**

View File

@ -779,7 +779,7 @@ internal class RustKeyBackupService @Inject constructor(
/**
* Do a backup if there are new keys, with a delay
*/
private fun maybeBackupKeys() {
fun maybeBackupKeys() {
when {
isStucked -> {
// If not already done, or in error case, check for a valid backup version on the homeserver.