mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-03-17 19:58:57 +03:00
crypto: Connect the decryption logic to the rust olm machine
This commit is contained in:
parent
8b1b771ae6
commit
3b73adf3c5
6 changed files with 87 additions and 8 deletions
|
@ -717,7 +717,11 @@ internal class DefaultCryptoService @Inject constructor(
|
|||
*/
|
||||
@Throws(MXCryptoError::class)
|
||||
override fun decryptEvent(event: Event, timeline: String): MXEventDecryptionResult {
|
||||
return internalDecryptEvent(event, timeline)
|
||||
val decrypted = runBlocking {
|
||||
olmMachine!!.decryptRoomEvent(event)
|
||||
}
|
||||
|
||||
return decrypted
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -19,6 +19,9 @@ 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.events.model.Event
|
||||
import org.matrix.android.sdk.api.util.JsonDict
|
||||
import org.matrix.android.sdk.internal.crypto.MXEventDecryptionResult
|
||||
import org.matrix.android.sdk.internal.di.MoshiProvider
|
||||
import org.matrix.android.sdk.internal.session.sync.model.DeviceListResponse
|
||||
import org.matrix.android.sdk.internal.session.sync.model.DeviceOneTimeKeysCountSyncResponse
|
||||
|
@ -115,4 +118,21 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) {
|
|||
else -> Device(device, inner)
|
||||
}
|
||||
}
|
||||
|
||||
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!!)
|
||||
|
||||
val deserializationAdapter = MoshiProvider.providesMoshi().adapter<JsonDict>(Map::class.java)
|
||||
val clearEvent = deserializationAdapter.fromJson(decrypted.clearEvent)!!
|
||||
|
||||
MXEventDecryptionResult(
|
||||
clearEvent,
|
||||
decrypted.senderCurve25519Key,
|
||||
decrypted.claimedEd25519Key,
|
||||
decrypted.forwardingCurve25519Chain
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,8 +9,6 @@ crate-type = ["cdylib", "lib"]
|
|||
name = "matrix_crypto"
|
||||
|
||||
[dependencies]
|
||||
matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk/"}
|
||||
|
||||
serde_json = "1.0.62"
|
||||
http = "0.2.3"
|
||||
|
||||
|
@ -22,8 +20,13 @@ tracing-subscriber = "0.2.15"
|
|||
version = "0.2.0"
|
||||
features = ["lax_deserialize"]
|
||||
|
||||
[dependencies.matrix-sdk-common]
|
||||
git = "https://github.com/matrix-org/matrix-rust-sdk/"
|
||||
branch = "encryption-info"
|
||||
|
||||
[dependencies.matrix-sdk-crypto]
|
||||
git = "https://github.com/matrix-org/matrix-rust-sdk/"
|
||||
branch = "encryption-info"
|
||||
features = ["sled_cryptostore"]
|
||||
|
||||
[dependencies.tokio]
|
||||
|
@ -38,6 +41,3 @@ branch = "tagged-unions"
|
|||
|
||||
[build-dependencies]
|
||||
uniffi_build = "0.7.0"
|
||||
|
||||
[patch.crates-io]
|
||||
olm-sys = { git = "https://gitlab.gnome.org/poljar/olm-sys/", branch = "android-cross" }
|
||||
|
|
|
@ -4,6 +4,6 @@ mod machine;
|
|||
|
||||
pub use error::{CryptoStoreError, MachineCreationError};
|
||||
pub use logger::{set_logger, Logger};
|
||||
pub use machine::{Device, DeviceLists, OlmMachine, Request, RequestType, Sas};
|
||||
pub use machine::{DecryptedEvent, Device, DeviceLists, OlmMachine, Request, RequestType, Sas};
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/olm.uniffi.rs"));
|
||||
|
|
|
@ -16,7 +16,9 @@ use matrix_sdk_common::{
|
|||
sync::sync_events::{DeviceLists as RumaDeviceLists, ToDevice},
|
||||
},
|
||||
assign,
|
||||
identifiers::{DeviceKeyAlgorithm, UserId},
|
||||
deserialized_responses::events::{AlgorithmInfo, SyncMessageEvent},
|
||||
events::{room::encrypted::EncryptedEventContent, EventContent},
|
||||
identifiers::{DeviceKeyAlgorithm, RoomId, UserId},
|
||||
uuid::Uuid,
|
||||
UInt,
|
||||
};
|
||||
|
@ -32,6 +34,13 @@ pub struct OlmMachine {
|
|||
runtime: Runtime,
|
||||
}
|
||||
|
||||
pub struct DecryptedEvent {
|
||||
pub clear_event: String,
|
||||
pub sender_curve25519_key: String,
|
||||
pub claimed_ed25519_key: Option<String>,
|
||||
pub forwarding_curve25519_chain: Vec<String>,
|
||||
}
|
||||
|
||||
pub struct DeviceLists {
|
||||
pub changed: Vec<String>,
|
||||
pub left: Vec<String>,
|
||||
|
@ -324,4 +333,41 @@ 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();
|
||||
|
||||
let decrypted = self
|
||||
.runtime
|
||||
.block_on(self.inner.decrypt_room_event(&event, &room_id))
|
||||
.unwrap();
|
||||
|
||||
let encryption_info = decrypted
|
||||
.encryption_info()
|
||||
.expect("Decrypted event didn't contain any encryption info");
|
||||
|
||||
let content = decrypted.content();
|
||||
|
||||
let clear_event = json!({
|
||||
"type": content.event_type(),
|
||||
"content": content,
|
||||
});
|
||||
|
||||
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"),
|
||||
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(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,13 @@ dictionary DeviceLists {
|
|||
sequence<string> left;
|
||||
};
|
||||
|
||||
dictionary DecryptedEvent {
|
||||
string clear_event;
|
||||
string sender_curve25519_key;
|
||||
string? claimed_ed25519_key;
|
||||
sequence<string> forwarding_curve25519_chain;
|
||||
};
|
||||
|
||||
dictionary Device {
|
||||
string user_id;
|
||||
string device_id;
|
||||
|
@ -58,6 +65,8 @@ interface OlmMachine {
|
|||
DeviceLists device_changes,
|
||||
record<DOMString, i32> key_counts);
|
||||
|
||||
DecryptedEvent decrypt_room_event([ByRef] string event, [ByRef] string room_id);
|
||||
|
||||
record<DOMString, string> identity_keys();
|
||||
|
||||
string user_id();
|
||||
|
|
Loading…
Add table
Reference in a new issue