Fix / Use transport to start verification

This commit is contained in:
Valere 2019-12-04 18:22:01 +01:00
parent d370f6d7c8
commit 7fc57bdf9b
8 changed files with 92 additions and 16 deletions

View file

@ -52,6 +52,13 @@ interface SasVerificationService {
fun requestKeyVerificationInDMs(userId: String, roomId: String, callback: MatrixCallback<String>?)
fun beginKeyVerificationInDMs(method: String,
transactionId: String,
roomId: String,
otherUserId: String,
otherDeviceId: String,
callback: MatrixCallback<String>?): String?
// fun transactionUpdated(tx: SasVerificationTransaction)
interface SasVerificationListener {

View file

@ -21,7 +21,7 @@ import im.vector.matrix.android.api.session.events.model.Content
import im.vector.matrix.android.api.session.room.model.relation.RelationDefaultContent
@JsonClass(generateAdapter = true)
internal data class MessageVerificationRequestContent(
data class MessageVerificationRequestContent(
@Json(name = "msgtype") override val type: String = MessageType.MSGTYPE_VERIFICATION_REQUEST,
@Json(name = "body") override val body: String,
@Json(name = "from_device") val fromDevice: String,

View file

@ -78,14 +78,15 @@ internal class DefaultOutgoingSASVerificationRequest(
throw IllegalStateException("Interactive Key verification already started")
}
val startMessage = KeyVerificationStart()
startMessage.fromDevice = credentials.deviceId
startMessage.method = KeyVerificationStart.VERIF_METHOD_SAS
startMessage.transactionID = transactionId
startMessage.keyAgreementProtocols = KNOWN_AGREEMENT_PROTOCOLS
startMessage.hashes = KNOWN_HASHES
startMessage.messageAuthenticationCodes = KNOWN_MACS
startMessage.shortAuthenticationStrings = KNOWN_SHORT_CODES
val startMessage = transport.createStart(
credentials.deviceId ?: "",
KeyVerificationStart.VERIF_METHOD_SAS,
transactionId,
KNOWN_AGREEMENT_PROTOCOLS,
KNOWN_HASHES,
KNOWN_MACS,
KNOWN_SHORT_CODES
)
startReq = startMessage
state = SasVerificationTxState.SendingStart

View file

@ -348,7 +348,7 @@ internal class DefaultSasVerificationService @Inject constructor(
if (!cancelReq.isValid()) {
// ignore
Timber.e("## SAS Received invalid accept request")
Timber.e("## SAS Received invalid cancel request")
return
}
val otherUserId = event.senderId!!
@ -477,7 +477,7 @@ internal class DefaultSasVerificationService @Inject constructor(
Timber.v("## SAS Received $macReq")
val existing = getExistingTransaction(senderId, macReq.transactionID!!)
if (existing == null) {
Timber.e("## SAS Received invalid accept request")
Timber.e("## SAS Received invalid Mac request")
return
}
if (existing is SASVerificationTransaction) {
@ -532,6 +532,7 @@ internal class DefaultSasVerificationService @Inject constructor(
txID,
userId,
deviceID)
tx.transport = sasTransportToDeviceFactory.createTransport(tx)
addTransaction(tx)
tx.start()
@ -565,6 +566,28 @@ internal class DefaultSasVerificationService @Inject constructor(
}.executeBy(taskExecutor)
}
override fun beginKeyVerificationInDMs(method: String, transactionId: String, roomId: String,
otherUserId: String, otherDeviceId: String,
callback: MatrixCallback<String>?): String? {
if (KeyVerificationStart.VERIF_METHOD_SAS == method) {
val tx = DefaultOutgoingSASVerificationRequest(
setDeviceVerificationAction,
credentials,
cryptoStore,
myDeviceInfoHolder.get().myDevice.fingerprint()!!,
transactionId,
otherUserId,
otherDeviceId)
tx.transport = sasTransportRoomMessageFactory.createTransport(roomId, cryptoService)
addTransaction(tx)
tx.start()
return transactionId
} else {
throw IllegalArgumentException("Unknown verification method")
}
}
/**
* This string must be unique for the pair of users performing verification for the duration that the transaction is valid
*/

View file

@ -49,5 +49,13 @@ internal interface SasTransport {
fun createKey(tid: String,
pubKey: String): VerificationInfoKey
fun createStart(fromDevice: String,
method: String,
transactionID: String,
keyAgreementProtocols: List<String>,
hashes: List<String>,
messageAuthenticationCodes: List<String>,
shortAuthenticationStrings: List<String>) : VerificationInfoStart
fun createMac(tid: String, mac: Map<String, String>, keys: String): VerificationInfoMac
}

View file

@ -125,6 +125,27 @@ internal class SasTransportRoomMessage(
override fun createKey(tid: String, pubKey: String): VerificationInfoKey = MessageVerificationKeyContent.create(tid, pubKey)
override fun createMac(tid: String, mac: Map<String, String>, keys: String) = MessageVerificationMacContent.create(tid, mac, keys)
override fun createStart(fromDevice: String,
method: String,
transactionID: String,
keyAgreementProtocols: List<String>,
hashes: List<String>,
messageAuthenticationCodes: List<String>,
shortAuthenticationStrings: List<String>): VerificationInfoStart {
return MessageVerificationStartContent(
fromDevice,
hashes,
keyAgreementProtocols,
messageAuthenticationCodes,
shortAuthenticationStrings,
method,
RelationDefaultContent(
type = RelationType.REFERENCE,
eventId = transactionID
)
)
}
}
internal class SasTransportRoomMessageFactory @Inject constructor(

View file

@ -20,10 +20,7 @@ import im.vector.matrix.android.api.session.crypto.sas.CancelCode
import im.vector.matrix.android.api.session.crypto.sas.SasVerificationTxState
import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap
import im.vector.matrix.android.internal.crypto.model.rest.KeyVerificationAccept
import im.vector.matrix.android.internal.crypto.model.rest.KeyVerificationCancel
import im.vector.matrix.android.internal.crypto.model.rest.KeyVerificationKey
import im.vector.matrix.android.internal.crypto.model.rest.KeyVerificationMac
import im.vector.matrix.android.internal.crypto.model.rest.*
import im.vector.matrix.android.internal.crypto.tasks.SendToDeviceTask
import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith
@ -113,6 +110,24 @@ internal class SasTransportToDevice(
override fun createKey(tid: String, pubKey: String): VerificationInfoKey = KeyVerificationKey.create(tid, pubKey)
override fun createMac(tid: String, mac: Map<String, String>, keys: String) = KeyVerificationMac.create(tid, mac, keys)
override fun createStart(fromDevice: String,
method: String,
transactionID: String,
keyAgreementProtocols: List<String>,
hashes: List<String>,
messageAuthenticationCodes: List<String>,
shortAuthenticationStrings: List<String>): VerificationInfoStart {
return KeyVerificationStart().apply {
this.fromDevice = fromDevice
this.method = method
this.transactionID = transactionID
this.keyAgreementProtocols = keyAgreementProtocols
this.hashes = hashes
this.messageAuthenticationCodes = messageAuthenticationCodes
this.shortAuthenticationStrings = shortAuthenticationStrings
}
}
}
internal class SasTransportToDeviceFactory @Inject constructor(

View file

@ -46,6 +46,7 @@ object MoshiProvider {
.registerSubtype(MessageVideoContent::class.java, MessageType.MSGTYPE_VIDEO)
.registerSubtype(MessageLocationContent::class.java, MessageType.MSGTYPE_LOCATION)
.registerSubtype(MessageFileContent::class.java, MessageType.MSGTYPE_FILE)
.registerSubtype(MessageVerificationRequestContent::class.java, MessageType.MSGTYPE_VERIFICATION_REQUEST)
)
.add(SerializeNulls.JSON_ADAPTER_FACTORY)
.build()