From 891622d64b8129aaf2fdf57a201fc481efe6bc58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 19 Feb 2021 16:33:30 +0100 Subject: [PATCH] crypto: Propagate decryption errors to the kotlin side --- .../matrix/android/sdk/internal/newCrypto.kt | 25 ++++++++++++------- rust-sdk/src/error.rs | 13 ++++++++-- rust-sdk/src/lib.rs | 2 +- rust-sdk/src/machine.rs | 22 ++++++++-------- rust-sdk/src/olm.udl | 8 ++++++ 5 files changed, 48 insertions(+), 22 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/newCrypto.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/newCrypto.kt index 261d469854..7712da129b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/newCrypto.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/newCrypto.kt @@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal import java.io.File import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import org.matrix.android.sdk.api.session.crypto.MXCryptoError import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.util.JsonDict import org.matrix.android.sdk.internal.crypto.MXEventDecryptionResult @@ -119,20 +120,26 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) { } } + @Throws(MXCryptoError::class) suspend fun decryptRoomEvent(event: Event): MXEventDecryptionResult = withContext(Dispatchers.IO) { val adapter = MoshiProvider.providesMoshi().adapter(Event::class.java) val serializedEvent = adapter.toJson(event) - val decrypted = inner.decryptRoomEvent(serializedEvent, event.roomId!!) + try { + val decrypted = inner.decryptRoomEvent(serializedEvent, event.roomId!!) - val deserializationAdapter = MoshiProvider.providesMoshi().adapter(Map::class.java) - val clearEvent = deserializationAdapter.fromJson(decrypted.clearEvent)!! + val deserializationAdapter = MoshiProvider.providesMoshi().adapter(Map::class.java) + val clearEvent = deserializationAdapter.fromJson(decrypted.clearEvent)!! - MXEventDecryptionResult( - clearEvent, - decrypted.senderCurve25519Key, - decrypted.claimedEd25519Key, - decrypted.forwardingCurve25519Chain - ) + MXEventDecryptionResult( + clearEvent, + decrypted.senderCurve25519Key, + decrypted.claimedEd25519Key, + decrypted.forwardingCurve25519Chain + ) + } catch (throwable: Throwable) { + val reason = String.format(MXCryptoError.UNABLE_TO_DECRYPT_REASON, throwable.message, "m.megolm.v1.aes-sha2") + throw MXCryptoError.Base(MXCryptoError.ErrorType.UNABLE_TO_DECRYPT, reason) + } } } diff --git a/rust-sdk/src/error.rs b/rust-sdk/src/error.rs index a2f3c57684..d2e4afb5f1 100644 --- a/rust-sdk/src/error.rs +++ b/rust-sdk/src/error.rs @@ -1,6 +1,5 @@ use matrix_sdk_common::identifiers::Error as RumaIdentifierError; - -use matrix_sdk_crypto::{store::CryptoStoreError as InnerStoreError, OlmError}; +use matrix_sdk_crypto::{store::CryptoStoreError as InnerStoreError, MegolmError, OlmError}; #[derive(Debug, thiserror::Error)] pub enum MachineCreationError { @@ -17,3 +16,13 @@ pub enum CryptoStoreError { #[error(transparent)] OlmError(#[from] OlmError), } + +#[derive(Debug, thiserror::Error)] +pub enum DecryptionError { + #[error(transparent)] + Serialization(#[from] serde_json::Error), + #[error(transparent)] + Identifier(#[from] RumaIdentifierError), + #[error(transparent)] + Megolm(#[from] MegolmError), +} diff --git a/rust-sdk/src/lib.rs b/rust-sdk/src/lib.rs index 799f045a3c..9ca691778e 100644 --- a/rust-sdk/src/lib.rs +++ b/rust-sdk/src/lib.rs @@ -2,7 +2,7 @@ mod error; mod logger; mod machine; -pub use error::{CryptoStoreError, MachineCreationError}; +pub use error::{CryptoStoreError, DecryptionError, MachineCreationError}; pub use logger::{set_logger, Logger}; pub use machine::{DecryptedEvent, Device, DeviceLists, OlmMachine, Request, RequestType, Sas}; diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index 6ac6db6d76..12072f4e73 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -27,7 +27,7 @@ use matrix_sdk_crypto::{ IncomingResponse, OlmMachine as InnerMachine, OutgoingRequest, ToDeviceRequest, }; -use crate::error::{CryptoStoreError, MachineCreationError}; +use crate::error::{CryptoStoreError, DecryptionError, MachineCreationError}; pub struct OlmMachine { inner: InnerMachine, @@ -334,14 +334,17 @@ impl OlmMachine { .unwrap(); } - pub fn decrypt_room_event(&self, event: &str, room_id: &str) -> DecryptedEvent { - let event: SyncMessageEvent = serde_json::from_str(event).unwrap(); - let room_id = RoomId::try_from(room_id).unwrap(); + pub fn decrypt_room_event( + &self, + event: &str, + room_id: &str, + ) -> Result { + let event: SyncMessageEvent = serde_json::from_str(event)?; + let room_id = RoomId::try_from(room_id)?; let decrypted = self .runtime - .block_on(self.inner.decrypt_room_event(&event, &room_id)) - .unwrap(); + .block_on(self.inner.decrypt_room_event(&event, &room_id))?; let encryption_info = decrypted .encryption_info() @@ -354,20 +357,19 @@ impl OlmMachine { "content": content, }); - match &encryption_info.algorithm_info { + Ok(match &encryption_info.algorithm_info { AlgorithmInfo::MegolmV1AesSha2 { curve25519_key, sender_claimed_keys, forwarding_curve25519_key_chain, } => DecryptedEvent { - clear_event: serde_json::to_string(&clear_event) - .expect("Can't serialize the decrypted json object"), + clear_event: serde_json::to_string(&clear_event)?, sender_curve25519_key: curve25519_key.to_owned(), claimed_ed25519_key: sender_claimed_keys .get(&DeviceKeyAlgorithm::Ed25519) .cloned(), forwarding_curve25519_chain: forwarding_curve25519_key_chain.to_owned(), }, - } + }) } } diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index 6e0c9b5a50..7107a8b40e 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -18,6 +18,13 @@ enum CryptoStoreError { "OlmError", }; +[Error] +enum DecryptionError { + "Identifier", + "Serialization", + "Megolm", +}; + dictionary DeviceLists { sequence changed; sequence left; @@ -65,6 +72,7 @@ interface OlmMachine { DeviceLists device_changes, record key_counts); + [Throws=DecryptionError] DecryptedEvent decrypt_room_event([ByRef] string event, [ByRef] string room_id); record identity_keys();