crypto: Clean up the key querying logic.

master
Damir Jelić 2020-04-21 09:44:44 +02:00
parent 7c54ad5c5f
commit 00f4dadbbb
1 changed files with 36 additions and 26 deletions

View File

@ -342,15 +342,17 @@ impl OlmMachine {
/// Receive a successful keys query response. /// Receive a successful keys query response.
/// ///
/// Returns a list of devices newly discovered devices and devices that
/// changed.
///
/// # Arguments /// # Arguments
/// ///
/// * `response` - The keys query response of the request that the client /// * `response` - The keys query response of the request that the client
/// performed. /// performed.
// TODO this should return a list of changed devices.
pub async fn receive_keys_query_response( pub async fn receive_keys_query_response(
&mut self, &mut self,
response: &keys::get_keys::Response, response: &keys::get_keys::Response,
) -> Result<()> { ) -> Result<Vec<Device>> {
let mut changed_devices = Vec::new(); let mut changed_devices = Vec::new();
for (user_id, device_map) in &response.device_keys { for (user_id, device_map) in &response.device_keys {
@ -370,20 +372,15 @@ impl OlmMachine {
continue; continue;
} }
// let curve_key_id =
// AlgorithmAndDeviceId(KeyAlgorithm::Curve25519, device_id.to_owned());
let ed_key_id = AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, device_id.to_owned()); let ed_key_id = AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, device_id.to_owned());
// TODO check if the curve key changed for an existing device.
// let sender_key = if let Some(k) = device_keys.keys.get(&curve_key_id) {
// k
// } else {
// continue;
// };
let signing_key = if let Some(k) = device_keys.keys.get(&ed_key_id) { let signing_key = if let Some(k) = device_keys.keys.get(&ed_key_id) {
k k
} else { } else {
warn!(
"Ed25519 identity key wasn't found for user/device {} {}",
user_id, device_id
);
continue; continue;
}; };
@ -398,19 +395,28 @@ impl OlmMachine {
continue; continue;
} }
let device = self let device = self.store.get_device(&user_id, device_id).await?;
.store
.get_device(&user_id, device_id)
.await
.expect("Can't load device");
if let Some(_d) = device { let device = if let Some(mut d) = device {
// TODO check what and if anything changed for the device. let stored_signing_key = d.get_key(&KeyAlgorithm::Ed25519);
if let Some(stored_signing_key) = stored_signing_key {
if stored_signing_key != signing_key {
warn!("Ed25519 key has changed for {} {}", user_id, device_id);
continue;
}
}
d.update_device(device_keys);
d
} else { } else {
let device = Device::from(device_keys); let device = Device::from(device_keys);
info!("Found new device {:?}", device); info!("Adding a new device to the device store {:?}", device);
changed_devices.push(device); device
} };
changed_devices.push(device);
} }
let current_devices: HashSet<&DeviceId> = device_map.keys().collect(); let current_devices: HashSet<&DeviceId> = device_map.keys().collect();
@ -419,16 +425,20 @@ impl OlmMachine {
let deleted_devices = stored_devices_set.difference(&current_devices); let deleted_devices = stored_devices_set.difference(&current_devices);
for _device_id in deleted_devices { for device_id in deleted_devices {
// TODO delete devices here. if let Some(device) = stored_devices.get(device_id) {
device.mark_as_deleted();
// TODO change this to a delete device.
self.store.save_device(device).await?;
}
} }
} }
for device in changed_devices { for device in &changed_devices {
self.store.save_device(device).await.unwrap(); self.store.save_device(device.clone()).await?;
} }
Ok(()) Ok(changed_devices)
} }
/// Generate new one-time keys. /// Generate new one-time keys.