1use std::{
21 collections::{BTreeMap, HashMap, HashSet},
22 time::Duration,
23};
24
25use rand::Rng;
26use ruma::{
27 MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedRoomId, OwnedUserId,
28 events::secret::request::SecretName,
29};
30use serde::{Deserialize, Serialize};
31use vodozemac::{Curve25519PublicKey, base64_encode};
32use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
33
34use super::{DehydrationError, GossipRequest};
35#[cfg(feature = "experimental-push-secrets")]
36use crate::types::events::secret_push::SecretPushContent;
37use crate::{
38 Account, Device, DeviceData, GossippedSecret, Session, UserIdentity, UserIdentityData,
39 olm::{
40 InboundGroupSession, OlmMessageHash, OutboundGroupSession, PrivateCrossSigningIdentity,
41 SenderData,
42 },
43 types::{
44 EventEncryptionAlgorithm,
45 events::{
46 room_key_bundle::RoomKeyBundleContent,
47 room_key_withheld::{RoomKeyWithheldContent, RoomKeyWithheldEvent},
48 },
49 },
50};
51
52#[derive(Default, Debug)]
58#[allow(missing_docs)]
59pub struct PendingChanges {
60 pub account: Option<Account>,
61}
62
63impl PendingChanges {
64 pub fn is_empty(&self) -> bool {
66 self.account.is_none()
67 }
68}
69
70#[derive(Default, Debug)]
73#[allow(missing_docs)]
74pub struct Changes {
75 pub private_identity: Option<PrivateCrossSigningIdentity>,
76 pub backup_version: Option<String>,
77 pub backup_decryption_key: Option<BackupDecryptionKey>,
78 pub dehydrated_device_pickle_key: Option<DehydratedDeviceKey>,
79 pub sessions: Vec<Session>,
80 pub message_hashes: Vec<OlmMessageHash>,
81 pub inbound_group_sessions: Vec<InboundGroupSession>,
82 pub outbound_group_sessions: Vec<OutboundGroupSession>,
83 pub key_requests: Vec<GossipRequest>,
84 pub identities: IdentityChanges,
85 pub devices: DeviceChanges,
86 pub withheld_session_info: BTreeMap<OwnedRoomId, BTreeMap<String, RoomKeyWithheldEntry>>,
88 pub room_settings: HashMap<OwnedRoomId, RoomSettings>,
89 pub secrets: Vec<SecretsInboxItem>,
90 pub next_batch_token: Option<String>,
91
92 pub received_room_key_bundles: Vec<StoredRoomKeyBundleData>,
95
96 pub room_key_backups_fully_downloaded: HashSet<OwnedRoomId>,
99
100 pub rooms_pending_key_bundle: HashMap<OwnedRoomId, Option<RoomPendingKeyBundleDetails>>,
103}
104
105#[derive(Clone)]
108pub struct SecretsInboxItem {
109 pub secret_name: SecretName,
111 pub secret: Zeroizing<String>,
113}
114
115#[cfg(not(tarpaulin_include))]
116impl std::fmt::Debug for SecretsInboxItem {
117 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118 f.debug_tuple("SecretsInboxItem").field(&self.secret_name).finish()
119 }
120}
121
122impl From<GossippedSecret> for SecretsInboxItem {
123 fn from(secret: GossippedSecret) -> Self {
124 Self { secret_name: secret.secret_name, secret: secret.event.content.secret.clone().into() }
125 }
126}
127
128#[cfg(feature = "experimental-push-secrets")]
129impl From<SecretPushContent> for SecretsInboxItem {
130 fn from(secret: SecretPushContent) -> Self {
131 Self { secret_name: secret.name.clone(), secret: secret.secret.clone().into() }
132 }
133}
134
135#[derive(Clone, Debug, Serialize, Deserialize)]
139pub struct StoredRoomKeyBundleData {
140 pub sender_user: OwnedUserId,
142
143 pub sender_key: Curve25519PublicKey,
145
146 pub sender_data: SenderData,
149
150 pub bundle_data: RoomKeyBundleContent,
152}
153
154#[derive(Clone, Debug, Serialize, Deserialize)]
156pub struct TrackedUser {
157 pub user_id: OwnedUserId,
159 pub dirty: bool,
164}
165
166impl Changes {
167 pub fn is_empty(&self) -> bool {
169 self.private_identity.is_none()
170 && self.backup_version.is_none()
171 && self.backup_decryption_key.is_none()
172 && self.dehydrated_device_pickle_key.is_none()
173 && self.sessions.is_empty()
174 && self.message_hashes.is_empty()
175 && self.inbound_group_sessions.is_empty()
176 && self.outbound_group_sessions.is_empty()
177 && self.key_requests.is_empty()
178 && self.identities.is_empty()
179 && self.devices.is_empty()
180 && self.withheld_session_info.is_empty()
181 && self.room_settings.is_empty()
182 && self.secrets.is_empty()
183 && self.next_batch_token.is_none()
184 && self.received_room_key_bundles.is_empty()
185 }
186}
187
188#[derive(Debug, Clone, Default)]
199#[allow(missing_docs)]
200pub struct IdentityChanges {
201 pub new: Vec<UserIdentityData>,
202 pub changed: Vec<UserIdentityData>,
203 pub unchanged: Vec<UserIdentityData>,
204}
205
206impl IdentityChanges {
207 pub(super) fn is_empty(&self) -> bool {
208 self.new.is_empty() && self.changed.is_empty()
209 }
210
211 pub(super) fn into_maps(
214 self,
215 ) -> (
216 BTreeMap<OwnedUserId, UserIdentityData>,
217 BTreeMap<OwnedUserId, UserIdentityData>,
218 BTreeMap<OwnedUserId, UserIdentityData>,
219 ) {
220 let new: BTreeMap<_, _> = self
221 .new
222 .into_iter()
223 .map(|identity| (identity.user_id().to_owned(), identity))
224 .collect();
225
226 let changed: BTreeMap<_, _> = self
227 .changed
228 .into_iter()
229 .map(|identity| (identity.user_id().to_owned(), identity))
230 .collect();
231
232 let unchanged: BTreeMap<_, _> = self
233 .unchanged
234 .into_iter()
235 .map(|identity| (identity.user_id().to_owned(), identity))
236 .collect();
237
238 (new, changed, unchanged)
239 }
240}
241
242#[derive(Debug, Clone, Default)]
243#[allow(missing_docs)]
244pub struct DeviceChanges {
245 pub new: Vec<DeviceData>,
246 pub changed: Vec<DeviceData>,
247 pub deleted: Vec<DeviceData>,
248}
249
250#[derive(Clone, Debug, Default)]
253pub struct DeviceUpdates {
254 pub new: BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceId, Device>>,
260 pub changed: BTreeMap<OwnedUserId, BTreeMap<OwnedDeviceId, Device>>,
262}
263
264#[derive(Clone, Debug, Default)]
267pub struct IdentityUpdates {
268 pub new: BTreeMap<OwnedUserId, UserIdentity>,
274 pub changed: BTreeMap<OwnedUserId, UserIdentity>,
276 pub unchanged: BTreeMap<OwnedUserId, UserIdentity>,
278}
279
280#[derive(Clone, Zeroize, ZeroizeOnDrop, Deserialize, Serialize)]
290#[serde(transparent)]
291pub struct BackupDecryptionKey {
292 pub(crate) inner: Box<[u8; BackupDecryptionKey::KEY_SIZE]>,
293}
294
295impl BackupDecryptionKey {
296 pub const KEY_SIZE: usize = 32;
298
299 #[allow(clippy::new_without_default)]
301 pub fn new() -> Self {
302 let mut rng = rand::rng();
303
304 let mut key = Box::new([0u8; Self::KEY_SIZE]);
305 rng.fill_bytes(key.as_mut_slice());
306
307 Self { inner: key }
308 }
309
310 pub fn to_base64(&self) -> String {
312 base64_encode(self.inner.as_slice())
313 }
314}
315
316#[cfg(not(tarpaulin_include))]
317impl std::fmt::Debug for BackupDecryptionKey {
318 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
319 f.debug_tuple("BackupDecryptionKey").field(&"...").finish()
320 }
321}
322
323#[derive(Clone, Zeroize, ZeroizeOnDrop, Deserialize, Serialize)]
328#[serde(transparent)]
329pub struct DehydratedDeviceKey {
330 pub(crate) inner: Box<[u8; DehydratedDeviceKey::KEY_SIZE]>,
331}
332
333impl DehydratedDeviceKey {
334 pub const KEY_SIZE: usize = 32;
336
337 #[allow(clippy::new_without_default)]
339 pub fn new() -> Self {
340 let mut rng = rand::rng();
341
342 let mut key = Box::new([0u8; Self::KEY_SIZE]);
343 rng.fill_bytes(key.as_mut_slice());
344
345 Self { inner: key }
346 }
347
348 pub fn from_slice(slice: &[u8]) -> Result<Self, DehydrationError> {
352 if slice.len() == 32 {
353 let mut key = Box::new([0u8; 32]);
354 key.copy_from_slice(slice);
355 Ok(DehydratedDeviceKey { inner: key })
356 } else {
357 Err(DehydrationError::PickleKeyLength(slice.len()))
358 }
359 }
360
361 pub fn from_bytes(raw_key: &[u8; 32]) -> Self {
363 let mut inner = Box::new([0u8; Self::KEY_SIZE]);
364 inner.copy_from_slice(raw_key);
365
366 Self { inner }
367 }
368
369 pub fn to_base64(&self) -> String {
371 base64_encode(self.inner.as_slice())
372 }
373}
374
375impl From<&[u8; 32]> for DehydratedDeviceKey {
376 fn from(value: &[u8; 32]) -> Self {
377 DehydratedDeviceKey { inner: Box::new(*value) }
378 }
379}
380
381impl From<DehydratedDeviceKey> for Vec<u8> {
382 fn from(key: DehydratedDeviceKey) -> Self {
383 key.inner.to_vec()
384 }
385}
386
387#[cfg(not(tarpaulin_include))]
388impl std::fmt::Debug for DehydratedDeviceKey {
389 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
390 f.debug_tuple("DehydratedDeviceKey").field(&"...").finish()
391 }
392}
393
394impl DeviceChanges {
395 pub fn extend(&mut self, other: DeviceChanges) {
397 self.new.extend(other.new);
398 self.changed.extend(other.changed);
399 self.deleted.extend(other.deleted);
400 }
401
402 pub fn is_empty(&self) -> bool {
404 self.new.is_empty() && self.changed.is_empty() && self.deleted.is_empty()
405 }
406}
407
408#[derive(Debug, Clone, Default)]
410pub struct RoomKeyCounts {
411 pub total: usize,
413 pub backed_up: usize,
415}
416
417#[derive(Default, Clone, Debug)]
419pub struct BackupKeys {
420 pub decryption_key: Option<BackupDecryptionKey>,
422 pub backup_version: Option<String>,
424}
425
426#[derive(Default, Zeroize, ZeroizeOnDrop)]
429pub struct CrossSigningKeyExport {
430 pub master_key: Option<String>,
432 pub self_signing_key: Option<String>,
434 pub user_signing_key: Option<String>,
436}
437
438#[cfg(not(tarpaulin_include))]
439impl std::fmt::Debug for CrossSigningKeyExport {
440 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
441 f.debug_struct("CrossSigningKeyExport")
442 .field("master_key", &self.master_key.is_some())
443 .field("self_signing_key", &self.self_signing_key.is_some())
444 .field("user_signing_key", &self.user_signing_key.is_some())
445 .finish_non_exhaustive()
446 }
447}
448
449#[derive(Clone, Copy, Debug, PartialEq, Eq)]
452pub(crate) enum UserKeyQueryResult {
453 WasPending,
454 WasNotPending,
455
456 TimeoutExpired,
458}
459
460#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
462pub struct RoomSettings {
463 pub algorithm: EventEncryptionAlgorithm,
465
466 #[cfg(feature = "experimental-encrypted-state-events")]
468 #[serde(default)]
469 pub encrypt_state_events: bool,
470
471 pub only_allow_trusted_devices: bool,
474
475 pub session_rotation_period: Option<Duration>,
478
479 pub session_rotation_period_messages: Option<usize>,
482}
483
484impl Default for RoomSettings {
485 fn default() -> Self {
486 Self {
487 algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
488 #[cfg(feature = "experimental-encrypted-state-events")]
489 encrypt_state_events: false,
490 only_allow_trusted_devices: false,
491 session_rotation_period: None,
492 session_rotation_period_messages: None,
493 }
494 }
495}
496
497#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
499pub struct RoomKeyInfo {
500 pub algorithm: EventEncryptionAlgorithm,
505
506 pub room_id: OwnedRoomId,
508
509 pub sender_key: Curve25519PublicKey,
511
512 pub session_id: String,
514}
515
516impl From<&InboundGroupSession> for RoomKeyInfo {
517 fn from(group_session: &InboundGroupSession) -> Self {
518 RoomKeyInfo {
519 algorithm: group_session.algorithm().clone(),
520 room_id: group_session.room_id().to_owned(),
521 sender_key: group_session.sender_key(),
522 session_id: group_session.session_id().to_owned(),
523 }
524 }
525}
526
527#[derive(Clone, Debug, Deserialize, Serialize)]
529pub struct RoomKeyWithheldInfo {
530 pub room_id: OwnedRoomId,
532
533 pub session_id: String,
535
536 pub withheld_event: RoomKeyWithheldEntry,
541}
542
543#[derive(Clone, Debug, Serialize, Deserialize)]
546pub struct RoomKeyWithheldEntry {
547 pub sender: OwnedUserId,
552 pub content: RoomKeyWithheldContent,
555}
556
557impl From<RoomKeyWithheldEvent> for RoomKeyWithheldEntry {
558 fn from(value: RoomKeyWithheldEvent) -> Self {
559 Self { sender: value.sender, content: value.content }
560 }
561}
562
563#[derive(Debug, Clone)]
571pub struct RoomKeyBundleInfo {
572 pub sender: OwnedUserId,
574
575 pub sender_key: Curve25519PublicKey,
577
578 pub room_id: OwnedRoomId,
580}
581
582impl From<&StoredRoomKeyBundleData> for RoomKeyBundleInfo {
583 fn from(value: &StoredRoomKeyBundleData) -> Self {
584 let StoredRoomKeyBundleData { sender_user, sender_data: _, bundle_data, sender_key } =
585 value;
586 let sender_key = *sender_key;
587
588 Self { sender: sender_user.clone(), room_id: bundle_data.room_id.clone(), sender_key }
589 }
590}
591
592#[derive(Debug, Clone, Serialize, Deserialize)]
595pub struct RoomPendingKeyBundleDetails {
596 pub room_id: OwnedRoomId,
600
601 pub invite_accepted_at: MilliSecondsSinceUnixEpoch,
604
605 pub inviter: OwnedUserId,
607}