Skip to content

Start uploading fallback keys #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/matrix-sdk-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ http = { version = "0.2.4", optional = true}

[target.'cfg(not(target_arch = "wasm32"))'.dependencies.vodozemac]
git = "https://github.com/matrix-org/vodozemac"
rev = "b6e30ba43742e27f2c996f19f071a6c78e3b30ce"
rev = "71a811026de9076922434381fa593f4184bf5a37"

[target.'cfg(target_arch = "wasm32")'.dependencies.vodozemac]
git = "https://github.com/matrix-org/vodozemac"
rev = "b6e30ba43742e27f2c996f19f071a6c78e3b30ce"
rev = "71a811026de9076922434381fa593f4184bf5a37"
features = ["js"]

[dependencies.ruma]
Expand Down
3 changes: 2 additions & 1 deletion crates/matrix-sdk-crypto/src/identities/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use atomic::Atomic;
use matrix_sdk_common::locks::Mutex;
use ruma::{
api::client::keys::upload_signatures::v3::Request as SignatureUploadRequest,
encryption::{DeviceKeys, SignedKey},
encryption::DeviceKeys,
events::{
forwarded_room_key::ToDeviceForwardedRoomKeyEventContent,
key::verification::VerificationMethod, room::encrypted::ToDeviceRoomEncryptedEventContent,
Expand All @@ -45,6 +45,7 @@ use crate::{
identities::{ReadOnlyOwnUserIdentity, ReadOnlyUserIdentities},
olm::{InboundGroupSession, Session, VerifyJson},
store::{Changes, CryptoStore, DeviceChanges, Result as StoreResult},
types::one_time_keys::SignedKey,
verification::VerificationMachine,
OutgoingVerificationRequest, Sas, ToDeviceRequest, VerificationRequest,
};
Expand Down
1 change: 1 addition & 0 deletions crates/matrix-sdk-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod olm;
mod requests;
mod session_manager;
pub mod store;
mod types;
mod utilities;
mod verification;

Expand Down
65 changes: 24 additions & 41 deletions crates/matrix-sdk-crypto/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,14 +529,18 @@ impl OlmMachine {
/// [`receive_keys_upload_response`]: #method.receive_keys_upload_response
/// [`OlmMachine`]: struct.OlmMachine.html
async fn keys_for_upload(&self) -> Option<upload_keys::v3::Request> {
let (device_keys, one_time_keys) = self.account.keys_for_upload().await?;
let (device_keys, one_time_keys, fallback_keys) = self.account.keys_for_upload().await;

let device_keys =
device_keys.map(|d| Raw::new(&d).expect("Coulnd't serialize device keys"));
if device_keys.is_none() && one_time_keys.is_empty() && fallback_keys.is_empty() {
None
} else {
let device_keys =
device_keys.map(|d| Raw::new(&d).expect("Coulnd't serialize device keys"));

Some(assign!(upload_keys::v3::Request::new(), {
device_keys, one_time_keys, fallback_keys: BTreeMap::new(),
}))
Some(assign!(upload_keys::v3::Request::new(), {
device_keys, one_time_keys, fallback_keys
}))
}
}

/// Decrypt a to-device event.
Expand Down Expand Up @@ -833,9 +837,9 @@ impl OlmMachine {
async fn update_key_counts(
&self,
one_time_key_count: &BTreeMap<DeviceKeyAlgorithm, UInt>,
#[allow(unused_variables)] unused_fallback_keys: Option<&[DeviceKeyAlgorithm]>,
unused_fallback_keys: Option<&[DeviceKeyAlgorithm]>,
) {
self.account.update_uploaded_key_count(one_time_key_count);
self.account.update_key_counts(one_time_key_count, unused_fallback_keys).await;
}

async fn handle_to_device_event(&self, event: &AnyToDeviceEvent) {
Expand Down Expand Up @@ -1468,7 +1472,10 @@ impl OlmMachine {
let device_key_id = DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, self.device_id());
let signature = self.account.sign(message).await;

signatures.entry(self.user_id().to_owned()).or_default().insert(device_key_id, signature);
signatures
.entry(self.user_id().to_owned())
.or_default()
.insert(device_key_id, signature.to_base64());
}

async fn sign_master(
Expand Down Expand Up @@ -1692,48 +1699,23 @@ pub(crate) mod test {
#[async_test]
async fn create_olm_machine() {
let machine = OlmMachine::new(user_id(), alice_device_id());
assert!(machine.account().should_upload_keys().await);
}

#[async_test]
async fn receive_keys_upload_response() {
let machine = OlmMachine::new(user_id(), alice_device_id());
let mut response = keys_upload_response();

response.one_time_key_counts.remove(&DeviceKeyAlgorithm::SignedCurve25519).unwrap();

assert!(machine.account().should_upload_keys().await);
machine.receive_keys_upload_response(&response).await.unwrap();
assert!(machine.account().should_upload_keys().await);

response.one_time_key_counts.insert(DeviceKeyAlgorithm::SignedCurve25519, uint!(10));
machine.receive_keys_upload_response(&response).await.unwrap();
assert!(machine.account().should_upload_keys().await);

response.one_time_key_counts.insert(DeviceKeyAlgorithm::SignedCurve25519, uint!(50));
machine.receive_keys_upload_response(&response).await.unwrap();
assert!(!machine.account().should_upload_keys().await);

response.one_time_key_counts.remove(&DeviceKeyAlgorithm::SignedCurve25519);
machine.receive_keys_upload_response(&response).await.unwrap();
assert!(!machine.account().should_upload_keys().await);
assert!(!machine.account().shared());
}

#[async_test]
async fn generate_one_time_keys() {
let machine = OlmMachine::new(user_id(), alice_device_id());

let mut response = keys_upload_response();
assert!(machine.account.generate_one_time_keys().await.is_some());

assert!(machine.account().should_upload_keys().await);
let mut response = keys_upload_response();

machine.receive_keys_upload_response(&response).await.unwrap();
assert!(machine.account().should_upload_keys().await);
assert!(machine.account.generate_one_time_keys().await.is_ok());
assert!(machine.account.generate_one_time_keys().await.is_some());

response.one_time_key_counts.insert(DeviceKeyAlgorithm::SignedCurve25519, uint!(50));
machine.receive_keys_upload_response(&response).await.unwrap();
assert!(machine.account.generate_one_time_keys().await.is_err());
assert!(machine.account.generate_one_time_keys().await.is_none());
}

#[async_test]
Expand Down Expand Up @@ -1790,10 +1772,11 @@ pub(crate) mod test {
let machine = OlmMachine::new(user_id(), alice_device_id());
machine.account.inner.update_uploaded_key_count(49);

let mut one_time_keys = machine.account.signed_one_time_keys().await.unwrap();
let mut one_time_keys = machine.account.signed_one_time_keys().await;
let ed25519_key = machine.account.identity_keys().ed25519;

let mut one_time_key = one_time_keys.values_mut().next().unwrap();
let mut one_time_key =
one_time_keys.values_mut().next().expect("One time keys should be generated");

ed25519_key
.verify_json(
Expand Down
Loading