matrix_sdk_crypto/olm/signing/
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
15mod pk_signing;
16
17use std::sync::{
18    atomic::{AtomicBool, Ordering},
19    Arc,
20};
21
22pub use pk_signing::{MasterSigning, PickledSignings, SelfSigning, SigningError, UserSigning};
23use ruma::{
24    api::client::keys::upload_signatures::v3::{Request as SignatureUploadRequest, SignedKeys},
25    events::secret::request::SecretName,
26    DeviceKeyAlgorithm, DeviceKeyId, OwnedDeviceId, OwnedDeviceKeyId, OwnedUserId, UserId,
27};
28use serde::{Deserialize, Serialize};
29use tokio::sync::Mutex;
30use vodozemac::Ed25519Signature;
31
32use super::StaticAccountData;
33use crate::{
34    error::SignatureError,
35    store::SecretImportError,
36    types::{
37        requests::UploadSigningKeysRequest, DeviceKeys, MasterPubkey, SelfSigningPubkey,
38        UserSigningPubkey,
39    },
40    Account, DeviceData, OtherUserIdentityData, OwnUserIdentity, OwnUserIdentityData,
41};
42
43/// Private cross signing identity.
44///
45/// This object holds the private and public ed25519 key triplet that is used
46/// for cross signing.
47///
48/// The object might be completely empty or have only some of the key pairs
49/// available.
50///
51/// It can be used to sign devices or other identities.
52#[derive(Clone, Debug)]
53pub struct PrivateCrossSigningIdentity {
54    user_id: OwnedUserId,
55    shared: Arc<AtomicBool>,
56    pub(crate) master_key: Arc<Mutex<Option<MasterSigning>>>,
57    pub(crate) user_signing_key: Arc<Mutex<Option<UserSigning>>>,
58    pub(crate) self_signing_key: Arc<Mutex<Option<SelfSigning>>>,
59}
60
61/// A struct containing information on whether any of our cross-signing keys
62/// differ from the public keys that exist on the server.
63#[derive(Debug, Clone)]
64pub struct DiffResult {
65    /// Does the master key differ?
66    master_differs: bool,
67    /// Does the self-signing key differ?
68    self_signing_differs: bool,
69    /// Does the user-signing key differ?
70    user_signing_differs: bool,
71}
72
73impl DiffResult {
74    /// Do any of the cross-signing keys differ?
75    pub fn any_differ(&self) -> bool {
76        self.master_differs || self.self_signing_differs || self.user_signing_differs
77    }
78
79    /// Do none of the cross-signing keys differ?
80    pub fn none_differ(&self) -> bool {
81        !self.master_differs && !self.self_signing_differs && !self.user_signing_differs
82    }
83}
84
85/// The pickled version of a `PrivateCrossSigningIdentity`.
86///
87/// Can be used to store the identity.
88#[derive(Serialize, Deserialize)]
89#[allow(missing_debug_implementations)]
90pub struct PickledCrossSigningIdentity {
91    /// The user id of the identity owner.
92    pub user_id: OwnedUserId,
93    /// Have the public keys of the identity been shared.
94    pub shared: bool,
95    /// The pickled signing keys
96    pub keys: PickledSignings,
97}
98
99/// Struct representing the state of our private cross signing keys, it shows
100/// which private cross signing keys we have locally stored.
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct CrossSigningStatus {
103    /// Do we have the master key.
104    pub has_master: bool,
105    /// Do we have the self signing key, this one is necessary to sign our own
106    /// devices.
107    pub has_self_signing: bool,
108    /// Do we have the user signing key, this one is necessary to sign other
109    /// users.
110    pub has_user_signing: bool,
111}
112
113impl CrossSigningStatus {
114    /// Do we have all the cross signing keys locally stored.
115    pub fn is_complete(&self) -> bool {
116        self.has_master && self.has_user_signing && self.has_self_signing
117    }
118}
119
120impl PrivateCrossSigningIdentity {
121    /// Get the user id that this identity belongs to.
122    pub fn user_id(&self) -> &UserId {
123        &self.user_id
124    }
125
126    /// Is the identity empty.
127    ///
128    /// An empty identity doesn't contain any private keys.
129    ///
130    /// It is usual for the identity not to contain the master key since the
131    /// master key is only needed to sign the subkeys.
132    ///
133    /// An empty identity indicates that either no identity was created for this
134    /// use or that another device created it and hasn't shared it yet with us.
135    pub async fn is_empty(&self) -> bool {
136        let has_master = self.master_key.lock().await.is_some();
137        let has_user = self.user_signing_key.lock().await.is_some();
138        let has_self = self.self_signing_key.lock().await.is_some();
139
140        !(has_master && has_user && has_self)
141    }
142
143    /// Get the key ID of the master key.
144    pub async fn master_key_id(&self) -> Option<OwnedDeviceKeyId> {
145        let master_key = self.master_public_key().await?.get_first_key()?.to_base64();
146        let master_key = OwnedDeviceId::from(master_key);
147
148        Some(DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, &master_key))
149    }
150
151    /// Can we sign our own devices, i.e. do we have a self signing key.
152    pub async fn can_sign_devices(&self) -> bool {
153        self.self_signing_key.lock().await.is_some()
154    }
155
156    /// Can we sign other users, i.e. do we have a user signing key.
157    pub async fn can_sign_users(&self) -> bool {
158        self.user_signing_key.lock().await.is_some()
159    }
160
161    /// Do we have the master key.
162    pub async fn has_master_key(&self) -> bool {
163        self.master_key.lock().await.is_some()
164    }
165
166    /// Get the status of our private cross signing keys, i.e. if we have the
167    /// master key and the subkeys.
168    pub async fn status(&self) -> CrossSigningStatus {
169        CrossSigningStatus {
170            has_master: self.has_master_key().await,
171            has_self_signing: self.can_sign_devices().await,
172            has_user_signing: self.can_sign_users().await,
173        }
174    }
175
176    /// Get the public part of the master key, if we have one.
177    pub async fn master_public_key(&self) -> Option<MasterPubkey> {
178        self.master_key.lock().await.as_ref().map(|m| m.public_key().to_owned())
179    }
180
181    /// Get the public part of the self-signing key, if we have one.
182    pub async fn self_signing_public_key(&self) -> Option<SelfSigningPubkey> {
183        self.self_signing_key.lock().await.as_ref().map(|k| k.public_key().to_owned())
184    }
185
186    /// Get the public part of the user-signing key, if we have one.
187    pub async fn user_signing_public_key(&self) -> Option<UserSigningPubkey> {
188        self.user_signing_key.lock().await.as_ref().map(|k| k.public_key().to_owned())
189    }
190
191    /// Export the seed of the private cross signing key
192    ///
193    /// The exported seed will be encoded as unpadded base64.
194    ///
195    /// # Arguments
196    ///
197    /// * `secret_name` - The type of the cross signing key that should be
198    ///   exported.
199    pub async fn export_secret(&self, secret_name: &SecretName) -> Option<String> {
200        match secret_name {
201            SecretName::CrossSigningMasterKey => {
202                self.master_key.lock().await.as_ref().map(|m| m.export_seed())
203            }
204            SecretName::CrossSigningUserSigningKey => {
205                self.user_signing_key.lock().await.as_ref().map(|m| m.export_seed())
206            }
207            SecretName::CrossSigningSelfSigningKey => {
208                self.self_signing_key.lock().await.as_ref().map(|m| m.export_seed())
209            }
210            _ => None,
211        }
212    }
213
214    pub(crate) async fn import_secret(
215        &self,
216        public_identity: OwnUserIdentity,
217        secret_name: &SecretName,
218        seed: &str,
219    ) -> Result<(), SecretImportError> {
220        let (master, self_signing, user_signing) = match secret_name {
221            SecretName::CrossSigningMasterKey => (Some(seed), None, None),
222            SecretName::CrossSigningSelfSigningKey => (None, Some(seed), None),
223            SecretName::CrossSigningUserSigningKey => (None, None, Some(seed)),
224            _ => return Ok(()),
225        };
226
227        self.import_secrets(public_identity, master, self_signing, user_signing).await
228    }
229
230    pub(crate) async fn import_secrets(
231        &self,
232        public_identity: OwnUserIdentity,
233        master_key: Option<&str>,
234        self_signing_key: Option<&str>,
235        user_signing_key: Option<&str>,
236    ) -> Result<(), SecretImportError> {
237        let master = if let Some(master_key) = master_key {
238            let master = MasterSigning::from_base64(self.user_id().to_owned(), master_key)?;
239
240            if public_identity.master_key() == master.public_key() {
241                Some(master)
242            } else {
243                return Err(SecretImportError::MismatchedPublicKeys);
244            }
245        } else {
246            None
247        };
248
249        let user_signing = if let Some(user_signing_key) = user_signing_key {
250            let subkey = UserSigning::from_base64(self.user_id().to_owned(), user_signing_key)?;
251
252            if public_identity.user_signing_key() == subkey.public_key() {
253                Ok(Some(subkey))
254            } else {
255                Err(SecretImportError::MismatchedPublicKeys)
256            }
257        } else {
258            Ok(None)
259        }?;
260
261        let self_signing = if let Some(self_signing_key) = self_signing_key {
262            let subkey = SelfSigning::from_base64(self.user_id().to_owned(), self_signing_key)?;
263
264            if public_identity.self_signing_key() == subkey.public_key() {
265                Ok(Some(subkey))
266            } else {
267                Err(SecretImportError::MismatchedPublicKeys)
268            }
269        } else {
270            Ok(None)
271        }?;
272
273        if let Some(master) = master {
274            *self.master_key.lock().await = Some(master);
275        }
276
277        if let Some(self_signing) = self_signing {
278            *self.self_signing_key.lock().await = Some(self_signing);
279        }
280
281        if let Some(user_signing) = user_signing {
282            *self.user_signing_key.lock().await = Some(user_signing);
283        }
284
285        Ok(())
286    }
287
288    /// Import the private parts of the cross signing keys into this identity.
289    ///
290    /// The private parts should be unexpanded Ed25519 keys encoded as a base64
291    /// string.
292    ///
293    /// *Note*: This method won't check if the public keys match the public
294    /// keys present on the server.
295    pub async fn import_secrets_unchecked(
296        &self,
297        master_key: Option<&str>,
298        self_signing_key: Option<&str>,
299        user_signing_key: Option<&str>,
300    ) -> Result<(), SecretImportError> {
301        if let Some(master_key) = master_key {
302            let master = MasterSigning::from_base64(self.user_id().to_owned(), master_key)?;
303            *self.master_key.lock().await = Some(master);
304        }
305
306        if let Some(user_signing_key) = user_signing_key {
307            let subkey = UserSigning::from_base64(self.user_id().to_owned(), user_signing_key)?;
308            *self.user_signing_key.lock().await = Some(subkey);
309        };
310
311        if let Some(self_signing_key) = self_signing_key {
312            let subkey = SelfSigning::from_base64(self.user_id().to_owned(), self_signing_key)?;
313            *self.self_signing_key.lock().await = Some(subkey);
314        }
315
316        Ok(())
317    }
318
319    /// Remove our private cross signing key if the public keys differ from
320    /// what's found in the [`OwnUserIdentityData`].
321    pub(crate) async fn clear_if_differs(
322        &self,
323        public_identity: &OwnUserIdentityData,
324    ) -> DiffResult {
325        let result = self.get_public_identity_diff(public_identity).await;
326
327        if result.master_differs {
328            *self.master_key.lock().await = None;
329        }
330
331        if result.user_signing_differs {
332            *self.user_signing_key.lock().await = None;
333        }
334
335        if result.self_signing_differs {
336            *self.self_signing_key.lock().await = None;
337        }
338
339        result
340    }
341
342    pub(crate) async fn get_public_identity_diff(
343        &self,
344        public_identity: &OwnUserIdentityData,
345    ) -> DiffResult {
346        let master_differs = self
347            .master_public_key()
348            .await
349            .is_some_and(|master| &master != public_identity.master_key());
350
351        let user_signing_differs = self
352            .user_signing_public_key()
353            .await
354            .is_some_and(|subkey| &subkey != public_identity.user_signing_key());
355
356        let self_signing_differs = self
357            .self_signing_public_key()
358            .await
359            .is_some_and(|subkey| &subkey != public_identity.self_signing_key());
360
361        DiffResult { master_differs, user_signing_differs, self_signing_differs }
362    }
363
364    /// Get the names of the secrets we are missing.
365    pub(crate) async fn get_missing_secrets(&self) -> Vec<SecretName> {
366        let mut missing = Vec::new();
367
368        if !self.has_master_key().await {
369            missing.push(SecretName::CrossSigningMasterKey);
370        }
371
372        if !self.can_sign_devices().await {
373            missing.push(SecretName::CrossSigningSelfSigningKey);
374        }
375
376        if !self.can_sign_users().await {
377            missing.push(SecretName::CrossSigningUserSigningKey);
378        }
379
380        missing
381    }
382
383    /// Create a new empty identity.
384    pub fn empty(user_id: &UserId) -> Self {
385        Self {
386            user_id: user_id.into(),
387            shared: Arc::new(AtomicBool::new(false)),
388            master_key: Arc::new(Mutex::new(None)),
389            self_signing_key: Arc::new(Mutex::new(None)),
390            user_signing_key: Arc::new(Mutex::new(None)),
391        }
392    }
393
394    async fn public_keys(
395        &self,
396    ) -> Result<(MasterPubkey, SelfSigningPubkey, UserSigningPubkey), SignatureError> {
397        let master_private_key = self.master_key.lock().await;
398        let master_private_key =
399            master_private_key.as_ref().ok_or(SignatureError::MissingSigningKey)?;
400        let self_signing_private_key = self.self_signing_key.lock().await;
401        let self_signing_private_key =
402            self_signing_private_key.as_ref().ok_or(SignatureError::MissingSigningKey)?;
403        let user_signing_private_key = self.user_signing_key.lock().await;
404        let user_signing_private_key =
405            user_signing_private_key.as_ref().ok_or(SignatureError::MissingSigningKey)?;
406
407        let mut master = master_private_key.public_key().to_owned();
408        let mut self_signing = self_signing_private_key.public_key().to_owned();
409        let mut user_signing = user_signing_private_key.public_key().to_owned();
410
411        master_private_key.sign_subkey(master.as_mut());
412        master_private_key.sign_subkey(self_signing.as_mut());
413        master_private_key.sign_subkey(user_signing.as_mut());
414
415        Ok((master, self_signing, user_signing))
416    }
417
418    pub(crate) async fn to_public_identity(&self) -> Result<OwnUserIdentityData, SignatureError> {
419        let (master, self_signing, user_signing) = self.public_keys().await?;
420
421        let identity = OwnUserIdentityData::new(master, self_signing, user_signing)?;
422        identity.mark_as_verified();
423
424        Ok(identity)
425    }
426
427    /// Sign the given public user identity with this private identity.
428    pub(crate) async fn sign_user(
429        &self,
430        user_identity: &OtherUserIdentityData,
431    ) -> Result<SignatureUploadRequest, SignatureError> {
432        let master_key = self
433            .user_signing_key
434            .lock()
435            .await
436            .as_ref()
437            .ok_or(SignatureError::MissingSigningKey)?
438            .sign_user(user_identity)?;
439
440        let mut user_signed_keys = SignedKeys::new();
441        user_signed_keys.add_cross_signing_keys(
442            user_identity
443                .master_key()
444                .get_first_key()
445                .ok_or(SignatureError::MissingSigningKey)?
446                .to_base64()
447                .into(),
448            master_key.to_raw(),
449        );
450
451        let signed_keys = [(user_identity.user_id().to_owned(), user_signed_keys)].into();
452        Ok(SignatureUploadRequest::new(signed_keys))
453    }
454
455    /// Sign the given device keys with this identity.
456    pub(crate) async fn sign_device(
457        &self,
458        device: &DeviceData,
459    ) -> Result<SignatureUploadRequest, SignatureError> {
460        let mut device_keys = device.as_device_keys().to_owned();
461        device_keys.signatures.clear();
462        self.sign_device_keys(&mut device_keys).await
463    }
464
465    /// Sign an Olm account with this private identity.
466    pub(crate) async fn sign_account(
467        &self,
468        account: &StaticAccountData,
469    ) -> Result<SignatureUploadRequest, SignatureError> {
470        let mut device_keys = account.unsigned_device_keys();
471        self.sign_device_keys(&mut device_keys).await
472    }
473
474    pub(crate) async fn sign_device_keys(
475        &self,
476        device_keys: &mut DeviceKeys,
477    ) -> Result<SignatureUploadRequest, SignatureError> {
478        self.self_signing_key
479            .lock()
480            .await
481            .as_ref()
482            .ok_or(SignatureError::MissingSigningKey)?
483            .sign_device(device_keys)?;
484
485        let mut user_signed_keys = SignedKeys::new();
486        user_signed_keys.add_device_keys(device_keys.device_id.clone(), device_keys.to_raw());
487
488        let signed_keys = [((*self.user_id).to_owned(), user_signed_keys)].into();
489        Ok(SignatureUploadRequest::new(signed_keys))
490    }
491
492    pub(crate) async fn sign(&self, message: &str) -> Result<Ed25519Signature, SignatureError> {
493        Ok(self
494            .master_key
495            .lock()
496            .await
497            .as_ref()
498            .ok_or(SignatureError::MissingSigningKey)?
499            .sign(message))
500    }
501
502    /// Create a new identity for the given Olm Account.
503    ///
504    /// Returns the new identity, the upload signing keys request and a
505    /// signature upload request that contains the signature of the account
506    /// signed by the self signing key.
507    ///
508    /// # Arguments
509    ///
510    /// * `account` - The Olm account that is creating the new identity. The
511    ///   account will sign the master key and the self signing key will sign
512    ///   the account.
513    pub(crate) async fn with_account(
514        account: &Account,
515    ) -> (Self, UploadSigningKeysRequest, SignatureUploadRequest) {
516        let mut master = MasterSigning::new(account.user_id().into());
517
518        account
519            .sign_cross_signing_key(master.public_key_mut().as_mut())
520            .expect("Can't sign our freshly created master key with our account");
521
522        let identity = Self::new_helper(account.user_id(), master);
523        let signature_request = identity
524            .sign_account(account.static_data())
525            .await
526            .expect("Can't sign own device with new cross signing keys");
527
528        let request = identity.as_upload_request().await;
529
530        (identity, request, signature_request)
531    }
532
533    fn new_helper(user_id: &UserId, master: MasterSigning) -> Self {
534        let (user, self_signing) = master.new_subkeys();
535
536        Self {
537            user_id: user_id.into(),
538            shared: Arc::new(AtomicBool::new(false)),
539            master_key: Arc::new(Mutex::new(Some(master))),
540            self_signing_key: Arc::new(Mutex::new(Some(self_signing))),
541            user_signing_key: Arc::new(Mutex::new(Some(user))),
542        }
543    }
544
545    /// Create a new cross signing identity without signing the device that
546    /// created it.
547    #[cfg(any(test, feature = "testing"))]
548    #[allow(dead_code)]
549    pub fn new(user_id: OwnedUserId) -> Self {
550        let master = MasterSigning::new(user_id.to_owned());
551        Self::new_helper(&user_id, master)
552    }
553
554    #[cfg(any(test, feature = "testing"))]
555    #[allow(dead_code)]
556    /// Testing helper to reset this CrossSigning with a fresh one using the
557    /// local identity
558    pub fn reset(&mut self) {
559        let new = Self::new(self.user_id().to_owned());
560        *self = new
561    }
562
563    /// Mark the identity as shared.
564    pub fn mark_as_shared(&self) {
565        self.shared.store(true, Ordering::SeqCst)
566    }
567
568    /// Has the identity been shared.
569    ///
570    /// A shared identity here means that the public keys of the identity have
571    /// been uploaded to the server.
572    pub fn shared(&self) -> bool {
573        self.shared.load(Ordering::SeqCst)
574    }
575
576    /// Store the cross signing identity as a pickle.
577    ///
578    /// # Arguments
579    ///
580    /// * `pickle_key` - The key that should be used to encrypt the signing
581    ///   object, must be 32 bytes long.
582    ///
583    /// # Panics
584    ///
585    /// This will panic if the provided pickle key isn't 32 bytes long.
586    pub async fn pickle(&self) -> PickledCrossSigningIdentity {
587        let master_key = self.master_key.lock().await.as_ref().map(|m| m.pickle());
588
589        let self_signing_key = self.self_signing_key.lock().await.as_ref().map(|m| m.pickle());
590
591        let user_signing_key = self.user_signing_key.lock().await.as_ref().map(|m| m.pickle());
592
593        let keys = PickledSignings { master_key, user_signing_key, self_signing_key };
594
595        PickledCrossSigningIdentity { user_id: self.user_id.clone(), shared: self.shared(), keys }
596    }
597
598    /// Restore the private cross signing identity from a pickle.
599    ///
600    /// # Panic
601    ///
602    /// Panics if the pickle_key isn't 32 bytes long.
603    pub fn from_pickle(pickle: PickledCrossSigningIdentity) -> Result<Self, SigningError> {
604        let keys = pickle.keys;
605
606        let master = keys.master_key.map(MasterSigning::from_pickle).transpose()?;
607        let self_signing = keys.self_signing_key.map(SelfSigning::from_pickle).transpose()?;
608        let user_signing = keys.user_signing_key.map(UserSigning::from_pickle).transpose()?;
609
610        Ok(Self {
611            user_id: (*pickle.user_id).into(),
612            shared: Arc::new(AtomicBool::from(pickle.shared)),
613            master_key: Arc::new(Mutex::new(master)),
614            self_signing_key: Arc::new(Mutex::new(self_signing)),
615            user_signing_key: Arc::new(Mutex::new(user_signing)),
616        })
617    }
618
619    /// Get the upload request that is needed to share the public keys of this
620    /// identity.
621    pub(crate) async fn as_upload_request(&self) -> UploadSigningKeysRequest {
622        let master_key =
623            self.master_key.lock().await.as_ref().map(|k| k.public_key().as_ref().clone());
624
625        let user_signing_key =
626            self.user_signing_key.lock().await.as_ref().map(|k| k.public_key().as_ref().clone());
627
628        let self_signing_key =
629            self.self_signing_key.lock().await.as_ref().map(|k| k.public_key().as_ref().clone());
630
631        UploadSigningKeysRequest { master_key, self_signing_key, user_signing_key }
632    }
633}
634
635#[cfg(test)]
636mod tests {
637    use std::sync::Arc;
638
639    use matrix_sdk_test::async_test;
640    use ruma::{device_id, user_id, CanonicalJsonValue, DeviceKeyAlgorithm, DeviceKeyId, UserId};
641    use serde_json::json;
642
643    use super::{pk_signing::Signing, PrivateCrossSigningIdentity};
644    use crate::{
645        identities::{DeviceData, OtherUserIdentityData},
646        olm::{Account, SignedJsonObject, VerifyJson},
647        types::Signatures,
648    };
649
650    fn user_id() -> &'static UserId {
651        user_id!("@example:localhost")
652    }
653
654    #[test]
655    fn test_signature_verification() {
656        let signing = Signing::new();
657        let user_id = user_id();
658        let key_id = DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, "DEVICEID".into());
659
660        let json = json!({
661            "hello": "world"
662        });
663
664        let canonicalized: CanonicalJsonValue = json.try_into().unwrap();
665        let canonicalized = canonicalized.to_string();
666
667        let signature = signing.sign(&canonicalized);
668        let mut signatures = Signatures::new();
669        signatures.add_signature(user_id.to_owned(), key_id.clone(), signature);
670
671        let public_key = signing.public_key();
672
673        public_key
674            .verify_canonicalized_json(user_id, &key_id, &signatures, &canonicalized)
675            .expect("The signature can be verified");
676    }
677
678    #[test]
679    fn test_pickling_signing() {
680        let signing = Signing::new();
681        let pickled = signing.pickle();
682
683        let unpickled = Signing::from_pickle(pickled).unwrap();
684
685        assert_eq!(signing.public_key(), unpickled.public_key());
686    }
687
688    #[async_test]
689    async fn test_private_identity_creation() {
690        let identity = PrivateCrossSigningIdentity::new(user_id().to_owned());
691
692        let master_key = identity.master_key.lock().await;
693        let master_key = master_key.as_ref().unwrap();
694
695        master_key
696            .public_key()
697            .verify_subkey(identity.self_signing_key.lock().await.as_ref().unwrap().public_key())
698            .unwrap();
699
700        master_key
701            .public_key()
702            .verify_subkey(identity.user_signing_key.lock().await.as_ref().unwrap().public_key())
703            .unwrap();
704    }
705
706    #[async_test]
707    async fn test_identity_pickling() {
708        let identity = PrivateCrossSigningIdentity::new(user_id().to_owned());
709
710        let pickled = identity.pickle().await;
711
712        let unpickled = PrivateCrossSigningIdentity::from_pickle(pickled).unwrap();
713
714        assert_eq!(identity.user_id, unpickled.user_id);
715        assert_eq!(&*identity.master_key.lock().await, &*unpickled.master_key.lock().await);
716        assert_eq!(
717            &*identity.user_signing_key.lock().await,
718            &*unpickled.user_signing_key.lock().await
719        );
720        assert_eq!(
721            &*identity.self_signing_key.lock().await,
722            &*unpickled.self_signing_key.lock().await
723        );
724    }
725
726    #[async_test]
727    async fn test_private_identity_signed_by_account() {
728        let account = Account::with_device_id(user_id(), device_id!("DEVICEID"));
729        let (identity, _, _) = PrivateCrossSigningIdentity::with_account(&account).await;
730        let master = identity.master_key.lock().await;
731        let master = master.as_ref().unwrap();
732
733        let public_key = master.public_key().as_ref();
734        let signatures = &public_key.signatures;
735        let canonical_json = public_key.to_canonical_json().unwrap();
736
737        account
738            .has_signed_raw(signatures, &canonical_json)
739            .expect("The account should have signed the master key");
740
741        master
742            .public_key()
743            .has_signed_raw(signatures, &canonical_json)
744            .expect("The master key should have self-signed");
745
746        assert!(!master.public_key().signatures().is_empty());
747    }
748
749    #[async_test]
750    async fn test_sign_device() {
751        let account = Account::with_device_id(user_id(), device_id!("DEVICEID"));
752        let (identity, _, _) = PrivateCrossSigningIdentity::with_account(&account).await;
753
754        let mut device = DeviceData::from_account(&account);
755        let self_signing = identity.self_signing_key.lock().await;
756        let self_signing = self_signing.as_ref().unwrap();
757
758        let mut device_keys = device.as_device_keys().to_owned();
759        self_signing.sign_device(&mut device_keys).unwrap();
760        device.update_device(&device_keys).unwrap();
761
762        let public_key = &self_signing.public_key();
763        public_key.verify_device(&device).unwrap()
764    }
765
766    #[async_test]
767    async fn test_sign_user_identity() {
768        let account = Account::with_device_id(user_id(), device_id!("DEVICEID"));
769        let (identity, _, _) = PrivateCrossSigningIdentity::with_account(&account).await;
770
771        let bob_account =
772            Account::with_device_id(user_id!("@bob:localhost"), device_id!("DEVICEID"));
773        let (bob_private, _, _) = PrivateCrossSigningIdentity::with_account(&bob_account).await;
774        let mut bob_public = OtherUserIdentityData::from_private(&bob_private).await;
775
776        let user_signing = identity.user_signing_key.lock().await;
777        let user_signing = user_signing.as_ref().unwrap();
778
779        let master = user_signing.sign_user(&bob_public).unwrap();
780
781        assert_eq!(
782            master.signatures.signature_count(),
783            1,
784            "We're only uploading our own signature"
785        );
786
787        bob_public.master_key = Arc::new(master.try_into().unwrap());
788
789        user_signing.public_key().verify_master_key(bob_public.master_key()).unwrap();
790    }
791}