wip async crypto + persist

This commit is contained in:
Valere 2019-07-08 11:18:27 +02:00
parent e50dd265d4
commit 94b4351e19
8 changed files with 29 additions and 33 deletions

View File

@ -20,9 +20,8 @@ import android.text.TextUtils
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.api.session.crypto.MXCryptoError
import im.vector.matrix.android.api.session.room.model.message.MessageContent
import im.vector.matrix.android.api.util.JsonDict
import im.vector.matrix.android.internal.crypto.algorithms.MXDecryptionResult
import im.vector.matrix.android.internal.crypto.algorithms.olm.OlmDecryptionResult
import im.vector.matrix.android.internal.di.MoshiProvider
import timber.log.Timber

@ -79,8 +78,8 @@ data class Event(
) {


var mxDecryptionResult: MXDecryptionResult? = null
var mCryptoError: MXCryptoError? = null
var mxDecryptionResult: OlmDecryptionResult? = null
var mCryptoError: MXCryptoError.ErrorType? = null

/**
* Check if event is a state event.
@ -181,7 +180,7 @@ data class Event(
*/
fun getSenderKey(): String? {
return mxDecryptionResult?.senderKey
// return mClearEvent?.mSenderCurve25519Key ?: mSenderCurve25519Key
// return mClearEvent?.mSenderCurve25519Key ?: mSenderCurve25519Key
}

/**

View File

@ -16,30 +16,33 @@

package im.vector.matrix.android.internal.crypto.algorithms.olm

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.api.util.JsonDict

/**
* This class represents the decryption result.
*/
@JsonClass(generateAdapter = true)
data class OlmDecryptionResult(
/**
* The decrypted payload (with properties 'type', 'content')
*/
val payload: JsonDict? = null,
@Json(name = "payload") val payload: JsonDict? = null,

/**
* keys that the sender of the event claims ownership of:
* map from key type to base64-encoded key.
*/
val keysClaimed: Map<String, String>? = null,
@Json(name = "keysClaimed") val keysClaimed: Map<String, String>? = null,

/**
* The curve25519 key that the sender of the event is known to have ownership of.
*/
val senderKey: String? = null,
@Json(name = "senderKey") val senderKey: String? = null,

/**
* Devices which forwarded this session to us (normally empty).
*/
val forwardingCurve25519KeyChain: List<String>? = null
@Json(name = "forwardingCurve25519KeyChain") val forwardingCurve25519KeyChain: List<String>? = null
)

View File

@ -20,7 +20,9 @@ import com.squareup.moshi.JsonDataException
import im.vector.matrix.android.api.session.crypto.MXCryptoError
import im.vector.matrix.android.api.session.events.model.Event
import im.vector.matrix.android.api.session.events.model.UnsignedData
import im.vector.matrix.android.internal.crypto.algorithms.MXDecryptionResult
import im.vector.matrix.android.internal.crypto.MXEventDecryptionResult
import im.vector.matrix.android.internal.crypto.algorithms.olm.MXOlmDecryption
import im.vector.matrix.android.internal.crypto.algorithms.olm.OlmDecryptionResult
import im.vector.matrix.android.internal.database.model.EventEntity
import im.vector.matrix.android.internal.di.MoshiProvider
import timber.log.Timber
@ -73,13 +75,13 @@ internal object EventMapper {
).also {
eventEntity.decryptionResultJson?.let { json ->
try {
it.mxDecryptionResult = MoshiProvider.providesMoshi().adapter(MXDecryptionResult::class.java).fromJson(json)
it.mxDecryptionResult = MoshiProvider.providesMoshi().adapter(OlmDecryptionResult::class.java).fromJson(json)
} catch (t: JsonDataException) {
Timber.e(t, "Failed to parse decryption result")
}
}
//TODO get the full crypto error object
it.mCryptoError = eventEntity.decryptionErrorCode?.let { MXCryptoError(it, it) }
it.mCryptoError = eventEntity.decryptionErrorCode?.let { MXCryptoError.ErrorType.valueOf(it) }
}
}


View File

@ -18,7 +18,7 @@ package im.vector.matrix.android.internal.database.model

