crypto-ffi: Use UniFFI proc-macros for more OlmMachine methods

This commit is contained in:
Jonas Platte
2023-03-07 16:53:34 +01:00
committed by Jonas Platte
parent 9fe880d05d
commit fb23cbff97
3 changed files with 61 additions and 117 deletions

View File

@@ -717,10 +717,14 @@ mod uniffi_types {
backup_recovery_key::{
BackupRecoveryKey, DecodeError, MegolmV1BackupKey, PassphraseInfo, PkDecryptionError,
},
error::CryptoStoreError,
machine::OlmMachine,
responses::Request,
BackupKeys, CrossSigningStatus, RoomKeyCounts,
error::{CryptoStoreError, DecryptionError, SecretImportError},
machine::{KeyRequestPair, OlmMachine},
responses::{BootstrapCrossSigningResult, DeviceLists, Request},
verification::{
RequestVerificationResult, StartSasResult, Verification, VerificationRequest,
},
BackupKeys, CrossSigningKeyExport, CrossSigningStatus, DecryptedEvent, EncryptionSettings,
RoomKeyCounts,
};
}

View File

@@ -450,7 +450,10 @@ impl OlmMachine {
Ok(())
}
}
#[uniffi::export]
impl OlmMachine {
/// Let the state machine know about E2EE related sync changes that we
/// received from the server.
///
@@ -468,12 +471,12 @@ impl OlmMachine {
/// * `key_counts` - The map of uploaded one-time key types and counts.
pub fn receive_sync_changes(
&self,
events: &str,
events: String,
device_changes: DeviceLists,
key_counts: HashMap<String, i32>,
unused_fallback_keys: Option<Vec<String>>,
) -> Result<String, CryptoStoreError> {
let to_device: ToDevice = serde_json::from_str(events)?;
let to_device: ToDevice = serde_json::from_str(&events)?;
let device_changes: RumaDeviceLists = device_changes.into();
let key_counts: BTreeMap<DeviceKeyAlgorithm, UInt> = key_counts
.into_iter()
@@ -528,8 +531,8 @@ impl OlmMachine {
///
/// A user can be marked for tracking using the
/// [`OlmMachine::update_tracked_users()`] method.
pub fn is_user_tracked(&self, user_id: &str) -> Result<bool, CryptoStoreError> {
let user_id = parse_user_id(user_id)?;
pub fn is_user_tracked(&self, user_id: String) -> Result<bool, CryptoStoreError> {
let user_id = parse_user_id(&user_id)?;
Ok(self.runtime.block_on(self.inner.tracked_users())?.contains(&user_id))
}
@@ -581,7 +584,7 @@ impl OlmMachine {
/// * `settings` - The settings that should be used for the room key.
pub fn share_room_key(
&self,
room_id: &str,
room_id: String,
users: Vec<String>,
settings: EncryptionSettings,
) -> Result<Vec<Request>, CryptoStoreError> {
@@ -633,16 +636,16 @@ impl OlmMachine {
/// * `content` - The serialized content of the event.
pub fn encrypt(
&self,
room_id: &str,
event_type: &str,
content: &str,
room_id: String,
event_type: String,
content: String,
) -> Result<String, CryptoStoreError> {
let room_id = RoomId::parse(room_id)?;
let content: Value = serde_json::from_str(content)?;
let content: Value = serde_json::from_str(&content)?;
let encrypted_content = self
.runtime
.block_on(self.inner.encrypt_room_event_raw(&room_id, content, event_type))
.block_on(self.inner.encrypt_room_event_raw(&room_id, content, &event_type))
.expect("Encrypting an event produced an error");
Ok(serde_json::to_string(&encrypted_content)?)
@@ -657,8 +660,8 @@ impl OlmMachine {
/// * `room_id` - The unique id of the room where the event was sent to.
pub fn decrypt_room_event(
&self,
event: &str,
room_id: &str,
event: String,
room_id: String,
handle_verification_events: bool,
) -> Result<DecryptedEvent, DecryptionError> {
// Element Android wants only the content and the type and will create a
@@ -672,7 +675,7 @@ impl OlmMachine {
content: &'a RawValue,
}
let event: Raw<_> = serde_json::from_str(event)?;
let event: Raw<_> = serde_json::from_str(&event)?;
let room_id = RoomId::parse(room_id)?;
let decrypted = self.runtime.block_on(self.inner.decrypt_room_event(&event, &room_id))?;
@@ -727,10 +730,10 @@ impl OlmMachine {
/// * `room_id` - The id of the room the event was sent to.
pub fn request_room_key(
&self,
event: &str,
room_id: &str,
event: String,
room_id: String,
) -> Result<KeyRequestPair, DecryptionError> {
let event: Raw<_> = serde_json::from_str(event)?;
let event: Raw<_> = serde_json::from_str(&event)?;
let room_id = RoomId::parse(room_id)?;
let (cancel, request) =
@@ -753,17 +756,19 @@ impl OlmMachine {
/// passphrase into an key.
pub fn export_room_keys(
&self,
passphrase: &str,
passphrase: String,
rounds: i32,
) -> Result<String, CryptoStoreError> {
let keys = self.runtime.block_on(self.inner.export_room_keys(|_| true))?;
let encrypted = encrypt_room_key_export(&keys, passphrase, rounds as u32)
let encrypted = encrypt_room_key_export(&keys, &passphrase, rounds as u32)
.map_err(CryptoStoreError::Serialization)?;
Ok(encrypted)
}
}
impl OlmMachine {
fn import_room_keys_helper(
&self,
keys: Vec<ExportedRoomKey>,
@@ -838,10 +843,13 @@ impl OlmMachine {
self.import_room_keys_helper(keys, true, progress_listener)
}
}
#[uniffi::export]
impl OlmMachine {
/// Discard the currently active room key for the given room if there is
/// one.
pub fn discard_room_key(&self, room_id: &str) -> Result<(), CryptoStoreError> {
pub fn discard_room_key(&self, room_id: String) -> Result<(), CryptoStoreError> {
let room_id = RoomId::parse(room_id)?;
self.runtime.block_on(self.inner.invalidate_group_session(&room_id))?;
@@ -857,8 +865,8 @@ impl OlmMachine {
/// **Note**: This has been deprecated.
pub fn receive_unencrypted_verification_event(
&self,
event: &str,
room_id: &str,
event: String,
room_id: String,
) -> Result<(), CryptoStoreError> {
self.receive_verification_event(event, room_id)
}
@@ -869,11 +877,11 @@ impl OlmMachine {
/// in rooms to the `OlmMachine`. The event should be in the decrypted form.
pub fn receive_verification_event(
&self,
event: &str,
room_id: &str,
event: String,
room_id: String,
) -> Result<(), CryptoStoreError> {
let room_id = RoomId::parse(room_id)?;
let event: AnySyncMessageLikeEvent = serde_json::from_str(event)?;
let event: AnySyncMessageLikeEvent = serde_json::from_str(&event)?;
let event = event.into_full_event(room_id);
@@ -888,7 +896,7 @@ impl OlmMachine {
///
/// * `user_id` - The ID of the user for which we would like to fetch the
/// verification requests.
pub fn get_verification_requests(&self, user_id: &str) -> Vec<Arc<VerificationRequest>> {
pub fn get_verification_requests(&self, user_id: String) -> Vec<Arc<VerificationRequest>> {
let Ok(user_id) = UserId::parse(user_id) else {
return vec![];
};
@@ -913,8 +921,8 @@ impl OlmMachine {
/// * `flow_id` - The ID that uniquely identifies the verification flow.
pub fn get_verification_request(
&self,
user_id: &str,
flow_id: &str,
user_id: String,
flow_id: String,
) -> Option<Arc<VerificationRequest>> {
let user_id = UserId::parse(user_id).ok()?;
@@ -934,10 +942,10 @@ impl OlmMachine {
/// support.
pub fn verification_request_content(
&self,
user_id: &str,
user_id: String,
methods: Vec<String>,
) -> Result<Option<String>, CryptoStoreError> {
let user_id = parse_user_id(user_id)?;
let user_id = parse_user_id(&user_id)?;
let identity = self.runtime.block_on(self.inner.get_identity(&user_id, None))?;
@@ -974,12 +982,12 @@ impl OlmMachine {
/// [verification_request_content()]: #method.verification_request_content
pub fn request_verification(
&self,
user_id: &str,
room_id: &str,
event_id: &str,
user_id: String,
room_id: String,
event_id: String,
methods: Vec<String>,
) -> Result<Option<Arc<VerificationRequest>>, CryptoStoreError> {
let user_id = parse_user_id(user_id)?;
let user_id = parse_user_id(&user_id)?;
let event_id = EventId::parse(event_id)?;
let room_id = RoomId::parse(room_id)?;
@@ -1016,17 +1024,18 @@ impl OlmMachine {
/// supported in the `m.key.verification.request` event.
pub fn request_verification_with_device(
&self,
user_id: &str,
device_id: &str,
user_id: String,
device_id: String,
methods: Vec<String>,
) -> Result<Option<RequestVerificationResult>, CryptoStoreError> {
let user_id = parse_user_id(user_id)?;
let user_id = parse_user_id(&user_id)?;
let device_id = device_id.as_str().into();
let methods = methods.into_iter().map(VerificationMethod::from).collect();
Ok(
if let Some(device) =
self.runtime.block_on(self.inner.get_device(&user_id, device_id.into(), None))?
self.runtime.block_on(self.inner.get_device(&user_id, device_id, None))?
{
let (verification, request) =
self.runtime.block_on(device.request_verification_with_methods(methods));
@@ -1085,11 +1094,11 @@ impl OlmMachine {
/// verification.
///
/// * `flow_id` - The ID that uniquely identifies the verification flow.
pub fn get_verification(&self, user_id: &str, flow_id: &str) -> Option<Arc<Verification>> {
pub fn get_verification(&self, user_id: String, flow_id: String) -> Option<Arc<Verification>> {
let user_id = UserId::parse(user_id).ok()?;
self.inner
.get_verification(&user_id, flow_id)
.get_verification(&user_id, &flow_id)
.map(|v| Verification { inner: v, runtime: self.runtime.handle().to_owned() }.into())
}
@@ -1109,14 +1118,15 @@ impl OlmMachine {
/// [request_verification_with_device()]: #method.request_verification_with_device
pub fn start_sas_with_device(
&self,
user_id: &str,
device_id: &str,
user_id: String,
device_id: String,
) -> Result<Option<StartSasResult>, CryptoStoreError> {
let user_id = parse_user_id(user_id)?;
let user_id = parse_user_id(&user_id)?;
let device_id = device_id.as_str().into();
Ok(
if let Some(device) =
self.runtime.block_on(self.inner.get_device(&user_id, device_id.into(), None))?
self.runtime.block_on(self.inner.get_device(&user_id, device_id, None))?
{
let (sas, request) = self.runtime.block_on(device.start_verification())?;
@@ -1159,10 +1169,7 @@ impl OlmMachine {
Ok(())
}
}
#[uniffi::export]
impl OlmMachine {
/// Activate the given backup key to be used with the given backup version.
///
/// **Warning**: The caller needs to make sure that the given `BackupKey` is

View File

@@ -354,11 +354,6 @@ interface OlmMachine {
string? passphrase
);
[Throws=CryptoStoreError]
string receive_sync_changes([ByRef] string events,
DeviceLists device_changes,
record<DOMString, i32> key_counts,
sequence<string>? unused_fallback_keys);
[Throws=CryptoStoreError]
sequence<Request> outgoing_requests();
[Throws=CryptoStoreError]
@@ -368,11 +363,6 @@ interface OlmMachine {
[ByRef] string response
);
[Throws=DecryptionError]
DecryptedEvent decrypt_room_event([ByRef] string event, [ByRef] string room_id, boolean handle_verificaton_events);
[Throws=CryptoStoreError]
string encrypt([ByRef] string room_id, [ByRef] string event_type, [ByRef] string content);
[Throws=CryptoStoreError]
UserIdentity? get_identity([ByRef] string user_id, u32 timeout);
[Throws=SignatureError]
@@ -386,56 +376,6 @@ interface OlmMachine {
[Throws=CryptoStoreError]
sequence<Device> get_user_devices([ByRef] string user_id, u32 timeout);
[Throws=CryptoStoreError]
boolean is_user_tracked([ByRef] string user_id);
[Throws=CryptoStoreError]
void update_tracked_users(sequence<string> users);
[Throws=CryptoStoreError]
Request? get_missing_sessions(sequence<string> users);
[Throws=CryptoStoreError]
sequence<Request> share_room_key(
[ByRef] string room_id,
sequence<string> users,
EncryptionSettings settings
);
[Throws=CryptoStoreError]
void receive_unencrypted_verification_event([ByRef] string event, [ByRef] string room_id);
[Throws=CryptoStoreError]
void receive_verification_event([ByRef] string event, [ByRef] string room_id);
sequence<VerificationRequest> get_verification_requests([ByRef] string user_id);
VerificationRequest? get_verification_request([ByRef] string user_id, [ByRef] string flow_id);
Verification? get_verification([ByRef] string user_id, [ByRef] string flow_id);
[Throws=CryptoStoreError]
VerificationRequest? request_verification(
[ByRef] string user_id,
[ByRef] string room_id,
[ByRef] string event_id,
sequence<string> methods
);
[Throws=CryptoStoreError]
string? verification_request_content(
[ByRef] string user_id,
sequence<string> methods
);
[Throws=CryptoStoreError]
RequestVerificationResult? request_self_verification(sequence<string> methods);
[Throws=CryptoStoreError]
RequestVerificationResult? request_verification_with_device(
[ByRef] string user_id,
[ByRef] string device_id,
sequence<string> methods
);
[Throws=CryptoStoreError]
StartSasResult? start_sas_with_device([ByRef] string user_id, [ByRef] string device_id);
[Throws=DecryptionError]
KeyRequestPair request_room_key([ByRef] string event, [ByRef] string room_id);
[Throws=CryptoStoreError]
string export_room_keys([ByRef] string passphrase, i32 rounds);
[Throws=KeyImportError]
KeysImportResult import_room_keys(
[ByRef] string keys,
@@ -447,14 +387,7 @@ interface OlmMachine {
[ByRef] string keys,
ProgressListener progress_listener
);
[Throws=CryptoStoreError]
void discard_room_key([ByRef] string room_id);
[Throws=CryptoStoreError]
BootstrapCrossSigningResult bootstrap_cross_signing();
CrossSigningKeyExport? export_cross_signing_keys();
[Throws=SecretImportError]
void import_cross_signing_keys(CrossSigningKeyExport export);
[Throws=CryptoStoreError]
boolean is_identity_verified([ByRef] string user_id);