Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #555 from matrix-org/feature/fix_crash
Browse files Browse the repository at this point in the history
Fix a crash on the crypto code (race condition?)
  • Loading branch information
bmarty authored Sep 14, 2020
2 parents 58f933d + 97c657d commit 28d2b7e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Improvements:

Bugfix:
- Killed Application misses some notifications
- Fix a crash on the crypto code (race condition?)

API Change:
-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public MXCryptoImpl(@NonNull CryptoSession matrixSession,
mCryptoStore.storeUserDevices(mSession.getMyUserId(), myDevices);

// Create the VerificationManager before setting the CryptoEventsListener, to avoid crash (vector-im/riot-android#3396)
mShortCodeVerificationManager = new VerificationManager(mSession);
mShortCodeVerificationManager = new VerificationManager(mSession, this);

mSession.getDataHandler().setCryptoEventsListener(mEventListener);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.matrix.androidsdk.core.JsonUtility
import org.matrix.androidsdk.core.Log
import org.matrix.androidsdk.core.callback.ApiCallback
import org.matrix.androidsdk.core.model.MatrixError
import org.matrix.androidsdk.crypto.MXCrypto
import org.matrix.androidsdk.crypto.data.MXDeviceInfo
import org.matrix.androidsdk.crypto.data.MXUsersDevicesMap
import org.matrix.androidsdk.crypto.interfaces.CryptoEvent
Expand All @@ -35,7 +36,10 @@ import kotlin.collections.HashMap
* Short codes interactive verification is a more user friendly way of verifying devices
* that is still maintaining a good level of security (alternative to the 43-character strings compare method).
*/
class VerificationManager(val session: CryptoSession) : VerificationTransaction.Listener {
class VerificationManager (
private val session: CryptoSession,
private val mxCrypto: MXCrypto
) : VerificationTransaction.Listener {

interface VerificationManagerListener {
fun transactionCreated(tx: VerificationTransaction)
Expand All @@ -50,7 +54,7 @@ class VerificationManager(val session: CryptoSession) : VerificationTransaction.

// Event received from the sync
fun onToDeviceEvent(event: CryptoEvent) {
session.requireCrypto().getDecryptingThreadHandler().post {
mxCrypto.getDecryptingThreadHandler().post {
when (event.getType()) {
CryptoEvent.EVENT_TYPE_KEY_VERIFICATION_START -> {
onStartRequestReceived(event)
Expand Down Expand Up @@ -116,7 +120,7 @@ class VerificationManager(val session: CryptoSession) : VerificationTransaction.
}

fun markedLocallyAsManuallyVerified(userId: String, deviceID: String) {
session.requireCrypto().setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_VERIFIED,
mxCrypto.setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_VERIFIED,
deviceID,
userId,
object : ApiCallback<Void> {
Expand Down Expand Up @@ -210,27 +214,27 @@ class VerificationManager(val session: CryptoSession) : VerificationTransaction.
startReq: KeyVerificationStart,
success: (MXUsersDevicesMap<MXDeviceInfo>) -> Unit,
error: () -> Unit) {
session.requireCrypto().getDeviceList().downloadKeys(listOf(otherUserId), true, object : ApiCallback<MXUsersDevicesMap<MXDeviceInfo>> {
mxCrypto.getDeviceList().downloadKeys(listOf(otherUserId), true, object : ApiCallback<MXUsersDevicesMap<MXDeviceInfo>> {
override fun onUnexpectedError(e: Exception) {
session.requireCrypto().getDecryptingThreadHandler().post {
mxCrypto.getDecryptingThreadHandler().post {
error()
}
}

override fun onNetworkError(e: Exception) {
session.requireCrypto().getDecryptingThreadHandler().post {
mxCrypto.getDecryptingThreadHandler().post {
error()
}
}

override fun onMatrixError(e: MatrixError) {
session.requireCrypto().getDecryptingThreadHandler().post {
mxCrypto.getDecryptingThreadHandler().post {
error()
}
}

override fun onSuccess(info: MXUsersDevicesMap<MXDeviceInfo>) {
session.requireCrypto().getDecryptingThreadHandler().post {
mxCrypto.getDecryptingThreadHandler().post {
if (info.getUserDeviceIds(otherUserId).contains(startReq.fromDevice)) {
success(info)
} else {
Expand Down Expand Up @@ -371,7 +375,7 @@ class VerificationManager(val session: CryptoSession) : VerificationTransaction.
if (KeyVerificationStart.VERIF_METHOD_SAS == method) {
val tx = OutgoingSASVerificationRequest(txID, userId, deviceID)
addTransaction(tx)
session.requireCrypto().getDecryptingThreadHandler().post {
mxCrypto.getDecryptingThreadHandler().post {
tx.start(session)
}
return txID
Expand All @@ -387,7 +391,7 @@ class VerificationManager(val session: CryptoSession) : VerificationTransaction.
val buff = StringBuffer()
buff
.append(session.myUserId).append("|")
.append(session.requireCrypto().myDevice.deviceId).append("|")
.append(mxCrypto.myDevice.deviceId).append("|")
.append(userId).append("|")
.append(deviceID).append("|")
.append(UUID.randomUUID().toString())
Expand Down

0 comments on commit 28d2b7e

Please sign in to comment.