@@ -1212,94 +1212,91 @@ internal class DefaultKeysBackupService @Inject constructor(
1212
1212
1213
1213
// Gather data to send to the homeserver
1214
1214
// roomId -> sessionId -> MXKeyBackupData
1215
- val keysBackupData = KeysBackupData (
1216
- roomIdToRoomKeysBackupData = HashMap ()
1217
- )
1215
+ val keysBackupData = KeysBackupData ()
1218
1216
1219
- for (olmInboundGroupSessionWrapper in olmInboundGroupSessionWrappers) {
1220
- val keyBackupData = encryptGroupSession(olmInboundGroupSessionWrapper)
1221
- if (keysBackupData.roomIdToRoomKeysBackupData[olmInboundGroupSessionWrapper.roomId] == null ) {
1222
- val roomKeysBackupData = RoomKeysBackupData (
1223
- sessionIdToKeyBackupData = HashMap ()
1224
- )
1225
- keysBackupData.roomIdToRoomKeysBackupData[olmInboundGroupSessionWrapper.roomId!! ] = roomKeysBackupData
1226
- }
1217
+ olmInboundGroupSessionWrappers.forEach { olmInboundGroupSessionWrapper ->
1218
+ val roomId = olmInboundGroupSessionWrapper.roomId ? : return @forEach
1219
+ val olmInboundGroupSession = olmInboundGroupSessionWrapper.olmInboundGroupSession ? : return @forEach
1227
1220
1228
1221
try {
1229
- keysBackupData.roomIdToRoomKeysBackupData[olmInboundGroupSessionWrapper.roomId]!!
1230
- .sessionIdToKeyBackupData[olmInboundGroupSessionWrapper.olmInboundGroupSession!! .sessionIdentifier()] = keyBackupData
1222
+ encryptGroupSession(olmInboundGroupSessionWrapper)
1223
+ ?.let {
1224
+ keysBackupData.roomIdToRoomKeysBackupData
1225
+ .getOrPut(roomId) { RoomKeysBackupData () }
1226
+ .sessionIdToKeyBackupData[olmInboundGroupSession.sessionIdentifier()] = it
1227
+ }
1231
1228
} catch (e: OlmException ) {
1232
1229
Timber .e(e, " OlmException" )
1233
1230
}
1234
1231
}
1235
1232
1236
1233
Timber .v(" backupKeys: 4 - Sending request" )
1237
1234
1238
- val sendingRequestCallback = object : MatrixCallback <BackupKeysResult > {
1239
- override fun onSuccess (data : BackupKeysResult ) {
1240
- uiHandler.post {
1241
- Timber .v(" backupKeys: 5a - Request complete" )
1235
+ // Make the request
1236
+ val version = keysBackupVersion?.version ? : return @withContext
1242
1237
1243
- // Mark keys as backed up
1244
- cryptoStore.markBackupDoneForInboundGroupSessions(olmInboundGroupSessionWrappers)
1238
+ storeSessionDataTask
1239
+ .configureWith(StoreSessionsDataTask .Params (version, keysBackupData)) {
1240
+ this .callback = object : MatrixCallback <BackupKeysResult > {
1241
+ override fun onSuccess (data : BackupKeysResult ) {
1242
+ uiHandler.post {
1243
+ Timber .v(" backupKeys: 5a - Request complete" )
1245
1244
1246
- if (olmInboundGroupSessionWrappers.size < KEY_BACKUP_SEND_KEYS_MAX_COUNT ) {
1247
- Timber .v(" backupKeys: All keys have been backed up" )
1248
- onServerDataRetrieved(data.count, data.hash)
1245
+ // Mark keys as backed up
1246
+ cryptoStore.markBackupDoneForInboundGroupSessions(olmInboundGroupSessionWrappers)
1249
1247
1250
- // Note: Changing state will trigger the call to backupAllGroupSessionsCallback.onSuccess()
1251
- keysBackupStateManager.state = KeysBackupState .ReadyToBackUp
1252
- } else {
1253
- Timber .v(" backupKeys: Continue to back up keys" )
1254
- keysBackupStateManager.state = KeysBackupState .WillBackUp
1248
+ if (olmInboundGroupSessionWrappers.size < KEY_BACKUP_SEND_KEYS_MAX_COUNT ) {
1249
+ Timber .v(" backupKeys: All keys have been backed up" )
1250
+ onServerDataRetrieved(data.count, data.hash)
1255
1251
1256
- backupKeys()
1257
- }
1258
- }
1259
- }
1252
+ // Note: Changing state will trigger the call to backupAllGroupSessionsCallback.onSuccess()
1253
+ keysBackupStateManager.state = KeysBackupState .ReadyToBackUp
1254
+ } else {
1255
+ Timber .v(" backupKeys: Continue to back up keys" )
1256
+ keysBackupStateManager.state = KeysBackupState .WillBackUp
1260
1257
1261
- override fun onFailure (failure : Throwable ) {
1262
- if (failure is Failure .ServerError ) {
1263
- uiHandler.post {
1264
- Timber .e(failure, " backupKeys: backupKeys failed." )
1265
-
1266
- when (failure.error.code) {
1267
- MatrixError .M_NOT_FOUND ,
1268
- MatrixError .M_WRONG_ROOM_KEYS_VERSION -> {
1269
- // Backup has been deleted on the server, or we are not using the last backup version
1270
- keysBackupStateManager.state = KeysBackupState .WrongBackUpVersion
1271
- backupAllGroupSessionsCallback?.onFailure(failure)
1272
- resetBackupAllGroupSessionsListeners()
1273
- resetKeysBackupData()
1274
- keysBackupVersion = null
1275
-
1276
- // Do not stay in KeysBackupState.WrongBackUpVersion but check what is available on the homeserver
1277
- checkAndStartKeysBackup()
1258
+ backupKeys()
1259
+ }
1278
1260
}
1279
- else ->
1280
- // Come back to the ready state so that we will retry on the next received key
1281
- keysBackupStateManager.state = KeysBackupState .ReadyToBackUp
1282
1261
}
1283
- }
1284
- } else {
1285
- uiHandler.post {
1286
- backupAllGroupSessionsCallback?.onFailure(failure)
1287
- resetBackupAllGroupSessionsListeners()
1288
1262
1289
- Timber .e(" backupKeys: backupKeys failed." )
1263
+ override fun onFailure (failure : Throwable ) {
1264
+ if (failure is Failure .ServerError ) {
1265
+ uiHandler.post {
1266
+ Timber .e(failure, " backupKeys: backupKeys failed." )
1267
+
1268
+ when (failure.error.code) {
1269
+ MatrixError .M_NOT_FOUND ,
1270
+ MatrixError .M_WRONG_ROOM_KEYS_VERSION -> {
1271
+ // Backup has been deleted on the server, or we are not using the last backup version
1272
+ keysBackupStateManager.state = KeysBackupState .WrongBackUpVersion
1273
+ backupAllGroupSessionsCallback?.onFailure(failure)
1274
+ resetBackupAllGroupSessionsListeners()
1275
+ resetKeysBackupData()
1276
+ keysBackupVersion = null
1277
+
1278
+ // Do not stay in KeysBackupState.WrongBackUpVersion but check what is available on the homeserver
1279
+ checkAndStartKeysBackup()
1280
+ }
1281
+ else ->
1282
+ // Come back to the ready state so that we will retry on the next received key
1283
+ keysBackupStateManager.state = KeysBackupState .ReadyToBackUp
1284
+ }
1285
+ }
1286
+ } else {
1287
+ uiHandler.post {
1288
+ backupAllGroupSessionsCallback?.onFailure(failure)
1289
+ resetBackupAllGroupSessionsListeners()
1290
1290
1291
- // Retry a bit later
1292
- keysBackupStateManager.state = KeysBackupState .ReadyToBackUp
1293
- maybeBackupKeys()
1294
- }
1295
- }
1296
- }
1297
- }
1291
+ Timber .e(" backupKeys: backupKeys failed." )
1298
1292
1299
- // Make the request
1300
- storeSessionDataTask
1301
- .configureWith(StoreSessionsDataTask .Params (keysBackupVersion!! .version, keysBackupData)) {
1302
- this .callback = sendingRequestCallback
1293
+ // Retry a bit later
1294
+ keysBackupStateManager.state = KeysBackupState .ReadyToBackUp
1295
+ maybeBackupKeys()
1296
+ }
1297
+ }
1298
+ }
1299
+ }
1303
1300
}
1304
1301
.executeBy(taskExecutor)
1305
1302
}
@@ -1308,46 +1305,45 @@ internal class DefaultKeysBackupService @Inject constructor(
1308
1305
1309
1306
@VisibleForTesting
1310
1307
@WorkerThread
1311
- fun encryptGroupSession (olmInboundGroupSessionWrapper : OlmInboundGroupSessionWrapper2 ): KeyBackupData {
1308
+ fun encryptGroupSession (olmInboundGroupSessionWrapper : OlmInboundGroupSessionWrapper2 ): KeyBackupData ? {
1312
1309
// Gather information for each key
1313
- val device = cryptoStore.deviceWithIdentityKey(olmInboundGroupSessionWrapper.senderKey !! )
1310
+ val device = olmInboundGroupSessionWrapper.senderKey?. let { cryptoStore.deviceWithIdentityKey(it) }
1314
1311
1315
1312
// Build the m.megolm_backup.v1.curve25519-aes-sha2 data as defined at
1316
1313
// https://github.com/uhoreg/matrix-doc/blob/e2e_backup/proposals/1219-storing-megolm-keys-serverside.md#mmegolm_backupv1curve25519-aes-sha2-key-format
1317
- val sessionData = olmInboundGroupSessionWrapper.exportKeys()
1314
+ val sessionData = olmInboundGroupSessionWrapper.exportKeys() ? : return null
1318
1315
val sessionBackupData = mapOf (
1319
- " algorithm" to sessionData!! .algorithm,
1316
+ " algorithm" to sessionData.algorithm,
1320
1317
" sender_key" to sessionData.senderKey,
1321
1318
" sender_claimed_keys" to sessionData.senderClaimedKeys,
1322
1319
" forwarding_curve25519_key_chain" to (sessionData.forwardingCurve25519KeyChain.orEmpty()),
1323
1320
" session_key" to sessionData.sessionKey)
1324
1321
1325
- var encryptedSessionBackupData: OlmPkMessage ? = null
1326
-
1327
- val moshi = MoshiProvider .providesMoshi()
1328
- val adapter = moshi.adapter(Map ::class .java)
1329
-
1330
- try {
1331
- val json = adapter.toJson(sessionBackupData)
1322
+ val json = MoshiProvider .providesMoshi()
1323
+ .adapter(Map ::class .java)
1324
+ .toJson(sessionBackupData)
1332
1325
1333
- encryptedSessionBackupData = backupOlmPkEncryption?.encrypt(json)
1326
+ val encryptedSessionBackupData = try {
1327
+ backupOlmPkEncryption?.encrypt(json)
1334
1328
} catch (e: OlmException ) {
1335
1329
Timber .e(e, " OlmException" )
1330
+ null
1336
1331
}
1332
+ ? : return null
1337
1333
1338
1334
// Build backup data for that key
1339
1335
return KeyBackupData (
1340
1336
firstMessageIndex = try {
1341
- olmInboundGroupSessionWrapper.olmInboundGroupSession!! .firstKnownIndex
1337
+ olmInboundGroupSessionWrapper.olmInboundGroupSession? .firstKnownIndex ? : 0
1342
1338
} catch (e: OlmException ) {
1343
1339
Timber .e(e, " OlmException" )
1344
1340
0L
1345
1341
},
1346
- forwardedCount = olmInboundGroupSessionWrapper.forwardingCurve25519KeyChain!! .size,
1342
+ forwardedCount = olmInboundGroupSessionWrapper.forwardingCurve25519KeyChain.orEmpty() .size,
1347
1343
isVerified = device?.isVerified == true ,
1348
1344
1349
1345
sessionData = mapOf (
1350
- " ciphertext" to encryptedSessionBackupData!! .mCipherText,
1346
+ " ciphertext" to encryptedSessionBackupData.mCipherText,
1351
1347
" mac" to encryptedSessionBackupData.mMac,
1352
1348
" ephemeral" to encryptedSessionBackupData.mEphemeralKey)
1353
1349
)
0 commit comments