matrix_sdk_crypto/olm/group_sessions/
sender_data.rs

1// Copyright 2024 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{cmp::Ordering, fmt};
16
17use ruma::{DeviceId, OwnedDeviceId, OwnedUserId, UserId};
18use serde::{de, de::Visitor, Deserialize, Deserializer, Serialize};
19use tracing::error;
20use vodozemac::Ed25519PublicKey;
21
22use crate::{
23    types::{serialize_ed25519_key, DeviceKeys},
24    Device,
25};
26
27/// Information about the sender of a megolm session where we know the
28/// cross-signing identity of the sender.
29#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
30pub struct KnownSenderData {
31    /// The user ID of the user who established this session.
32    pub user_id: OwnedUserId,
33
34    /// The device ID of the device that send the session.
35    /// This is an `Option` for backwards compatibility, but we should always
36    /// populate it on creation.
37    pub device_id: Option<OwnedDeviceId>,
38
39    /// The cross-signing key of the user who established this session.
40    #[serde(
41        serialize_with = "serialize_ed25519_key",
42        deserialize_with = "deserialize_sender_msk_base64_or_array"
43    )]
44    pub master_key: Box<Ed25519PublicKey>,
45}
46
47/// In an initial version the master key was serialized as an array of number,
48/// it is now exported in base64. This code adds backward compatibility.
49pub(crate) fn deserialize_sender_msk_base64_or_array<'de, D>(
50    de: D,
51) -> Result<Box<Ed25519PublicKey>, D::Error>
52where
53    D: Deserializer<'de>,
54{
55    struct KeyVisitor;
56
57    impl<'de> Visitor<'de> for KeyVisitor {
58        type Value = Box<Ed25519PublicKey>;
59
60        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
61            write!(formatter, "a base64 string or an array of 32 bytes")
62        }
63
64        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
65        where
66            E: de::Error,
67        {
68            let decoded = Ed25519PublicKey::from_base64(v)
69                .map_err(|_| de::Error::custom("Base64 decoding error"))?;
70            Ok(Box::new(decoded))
71        }
72
73        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
74        where
75            A: de::SeqAccess<'de>,
76        {
77            let mut buf = [0u8; Ed25519PublicKey::LENGTH];
78
79            for (i, item) in buf.iter_mut().enumerate() {
80                *item = seq.next_element()?.ok_or_else(|| de::Error::invalid_length(i, &self))?;
81            }
82
83            let key = Ed25519PublicKey::from_slice(&buf).map_err(|e| de::Error::custom(&e))?;
84
85            Ok(Box::new(key))
86        }
87
88        fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
89        where
90            E: de::Error,
91        {
92            if v.len() == Ed25519PublicKey::LENGTH {
93                let mut buf = [0u8; Ed25519PublicKey::LENGTH];
94                buf.copy_from_slice(v);
95
96                let key = Ed25519PublicKey::from_slice(&buf).map_err(|e| de::Error::custom(&e))?;
97                Ok(Box::new(key))
98            } else {
99                Err(de::Error::invalid_length(v.len(), &self))
100            }
101        }
102    }
103
104    de.deserialize_any(KeyVisitor)
105}
106
107/// Information on the device and user that sent the megolm session data to us
108///
109/// Sessions start off in `UnknownDevice` state, and progress into `DeviceInfo`
110/// state when we get the device info. Finally, if we can look up the sender
111/// using the device info, the session can be moved into
112/// `VerificationViolation`, `SenderUnverified`, or
113/// `SenderVerified` state, depending on the verification status of the user.
114/// If the user's verification state changes, the state may change accordingly.
115#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
116#[serde(from = "SenderDataReader")]
117pub enum SenderData {
118    /// We have not yet found the (signed) device info for the sending device,
119    /// or we did find a device but it does not own the session.
120    UnknownDevice {
121        /// Was this session created before we started collecting trust
122        /// information about sessions? If so, we may choose to display its
123        /// messages even though trust info is missing.
124        legacy_session: bool,
125
126        /// If true, we found the device but it was not the owner of the
127        /// session. If false, we could not find the device.
128        #[serde(skip_serializing_if = "std::ops::Not::not")]
129        #[serde(default)]
130        owner_check_failed: bool,
131    },
132
133    /// We have the signed device info for the sending device, but not yet the
134    /// cross-signing key that it was signed with.
135    DeviceInfo {
136        /// Information about the device that sent the to-device message
137        /// creating this session.
138        device_keys: DeviceKeys,
139
140        /// Was this session created before we started collecting trust
141        /// information about sessions? If so, we may choose to display its
142        /// messages even though trust info is missing.
143        legacy_session: bool,
144    },
145
146    /// We have found proof that this user, with this cross-signing key, sent
147    /// the to-device message that established this session, but we have not yet
148    /// verified the cross-signing key, and we had verified a previous
149    /// cross-signing key for this user.
150    VerificationViolation(KnownSenderData),
151
152    /// We have found proof that this user, with this cross-signing key, sent
153    /// the to-device message that established this session, but we have not yet
154    /// verified the cross-signing key.
155    SenderUnverified(KnownSenderData),
156
157    /// We have found proof that this user, with this cross-signing key, sent
158    /// the to-device message that established this session, and we have
159    /// verified the cross-signing key.
160    SenderVerified(KnownSenderData),
161}
162
163impl SenderData {
164    /// Create a [`SenderData`] which contains no device info.
165    pub fn unknown() -> Self {
166        Self::UnknownDevice { legacy_session: false, owner_check_failed: false }
167    }
168
169    /// Create a [`SenderData`] which contains device info.
170    pub fn device_info(device_keys: DeviceKeys) -> Self {
171        Self::DeviceInfo { device_keys, legacy_session: false }
172    }
173
174    /// Create a [`SenderData`] with a known but unverified sender, where the
175    /// sender was previously verified.
176    pub fn sender_verification_violation(
177        user_id: &UserId,
178        device_id: &DeviceId,
179        master_key: Ed25519PublicKey,
180    ) -> Self {
181        Self::VerificationViolation(KnownSenderData {
182            user_id: user_id.to_owned(),
183            device_id: Some(device_id.to_owned()),
184            master_key: Box::new(master_key),
185        })
186    }
187
188    /// Create a [`SenderData`] with a known but unverified sender.
189    pub fn sender_unverified(
190        user_id: &UserId,
191        device_id: &DeviceId,
192        master_key: Ed25519PublicKey,
193    ) -> Self {
194        Self::SenderUnverified(KnownSenderData {
195            user_id: user_id.to_owned(),
196            device_id: Some(device_id.to_owned()),
197            master_key: Box::new(master_key),
198        })
199    }
200
201    /// Create a [`SenderData`] with a verified sender.
202    pub fn sender_verified(
203        user_id: &UserId,
204        device_id: &DeviceId,
205        master_key: Ed25519PublicKey,
206    ) -> Self {
207        Self::SenderVerified(KnownSenderData {
208            user_id: user_id.to_owned(),
209            device_id: Some(device_id.to_owned()),
210            master_key: Box::new(master_key),
211        })
212    }
213
214    /// Create a [`SenderData`] which has the legacy flag set. Caution: messages
215    /// within sessions with this flag will be displayed in some contexts,
216    /// even when we are unable to verify the sender.
217    ///
218    /// The returned struct contains no device info.
219    pub fn legacy() -> Self {
220        Self::UnknownDevice { legacy_session: true, owner_check_failed: false }
221    }
222
223    /// Create a [`SenderData`] representing the current verification state of
224    /// the given device.
225    ///
226    /// Depending on whether the device is correctly cross-signed or not, and
227    /// whether the user has been verified or not, this can return
228    /// [`SenderData::DeviceInfo`], [`SenderData::VerificationViolation`],
229    /// [`SenderData::SenderUnverified`] or [`SenderData::SenderVerified`]
230    pub fn from_device(sender_device: &Device) -> Self {
231        // Is the device cross-signed?
232        // Does the cross-signing key match that used to sign the device?
233        // And is the signature in the device valid?
234        let cross_signed = sender_device.is_cross_signed_by_owner();
235
236        if cross_signed {
237            Self::from_cross_signed_device(sender_device)
238        } else {
239            // We have device keys, but they are not signed by the sender
240            SenderData::device_info(sender_device.as_device_keys().clone())
241        }
242    }
243
244    fn from_cross_signed_device(sender_device: &Device) -> Self {
245        let user_id = sender_device.user_id().to_owned();
246        let device_id = Some(sender_device.device_id().to_owned());
247
248        let device_owner = sender_device.device_owner_identity.as_ref();
249        let master_key = device_owner.and_then(|i| i.master_key().get_first_key());
250
251        match (device_owner, master_key) {
252            (Some(device_owner), Some(master_key)) => {
253                // We have user_id and master_key for the user sending the to-device message.
254                let master_key = Box::new(master_key);
255                let known_sender_data = KnownSenderData { user_id, device_id, master_key };
256                if sender_device.is_cross_signing_trusted() {
257                    Self::SenderVerified(known_sender_data)
258                } else if device_owner.was_previously_verified() {
259                    Self::VerificationViolation(known_sender_data)
260                } else {
261                    Self::SenderUnverified(known_sender_data)
262                }
263            }
264
265            (_, _) => {
266                // Surprisingly, there was no key in the MasterPubkey. We did not expect this:
267                // treat it as if the device was not signed by this master key.
268                //
269                error!("MasterPubkey for user {user_id} does not contain any keys!");
270                Self::device_info(sender_device.as_device_keys().clone())
271            }
272        }
273    }
274
275    /// Returns `Greater` if this `SenderData` represents a greater level of
276    /// trust than the supplied one, `Equal` if they have the same level, and
277    /// `Less` if the supplied one has a greater level of trust.
278    ///
279    /// So calling this method on a `SenderKnown` or `DeviceInfo` `SenderData`
280    /// would return `Greater` if passed an `UnknownDevice` as its
281    /// argument, and a `SenderKnown` with `master_key_verified == true`
282    /// would return `Greater` if passed a `SenderKnown` with
283    /// `master_key_verified == false`.
284    pub(crate) fn compare_trust_level(&self, other: &Self) -> Ordering {
285        self.trust_number().cmp(&other.trust_number())
286    }
287
288    /// Internal function to give a numeric value of how much trust this
289    /// `SenderData` represents. Used to make the implementation of
290    /// compare_trust_level simpler.
291    fn trust_number(&self) -> u8 {
292        match self {
293            SenderData::UnknownDevice { .. } => 0,
294            SenderData::DeviceInfo { .. } => 1,
295            SenderData::VerificationViolation(..) => 2,
296            SenderData::SenderUnverified(..) => 3,
297            SenderData::SenderVerified(..) => 4,
298        }
299    }
300
301    /// Return our type as a [`SenderDataType`].
302    pub fn to_type(&self) -> SenderDataType {
303        match self {
304            Self::UnknownDevice { .. } => SenderDataType::UnknownDevice,
305            Self::DeviceInfo { .. } => SenderDataType::DeviceInfo,
306            Self::VerificationViolation { .. } => SenderDataType::VerificationViolation,
307            Self::SenderUnverified { .. } => SenderDataType::SenderUnverified,
308            Self::SenderVerified { .. } => SenderDataType::SenderVerified,
309        }
310    }
311}
312
313/// Used when deserialising and the sender_data property is missing.
314/// If we are deserialising an InboundGroupSession session with missing
315/// sender_data, this must be a legacy session (i.e. it was created before we
316/// started tracking sender data). We set its legacy flag to true, so we can
317/// populate it with trust information if it is available later.
318impl Default for SenderData {
319    fn default() -> Self {
320        Self::legacy()
321    }
322}
323
324/// Deserialisation type, to handle conversion from older formats
325#[derive(Deserialize)]
326enum SenderDataReader {
327    UnknownDevice {
328        legacy_session: bool,
329        #[serde(default)]
330        owner_check_failed: bool,
331    },
332
333    DeviceInfo {
334        device_keys: DeviceKeys,
335        legacy_session: bool,
336    },
337
338    #[serde(alias = "SenderUnverifiedButPreviouslyVerified")]
339    VerificationViolation(KnownSenderData),
340
341    SenderUnverified(KnownSenderData),
342
343    SenderVerified(KnownSenderData),
344
345    // If we read this older variant, it gets changed to SenderUnverified or
346    // SenderVerified, depending on the master_key_verified flag.
347    SenderKnown {
348        user_id: OwnedUserId,
349        device_id: Option<OwnedDeviceId>,
350        master_key: Box<Ed25519PublicKey>,
351        master_key_verified: bool,
352    },
353}
354
355impl From<SenderDataReader> for SenderData {
356    fn from(data: SenderDataReader) -> Self {
357        match data {
358            SenderDataReader::UnknownDevice { legacy_session, owner_check_failed } => {
359                Self::UnknownDevice { legacy_session, owner_check_failed }
360            }
361            SenderDataReader::DeviceInfo { device_keys, legacy_session } => {
362                Self::DeviceInfo { device_keys, legacy_session }
363            }
364            SenderDataReader::VerificationViolation(data) => Self::VerificationViolation(data),
365            SenderDataReader::SenderUnverified(data) => Self::SenderUnverified(data),
366            SenderDataReader::SenderVerified(data) => Self::SenderVerified(data),
367            SenderDataReader::SenderKnown {
368                user_id,
369                device_id,
370                master_key,
371                master_key_verified,
372            } => {
373                let known_sender_data = KnownSenderData { user_id, device_id, master_key };
374                if master_key_verified {
375                    Self::SenderVerified(known_sender_data)
376                } else {
377                    Self::SenderUnverified(known_sender_data)
378                }
379            }
380        }
381    }
382}
383
384/// Used when serializing [`crate::olm::group_sessions::InboundGroupSession`]s.
385/// We want just the type of the session's [`SenderData`] to be queryable, so we
386/// store the type as a separate column/property in the database.
387#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
388pub enum SenderDataType {
389    /// The [`SenderData`] is of type `UnknownDevice`.
390    UnknownDevice = 1,
391    /// The [`SenderData`] is of type `DeviceInfo`.
392    DeviceInfo = 2,
393    /// The [`SenderData`] is of type `VerificationViolation`.
394    VerificationViolation = 3,
395    /// The [`SenderData`] is of type `SenderUnverified`.
396    SenderUnverified = 4,
397    /// The [`SenderData`] is of type `SenderVerified`.
398    SenderVerified = 5,
399}
400
401#[cfg(test)]
402mod tests {
403    use std::{cmp::Ordering, collections::BTreeMap, ops::Deref};
404
405    use assert_matches2::assert_let;
406    use insta::assert_json_snapshot;
407    use matrix_sdk_test::async_test;
408    use ruma::{
409        device_id, owned_device_id, owned_user_id, user_id, DeviceKeyAlgorithm, DeviceKeyId,
410    };
411    use serde_json::json;
412    use vodozemac::{base64_decode, Curve25519PublicKey, Ed25519PublicKey};
413
414    use super::SenderData;
415    use crate::{
416        machine::test_helpers::{
417            create_signed_device_of_unverified_user, create_signed_device_of_verified_user,
418            create_unsigned_device,
419        },
420        olm::{KnownSenderData, PickledInboundGroupSession, PrivateCrossSigningIdentity},
421        types::{DeviceKey, DeviceKeys, EventEncryptionAlgorithm, Signatures},
422        Account,
423    };
424
425    #[test]
426    fn serializing_unknown_device_correctly_preserves_owner_check_failed_if_true() {
427        // Given an unknown device SenderData with failed owner check
428        let start = SenderData::UnknownDevice { legacy_session: false, owner_check_failed: true };
429
430        // When we round-trip it to JSON and back
431        let json = serde_json::to_string(&start).unwrap();
432        let end: SenderData = serde_json::from_str(&json).unwrap();
433
434        // Then the failed owner check flag is preserved
435        assert_let!(SenderData::UnknownDevice { owner_check_failed, .. } = &end);
436        assert!(owner_check_failed);
437
438        // And for good measure, everything is preserved
439        assert_eq!(start, end);
440    }
441
442    #[test]
443    fn serializing_unknown_device_without_failed_owner_check_excludes_it() {
444        // Given an unknown device SenderData with owner_check_failed==false
445        let start = SenderData::UnknownDevice { legacy_session: false, owner_check_failed: false };
446
447        // When we write it to JSON
448        let json = serde_json::to_string(&start).unwrap();
449
450        // Then the JSON does not mention `owner_check_failed`
451        assert!(!json.contains("owner_check_failed"), "JSON contains 'owner_check_failed'!");
452
453        // And for good measure, it round-trips fully
454        let end: SenderData = serde_json::from_str(&json).unwrap();
455        assert_eq!(start, end);
456    }
457
458    #[test]
459    fn deserializing_unknown_device_with_extra_retry_info_ignores_it() {
460        // Previously, SenderData contained `retry_details` but it is no longer needed -
461        // just check that we are able to deserialize even if it is present.
462        let json = r#"
463            {
464                "UnknownDevice":{
465                    "retry_details":{
466                        "retry_count":3,
467                        "next_retry_time_ms":10000
468                    },
469                    "legacy_session":false
470                }
471            }
472            "#;
473
474        let end: SenderData = serde_json::from_str(json).expect("Failed to parse!");
475        assert_let!(SenderData::UnknownDevice { .. } = end);
476    }
477
478    #[test]
479    fn deserializing_senderknown_without_device_id_defaults_to_none() {
480        let json = r#"
481            {
482                "SenderKnown":{
483                    "user_id":"@u:s.co",
484                    "master_key":[
485                        150,140,249,139,141,29,63,230,179,14,213,175,176,61,11,255,
486                        26,103,10,51,100,154,183,47,181,117,87,204,33,215,241,92
487                    ],
488                    "master_key_verified":true
489                }
490            }
491            "#;
492
493        let end: SenderData = serde_json::from_str(json).expect("Failed to parse!");
494        assert_let!(SenderData::SenderVerified { .. } = end);
495    }
496
497    #[test]
498    fn deserializing_sender_unverified_but_previously_verified_migrates_to_verification_violation()
499    {
500        let json = r#"
501            {
502                "SenderUnverifiedButPreviouslyVerified":{
503                    "user_id":"@u:s.co",
504                    "master_key":[
505                        150,140,249,139,141,29,63,230,179,14,213,175,176,61,11,255,
506                        26,103,10,51,100,154,183,47,181,117,87,204,33,215,241,92
507                    ],
508                    "master_key_verified":true
509                }
510            }
511            "#;
512
513        let end: SenderData = serde_json::from_str(json).expect("Failed to parse!");
514        assert_let!(SenderData::VerificationViolation(KnownSenderData { user_id, .. }) = end);
515        assert_eq!(user_id, owned_user_id!("@u:s.co"));
516    }
517
518    #[test]
519    fn deserializing_verification_violation() {
520        let json = r#"
521            {
522                "VerificationViolation":{
523                    "user_id":"@u:s.co",
524                    "master_key":[
525                        150,140,249,139,141,29,63,230,179,14,213,175,176,61,11,255,
526                        26,103,10,51,100,154,183,47,181,117,87,204,33,215,241,92
527                    ],
528                    "master_key_verified":true
529                }
530            }
531            "#;
532
533        let end: SenderData = serde_json::from_str(json).expect("Failed to parse!");
534        assert_let!(SenderData::VerificationViolation(KnownSenderData { user_id, .. }) = end);
535        assert_eq!(user_id, owned_user_id!("@u:s.co"));
536    }
537
538    #[test]
539    fn equal_sessions_have_same_trust_level() {
540        let unknown = SenderData::unknown();
541        let device_keys = SenderData::device_info(DeviceKeys::new(
542            owned_user_id!("@u:s.co"),
543            owned_device_id!("DEV"),
544            Vec::new(),
545            BTreeMap::new(),
546            Signatures::new(),
547        ));
548        let master_key =
549            Ed25519PublicKey::from_base64("2/5LWJMow5zhJqakV88SIc7q/1pa8fmkfgAzx72w9G4").unwrap();
550        let sender_unverified =
551            SenderData::sender_unverified(user_id!("@u:s.co"), device_id!("DEV"), master_key);
552        let sender_verified =
553            SenderData::sender_verified(user_id!("@u:s.co"), device_id!("DEV"), master_key);
554
555        assert_eq!(unknown.compare_trust_level(&unknown), Ordering::Equal);
556        assert_eq!(device_keys.compare_trust_level(&device_keys), Ordering::Equal);
557        assert_eq!(sender_unverified.compare_trust_level(&sender_unverified), Ordering::Equal);
558        assert_eq!(sender_verified.compare_trust_level(&sender_verified), Ordering::Equal);
559    }
560
561    #[test]
562    fn more_trust_data_makes_you_more_trusted() {
563        let unknown = SenderData::unknown();
564        let device_keys = SenderData::device_info(DeviceKeys::new(
565            owned_user_id!("@u:s.co"),
566            owned_device_id!("DEV"),
567            Vec::new(),
568            BTreeMap::new(),
569            Signatures::new(),
570        ));
571        let master_key =
572            Ed25519PublicKey::from_base64("2/5LWJMow5zhJqakV88SIc7q/1pa8fmkfgAzx72w9G4").unwrap();
573        let sender_verification_violation = SenderData::sender_verification_violation(
574            user_id!("@u:s.co"),
575            device_id!("DEV"),
576            master_key,
577        );
578        let sender_unverified =
579            SenderData::sender_unverified(user_id!("@u:s.co"), device_id!("DEV"), master_key);
580        let sender_verified =
581            SenderData::sender_verified(user_id!("@u:s.co"), device_id!("DEV"), master_key);
582
583        assert_eq!(unknown.compare_trust_level(&device_keys), Ordering::Less);
584        assert_eq!(unknown.compare_trust_level(&sender_verification_violation), Ordering::Less);
585        assert_eq!(unknown.compare_trust_level(&sender_unverified), Ordering::Less);
586        assert_eq!(unknown.compare_trust_level(&sender_verified), Ordering::Less);
587        assert_eq!(device_keys.compare_trust_level(&unknown), Ordering::Greater);
588        assert_eq!(sender_verification_violation.compare_trust_level(&unknown), Ordering::Greater);
589        assert_eq!(sender_unverified.compare_trust_level(&unknown), Ordering::Greater);
590        assert_eq!(sender_verified.compare_trust_level(&unknown), Ordering::Greater);
591
592        assert_eq!(device_keys.compare_trust_level(&sender_unverified), Ordering::Less);
593        assert_eq!(device_keys.compare_trust_level(&sender_verified), Ordering::Less);
594        assert_eq!(
595            sender_verification_violation.compare_trust_level(&device_keys),
596            Ordering::Greater
597        );
598        assert_eq!(sender_unverified.compare_trust_level(&device_keys), Ordering::Greater);
599        assert_eq!(sender_verified.compare_trust_level(&device_keys), Ordering::Greater);
600
601        assert_eq!(
602            sender_verification_violation.compare_trust_level(&sender_verified),
603            Ordering::Less
604        );
605        assert_eq!(
606            sender_verification_violation.compare_trust_level(&sender_unverified),
607            Ordering::Less
608        );
609        assert_eq!(sender_unverified.compare_trust_level(&sender_verified), Ordering::Less);
610        assert_eq!(sender_verified.compare_trust_level(&sender_unverified), Ordering::Greater);
611    }
612
613    #[test]
614    fn snapshot_sender_data() {
615        assert_json_snapshot!(SenderData::UnknownDevice {
616            legacy_session: false,
617            owner_check_failed: true,
618        });
619
620        assert_json_snapshot!(SenderData::UnknownDevice {
621            legacy_session: true,
622            owner_check_failed: false,
623        });
624
625        assert_json_snapshot!(SenderData::DeviceInfo {
626            device_keys: DeviceKeys::new(
627                owned_user_id!("@foo:bar.baz"),
628                owned_device_id!("DEV"),
629                vec![
630                    EventEncryptionAlgorithm::MegolmV1AesSha2,
631                    EventEncryptionAlgorithm::OlmV1Curve25519AesSha2
632                ],
633                BTreeMap::from_iter(vec![(
634                    DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, device_id!("ABCDEFGH")),
635                    DeviceKey::Curve25519(Curve25519PublicKey::from_bytes([0u8; 32])),
636                )]),
637                Default::default(),
638            ),
639            legacy_session: false,
640        });
641
642        assert_json_snapshot!(SenderData::VerificationViolation(KnownSenderData {
643            user_id: owned_user_id!("@foo:bar.baz"),
644            device_id: Some(owned_device_id!("DEV")),
645            master_key: Box::new(Ed25519PublicKey::from_slice(&[0u8; 32]).unwrap()),
646        }));
647
648        assert_json_snapshot!(SenderData::SenderUnverified(KnownSenderData {
649            user_id: owned_user_id!("@foo:bar.baz"),
650            device_id: None,
651            master_key: Box::new(Ed25519PublicKey::from_slice(&[1u8; 32]).unwrap()),
652        }));
653
654        assert_json_snapshot!(SenderData::SenderVerified(KnownSenderData {
655            user_id: owned_user_id!("@foo:bar.baz"),
656            device_id: None,
657            master_key: Box::new(Ed25519PublicKey::from_slice(&[1u8; 32]).unwrap()),
658        }));
659    }
660
661    #[test]
662    fn test_sender_known_data_migration() {
663        let old_format = json!(
664        {
665            "SenderVerified": {
666                "user_id": "@foo:bar.baz",
667                "device_id": null,
668                "master_key": [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
669            }
670        });
671
672        let migrated: SenderData = serde_json::from_value(old_format).unwrap();
673
674        assert_let!(SenderData::SenderVerified(KnownSenderData { master_key, .. }) = migrated);
675
676        assert_eq!(
677            master_key.to_base64(),
678            Ed25519PublicKey::from_slice(&[0u8; 32]).unwrap().to_base64()
679        );
680    }
681
682    #[test]
683    fn test_sender_known_data_migration_with_efficient_bytes_array() {
684        // This is an serialized PickledInboundGroupSession as rmp_serde will generate.
685        //
686        // This export usse a more efficient serialization format for bytes. This was
687        // exported when the `KnownSenderData` master_key was serialized as an byte
688        // array instead of a base64 encoded string.
689        const SERIALIZED_B64: &str =
690            "iaZwaWNrbGWEr2luaXRpYWxfcmF0Y2hldIKlaW5uZXLcAIABYMzfSnBRzMlPKF1uKjYbzLtkzNJ4RcylzN0HzP\
691             9DzON1Tm05zO7M2MzFQsy9Acz9zPnMqDvM4syQzNrMzxF5KzbM4sy9zPUbBWfM7m4/zJzM18zDzMESKgfMkE7M\
692             yszIHszqWjYyQURbzKTMkx7M58zANsy+AGPM2A8tbcyFYczge8ykzMFdbVxJMMyAzN8azJEXGsy8zPJazMMaP8\
693             ziDszmWwfM+My2ajLMr8y+eczTRm9TFadjb3VudGVyAKtzaWduaW5nX2tlecQgefpCr6Duu7QUWzKIeMOFmxv/\
694             NjfcsYwZz8IN2ZOhdaS0c2lnbmluZ19rZXlfdmVyaWZpZWTDpmNvbmZpZ4GndmVyc2lvbqJWMapzZW5kZXJfa2\
695             V52StoMkIySDg2ajFpYmk2SW13ak9UUkhzbTVMamtyT2kyUGtiSXVUb0w0TWtFq3NpZ25pbmdfa2V5gadlZDI1\
696             NTE52StUWHJqNS9UYXpia3Yram1CZDl4UlB4NWNVaFFzNUNnblc1Q1pNRjgvNjZzq3NlbmRlcl9kYXRhgbBTZW\
697             5kZXJVbnZlcmlmaWVkg6d1c2VyX2lks0B2YWxvdTM1Om1hdHJpeC5vcmepZGV2aWNlX2lkqkZJQlNaRlJLUE2q\
698             bWFzdGVyX2tlecQgkOp9s4ClyQujYD7rRZA8xgE6kvYlqKSNnMrQNmSrcuGncm9vbV9pZL4hRWt5VEtGdkViYl\
699             B6SmxhaUhFOm1hdHJpeC5vcmeoaW1wb3J0ZWTCqWJhY2tlZF91cMKyaGlzdG9yeV92aXNpYmlsaXR5wKlhbGdv\
700             cml0aG20bS5tZWdvbG0udjEuYWVzLXNoYTI";
701
702        let input = base64_decode(SERIALIZED_B64).unwrap();
703        let sender_data: PickledInboundGroupSession = rmp_serde::from_slice(&input)
704            .expect("Should be able to deserialize serialized inbound group session");
705
706        assert_let!(
707            SenderData::SenderUnverified(KnownSenderData { master_key, .. }) =
708                sender_data.sender_data
709        );
710
711        assert_eq!(master_key.to_base64(), "kOp9s4ClyQujYD7rRZA8xgE6kvYlqKSNnMrQNmSrcuE");
712    }
713
714    #[async_test]
715    async fn test_from_device_for_unsigned_device() {
716        let bob_account =
717            Account::with_device_id(user_id!("@bob:example.com"), device_id!("BOB_DEVICE"));
718        let bob_device = create_unsigned_device(bob_account.device_keys());
719
720        let sender_data = SenderData::from_device(&bob_device);
721
722        assert_eq!(
723            sender_data,
724            SenderData::DeviceInfo {
725                device_keys: bob_device.device_keys.deref().clone(),
726                legacy_session: false
727            }
728        );
729    }
730
731    #[async_test]
732    async fn test_from_device_for_unverified_user() {
733        let bob_identity =
734            PrivateCrossSigningIdentity::new(user_id!("@bob:example.com").to_owned());
735        let bob_account =
736            Account::with_device_id(user_id!("@bob:example.com"), device_id!("BOB_DEVICE"));
737        let bob_device = create_signed_device_of_unverified_user(
738            bob_account.device_keys().clone(),
739            &bob_identity,
740        )
741        .await;
742
743        let sender_data = SenderData::from_device(&bob_device);
744
745        assert_eq!(
746            sender_data,
747            SenderData::SenderUnverified(KnownSenderData {
748                user_id: bob_account.user_id().to_owned(),
749                device_id: Some(bob_account.device_id().to_owned()),
750                master_key: Box::new(
751                    bob_identity.master_public_key().await.unwrap().get_first_key().unwrap()
752                ),
753            })
754        );
755    }
756
757    #[async_test]
758    async fn test_from_device_for_verified_user() {
759        let alice_account =
760            Account::with_device_id(user_id!("@alice:example.com"), device_id!("ALICE_DEVICE"));
761        let alice_identity = PrivateCrossSigningIdentity::with_account(&alice_account).await.0;
762
763        let bob_identity =
764            PrivateCrossSigningIdentity::new(user_id!("@bob:example.com").to_owned());
765        let bob_account =
766            Account::with_device_id(user_id!("@bob:example.com"), device_id!("BOB_DEVICE"));
767        let bob_device = create_signed_device_of_verified_user(
768            bob_account.device_keys().clone(),
769            &bob_identity,
770            &alice_identity,
771        )
772        .await;
773
774        let sender_data = SenderData::from_device(&bob_device);
775
776        assert_eq!(
777            sender_data,
778            SenderData::SenderVerified(KnownSenderData {
779                user_id: bob_account.user_id().to_owned(),
780                device_id: Some(bob_account.device_id().to_owned()),
781                master_key: Box::new(
782                    bob_identity.master_public_key().await.unwrap().get_first_key().unwrap()
783                ),
784            })
785        );
786    }
787
788    #[async_test]
789    async fn test_from_device_for_verification_violation_user() {
790        let bob_identity =
791            PrivateCrossSigningIdentity::new(user_id!("@bob:example.com").to_owned());
792        let bob_account =
793            Account::with_device_id(user_id!("@bob:example.com"), device_id!("BOB_DEVICE"));
794        let bob_device =
795            create_signed_device_of_unverified_user(bob_account.device_keys(), &bob_identity).await;
796        bob_device
797            .device_owner_identity
798            .as_ref()
799            .unwrap()
800            .other()
801            .unwrap()
802            .mark_as_previously_verified();
803
804        let sender_data = SenderData::from_device(&bob_device);
805
806        assert_eq!(
807            sender_data,
808            SenderData::VerificationViolation(KnownSenderData {
809                user_id: bob_account.user_id().to_owned(),
810                device_id: Some(bob_account.device_id().to_owned()),
811                master_key: Box::new(
812                    bob_identity.master_public_key().await.unwrap().get_first_key().unwrap()
813                ),
814            })
815        );
816    }
817}