matrix_sdk_crypto/identities/mod.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
15//! Collection of public identities used in Matrix.
16//!
17//! Matrix supports two main types of identities, a per-device identity and a
18//! per-user identity.
19//!
20//! ## Device
21//!
22//! Every E2EE capable Matrix client will create a new Olm account and upload
23//! the public keys of the Olm account to the server. This is represented as a
24//! [`DeviceData`] struct.
25//!
26//! Devices can have a local trust state which is needs to be saved in our
27//! `CryptoStore`, to avoid reference cycles a wrapper for the [`DeviceData`]
28//! exists which adds methods to manipulate the local trust state.
29//!
30//! ## User
31//!
32//! Cross-signing capable devices will upload 3 additional (master,
33//! self-signing, user-signing) public keys which represent the user identity
34//! owning all the devices. This is represented in two ways, as a `UserIdentity`
35//! for other users and as `OwnUserIdentity` for our own user.
36//!
37//! This is done because the server will only give us access to 2 of the 3
38//! additional public keys for other users, while it will give us access to all
39//! 3 for our own user.
40//!
41//! Both identity sets need to regularly fetched from the server using the
42//! `/keys/query` API call.
43pub(crate) mod device;
44pub(crate) mod manager;
45pub(crate) mod room_identity_state;
46pub(crate) mod user;
47
48use std::sync::{
49 atomic::{AtomicBool, Ordering},
50 Arc,
51};
52
53pub use device::{Device, DeviceData, LocalTrust, UserDevices};
54pub(crate) use manager::IdentityManager;
55use serde::{Deserialize, Deserializer, Serializer};
56pub use user::{
57 OtherUserIdentity, OtherUserIdentityData, OwnUserIdentity, OwnUserIdentityData, UserIdentity,
58 UserIdentityData,
59};
60
61// These methods are only here because Serialize and Deserialize don't seem to
62// be implemented for WASM.
63fn atomic_bool_serializer<S>(x: &AtomicBool, s: S) -> Result<S::Ok, S::Error>
64where
65 S: Serializer,
66{
67 let value = x.load(Ordering::SeqCst);
68 s.serialize_some(&value)
69}
70
71fn atomic_bool_deserializer<'de, D>(deserializer: D) -> Result<Arc<AtomicBool>, D::Error>
72where
73 D: Deserializer<'de>,
74{
75 let value = bool::deserialize(deserializer)?;
76 Ok(Arc::new(AtomicBool::new(value)))
77}