Skip to content

Commit

Permalink
crypto: Make the return type of is_owner_of_session more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam committed Jul 29, 2024
1 parent 449e8b4 commit 0b46a7e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
6 changes: 6 additions & 0 deletions crates/matrix-sdk-crypto/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ impl std::fmt::Display for MismatchedIdentityKeysError {
}
}

impl From<MismatchedIdentityKeysError> for MegolmError {
fn from(value: MismatchedIdentityKeysError) -> Self {
MegolmError::MismatchedIdentityKeys(value)
}
}

/// Error that occurs when decrypting an event that is malformed.
#[derive(Error, Debug)]
pub enum EventError {
Expand Down
21 changes: 11 additions & 10 deletions crates/matrix-sdk-crypto/src/identities/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use crate::{
DeviceKey, DeviceKeys, EventEncryptionAlgorithm, Signatures, SignedKey,
},
verification::VerificationMachine,
Account, MegolmError, OutgoingVerificationRequest, Sas, ToDeviceRequest, VerificationRequest,
Account, OutgoingVerificationRequest, Sas, ToDeviceRequest, VerificationRequest,
};

pub enum MaybeEncryptedRoomKey {
Expand Down Expand Up @@ -177,7 +177,10 @@ impl Device {
/// An `InboundGroupSession` is exchanged between devices as an Olm
/// encrypted `m.room_key` event. This method determines if this `Device`
/// can be confirmed as the creator and owner of the `m.room_key`.
pub fn is_owner_of_session(&self, session: &InboundGroupSession) -> Result<bool, MegolmError> {
pub fn is_owner_of_session(
&self,
session: &InboundGroupSession,
) -> Result<bool, MismatchedIdentityKeysError> {
if session.has_been_imported() {
// An imported room key means that we did not receive the room key as a
// `m.room_key` event when the room key was initially exchanged.
Expand Down Expand Up @@ -261,14 +264,12 @@ impl Device {
match (ed25519_comparison, curve25519_comparison) {
// If we have any of the keys but they don't turn out to match, refuse to decrypt
// instead.
(_, Some(false)) | (Some(false), _) => {
Err(MegolmError::MismatchedIdentityKeys(MismatchedIdentityKeysError {
key_ed25519: key.into(),
device_ed25519: self.ed25519_key().map(Into::into),
key_curve25519: session.sender_key().into(),
device_curve25519: self.curve25519_key().map(Into::into),
}))
}
(_, Some(false)) | (Some(false), _) => Err(MismatchedIdentityKeysError {
key_ed25519: key.into(),
device_ed25519: self.ed25519_key().map(Into::into),
key_curve25519: session.sender_key().into(),
device_curve25519: self.curve25519_key().map(Into::into),
}),
// If both keys match, we have ourselves an owner.
(Some(true), Some(true)) => Ok(true),
// In the remaining cases, the device is missing at least one of the required
Expand Down

0 comments on commit 0b46a7e

Please sign in to comment.