Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AccountManager::linked_devices #300

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions libsignal-service/src/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
};
Expand Down Expand Up @@ -339,6 +339,35 @@ impl<Service: PushService> AccountManager<Service> {
Ok(())
}

pub async fn linked_devices(
&mut self,
aci_identity_store: &dyn IdentityKeyStore,
) -> Result<Vec<DeviceInfo>, 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,
Expand Down Expand Up @@ -841,6 +870,15 @@ pub fn encrypt_device_name<R: rand::Rng + rand::CryptoRng>(
Ok(device_name)
}

fn decrypt_device_name_from_device_info(
string: &str,
aci: &IdentityKeyPair,
) -> Result<String, ServiceError> {
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,
Expand Down
Loading