crypto: Connect the key exporting to the rust-sdk export method

This commit is contained in:
Damir Jelić 2021-03-29 16:36:40 +02:00
parent 32cf645c5f
commit 7f89e33037
5 changed files with 27 additions and 13 deletions

View File

@ -835,18 +835,12 @@ internal class DefaultCryptoService @Inject constructor(
* Export the crypto keys * Export the crypto keys
* *
* @param password the password * @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 { private suspend fun exportRoomKeys(password: String, anIterationCount: Int): ByteArray {
return withContext(coroutineDispatchers.crypto) { return withContext(coroutineDispatchers.crypto) {
val iterationCount = max(0, anIterationCount) val iterationCount = max(10000, anIterationCount)
olmMachine!!.exportKeys(password, iterationCount)
val exportedSessions = cryptoStore.getInboundGroupSessions().mapNotNull { it.exportKeys() }
val adapter = MoshiProvider.providesMoshi()
.adapter(List::class.java)
MXMegolmExportEncryption.encryptMegolmKeyFile(adapter.toJson(exportedSessions), password, iterationCount)
} }
} }

View File

@ -38,6 +38,7 @@ import uniffi.olm.Logger
import uniffi.olm.OlmMachine as InnerMachine import uniffi.olm.OlmMachine as InnerMachine
import uniffi.olm.Request import uniffi.olm.Request
import uniffi.olm.RequestType import uniffi.olm.RequestType
import uniffi.olm.CryptoStoreErrorException
import uniffi.olm.Sas as InnerSas import uniffi.olm.Sas as InnerSas
import uniffi.olm.setLogger 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) @Throws(MXCryptoError::class)
suspend fun decryptRoomEvent(event: Event): MXEventDecryptionResult = withContext(Dispatchers.IO) { suspend fun decryptRoomEvent(event: Event): MXEventDecryptionResult = withContext(Dispatchers.IO) {
val adapter = MoshiProvider.providesMoshi().adapter<Event>(Event::class.java) val adapter = MoshiProvider.providesMoshi().adapter<Event>(Event::class.java)

View File

@ -15,6 +15,8 @@ pub enum CryptoStoreError {
CryptoStore(#[from] InnerStoreError), CryptoStore(#[from] InnerStoreError),
#[error(transparent)] #[error(transparent)]
OlmError(#[from] OlmError), OlmError(#[from] OlmError),
#[error(transparent)]
Serialization(#[from] serde_json::Error),
} }
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]

View File

@ -10,8 +10,7 @@ use tokio::runtime::Runtime;
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::{ api::r0::{
keys::{ keys::{
claim_keys::{Request as KeysClaimRequest, Response as KeysClaimResponse}, claim_keys::Response as KeysClaimResponse, get_keys::Response as KeysQueryResponse,
get_keys::Response as KeysQueryResponse,
upload_keys::Response as KeysUploadResponse, upload_keys::Response as KeysUploadResponse,
}, },
sync::sync_events::{DeviceLists as RumaDeviceLists, ToDevice}, sync::sync_events::{DeviceLists as RumaDeviceLists, ToDevice},
@ -26,8 +25,8 @@ use matrix_sdk_common::{
}; };
use matrix_sdk_crypto::{ use matrix_sdk_crypto::{
EncryptionSettings, IncomingResponse, OlmMachine as InnerMachine, OutgoingRequest, encrypt_key_export, EncryptionSettings, IncomingResponse, OlmMachine as InnerMachine,
ToDeviceRequest, OutgoingRequest, ToDeviceRequest,
}; };
use crate::error::{CryptoStoreError, DecryptionError, MachineCreationError}; use crate::error::{CryptoStoreError, DecryptionError, MachineCreationError};
@ -420,6 +419,15 @@ impl OlmMachine {
serde_json::to_string(&encrypted_content).unwrap() serde_json::to_string(&encrypted_content).unwrap()
} }
pub fn export_keys(&self, passphrase: &str, rounds: i32) -> Result<String, CryptoStoreError> {
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( pub fn decrypt_room_event(
&self, &self,
event: &str, event: &str,

View File

@ -16,6 +16,7 @@ enum MachineCreationError {
enum CryptoStoreError { enum CryptoStoreError {
"CryptoStore", "CryptoStore",
"OlmError", "OlmError",
"Serialization",
}; };
[Error] [Error]
@ -102,4 +103,7 @@ interface OlmMachine {
[Throws=CryptoStoreError] [Throws=CryptoStoreError]
Sas start_verification([ByRef] Device device); Sas start_verification([ByRef] Device device);
[Throws=CryptoStoreError]
string export_keys([ByRef] string passphrase, i32 rounds);
}; };