code review

This commit is contained in:
Valere 2020-04-22 18:50:59 +02:00
parent d0f776a9cf
commit e37dd547b8
5 changed files with 20 additions and 41 deletions

View File

@ -88,8 +88,8 @@ class UnwedgingTest : InstrumentedTest {
val aliceCryptoStore = (aliceSession.cryptoService() as DefaultCryptoService).cryptoStoreForTesting
//bobSession.cryptoService().setWarnOnUnknownDevices(false)
//aliceSession.cryptoService().setWarnOnUnknownDevices(false)
// bobSession.cryptoService().setWarnOnUnknownDevices(false)
// aliceSession.cryptoService().setWarnOnUnknownDevices(false)
val roomFromBobPOV = bobSession.getRoom(aliceRoomId)!!
val roomFromAlicePOV = aliceSession.getRoom(aliceRoomId)!!
@ -143,7 +143,7 @@ class UnwedgingTest : InstrumentedTest {
val oldSession = serializeForRealm(olmSession.olmSession)
aliceSession.cryptoService().discardOutbundSession(roomFromAlicePOV.roomId)
aliceSession.cryptoService().discardOutboundSession(roomFromAlicePOV.roomId)
Thread.sleep(6_000)
latch = CountDownLatch(1)
@ -171,7 +171,7 @@ class UnwedgingTest : InstrumentedTest {
Thread.sleep(6_000)
// Force new session, and key share
aliceSession.cryptoService().discardOutbundSession(roomFromAlicePOV.roomId)
aliceSession.cryptoService().discardOutboundSession(roomFromAlicePOV.roomId)
// Wait for the message to be received by Bob
mTestHelper.waitWithLatch {
@ -220,7 +220,6 @@ class UnwedgingTest : InstrumentedTest {
}
}
bobTimeline.dispose()
cryptoTestData.cleanUp(mTestHelper)

View File

@ -111,7 +111,7 @@ interface CryptoService {
roomId: String,
callback: MatrixCallback<MXEncryptEventContentResult>)
fun discardOutbundSession(roomId: String)
fun discardOutboundSession(roomId: String)
@Throws(MXCryptoError::class)
fun decryptEvent(event: Event, timeline: String): MXEventDecryptionResult

View File

@ -635,7 +635,7 @@ internal class DefaultCryptoService @Inject constructor(
}
}
override fun discardOutbundSession(roomId: String) {
override fun discardOutboundSession(roomId: String) {
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
roomEncryptorsStore.get(roomId)?.discardSessionKey()
}
@ -718,38 +718,6 @@ internal class DefaultCryptoService @Inject constructor(
}
}
// private fun markOlmSessionForUnwedging(event: Event, mxMegolmDecryption: MXMegolmDecryption) {
// Timber.d("## CRYPTO |  markOlmSessionForUnwedging: ${event.eventId}")
// val senderId = event.senderId ?: return
// val encryptedMessage = event.content.toModel<EncryptedEventContent>() ?: return
// val deviceKey = encryptedMessage.senderKey ?: return
// encryptedMessage.algorithm?.takeIf { it == MXCRYPTO_ALGORITHM_MEGOLM } ?: return
//
// if (senderId == userId
// && deviceKey == olmDevice.deviceCurve25519Key) {
// Timber.d("## CRYPTO |  markOlmSessionForUnwedging: Do not unwedge ourselves")
// return
// }
//
// val lastForcedDate = lastNewSessionForcedDates.getObject(senderId, deviceKey) ?: 0
// val now = System.currentTimeMillis()
// if (now - lastForcedDate < CRYPTO_MIN_FORCE_SESSION_PERIOD_MILLIS) {
// Timber.d("## CRYPTO | markOlmSessionForUnwedging: New session already forced with device at $lastForcedDate. Not forcing another")
// return
// }
//
// // Establish a new olm session with this device since we're failing to decrypt messages
// // on a current session.
// val deviceInfo = getDeviceInfo(senderId, deviceKey) ?: return Unit.also {
// Timber.d("## CRYPTO | markOlmSessionForUnwedging: Couldn't find device for identity key $deviceKey: not re-establishing session")
// }
//
// Timber.d("## CRYPTO | markOlmSessionForUnwedging from $senderId:${deviceInfo.deviceId}")
// lastNewSessionForcedDates.setObject(senderId, deviceKey, now)
//
// mxMegolmDecryption.markOlmSessionForUnwedging(senderId, deviceInfo)
// }
/**
* Reset replay attack data for the given timeline.
*
@ -1197,6 +1165,18 @@ internal class DefaultCryptoService @Inject constructor(
}
private fun markOlmSessionForUnwedging(senderId: String, deviceInfo: CryptoDeviceInfo) {
val deviceKey = deviceInfo.identityKey()
val lastForcedDate = lastNewSessionForcedDates.getObject(senderId, deviceKey) ?: 0
val now = System.currentTimeMillis()
if (now - lastForcedDate < CRYPTO_MIN_FORCE_SESSION_PERIOD_MILLIS) {
Timber.d("## CRYPTO | markOlmSessionForUnwedging: New session already forced with device at $lastForcedDate. Not forcing another")
return
}
Timber.d("## CRYPTO | markOlmSessionForUnwedging from $senderId:${deviceInfo.deviceId}")
lastNewSessionForcedDates.setObject(senderId, deviceKey, now)
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
ensureOlmSessionsForDevicesAction.handle(mapOf(senderId to listOf(deviceInfo)), force = true)

View File

@ -44,7 +44,7 @@ enum class Command(val command: String, val parameters: String, @StringRes val d
POLL("/poll", "Question | Option 1 | Option 2 ...", R.string.command_description_poll),
SHRUG("/shrug", "<message>", R.string.command_description_shrug),
PLAIN("/plain", "<message>", R.string.command_description_plain),
DISCARD_SESSION("/discardsession","", R.string.command_description_discard_session),
DISCARD_SESSION("/discardsession", "", R.string.command_description_discard_session),
// TODO temporary command
VERIFY_USER("/verify", "<user-id>", R.string.command_description_verify);

View File

@ -449,7 +449,7 @@ class RoomDetailViewModel @AssistedInject constructor(
}
is ParsedCommand.DiscardSession -> {
if (room.isEncrypted()) {
session.cryptoService().discardOutbundSession(room.roomId)
session.cryptoService().discardOutboundSession(room.roomId)
_viewEvents.post(RoomDetailViewEvents.SlashCommandHandled())
popDraft()
} else {