Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src-tauri/src/matrix_crypto/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ async fn handle(machine: &OlmMachine, method: &str, args: &Value) -> Result<Opti
Value::Null
}
"isBackupEnabled" => Value::Bool(machine.backup_machine().enabled().await),
"backupVersion" => match machine.backup_machine().backup_version().await {
Some(version) => Value::String(version),
None => Value::Null,
},
"verifyBackup" => {
let info: RoomKeyBackupInfo =
serde_json::from_str(&str_arg(args, method, "backupInfo")?)
Expand Down
21 changes: 13 additions & 8 deletions src-tauri/src/matrix_crypto/cross_signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,21 @@ async fn bootstrap(machine: &OlmMachine, args: &Value) -> Result<Value, String>

let upload_keys_request = match requests.upload_keys_req.as_ref() {
Some(request) => match request.request() {
AnyOutgoingRequest::KeysUpload(req) => json!({
"id": request.request_id().to_string(),
"type": KEYS_UPLOAD_REQUEST_TYPE,
"className": "KeysUploadRequest",
"body": json!({
"device_keys": req.device_keys,
AnyOutgoingRequest::KeysUpload(req) => {
let mut body = json!({
"one_time_keys": req.one_time_keys,
"fallback_keys": req.fallback_keys,
}).to_string(),
}),
});
if let Some(device_keys) = &req.device_keys {
body["device_keys"] = json!(device_keys);
}
json!({
"id": request.request_id().to_string(),
"type": KEYS_UPLOAD_REQUEST_TYPE,
"className": "KeysUploadRequest",
"body": body.to_string(),
})
}
_ => {
return Err(
"bootstrapCrossSigning: upload_keys_req was not a /keys/upload request"
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/matrix_crypto/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ async fn encrypt_to_device_event(
.get("content")
.ok_or_else(|| format!("{method}: missing argument `content`"))?;
let Some(device) = device_for(machine, args, method).await? else {
return Err(format!("{method}: unknown device"));
return Ok(Value::Null);
};

let encrypted = device
Expand Down
69 changes: 65 additions & 4 deletions src-tauri/src/matrix_crypto/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use matrix_sdk_crypto::{EncryptionSyncChanges, OlmMachine};
use serde::Serialize;
use serde_json::{json, Value};

use matrix_sdk::deserialized_responses::{DeviceLinkProblem, VerificationLevel};
use matrix_sdk_crypto::MegolmError;

use super::args::{caller_decryption_settings, decryption_settings, room_id, str_arg};
use super::requests::{mark_request_sent, outgoing_requests};
use super::wasm_enums::processed_to_device_event_type;
Expand Down Expand Up @@ -137,6 +140,55 @@ fn processed_to_device_event_json(
Ok(value)
}

mod decryption_error_code {
pub const MISSING_ROOM_KEY: u8 = 0;
pub const UNKNOWN_MESSAGE_INDEX: u8 = 1;
pub const MISMATCHED_IDENTITY_KEYS: u8 = 2;
pub const UNKNOWN_SENDER_DEVICE: u8 = 3;
pub const UNSIGNED_SENDER_DEVICE: u8 = 4;
pub const SENDER_IDENTITY_VERIFICATION_VIOLATION: u8 = 5;
pub const UNABLE_TO_DECRYPT: u8 = 6;
pub const MISMATCHED_SENDER: u8 = 7;
}

fn decryption_error_json(error: &MegolmError) -> Value {
use decryption_error_code as code;

let (error_code, withheld) = match error {
MegolmError::MissingRoomKey(withheld) => (
code::MISSING_ROOM_KEY,
withheld.as_ref().map(|code| code.to_string()),
),
MegolmError::Decryption(
matrix_sdk_crypto::vodozemac::megolm::DecryptionError::UnknownMessageIndex(_, _),
) => (code::UNKNOWN_MESSAGE_INDEX, None),
MegolmError::MismatchedIdentityKeys(_) => (code::MISMATCHED_IDENTITY_KEYS, None),
MegolmError::SenderIdentityNotTrusted(level) => (
match level {
VerificationLevel::VerificationViolation => {
code::SENDER_IDENTITY_VERIFICATION_VIOLATION
}
VerificationLevel::UnsignedDevice => code::UNSIGNED_SENDER_DEVICE,
VerificationLevel::MismatchedSender => code::MISMATCHED_SENDER,
VerificationLevel::None(DeviceLinkProblem::MissingDevice)
| VerificationLevel::None(DeviceLinkProblem::InsecureSource) => {
code::UNKNOWN_SENDER_DEVICE
}
_ => code::UNABLE_TO_DECRYPT,
},
None,
),
_ => (code::UNABLE_TO_DECRYPT, None),
};

json!({
"className": "DecryptionError",
"code": error_code,
"description": error.to_string(),
"maybeWithheld": withheld,
})
}

fn verification_request_snapshot(
machine: &OlmMachine,
event: &ProcessedToDeviceEvent,
Expand Down Expand Up @@ -280,10 +332,13 @@ pub async fn invoke(machine: &OlmMachine, method: &str, args: Value) -> Result<V
let event: Raw<EncryptedEvent> = serde_json::from_str(&event_json)
.map_err(|e| format!("decryptRoomEvent: bad event json: {e}"))?;

let decrypted = machine
let decrypted = match machine
.decrypt_room_event(&event, &room, &caller_decryption_settings(&args))
.await
.map_err(|e| format!("decryptRoomEvent failed: {e:?}"))?;
{
Ok(decrypted) => decrypted,
Err(error) => return Ok(decryption_error_json(&error)),
};

let info = decrypted.encryption_info;
let (sender_curve25519_key, claimed_ed25519_key) = match &info.algorithm_info {
Expand All @@ -308,8 +363,14 @@ pub async fn invoke(machine: &OlmMachine, method: &str, args: Value) -> Result<V
"senderDevice": info.sender_device.as_ref().map(ToString::to_string),
"senderCurve25519Key": sender_curve25519_key,
"senderClaimedEd25519Key": claimed_ed25519_key,
"forwarder": Value::Null,
"forwarderDevice": Value::Null,
"forwarder": info
.forwarder
.as_ref()
.map(|forwarder| forwarder.user_id.to_string()),
"forwarderDevice": info
.forwarder
.as_ref()
.map(|forwarder| forwarder.device_id.to_string()),
"forwardingCurve25519KeyChain": Vec::<String>::new(),
}))
}
Expand Down
Loading
Loading