crypto: Connect the room key requesting to the rust side

This commit is contained in:
Damir Jelić 2021-04-20 13:29:52 +02:00
parent 0db07011b1
commit 8bfb7a6e0c
5 changed files with 68 additions and 6 deletions

View file

@ -952,7 +952,23 @@ internal class DefaultCryptoService @Inject constructor(
* @param event the event to decrypt again.
*/
override fun reRequestRoomKeyForEvent(event: Event) {
// TODO
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
val requestPair = olmMachine!!.requestRoomKey(event)
if (requestPair.cancellation != null) {
when (requestPair.cancellation) {
is Request.ToDevice -> {
sendToDevice(requestPair.cancellation)
}
}
}
when(requestPair.keyRequest) {
is Request.ToDevice -> {
sendToDevice(requestPair.keyRequest)
}
}
}
}
/**

View file

@ -40,8 +40,10 @@ import org.matrix.android.sdk.internal.session.sync.model.DeviceOneTimeKeysCount
import org.matrix.android.sdk.internal.session.sync.model.ToDeviceSyncResponse
import timber.log.Timber
import uniffi.olm.CryptoStoreErrorException
import uniffi.olm.DecryptionErrorException
import uniffi.olm.Device
import uniffi.olm.DeviceLists
import uniffi.olm.KeyRequestPair
import uniffi.olm.Logger
import uniffi.olm.OlmMachine as InnerMachine
import uniffi.olm.ProgressListener as RustProgressListener
@ -370,6 +372,14 @@ internal class OlmMachine(user_id: String, device_id: String, path: File, device
}
}
@Throws(DecryptionErrorException::class)
suspend fun requestRoomKey(event: Event): KeyRequestPair = withContext(Dispatchers.IO) {
val adapter = MoshiProvider.providesMoshi().adapter<Event>(Event::class.java)
val serializedEvent = adapter.toJson(event)
inner.requestRoomKey(serializedEvent, event.roomId!!)
}
/**
* Export all of our room keys.
*

View file

@ -7,7 +7,7 @@ mod responses;
pub use device::Device;
pub use error::{CryptoStoreError, DecryptionError, KeyImportError, MachineCreationError};
pub use logger::{set_logger, Logger};
pub use machine::{OlmMachine, Sas};
pub use machine::{OlmMachine, Sas, KeyRequestPair};
pub use responses::{DeviceLists, KeysImportResult, Request, RequestType};
pub trait ProgressListener {

View file

@ -47,6 +47,11 @@ pub struct Sas {
pub request: Request,
}
pub struct KeyRequestPair {
pub cancellation: Option<Request>,
pub key_request: Request,
}
impl OlmMachine {
/// Create a new `OlmMachine`
///
@ -133,12 +138,13 @@ impl OlmMachine {
/// [mark_request_as_sent()](#method.mark_request_as_sent) method.
///
/// **Note**: This method call should be locked per call.
pub fn outgoing_requests(&self) -> Vec<Request> {
self.runtime
.block_on(self.inner.outgoing_requests())
pub fn outgoing_requests(&self) -> Result<Vec<Request>, CryptoStoreError> {
Ok(self
.runtime
.block_on(self.inner.outgoing_requests())?
.into_iter()
.map(|r| r.into())
.collect()
.collect())
}
/// Mark a request that was sent to the server as sent.
@ -418,6 +424,27 @@ impl OlmMachine {
})
}
pub fn request_room_key(
&self,
event: &str,
room_id: &str,
) -> Result<KeyRequestPair, DecryptionError> {
let event: SyncMessageEvent<EncryptedEventContent> = serde_json::from_str(event)?;
let room_id = RoomId::try_from(room_id)?;
let (cancel, request) = self
.runtime
.block_on(self.inner.request_room_key(&event, &room_id))?;
let cancellation = cancel.map(|r| r.into());
let key_request = request.into();
Ok(KeyRequestPair {
cancellation,
key_request,
})
}
/// Export all of our room keys.
///
/// # Arguments

View file

@ -70,6 +70,11 @@ dictionary Sas {
Request request;
};
dictionary KeyRequestPair {
Request? cancellation;
Request key_request;
};
[Enum]
interface Request {
ToDevice(string request_id, string event_type, string body);
@ -98,6 +103,7 @@ interface OlmMachine {
string receive_sync_changes([ByRef] string events,
DeviceLists device_changes,
record<DOMString, i32> key_counts);
[Throws=CryptoStoreError]
sequence<Request> outgoing_requests();
[Throws=CryptoStoreError]
void mark_request_as_sent(
@ -125,6 +131,9 @@ interface OlmMachine {
[Throws=CryptoStoreError]
Sas start_verification([ByRef] Device device);
[Throws=DecryptionError]
KeyRequestPair request_room_key([ByRef] string event, [ByRef] string room_id);
[Throws=CryptoStoreError]
string export_keys([ByRef] string passphrase, i32 rounds);
[Throws=KeyImportError]