Skip to main content

matrix_sdk_crypto/identities/
device.rs

1// Copyright 2020 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::{
16    collections::{BTreeMap, HashMap},
17    ops::Deref,
18    sync::{
19        Arc,
20        atomic::{AtomicBool, Ordering},
21    },
22};
23
24use matrix_sdk_common::locks::RwLock;
25use ruma::{
26    DeviceId, DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, OwnedDeviceId,
27    OwnedDeviceKeyId, UInt, UserId,
28    api::client::keys::upload_signatures::v3::Request as SignatureUploadRequest,
29    events::{AnyToDeviceEventContent, key::verification::VerificationMethod},
30    serde::Raw,
31};
32use serde::{Deserialize, Serialize};
33use serde_json::Value;
34use tracing::{instrument, trace};
35use vodozemac::{Curve25519PublicKey, Ed25519PublicKey, olm::SessionConfig};
36
37use super::{atomic_bool_deserializer, atomic_bool_serializer};
38#[cfg(any(test, feature = "testing", doc))]
39use crate::OlmMachine;
40use crate::{
41    Account, Sas, VerificationRequest,
42    error::{MismatchedIdentityKeysError, OlmError, OlmResult, SignatureError},
43    identities::{OwnUserIdentityData, UserIdentityData},
44    olm::{InboundGroupSession, OutboundGroupSession, Session, ShareInfo, VerifyJson},
45    session_manager::{CollectStrategy, withheld_code_for_device_for_share_strategy},
46    store::{
47        CryptoStoreWrapper, Result as StoreResult,
48        caches::SequenceNumber,
49        types::{Changes, DeviceChanges},
50    },
51    types::{
52        DeviceKey, DeviceKeys, EventEncryptionAlgorithm, Signatures, SignedKey,
53        events::{
54            EventType, forwarded_room_key::ForwardedRoomKeyContent,
55            room::encrypted::ToDeviceEncryptedEventContent,
56        },
57        requests::{OutgoingVerificationRequest, ToDeviceRequest},
58    },
59    verification::VerificationMachine,
60};
61
62pub enum MaybeEncryptedRoomKey {
63    Encrypted {
64        // `Box` the session to reduce the size of `Encrypted`.
65        used_session: Box<Session>,
66        // `Box` the session to reduce the size of `Encrypted`.
67        share_info: Box<ShareInfo>,
68        message: Raw<AnyToDeviceEventContent>,
69    },
70    /// We could not encrypt a message to this device because there is no active
71    /// Olm session.
72    MissingSession,
73}
74
75/// A read-only version of a `Device`.
76#[derive(Clone, Serialize, Deserialize)]
77pub struct DeviceData {
78    #[serde(alias = "inner")]
79    pub(crate) device_keys: Arc<DeviceKeys>,
80    #[serde(
81        serialize_with = "atomic_bool_serializer",
82        deserialize_with = "atomic_bool_deserializer"
83    )]
84    deleted: Arc<AtomicBool>,
85    trust_state: Arc<RwLock<LocalTrust>>,
86    /// Flag remembering if we successfully sent an `m.no_olm` withheld code to
87    /// this device.
88    #[serde(
89        default,
90        serialize_with = "atomic_bool_serializer",
91        deserialize_with = "atomic_bool_deserializer"
92    )]
93    withheld_code_sent: Arc<AtomicBool>,
94    /// First time this device was seen in milliseconds since epoch.
95    /// Default to epoch for migration purpose.
96    #[serde(default = "default_timestamp")]
97    first_time_seen_ts: MilliSecondsSinceUnixEpoch,
98    /// The number of times the device has tried to unwedge Olm sessions with
99    /// us.
100    #[serde(default)]
101    pub(crate) olm_wedging_index: SequenceNumber,
102}
103
104fn default_timestamp() -> MilliSecondsSinceUnixEpoch {
105    MilliSecondsSinceUnixEpoch(UInt::default())
106}
107
108#[cfg(not(tarpaulin_include))]
109impl std::fmt::Debug for DeviceData {
110    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111        f.debug_struct("DeviceData")
112            .field("user_id", &self.user_id())
113            .field("device_id", &self.device_id())
114            .field("display_name", &self.display_name())
115            .field("keys", self.keys())
116            .field("deleted", &self.deleted.load(Ordering::SeqCst))
117            .field("trust_state", &self.trust_state)
118            .field("withheld_code_sent", &self.withheld_code_sent)
119            .finish()
120    }
121}
122
123/// A device represents a E2EE capable client of an user.
124#[derive(Clone)]
125pub struct Device {
126    pub(crate) inner: DeviceData,
127    pub(crate) verification_machine: VerificationMachine,
128    pub(crate) own_identity: Option<OwnUserIdentityData>,
129    pub(crate) device_owner_identity: Option<UserIdentityData>,
130}
131
132#[cfg(not(tarpaulin_include))]
133impl std::fmt::Debug for Device {
134    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
135        f.debug_struct("Device").field("device", &self.inner).finish()
136    }
137}
138
139impl Deref for Device {
140    type Target = DeviceData;
141
142    fn deref(&self) -> &Self::Target {
143        &self.inner
144    }
145}
146
147impl Device {
148    /// Start a interactive verification with this `Device`
149    ///
150    /// Returns a `Sas` object and a to-device request that needs to be sent
151    /// out.
152    ///
153    /// This method has been deprecated in the spec and the
154    /// [`request_verification()`] method should be used instead.
155    ///
156    /// [`request_verification()`]: #method.request_verification
157    pub async fn start_verification(&self) -> StoreResult<(Sas, ToDeviceRequest)> {
158        let (sas, request) = self.verification_machine.start_sas(self.inner.clone()).await?;
159
160        if let OutgoingVerificationRequest::ToDevice(r) = request {
161            Ok((sas, r))
162        } else {
163            panic!("Invalid verification request type");
164        }
165    }
166
167    /// Is this our own device?
168    pub fn is_our_own_device(&self) -> bool {
169        let own_ed25519_key = self.verification_machine.store.account.identity_keys.ed25519;
170        let own_curve25519_key = self.verification_machine.store.account.identity_keys.curve25519;
171
172        self.user_id() == self.verification_machine.own_user_id()
173            && self.device_id() == self.verification_machine.own_device_id()
174            && self.ed25519_key().is_some_and(|k| k == own_ed25519_key)
175            && self.curve25519_key().is_some_and(|k| k == own_curve25519_key)
176    }
177
178    /// Does the given `InboundGroupSession` belong to this device?
179    ///
180    /// An `InboundGroupSession` is exchanged between devices as an Olm
181    /// encrypted `m.room_key` event. This method determines if this `Device`
182    /// can be confirmed as the creator and owner of the `m.room_key`.
183    pub fn is_owner_of_session(
184        &self,
185        session: &InboundGroupSession,
186    ) -> Result<bool, MismatchedIdentityKeysError> {
187        if session.has_been_imported() {
188            // An imported room key means that we did not receive the room key as a
189            // `m.room_key` event when the room key was initially exchanged.
190            //
191            // This could mean a couple of things:
192            //      1. We received the room key as a `m.forwarded_room_key`.
193            //      2. We imported the room key through a file export.
194            //      3. We imported the room key through a backup.
195            //
196            // To be certain that a `Device` is the owner of a room key we need to have a
197            // proof that the `Curve25519` key of this `Device` was used to
198            // initially exchange the room key. This proof is provided by the Olm decryption
199            // step, see below for further clarification.
200            //
201            // Each of the above room key methods that receive room keys do not contain this
202            // proof and we received only a claim that the room key is tied to a
203            // `Curve25519` key.
204            //
205            // Since there's no way to verify that the claim is true, we say that we don't
206            // know that the room key belongs to this device.
207            Ok(false)
208        } else if let Some(key) =
209            session.signing_keys().get(&DeviceKeyAlgorithm::Ed25519).and_then(|k| k.ed25519())
210        {
211            // Room keys are received as an `m.room.encrypted` to-device message using the
212            // `m.olm` algorithm. Upon decryption of the `m.room.encrypted` to-device
213            // message, the decrypted content will contain also an `Ed25519` public key[1].
214            //
215            // The inclusion of this key means that the `Curve25519` key of the `Device` and
216            // Olm `Session`, established using the DH authentication of the
217            // double ratchet, "binds" the `Ed25519` key of the `Device`. In other words, it
218            // prevents an attack in which Mallory publishes Bob's public `Curve25519` key
219            // as her own, and subsequently forwards an Olm message she received from Bob to
220            // Alice, claiming that she, Mallory, originated the Olm message (leading Alice
221            // to believe that Mallory also sent the messages in the subsequent Megolm
222            // session).
223            //
224            // On the other hand, the `Ed25519` key binds the `Curve25519` key
225            // using a signature which is uploaded to the server as
226            // `device_keys` and downloaded by us using a `/keys/query` request.
227            //
228            // A `Device` is considered to be the owner of a room key iff:
229            //     1. The `Curve25519` key that was used to establish the Olm `Session` that
230            //        was used to decrypt the to-device message is binding the `Ed25519` key
231            //        of this `Device` via the content of the to-device message, and:
232            //     2. The `Ed25519` key of this device has signed a `device_keys` object
233            //        that contains the `Curve25519` key from step 1.
234            //
235            // We don't need to check the signature of the `Device` here, since we don't
236            // accept a `Device` unless it has a valid `Ed25519` signature.
237            //
238            // We do check that the `Curve25519` that was used to decrypt the event carrying
239            // the `m.room_key` and the `Ed25519` key that was part of the
240            // decrypted content matches the keys found in this `Device`.
241            //
242            // ```text
243            //                                              ┌───────────────────────┐
244            //                                              │ EncryptedToDeviceEvent│
245            //                                              └───────────────────────┘
246            //                                                         │
247            //    ┌──────────────────────────────────┐                 │
248            //    │              Device              │                 ▼
249            //    ├──────────────────────────────────┤        ┌──────────────────┐
250            //    │            Device Keys           │        │      Session     │
251            //    ├────────────────┬─────────────────┤        ├──────────────────┤
252            //    │   Ed25519 Key  │  Curve25519 Key │◄──────►│  Curve25519 Key  │
253            //    └────────────────┴─────────────────┘        └──────────────────┘
254            //            ▲                                            │
255            //            │                                            │
256            //            │                                            │ Decrypt
257            //            │                                            │
258            //            │                                            ▼
259            //            │                                 ┌───────────────────────┐
260            //            │                                 │  DecryptedOlmV1Event  │
261            //            │                                 ├───────────────────────┤
262            //            │                                 │         keys          │
263            //            │                                 ├───────────────────────┤
264            //            └────────────────────────────────►│       Ed25519 Key     │
265            //                                              └───────────────────────┘
266            // ```
267            //
268            // [1]: https://spec.matrix.org/v1.5/client-server-api/#molmv1curve25519-aes-sha2
269            let ed25519_comparison = self.ed25519_key().map(|k| k == key);
270            let curve25519_comparison = self.curve25519_key().map(|k| k == session.sender_key());
271
272            match (ed25519_comparison, curve25519_comparison) {
273                // If we have any of the keys but they don't turn out to match, refuse to decrypt
274                // instead.
275                (_, Some(false)) | (Some(false), _) => Err(MismatchedIdentityKeysError {
276                    key_ed25519: key.into(),
277                    device_ed25519: self.ed25519_key().map(Into::into),
278                    key_curve25519: session.sender_key().into(),
279                    device_curve25519: self.curve25519_key().map(Into::into),
280                }),
281                // If both keys match, we have ourselves an owner.
282                (Some(true), Some(true)) => Ok(true),
283                // In the remaining cases, the device is missing at least one of the required
284                // identity keys, so we default to a negative answer.
285                _ => Ok(false),
286            }
287        } else {
288            Ok(false)
289        }
290    }
291
292    /// Is this device cross signed by its owner?
293    pub fn is_cross_signed_by_owner(&self) -> bool {
294        self.device_owner_identity
295            .as_ref()
296            .is_some_and(|owner_identity| self.inner.is_cross_signed_by_owner(owner_identity))
297    }
298
299    /// Is the device owner verified by us?
300    pub fn is_device_owner_verified(&self) -> bool {
301        self.device_owner_identity.as_ref().is_some_and(|id| match id {
302            UserIdentityData::Own(own_identity) => own_identity.is_verified(),
303            UserIdentityData::Other(other_identity) => {
304                self.own_identity.as_ref().is_some_and(|oi| oi.is_identity_verified(other_identity))
305            }
306        })
307    }
308
309    /// Request an interactive verification with this `Device`.
310    ///
311    /// Returns a `VerificationRequest` object and a to-device request that
312    /// needs to be sent out.
313    pub fn request_verification(&self) -> (VerificationRequest, OutgoingVerificationRequest) {
314        self.request_verification_helper(None)
315    }
316
317    /// Request an interactive verification with this `Device`.
318    ///
319    /// Returns a `VerificationRequest` object and a to-device request that
320    /// needs to be sent out.
321    ///
322    /// # Arguments
323    ///
324    /// * `methods` - The verification methods that we want to support.
325    pub fn request_verification_with_methods(
326        &self,
327        methods: Vec<VerificationMethod>,
328    ) -> (VerificationRequest, OutgoingVerificationRequest) {
329        self.request_verification_helper(Some(methods))
330    }
331
332    fn request_verification_helper(
333        &self,
334        methods: Option<Vec<VerificationMethod>>,
335    ) -> (VerificationRequest, OutgoingVerificationRequest) {
336        self.verification_machine.request_to_device_verification(
337            self.user_id(),
338            vec![self.device_id().to_owned()],
339            methods,
340        )
341    }
342
343    /// Get the most recently created session that belongs to this device.
344    pub(crate) async fn get_most_recent_session(&self) -> OlmResult<Option<Session>> {
345        self.inner.get_most_recent_session(self.verification_machine.store.inner()).await
346    }
347
348    /// Is this device considered to be verified.
349    ///
350    /// This method returns true if either [`is_locally_trusted()`] returns true
351    /// or if [`is_cross_signing_trusted()`] returns true.
352    ///
353    /// [`is_locally_trusted()`]: #method.is_locally_trusted
354    /// [`is_cross_signing_trusted()`]: #method.is_cross_signing_trusted
355    pub fn is_verified(&self) -> bool {
356        self.inner.is_verified(&self.own_identity, &self.device_owner_identity)
357    }
358
359    /// Is this device considered to be verified using cross signing.
360    pub fn is_cross_signing_trusted(&self) -> bool {
361        self.inner.is_cross_signing_trusted(&self.own_identity, &self.device_owner_identity)
362    }
363
364    /// Manually verify this device.
365    ///
366    /// This method will attempt to sign the device using our private cross
367    /// signing key.
368    ///
369    /// This method will always fail if the device belongs to someone else, we
370    /// can only sign our own devices.
371    ///
372    /// It can also fail if we don't have the private part of our self-signing
373    /// key.
374    ///
375    /// Returns a request that needs to be sent out for the device to be marked
376    /// as verified.
377    pub async fn verify(&self) -> Result<SignatureUploadRequest, SignatureError> {
378        if self.user_id() == self.verification_machine.own_user_id() {
379            Ok(self
380                .verification_machine
381                .store
382                .private_identity
383                .lock()
384                .await
385                .sign_device(&self.inner)
386                .await?)
387        } else {
388            Err(SignatureError::UserIdMismatch)
389        }
390    }
391
392    /// Set the local trust state of the device to the given state.
393    ///
394    /// This won't affect any cross signing trust state, this only sets a flag
395    /// marking to have the given trust state.
396    ///
397    /// # Arguments
398    ///
399    /// * `trust_state` - The new trust state that should be set for the device.
400    pub async fn set_local_trust(&self, trust_state: LocalTrust) -> StoreResult<()> {
401        self.inner.set_trust_state(trust_state);
402
403        let changes = Changes {
404            devices: DeviceChanges { changed: vec![self.inner.clone()], ..Default::default() },
405            ..Default::default()
406        };
407
408        self.verification_machine.store.save_changes(changes).await
409    }
410
411    /// Encrypt the given content for this `Device`.
412    ///
413    /// # Arguments
414    ///
415    /// * `event_type` - The type of the event that should be encrypted.
416    /// * `content` - The content of the event that should be encrypted.
417    ///
418    /// # Returns
419    ///
420    /// On success, a tuple `(session, content, message_id)`, where `session` is
421    /// the Olm [`Session`] that was used to encrypt the content, `content`
422    /// is the content for the `m.room.encrypted` to-device event, and
423    /// `message_id` is the newly-minted message ID stored within the content.
424    ///
425    /// If an Olm session has not already been established with this device,
426    /// returns `Err(OlmError::MissingSession)`.
427    pub(crate) async fn encrypt(
428        &self,
429        event_type: &str,
430        content: impl Serialize,
431    ) -> OlmResult<(Session, Raw<ToDeviceEncryptedEventContent>, String)> {
432        self.inner.encrypt(self.verification_machine.store.inner(), event_type, content).await
433    }
434
435    /// Encrypt the given inbound group session as a forwarded room key for this
436    /// device.
437    pub async fn encrypt_room_key_for_forwarding(
438        &self,
439        session: InboundGroupSession,
440        message_index: Option<u32>,
441    ) -> OlmResult<(Session, Raw<ToDeviceEncryptedEventContent>)> {
442        let content: ForwardedRoomKeyContent = {
443            let export = if let Some(index) = message_index {
444                session.export_at_index(index).await
445            } else {
446                session.export().await
447            };
448
449            export.try_into()?
450        };
451
452        let event_type = content.event_type().to_owned();
453
454        self.encrypt(&event_type, content)
455            .await
456            .map(|(session, message, _message_id)| (session, message))
457    }
458
459    /// Encrypt an event for this device.
460    ///
461    /// Beware that the 1-to-1 session must be established prior to this
462    /// call by using the [`OlmMachine::get_missing_sessions`] method.
463    ///
464    /// Notable limitation: The caller is responsible for sending the encrypted
465    /// event to the target device, this encryption method supports out-of-order
466    /// messages to a certain extent (2000 messages), if multiple messages are
467    /// encrypted using this method they should be sent in the same order as
468    /// they are encrypted.
469    ///
470    /// *Note*: To instead encrypt an event meant for a room use the
471    /// [`OlmMachine::encrypt_room_event()`] method instead.
472    ///
473    /// # Arguments
474    /// * `event_type` - The type of the event to be sent.
475    /// * `content` - The content of the event to be sent. This should be a type
476    ///   that implements the `Serialize` trait.
477    /// * `share_strategy` - The share strategy to use to determine whether we
478    ///   should encrypt to the device.
479    ///
480    /// # Returns
481    ///
482    /// The encrypted raw content to be shared with your preferred transport
483    /// layer (usually to-device), [`OlmError::MissingSession`] if there is
484    /// no established session with the device.
485    pub async fn encrypt_event_raw(
486        &self,
487        event_type: &str,
488        content: &Value,
489        share_strategy: CollectStrategy,
490    ) -> OlmResult<Raw<ToDeviceEncryptedEventContent>> {
491        if let Some(withheld_code) = withheld_code_for_device_for_share_strategy(
492            &self.inner,
493            share_strategy,
494            &self.own_identity,
495            &self.device_owner_identity,
496        )
497        .await?
498        {
499            return Err(OlmError::Withheld(withheld_code));
500        }
501
502        let (used_session, raw_encrypted, _message_id) = self.encrypt(event_type, content).await?;
503
504        // Persist the used session
505        self.verification_machine
506            .store
507            .save_changes(Changes { sessions: vec![used_session], ..Default::default() })
508            .await?;
509
510        Ok(raw_encrypted)
511    }
512
513    /// True if this device is an [MSC3814](https://github.com/matrix-org/matrix-spec-proposals/pull/3814) dehydrated device.
514    pub fn is_dehydrated(&self) -> bool {
515        self.inner.is_dehydrated()
516    }
517}
518
519/// A read only view over all devices belonging to a user.
520#[derive(Debug)]
521pub struct UserDevices {
522    pub(crate) inner: HashMap<OwnedDeviceId, DeviceData>,
523    pub(crate) verification_machine: VerificationMachine,
524    pub(crate) own_identity: Option<OwnUserIdentityData>,
525    pub(crate) device_owner_identity: Option<UserIdentityData>,
526}
527
528impl UserDevices {
529    /// Get the specific device with the given device ID.
530    pub fn get(&self, device_id: &DeviceId) -> Option<Device> {
531        self.inner.get(device_id).map(|d| Device {
532            inner: d.clone(),
533            verification_machine: self.verification_machine.clone(),
534            own_identity: self.own_identity.clone(),
535            device_owner_identity: self.device_owner_identity.clone(),
536        })
537    }
538
539    fn own_user_id(&self) -> &UserId {
540        self.verification_machine.own_user_id()
541    }
542
543    fn own_device_id(&self) -> &DeviceId {
544        self.verification_machine.own_device_id()
545    }
546
547    /// Returns true if there is at least one devices of this user that is
548    /// considered to be verified, false otherwise.
549    ///
550    /// This won't consider your own device as verified, as your own device is
551    /// always implicitly verified.
552    pub fn is_any_verified(&self) -> bool {
553        self.inner
554            .values()
555            .filter(|d| {
556                !(d.user_id() == self.own_user_id() && d.device_id() == self.own_device_id())
557            })
558            .any(|d| d.is_verified(&self.own_identity, &self.device_owner_identity))
559    }
560
561    /// Iterator over all the device ids of the user devices.
562    pub fn keys(&self) -> impl Iterator<Item = &DeviceId> {
563        self.inner.keys().map(Deref::deref)
564    }
565
566    /// Iterator over all the devices of the user devices.
567    pub fn devices(&self) -> impl Iterator<Item = Device> + '_ {
568        self.inner.values().map(move |d| Device {
569            inner: d.clone(),
570            verification_machine: self.verification_machine.clone(),
571            own_identity: self.own_identity.clone(),
572            device_owner_identity: self.device_owner_identity.clone(),
573        })
574    }
575}
576
577/// The local trust state of a device.
578#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
579#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
580pub enum LocalTrust {
581    /// The device has been verified and is trusted.
582    Verified = 0,
583    /// The device been blacklisted from communicating.
584    BlackListed = 1,
585    /// The trust state of the device is being ignored.
586    Ignored = 2,
587    /// The trust state is unset.
588    Unset = 3,
589}
590
591impl From<i64> for LocalTrust {
592    fn from(state: i64) -> Self {
593        match state {
594            0 => LocalTrust::Verified,
595            1 => LocalTrust::BlackListed,
596            2 => LocalTrust::Ignored,
597            3 => LocalTrust::Unset,
598            _ => LocalTrust::Unset,
599        }
600    }
601}
602
603impl DeviceData {
604    /// Create a new Device, this constructor skips signature verification of
605    /// the keys, `TryFrom` should be used for completely new devices we
606    /// receive.
607    pub fn new(device_keys: DeviceKeys, trust_state: LocalTrust) -> Self {
608        Self {
609            device_keys: device_keys.into(),
610            trust_state: Arc::new(RwLock::new(trust_state)),
611            deleted: Arc::new(AtomicBool::new(false)),
612            withheld_code_sent: Arc::new(AtomicBool::new(false)),
613            first_time_seen_ts: MilliSecondsSinceUnixEpoch::now(),
614            olm_wedging_index: Default::default(),
615        }
616    }
617
618    /// The user id of the device owner.
619    pub fn user_id(&self) -> &UserId {
620        &self.device_keys.user_id
621    }
622
623    /// The unique ID of the device.
624    pub fn device_id(&self) -> &DeviceId {
625        &self.device_keys.device_id
626    }
627
628    /// Get the human readable name of the device.
629    pub fn display_name(&self) -> Option<&str> {
630        self.device_keys.unsigned.device_display_name.as_deref()
631    }
632
633    /// Get the key of the given key algorithm belonging to this device.
634    pub fn get_key(&self, algorithm: DeviceKeyAlgorithm) -> Option<&DeviceKey> {
635        self.device_keys.get_key(algorithm)
636    }
637
638    /// Get the Curve25519 key of the given device.
639    pub fn curve25519_key(&self) -> Option<Curve25519PublicKey> {
640        self.device_keys.curve25519_key()
641    }
642
643    /// Get the Ed25519 key of the given device.
644    pub fn ed25519_key(&self) -> Option<Ed25519PublicKey> {
645        self.device_keys.ed25519_key()
646    }
647
648    /// Get a map containing all the device keys.
649    pub fn keys(&self) -> &BTreeMap<OwnedDeviceKeyId, DeviceKey> {
650        &self.device_keys.keys
651    }
652
653    /// Get a map containing all the device signatures.
654    pub fn signatures(&self) -> &Signatures {
655        &self.device_keys.signatures
656    }
657
658    /// Get the trust state of the device.
659    pub fn local_trust_state(&self) -> LocalTrust {
660        *self.trust_state.read()
661    }
662
663    /// Is the device locally marked as trusted.
664    pub fn is_locally_trusted(&self) -> bool {
665        self.local_trust_state() == LocalTrust::Verified
666    }
667
668    /// Is the device locally marked as blacklisted.
669    ///
670    /// Blacklisted devices won't receive any group sessions.
671    pub fn is_blacklisted(&self) -> bool {
672        self.local_trust_state() == LocalTrust::BlackListed
673    }
674
675    /// Set the trust state of the device to the given state.
676    ///
677    /// Note: This should only done in the crypto store where the trust state
678    /// can be stored.
679    pub(crate) fn set_trust_state(&self, state: LocalTrust) {
680        *self.trust_state.write() = state;
681    }
682
683    pub(crate) fn mark_withheld_code_as_sent(&self) {
684        self.withheld_code_sent.store(true, Ordering::Relaxed)
685    }
686
687    /// Returns true if the `m.no_olm` withheld code was already sent to this
688    /// device.
689    pub fn was_withheld_code_sent(&self) -> bool {
690        self.withheld_code_sent.load(Ordering::Relaxed)
691    }
692
693    /// Get the list of algorithms this device supports.
694    pub fn algorithms(&self) -> &[EventEncryptionAlgorithm] {
695        &self.device_keys.algorithms
696    }
697
698    /// Does this device support any of our known Olm encryption algorithms.
699    pub fn supports_olm(&self) -> bool {
700        #[cfg(feature = "experimental-algorithms")]
701        {
702            self.algorithms().contains(&EventEncryptionAlgorithm::OlmV1Curve25519AesSha2)
703                || self.algorithms().contains(&EventEncryptionAlgorithm::OlmV2Curve25519AesSha2)
704        }
705
706        #[cfg(not(feature = "experimental-algorithms"))]
707        {
708            self.algorithms().contains(&EventEncryptionAlgorithm::OlmV1Curve25519AesSha2)
709        }
710    }
711
712    /// Find and return the most recently created Olm [`Session`] we are sharing
713    /// with this device.
714    pub(crate) async fn get_most_recent_session(
715        &self,
716        store: &CryptoStoreWrapper,
717    ) -> OlmResult<Option<Session>> {
718        if let Some(sender_key) = self.curve25519_key() {
719            if let Some(sessions) = store.get_sessions(&sender_key.to_base64()).await? {
720                let mut sessions = sessions.lock().await;
721                sessions.sort_by_key(|s| s.creation_time);
722
723                Ok(sessions.last().cloned())
724            } else {
725                Ok(None)
726            }
727        } else {
728            Ok(None)
729        }
730    }
731
732    /// Does this device support the olm.v2.curve25519-aes-sha2 encryption
733    /// algorithm.
734    #[cfg(feature = "experimental-algorithms")]
735    pub fn supports_olm_v2(&self) -> bool {
736        self.algorithms().contains(&EventEncryptionAlgorithm::OlmV2Curve25519AesSha2)
737    }
738
739    /// Get the optimal `SessionConfig` for this device.
740    pub fn olm_session_config(&self) -> SessionConfig {
741        #[cfg(feature = "experimental-algorithms")]
742        if self.supports_olm_v2() {
743            SessionConfig::version_2()
744        } else {
745            SessionConfig::version_1()
746        }
747
748        #[cfg(not(feature = "experimental-algorithms"))]
749        SessionConfig::version_1()
750    }
751
752    /// Is the device deleted.
753    pub fn is_deleted(&self) -> bool {
754        self.deleted.load(Ordering::Relaxed)
755    }
756
757    pub(crate) fn is_verified(
758        &self,
759        own_identity: &Option<OwnUserIdentityData>,
760        device_owner: &Option<UserIdentityData>,
761    ) -> bool {
762        self.is_locally_trusted() || self.is_cross_signing_trusted(own_identity, device_owner)
763    }
764
765    pub(crate) fn is_cross_signing_trusted(
766        &self,
767        own_identity: &Option<OwnUserIdentityData>,
768        device_owner: &Option<UserIdentityData>,
769    ) -> bool {
770        own_identity.as_ref().zip(device_owner.as_ref()).is_some_and(
771            |(own_identity, device_identity)| {
772                match device_identity {
773                    UserIdentityData::Own(_) => {
774                        own_identity.is_verified() && own_identity.is_device_signed(self)
775                    }
776
777                    // If it's a device from someone else, first check
778                    // that our user has verified the other user and then
779                    // check if the other user has signed this device.
780                    UserIdentityData::Other(device_identity) => {
781                        own_identity.is_identity_verified(device_identity)
782                            && device_identity.is_device_signed(self)
783                    }
784                }
785            },
786        )
787    }
788
789    pub(crate) fn is_cross_signed_by_owner(
790        &self,
791        device_owner_identity: &UserIdentityData,
792    ) -> bool {
793        match device_owner_identity {
794            // If it's one of our own devices, just check that
795            // we signed the device.
796            UserIdentityData::Own(identity) => identity.is_device_signed(self),
797            // If it's a device from someone else, check
798            // if the other user has signed this device.
799            UserIdentityData::Other(device_identity) => device_identity.is_device_signed(self),
800        }
801    }
802
803    /// Encrypt the given content for this device.
804    ///
805    /// # Arguments
806    ///
807    /// * `store` - The crypto store. Used to find an established Olm session
808    ///   for this device.
809    /// * `event_type` - The type of the event that should be encrypted.
810    /// * `content` - The content of the event that should be encrypted.
811    ///
812    /// # Returns
813    ///
814    /// On success, a tuple `(session, content, message_id)`, where `session` is
815    /// the Olm [`Session`] that was used to encrypt the content, `content`
816    /// is the content for the `m.room.encrypted` to-device event, and
817    /// `message_id` is the newly-minted message ID stored within the content.
818    ///
819    /// If an Olm session has not already been established with this device,
820    /// returns `Err(OlmError::MissingSession)`.
821    #[instrument(
822        skip_all,
823        fields(
824            recipient = ?self.user_id(),
825            recipient_device = ?self.device_id(),
826            recipient_key = ?self.curve25519_key(),
827            event_type,
828            message_id,
829        ))
830    ]
831    pub(crate) async fn encrypt(
832        &self,
833        store: &CryptoStoreWrapper,
834        event_type: &str,
835        content: impl Serialize,
836    ) -> OlmResult<(Session, Raw<ToDeviceEncryptedEventContent>, String)> {
837        #[cfg(not(target_family = "wasm"))]
838        let message_id = ulid::Ulid::new().to_string();
839        #[cfg(target_family = "wasm")]
840        let message_id = ruma::TransactionId::new().to_string();
841
842        tracing::Span::current().record("message_id", &message_id);
843
844        let session = self.get_most_recent_session(store).await?;
845
846        if let Some(mut session) = session {
847            let message =
848                session.encrypt(self, event_type, content, Some(message_id.clone())).await?;
849
850            Ok((session, message, message_id))
851        } else {
852            trace!("Trying to encrypt an event for a device, but no Olm session is found.");
853            Err(OlmError::MissingSession)
854        }
855    }
856
857    pub(crate) async fn maybe_encrypt_room_key(
858        &self,
859        store: &CryptoStoreWrapper,
860        session: OutboundGroupSession,
861    ) -> OlmResult<MaybeEncryptedRoomKey> {
862        let content = session.as_content().await;
863        let message_index = session.message_index().await;
864        let event_type = content.event_type().to_owned();
865
866        match self.encrypt(store, &event_type, content).await {
867            Ok((session, encrypted, _)) => Ok(MaybeEncryptedRoomKey::Encrypted {
868                share_info: Box::new(ShareInfo::new_shared(
869                    session.sender_key().to_owned(),
870                    message_index,
871                    self.olm_wedging_index,
872                )),
873                used_session: Box::new(session),
874                message: encrypted.cast(),
875            }),
876
877            Err(OlmError::MissingSession) => Ok(MaybeEncryptedRoomKey::MissingSession),
878            Err(e) => Err(e),
879        }
880    }
881
882    /// Update a device with a new device keys struct.
883    ///
884    /// Returns `true` if any changes were made to the data.
885    pub(crate) fn update_device(
886        &mut self,
887        device_keys: &DeviceKeys,
888    ) -> Result<bool, SignatureError> {
889        device_keys.check_self_signature()?;
890
891        if self.user_id() != device_keys.user_id || self.device_id() != device_keys.device_id {
892            Err(SignatureError::UserIdMismatch)
893        } else if self.ed25519_key() != device_keys.ed25519_key() {
894            Err(SignatureError::SigningKeyChanged(
895                self.ed25519_key().map(Box::new),
896                device_keys.ed25519_key().map(Box::new),
897            ))
898        } else if self.device_keys.as_ref() != device_keys {
899            trace!(
900                user_id = ?self.user_id(),
901                device_id = ?self.device_id(),
902                keys = ?self.keys(),
903                "Updated a device",
904            );
905
906            self.device_keys = device_keys.clone().into();
907
908            Ok(true)
909        } else {
910            // no changes needed
911            Ok(false)
912        }
913    }
914
915    /// Return the device keys
916    pub fn as_device_keys(&self) -> &DeviceKeys {
917        &self.device_keys
918    }
919
920    /// Check if the given JSON is signed by this device key.
921    ///
922    /// This method should only be used if an object's signature needs to be
923    /// checked multiple times, and you'd like to avoid performing the
924    /// canonicalization step each time.
925    ///
926    /// **Note**: Use this method with caution, the `canonical_json` needs to be
927    /// correctly canonicalized and make sure that the object you are checking
928    /// the signature for is allowed to be signed by a device.
929    pub(crate) fn has_signed_raw(
930        &self,
931        signatures: &Signatures,
932        canonical_json: &str,
933    ) -> Result<(), SignatureError> {
934        let key = self.ed25519_key().ok_or(SignatureError::MissingSigningKey)?;
935        let user_id = self.user_id();
936        let key_id = &DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, self.device_id());
937
938        key.verify_canonicalized_json(user_id, key_id, signatures, canonical_json)
939    }
940
941    pub(crate) fn verify_one_time_key(
942        &self,
943        one_time_key: &SignedKey,
944    ) -> Result<(), SignatureError> {
945        self.device_keys.has_signed(one_time_key)
946    }
947
948    /// Mark the device as deleted.
949    pub(crate) fn mark_as_deleted(&self) {
950        self.deleted.store(true, Ordering::Relaxed);
951    }
952
953    #[cfg(any(test, feature = "testing"))]
954    #[allow(dead_code)]
955    /// Generate the Device from a reference of an OlmMachine.
956    pub async fn from_machine_test_helper(
957        machine: &OlmMachine,
958    ) -> Result<DeviceData, crate::CryptoStoreError> {
959        Ok(DeviceData::from_account(&*machine.store().cache().await?.account().await?))
960    }
961
962    /// Create [`DeviceData`] from an [`Account`].
963    ///
964    /// We will have our own device data in the store once we receive a
965    /// `/keys/query` response, but this is useful to create it before we
966    /// receive such a response.
967    ///
968    /// It also makes it easier to check that the server doesn't lie about our
969    /// own device.
970    ///
971    /// *Don't* use this after we received a `/keys/query` response, other
972    /// users/devices might add signatures to our own device, which can't be
973    /// replicated locally.
974    pub fn from_account(account: &Account) -> DeviceData {
975        let device_keys = account.device_keys();
976        let mut device = DeviceData::try_from(&device_keys)
977            .expect("Creating a device from our own account should always succeed");
978        device.first_time_seen_ts = account.creation_local_time();
979
980        device
981    }
982
983    /// Get the local timestamp of when this device was first persisted, in
984    /// milliseconds since epoch (client local time).
985    pub fn first_time_seen_ts(&self) -> MilliSecondsSinceUnixEpoch {
986        self.first_time_seen_ts
987    }
988
989    /// True if this device is an [MSC3814](https://github.com/matrix-org/matrix-spec-proposals/pull/3814) dehydrated device.
990    pub fn is_dehydrated(&self) -> bool {
991        self.device_keys.dehydrated.unwrap_or(false)
992    }
993}
994
995impl TryFrom<&DeviceKeys> for DeviceData {
996    type Error = SignatureError;
997
998    fn try_from(device_keys: &DeviceKeys) -> Result<Self, Self::Error> {
999        device_keys.check_self_signature()?;
1000        Ok(Self {
1001            device_keys: device_keys.clone().into(),
1002            deleted: Arc::new(AtomicBool::new(false)),
1003            trust_state: Arc::new(RwLock::new(LocalTrust::Unset)),
1004            withheld_code_sent: Arc::new(AtomicBool::new(false)),
1005            first_time_seen_ts: MilliSecondsSinceUnixEpoch::now(),
1006            olm_wedging_index: Default::default(),
1007        })
1008    }
1009}
1010
1011impl PartialEq for DeviceData {
1012    fn eq(&self, other: &Self) -> bool {
1013        self.user_id() == other.user_id() && self.device_id() == other.device_id()
1014    }
1015}
1016
1017/// Testing Facilities for Device Management
1018#[cfg(any(test, feature = "testing"))]
1019#[allow(dead_code)]
1020pub(crate) mod testing {
1021    use serde_json::json;
1022
1023    use crate::{identities::DeviceData, types::DeviceKeys};
1024
1025    /// Generate default DeviceKeys for tests
1026    pub fn device_keys() -> DeviceKeys {
1027        let device_keys = json!({
1028          "algorithms": vec![
1029              "m.olm.v1.curve25519-aes-sha2",
1030              "m.megolm.v1.aes-sha2"
1031          ],
1032          "device_id": "BNYQQWUMXO",
1033          "user_id": "@example:localhost",
1034          "keys": {
1035              "curve25519:BNYQQWUMXO": "xfgbLIC5WAl1OIkpOzoxpCe8FsRDT6nch7NQsOb15nc",
1036              "ed25519:BNYQQWUMXO": "2/5LWJMow5zhJqakV88SIc7q/1pa8fmkfgAzx72w9G4"
1037          },
1038          "signatures": {
1039              "@example:localhost": {
1040                  "ed25519:BNYQQWUMXO": "kTwMrbsLJJM/uFGOj/oqlCaRuw7i9p/6eGrTlXjo8UJMCFAetoyWzoMcF35vSe4S6FTx8RJmqX6rM7ep53MHDQ"
1041              }
1042          },
1043          "unsigned": {
1044              "device_display_name": "Alice's mobile phone"
1045          }
1046        });
1047
1048        serde_json::from_value(device_keys).unwrap()
1049    }
1050
1051    /// Generate default [`DeviceData`] for tests
1052    pub fn get_device() -> DeviceData {
1053        let device_keys = device_keys();
1054        DeviceData::try_from(&device_keys).unwrap()
1055    }
1056}
1057
1058#[cfg(test)]
1059pub(crate) mod tests {
1060    use ruma::{MilliSecondsSinceUnixEpoch, user_id};
1061    use serde_json::json;
1062    use vodozemac::{Curve25519PublicKey, Ed25519PublicKey};
1063
1064    use super::testing::{device_keys, get_device};
1065    use crate::{DeviceData, identities::LocalTrust};
1066
1067    #[test]
1068    fn create_a_device() {
1069        let now = MilliSecondsSinceUnixEpoch::now();
1070        let user_id = user_id!("@example:localhost");
1071        let device_id = "BNYQQWUMXO";
1072
1073        let device = get_device();
1074
1075        assert_eq!(user_id, device.user_id());
1076        assert_eq!(device_id, device.device_id());
1077        assert_eq!(device.algorithms().len(), 2);
1078        assert_eq!(LocalTrust::Unset, device.local_trust_state());
1079        assert_eq!("Alice's mobile phone", device.display_name().unwrap());
1080        assert_eq!(
1081            device.curve25519_key().unwrap(),
1082            Curve25519PublicKey::from_base64("xfgbLIC5WAl1OIkpOzoxpCe8FsRDT6nch7NQsOb15nc")
1083                .unwrap(),
1084        );
1085        assert_eq!(
1086            device.ed25519_key().unwrap(),
1087            Ed25519PublicKey::from_base64("2/5LWJMow5zhJqakV88SIc7q/1pa8fmkfgAzx72w9G4").unwrap(),
1088        );
1089
1090        let then = MilliSecondsSinceUnixEpoch::now();
1091
1092        assert!(device.first_time_seen_ts() >= now);
1093        assert!(device.first_time_seen_ts() <= then);
1094    }
1095
1096    #[test]
1097    fn update_a_device() {
1098        let mut device = get_device();
1099
1100        assert_eq!("Alice's mobile phone", device.display_name().unwrap());
1101
1102        let display_name = "Alice's work computer".to_owned();
1103
1104        let mut device_keys = device_keys();
1105        device_keys.unsigned.device_display_name = Some(display_name.clone());
1106        assert!(device.update_device(&device_keys).unwrap());
1107        assert_eq!(&display_name, device.display_name().as_ref().unwrap());
1108
1109        // A second call to `update_device` with the same data should return `false`.
1110        assert!(!device.update_device(&device_keys).unwrap());
1111    }
1112
1113    #[test]
1114    #[allow(clippy::redundant_clone)]
1115    fn delete_a_device() {
1116        let device = get_device();
1117        assert!(!device.is_deleted());
1118
1119        let device_clone = device.clone();
1120
1121        device.mark_as_deleted();
1122        assert!(device.is_deleted());
1123        assert!(device_clone.is_deleted());
1124    }
1125
1126    #[test]
1127    fn deserialize_device() {
1128        let user_id = user_id!("@example:localhost");
1129        let device_id = "BNYQQWUMXO";
1130
1131        let device = json!({
1132            "inner": {
1133                "user_id": user_id,
1134                "device_id": device_id,
1135                "algorithms": ["m.olm.v1.curve25519-aes-sha2","m.megolm.v1.aes-sha2"],
1136                "keys": {
1137                    "curve25519:BNYQQWUMXO": "xfgbLIC5WAl1OIkpOzoxpCe8FsRDT6nch7NQsOb15nc",
1138                    "ed25519:BNYQQWUMXO": "2/5LWJMow5zhJqakV88SIc7q/1pa8fmkfgAzx72w9G4"
1139                },
1140                "signatures": {
1141                    "@example:localhost": {
1142                        "ed25519:BNYQQWUMXO": "kTwMrbsLJJM/uFGOj/oqlCaRuw7i9p/6eGrTlXjo8UJMCFAetoyWzoMcF35vSe4S6FTx8RJmqX6rM7ep53MHDQ"
1143                    }
1144                },
1145                "unsigned": {
1146                    "device_display_name": "Alice's mobile phone"
1147                }
1148            },
1149            "deleted": false,
1150            "trust_state": "Verified",
1151            "withheld_code_sent": false,
1152            "first_time_seen_ts": 1696931068314u64
1153        });
1154
1155        let device: DeviceData =
1156            serde_json::from_value(device).expect("We should be able to deserialize our device");
1157
1158        assert_eq!(user_id, device.user_id());
1159        assert_eq!(device_id, device.device_id());
1160        assert_eq!(device.algorithms().len(), 2);
1161        assert_eq!(LocalTrust::Verified, device.local_trust_state());
1162        assert_eq!("Alice's mobile phone", device.display_name().unwrap());
1163        assert_eq!(
1164            device.curve25519_key().unwrap(),
1165            Curve25519PublicKey::from_base64("xfgbLIC5WAl1OIkpOzoxpCe8FsRDT6nch7NQsOb15nc")
1166                .unwrap(),
1167        );
1168        assert_eq!(
1169            device.ed25519_key().unwrap(),
1170            Ed25519PublicKey::from_base64("2/5LWJMow5zhJqakV88SIc7q/1pa8fmkfgAzx72w9G4").unwrap(),
1171        );
1172    }
1173}