Remove all async thread

This commit is contained in:
Benoit Marty 2019-05-17 15:05:07 +02:00
parent c66e82c4ae
commit de4662b9d5
12 changed files with 558 additions and 902 deletions

View File

@ -43,11 +43,11 @@ interface CryptoService {


fun getKeysBackupService(): KeysBackupService fun getKeysBackupService(): KeysBackupService


fun isRoomBlacklistUnverifiedDevices(roomId: String, callback: MatrixCallback<Boolean>?) fun isRoomBlacklistUnverifiedDevices(roomId: String?): Boolean


fun setWarnOnUnknownDevices(warn: Boolean) fun setWarnOnUnknownDevices(warn: Boolean)


fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String, callback: MatrixCallback<Unit>) fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String)


fun getUserDevices(userId: String): MutableList<MXDeviceInfo> fun getUserDevices(userId: String): MutableList<MXDeviceInfo>


@ -57,11 +57,11 @@ interface CryptoService {


fun getMyDevice(): MXDeviceInfo fun getMyDevice(): MXDeviceInfo


fun getGlobalBlacklistUnverifiedDevices(callback: MatrixCallback<Boolean>?) fun getGlobalBlacklistUnverifiedDevices() : Boolean


fun setGlobalBlacklistUnverifiedDevices(block: Boolean, callback: MatrixCallback<Unit>?) fun setGlobalBlacklistUnverifiedDevices(block: Boolean)


fun setRoomUnBlacklistUnverifiedDevices(roomId: String, callback: MatrixCallback<Unit>) fun setRoomUnBlacklistUnverifiedDevices(roomId: String)


fun getDeviceTrackingStatus(userId: String): Int fun getDeviceTrackingStatus(userId: String): Int


@ -69,9 +69,9 @@ interface CryptoService {


fun exportRoomKeys(password: String, callback: MatrixCallback<ByteArray>) fun exportRoomKeys(password: String, callback: MatrixCallback<ByteArray>)


fun setRoomBlacklistUnverifiedDevices(roomId: String, callback: MatrixCallback<Unit>) fun setRoomBlacklistUnverifiedDevices(roomId: String)


fun getDeviceInfo(userId: String, deviceId: String?, callback: MatrixCallback<MXDeviceInfo?>) fun getDeviceInfo(userId: String, deviceId: String?): MXDeviceInfo?


fun reRequestRoomKeyForEvent(event: Event) fun reRequestRoomKeyForEvent(event: Event)



View File

@ -25,6 +25,7 @@ import android.os.Looper
private const val THREAD_ENCRYPT_NAME = "Crypto_Encrypt_Thread" private const val THREAD_ENCRYPT_NAME = "Crypto_Encrypt_Thread"
private const val THREAD_DECRYPT_NAME = "Crypto_Decrypt_Thread" private const val THREAD_DECRYPT_NAME = "Crypto_Decrypt_Thread"


// TODO Remove and replace by Task
internal object CryptoAsyncHelper { internal object CryptoAsyncHelper {


private var uiHandler: Handler? = null private var uiHandler: Handler? = null

View File

@ -55,9 +55,6 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore,
// tells if there is a download keys request in progress // tells if there is a download keys request in progress
private var mIsDownloadingKeys = false private var mIsDownloadingKeys = false


// Internal listener
private lateinit var mCryptoListener: DeviceListCryptoListener

/** /**
* Creator * Creator
* *
@ -330,13 +327,12 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore,
} }
} }


if (!mCryptoListener.hasBeenReleased()) { val callback = promise.mCallback
val callback = promise.mCallback


if (null != callback) { if (null != callback) {
CryptoAsyncHelper.getUiHandler().post { callback.onSuccess(usersDevicesInfoMap) } CryptoAsyncHelper.getUiHandler().post { callback.onSuccess(usersDevicesInfoMap) }
}
} }

promisesToRemove.add(promise) promisesToRemove.add(promise)
} }
} }
@ -703,15 +699,6 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore,
}) })
} }


fun setCryptoInternalListener(listener: DeviceListCryptoListener) {
mCryptoListener = listener
}


interface DeviceListCryptoListener {
fun hasBeenReleased(): Boolean
}

companion object { companion object {


/** /**

View File

@ -16,21 +16,20 @@


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


import android.os.Handler
import android.text.TextUtils import android.text.TextUtils
import im.vector.matrix.android.api.auth.data.Credentials import im.vector.matrix.android.api.auth.data.Credentials
import im.vector.matrix.android.api.session.crypto.keyshare.RoomKeysRequestListener import im.vector.matrix.android.api.session.crypto.keyshare.RoomKeysRequestListener
import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.events.model.Event
import im.vector.matrix.android.api.session.events.model.toModel import im.vector.matrix.android.api.session.events.model.toModel
import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore
import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyShare import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyShare
import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.*


internal class IncomingRoomKeyRequestManager( internal class IncomingRoomKeyRequestManager(
val mCredentials: Credentials, private val mCredentials: Credentials,
val mCryptoStore: IMXCryptoStore, private val mCryptoStore: IMXCryptoStore,
val mRoomDecryptorProvider: RoomDecryptorProvider) { private val mRoomDecryptorProvider: RoomDecryptorProvider) {




// list of IncomingRoomKeyRequests/IncomingRoomKeyRequestCancellations // list of IncomingRoomKeyRequests/IncomingRoomKeyRequestCancellations
@ -39,7 +38,7 @@ internal class IncomingRoomKeyRequestManager(
private val mReceivedRoomKeyRequestCancellations = ArrayList<IncomingRoomKeyRequestCancellation>() private val mReceivedRoomKeyRequestCancellations = ArrayList<IncomingRoomKeyRequestCancellation>()


// the listeners // the listeners
val mRoomKeysRequestListeners: MutableSet<RoomKeysRequestListener> = HashSet<RoomKeysRequestListener>() val mRoomKeysRequestListeners: MutableSet<RoomKeysRequestListener> = HashSet()


init { init {
mReceivedRoomKeyRequests.addAll(mCryptoStore.getPendingIncomingRoomKeyRequests()) mReceivedRoomKeyRequests.addAll(mCryptoStore.getPendingIncomingRoomKeyRequests())
@ -52,27 +51,19 @@ internal class IncomingRoomKeyRequestManager(
* @param event the announcement event. * @param event the announcement event.
*/ */
fun onRoomKeyRequestEvent(event: Event) { fun onRoomKeyRequestEvent(event: Event) {
val roomKeyShare = event.content.toModel<RoomKeyShare>()!! val roomKeyShare = event.content.toModel<RoomKeyShare>()


if (null != roomKeyShare.action) { when (roomKeyShare?.action) {
when (roomKeyShare.action) { RoomKeyShare.ACTION_SHARE_REQUEST -> synchronized(mReceivedRoomKeyRequests) {
RoomKeyShare.ACTION_SHARE_REQUEST -> synchronized(mReceivedRoomKeyRequests) { mReceivedRoomKeyRequests.add(IncomingRoomKeyRequest(event))
mReceivedRoomKeyRequests.add(IncomingRoomKeyRequest(event))
}
RoomKeyShare.ACTION_SHARE_CANCELLATION -> synchronized(mReceivedRoomKeyRequestCancellations) {
mReceivedRoomKeyRequestCancellations.add(IncomingRoomKeyRequestCancellation(event))
}
else -> Timber.e("## onRoomKeyRequestEvent() : unsupported action " + roomKeyShare.action!!)
} }
RoomKeyShare.ACTION_SHARE_CANCELLATION -> synchronized(mReceivedRoomKeyRequestCancellations) {
mReceivedRoomKeyRequestCancellations.add(IncomingRoomKeyRequestCancellation(event))
}
else -> Timber.e("## onRoomKeyRequestEvent() : unsupported action " + roomKeyShare?.action)
} }
} }


private lateinit var encryptingThreadHandler: Handler

fun setEncryptingThreadHandler(encryptingThreadHandler: Handler) {
this.encryptingThreadHandler = encryptingThreadHandler
}

/** /**
* Process any m.room_key_request events which were queued up during the * Process any m.room_key_request events which were queued up during the
* current sync. * current sync.
@ -129,13 +120,11 @@ internal class IncomingRoomKeyRequestManager(
} }


request.mShare = Runnable { request.mShare = Runnable {
encryptingThreadHandler.post { decryptor.shareKeysWithDevice(request)
decryptor.shareKeysWithDevice(request) mCryptoStore.deleteIncomingRoomKeyRequest(request)
mCryptoStore.deleteIncomingRoomKeyRequest(request)
}
} }


request.mIgnore = Runnable { encryptingThreadHandler.post { mCryptoStore.deleteIncomingRoomKeyRequest(request) } } request.mIgnore = Runnable { mCryptoStore.deleteIncomingRoomKeyRequest(request) }


// if the device is verified already, share the keys // if the device is verified already, share the keys
val device = mCryptoStore.getUserDevice(deviceId!!, userId) val device = mCryptoStore.getUserDevice(deviceId!!, userId)

View File

@ -20,11 +20,11 @@ package im.vector.matrix.android.internal.crypto
import android.os.Handler import android.os.Handler
import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore
import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap
import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyRequestBody import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyRequestBody
import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyShareCancellation import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyShareCancellation
import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyShareRequest import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyShareRequest
import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore
import im.vector.matrix.android.internal.crypto.tasks.SendToDeviceTask import im.vector.matrix.android.internal.crypto.tasks.SendToDeviceTask
import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith import im.vector.matrix.android.internal.task.configureWith
@ -36,9 +36,6 @@ internal class MXOutgoingRoomKeyRequestManager(
private val mSendToDeviceTask: SendToDeviceTask, private val mSendToDeviceTask: SendToDeviceTask,
private val mTaskExecutor: TaskExecutor) { private val mTaskExecutor: TaskExecutor) {


// working handler (should not be the UI thread)
private lateinit var mWorkingHandler: Handler

// running // running
var mClientRunning: Boolean = false var mClientRunning: Boolean = false


@ -49,10 +46,6 @@ internal class MXOutgoingRoomKeyRequestManager(
// of mSendOutgoingRoomKeyRequestsTimer // of mSendOutgoingRoomKeyRequestsTimer
private var mSendOutgoingRoomKeyRequestsRunning: Boolean = false private var mSendOutgoingRoomKeyRequestsRunning: Boolean = false


fun setWorkingHandler(encryptingThreadHandler: Handler) {
mWorkingHandler = encryptingThreadHandler
}

/** /**
* Called when the client is started. Sets background processes running. * Called when the client is started. Sets background processes running.
*/ */
@ -90,14 +83,12 @@ internal class MXOutgoingRoomKeyRequestManager(
* @param recipients recipients * @param recipients recipients
*/ */
fun sendRoomKeyRequest(requestBody: RoomKeyRequestBody?, recipients: List<Map<String, String>>) { fun sendRoomKeyRequest(requestBody: RoomKeyRequestBody?, recipients: List<Map<String, String>>) {
mWorkingHandler.post { val req = mCryptoStore.getOrAddOutgoingRoomKeyRequest(
val req = mCryptoStore.getOrAddOutgoingRoomKeyRequest( OutgoingRoomKeyRequest(requestBody, recipients, makeTxnId(), OutgoingRoomKeyRequest.RequestState.UNSENT))
OutgoingRoomKeyRequest(requestBody, recipients, makeTxnId(), OutgoingRoomKeyRequest.RequestState.UNSENT))




if (req!!.mState === OutgoingRoomKeyRequest.RequestState.UNSENT) { if (req!!.mState === OutgoingRoomKeyRequest.RequestState.UNSENT) {
startTimer() startTimer()
}
} }
} }


@ -154,21 +145,19 @@ internal class MXOutgoingRoomKeyRequestManager(
* Start the background timer to send queued requests, if the timer isn't already running. * Start the background timer to send queued requests, if the timer isn't already running.
*/ */
private fun startTimer() { private fun startTimer() {
mWorkingHandler.post(Runnable { if (mSendOutgoingRoomKeyRequestsRunning) {
return
}

Handler().postDelayed(Runnable {
if (mSendOutgoingRoomKeyRequestsRunning) { if (mSendOutgoingRoomKeyRequestsRunning) {
Timber.d("## startTimer() : RoomKeyRequestSend already in progress!")
return@Runnable return@Runnable
} }


mWorkingHandler.postDelayed(Runnable { mSendOutgoingRoomKeyRequestsRunning = true
if (mSendOutgoingRoomKeyRequestsRunning) { sendOutgoingRoomKeyRequests()
Timber.d("## startTimer() : RoomKeyRequestSend already in progress!") }, SEND_KEY_REQUESTS_DELAY_MS.toLong())
return@Runnable
}

mSendOutgoingRoomKeyRequestsRunning = true
sendOutgoingRoomKeyRequests()
}, SEND_KEY_REQUESTS_DELAY_MS.toLong())
})
} }


// look for and send any queued requests. Runs itself recursively until // look for and send any queued requests. Runs itself recursively until
@ -215,17 +204,15 @@ internal class MXOutgoingRoomKeyRequestManager(


sendMessageToDevices(requestMessage, request.mRecipients, request.mRequestId, object : MatrixCallback<Unit> { sendMessageToDevices(requestMessage, request.mRecipients, request.mRequestId, object : MatrixCallback<Unit> {
private fun onDone(state: OutgoingRoomKeyRequest.RequestState) { private fun onDone(state: OutgoingRoomKeyRequest.RequestState) {
mWorkingHandler.post { if (request.mState !== OutgoingRoomKeyRequest.RequestState.UNSENT) {
if (request.mState !== OutgoingRoomKeyRequest.RequestState.UNSENT) { Timber.d("## sendOutgoingRoomKeyRequest() : Cannot update room key request from UNSENT as it was already updated to " + request.mState)
Timber.d("## sendOutgoingRoomKeyRequest() : Cannot update room key request from UNSENT as it was already updated to " + request.mState) } else {
} else { request.mState = state
request.mState = state mCryptoStore.updateOutgoingRoomKeyRequest(request)
mCryptoStore.updateOutgoingRoomKeyRequest(request)
}

mSendOutgoingRoomKeyRequestsRunning = false
startTimer()
} }

mSendOutgoingRoomKeyRequestsRunning = false
startTimer()
} }


override fun onSuccess(data: Unit) { override fun onSuccess(data: Unit) {
@ -256,11 +243,9 @@ internal class MXOutgoingRoomKeyRequestManager(


sendMessageToDevices(roomKeyShareCancellation, request.mRecipients, request.mCancellationTxnId, object : MatrixCallback<Unit> { sendMessageToDevices(roomKeyShareCancellation, request.mRecipients, request.mCancellationTxnId, object : MatrixCallback<Unit> {
private fun onDone() { private fun onDone() {
mWorkingHandler.post { mCryptoStore.deleteOutgoingRoomKeyRequest(request.mRequestId)
mCryptoStore.deleteOutgoingRoomKeyRequest(request.mRequestId) mSendOutgoingRoomKeyRequestsRunning = false
mSendOutgoingRoomKeyRequestsRunning = false startTimer()
startTimer()
}
} }





View File

@ -154,10 +154,8 @@ internal class MXMegolmEncryption : IMXEncrypting {
override fun onSuccess(devicesInRoom: MXUsersDevicesMap<MXDeviceInfo>) { override fun onSuccess(devicesInRoom: MXUsersDevicesMap<MXDeviceInfo>) {
ensureOutboundSession(devicesInRoom, object : MatrixCallback<MXOutboundSessionInfo> { ensureOutboundSession(devicesInRoom, object : MatrixCallback<MXOutboundSessionInfo> {
override fun onSuccess(data: MXOutboundSessionInfo) { override fun onSuccess(data: MXOutboundSessionInfo) {
mCrypto!!.encryptingThreadHandler.post { Timber.d("## encryptEventContent () processPendingEncryptions after " + (System.currentTimeMillis() - t0) + "ms")
Timber.d("## encryptEventContent () processPendingEncryptions after " + (System.currentTimeMillis() - t0) + "ms") processPendingEncryptions(data)
processPendingEncryptions(data)
}
} }


override fun onFailure(failure: Throwable) { override fun onFailure(failure: Throwable) {
@ -178,12 +176,12 @@ internal class MXMegolmEncryption : IMXEncrypting {
* @return the session description * @return the session description
*/ */
private fun prepareNewSessionInRoom(): MXOutboundSessionInfo { private fun prepareNewSessionInRoom(): MXOutboundSessionInfo {
val sessionId = olmDevice!!.createOutboundGroupSession() val sessionId = olmDevice.createOutboundGroupSession()


val keysClaimedMap = HashMap<String, String>() val keysClaimedMap = HashMap<String, String>()
keysClaimedMap["ed25519"] = olmDevice.deviceEd25519Key!! keysClaimedMap["ed25519"] = olmDevice.deviceEd25519Key!!


olmDevice.addInboundGroupSession(sessionId!!, olmDevice.getSessionKey(sessionId)!!, mRoomId!!, olmDevice.deviceCurve25519Key!!, olmDevice.addInboundGroupSession(sessionId!!, olmDevice.getSessionKey(sessionId)!!, mRoomId, olmDevice.deviceCurve25519Key!!,
ArrayList(), keysClaimedMap, false) ArrayList(), keysClaimedMap, false)


mKeysBackup.maybeBackupKeys() mKeysBackup.maybeBackupKeys()
@ -296,12 +294,10 @@ internal class MXMegolmEncryption : IMXEncrypting {
Timber.d("## shareKey() ; userId $userIds") Timber.d("## shareKey() ; userId $userIds")
shareUserDevicesKey(session, subMap, object : MatrixCallback<Unit> { shareUserDevicesKey(session, subMap, object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) { override fun onSuccess(data: Unit) {
mCrypto!!.encryptingThreadHandler.post { for (userId in userIds) {
for (userId in userIds) { devicesByUsers.remove(userId)
devicesByUsers.remove(userId)
}
shareKey(session, devicesByUsers, callback)
} }
shareKey(session, devicesByUsers, callback)
} }


override fun onFailure(failure: Throwable) { override fun onFailure(failure: Throwable) {
@ -326,7 +322,7 @@ internal class MXMegolmEncryption : IMXEncrypting {


val submap = HashMap<String, Any>() val submap = HashMap<String, Any>()
submap["algorithm"] = MXCRYPTO_ALGORITHM_MEGOLM submap["algorithm"] = MXCRYPTO_ALGORITHM_MEGOLM
submap["room_id"] = mRoomId!! submap["room_id"] = mRoomId
submap["session_id"] = session.mSessionId submap["session_id"] = session.mSessionId
submap["session_key"] = sessionKey!! submap["session_key"] = sessionKey!!
submap["chain_index"] = chainIndex submap["chain_index"] = chainIndex
@ -338,88 +334,84 @@ internal class MXMegolmEncryption : IMXEncrypting {
val t0 = System.currentTimeMillis() val t0 = System.currentTimeMillis()
Timber.d("## shareUserDevicesKey() : starts") Timber.d("## shareUserDevicesKey() : starts")


mCrypto!!.ensureOlmSessionsForDevices(devicesByUser, object : MatrixCallback<MXUsersDevicesMap<MXOlmSessionResult>> { mCrypto.ensureOlmSessionsForDevices(devicesByUser, object : MatrixCallback<MXUsersDevicesMap<MXOlmSessionResult>> {
override fun onSuccess(results: MXUsersDevicesMap<MXOlmSessionResult>) { override fun onSuccess(data: MXUsersDevicesMap<MXOlmSessionResult>) {
mCrypto!!.encryptingThreadHandler.post { Timber.d("## shareUserDevicesKey() : ensureOlmSessionsForDevices succeeds after "
Timber.d("## shareUserDevicesKey() : ensureOlmSessionsForDevices succeeds after " + (System.currentTimeMillis() - t0) + " ms")
+ (System.currentTimeMillis() - t0) + " ms") val contentMap = MXUsersDevicesMap<Any>()
val contentMap = MXUsersDevicesMap<Any>()


var haveTargets = false var haveTargets = false
val userIds = results.userIds val userIds = data.userIds


for (userId in userIds) { for (userId in userIds) {
val devicesToShareWith = devicesByUser[userId] val devicesToShareWith = devicesByUser[userId]


for ((deviceID) in devicesToShareWith!!) { for ((deviceID) in devicesToShareWith!!) {


val sessionResult = results.getObject(deviceID, userId) val sessionResult = data.getObject(deviceID, userId)


if (null == sessionResult || null == sessionResult.mSessionId) { if (null == sessionResult || null == sessionResult.mSessionId) {
// no session with this device, probably because there // no session with this device, probably because there
// were no one-time keys. // were no one-time keys.
// //
// we could send them a to_device message anyway, as a // we could send them a to_device message anyway, as a
// signal that they have missed out on the key sharing // signal that they have missed out on the key sharing
// message because of the lack of keys, but there's not // message because of the lack of keys, but there's not
// much point in that really; it will mostly serve to clog // much point in that really; it will mostly serve to clog
// up to_device inboxes. // up to_device inboxes.
// //
// ensureOlmSessionsForUsers has already done the logging, // ensureOlmSessionsForUsers has already done the logging,
// so just skip it. // so just skip it.
continue continue
}

Timber.d("## shareUserDevicesKey() : Sharing keys with device $userId:$deviceID")
//noinspection ArraysAsListWithZeroOrOneArgument,ArraysAsListWithZeroOrOneArgument
contentMap.setObject(mCrypto!!.encryptMessage(payload, Arrays.asList(sessionResult.mDevice)), userId, deviceID)
haveTargets = true
} }

Timber.d("## shareUserDevicesKey() : Sharing keys with device $userId:$deviceID")
//noinspection ArraysAsListWithZeroOrOneArgument,ArraysAsListWithZeroOrOneArgument
contentMap.setObject(mCrypto.encryptMessage(payload, Arrays.asList(sessionResult.mDevice)), userId, deviceID)
haveTargets = true
} }
}


if (haveTargets && !mCrypto!!.hasBeenReleased()) { if (haveTargets) {
val t0 = System.currentTimeMillis() val t0 = System.currentTimeMillis()
Timber.d("## shareUserDevicesKey() : has target") Timber.d("## shareUserDevicesKey() : has target")


mSendToDeviceTask.configureWith(SendToDeviceTask.Params(EventType.ENCRYPTED, contentMap)) mSendToDeviceTask.configureWith(SendToDeviceTask.Params(EventType.ENCRYPTED, contentMap))
.dispatchTo(object : MatrixCallback<Unit> { .dispatchTo(object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) { override fun onSuccess(data: Unit) {
mCrypto!!.encryptingThreadHandler.post { Timber.d("## shareUserDevicesKey() : sendToDevice succeeds after "
Timber.d("## shareUserDevicesKey() : sendToDevice succeeds after " + (System.currentTimeMillis() - t0) + " ms")
+ (System.currentTimeMillis() - t0) + " ms")


// Add the devices we have shared with to session.sharedWithDevices. // Add the devices we have shared with to session.sharedWithDevices.
// we deliberately iterate over devicesByUser (ie, the devices we // we deliberately iterate over devicesByUser (ie, the devices we
// attempted to share with) rather than the contentMap (those we did // attempted to share with) rather than the contentMap (those we did
// share with), because we don't want to try to claim a one-time-key // share with), because we don't want to try to claim a one-time-key
// for dead devices on every message. // for dead devices on every message.
for (userId in devicesByUser.keys) { for (userId in devicesByUser.keys) {
val devicesToShareWith = devicesByUser[userId] val devicesToShareWith = devicesByUser[userId]


for ((deviceId) in devicesToShareWith!!) { for ((deviceId) in devicesToShareWith!!) {
session.mSharedWithDevices.setObject(chainIndex, userId, deviceId) session.mSharedWithDevices.setObject(chainIndex, userId, deviceId)
}
}

CryptoAsyncHelper.getUiHandler().post {
callback?.onSuccess(Unit)
}
} }
} }


override fun onFailure(failure: Throwable) { CryptoAsyncHelper.getUiHandler().post {
Timber.e(failure, "## shareUserDevicesKey() : sendToDevice") callback?.onSuccess(Unit)

callback?.onFailure(failure)
} }
}) }
.executeBy(mTaskExecutor)
} else {
Timber.d("## shareUserDevicesKey() : no need to sharekey")


if (null != callback) { override fun onFailure(failure: Throwable) {
CryptoAsyncHelper.getUiHandler().post { callback.onSuccess(Unit) } Timber.e(failure, "## shareUserDevicesKey() : sendToDevice")
}
callback?.onFailure(failure)
}
})
.executeBy(mTaskExecutor)
} else {
Timber.d("## shareUserDevicesKey() : no need to sharekey")

if (null != callback) {
CryptoAsyncHelper.getUiHandler().post { callback.onSuccess(Unit) }
} }
} }
} }
@ -443,7 +435,7 @@ internal class MXMegolmEncryption : IMXEncrypting {
for (queuedEncryption in queuedEncryptions) { for (queuedEncryption in queuedEncryptions) {
val payloadJson = HashMap<String, Any>() val payloadJson = HashMap<String, Any>()


payloadJson["room_id"] = mRoomId!! payloadJson["room_id"] = mRoomId
payloadJson["type"] = queuedEncryption.mEventType!! payloadJson["type"] = queuedEncryption.mEventType!!
payloadJson["content"] = queuedEncryption.mEventContent!! payloadJson["content"] = queuedEncryption.mEventContent!!


@ -487,54 +479,50 @@ internal class MXMegolmEncryption : IMXEncrypting {
// with them, which means that they will have announced any new devices via // with them, which means that they will have announced any new devices via
// an m.new_device. // an m.new_device.
mDeviceListManager.downloadKeys(userIds, false, object : MatrixCallback<MXUsersDevicesMap<MXDeviceInfo>> { mDeviceListManager.downloadKeys(userIds, false, object : MatrixCallback<MXUsersDevicesMap<MXDeviceInfo>> {
override fun onSuccess(devices: MXUsersDevicesMap<MXDeviceInfo>) { override fun onSuccess(data: MXUsersDevicesMap<MXDeviceInfo>) {
mCrypto!!.encryptingThreadHandler.post { val encryptToVerifiedDevicesOnly = mCrypto.getGlobalBlacklistUnverifiedDevices() || mCrypto.isRoomBlacklistUnverifiedDevices(mRoomId)
val encryptToVerifiedDevicesOnly = mCrypto!!.globalBlacklistUnverifiedDevices || mCrypto!!.isRoomBlacklistUnverifiedDevices(mRoomId)


val devicesInRoom = MXUsersDevicesMap<MXDeviceInfo>() val devicesInRoom = MXUsersDevicesMap<MXDeviceInfo>()
val unknownDevices = MXUsersDevicesMap<MXDeviceInfo>() val unknownDevices = MXUsersDevicesMap<MXDeviceInfo>()


val userIds = devices.userIds for (userId in data.userIds) {
val deviceIds = data.getUserDeviceIds(userId)


for (userId in userIds) { for (deviceId in deviceIds!!) {
val deviceIds = devices.getUserDeviceIds(userId) val deviceInfo = data.getObject(deviceId, userId)


for (deviceId in deviceIds!!) { if (mCrypto.warnOnUnknownDevices() && deviceInfo!!.isUnknown) {
val deviceInfo = devices.getObject(deviceId, userId) // The device is not yet known by the user

unknownDevices.setObject(deviceInfo, userId, deviceId)
if (mCrypto!!.warnOnUnknownDevices() && deviceInfo!!.isUnknown) { continue
// The device is not yet known by the user
unknownDevices.setObject(deviceInfo, userId, deviceId)
continue
}

if (deviceInfo!!.isBlocked) {
// Remove any blocked devices
continue
}

if (!deviceInfo.isVerified && encryptToVerifiedDevicesOnly) {
continue
}

if (TextUtils.equals(deviceInfo.identityKey(), olmDevice.deviceCurve25519Key)) {
// Don't bother sending to ourself
continue
}

devicesInRoom.setObject(deviceInfo, userId, deviceId)
} }

if (deviceInfo!!.isBlocked) {
// Remove any blocked devices
continue
}

if (!deviceInfo.isVerified && encryptToVerifiedDevicesOnly) {
continue
}

if (TextUtils.equals(deviceInfo.identityKey(), olmDevice.deviceCurve25519Key)) {
// Don't bother sending to ourself
continue
}

devicesInRoom.setObject(deviceInfo, userId, deviceId)
} }
}


CryptoAsyncHelper.getUiHandler().post { CryptoAsyncHelper.getUiHandler().post {
// Check if any of these devices are not yet known to the user. // Check if any of these devices are not yet known to the user.
// if so, warn the user so they can verify or ignore. // if so, warn the user so they can verify or ignore.
if (0 != unknownDevices.map.size) { if (0 != unknownDevices.map.size) {
callback.onFailure(Failure.CryptoError(MXCryptoError(MXCryptoError.UNKNOWN_DEVICES_CODE, callback.onFailure(Failure.CryptoError(MXCryptoError(MXCryptoError.UNKNOWN_DEVICES_CODE,
MXCryptoError.UNABLE_TO_ENCRYPT, MXCryptoError.UNKNOWN_DEVICES_REASON, unknownDevices))) MXCryptoError.UNABLE_TO_ENCRYPT, MXCryptoError.UNKNOWN_DEVICES_REASON, unknownDevices)))
} else { } else {
callback.onSuccess(devicesInRoom) callback.onSuccess(devicesInRoom)
}
} }
} }
} }

View File

@ -701,8 +701,4 @@ internal class RealmCryptoStore(private val enableFileEncryption: Boolean = fals
} }
.toMutableList() .toMutableList()
} }

companion object {
private const val LOG_TAG = "RealmCryptoStore"
}
} }

View File

@ -29,10 +29,10 @@ import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.events.model.toModel import im.vector.matrix.android.api.session.events.model.toModel
import im.vector.matrix.android.internal.crypto.CryptoAsyncHelper import im.vector.matrix.android.internal.crypto.CryptoAsyncHelper
import im.vector.matrix.android.internal.crypto.DeviceListManager import im.vector.matrix.android.internal.crypto.DeviceListManager
import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore
import im.vector.matrix.android.internal.crypto.model.MXDeviceInfo import im.vector.matrix.android.internal.crypto.model.MXDeviceInfo
import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap
import im.vector.matrix.android.internal.crypto.model.rest.* import im.vector.matrix.android.internal.crypto.model.rest.*
import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore
import im.vector.matrix.android.internal.crypto.tasks.SendToDeviceTask import im.vector.matrix.android.internal.crypto.tasks.SendToDeviceTask
import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith import im.vector.matrix.android.internal.task.configureWith
@ -59,9 +59,10 @@ internal class DefaultSasVerificationService(private val mCredentials: Credentia


// Event received from the sync // Event received from the sync
fun onToDeviceEvent(event: Event) { fun onToDeviceEvent(event: Event) {
CryptoAsyncHelper.getDecryptBackgroundHandler().post { // TODO We are already in a BG thread CryptoAsyncHelper.getDecryptBackgroundHandler().post {
// TODO We are already in a BG thread
when (event.type) { when (event.type) {
EventType.KEY_VERIFICATION_START -> { EventType.KEY_VERIFICATION_START -> {
onStartRequestReceived(event) onStartRequestReceived(event)
} }
EventType.KEY_VERIFICATION_CANCEL -> { EventType.KEY_VERIFICATION_CANCEL -> {
@ -70,13 +71,13 @@ internal class DefaultSasVerificationService(private val mCredentials: Credentia
EventType.KEY_VERIFICATION_ACCEPT -> { EventType.KEY_VERIFICATION_ACCEPT -> {
onAcceptReceived(event) onAcceptReceived(event)
} }
EventType.KEY_VERIFICATION_KEY -> { EventType.KEY_VERIFICATION_KEY -> {
onKeyReceived(event) onKeyReceived(event)
} }
EventType.KEY_VERIFICATION_MAC -> { EventType.KEY_VERIFICATION_MAC -> {
onMacReceived(event) onMacReceived(event)
} }
else -> { else -> {
//ignore //ignore
} }
} }
@ -131,24 +132,15 @@ internal class DefaultSasVerificationService(private val mCredentials: Credentia
override fun markedLocallyAsManuallyVerified(userId: String, deviceID: String) { override fun markedLocallyAsManuallyVerified(userId: String, deviceID: String) {
mCryptoListener.setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_VERIFIED, mCryptoListener.setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_VERIFIED,
deviceID, deviceID,
userId, userId)
object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {
uiHandler.post {
listeners.forEach {
try {
it.markedAsManuallyVerified(userId, deviceID)
} catch (e: Throwable) {
Timber.e(e, "## Error while notifying listeners")
}
}
}
}


override fun onFailure(failure: Throwable) { listeners.forEach {
Timber.e(failure, "## Manual verification failed in state") try {
} it.markedAsManuallyVerified(userId, deviceID)
}) } catch (e: Throwable) {
Timber.e(e, "## Error while notifying listeners")
}
}
} }


private fun onStartRequestReceived(event: Event) { private fun onStartRequestReceived(event: Event) {
@ -435,12 +427,12 @@ internal class DefaultSasVerificationService(private val mCredentials: Credentia
mCryptoListener = listener mCryptoListener = listener
} }


fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String, callback: MatrixCallback<Unit>) { fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String) {
mCryptoListener.setDeviceVerification(verificationStatus, deviceId, userId, callback) mCryptoListener.setDeviceVerification(verificationStatus, deviceId, userId)
} }


interface SasCryptoListener { interface SasCryptoListener {
fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String, callback: MatrixCallback<Unit>) fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String)
fun getMyDevice(): MXDeviceInfo fun getMyDevice(): MXDeviceInfo
} }
} }

View File

@ -239,33 +239,15 @@ internal abstract class SASVerificationTransaction(


setDeviceVerified( setDeviceVerified(
otherDeviceId ?: "", otherDeviceId ?: "",
otherUserId, otherUserId)
success = {
state = SasVerificationTxState.Verified state = SasVerificationTxState.Verified
},
error = {
//mmm what to do?, looks like this is never called
}
)
} }


private fun setDeviceVerified(deviceId: String, userId: String, success: () -> Unit, error: () -> Unit) { private fun setDeviceVerified(deviceId: String, userId: String) {
mSasVerificationService.setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_VERIFIED, mSasVerificationService.setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_VERIFIED,
deviceId, deviceId,
userId, userId)
object : MatrixCallback<Unit> {

override fun onSuccess(data: Unit) {
//We good
Timber.d("## SAS verification complete and device status updated for id:$transactionId")
success()
}

override fun onFailure(failure: Throwable) {
Timber.e(failure, "## SAS verification [$transactionId] failed in state : $state")
error()
}
})
} }


override fun cancel() { override fun cancel() {

View File

@ -257,16 +257,16 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi
return cryptoService.getKeysBackupService() return cryptoService.getKeysBackupService()
} }


override fun isRoomBlacklistUnverifiedDevices(roomId: String, callback: MatrixCallback<Boolean>?) { override fun isRoomBlacklistUnverifiedDevices(roomId: String?): Boolean {
cryptoService.isRoomBlacklistUnverifiedDevices(roomId, callback) return cryptoService.isRoomBlacklistUnverifiedDevices(roomId)
} }


override fun setWarnOnUnknownDevices(warn: Boolean) { override fun setWarnOnUnknownDevices(warn: Boolean) {
cryptoService.setWarnOnUnknownDevices(warn) cryptoService.setWarnOnUnknownDevices(warn)
} }


override fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String, callback: MatrixCallback<Unit>) { override fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String) {
cryptoService.setDeviceVerification(verificationStatus, deviceId, userId, callback) cryptoService.setDeviceVerification(verificationStatus, deviceId, userId)
} }


override fun getUserDevices(userId: String): MutableList<MXDeviceInfo> { override fun getUserDevices(userId: String): MutableList<MXDeviceInfo> {
@ -293,16 +293,16 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi
return cryptoService.inboundGroupSessionsCount(onlyBackedUp) return cryptoService.inboundGroupSessionsCount(onlyBackedUp)
} }


override fun getGlobalBlacklistUnverifiedDevices(callback: MatrixCallback<Boolean>?) { override fun getGlobalBlacklistUnverifiedDevices(): Boolean {
cryptoService.getGlobalBlacklistUnverifiedDevices(callback) return cryptoService.getGlobalBlacklistUnverifiedDevices()
} }


override fun setGlobalBlacklistUnverifiedDevices(block: Boolean, callback: MatrixCallback<Unit>?) { override fun setGlobalBlacklistUnverifiedDevices(block: Boolean) {
cryptoService.setGlobalBlacklistUnverifiedDevices(block, callback) cryptoService.setGlobalBlacklistUnverifiedDevices(block)
} }


override fun setRoomUnBlacklistUnverifiedDevices(roomId: String, callback: MatrixCallback<Unit>) { override fun setRoomUnBlacklistUnverifiedDevices(roomId: String) {
cryptoService.setRoomUnBlacklistUnverifiedDevices(roomId, callback) cryptoService.setRoomUnBlacklistUnverifiedDevices(roomId)
} }


override fun getDeviceTrackingStatus(userId: String): Int { override fun getDeviceTrackingStatus(userId: String): Int {
@ -317,12 +317,12 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi
cryptoService.exportRoomKeys(password, callback) cryptoService.exportRoomKeys(password, callback)
} }


override fun setRoomBlacklistUnverifiedDevices(roomId: String, callback: MatrixCallback<Unit>) { override fun setRoomBlacklistUnverifiedDevices(roomId: String) {
cryptoService.setRoomBlacklistUnverifiedDevices(roomId, callback) cryptoService.setRoomBlacklistUnverifiedDevices(roomId)
} }


override fun getDeviceInfo(userId: String, deviceId: String?, callback: MatrixCallback<MXDeviceInfo?>) { override fun getDeviceInfo(userId: String, deviceId: String?): MXDeviceInfo? {
cryptoService.getDeviceInfo(userId, deviceId, callback) return cryptoService.getDeviceInfo(userId, deviceId)
} }


override fun reRequestRoomKeyForEvent(event: Event) { override fun reRequestRoomKeyForEvent(event: Event) {

View File

@ -47,7 +47,6 @@ import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.extensions.getFingerprintHumanReadable import im.vector.matrix.android.api.extensions.getFingerprintHumanReadable
import im.vector.matrix.android.api.extensions.sortByLastSeen import im.vector.matrix.android.api.extensions.sortByLastSeen
import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.internal.crypto.model.MXDeviceInfo
import im.vector.matrix.android.internal.crypto.model.rest.DeviceInfo import im.vector.matrix.android.internal.crypto.model.rest.DeviceInfo
import im.vector.matrix.android.internal.crypto.model.rest.DevicesListResponse import im.vector.matrix.android.internal.crypto.model.rest.DevicesListResponse
import im.vector.riotredesign.R import im.vector.riotredesign.R
@ -2210,42 +2209,26 @@ class VectorSettingsPreferencesFragment : VectorPreferenceFragment(), SharedPref


// crypto section: device key (fingerprint) // crypto section: device key (fingerprint)
if (!TextUtils.isEmpty(deviceId) && !TextUtils.isEmpty(userId)) { if (!TextUtils.isEmpty(deviceId) && !TextUtils.isEmpty(userId)) {
mSession.getDeviceInfo(userId, deviceId, object : MatrixCallback<MXDeviceInfo?> { val deviceInfo = mSession.getDeviceInfo(userId, deviceId)
override fun onSuccess(data: MXDeviceInfo?) {
if (null != data && !TextUtils.isEmpty(data.fingerprint()) && null != activity) {
cryptoInfoTextPreference.summary = data.getFingerprintHumanReadable()


cryptoInfoTextPreference.setOnPreferenceClickListener { if (null != deviceInfo && !TextUtils.isEmpty(deviceInfo.fingerprint())) {
data.fingerprint()?.let { cryptoInfoTextPreference.summary = deviceInfo.getFingerprintHumanReadable()
copyToClipboard(requireActivity(), it)
} cryptoInfoTextPreference.setOnPreferenceClickListener {
true deviceInfo.fingerprint()?.let {
} copyToClipboard(requireActivity(), it)
} }
true
} }
}) }
} }


sendToUnverifiedDevicesPref.isChecked = false sendToUnverifiedDevicesPref.isChecked = false


mSession.getGlobalBlacklistUnverifiedDevices(object : MatrixCallback<Boolean> { sendToUnverifiedDevicesPref.isChecked = mSession.getGlobalBlacklistUnverifiedDevices()
override fun onSuccess(data: Boolean) {
sendToUnverifiedDevicesPref.isChecked = data
}
})


sendToUnverifiedDevicesPref.onPreferenceClickListener = Preference.OnPreferenceClickListener { sendToUnverifiedDevicesPref.onPreferenceClickListener = Preference.OnPreferenceClickListener {
mSession.getGlobalBlacklistUnverifiedDevices(object : MatrixCallback<Boolean> { mSession.setGlobalBlacklistUnverifiedDevices(sendToUnverifiedDevicesPref.isChecked)
override fun onSuccess(data: Boolean) {
if (sendToUnverifiedDevicesPref.isChecked != data) {
mSession.setGlobalBlacklistUnverifiedDevices(sendToUnverifiedDevicesPref.isChecked, object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {

}
})
}
}
})


true true
} }
@ -2871,8 +2854,6 @@ if (sharedDataItems.isNotEmpty() && thisActivity != null) {
* ========================================================================================== */ * ========================================================================================== */


companion object { companion object {
private val LOG_TAG = VectorSettingsPreferencesFragment::class.java.simpleName

// arguments indexes // arguments indexes
private const val ARG_MATRIX_ID = "VectorSettingsPreferencesFragment.ARG_MATRIX_ID" private const val ARG_MATRIX_ID = "VectorSettingsPreferencesFragment.ARG_MATRIX_ID"