Continue cleaning up code and fix some verification code

This commit is contained in:
ganfra 2022-04-21 20:09:08 +02:00
parent 48793f531c
commit 39755b08ee
6 changed files with 49 additions and 43 deletions

View File

@ -17,7 +17,6 @@
package org.matrix.android.sdk.api.session.crypto.crosssigning package org.matrix.android.sdk.api.session.crypto.crosssigning
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.auth.UserInteractiveAuthInterceptor import org.matrix.android.sdk.api.auth.UserInteractiveAuthInterceptor
import org.matrix.android.sdk.api.crypto.RoomEncryptionTrustLevel import org.matrix.android.sdk.api.crypto.RoomEncryptionTrustLevel
import org.matrix.android.sdk.api.util.Optional import org.matrix.android.sdk.api.util.Optional
@ -92,8 +91,7 @@ interface CrossSigningService {
fun allPrivateKeysKnown(): Boolean fun allPrivateKeysKnown(): Boolean
/** Mark a user identity as trusted and sign and upload signatures of our user-signing key to the server */ /** Mark a user identity as trusted and sign and upload signatures of our user-signing key to the server */
suspend fun trustUser(otherUserId: String, suspend fun trustUser(otherUserId: String)
callback: MatrixCallback<Unit>)
/** Mark our own master key as trusted */ /** Mark our own master key as trusted */
suspend fun markMyMasterKeyAsTrusted() suspend fun markMyMasterKeyAsTrusted()

View File

@ -91,6 +91,7 @@ object EventType {
const val SEND_SECRET = "m.secret.send" const val SEND_SECRET = "m.secret.send"
// Interactive key verification // Interactive key verification
const val KEY_VERIFICATION_REQUEST = "m.key.verification.request"
const val KEY_VERIFICATION_START = "m.key.verification.start" const val KEY_VERIFICATION_START = "m.key.verification.start"
const val KEY_VERIFICATION_ACCEPT = "m.key.verification.accept" const val KEY_VERIFICATION_ACCEPT = "m.key.verification.accept"
const val KEY_VERIFICATION_KEY = "m.key.verification.key" const val KEY_VERIFICATION_KEY = "m.key.verification.key"

View File

@ -16,6 +16,8 @@
package org.matrix.android.sdk.api.session.room.model.message package org.matrix.android.sdk.api.session.room.model.message
import org.matrix.android.sdk.api.session.events.model.EventType
object MessageType { object MessageType {
const val MSGTYPE_TEXT = "m.text" const val MSGTYPE_TEXT = "m.text"
const val MSGTYPE_EMOTE = "m.emote" const val MSGTYPE_EMOTE = "m.emote"
@ -26,7 +28,7 @@ object MessageType {
const val MSGTYPE_LOCATION = "m.location" const val MSGTYPE_LOCATION = "m.location"
const val MSGTYPE_FILE = "m.file" const val MSGTYPE_FILE = "m.file"
const val MSGTYPE_VERIFICATION_REQUEST = "m.key.verification.request" const val MSGTYPE_VERIFICATION_REQUEST = EventType.KEY_VERIFICATION_REQUEST
// Add, in local, a fake message type in order to StickerMessage can inherit Message class // Add, in local, a fake message type in order to StickerMessage can inherit Message class
// Because sticker isn't a message type but a event type without msgtype field // Because sticker isn't a message type but a event type without msgtype field

View File

@ -146,22 +146,20 @@ internal class RustCrossSigningService @Inject constructor(
} }
/** Mark a user identity as trusted and sign and upload signatures of our user-signing key to the server */ /** Mark a user identity as trusted and sign and upload signatures of our user-signing key to the server */
override suspend fun trustUser(otherUserId: String, callback: MatrixCallback<Unit>) { override suspend fun trustUser(otherUserId: String) {
// This is only used in a test // This is only used in a test
val userIdentity = olmMachine.getIdentity(otherUserId) val userIdentity = olmMachine.getIdentity(otherUserId)
if (userIdentity != null) { if (userIdentity != null) {
userIdentity.verify() userIdentity.verify()
callback.onSuccess(Unit)
} else { } else {
callback.onFailure(Throwable("## CrossSigning - CrossSigning is not setup for this account")) throw Throwable("## CrossSigning - CrossSigning is not setup for this account")
} }
} }
/** Mark our own master key as trusted */ /** Mark our own master key as trusted */
override suspend fun markMyMasterKeyAsTrusted() { override suspend fun markMyMasterKeyAsTrusted() {
// This doesn't seem to be used? // This doesn't seem to be used?
trustUser(olmMachine.userId(), NoOpMatrixCallback()) trustUser(olmMachine.userId())
} }
/** /**

View File

@ -67,7 +67,6 @@ internal class DefaultSendVerificationMessageTask @Inject constructor(
return encryptEventTask.execute(EncryptEventTask.Params( return encryptEventTask.execute(EncryptEventTask.Params(
params.event.roomId ?: "", params.event.roomId ?: "",
params.event, params.event,
listOf("m.relates_to")
)) ))
} catch (throwable: Throwable) { } catch (throwable: Throwable) {
// We said it's ok to send verification request in clear // We said it's ok to send verification request in clear

View File

@ -25,6 +25,7 @@ import org.matrix.android.sdk.api.session.crypto.verification.VerificationTransa
import org.matrix.android.sdk.api.session.events.model.Event 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.EventType
import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.room.model.message.MessageContent
import org.matrix.android.sdk.api.session.room.model.message.MessageRelationContent import org.matrix.android.sdk.api.session.room.model.message.MessageRelationContent
import org.matrix.android.sdk.api.session.room.model.message.MessageType import org.matrix.android.sdk.api.session.room.model.message.MessageType
import org.matrix.android.sdk.internal.crypto.OlmMachineProvider import org.matrix.android.sdk.internal.crypto.OlmMachineProvider
@ -76,7 +77,7 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
olmMachineProvider.olmMachine olmMachineProvider.olmMachine
} }
private val dispatcher = UpdateDispatcher(this.olmMachine.verificationListeners) private val dispatcher = UpdateDispatcher(olmMachine.verificationListeners)
/** The main entry point for the verification service /** The main entry point for the verification service
* *
@ -89,18 +90,22 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
* dispatches updates to our listeners. * dispatches updates to our listeners.
*/ */
internal suspend fun onEvent(event: Event) = when (event.getClearType()) { internal suspend fun onEvent(event: Event) = when (event.getClearType()) {
// I'm not entirely sure why getClearType() returns a msgtype in one case EventType.KEY_VERIFICATION_REQUEST -> onRequest(event, fromRoomMessage = false)
// and a event type in the other case, but this is how the old verification EventType.KEY_VERIFICATION_START -> onStart(event)
// service did things and it does seem to work.
MessageType.MSGTYPE_VERIFICATION_REQUEST -> onRequest(event)
EventType.KEY_VERIFICATION_START -> onStart(event)
EventType.KEY_VERIFICATION_READY, EventType.KEY_VERIFICATION_READY,
EventType.KEY_VERIFICATION_ACCEPT, EventType.KEY_VERIFICATION_ACCEPT,
EventType.KEY_VERIFICATION_KEY, EventType.KEY_VERIFICATION_KEY,
EventType.KEY_VERIFICATION_MAC, EventType.KEY_VERIFICATION_MAC,
EventType.KEY_VERIFICATION_CANCEL, EventType.KEY_VERIFICATION_CANCEL,
EventType.KEY_VERIFICATION_DONE -> onUpdate(event) EventType.KEY_VERIFICATION_DONE -> onUpdate(event)
else -> { EventType.MESSAGE -> onRoomMessage(event)
else -> Unit
}
private fun onRoomMessage(event: Event) {
val messageContent = event.getClearContent()?.toModel<MessageContent>() ?: return
if (messageContent.msgType == MessageType.MSGTYPE_VERIFICATION_REQUEST) {
onRequest(event, fromRoomMessage = true)
} }
} }
@ -109,9 +114,9 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
val sender = event.senderId ?: return val sender = event.senderId ?: return
val flowId = getFlowId(event) ?: return val flowId = getFlowId(event) ?: return
this.olmMachine.getVerificationRequest(sender, flowId)?.dispatchRequestUpdated() olmMachine.getVerificationRequest(sender, flowId)?.dispatchRequestUpdated()
val verification = this.getExistingTransaction(sender, flowId) ?: return val verification = getExistingTransaction(sender, flowId) ?: return
this.dispatcher.dispatchTxUpdated(verification) dispatcher.dispatchTxUpdated(verification)
} }
/** Check if the start event created new verification objects and dispatch updates */ /** Check if the start event created new verification objects and dispatch updates */
@ -119,8 +124,8 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
val sender = event.senderId ?: return val sender = event.senderId ?: return
val flowId = getFlowId(event) ?: return val flowId = getFlowId(event) ?: return
val verification = this.getExistingTransaction(sender, flowId) ?: return val verification = getExistingTransaction(sender, flowId) ?: return
val request = this.olmMachine.getVerificationRequest(sender, flowId) val request = olmMachine.getVerificationRequest(sender, flowId)
if (request != null && request.isReady()) { if (request != null && request.isReady()) {
// If this is a SAS verification originating from a `m.key.verification.request` // If this is a SAS verification originating from a `m.key.verification.request`
@ -131,34 +136,37 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
Timber.d("## Verification: Auto accepting SAS verification with $sender") Timber.d("## Verification: Auto accepting SAS verification with $sender")
verification.accept() verification.accept()
} else { } else {
this.dispatcher.dispatchTxUpdated(verification) dispatcher.dispatchTxUpdated(verification)
} }
} else { } else {
// This didn't originate from a request, so tell our listeners that // This didn't originate from a request, so tell our listeners that
// this is a new verification. // this is a new verification.
this.dispatcher.dispatchTxAdded(verification) dispatcher.dispatchTxAdded(verification)
// The IncomingVerificationRequestHandler seems to only listen to updates // The IncomingVerificationRequestHandler seems to only listen to updates
// so let's trigger an update after the addition as well. // so let's trigger an update after the addition as well.
this.dispatcher.dispatchTxUpdated(verification) dispatcher.dispatchTxUpdated(verification)
} }
} }
/** Check if the request event created a nev verification request object and dispatch that it dis so */ /** Check if the request event created a nev verification request object and dispatch that it dis so */
private fun onRequest(event: Event) { private fun onRequest(event: Event, fromRoomMessage: Boolean) {
val flowId = getFlowId(event) ?: return val flowId = if (fromRoomMessage) {
event.eventId
} else {
event.getClearContent().toModel<ToDeviceVerificationEvent>()?.transactionId
} ?: return
val sender = event.senderId ?: return val sender = event.senderId ?: return
val request = getExistingVerificationRequest(sender, flowId) ?: return
val request = this.getExistingVerificationRequest(sender, flowId) ?: return dispatcher.dispatchRequestAdded(request)
this.dispatcher.dispatchRequestAdded(request)
} }
override fun addListener(listener: VerificationService.Listener) { override fun addListener(listener: VerificationService.Listener) {
this.dispatcher.addListener(listener) dispatcher.addListener(listener)
} }
override fun removeListener(listener: VerificationService.Listener) { override fun removeListener(listener: VerificationService.Listener) {
this.dispatcher.removeListener(listener) dispatcher.removeListener(listener)
} }
override suspend fun markedLocallyAsManuallyVerified(userId: String, deviceID: String) { override suspend fun markedLocallyAsManuallyVerified(userId: String, deviceID: String) {
@ -175,13 +183,13 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
otherUserId: String, otherUserId: String,
tid: String, tid: String,
): VerificationTransaction? { ): VerificationTransaction? {
return this.olmMachine.getVerification(otherUserId, tid) return olmMachine.getVerification(otherUserId, tid)
} }
override fun getExistingVerificationRequests( override fun getExistingVerificationRequests(
otherUserId: String otherUserId: String
): List<PendingVerificationRequest> { ): List<PendingVerificationRequest> {
return this.olmMachine.getVerificationRequests(otherUserId).map { return olmMachine.getVerificationRequests(otherUserId).map {
it.toPendingVerificationRequest() it.toPendingVerificationRequest()
} }
} }
@ -191,7 +199,7 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
tid: String? tid: String?
): PendingVerificationRequest? { ): PendingVerificationRequest? {
return if (tid != null) { return if (tid != null) {
this.olmMachine.getVerificationRequest(otherUserId, tid)?.toPendingVerificationRequest() olmMachine.getVerificationRequest(otherUserId, tid)?.toPendingVerificationRequest()
} else { } else {
null null
} }
@ -256,8 +264,7 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
otherUserId: String, otherUserId: String,
transactionId: String transactionId: String
): Boolean { ): Boolean {
val request = this.olmMachine.getVerificationRequest(otherUserId, transactionId) val request = olmMachine.getVerificationRequest(otherUserId, transactionId)
return if (request != null) { return if (request != null) {
request.acceptWithMethods(methods) request.acceptWithMethods(methods)
@ -265,7 +272,7 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
val qrcode = request.startQrVerification() val qrcode = request.startQrVerification()
if (qrcode != null) { if (qrcode != null) {
this.dispatcher.dispatchTxAdded(qrcode) dispatcher.dispatchTxAdded(qrcode)
} }
true true
@ -294,7 +301,7 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
): String? { ): String? {
return if (method == VerificationMethod.SAS) { return if (method == VerificationMethod.SAS) {
if (transactionId != null) { if (transactionId != null) {
val request = this.olmMachine.getVerificationRequest(otherUserId, transactionId) val request = olmMachine.getVerificationRequest(otherUserId, transactionId)
val sas = request?.startSasVerification() val sas = request?.startSasVerification()
@ -310,7 +317,8 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
// be wise do do so as well // be wise do do so as well
// DeviceListBottomSheetViewModel triggers this, interestingly the method that // DeviceListBottomSheetViewModel triggers this, interestingly the method that
// triggers this is called `manuallyVerify()` // triggers this is called `manuallyVerify()`
val verification = olmMachine.getDevice(otherUserId, otherDeviceId)?.startVerification() val otherDevice = olmMachine.getDevice(otherUserId, otherDeviceId)
val verification = otherDevice?.startVerification()
if (verification != null) { if (verification != null) {
dispatcher.dispatchTxAdded(verification) dispatcher.dispatchTxAdded(verification)
verification.transactionId verification.transactionId
@ -338,7 +346,7 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
override suspend fun cancelVerificationRequest(request: PendingVerificationRequest) { override suspend fun cancelVerificationRequest(request: PendingVerificationRequest) {
val verificationRequest = request.transactionId?.let { val verificationRequest = request.transactionId?.let {
this.olmMachine.getVerificationRequest(request.otherUserId, it) olmMachine.getVerificationRequest(request.otherUserId, it)
} }
verificationRequest?.cancel() verificationRequest?.cancel()
} }
@ -348,7 +356,7 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
transactionId: String, transactionId: String,
roomId: String roomId: String
) { ) {
val verificationRequest = this.olmMachine.getVerificationRequest(otherUserId, transactionId) val verificationRequest = olmMachine.getVerificationRequest(otherUserId, transactionId)
verificationRequest?.cancel() verificationRequest?.cancel()
} }
} }