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
*
* @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)
}
}

View File

@ -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>(Event::class.java)

View File

@ -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)]

View File

@ -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<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(
&self,
event: &str,

View File

@ -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);
};