From 7f89e3303739546e441d04ceb2921b7b0df57b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 29 Mar 2021 16:36:40 +0200 Subject: [PATCH] crypto: Connect the key exporting to the rust-sdk export method --- .../sdk/internal/crypto/DefaultCryptoService.kt | 12 +++--------- .../org/matrix/android/sdk/internal/newCrypto.kt | 6 ++++++ rust-sdk/src/error.rs | 2 ++ rust-sdk/src/machine.rs | 16 ++++++++++++---- rust-sdk/src/olm.udl | 4 ++++ 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt index 4b6d0fde63..c5df78b3cc 100755 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt @@ -835,18 +835,12 @@ internal class DefaultCryptoService @Inject constructor( * Export the crypto keys * * @param password the password - * @param anIterationCount the encryption iteration count (0 means no encryption) + * @param anIterationCount the encryption iteration count */ private suspend fun exportRoomKeys(password: String, anIterationCount: Int): ByteArray { return withContext(coroutineDispatchers.crypto) { - val iterationCount = max(0, anIterationCount) - - val exportedSessions = cryptoStore.getInboundGroupSessions().mapNotNull { it.exportKeys() } - - val adapter = MoshiProvider.providesMoshi() - .adapter(List::class.java) - - MXMegolmExportEncryption.encryptMegolmKeyFile(adapter.toJson(exportedSessions), password, iterationCount) + val iterationCount = max(10000, anIterationCount) + olmMachine!!.exportKeys(password, iterationCount) } } 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 ff651d0de0..a8e536bf76 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 @@ -38,6 +38,7 @@ import uniffi.olm.Logger import uniffi.olm.OlmMachine as InnerMachine import uniffi.olm.Request import uniffi.olm.RequestType +import uniffi.olm.CryptoStoreErrorException import uniffi.olm.Sas as InnerSas import uniffi.olm.setLogger @@ -179,6 +180,11 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) { } } + @Throws(CryptoStoreErrorException::class) + suspend fun exportKeys(passphrase: String, rounds: Int): ByteArray = withContext(Dispatchers.IO) { + inner.exportKeys(passphrase, rounds).toByteArray() + } + @Throws(MXCryptoError::class) suspend fun decryptRoomEvent(event: Event): MXEventDecryptionResult = withContext(Dispatchers.IO) { val adapter = MoshiProvider.providesMoshi().adapter(Event::class.java) diff --git a/rust-sdk/src/error.rs b/rust-sdk/src/error.rs index d2e4afb5f1..ec2e2c07ac 100644 --- a/rust-sdk/src/error.rs +++ b/rust-sdk/src/error.rs @@ -15,6 +15,8 @@ pub enum CryptoStoreError { CryptoStore(#[from] InnerStoreError), #[error(transparent)] OlmError(#[from] OlmError), + #[error(transparent)] + Serialization(#[from] serde_json::Error), } #[derive(Debug, thiserror::Error)] diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index 3c8a041111..8baebb3db3 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -10,8 +10,7 @@ use tokio::runtime::Runtime; use matrix_sdk_common::{ api::r0::{ keys::{ - claim_keys::{Request as KeysClaimRequest, Response as KeysClaimResponse}, - get_keys::Response as KeysQueryResponse, + claim_keys::Response as KeysClaimResponse, get_keys::Response as KeysQueryResponse, upload_keys::Response as KeysUploadResponse, }, sync::sync_events::{DeviceLists as RumaDeviceLists, ToDevice}, @@ -26,8 +25,8 @@ use matrix_sdk_common::{ }; use matrix_sdk_crypto::{ - EncryptionSettings, IncomingResponse, OlmMachine as InnerMachine, OutgoingRequest, - ToDeviceRequest, + encrypt_key_export, EncryptionSettings, IncomingResponse, OlmMachine as InnerMachine, + OutgoingRequest, ToDeviceRequest, }; use crate::error::{CryptoStoreError, DecryptionError, MachineCreationError}; @@ -420,6 +419,15 @@ impl OlmMachine { serde_json::to_string(&encrypted_content).unwrap() } + pub fn export_keys(&self, passphrase: &str, rounds: i32) -> Result { + let keys = self.runtime.block_on(self.inner.export_keys(|_| true))?; + + let encrypted = encrypt_key_export(&keys, passphrase, rounds as u32) + .map_err(CryptoStoreError::Serialization)?; + + Ok(encrypted) + } + pub fn decrypt_room_event( &self, event: &str, diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index 36bda6c96f..367de7cec3 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -16,6 +16,7 @@ enum MachineCreationError { enum CryptoStoreError { "CryptoStore", "OlmError", + "Serialization", }; [Error] @@ -102,4 +103,7 @@ interface OlmMachine { [Throws=CryptoStoreError] Sas start_verification([ByRef] Device device); + + [Throws=CryptoStoreError] + string export_keys([ByRef] string passphrase, i32 rounds); };