crypto: Propagate decryption errors to the kotlin side

This commit is contained in:
Damir Jelić 2021-02-19 16:33:30 +01:00
parent 3b73adf3c5
commit 891622d64b
5 changed files with 48 additions and 22 deletions

View File

@ -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>(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<JsonDict>(Map::class.java)
val clearEvent = deserializationAdapter.fromJson(decrypted.clearEvent)!!
val deserializationAdapter = MoshiProvider.providesMoshi().adapter<JsonDict>(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)
}
}
}

View File

@ -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),
}

View File

@ -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};

View File

@ -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<EncryptedEventContent> = 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<DecryptedEvent, DecryptionError> {
let event: SyncMessageEvent<EncryptedEventContent> = 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(),
},
}
})
}
}

View File

@ -18,6 +18,13 @@ enum CryptoStoreError {
"OlmError",
};
[Error]
enum DecryptionError {
"Identifier",
"Serialization",
"Megolm",
};
dictionary DeviceLists {
sequence<string> changed;
sequence<string> left;
@ -65,6 +72,7 @@ interface OlmMachine {
DeviceLists device_changes,
record<DOMString, i32> key_counts);
[Throws=DecryptionError]
DecryptedEvent decrypt_room_event([ByRef] string event, [ByRef] string room_id);
record<DOMString, string> identity_keys();