Skip to content
Open
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
43 changes: 31 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ qrcode = ["matrix-sdk-crypto/qrcode"]
tracing = ["dep:tracing-subscriber"]

[dependencies]
matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "931c5649420adb071caf1abafc7964758487e472", features = ["js"] }
matrix-sdk-sqlite = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "931c5649420adb071caf1abafc7964758487e472", features = ["crypto-store"] }
matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "48220571630521fbc58529df36a0f2d4772ef5e1", features = ["js"] }
matrix-sdk-sqlite = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "48220571630521fbc58529df36a0f2d4772ef5e1", features = ["crypto-store"] }
napi = { version = "2.9.1", default-features = false, features = ["napi6", "tokio_rt"] }
napi-derive = "2.9.1"
# Fix error[E0635]: unknown feature `stdsimd` caused by ahash < 0.8.7
Expand All @@ -37,7 +37,7 @@ zeroize = "1.3.0"

[dependencies.matrix-sdk-crypto]
git = "https://github.com/matrix-org/matrix-rust-sdk"
rev = "931c5649420adb071caf1abafc7964758487e472"
rev = "48220571630521fbc58529df36a0f2d4772ef5e1"
default-features = false
features = ["js", "automatic-room-key-forwarding"]

Expand Down
56 changes: 55 additions & 1 deletion src/backup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Megolm backup types

use matrix_sdk_crypto::{backups::MegolmV1BackupKey as InnerMegolmV1BackupKey, store};
use matrix_sdk_crypto::{
backups::MegolmV1BackupKey as InnerMegolmV1BackupKey, olm::InboundGroupSession, store,
};
use napi_derive::*;

use crate::into_err;
Expand All @@ -19,6 +21,29 @@ pub struct MegolmV1BackupKey {
inner: InnerMegolmV1BackupKey,
}

#[napi(object)]
#[derive(Debug, Clone)]
pub struct SessionData {
/// Unpadded base64-encoded public half of the ephemeral key.
pub ephemeral: String,

/// Ciphertext, encrypted using AES-CBC-256 with PKCS#7 padding, encoded in
/// base64.
pub ciphertext: String,

/// First 8 bytes of MAC key, encoded in base64.
pub mac: String,
}

#[napi]
#[derive(Debug, Clone)]
pub struct KeyBackupData {
pub first_message_index: i64,
pub forwarded_count: i64,
pub is_verified: bool,
pub session_data: SessionData,
}

#[napi]
impl MegolmV1BackupKey {
/// The actual base64 encoded public key.
Expand All @@ -32,6 +57,35 @@ impl MegolmV1BackupKey {
pub fn backup_algorithm(&self) -> String {
self.inner.backup_algorithm().into()
}

/// Try to create a [`MegolmV1BackupKey`] from a base 64 encoded string.
#[napi(strict)]
pub fn from_base64(key: String) -> napi::Result<MegolmV1BackupKey> {
Ok(Self { inner: InnerMegolmV1BackupKey::from_base64(&key).map_err(into_err)? })
}

/// Encrypt an exported room session which can then be uploaded to the
/// homeserver's key backup. Consumes a single session exported from
/// `OlmMachine.export_room_keys_for_session`
#[napi(strict)]
pub async fn encrypt_exported_session(
&self,
exported_session_json: String,
) -> napi::Result<KeyBackupData> {
let exported_session = serde_json::from_str(&exported_session_json).map_err(into_err)?;
let session = InboundGroupSession::from_export(&exported_session).map_err(into_err)?;
let res = self.inner.encrypt(session).await;
Ok(KeyBackupData {
first_message_index: res.first_message_index.into(),
forwarded_count: res.forwarded_count.into(),
is_verified: res.is_verified,
session_data: SessionData {
ephemeral: res.session_data.ephemeral.to_string(),
ciphertext: res.session_data.ciphertext.to_string(),
mac: res.session_data.mac.to_string(),
},
})
}
}

#[napi]
Expand Down