From 5f9a4fc6d1dccc5a47746fa7043de76cd88507a4 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 14 Aug 2024 18:41:14 +0100 Subject: [PATCH] crypto: update `OwnUserIdentityData::is_device_signed` to return bool As before: since all the callers end up calling `.is_ok()` on the result, and the name implies it should return a bool, let's just return a bool. --- .../src/identities/device.rs | 4 ++-- .../matrix-sdk-crypto/src/identities/user.rs | 24 +++++++------------ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/identities/device.rs b/crates/matrix-sdk-crypto/src/identities/device.rs index a1ec2e174..becd04fbe 100644 --- a/crates/matrix-sdk-crypto/src/identities/device.rs +++ b/crates/matrix-sdk-crypto/src/identities/device.rs @@ -744,7 +744,7 @@ impl DeviceData { |(own_identity, device_identity)| { match device_identity { UserIdentityData::Own(_) => { - own_identity.is_verified() && own_identity.is_device_signed(self).is_ok() + own_identity.is_verified() && own_identity.is_device_signed(self) } // If it's a device from someone else, first check @@ -766,7 +766,7 @@ impl DeviceData { match device_owner_identity { // If it's one of our own devices, just check that // we signed the device. - UserIdentityData::Own(identity) => identity.is_device_signed(self).is_ok(), + UserIdentityData::Own(identity) => identity.is_device_signed(self), // If it's a device from someone else, check // if the other user has signed this device. UserIdentityData::Other(device_identity) => { diff --git a/crates/matrix-sdk-crypto/src/identities/user.rs b/crates/matrix-sdk-crypto/src/identities/user.rs index d5bed00a1..56264a796 100644 --- a/crates/matrix-sdk-crypto/src/identities/user.rs +++ b/crates/matrix-sdk-crypto/src/identities/user.rs @@ -916,22 +916,17 @@ impl OwnUserIdentityData { /// Check if the given device has been signed by this identity. /// - /// Only devices of our own user should be checked with this method, if a - /// device of a different user is given the signature check will always fail - /// even if a valid signature exists. + /// Only devices of our own user should be checked with this method. If a + /// device of a different user is given, the signature check will always + /// fail even if a valid signature exists. /// /// # Arguments /// /// * `device` - The device that should be checked for a valid signature. /// - /// Returns an empty result if the signature check succeeded, otherwise a - /// SignatureError indicating why the check failed. - pub(crate) fn is_device_signed(&self, device: &DeviceData) -> Result<(), SignatureError> { - if self.user_id() != device.user_id() { - return Err(SignatureError::UserIdMismatch); - } - - self.self_signing_key.verify_device(device) + /// Returns `true` if the signature check succeeded, otherwise `false`. + pub(crate) fn is_device_signed(&self, device: &DeviceData) -> bool { + self.user_id() == device.user_id() && self.self_signing_key.verify_device(device).is_ok() } /// Mark our identity as verified. @@ -995,8 +990,7 @@ impl OwnUserIdentityData { devices .into_iter() .filter_map(|(device_id, device)| { - (device_id != own_device_id && self.is_device_signed(&device).is_ok()) - .then_some(device_id) + (device_id != own_device_id && self.is_device_signed(&device)).then_some(device_id) }) .collect() } @@ -1217,8 +1211,8 @@ pub(crate) mod tests { let identity = get_own_identity(); let (first, second) = device(&response); - identity.is_device_signed(&first).unwrap_err(); - identity.is_device_signed(&second).unwrap(); + assert!(!identity.is_device_signed(&first)); + assert!(identity.is_device_signed(&second)); let private_identity = Arc::new(Mutex::new(PrivateCrossSigningIdentity::empty(second.user_id())));