crypto: Add the scaffolding to connect the SAS verification to the rust side

This commit is contained in:
Damir Jelić 2021-06-20 22:34:54 +02:00
parent 0cb9f6be10
commit d00b54929f
2 changed files with 109 additions and 20 deletions

View File

@ -29,9 +29,11 @@ import org.matrix.android.sdk.api.session.crypto.MXCryptoError
import org.matrix.android.sdk.api.session.crypto.verification.CancelCode import org.matrix.android.sdk.api.session.crypto.verification.CancelCode
import org.matrix.android.sdk.api.session.crypto.verification.EmojiRepresentation import org.matrix.android.sdk.api.session.crypto.verification.EmojiRepresentation
import org.matrix.android.sdk.api.session.crypto.verification.PendingVerificationRequest import org.matrix.android.sdk.api.session.crypto.verification.PendingVerificationRequest
import org.matrix.android.sdk.api.session.crypto.verification.SasVerificationTransaction
import org.matrix.android.sdk.api.session.crypto.verification.ValidVerificationInfoReady import org.matrix.android.sdk.api.session.crypto.verification.ValidVerificationInfoReady
import org.matrix.android.sdk.api.session.crypto.verification.ValidVerificationInfoRequest import org.matrix.android.sdk.api.session.crypto.verification.ValidVerificationInfoRequest
import org.matrix.android.sdk.api.session.crypto.verification.VerificationMethod import org.matrix.android.sdk.api.session.crypto.verification.VerificationMethod
import org.matrix.android.sdk.api.session.crypto.verification.VerificationTxState
import org.matrix.android.sdk.api.session.events.model.Content import org.matrix.android.sdk.api.session.events.model.Content
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.util.JsonDict import org.matrix.android.sdk.api.util.JsonDict
@ -63,6 +65,7 @@ import uniffi.olm.ProgressListener as RustProgressListener
import uniffi.olm.Request import uniffi.olm.Request
import uniffi.olm.RequestType import uniffi.olm.RequestType
import uniffi.olm.Sas as InnerSas import uniffi.olm.Sas as InnerSas
import uniffi.olm.StartSasResult
import uniffi.olm.VerificationRequest as InnerRequest import uniffi.olm.VerificationRequest as InnerRequest
import uniffi.olm.setLogger import uniffi.olm.setLogger
@ -148,13 +151,15 @@ internal class VerificationRequest(
fun accept_with_methods(methods: List<VerificationMethod>): OutgoingVerificationRequest? { fun accept_with_methods(methods: List<VerificationMethod>): OutgoingVerificationRequest? {
val stringMethods: MutableList<String> = val stringMethods: MutableList<String> =
methods.map { methods
.map {
when (it) { when (it) {
VerificationMethod.QR_CODE_SCAN -> VERIFICATION_METHOD_QR_CODE_SCAN VerificationMethod.QR_CODE_SCAN -> VERIFICATION_METHOD_QR_CODE_SCAN
VerificationMethod.QR_CODE_SHOW -> VERIFICATION_METHOD_QR_CODE_SHOW VerificationMethod.QR_CODE_SHOW -> VERIFICATION_METHOD_QR_CODE_SHOW
VerificationMethod.SAS -> VERIFICATION_METHOD_SAS VerificationMethod.SAS -> VERIFICATION_METHOD_SAS
} }
}.toMutableList() }
.toMutableList()
if (stringMethods.contains(VERIFICATION_METHOD_QR_CODE_SHOW) || if (stringMethods.contains(VERIFICATION_METHOD_QR_CODE_SHOW) ||
stringMethods.contains(VERIFICATION_METHOD_QR_CODE_SCAN)) { stringMethods.contains(VERIFICATION_METHOD_QR_CODE_SCAN)) {
@ -180,6 +185,14 @@ internal class VerificationRequest(
return this.inner.isReady return this.inner.isReady
} }
suspend fun startSasVerification(): StartSasResult? {
refreshData()
return withContext(Dispatchers.IO) {
machine.startSasVerification(inner.otherUserId, inner.flowId)
}
}
fun toPendingVerificationRequest(): PendingVerificationRequest { fun toPendingVerificationRequest(): PendingVerificationRequest {
refreshData() refreshData()
val code = this.inner.cancelCode val code = this.inner.cancelCode
@ -276,9 +289,12 @@ private fun toCancelCode(cancelCode: RustCancelCode): CancelCode {
} }
} }
internal class Sas(private val machine: InnerMachine, private var inner: InnerSas) { public class SasVerification(private val machine: InnerMachine, private var inner: InnerSas) :
SasVerificationTransaction {
private var stateField: VerificationTxState = VerificationTxState.OnStarted
private fun refreshData() { private fun refreshData() {
val sas = this.machine.getVerification(this.inner.flowId) val sas = this.machine.getVerification(this.inner.otherUserId, this.inner.flowId)
if (sas != null) { if (sas != null) {
this.inner = sas this.inner = sas
@ -287,6 +303,68 @@ internal class Sas(private val machine: InnerMachine, private var inner: InnerSa
return return
} }
override val isIncoming: Boolean
get() {
return false
}
override var otherDeviceId: String?
get() {
return this.inner.otherDeviceId
}
set(value) {
if (value != null) {
this.inner.otherDeviceId = value
}
}
override val otherUserId: String
get() {
return this.inner.otherUserId
}
override var state: VerificationTxState
get() {
TODO()
}
set(v) {
this.stateField = v
}
override val transactionId: String
get() {
return this.inner.flowId
}
override fun cancel() {
TODO()
}
override fun cancel(code: CancelCode) {
TODO()
}
override fun shortCodeDoesNotMatch() {
TODO()
}
override fun isToDeviceTransport(): Boolean {
return false
}
override fun supportsDecimal(): Boolean {
return false
}
override fun supportsEmoji(): Boolean {
return false
}
override fun userHasVerifiedShortCode() {
// This is confirm
TODO()
}
fun isCanceled(): Boolean { fun isCanceled(): Boolean {
refreshData() refreshData()
return this.inner.isCancelled return this.inner.isCancelled
@ -308,19 +386,21 @@ internal class Sas(private val machine: InnerMachine, private var inner: InnerSa
} }
fun accept(): OutgoingVerificationRequest? { fun accept(): OutgoingVerificationRequest? {
return this.machine.acceptVerification(inner.flowId) return this.machine.acceptSasVerification(this.inner.otherUserId, inner.flowId)
} }
@Throws(CryptoStoreErrorException::class) @Throws(CryptoStoreErrorException::class)
suspend fun confirm(): OutgoingVerificationRequest? = suspend fun confirm(): OutgoingVerificationRequest? =
withContext(Dispatchers.IO) { machine.confirmVerification(inner.flowId) } withContext(Dispatchers.IO) {
machine.confirmVerification(inner.otherUserId, inner.flowId)
fun cancel(): OutgoingVerificationRequest? {
return this.machine.cancelVerification(inner.flowId)
} }
fun emoji(): List<EmojiRepresentation> { fun cancelHelper(): OutgoingVerificationRequest? {
val emojiIndex = this.machine.getEmojiIndex(this.inner.flowId) return this.machine.cancelVerification(this.inner.otherUserId, inner.flowId)
}
override fun getEmojiCodeRepresentation(): List<EmojiRepresentation> {
val emojiIndex = this.machine.getEmojiIndex(this.inner.otherUserId, this.inner.flowId)
return if (emojiIndex != null) { return if (emojiIndex != null) {
emojiIndex.map { getEmojiForCode(it) } emojiIndex.map { getEmojiForCode(it) }
@ -329,8 +409,14 @@ internal class Sas(private val machine: InnerMachine, private var inner: InnerSa
} }
} }
fun decimals(): List<Int>? { override fun getDecimalCodeRepresentation(): String {
return this.machine.getDecimals(this.inner.flowId) val decimals = this.machine.getDecimals(this.inner.otherUserId, this.inner.flowId)
return if (decimals != null) {
decimals.joinToString(" ")
} else {
""
}
} }
} }
@ -753,13 +839,13 @@ internal class OlmMachine(
} }
/** Get an active verification */ /** Get an active verification */
fun getVerification(flowId: String): Sas? { fun getVerification(userId: String, flowId: String): SasVerification? {
val sas = this.inner.getVerification(flowId) val sas = this.inner.getVerification(userId, flowId)
return if (sas == null) { return if (sas == null) {
null null
} else { } else {
Sas(this.inner, sas) SasVerification(this.inner, sas)
} }
} }
} }

View File

@ -300,6 +300,9 @@ constructor(
} }
} }
// TODO create a class that handles this, the DefaultCryptoService has
// similar needs so we could share code there, beware that local echo seems
// to be handled here
suspend fun sendRequest(request: OutgoingVerificationRequest) { suspend fun sendRequest(request: OutgoingVerificationRequest) {
when (request) { when (request) {
is OutgoingVerificationRequest.ToDevice -> { is OutgoingVerificationRequest.ToDevice -> {