From e873d07ad4c8e2cbabba056f98b29d4b6bc282c2 Mon Sep 17 00:00:00 2001 From: Schmiddiii Date: Mon, 3 Jun 2024 19:31:25 +0200 Subject: [PATCH] Add AccountManager::linked_devices --- libsignal-service/src/account_manager.rs | 46 +++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/libsignal-service/src/account_manager.rs b/libsignal-service/src/account_manager.rs index e151b476b..cf1628222 100644 --- a/libsignal-service/src/account_manager.rs +++ b/libsignal-service/src/account_manager.rs @@ -8,9 +8,9 @@ use aes::cipher::{KeyIvInit, StreamCipher as _}; use hmac::digest::Output; use hmac::{Hmac, Mac}; use libsignal_protocol::{ - kem, GenericSignedPreKey, IdentityKey, IdentityKeyStore, KeyPair, - KyberPreKeyRecord, PrivateKey, ProtocolStore, PublicKey, SenderKeyStore, - SignedPreKeyRecord, + kem, GenericSignedPreKey, IdentityKey, IdentityKeyPair, IdentityKeyStore, + KeyPair, KyberPreKeyRecord, PrivateKey, ProtocolStore, PublicKey, + SenderKeyStore, SignedPreKeyRecord, }; use prost::Message; use serde::{Deserialize, Serialize}; @@ -28,7 +28,7 @@ use crate::proto::sync_message::PniChangeNumber; use crate::proto::{DeviceName, SyncMessage}; use crate::provisioning::generate_registration_id; use crate::push_service::{ - AvatarWrite, DeviceActivationRequest, RecaptchaAttributes, + AvatarWrite, DeviceActivationRequest, DeviceInfo, RecaptchaAttributes, RegistrationMethod, ServiceIdType, VerifyAccountResponse, DEFAULT_DEVICE_ID, }; @@ -339,6 +339,35 @@ impl AccountManager { Ok(()) } + pub async fn linked_devices( + &mut self, + aci_identity_store: &dyn IdentityKeyStore, + ) -> Result, ServiceError> { + let device_infos = self.service.devices().await?; + let aci_identity_keypair = + aci_identity_store.get_identity_key_pair().await?; + + device_infos + .into_iter() + .map(|i| { + Ok(DeviceInfo { + id: i.id, + name: i + .name + .map(|s| { + decrypt_device_name_from_device_info( + &s, + &aci_identity_keypair, + ) + }) + .transpose()?, + created: i.created, + last_seen: i.last_seen, + }) + }) + .collect() + } + pub async fn register_account< R: rand::Rng + rand::CryptoRng, Aci: PreKeysStore + IdentityKeyStore, @@ -841,6 +870,15 @@ pub fn encrypt_device_name( Ok(device_name) } +fn decrypt_device_name_from_device_info( + string: &str, + aci: &IdentityKeyPair, +) -> Result { + let data = BASE64_RELAXED.decode(string)?; + let name = DeviceName::decode(&*data)?; + Ok(crate::decrypt_device_name(&aci.private_key(), &name)?) +} + pub fn decrypt_device_name( private_key: &PrivateKey, device_name: &DeviceName,