Skip to content

Commit

Permalink
Create asValidObject method - make it compiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarty committed Mar 5, 2020
1 parent 7b5a50e commit a3f8a53
Show file tree
Hide file tree
Showing 19 changed files with 204 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ internal data class MessageVerificationDoneContent(
return null
}

return ValidVerificationDone
return ValidVerificationDone(
transactionID!!
)
}
}

internal object ValidVerificationDone
internal data class ValidVerificationDone(
val transactionID: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal data class MessageVerificationStartContent(
@Json(name = "secret") override val sharedSecret: String?
) : VerificationInfoStart {

override fun toCanonicalJson(): String? {
override fun toCanonicalJson(): String {
return JsonCanonicalizer.getCanonicalJson(MessageVerificationStartContent::class.java, this)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal data class KeyVerificationStart(
@Json(name = "secret") override val sharedSecret: String? = null
) : SendToDeviceObject, VerificationInfoStart {

override fun toCanonicalJson(): String? {
override fun toCanonicalJson(): String {
return JsonCanonicalizer.getCanonicalJson(KeyVerificationStart::class.java, this)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal class DefaultIncomingSASDefaultVerificationTransaction(
}
}

override fun onVerificationStart(startReq: VerificationInfoStart) {
override fun onVerificationStart(startReq: ValidVerificationInfoStart.SasVerificationInfoStart) {
Timber.v("## SAS I: received verification request from state $state")
if (state != VerificationTxState.None) {
Timber.e("## SAS I: received verification request from invalid state")
Expand All @@ -100,10 +100,10 @@ internal class DefaultIncomingSASDefaultVerificationTransaction(

// Select a key agreement protocol, a hash algorithm, a message authentication code,
// and short authentication string methods out of the lists given in requester's message.
val agreedProtocol = startReq!!.keyAgreementProtocols?.firstOrNull { KNOWN_AGREEMENT_PROTOCOLS.contains(it) }
val agreedHash = startReq!!.hashes?.firstOrNull { KNOWN_HASHES.contains(it) }
val agreedMac = startReq!!.messageAuthenticationCodes?.firstOrNull { KNOWN_MACS.contains(it) }
val agreedShortCode = startReq!!.shortAuthenticationStrings?.filter { KNOWN_SHORT_CODES.contains(it) }
val agreedProtocol = startReq!!.keyAgreementProtocols.firstOrNull { KNOWN_AGREEMENT_PROTOCOLS.contains(it) }
val agreedHash = startReq!!.hashes.firstOrNull { KNOWN_HASHES.contains(it) }
val agreedMac = startReq!!.messageAuthenticationCodes.firstOrNull { KNOWN_MACS.contains(it) }
val agreedShortCode = startReq!!.shortAuthenticationStrings.filter { KNOWN_SHORT_CODES.contains(it) }

// No common key sharing/hashing/hmac/SAS methods.
// If a device is unable to complete the verification because the devices are unable to find a common key sharing,
Expand Down Expand Up @@ -141,12 +141,12 @@ internal class DefaultIncomingSASDefaultVerificationTransaction(
}

private fun doAccept(accept: VerificationInfoAccept) {
this.accepted = accept
this.accepted = accept.asValidObject()
Timber.v("## SAS incoming accept request id:$transactionId")

// The hash commitment is the hash (using the selected hash algorithm) of the unpadded base64 representation of QB,
// concatenated with the canonical JSON representation of the content of the m.key.verification.start message
val concat = getSAS().publicKey + startReq!!.toCanonicalJson()
val concat = getSAS().publicKey + startReq!!.canonicalJson
accept.commitment = hashUsingAgreedHashMethod(concat) ?: ""
// we need to send this to other device now
state = VerificationTxState.SendingAccept
Expand All @@ -158,12 +158,12 @@ internal class DefaultIncomingSASDefaultVerificationTransaction(
}
}

override fun onVerificationAccept(accept: VerificationInfoAccept) {
override fun onVerificationAccept(accept: ValidVerificationInfoAccept) {
Timber.v("## SAS invalid message for incoming request id:$transactionId")
cancel(CancelCode.UnexpectedMessage)
}

override fun onKeyVerificationKey(vKey: VerificationInfoKey) {
override fun onKeyVerificationKey(vKey: ValidVerificationInfoKey) {
Timber.v("## SAS received key for request id:$transactionId")
if (state != VerificationTxState.SendingAccept && state != VerificationTxState.Accepted) {
Timber.e("## SAS received key from invalid state $state")
Expand Down Expand Up @@ -213,7 +213,7 @@ internal class DefaultIncomingSASDefaultVerificationTransaction(
state = VerificationTxState.ShortCodeReady
}

override fun onKeyVerificationMac(vKey: VerificationInfoMac) {
override fun onKeyVerificationMac(vKey: ValidVerificationInfoMac) {
Timber.v("## SAS I: received mac for request id:$transactionId")
// Check for state?
if (state != VerificationTxState.SendingKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal class DefaultOutgoingSASDefaultVerificationTransaction(
}
}

override fun onVerificationStart(startReq: VerificationInfoStart) {
override fun onVerificationStart(startReq: ValidVerificationInfoStart.SasVerificationInfoStart) {
Timber.e("## SAS O: onVerificationStart - unexpected id:$transactionId")
cancel(CancelCode.UnexpectedMessage)
}
Expand All @@ -94,8 +94,8 @@ internal class DefaultOutgoingSASDefaultVerificationTransaction(
KNOWN_MACS,
KNOWN_SHORT_CODES
)

startReq = startMessage
kk
startReq = startMessage.asValidObject() as? ValidVerificationInfoStart.SasVerificationInfoStart
state = VerificationTxState.SendingStart

sendToOther(
Expand Down Expand Up @@ -130,7 +130,7 @@ internal class DefaultOutgoingSASDefaultVerificationTransaction(
// )
// }

override fun onVerificationAccept(accept: VerificationInfoAccept) {
override fun onVerificationAccept(accept: ValidVerificationInfoAccept) {
Timber.v("## SAS O: onVerificationAccept id:$transactionId")
if (state != VerificationTxState.Started) {
Timber.e("## SAS O: received accept request from invalid state $state")
Expand All @@ -141,7 +141,7 @@ internal class DefaultOutgoingSASDefaultVerificationTransaction(
if (!KNOWN_AGREEMENT_PROTOCOLS.contains(accept.keyAgreementProtocol)
|| !KNOWN_HASHES.contains(accept.hash)
|| !KNOWN_MACS.contains(accept.messageAuthenticationCode)
|| accept.shortAuthenticationStrings!!.intersect(KNOWN_SHORT_CODES).isEmpty()) {
|| accept.shortAuthenticationStrings.intersect(KNOWN_SHORT_CODES).isEmpty()) {
Timber.e("## SAS O: received accept request from invalid state")
cancel(CancelCode.UnknownMethod)
return
Expand All @@ -167,7 +167,7 @@ internal class DefaultOutgoingSASDefaultVerificationTransaction(
}
}

override fun onKeyVerificationKey(vKey: VerificationInfoKey) {
override fun onKeyVerificationKey(vKey: ValidVerificationInfoKey) {
Timber.v("## SAS O: onKeyVerificationKey id:$transactionId")
if (state != VerificationTxState.SendingKey && state != VerificationTxState.KeySent) {
Timber.e("## received key from invalid state $state")
Expand All @@ -182,7 +182,7 @@ internal class DefaultOutgoingSASDefaultVerificationTransaction(
// in Bob’s m.key.verification.key and the content of Alice’s m.key.verification.start message.

// check commitment
val concat = vKey.key + startReq!!.toCanonicalJson()
val concat = vKey.key + startReq!!.canonicalJson
val otherCommitment = hashUsingAgreedHashMethod(concat) ?: ""

if (accepted!!.commitment.equals(otherCommitment)) {
Expand All @@ -206,7 +206,7 @@ internal class DefaultOutgoingSASDefaultVerificationTransaction(
}
}

override fun onKeyVerificationMac(vKey: VerificationInfoMac) {
override fun onKeyVerificationMac(vKey: ValidVerificationInfoMac) {
Timber.v("## SAS O: onKeyVerificationMac id:$transactionId")
if (state != VerificationTxState.OnKeyReceived
&& state != VerificationTxState.ShortCodeReady
Expand Down
Loading

0 comments on commit a3f8a53

Please sign in to comment.