crypto: Remove the intermediately CancelCode and use strings to map over FFI

This commit is contained in:
Damir Jelić 2021-06-25 13:38:49 +02:00
parent b53b0a0093
commit f854e9cf1c
6 changed files with 33 additions and 82 deletions

View File

@ -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.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.safeValueOf
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
@ -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.DeviceOneTimeKeysCountSyncResponse
import org.matrix.android.sdk.internal.session.sync.model.ToDeviceSyncResponse import org.matrix.android.sdk.internal.session.sync.model.ToDeviceSyncResponse
import timber.log.Timber import timber.log.Timber
import uniffi.olm.CancelCode as RustCancelCode
import uniffi.olm.CryptoStoreErrorException import uniffi.olm.CryptoStoreErrorException
import uniffi.olm.DecryptionErrorException import uniffi.olm.DecryptionErrorException
import uniffi.olm.Device import uniffi.olm.Device
@ -194,7 +194,7 @@ internal class VerificationRequest(
val cancelCode = val cancelCode =
if (code != null) { if (code != null) {
toCancelCode(code) safeValueOf(code)
} else { } else {
null 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( internal class OlmMachine(
user_id: String, user_id: String,
device_id: String, device_id: String,

View File

@ -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.SasVerificationTransaction
import org.matrix.android.sdk.api.session.crypto.verification.VerificationService 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.VerificationTxState
import org.matrix.android.sdk.api.session.crypto.verification.safeValueOf
import org.matrix.android.sdk.internal.crypto.verification.getEmojiForCode import org.matrix.android.sdk.internal.crypto.verification.getEmojiForCode
import timber.log.Timber import timber.log.Timber
import uniffi.olm.CryptoStoreErrorException import uniffi.olm.CryptoStoreErrorException
@ -93,15 +94,9 @@ internal class SasVerification(
this.inner.haveWeConfirmed -> VerificationTxState.ShortCodeAccepted this.inner.haveWeConfirmed -> VerificationTxState.ShortCodeAccepted
this.inner.canBePresented -> VerificationTxState.ShortCodeReady this.inner.canBePresented -> VerificationTxState.ShortCodeReady
this.inner.isCancelled -> { this.inner.isCancelled -> {
val rustCancelCode = this.inner.cancelCode val cancelCode = safeValueOf(this.inner.cancelCode)
val cancelCode = val byMe = this.inner.cancelledByUs ?: false
if (rustCancelCode != null) { VerificationTxState.Cancelled(cancelCode, byMe)
toCancelCode(rustCancelCode)
} else {
CancelCode.UnexpectedMessage
}
// TODO get byMe from the rust side
VerificationTxState.Cancelled(cancelCode, false)
} }
else -> { else -> {
VerificationTxState.Started VerificationTxState.Started

View File

@ -24,11 +24,11 @@ features = ["lax_deserialize"]
[dependencies.matrix-sdk-common] [dependencies.matrix-sdk-common]
git = "https://github.com/matrix-org/matrix-rust-sdk/" git = "https://github.com/matrix-org/matrix-rust-sdk/"
rev = "43583c292644e0e9bdaa6c83b2af35721416a263" rev = "710b519c110278a0857f26a86a0f8329609c1a5f"
[dependencies.matrix-sdk-crypto] [dependencies.matrix-sdk-crypto]
git = "https://github.com/matrix-org/matrix-rust-sdk/" git = "https://github.com/matrix-org/matrix-rust-sdk/"
rev = "43583c292644e0e9bdaa6c83b2af35721416a263" rev = "710b519c110278a0857f26a86a0f8329609c1a5f"
features = ["sled_cryptostore"] features = ["sled_cryptostore"]
[dependencies.tokio] [dependencies.tokio]

View File

@ -7,7 +7,7 @@ mod responses;
pub use device::Device; pub use device::Device;
pub use error::{CryptoStoreError, DecryptionError, KeyImportError, MachineCreationError}; pub use error::{CryptoStoreError, DecryptionError, KeyImportError, MachineCreationError};
pub use logger::{set_logger, Logger}; 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::{ pub use responses::{
DeviceLists, KeysImportResult, OutgoingVerificationRequest, Request, RequestType, DeviceLists, KeysImportResult, OutgoingVerificationRequest, Request, RequestType,
}; };
@ -30,35 +30,4 @@ pub struct DecryptedEvent {
pub forwarding_curve25519_chain: Vec<String>, pub forwarding_curve25519_chain: Vec<String>,
} }
pub enum CancelCode {
User,
Timeout,
UnknownTransaction,
UnknownMethod,
UnexpectedMessage,
KeyMismatch,
UserMismatch,
InvalidMessage,
Accepted,
}
impl From<ruma::events::key::verification::cancel::CancelCode> 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")); include!(concat!(env!("OUT_DIR"), "/olm.uniffi.rs"));

View File

@ -36,8 +36,8 @@ use matrix_sdk_crypto::{
use crate::{ use crate::{
error::{CryptoStoreError, DecryptionError, MachineCreationError}, error::{CryptoStoreError, DecryptionError, MachineCreationError},
responses::{response_from_string, OutgoingVerificationRequest, OwnedResponse}, responses::{response_from_string, OutgoingVerificationRequest, OwnedResponse},
CancelCode, DecryptedEvent, Device, DeviceLists, KeyImportError, KeysImportResult, DecryptedEvent, Device, DeviceLists, KeyImportError, KeysImportResult, ProgressListener,
ProgressListener, Request, RequestType, Request, RequestType,
}; };
/// A high level state machine that handles E2EE for Matrix. /// A high level state machine that handles E2EE for Matrix.
@ -50,10 +50,13 @@ pub struct Sas {
pub other_user_id: String, pub other_user_id: String,
pub other_device_id: String, pub other_device_id: String,
pub flow_id: String, pub flow_id: String,
pub room_id: Option<String>,
pub have_we_confirmed: bool, pub have_we_confirmed: bool,
pub is_cancelled: bool, pub is_cancelled: bool,
pub is_done: bool, pub is_done: bool,
pub cancel_code: Option<CancelCode>, pub cancel_code: Option<String>,
pub cancelled_by_us: Option<bool>,
pub we_started: bool,
pub can_be_presented: bool, pub can_be_presented: bool,
pub supports_emoji: bool, pub supports_emoji: bool,
pub timed_out: bool, pub timed_out: bool,
@ -76,7 +79,10 @@ impl From<InnerSas> for Sas {
timed_out: sas.timed_out(), timed_out: sas.timed_out(),
supports_emoji: sas.supports_emoji(), supports_emoji: sas.supports_emoji(),
have_we_confirmed: sas.have_we_confirmed(), 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_done: bool,
pub is_ready: bool, pub is_ready: bool,
pub room_id: Option<String>, pub room_id: Option<String>,
pub cancel_code: Option<CancelCode>, pub cancel_code: Option<String>,
pub we_started: bool, pub we_started: bool,
pub is_passive: bool, pub is_passive: bool,
pub their_methods: Option<Vec<String>>, pub their_methods: Option<Vec<String>>,
@ -106,7 +112,7 @@ impl From<InnerVerificationRequest> for VerificationRequest {
is_done: v.is_done(), is_done: v.is_done(),
is_ready: v.is_ready(), is_ready: v.is_ready(),
room_id: v.room_id().map(|r| r.to_string()), 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(), we_started: v.we_started(),
is_passive: v.is_passive(), is_passive: v.is_passive(),
their_methods: v their_methods: v
@ -699,12 +705,13 @@ impl OlmMachine {
&self, &self,
user_id: &str, user_id: &str,
flow_id: &str, flow_id: &str,
cancel_code: &str,
) -> Option<OutgoingVerificationRequest> { ) -> Option<OutgoingVerificationRequest> {
let user_id = UserId::try_from(user_id).ok()?; let user_id = UserId::try_from(user_id).ok()?;
if let Some(verification) = self.inner.get_verification(&user_id, flow_id) { if let Some(verification) = self.inner.get_verification(&user_id, flow_id) {
match verification { 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()), Verification::QrV1(v) => v.cancel().map(|r| r.into()),
} }
} else { } else {

View File

@ -74,7 +74,10 @@ dictionary Sas {
string other_user_id; string other_user_id;
string other_device_id; string other_device_id;
string flow_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 have_we_confirmed;
boolean is_done; boolean is_done;
boolean is_cancelled; boolean is_cancelled;
@ -93,24 +96,12 @@ dictionary VerificationRequest {
boolean we_started; boolean we_started;
boolean is_passive; boolean is_passive;
string? room_id; string? room_id;
CancelCode? cancel_code; string? cancel_code;
sequence<string>? their_methods; sequence<string>? their_methods;
sequence<string>? our_methods; sequence<string>? our_methods;
}; };
enum CancelCode {
"User",
"Timeout",
"UnknownTransaction",
"UnknownMethod",
"UnexpectedMessage",
"KeyMismatch",
"UserMismatch",
"InvalidMessage",
"Accepted",
};
dictionary KeyRequestPair { dictionary KeyRequestPair {
Request? cancellation; Request? cancellation;
Request key_request; Request key_request;
@ -188,7 +179,11 @@ interface OlmMachine {
StartSasResult? start_sas_verification([ByRef] string user_id, [ByRef] string flow_id); StartSasResult? start_sas_verification([ByRef] string user_id, [ByRef] string flow_id);
[Throws=CryptoStoreError] [Throws=CryptoStoreError]
OutgoingVerificationRequest? confirm_verification([ByRef] string user_id, [ByRef] string flow_id); 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); OutgoingVerificationRequest? accept_sas_verification([ByRef] string user_id, [ByRef] string flow_id);
sequence<i32>? get_emoji_index([ByRef] string user_id, [ByRef] string flow_id); sequence<i32>? get_emoji_index([ByRef] string user_id, [ByRef] string flow_id);