diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationAcceptContent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationAcceptContent.kt index d4fb3a44fd1..454110ed198 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationAcceptContent.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationAcceptContent.kt @@ -37,19 +37,6 @@ internal data class MessageVerificationAcceptContent( override val transactionID: String? get() = relatesTo?.eventId - override fun isValid(): Boolean { - if (transactionID.isNullOrBlank() - || keyAgreementProtocol.isNullOrBlank() - || hash.isNullOrBlank() - || commitment.isNullOrBlank() - || messageAuthenticationCode.isNullOrBlank() - || shortAuthenticationStrings.isNullOrEmpty()) { - Timber.e("## received invalid verification request") - return false - } - return true - } - override fun toEventContent() = toContent() companion object : VerificationInfoAcceptFactory { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationCancelContent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationCancelContent.kt index 9b7ca13df80..5cfef097c15 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationCancelContent.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationCancelContent.kt @@ -28,7 +28,6 @@ data class MessageVerificationCancelContent( @Json(name = "code") override val code: String? = null, @Json(name = "reason") override val reason: String? = null, @Json(name = "m.relates_to") val relatesTo: RelationDefaultContent? - ) : VerificationInfoCancel { override val transactionID: String? @@ -36,13 +35,6 @@ data class MessageVerificationCancelContent( override fun toEventContent() = toContent() - override fun isValid(): Boolean { - if (transactionID.isNullOrBlank() || code.isNullOrBlank()) { - return false - } - return true - } - companion object { fun create(transactionId: String, reason: CancelCode): MessageVerificationCancelContent { return MessageVerificationCancelContent( diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationDoneContent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationDoneContent.kt index 224c5c52d30..d884bc16201 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationDoneContent.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationDoneContent.kt @@ -25,12 +25,20 @@ import im.vector.matrix.android.internal.crypto.verification.VerificationInfo @JsonClass(generateAdapter = true) internal data class MessageVerificationDoneContent( @Json(name = "m.relates_to") val relatesTo: RelationDefaultContent? -) : VerificationInfo { +) : VerificationInfo { override val transactionID: String? get() = relatesTo?.eventId - override fun isValid() = transactionID?.isNotEmpty() == true - override fun toEventContent(): Content? = toContent() + + override fun asValidObject(): ValidVerificationDone? { + if (transactionID.isNullOrEmpty()) { + return null + } + + return ValidVerificationDone + } } + +internal object ValidVerificationDone diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationKeyContent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationKeyContent.kt index 777e0e73188..7a4b95f3390 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationKeyContent.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationKeyContent.kt @@ -22,7 +22,6 @@ import im.vector.matrix.android.api.session.events.model.toContent import im.vector.matrix.android.api.session.room.model.relation.RelationDefaultContent import im.vector.matrix.android.internal.crypto.verification.VerificationInfoKey import im.vector.matrix.android.internal.crypto.verification.VerificationInfoKeyFactory -import timber.log.Timber @JsonClass(generateAdapter = true) internal data class MessageVerificationKeyContent( @@ -36,14 +35,6 @@ internal data class MessageVerificationKeyContent( override val transactionID: String? get() = relatesTo?.eventId - override fun isValid(): Boolean { - if (transactionID.isNullOrBlank() || key.isNullOrBlank()) { - Timber.e("## received invalid verification request") - return false - } - return true - } - override fun toEventContent() = toContent() companion object : VerificationInfoKeyFactory { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationMacContent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationMacContent.kt index 05e614dee97..1d732bf605a 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationMacContent.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationMacContent.kt @@ -35,13 +35,6 @@ internal data class MessageVerificationMacContent( override fun toEventContent() = toContent() - override fun isValid(): Boolean { - if (transactionID.isNullOrBlank() || keys.isNullOrBlank() || mac.isNullOrEmpty()) { - return false - } - return true - } - companion object : VerificationInfoMacFactory { override fun create(tid: String, mac: Map, keys: String): VerificationInfoMac { return MessageVerificationMacContent( diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationReadyContent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationReadyContent.kt index 4e237ba1c9a..d4b7e728ab1 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationReadyContent.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationReadyContent.kt @@ -35,13 +35,6 @@ internal data class MessageVerificationReadyContent( override fun toEventContent() = toContent() - override fun isValid(): Boolean { - if (transactionID.isNullOrBlank() || methods.isNullOrEmpty() || fromDevice.isNullOrEmpty()) { - return false - } - return true - } - companion object : MessageVerificationReadyFactory { override fun create(tid: String, methods: List, fromDevice: String): VerificationInfoReady { return MessageVerificationReadyContent( diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationRequestContent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationRequestContent.kt index 2ef4979f9d5..d19f938f45e 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationRequestContent.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/MessageVerificationRequestContent.kt @@ -36,13 +36,6 @@ data class MessageVerificationRequestContent( @Json(name = "m.new_content") override val newContent: Content? = null ) : MessageContent, VerificationInfoRequest { - override fun isValid(): Boolean { - if (transactionID.isNullOrBlank() || methods.isNullOrEmpty() || fromDevice.isNullOrEmpty()) { - return false - } - return true - } - override val transactionID: String? get() = relatesTo?.eventId diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationAccept.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationAccept.kt index dc6075ca439..757b504db21 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationAccept.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationAccept.kt @@ -67,19 +67,6 @@ internal data class KeyVerificationAccept( override var commitment: String? = null ) : SendToDeviceObject, VerificationInfoAccept { - override fun isValid(): Boolean { - if (transactionID.isNullOrBlank() - || keyAgreementProtocol.isNullOrBlank() - || hash.isNullOrBlank() - || commitment.isNullOrBlank() - || messageAuthenticationCode.isNullOrBlank() - || shortAuthenticationStrings.isNullOrEmpty()) { - Timber.e("## received invalid verification request") - return false - } - return true - } - override fun toSendToDeviceObject() = this companion object : VerificationInfoAcceptFactory { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationCancel.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationCancel.kt index dcf08531c80..6f940fd3285 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationCancel.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationCancel.kt @@ -53,11 +53,4 @@ internal data class KeyVerificationCancel( } override fun toSendToDeviceObject() = this - - override fun isValid(): Boolean { - if (transactionID.isNullOrBlank() || code.isNullOrBlank()) { - return false - } - return true - } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationDone.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationDone.kt index bdce77b31d5..eaaf8b82813 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationDone.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationDone.kt @@ -28,11 +28,4 @@ internal data class KeyVerificationDone( ) : SendToDeviceObject, VerificationInfoDone { override fun toSendToDeviceObject() = this - - override fun isValid(): Boolean { - if (transactionID.isNullOrBlank()) { - return false - } - return true - } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationKey.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationKey.kt index 4f90f8db8c9..a3baed85588 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationKey.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationKey.kt @@ -44,11 +44,4 @@ internal data class KeyVerificationKey( } override fun toSendToDeviceObject() = this - - override fun isValid(): Boolean { - if (transactionID.isNullOrBlank() || key.isNullOrBlank()) { - return false - } - return true - } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationMac.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationMac.kt index d6e712e0565..a72a7cbc64a 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationMac.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationMac.kt @@ -31,13 +31,6 @@ internal data class KeyVerificationMac( ) : SendToDeviceObject, VerificationInfoMac { - override fun isValid(): Boolean { - if (transactionID.isNullOrBlank() || keys.isNullOrBlank() || mac.isNullOrEmpty()) { - return false - } - return true - } - override fun toSendToDeviceObject(): SendToDeviceObject? = this companion object : VerificationInfoMacFactory { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationReady.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationReady.kt index 004477e90b1..9050cbd9086 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationReady.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationReady.kt @@ -30,8 +30,4 @@ internal data class KeyVerificationReady( ) : SendToDeviceObject, VerificationInfoReady { override fun toSendToDeviceObject() = this - - override fun isValid(): Boolean { - return !transactionID.isNullOrBlank() && !fromDevice.isNullOrBlank() && !methods.isNullOrEmpty() - } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationRequest.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationRequest.kt index fcddb5c3d48..5aeeb8e85d4 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationRequest.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/model/rest/KeyVerificationRequest.kt @@ -32,11 +32,4 @@ internal data class KeyVerificationRequest( ) : SendToDeviceObject, VerificationInfoRequest { override fun toSendToDeviceObject() = this - - override fun isValid(): Boolean { - if (transactionID.isNullOrBlank() || methods.isNullOrEmpty() || fromDevice.isNullOrEmpty()) { - return false - } - return true - } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfo.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfo.kt index ed2e5dfbe0e..8f01bdd14af 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfo.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfo.kt @@ -18,10 +18,11 @@ package im.vector.matrix.android.internal.crypto.verification import im.vector.matrix.android.api.session.events.model.Content import im.vector.matrix.android.internal.crypto.model.rest.SendToDeviceObject -interface VerificationInfo { +interface VerificationInfo { fun toEventContent(): Content? = null fun toSendToDeviceObject(): SendToDeviceObject? = null - fun isValid(): Boolean + + fun asValidObject(): ValidObject? /** * String to identify the transaction. diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoAccept.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoAccept.kt index cc6c8c8530c..7adb94374a8 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoAccept.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoAccept.kt @@ -15,7 +15,9 @@ */ package im.vector.matrix.android.internal.crypto.verification -internal interface VerificationInfoAccept : VerificationInfo { +import timber.log.Timber + +internal interface VerificationInfoAccept : VerificationInfo { /** * The key agreement protocol that Bob’s device has selected to use, out of the list proposed by Alice’s device */ @@ -41,6 +43,27 @@ internal interface VerificationInfoAccept : VerificationInfo { * and the canonical JSON representation of the m.key.verification.start message. */ var commitment: String? + + override fun asValidObject(): ValidVerificationInfoAccept? { + if (transactionID.isNullOrBlank() + || keyAgreementProtocol.isNullOrBlank() + || hash.isNullOrBlank() + || commitment.isNullOrBlank() + || messageAuthenticationCode.isNullOrBlank() + || shortAuthenticationStrings.isNullOrEmpty()) { + Timber.e("## received invalid verification request") + return null + } + + return ValidVerificationInfoAccept( + transactionID!!, + keyAgreementProtocol!!, + hash!!, + messageAuthenticationCode!!, + shortAuthenticationStrings!!, + commitment + ) + } } internal interface VerificationInfoAcceptFactory { @@ -52,3 +75,12 @@ internal interface VerificationInfoAcceptFactory { messageAuthenticationCode: String, shortAuthenticationStrings: List): VerificationInfoAccept } + +internal data class ValidVerificationInfoAccept( + val transactionID: String, + val keyAgreementProtocol: String, + val hash: String, + val messageAuthenticationCode: String, + val shortAuthenticationStrings: List, + var commitment: String? +) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoCancel.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoCancel.kt index 713453e93e8..4de77993e03 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoCancel.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoCancel.kt @@ -15,7 +15,7 @@ */ package im.vector.matrix.android.internal.crypto.verification -internal interface VerificationInfoCancel : VerificationInfo { +internal interface VerificationInfoCancel : VerificationInfo { /** * machine-readable reason for cancelling, see [CancelCode] */ @@ -25,4 +25,19 @@ internal interface VerificationInfoCancel : VerificationInfo { * human-readable reason for cancelling. This should only be used if the receiving client does not understand the code given. */ val reason: String? + + override fun asValidObject(): ValidVerificationInfoCancel? { + if (transactionID.isNullOrBlank() || code.isNullOrBlank()) { + return null + } + return ValidVerificationInfoCancel( + code!!, + reason + ) + } } + +internal data class ValidVerificationInfoCancel( + val code: String, + val reason: String? +) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoDone.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoDone.kt index 9c7c00e84b6..e0af4318a1a 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoDone.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoDone.kt @@ -15,4 +15,14 @@ */ package im.vector.matrix.android.internal.crypto.verification -interface VerificationInfoDone : VerificationInfo +internal interface VerificationInfoDone : VerificationInfo { + + override fun asValidObject(): ValidVerificationInfoDone? { + if (transactionID.isNullOrBlank()) { + return null + } + return ValidVerificationInfoDone + } +} + +internal object ValidVerificationInfoDone diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoKey.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoKey.kt index 2466660ca4b..15b3703c592 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoKey.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoKey.kt @@ -18,13 +18,26 @@ package im.vector.matrix.android.internal.crypto.verification /** * Sent by both devices to send their ephemeral Curve25519 public key to the other device. */ -internal interface VerificationInfoKey : VerificationInfo { +internal interface VerificationInfoKey : VerificationInfo { /** * The device’s ephemeral public key, as an unpadded base64 string */ val key: String? + + override fun asValidObject(): ValidVerificationInfoKey? { + if (transactionID.isNullOrBlank() || key.isNullOrBlank()) { + return null + } + return ValidVerificationInfoKey( + key!! + ) + } } internal interface VerificationInfoKeyFactory { fun create(tid: String, pubKey: String): VerificationInfoKey } + +internal data class ValidVerificationInfoKey( + val key: String +) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoMac.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoMac.kt index e895973ae86..68c86249d48 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoMac.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoMac.kt @@ -15,7 +15,7 @@ */ package im.vector.matrix.android.internal.crypto.verification -internal interface VerificationInfoMac : VerificationInfo { +internal interface VerificationInfoMac : VerificationInfo { /** * A map of key ID to the MAC of the key, as an unpadded base64 string, calculated using the MAC key */ @@ -28,8 +28,23 @@ internal interface VerificationInfoMac : VerificationInfo { * give the MAC of the string “ed25519:ABCDEFG,ed25519:HIJKLMN”. */ val keys: String? + + override fun asValidObject(): ValidVerificationInfoMac? { + if (transactionID.isNullOrBlank() || keys.isNullOrBlank() || mac.isNullOrEmpty()) { + return null + } + return ValidVerificationInfoMac( + mac!!, + keys!! + ) + } } internal interface VerificationInfoMacFactory { - fun create(tid: String, mac: Map, keys: String) : VerificationInfoMac + fun create(tid: String, mac: Map, keys: String): VerificationInfoMac } + +internal data class ValidVerificationInfoMac( + val mac: Map, + val keys: String +) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoReady.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoReady.kt index 4c269513159..aa998b2258e 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoReady.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoReady.kt @@ -23,7 +23,7 @@ package im.vector.matrix.android.internal.crypto.verification * with a m.key.verification.start event instead. */ -interface VerificationInfoReady : VerificationInfo { +internal interface VerificationInfoReady : VerificationInfo { /** * The ID of the device that sent the m.key.verification.ready message */ @@ -33,8 +33,26 @@ interface VerificationInfoReady : VerificationInfo { * An array of verification methods that the device supports */ val methods: List? + + override fun asValidObject(): ValidVerificationInfoReady? { + if (transactionID.isNullOrBlank() + || fromDevice.isNullOrBlank() + || methods.isNullOrEmpty()) { + return null + } + + return ValidVerificationInfoReady( + fromDevice!!, + methods!! + ) + } } internal interface MessageVerificationReadyFactory { fun create(tid: String, methods: List, fromDevice: String): VerificationInfoReady } + +internal data class ValidVerificationInfoReady( + val fromDevice: String, + val methods: List +) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoRequest.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoRequest.kt index 529334c3ea5..a4a12336699 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoRequest.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoRequest.kt @@ -15,7 +15,7 @@ */ package im.vector.matrix.android.internal.crypto.verification -interface VerificationInfoRequest : VerificationInfo { +internal interface VerificationInfoRequest : VerificationInfo { /** * Required. The device ID which is initiating the request. @@ -33,4 +33,22 @@ interface VerificationInfoRequest : VerificationInfo { * the message should be ignored by the receiver. */ val timestamp: Long? + + override fun asValidObject(): ValidVerificationInfoRequest? { + // FIXME No check on Timestamp? + if (transactionID.isNullOrBlank() || methods.isNullOrEmpty() || fromDevice.isNullOrEmpty()) { + return null + } + return ValidVerificationInfoRequest( + fromDevice!!, + methods!!, + timestamp + ) + } } + +internal data class ValidVerificationInfoRequest( + val fromDevice: String, + val methods: List, + val timestamp: Long? +) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoStart.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoStart.kt index bd4fd529803..985e7c05f6d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoStart.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/VerificationInfoStart.kt @@ -18,9 +18,8 @@ package im.vector.matrix.android.internal.crypto.verification import im.vector.matrix.android.api.session.crypto.verification.SasMode import im.vector.matrix.android.internal.crypto.model.rest.VERIFICATION_METHOD_RECIPROCATE import im.vector.matrix.android.internal.crypto.model.rest.VERIFICATION_METHOD_SAS -import timber.log.Timber -internal interface VerificationInfoStart : VerificationInfo { +internal interface VerificationInfoStart : VerificationInfo { val method: String? @@ -64,40 +63,54 @@ internal interface VerificationInfoStart : VerificationInfo { fun toCanonicalJson(): String? - override fun isValid(): Boolean { + override fun asValidObject(): ValidVerificationInfoStart? { if (transactionID.isNullOrBlank() - || fromDevice.isNullOrBlank() - || (method == VERIFICATION_METHOD_SAS && !isValidSas()) - || (method == VERIFICATION_METHOD_RECIPROCATE && !isValidReciprocate())) { - Timber.e("## received invalid verification request") - return false + || fromDevice.isNullOrBlank()) { + return null } - return true - } - - private fun isValidSas(): Boolean { - val myHashes = hashes - val myMessageAuthenticationCodes = messageAuthenticationCodes - val myShortAuthenticationStrings = shortAuthenticationStrings - if (keyAgreementProtocols.isNullOrEmpty() - || myHashes.isNullOrEmpty() - || !myHashes.contains("sha256") || myMessageAuthenticationCodes.isNullOrEmpty() - || (!myMessageAuthenticationCodes.contains(SASDefaultVerificationTransaction.SAS_MAC_SHA256) - && !myMessageAuthenticationCodes.contains(SASDefaultVerificationTransaction.SAS_MAC_SHA256_LONGKDF)) - || myShortAuthenticationStrings.isNullOrEmpty() - || !myShortAuthenticationStrings.contains(SasMode.DECIMAL)) { - return false + return when (method) { + VERIFICATION_METHOD_SAS -> { + if (keyAgreementProtocols.isNullOrEmpty() + || hashes.isNullOrEmpty() + || !hashes!!.contains("sha256") || messageAuthenticationCodes.isNullOrEmpty() + || (!messageAuthenticationCodes!!.contains(SASDefaultVerificationTransaction.SAS_MAC_SHA256) + && !messageAuthenticationCodes!!.contains(SASDefaultVerificationTransaction.SAS_MAC_SHA256_LONGKDF)) + || shortAuthenticationStrings.isNullOrEmpty() + || !shortAuthenticationStrings!!.contains(SasMode.DECIMAL)) { + null + } else { + ValidVerificationInfoStart.SasVerificationInfoStart( + keyAgreementProtocols!!, + hashes!!, + messageAuthenticationCodes!!, + shortAuthenticationStrings!! + ) + } + } + VERIFICATION_METHOD_RECIPROCATE -> { + if (sharedSecret.isNullOrBlank()) { + null + } else { + ValidVerificationInfoStart.ReciprocateVerificationInfoStart( + sharedSecret!! + ) + } + } + else -> null } - - return true } +} - private fun isValidReciprocate(): Boolean { - if (sharedSecret.isNullOrBlank()) { - return false - } - - return true - } +sealed class ValidVerificationInfoStart { + data class SasVerificationInfoStart( + val keyAgreementProtocols: List, + val hashes: List, + val messageAuthenticationCodes: List, + val shortAuthenticationStrings: List + ) : ValidVerificationInfoStart() + + data class ReciprocateVerificationInfoStart( + val sharedSecret: String + ) : ValidVerificationInfoStart() }