Skip to content

Commit

Permalink
Fix crypto migration for mono session
Browse files Browse the repository at this point in the history
  • Loading branch information
yostyle committed Oct 6, 2023
1 parent 29c2228 commit 3c2a9d3
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import org.matrix.android.sdk.api.session.crypto.model.MXUsersDevicesMap
import org.matrix.android.sdk.api.session.events.model.Content
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.content.EncryptedEventContent
import org.matrix.android.sdk.api.session.events.model.content.EncryptionEventContent
import org.matrix.android.sdk.api.session.events.model.content.RoomKeyContent
import org.matrix.android.sdk.api.session.events.model.content.RoomKeyWithHeldContent
Expand Down Expand Up @@ -133,6 +134,7 @@ internal class RustCryptoService @Inject constructor(
private val getRoomUserIds: GetRoomUserIdsUseCase,
private val outgoingRequestsProcessor: OutgoingRequestsProcessor,
private val matrixConfiguration: MatrixConfiguration,
private val rateLimiter: PerSessionBackupQueryRateLimiter,
) : CryptoService {

private val isStarting = AtomicBoolean(false)
Expand Down Expand Up @@ -493,7 +495,30 @@ internal class RustCryptoService @Inject constructor(
*/
@Throws(MXCryptoError::class)
override suspend fun decryptEvent(event: Event, timeline: String): MXEventDecryptionResult {
return olmMachine.decryptRoomEvent(event)
return try {
olmMachine.decryptRoomEvent(event)
} catch (mxCryptoError: MXCryptoError) {
if (mxCryptoError is MXCryptoError.Base && mxCryptoError.errorType == MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID) {
Timber.v("Try to perform a lazy migration from legacy store")
/**
* It's a bit hacky, check how this can be better integrated with rust?
*/
val content = event.content?.toModel<EncryptedEventContent>() ?: throw mxCryptoError
val roomId = event.roomId
val sessionId = content.sessionId
val senderKey = content.senderKey
if (roomId != null && sessionId != null) {
// try to perform a lazy migration from legacy store
val legacy = tryOrNull("Failed to access legacy crypto store") {
cryptoStore.getInboundGroupSession(sessionId, senderKey.orEmpty())
}
if (legacy == null || olmMachine.importRoomKey(legacy).isFailure) {
rateLimiter.tryFromBackupIfPossible(sessionId, roomId)
}
}
}
throw mxCryptoError
}
}

/**
Expand Down

0 comments on commit 3c2a9d3

Please sign in to comment.