Skip to content

Commit b8cbd6c

Browse files
authored
Merge pull request #5654 from matrix-org/rav/identity_test_cleanups
crypto: Simplify `PrivateCrossSigningIdentity::with_account`
2 parents 5ccbc1c + 0002ea4 commit b8cbd6c

File tree

7 files changed

+59
-49
lines changed

7 files changed

+59
-49
lines changed

crates/matrix-sdk-crypto/src/identities/room_identity_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ mod tests {
11221122
let account = Account::with_device_id(user_id, &device_id);
11231123

11241124
let private_identity =
1125-
Arc::new(Mutex::new(PrivateCrossSigningIdentity::with_account(&account).await.0));
1125+
Arc::new(Mutex::new(PrivateCrossSigningIdentity::for_account(&account)));
11261126

11271127
let other_user_identity_data =
11281128
OtherUserIdentityData::from_private(&*private_identity.lock().await).await;
@@ -1162,7 +1162,7 @@ mod tests {
11621162
let account = Account::with_device_id(user_id, &device_id);
11631163

11641164
let private_identity =
1165-
Arc::new(Mutex::new(PrivateCrossSigningIdentity::with_account(&account).await.0));
1165+
Arc::new(Mutex::new(PrivateCrossSigningIdentity::for_account(&account)));
11661166

11671167
let own_user_identity_data =
11681168
OwnUserIdentityData::from_private(&*private_identity.lock().await).await;

crates/matrix-sdk-crypto/src/identities/user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ pub(crate) mod tests {
16411641
let (_, device) = device(&response);
16421642

16431643
let account = Account::with_device_id(device.user_id(), device.device_id());
1644-
let (identity, _, _) = PrivateCrossSigningIdentity::with_account(&account).await;
1644+
let identity = PrivateCrossSigningIdentity::for_account(&account);
16451645

16461646
let id = Arc::new(Mutex::new(identity.clone()));
16471647

crates/matrix-sdk-crypto/src/olm/account.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,11 +806,30 @@ impl Account {
806806
device_keys
807807
}
808808

809-
/// Bootstrap Cross-Signing
809+
/// Bootstraps cross-signing, generating new cross-signing keys and creating
810+
/// the necessary upload and signature requests.
811+
///
812+
/// # Returns
813+
/// A tuple containing:
814+
/// - [`PrivateCrossSigningIdentity`]: The newly-generated cross-signing
815+
/// identity (including a signature from this device).
816+
/// - [`UploadSigningKeysRequest`]: The request to upload the
817+
/// newly-generated cross-signing keys to the server.
818+
/// - [`SignatureUploadRequest`]: The request to upload the signature of
819+
/// this device to the server.
810820
pub async fn bootstrap_cross_signing(
811821
&self,
812822
) -> (PrivateCrossSigningIdentity, UploadSigningKeysRequest, SignatureUploadRequest) {
813-
PrivateCrossSigningIdentity::with_account(self).await
823+
let identity = PrivateCrossSigningIdentity::for_account(self);
824+
825+
let signature_request = identity
826+
.sign_account(self.static_data())
827+
.await
828+
.expect("Can't sign own device with new cross signing keys");
829+
830+
let upload_request = identity.as_upload_request().await;
831+
832+
(identity, upload_request, signature_request)
814833
}
815834

816835
/// Sign the given CrossSigning Key in place

crates/matrix-sdk-crypto/src/olm/group_sessions/sender_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ mod tests {
797797
async fn test_from_device_for_verified_user() {
798798
let alice_account =
799799
Account::with_device_id(user_id!("@alice:example.com"), device_id!("ALICE_DEVICE"));
800-
let alice_identity = PrivateCrossSigningIdentity::with_account(&alice_account).await.0;
800+
let alice_identity = PrivateCrossSigningIdentity::for_account(&alice_account);
801801

802802
let bob_identity =
803803
PrivateCrossSigningIdentity::new(user_id!("@bob:example.com").to_owned());

crates/matrix-sdk-crypto/src/olm/group_sessions/sender_data_finder.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,8 @@ mod tests {
912912
) -> Self {
913913
let account = Account::with_device_id(user_id, device_id);
914914
let user_id = user_id.to_owned();
915-
let private_identity = Arc::new(Mutex::new(create_private_identity(&account).await));
915+
let private_identity =
916+
Arc::new(Mutex::new(PrivateCrossSigningIdentity::for_account(&account)));
916917

917918
let user_identity =
918919
create_user_identity(&*private_identity.lock().await, is_me, is_verified, signer)
@@ -978,10 +979,6 @@ mod tests {
978979
}
979980
}
980981

981-
async fn create_private_identity(account: &Account) -> PrivateCrossSigningIdentity {
982-
PrivateCrossSigningIdentity::with_account(account).await.0
983-
}
984-
985982
fn create_room_key_event(
986983
sender: &UserId,
987984
receiver: &UserId,

crates/matrix-sdk-crypto/src/olm/signing/mod.rs

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -499,37 +499,6 @@ impl PrivateCrossSigningIdentity {
499499
.sign(message))
500500
}
501501

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-
533502
fn new_helper(user_id: &UserId, master: MasterSigning) -> Self {
534503
let (user, self_signing) = master.new_subkeys();
535504

@@ -551,6 +520,32 @@ impl PrivateCrossSigningIdentity {
551520
Self::new_helper(&user_id, master)
552521
}
553522

523+
/**
524+
* Create a new private identity, suitable for the given [`Account`].
525+
*
526+
* The identity will be created with a fresh set of cross-signing keys.
527+
* The master key will be signed by the `OlmAccount` (i.e. the device).
528+
* The user-signing and self-signing keys will be signed by the
529+
* master key.
530+
*
531+
* Note that after creating a new identity, the device will need to be
532+
* signed by the self-signing key. This can be done via
533+
* [`PrivateCrossSigningIdentity::sign_account`].
534+
*
535+
* # Arguments
536+
*
537+
* * `account` - The Olm account that is creating the new identity.
538+
*/
539+
pub(crate) fn for_account(account: &Account) -> PrivateCrossSigningIdentity {
540+
let mut master = MasterSigning::new(account.user_id().into());
541+
542+
account
543+
.sign_cross_signing_key(master.public_key_mut().as_mut())
544+
.expect("Can't sign our freshly created master key with our account");
545+
546+
Self::new_helper(account.user_id(), master)
547+
}
548+
554549
#[cfg(any(test, feature = "testing"))]
555550
#[allow(dead_code)]
556551
/// Testing helper to reset this CrossSigning with a fresh one using the
@@ -726,7 +721,7 @@ mod tests {
726721
#[async_test]
727722
async fn test_private_identity_signed_by_account() {
728723
let account = Account::with_device_id(user_id(), device_id!("DEVICEID"));
729-
let (identity, _, _) = PrivateCrossSigningIdentity::with_account(&account).await;
724+
let identity = PrivateCrossSigningIdentity::for_account(&account);
730725
let master = identity.master_key.lock().await;
731726
let master = master.as_ref().unwrap();
732727

@@ -749,7 +744,7 @@ mod tests {
749744
#[async_test]
750745
async fn test_sign_device() {
751746
let account = Account::with_device_id(user_id(), device_id!("DEVICEID"));
752-
let (identity, _, _) = PrivateCrossSigningIdentity::with_account(&account).await;
747+
let identity = PrivateCrossSigningIdentity::for_account(&account);
753748

754749
let mut device = DeviceData::from_account(&account);
755750
let self_signing = identity.self_signing_key.lock().await;
@@ -766,11 +761,11 @@ mod tests {
766761
#[async_test]
767762
async fn test_sign_user_identity() {
768763
let account = Account::with_device_id(user_id(), device_id!("DEVICEID"));
769-
let (identity, _, _) = PrivateCrossSigningIdentity::with_account(&account).await;
764+
let identity = PrivateCrossSigningIdentity::for_account(&account);
770765

771766
let bob_account =
772767
Account::with_device_id(user_id!("@bob:localhost"), device_id!("DEVICEID"));
773-
let (bob_private, _, _) = PrivateCrossSigningIdentity::with_account(&bob_account).await;
768+
let bob_private = PrivateCrossSigningIdentity::for_account(&bob_account);
774769
let mut bob_public = OtherUserIdentityData::from_private(&bob_private).await;
775770

776771
let user_signing = identity.user_signing_key.lock().await;

crates/matrix-sdk-crypto/src/verification/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -830,13 +830,12 @@ pub(crate) mod tests {
830830
pub(crate) async fn setup_stores() -> (Account, VerificationStore, Account, VerificationStore) {
831831
let alice = Account::with_device_id(alice_id(), alice_device_id());
832832
let alice_store = MemoryStore::new();
833-
let (alice_private_identity, _, _) =
834-
PrivateCrossSigningIdentity::with_account(&alice).await;
833+
let alice_private_identity = PrivateCrossSigningIdentity::for_account(&alice);
835834
let alice_private_identity = Mutex::new(alice_private_identity);
836835

837836
let bob = Account::with_device_id(bob_id(), bob_device_id());
838837
let bob_store = MemoryStore::new();
839-
let (bob_private_identity, _, _) = PrivateCrossSigningIdentity::with_account(&bob).await;
838+
let bob_private_identity = PrivateCrossSigningIdentity::for_account(&bob);
840839
let bob_private_identity = Mutex::new(bob_private_identity);
841840

842841
let alice_public_identity =

0 commit comments

Comments
 (0)