From e401c87246951dcdaa663916dabe34fcf436de90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 4 Aug 2021 10:55:41 +0200 Subject: [PATCH] crypto: When we check the signature of a device use the DeviceKeys struct --- matrix_sdk_crypto/src/identities/device.rs | 12 +-------- matrix_sdk_crypto/src/identities/user.rs | 30 +++++++++++++--------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/matrix_sdk_crypto/src/identities/device.rs b/matrix_sdk_crypto/src/identities/device.rs index 22871059..6ad19595 100644 --- a/matrix_sdk_crypto/src/identities/device.rs +++ b/matrix_sdk_crypto/src/identities/device.rs @@ -539,21 +539,11 @@ impl ReadOnlyDevice { ) } - pub(crate) fn as_signature_message(&self) -> Value { - json!({ - "user_id": &*self.user_id, - "device_id": &*self.device_id, - "keys": &*self.keys, - "algorithms": &*self.algorithms, - "signatures": &*self.signatures, - }) - } - pub(crate) fn verify_device_keys( &self, device_keys: &DeviceKeys, ) -> Result<(), SignatureError> { - let mut device_keys = serde_json::to_value(device_keys).unwrap(); + let mut device_keys = serde_json::to_value(device_keys)?; self.is_signed_by_device(&mut device_keys) } diff --git a/matrix_sdk_crypto/src/identities/user.rs b/matrix_sdk_crypto/src/identities/user.rs index 178aae54..aa683ebc 100644 --- a/matrix_sdk_crypto/src/identities/user.rs +++ b/matrix_sdk_crypto/src/identities/user.rs @@ -23,7 +23,7 @@ use std::{ }; use ruma::{ - encryption::{CrossSigningKey, KeyUsage}, + encryption::{CrossSigningKey, DeviceKeys, KeyUsage}, events::{ key::verification::VerificationMethod, room::message::KeyVerificationRequestEventContent, }, @@ -494,6 +494,22 @@ impl SelfSigningPubkey { &self.0.keys } + pub(crate) fn verify_device_keys(&self, device_keys: DeviceKeys) -> Result<(), SignatureError> { + let (key_id, key) = self.0.keys.iter().next().ok_or(SignatureError::MissingSigningKey)?; + // TODO check that the usage is OK. + + let mut device = to_value(device_keys)?; + + let utility = Utility::new(); + + utility.verify_json( + &self.0.user_id, + &DeviceKeyId::try_from(key_id.as_str())?, + key, + &mut device, + ) + } + /// Check if the given device is signed by this self signing key. /// /// # Arguments @@ -503,17 +519,7 @@ impl SelfSigningPubkey { /// Returns an empty result if the signature check succeeded, otherwise a /// SignatureError indicating why the check failed. pub(crate) fn verify_device(&self, device: &ReadOnlyDevice) -> Result<(), SignatureError> { - let (key_id, key) = self.0.keys.iter().next().ok_or(SignatureError::MissingSigningKey)?; - - // TODO check that the usage is OK. - - let utility = Utility::new(); - utility.verify_json( - &self.0.user_id, - &DeviceKeyId::try_from(key_id.as_str())?, - key, - &mut device.as_signature_message(), - ) + self.verify_device_keys(device.as_device_keys()) } }