feat(bindings)!: Allow passing the E2EE settings when sharing a room key

This commit is contained in:
Damir Jelić
2022-10-20 18:17:37 +02:00
parent 4a6208f808
commit c03c90c1cf
3 changed files with 128 additions and 13 deletions

View File

@@ -16,7 +16,7 @@ mod uniffi_api;
mod users;
mod verification;
use std::{borrow::Borrow, collections::HashMap, str::FromStr, sync::Arc};
use std::{borrow::Borrow, collections::HashMap, str::FromStr, sync::Arc, time::Duration};
pub use backup_recovery_key::{
BackupRecoveryKey, DecodeError, MegolmV1BackupKey, PassphraseInfo, PkDecryptionError,
@@ -29,14 +29,17 @@ use js_int::UInt;
pub use logger::{set_logger, Logger};
pub use machine::{KeyRequestPair, OlmMachine};
use matrix_sdk_crypto::{
types::{EventEncryptionAlgorithm, SigningKey},
LocalTrust,
types::{EventEncryptionAlgorithm as RustEventEncryptionAlgorithm, SigningKey},
EncryptionSettings as RustEncryptionSettings, LocalTrust,
};
pub use responses::{
BootstrapCrossSigningResult, DeviceLists, KeysImportResult, OutgoingVerificationRequest,
Request, RequestType, SignatureUploadRequest, UploadSigningKeysRequest,
};
use ruma::{DeviceId, DeviceKeyAlgorithm, OwnedUserId, RoomId, SecondsSinceUnixEpoch, UserId};
use ruma::{
events::room::history_visibility::HistoryVisibility as RustHistoryVisibility, DeviceId,
DeviceKeyAlgorithm, OwnedUserId, RoomId, SecondsSinceUnixEpoch, UserId,
};
use serde::{Deserialize, Serialize};
pub use users::UserIdentity;
pub use verification::{
@@ -275,7 +278,7 @@ pub fn migrate(
imported: session.imported,
backed_up: session.backed_up,
history_visibility: None,
algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
algorithm: RustEventEncryptionAlgorithm::MegolmV1AesSha2,
};
let session = matrix_sdk_crypto::olm::InboundGroupSession::from_pickle(pickle)?;
@@ -350,6 +353,94 @@ impl<T: Fn(i32, i32)> ProgressListener for T {
}
}
/// An encryption algorithm to be used to encrypt messages sent to a room.
pub enum EventEncryptionAlgorithm {
/// Olm version 1 using Curve25519, AES-256, and SHA-256.
OlmV1Curve25519AesSha2,
/// Megolm version 1 using AES-256 and SHA-256.
MegolmV1AesSha2,
}
impl From<EventEncryptionAlgorithm> for RustEventEncryptionAlgorithm {
fn from(a: EventEncryptionAlgorithm) -> Self {
match a {
EventEncryptionAlgorithm::OlmV1Curve25519AesSha2 => {
RustEventEncryptionAlgorithm::OlmV1Curve25519AesSha2
}
EventEncryptionAlgorithm::MegolmV1AesSha2 => {
RustEventEncryptionAlgorithm::MegolmV1AesSha2
}
}
}
}
/// Who can see a room's history.
pub enum HistoryVisibility {
/// Previous events are accessible to newly joined members from the point
/// they were invited onwards.
///
/// Events stop being accessible when the member's state changes to
/// something other than *invite* or *join*.
Invited,
/// Previous events are accessible to newly joined members from the point
/// they joined the room onwards.
/// Events stop being accessible when the member's state changes to
/// something other than *join*.
Joined,
/// Previous events are always accessible to newly joined members.
///
/// All events in the room are accessible, even those sent when the member
/// was not a part of the room.
Shared,
/// All events while this is the `HistoryVisibility` value may be shared by
/// any participating homeserver with anyone, regardless of whether they
/// have ever joined the room.
WorldReadable,
}
impl From<HistoryVisibility> for RustHistoryVisibility {
fn from(h: HistoryVisibility) -> Self {
match h {
HistoryVisibility::Invited => RustHistoryVisibility::Invited,
HistoryVisibility::Joined => RustHistoryVisibility::Joined,
HistoryVisibility::Shared => RustHistoryVisibility::Shared,
HistoryVisibility::WorldReadable => RustHistoryVisibility::Shared,
}
}
}
/// Settings for an encrypted room.
///
/// This determines the algorithm and rotation periods of a group session.
pub struct EncryptionSettings {
/// The encryption algorithm that should be used in the room.
pub algorithm: EventEncryptionAlgorithm,
/// How long the session should be used before changing it. Time in seconds.
pub rotation_period: u64,
/// How many messages should be sent before changing the session.
pub rotation_period_msgs: u64,
/// The history visibility of the room when the session was created.
pub history_visibility: HistoryVisibility,
/// Should untrusted devices receive the room key, or should they be
/// excluded from the conversation.
pub only_allow_trusted_devices: bool,
}
impl From<EncryptionSettings> for RustEncryptionSettings {
fn from(v: EncryptionSettings) -> Self {
RustEncryptionSettings {
algorithm: v.algorithm.into(),
rotation_period: Duration::from_secs(v.rotation_period),
rotation_period_msgs: v.rotation_period_msgs,
history_visibility: v.history_visibility.into(),
only_allow_trusted_devices: v.only_allow_trusted_devices,
}
}
}
/// An event that was successfully decrypted.
pub struct DecryptedEvent {
/// The decrypted version of the event.

View File

@@ -11,9 +11,8 @@ use js_int::UInt;
use matrix_sdk_common::deserialized_responses::AlgorithmInfo;
use matrix_sdk_crypto::{
backups::MegolmV1BackupKey as RustBackupKey, decrypt_room_key_export, encrypt_room_key_export,
matrix_sdk_qrcode::QrVerificationData, olm::ExportedRoomKey, store::RecoveryKey,
EncryptionSettings, LocalTrust, OlmMachine as InnerMachine, UserIdentities,
Verification as RustVerification,
matrix_sdk_qrcode::QrVerificationData, olm::ExportedRoomKey, store::RecoveryKey, LocalTrust,
OlmMachine as InnerMachine, UserIdentities, Verification as RustVerification,
};
use ruma::{
api::{
@@ -46,9 +45,9 @@ use crate::{
responses::{response_from_string, OutgoingVerificationRequest, OwnedResponse},
BackupKeys, BackupRecoveryKey, BootstrapCrossSigningResult, ConfirmVerificationResult,
CrossSigningKeyExport, CrossSigningStatus, DecodeError, DecryptedEvent, Device, DeviceLists,
KeyImportError, KeysImportResult, MegolmV1BackupKey, ProgressListener, QrCode, Request,
RequestType, RequestVerificationResult, RoomKeyCounts, ScanResult, SignatureUploadRequest,
StartSasResult, UserIdentity, Verification, VerificationRequest,
EncryptionSettings, KeyImportError, KeysImportResult, MegolmV1BackupKey, ProgressListener,
QrCode, Request, RequestType, RequestVerificationResult, RoomKeyCounts, ScanResult,
SignatureUploadRequest, StartSasResult, UserIdentity, Verification, VerificationRequest,
};
/// A high level state machine that handles E2EE for Matrix.
@@ -521,6 +520,7 @@ impl OlmMachine {
&self,
room_id: &str,
users: Vec<String>,
settings: EncryptionSettings,
) -> Result<Vec<Request>, CryptoStoreError> {
let users: Vec<OwnedUserId> =
users.into_iter().filter_map(|u| UserId::parse(u).ok()).collect();
@@ -529,7 +529,7 @@ impl OlmMachine {
let requests = self.runtime.block_on(self.inner.share_room_key(
&room_id,
users.iter().map(Deref::deref),
EncryptionSettings::default(),
settings,
))?;
Ok(requests.into_iter().map(|r| r.as_ref().into()).collect())

View File

@@ -254,6 +254,26 @@ enum LocalTrust {
"Unset",
};
enum EventEncryptionAlgorithm {
"OlmV1Curve25519AesSha2",
"MegolmV1AesSha2",
};
enum HistoryVisibility {
"Invited",
"Joined",
"Shared",
"WorldReadable",
};
dictionary EncryptionSettings {
EventEncryptionAlgorithm algorithm;
u64 rotation_period;
u64 rotation_period_msgs;
HistoryVisibility history_visibility;
boolean only_allow_trusted_devices;
};
interface OlmMachine {
[Throws=CryptoStoreError]
constructor(
@@ -301,7 +321,11 @@ interface OlmMachine {
[Throws=CryptoStoreError]
Request? get_missing_sessions(sequence<string> users);
[Throws=CryptoStoreError]
sequence<Request> share_room_key([ByRef] string room_id, sequence<string> users);
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);