From f854e9cf1c77c3606c1702855e27f1f91ad09695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 25 Jun 2021 13:38:49 +0200 Subject: [PATCH] crypto: Remove the intermediately CancelCode and use strings to map over FFI --- .../android/sdk/internal/crypto/OlmMachine.kt | 19 ++--------- .../internal/crypto/RustSasVerification.kt | 13 +++----- rust-sdk/Cargo.toml | 4 +-- rust-sdk/src/lib.rs | 33 +------------------ rust-sdk/src/machine.rs | 21 ++++++++---- rust-sdk/src/olm.udl | 25 ++++++-------- 6 files changed, 33 insertions(+), 82 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt index db501a1f20..e05287052a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt @@ -31,6 +31,7 @@ import org.matrix.android.sdk.api.session.crypto.verification.PendingVerificatio 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.VerificationMethod +import org.matrix.android.sdk.api.session.crypto.verification.safeValueOf 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.util.JsonDict @@ -48,7 +49,6 @@ import org.matrix.android.sdk.internal.session.sync.model.DeviceListResponse import org.matrix.android.sdk.internal.session.sync.model.DeviceOneTimeKeysCountSyncResponse import org.matrix.android.sdk.internal.session.sync.model.ToDeviceSyncResponse import timber.log.Timber -import uniffi.olm.CancelCode as RustCancelCode import uniffi.olm.CryptoStoreErrorException import uniffi.olm.DecryptionErrorException import uniffi.olm.Device @@ -194,7 +194,7 @@ internal class VerificationRequest( val cancelCode = if (code != null) { - toCancelCode(code) + safeValueOf(code) } else { null } @@ -269,21 +269,6 @@ internal class VerificationRequest( } } -internal fun toCancelCode(cancelCode: RustCancelCode): CancelCode { - return when (cancelCode) { - RustCancelCode.USER -> CancelCode.User - RustCancelCode.TIMEOUT -> CancelCode.Timeout - RustCancelCode.UNKNOWN_TRANSACTION -> CancelCode.UnknownTransaction - RustCancelCode.UNKNOWN_METHOD -> CancelCode.UnknownMethod - RustCancelCode.UNEXPECTED_MESSAGE -> CancelCode.UnexpectedMessage - RustCancelCode.KEY_MISMATCH -> CancelCode.MismatchedKeys - RustCancelCode.USER_MISMATCH -> CancelCode.MismatchedKeys - RustCancelCode.INVALID_MESSAGE -> CancelCode.InvalidMessage - // TODO why don't the Ruma codes match what's in EA? - RustCancelCode.ACCEPTED -> CancelCode.User - } -} - internal class OlmMachine( user_id: String, device_id: String, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustSasVerification.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustSasVerification.kt index a4cb81150e..6a8ab11d82 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustSasVerification.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustSasVerification.kt @@ -26,6 +26,7 @@ import org.matrix.android.sdk.api.session.crypto.verification.EmojiRepresentatio import org.matrix.android.sdk.api.session.crypto.verification.SasVerificationTransaction import org.matrix.android.sdk.api.session.crypto.verification.VerificationService import org.matrix.android.sdk.api.session.crypto.verification.VerificationTxState +import org.matrix.android.sdk.api.session.crypto.verification.safeValueOf import org.matrix.android.sdk.internal.crypto.verification.getEmojiForCode import timber.log.Timber import uniffi.olm.CryptoStoreErrorException @@ -93,15 +94,9 @@ internal class SasVerification( this.inner.haveWeConfirmed -> VerificationTxState.ShortCodeAccepted this.inner.canBePresented -> VerificationTxState.ShortCodeReady this.inner.isCancelled -> { - val rustCancelCode = this.inner.cancelCode - val cancelCode = - if (rustCancelCode != null) { - toCancelCode(rustCancelCode) - } else { - CancelCode.UnexpectedMessage - } - // TODO get byMe from the rust side - VerificationTxState.Cancelled(cancelCode, false) + val cancelCode = safeValueOf(this.inner.cancelCode) + val byMe = this.inner.cancelledByUs ?: false + VerificationTxState.Cancelled(cancelCode, byMe) } else -> { VerificationTxState.Started diff --git a/rust-sdk/Cargo.toml b/rust-sdk/Cargo.toml index 86e3645dab..fdf306d537 100644 --- a/rust-sdk/Cargo.toml +++ b/rust-sdk/Cargo.toml @@ -24,11 +24,11 @@ features = ["lax_deserialize"] [dependencies.matrix-sdk-common] git = "https://github.com/matrix-org/matrix-rust-sdk/" -rev = "43583c292644e0e9bdaa6c83b2af35721416a263" +rev = "710b519c110278a0857f26a86a0f8329609c1a5f" [dependencies.matrix-sdk-crypto] git = "https://github.com/matrix-org/matrix-rust-sdk/" -rev = "43583c292644e0e9bdaa6c83b2af35721416a263" +rev = "710b519c110278a0857f26a86a0f8329609c1a5f" features = ["sled_cryptostore"] [dependencies.tokio] diff --git a/rust-sdk/src/lib.rs b/rust-sdk/src/lib.rs index fea61ce7b5..fe2d973033 100644 --- a/rust-sdk/src/lib.rs +++ b/rust-sdk/src/lib.rs @@ -7,7 +7,7 @@ mod responses; pub use device::Device; pub use error::{CryptoStoreError, DecryptionError, KeyImportError, MachineCreationError}; pub use logger::{set_logger, Logger}; -pub use machine::{KeyRequestPair, OlmMachine, Sas, VerificationRequest, StartSasResult}; +pub use machine::{KeyRequestPair, OlmMachine, Sas, StartSasResult, VerificationRequest}; pub use responses::{ DeviceLists, KeysImportResult, OutgoingVerificationRequest, Request, RequestType, }; @@ -30,35 +30,4 @@ pub struct DecryptedEvent { pub forwarding_curve25519_chain: Vec, } -pub enum CancelCode { - User, - Timeout, - UnknownTransaction, - UnknownMethod, - UnexpectedMessage, - KeyMismatch, - UserMismatch, - InvalidMessage, - Accepted, -} - -impl From for CancelCode { - fn from(c: ruma::events::key::verification::cancel::CancelCode) -> Self { - use ruma::events::key::verification::cancel::CancelCode as RumaCancelCode; - - match c { - RumaCancelCode::User => Self::User, - RumaCancelCode::Timeout => Self::Timeout, - RumaCancelCode::UnknownTransaction => Self::UnknownTransaction, - RumaCancelCode::UnknownMethod => Self::UnknownMethod, - RumaCancelCode::UnexpectedMessage => Self::UnexpectedMessage, - RumaCancelCode::KeyMismatch => Self::KeyMismatch, - RumaCancelCode::UserMismatch => Self::UserMismatch, - RumaCancelCode::InvalidMessage => Self::InvalidMessage, - RumaCancelCode::Accepted => Self::Accepted, - RumaCancelCode::_Custom(_) => Self::User, - } - } -} - include!(concat!(env!("OUT_DIR"), "/olm.uniffi.rs")); diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index a730884d28..8cdfd40e57 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -36,8 +36,8 @@ use matrix_sdk_crypto::{ use crate::{ error::{CryptoStoreError, DecryptionError, MachineCreationError}, responses::{response_from_string, OutgoingVerificationRequest, OwnedResponse}, - CancelCode, DecryptedEvent, Device, DeviceLists, KeyImportError, KeysImportResult, - ProgressListener, Request, RequestType, + DecryptedEvent, Device, DeviceLists, KeyImportError, KeysImportResult, ProgressListener, + Request, RequestType, }; /// A high level state machine that handles E2EE for Matrix. @@ -50,10 +50,13 @@ pub struct Sas { pub other_user_id: String, pub other_device_id: String, pub flow_id: String, + pub room_id: Option, pub have_we_confirmed: bool, pub is_cancelled: bool, pub is_done: bool, - pub cancel_code: Option, + pub cancel_code: Option, + pub cancelled_by_us: Option, + pub we_started: bool, pub can_be_presented: bool, pub supports_emoji: bool, pub timed_out: bool, @@ -76,7 +79,10 @@ impl From for Sas { timed_out: sas.timed_out(), supports_emoji: sas.supports_emoji(), have_we_confirmed: sas.have_we_confirmed(), - cancel_code: sas.cancel_code().map(|c| c.into()), + cancel_code: sas.cancel_code().map(|c| c.as_str().to_owned()), + we_started: sas.we_started(), + room_id: sas.room_id().map(|r| r.to_string()), + cancelled_by_us: sas.cancelled_by_us(), } } } @@ -89,7 +95,7 @@ pub struct VerificationRequest { pub is_done: bool, pub is_ready: bool, pub room_id: Option, - pub cancel_code: Option, + pub cancel_code: Option, pub we_started: bool, pub is_passive: bool, pub their_methods: Option>, @@ -106,7 +112,7 @@ impl From for VerificationRequest { is_done: v.is_done(), is_ready: v.is_ready(), room_id: v.room_id().map(|r| r.to_string()), - cancel_code: v.cancel_code().map(|c| c.into()), + cancel_code: v.cancel_code().map(|c| c.as_str().to_owned()), we_started: v.we_started(), is_passive: v.is_passive(), their_methods: v @@ -699,12 +705,13 @@ impl OlmMachine { &self, user_id: &str, flow_id: &str, + cancel_code: &str, ) -> Option { let user_id = UserId::try_from(user_id).ok()?; if let Some(verification) = self.inner.get_verification(&user_id, flow_id) { match verification { - Verification::SasV1(v) => v.cancel().map(|r| r.into()), + Verification::SasV1(v) => v.cancel_with_code(cancel_code.into()).map(|r| r.into()), Verification::QrV1(v) => v.cancel().map(|r| r.into()), } } else { diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index fb17755e8d..41baa128a3 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -74,7 +74,10 @@ dictionary Sas { string other_user_id; string other_device_id; string flow_id; - CancelCode? cancel_code; + string? cancel_code; + string? room_id; + boolean we_started; + boolean? cancelled_by_us; boolean have_we_confirmed; boolean is_done; boolean is_cancelled; @@ -93,24 +96,12 @@ dictionary VerificationRequest { boolean we_started; boolean is_passive; string? room_id; - CancelCode? cancel_code; + string? cancel_code; sequence? their_methods; sequence? our_methods; }; -enum CancelCode { - "User", - "Timeout", - "UnknownTransaction", - "UnknownMethod", - "UnexpectedMessage", - "KeyMismatch", - "UserMismatch", - "InvalidMessage", - "Accepted", -}; - dictionary KeyRequestPair { Request? cancellation; Request key_request; @@ -188,7 +179,11 @@ interface OlmMachine { StartSasResult? start_sas_verification([ByRef] string user_id, [ByRef] string flow_id); [Throws=CryptoStoreError] OutgoingVerificationRequest? confirm_verification([ByRef] string user_id, [ByRef] string flow_id); - OutgoingVerificationRequest? cancel_verification([ByRef] string user_id, [ByRef] string flow_id); + OutgoingVerificationRequest? cancel_verification( + [ByRef] string user_id, + [ByRef] string flow_id, + [ByRef] string cancel_code + ); OutgoingVerificationRequest? accept_sas_verification([ByRef] string user_id, [ByRef] string flow_id); sequence? get_emoji_index([ByRef] string user_id, [ByRef] string flow_id);