matrix_sdk_base/room/
encryption.rs

1// Copyright 2025 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 ruma::events::room::encryption::RoomEncryptionEventContent;
16
17use super::Room;
18
19impl Room {
20    /// Get the encryption state of this room.
21    pub fn encryption_state(&self) -> EncryptionState {
22        self.info.read().encryption_state()
23    }
24
25    /// Get the `m.room.encryption` content that enabled end to end encryption
26    /// in the room.
27    pub fn encryption_settings(&self) -> Option<RoomEncryptionEventContent> {
28        self.info.read().base_info.encryption.clone()
29    }
30}
31
32/// Represents the state of a room encryption.
33#[derive(Debug)]
34#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
35pub enum EncryptionState {
36    /// The room is encrypted.
37    Encrypted,
38
39    /// The room is encrypted, additionally requiring state events to be
40    /// encrypted.
41    #[cfg(feature = "experimental-encrypted-state-events")]
42    StateEncrypted,
43
44    /// The room is not encrypted.
45    NotEncrypted,
46
47    /// The state of the room encryption is unknown, probably because the
48    /// `/sync` did not provide all data needed to decide.
49    Unknown,
50}
51
52impl EncryptionState {
53    /// Check whether `EncryptionState` is [`Encrypted`][Self::Encrypted].
54    #[cfg(not(feature = "experimental-encrypted-state-events"))]
55    pub fn is_encrypted(&self) -> bool {
56        matches!(self, Self::Encrypted)
57    }
58
59    /// Check whether `EncryptionState` is [`Encrypted`][Self::Encrypted] or
60    /// [`StateEncrypted`][Self::StateEncrypted].
61    #[cfg(feature = "experimental-encrypted-state-events")]
62    pub fn is_encrypted(&self) -> bool {
63        matches!(self, Self::Encrypted | Self::StateEncrypted)
64    }
65
66    /// Check whether `EncryptionState` is
67    /// [`StateEncrypted`][Self::StateEncrypted].
68    #[cfg(feature = "experimental-encrypted-state-events")]
69    pub fn is_state_encrypted(&self) -> bool {
70        matches!(self, Self::StateEncrypted)
71    }
72
73    /// Check whether `EncryptionState` is [`Unknown`][Self::Unknown].
74    pub fn is_unknown(&self) -> bool {
75        matches!(self, Self::Unknown)
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use std::{
82        ops::{Not, Sub},
83        sync::Arc,
84        time::Duration,
85    };
86
87    use assert_matches::assert_matches;
88    use matrix_sdk_test::{ALICE, event_factory::EventFactory};
89    use ruma::{
90        EventEncryptionAlgorithm, MilliSecondsSinceUnixEpoch, event_id,
91        events::{AnySyncStateEvent, room::encryption::RoomEncryptionEventContent},
92        room_id,
93        serde::Raw,
94        time::SystemTime,
95        user_id,
96    };
97
98    use super::{EncryptionState, Room};
99    use crate::{RoomState, store::MemoryStore, utils::RawSyncStateEventWithKeys};
100
101    fn make_room_test_helper(room_type: RoomState) -> (Arc<MemoryStore>, Room) {
102        let store = Arc::new(MemoryStore::new());
103        let user_id = user_id!("@me:example.org");
104        let room_id = room_id!("!test:localhost");
105        let (sender, _receiver) = tokio::sync::broadcast::channel(1);
106
107        (store.clone(), Room::new(user_id, store, room_id, room_type, sender))
108    }
109
110    fn timestamp(minutes_ago: u32) -> MilliSecondsSinceUnixEpoch {
111        MilliSecondsSinceUnixEpoch::from_system_time(
112            SystemTime::now().sub(Duration::from_secs((60 * minutes_ago).into())),
113        )
114        .expect("date out of range")
115    }
116
117    fn receive_state_events(room: &Room, events: Vec<Raw<AnySyncStateEvent>>) {
118        room.info.update_if(|info| {
119            let mut res = false;
120            for ev in events {
121                res |= info.handle_state_event(
122                    &mut RawSyncStateEventWithKeys::try_from_raw_state_event(ev)
123                        .expect("generated state event should be valid"),
124                );
125            }
126            res
127        });
128    }
129
130    #[test]
131    fn test_encryption_is_set_when_encryption_event_is_received_encrypted() {
132        let (_store, room) = make_room_test_helper(RoomState::Joined);
133
134        assert_matches!(room.encryption_state(), EncryptionState::Unknown);
135
136        let encryption_content =
137            RoomEncryptionEventContent::new(EventEncryptionAlgorithm::MegolmV1AesSha2);
138        let encryption_event = EventFactory::new()
139            .sender(*ALICE)
140            .event(encryption_content)
141            .state_key("")
142            .event_id(event_id!("$1234_1"))
143            // we can simply use now here since this will be dropped when using a MinimalStateEvent
144            // in the roomInfo
145            .server_ts(timestamp(0))
146            .into();
147        receive_state_events(&room, vec![encryption_event]);
148
149        assert_matches!(room.encryption_state(), EncryptionState::Encrypted);
150    }
151
152    #[test]
153    fn test_encryption_is_set_when_encryption_event_is_received_not_encrypted() {
154        let (_store, room) = make_room_test_helper(RoomState::Joined);
155
156        assert_matches!(room.encryption_state(), EncryptionState::Unknown);
157        room.info.update_if(|info| {
158            info.mark_encryption_state_synced();
159
160            false
161        });
162
163        assert_matches!(room.encryption_state(), EncryptionState::NotEncrypted);
164    }
165
166    #[test]
167    fn test_encryption_state() {
168        assert!(EncryptionState::Unknown.is_unknown());
169        assert!(EncryptionState::Encrypted.is_unknown().not());
170        assert!(EncryptionState::NotEncrypted.is_unknown().not());
171
172        assert!(EncryptionState::Unknown.is_encrypted().not());
173        assert!(EncryptionState::Encrypted.is_encrypted());
174        assert!(EncryptionState::NotEncrypted.is_encrypted().not());
175
176        #[cfg(feature = "experimental-encrypted-state-events")]
177        {
178            assert!(EncryptionState::StateEncrypted.is_unknown().not());
179            assert!(EncryptionState::StateEncrypted.is_encrypted());
180
181            assert!(EncryptionState::Unknown.is_state_encrypted().not());
182            assert!(EncryptionState::Encrypted.is_state_encrypted().not());
183            assert!(EncryptionState::StateEncrypted.is_state_encrypted());
184            assert!(EncryptionState::NotEncrypted.is_state_encrypted().not());
185        }
186    }
187}