Convert KeyVerificationStart to data class

This commit is contained in:
Valere 2019-12-05 10:08:27 +01:00
parent e14602d1dc
commit 3cdd373368
4 changed files with 40 additions and 73 deletions

View file

@ -27,75 +27,36 @@ import timber.log.Timber
* Sent by Alice to initiate an interactive key verification.
*/
@JsonClass(generateAdapter = true)
class KeyVerificationStart : SendToDeviceObject, VerificationInfoStart {
data class KeyVerificationStart(
@Json(name = "from_device") override val fromDevice: String? = null,
override val method: String? = null,
@Json(name = "transaction_id") override val transactionID: String? = null,
@Json(name = "key_agreement_protocols") override val keyAgreementProtocols: List<String>? = null,
@Json(name = "hashes") override val hashes: List<String>? = null,
@Json(name = "message_authentication_codes") override val messageAuthenticationCodes: List<String>? = null,
@Json(name = "short_authentication_string") override val shortAuthenticationStrings: List<String>? = null
) : SendToDeviceObject, VerificationInfoStart {
override fun toCanonicalJson(): String? {
return JsonCanonicalizer.getCanonicalJson(KeyVerificationStart::class.java, this)
}
/**
* Alices device ID
*/
@Json(name = "from_device")
override var fromDevice: String? = null
override var method: String? = null
/**
* String to identify the transaction.
* This string must be unique for the pair of users performing verification for the duration that the transaction is valid.
* Alices device should record this ID and use it in future messages in this transaction.
*/
@Json(name = "transaction_id")
override var transactionID: String? = null
/**
* An array of key agreement protocols that Alices client understands.
* Must include curve25519.
* Other methods may be defined in the future
*/
@Json(name = "key_agreement_protocols")
override var keyAgreementProtocols: List<String>? = null
/**
* An array of hashes that Alices client understands.
* Must include sha256. Other methods may be defined in the future.
*/
override var hashes: List<String>? = null
/**
* An array of message authentication codes that Alices client understands.
* Must include hkdf-hmac-sha256.
* Other methods may be defined in the future.
*/
@Json(name = "message_authentication_codes")
override var messageAuthenticationCodes: List<String>? = null
/**
* An array of short authentication string methods that Alices client (and Alice) understands.
* Must include decimal.
* This document also describes the emoji method.
* Other methods may be defined in the future
*/
@Json(name = "short_authentication_string")
override var shortAuthenticationStrings: List<String>? = null
companion object {
const val VERIF_METHOD_SAS = "m.sas.v1"
}
override fun isValid(): Boolean {
if (transactionID.isNullOrBlank()
|| fromDevice.isNullOrBlank()
|| method != VERIF_METHOD_SAS
|| keyAgreementProtocols.isNullOrEmpty()
|| hashes.isNullOrEmpty()
|| hashes?.contains("sha256") == false
if ((transactionID.isNullOrBlank()
|| fromDevice.isNullOrBlank()
|| method != VERIF_METHOD_SAS
|| keyAgreementProtocols.isNullOrEmpty()
|| hashes.isNullOrEmpty())
|| !hashes.contains("sha256")
|| messageAuthenticationCodes.isNullOrEmpty()
|| (messageAuthenticationCodes?.contains(SASVerificationTransaction.SAS_MAC_SHA256) == false
&& messageAuthenticationCodes?.contains(SASVerificationTransaction.SAS_MAC_SHA256_LONGKDF) == false)
|| shortAuthenticationStrings.isNullOrEmpty()
|| shortAuthenticationStrings?.contains(SasMode.DECIMAL) == false) {
|| (!messageAuthenticationCodes.contains(SASVerificationTransaction.SAS_MAC_SHA256)
&& !messageAuthenticationCodes.contains(SASVerificationTransaction.SAS_MAC_SHA256_LONGKDF))
|| shortAuthenticationStrings.isNullOrEmpty() || !shortAuthenticationStrings.contains(SasMode.DECIMAL)) {
Timber.e("## received invalid verification request")
return false
}

View file

@ -239,14 +239,8 @@ internal class DefaultSasVerificationService @Inject constructor(
if (!startReq.isValid()) {
Timber.e("## SAS received invalid verification request")
if (startReq.transactionID != null) {
// cancelTransaction(
// startReq.transactionID!!,
// otherUserId!!,
// startReq.fromDevice ?: event.getSenderKey()!!,
// CancelCode.UnknownMethod
// )
sasTransportToDeviceFactory.createTransport(null).cancelTransaction(
startReq.transactionID ?: "",
startReq.transactionID,
otherUserId!!,
startReq.fromDevice ?: event.getSenderKey()!!,
CancelCode.UnknownMethod

View file

@ -118,15 +118,14 @@ internal class SasTransportToDevice(
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
}
return KeyVerificationStart(
fromDevice,
method,
transactionID,
keyAgreementProtocols,
hashes,
messageAuthenticationCodes,
shortAuthenticationStrings)
}
}

View file

@ -18,10 +18,23 @@ package im.vector.matrix.android.internal.crypto.verification
internal interface VerificationInfoStart : VerificationInfo {
val method: String?
/**
* Alices device ID
*/
val fromDevice: String?
/**
* String to identify the transaction.
* This string must be unique for the pair of users performing verification for the duration that the transaction is valid.
* Alices device should record this ID and use it in future messages in this transaction.
*/
val transactionID: String?
/**
* An array of key agreement protocols that Alices client understands.
* Must include curve25519.
* Other methods may be defined in the future
*/
val keyAgreementProtocols: List<String>?
/**