import im.vector.matrix.android.api.session.room.send.SendState
import im.vector.matrix.android.internal.crypto.MXEventDecryptionResult
import im.vector.matrix.android.internal.crypto.algorithms.MXDecryptionResult
import im.vector.matrix.android.internal.crypto.algorithms.olm.OlmDecryptionResult
import im.vector.matrix.android.internal.di.MoshiProvider
import io.realm.RealmObject
import io.realm.RealmResults
@ -73,13 +73,13 @@ internal open class EventEntity(@PrimaryKey var localId: String = UUID.randomUUI


fun setDecryptionResult(result: MXEventDecryptionResult) {
val decryptionResult = MXDecryptionResult(
val decryptionResult = OlmDecryptionResult(
payload = result.clearEvent,
senderKey = result.senderCurve25519Key,
keysClaimed = result.claimedEd25519Key?.let { mapOf("ed25519" to it) },
forwardingCurve25519KeyChain = result.forwardingCurve25519KeyChain
)
val adapter = MoshiProvider.providesMoshi().adapter<MXDecryptionResult>(MXDecryptionResult::class.java)
val adapter = MoshiProvider.providesMoshi().adapter<OlmDecryptionResult>(OlmDecryptionResult::class.java)
decryptionResultJson = adapter.toJson(decryptionResult)
}
}

View File

@ -22,9 +22,6 @@ import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.room.timeline.Timeline
import im.vector.matrix.android.api.session.room.timeline.TimelineEvent
import im.vector.matrix.android.api.util.CancelableBag
import im.vector.matrix.android.api.util.addTo
import im.vector.matrix.android.internal.crypto.NewSessionListener
import im.vector.matrix.android.internal.crypto.model.event.EncryptedEventContent
import im.vector.matrix.android.internal.database.mapper.asDomain
import im.vector.matrix.android.internal.database.model.*
import im.vector.matrix.android.internal.database.model.EventEntity

View File

@ -18,7 +18,6 @@ package im.vector.matrix.android.internal.session.room.timeline
import im.vector.matrix.android.api.session.crypto.CryptoService
import im.vector.matrix.android.api.session.crypto.MXCryptoError
import im.vector.matrix.android.api.session.events.model.toModel
import im.vector.matrix.android.internal.crypto.MXDecryptionException
import im.vector.matrix.android.internal.crypto.NewSessionListener
import im.vector.matrix.android.internal.crypto.model.event.EncryptedEventContent
import im.vector.matrix.android.internal.database.mapper.asDomain
@ -107,9 +106,10 @@ internal class TimelineEventDecryptor(
eventEntity.setDecryptionResult(result)
}

} catch (e: MXDecryptionException) {
if (e.cryptoError?.code == MXCryptoError.UNKNOWN_INBOUND_SESSION_ID_ERROR_CODE) {
} catch (e: MXCryptoError) {
if (e is MXCryptoError.Base && e.errorType == MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID) {
//Keep track of unknown sessions to automatically try to decrypt on new session
eventEntity.decryptionErrorCode = e.errorType.name
event.content?.toModel<EncryptedEventContent>()?.let { content ->
content.sessionId?.let { sessionId ->
synchronized(unknownSessionsFailure) {

View File

@ -24,7 +24,7 @@ import im.vector.matrix.android.api.session.events.model.toModel
import im.vector.matrix.android.api.session.room.model.message.MessageContent
import im.vector.matrix.android.internal.crypto.CryptoManager
import im.vector.matrix.android.internal.crypto.MXEventDecryptionResult
import im.vector.matrix.android.internal.crypto.algorithms.MXDecryptionResult
import im.vector.matrix.android.internal.crypto.algorithms.olm.OlmDecryptionResult
import im.vector.matrix.android.internal.crypto.verification.DefaultSasVerificationService
import im.vector.matrix.android.internal.session.sync.model.SyncResponse
import im.vector.matrix.android.internal.session.sync.model.ToDeviceSyncResponse
@ -67,7 +67,7 @@ internal class CryptoSyncHandler @Inject constructor(private val cryptoManager:
try {
result = cryptoManager.decryptEvent(event, timelineId ?: "")
} catch (exception: MXCryptoError) {
event.setCryptoError(exception)
event.mCryptoError = (exception as? MXCryptoError.Base)?.errorType //setCryptoError(exception.cryptoError)
}

if (null != result) {
@ -76,7 +76,7 @@ internal class CryptoSyncHandler @Inject constructor(private val cryptoManager:
// keysClaimed = map
// )
//TODO persist that?
event.mxDecryptionResult = MXDecryptionResult(
event.mxDecryptionResult = OlmDecryptionResult(
payload = result.clearEvent,
senderKey = result.senderCurve25519Key,
keysClaimed = result.claimedEd25519Key?.let { mapOf("ed25519" to it) },

View File

@ -48,16 +48,11 @@ class EncryptedItemFactory @Inject constructor(private val messageInformationDat
EventType.ENCRYPTED == event.root.getClearType() -> {
val cryptoError = event.root.mCryptoError
val errorDescription =
if (cryptoError is MXCryptoError.Base) {
if (cryptoError.errorType == MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID) {
stringProvider.getString(R.string.notice_crypto_error_unkwown_inbound_session_id)
} else {
// TODO i18n
cryptoError.technicalMessage
}
if (cryptoError == MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID) {
stringProvider.getString(R.string.notice_crypto_error_unkwown_inbound_session_id)
} else {
// Cannot happen (for now)
"Other error"
// TODO i18n
cryptoError?.name
}

val message = stringProvider.getString(R.string.notice_crypto_unable_to_decrypt, errorDescription)