Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: Store device_id in SenderData #3769

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions crates/matrix-sdk-crypto/src/olm/group_sessions/sender_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use ruma::{MilliSecondsSinceUnixEpoch, OwnedUserId};
use ruma::{MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedUserId};
use serde::{Deserialize, Serialize};
use vodozemac::Ed25519PublicKey;

Expand Down Expand Up @@ -66,8 +66,13 @@ pub enum SenderData {
/// The user ID of the user who established this session.
user_id: OwnedUserId,

/// The device ID of the device that send the session.
/// This is an `Option` for backwards compatibility, but we should
/// always populate it on creation.
device_id: Option<OwnedDeviceId>,
andybalaam marked this conversation as resolved.
Show resolved Hide resolved

/// The cross-signing key of the user who established this session.
master_key: Ed25519PublicKey,
master_key: Box<Ed25519PublicKey>,

/// Whether, at the time we checked the signature on the device,
/// we had actively verified that `master_key` belongs to the user.
Expand Down Expand Up @@ -201,4 +206,22 @@ mod tests {
let end: SenderData = serde_json::from_str(&json).unwrap();
assert_eq!(start, end);
}

#[test]
fn deserializing_senderknown_without_device_id_defaults_to_none() {
let json = r#"
{
"SenderKnown":{
"user_id":"@u:s.co",
"master_key":[
150,140,249,139,141,29,63,230,179,14,213,175,176,61,11,255,
26,103,10,51,100,154,183,47,181,117,87,204,33,215,241,92
],
"master_key_verified":true
}
}
"#;

let _end: SenderData = serde_json::from_str(json).expect("Failed to parse!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl<'a> SenderDataFinder<'a> {
fn device_is_cross_signed_by_sender(&self, sender_device: Device) -> SenderData {
// H (cross-signing key matches that used to sign the device!)
let user_id = sender_device.user_id().to_owned();
let device_id = Some(sender_device.device_id().to_owned());

let master_key = sender_device
.device_owner_identity
Expand All @@ -265,8 +266,9 @@ impl<'a> SenderDataFinder<'a> {

if let Some(master_key) = master_key {
// We have user_id and master_key for the user sending the to-device message.
let master_key = Box::new(master_key);
let master_key_verified = sender_device.is_cross_signing_trusted();
SenderData::SenderKnown { user_id, master_key, master_key_verified }
SenderData::SenderKnown { user_id, device_id, master_key, master_key_verified }
} else {
// Surprisingly, there was no key in the MasterPubkey. We did not expect this:
// treat it as if the device was not signed by this master key.
Expand Down Expand Up @@ -442,10 +444,12 @@ mod tests {

// Then we get back the information about the sender
assert_let!(
SenderData::SenderKnown { user_id, master_key, master_key_verified } = sender_data
SenderData::SenderKnown { user_id, device_id, master_key, master_key_verified } =
sender_data
);
assert_eq!(user_id, setup.sender.user_id);
assert_eq!(master_key, setup.sender_master_key());
assert_eq!(device_id.unwrap(), setup.sender_device.device_id());
assert_eq!(*master_key, setup.sender_master_key());
assert!(!master_key_verified);
}

Expand All @@ -469,10 +473,12 @@ mod tests {

// Then we get back the information about the sender
assert_let!(
SenderData::SenderKnown { user_id, master_key, master_key_verified } = sender_data
SenderData::SenderKnown { user_id, device_id, master_key, master_key_verified } =
sender_data
);
assert_eq!(user_id, setup.sender.user_id);
assert_eq!(master_key, setup.sender_master_key());
assert_eq!(device_id.unwrap(), setup.sender_device.device_id());
assert_eq!(*master_key, setup.sender_master_key());
assert!(!master_key_verified);
}

Expand All @@ -497,10 +503,12 @@ mod tests {

// Then we get back the information about the sender
assert_let!(
SenderData::SenderKnown { user_id, master_key, master_key_verified } = sender_data
SenderData::SenderKnown { user_id, device_id, master_key, master_key_verified } =
sender_data
);
assert_eq!(user_id, setup.sender.user_id);
assert_eq!(master_key, setup.sender_master_key());
assert_eq!(device_id.unwrap(), setup.sender_device.device_id());
assert_eq!(*master_key, setup.sender_master_key());
assert!(!master_key_verified);
}

Expand All @@ -524,10 +532,12 @@ mod tests {

// Then we get back the information about the sender
assert_let!(
SenderData::SenderKnown { user_id, master_key, master_key_verified } = sender_data
SenderData::SenderKnown { user_id, device_id, master_key, master_key_verified } =
sender_data
);
assert_eq!(user_id, setup.sender.user_id);
assert_eq!(master_key, setup.sender_master_key());
assert_eq!(device_id.unwrap(), setup.sender_device.device_id());
assert_eq!(*master_key, setup.sender_master_key());
assert!(!master_key_verified);
}

Expand Down Expand Up @@ -590,10 +600,12 @@ mod tests {

// Then we get back the information about the sender
assert_let!(
SenderData::SenderKnown { user_id, master_key, master_key_verified } = sender_data
SenderData::SenderKnown { user_id, device_id, master_key, master_key_verified } =
sender_data
);
assert_eq!(user_id, setup.sender.user_id);
assert_eq!(master_key, setup.sender_master_key());
assert_eq!(device_id.unwrap(), setup.sender_device.device_id());
assert_eq!(*master_key, setup.sender_master_key());
// Including the fact that it was verified
assert!(master_key_verified);
}
Expand Down Expand Up @@ -621,10 +633,12 @@ mod tests {

// Then we get back the information about the sender
assert_let!(
SenderData::SenderKnown { user_id, master_key, master_key_verified } = sender_data
SenderData::SenderKnown { user_id, device_id, master_key, master_key_verified } =
sender_data
);
assert_eq!(user_id, setup.sender.user_id);
assert_eq!(master_key, setup.sender_master_key());
assert_eq!(device_id.unwrap(), setup.sender_device.device_id());
assert_eq!(*master_key, setup.sender_master_key());
// Including the fact that it was verified
assert!(master_key_verified);
}
Expand All @@ -643,10 +657,12 @@ mod tests {

// Then it is found using the device we supplied
assert_let!(
SenderData::SenderKnown { user_id, master_key, master_key_verified } = sender_data
SenderData::SenderKnown { user_id, device_id, master_key, master_key_verified } =
sender_data
);
assert_eq!(user_id, setup.sender.user_id);
assert_eq!(master_key, setup.sender_master_key());
assert_eq!(device_id.unwrap(), setup.sender_device.device_id());
assert_eq!(*master_key, setup.sender_master_key());
assert!(!master_key_verified);
}

Expand Down
Loading