Skip to content

Commit 0a18741

Browse files
committed
crypto: look up sender device for key bundles
1 parent cbd1a60 commit 0a18741

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

crates/matrix-sdk-crypto/src/olm/account.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,14 @@ impl Account {
15401540
}
15411541

15421542
/// Look up the [`Device`] that sent us a successfully-decrypted event.
1543+
///
1544+
/// Also validates the `sender_device_keys` field, if present.
1545+
///
1546+
/// `m.room_key` events are special-cased and return `None`: we look up
1547+
/// their devices later on.
1548+
///
1549+
/// For other events, we look up the device in the store, and return the
1550+
/// details.
15431551
async fn get_event_sender_device(
15441552
store: &Store,
15451553
sender_key: Curve25519PublicKey,
@@ -1550,13 +1558,15 @@ impl Account {
15501558
// valid. The processing of the historic room key bundle depends on this being
15511559
// here.
15521560
Self::check_sender_device_keys(event, sender_key)?;
1553-
let mut sender_device: Option<Device> = None;
15541561
if let AnyDecryptedOlmEvent::RoomKey(_) = event {
15551562
// If this event is an `m.room_key` event, defer the check for
15561563
// the Ed25519 key of the sender until we decrypt room events.
15571564
// This ensures that we receive the room key even if we don't
15581565
// have access to the device.
1559-
} else if let AnyDecryptedOlmEvent::RoomKeyBundle(_) = event {
1566+
return Ok(None);
1567+
}
1568+
1569+
if let AnyDecryptedOlmEvent::RoomKeyBundle(_) = event {
15601570
// If this is a room key bundle we're requiring the device keys to be part of
15611571
// the `AnyDecryptedOlmEvent`. This ensures that we can skip the check for the
15621572
// Ed25519 key below since `Self::check_sender_device_keys` already did so.
@@ -1566,22 +1576,24 @@ impl Account {
15661576
event.sender_device_keys().ok_or(EventError::MissingSigningKey).inspect_err(|_| {
15671577
warn!("The room key bundle was missing the sender device keys in the event")
15681578
})?;
1569-
} else {
1570-
let device = store
1571-
.get_device_from_curve_key(event.sender(), sender_key)
1572-
.await?
1573-
.ok_or(EventError::MissingSigningKey)?;
1579+
}
15741580

1575-
let key = device.ed25519_key().ok_or(EventError::MissingSigningKey)?;
1581+
let device = store
1582+
.get_device_from_curve_key(event.sender(), sender_key)
1583+
.await?
1584+
.ok_or(EventError::MissingSigningKey)?;
15761585

1577-
if key != event.keys().ed25519 {
1578-
return Err(
1579-
EventError::MismatchedKeys(key.into(), event.keys().ed25519.into()).into()
1580-
);
1581-
}
1582-
sender_device = Some(device);
1586+
let key = device.ed25519_key().ok_or(EventError::MissingSigningKey)?;
1587+
1588+
if key != event.keys().ed25519 {
1589+
return Err(EventError::MismatchedKeys(key.into(), event.keys().ed25519.into()).into());
15831590
}
1584-
Ok(sender_device)
1591+
1592+
// TODO: we may want to consider falling back to the details from
1593+
// `sender_device_keys` if the device isn't in the store, to make
1594+
// things work better when the sending device is not (yet) known.
1595+
1596+
Ok(Some(device))
15851597
}
15861598

15871599
/// Return true if:

0 commit comments

Comments
 (0)