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 support for PNI fields and fix fetching (encrypted groups) #224

Merged
merged 9 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
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
13 changes: 9 additions & 4 deletions libsignal-service-actix/examples/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ async fn main() -> Result<(), Error> {
phone_number: _,
device_id: _,
registration_id: _,
uuid,
private_key: _,
public_key: _,
service_ids,
profile_key: _,
aci_private_key: _,
aci_public_key: _,
pni_private_key: _,
pni_public_key: _,
} => {
log::info!("successfully registered device {}", &uuid);
log::info!(
"successfully registered device {}",
&service_ids
);
// here you would store all of this data somehow to use it later!
},
}
Expand Down
31 changes: 19 additions & 12 deletions libsignal-service/src/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use aes::cipher::{NewCipher, StreamCipher};
use aes::Aes256Ctr;
use hmac::{Hmac, Mac};
use libsignal_protocol::{
IdentityKeyStore, KeyPair, PreKeyRecord, PreKeyStore, PrivateKey,
PublicKey, SignalProtocolError, SignedPreKeyRecord, SignedPreKeyStore,
IdentityKeyStore, KeyPair, PreKeyRecord, PrivateKey, ProtocolStore,
PublicKey, SignalProtocolError, SignedPreKeyRecord,
};
use serde::{Deserialize, Serialize};
use sha2::Sha256;
use zkgroup::profiles::ProfileKey;

use crate::push_service::{AvatarWrite, RecaptchaAttributes};
use crate::push_service::{AvatarWrite, RecaptchaAttributes, ServiceIdType};
use crate::ServiceAddress;
use crate::{
configuration::{Endpoint, ServiceCredentials},
Expand Down Expand Up @@ -84,17 +84,22 @@ impl<Service: PushService> AccountManager<Service> {
///
/// Returns the next pre-key offset and next signed pre-key offset as a tuple.
#[allow(clippy::too_many_arguments)]
pub async fn update_pre_key_bundle<R: rand::Rng + rand::CryptoRng>(
pub async fn update_pre_key_bundle<
R: rand::Rng + rand::CryptoRng,
P: ProtocolStore,
>(
&mut self,
identity_store: &dyn IdentityKeyStore,
pre_key_store: &mut dyn PreKeyStore,
signed_pre_key_store: &mut dyn SignedPreKeyStore,
protocol_store: &mut P,
csprng: &mut R,
pre_keys_offset_id: u32,
next_signed_pre_key_id: u32,
use_last_resort_key: bool,
) -> Result<(u32, u32), ServiceError> {
let prekey_count = match self.service.get_pre_key_status().await {
let prekey_count = match self
.service
.get_pre_key_status(ServiceIdType::AccountIdentity)
.await
{
Ok(status) => status.count,
Err(ServiceError::Unauthorized) => {
log::info!("Got Unauthorized when fetching pre-key status. Assuming first installment.");
Expand All @@ -119,7 +124,7 @@ impl<Service: PushService> AccountManager<Service> {
+ 1)
.into();
let pre_key_record = PreKeyRecord::new(pre_key_id, &key_pair);
pre_key_store
protocol_store
.save_pre_key(pre_key_id, &pre_key_record, None)
.await?;

Expand All @@ -128,7 +133,7 @@ impl<Service: PushService> AccountManager<Service> {

// Generate and store the next signed prekey
let identity_key_pair =
identity_store.get_identity_key_pair(None).await?;
protocol_store.get_identity_key_pair(None).await?;
let signed_pre_key_pair = KeyPair::generate(csprng);
let signed_pre_key_public = signed_pre_key_pair.public_key;
let signed_pre_key_signature = identity_key_pair
Expand All @@ -146,7 +151,7 @@ impl<Service: PushService> AccountManager<Service> {
&signed_pre_key_signature,
);

signed_pre_key_store
protocol_store
.save_signed_pre_key(
next_signed_pre_key_id.into(),
&signed_prekey_record,
Expand All @@ -168,7 +173,9 @@ impl<Service: PushService> AccountManager<Service> {
},
};

self.service.register_pre_keys(pre_key_state).await?;
self.service
.register_pre_keys(ServiceIdType::AccountIdentity, pre_key_state)
.await?;

log::trace!("Successfully refreshed prekeys");
Ok((
Expand Down
75 changes: 29 additions & 46 deletions libsignal-service/src/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use libsignal_protocol::{
message_encrypt, process_sender_key_distribution_message,
sealed_sender_decrypt_to_usmc, sealed_sender_encrypt,
CiphertextMessageType, Context, DeviceId, IdentityKeyStore,
PreKeySignalMessage, PreKeyStore, ProtocolAddress, PublicKey,
SealedSenderDecryptionResult, SenderCertificate,
PreKeySignalMessage, PreKeyStore, ProtocolAddress, ProtocolStore,
PublicKey, SealedSenderDecryptionResult, SenderCertificate,
SenderKeyDistributionMessage, SenderKeyStore, SessionStore, SignalMessage,
SignalProtocolError, SignedPreKeyStore,
};
Expand All @@ -26,45 +26,28 @@ use crate::{
///
/// Equivalent of SignalServiceCipher in Java.
#[derive(Clone)]
pub struct ServiceCipher<S, I, SP, P, SK, R> {
session_store: S,
identity_key_store: I,
signed_pre_key_store: SP,
pre_key_store: P,
sender_key_store: SK,
pub struct ServiceCipher<S, R> {
protocol_store: S,
csprng: R,
trust_root: PublicKey,
local_uuid: Uuid,
local_device_id: u32,
}

impl<S, I, SP, P, SK, R> ServiceCipher<S, I, SP, P, SK, R>
impl<S, R> ServiceCipher<S, R>
where
S: SessionStore + Clone,
I: IdentityKeyStore + Clone,
SP: SignedPreKeyStore + Clone,
SK: SenderKeyStore + Clone,
P: PreKeyStore + Clone,
S: ProtocolStore + SenderKeyStore + Clone,
R: Rng + CryptoRng + Clone,
{
#[allow(clippy::too_many_arguments)]
pub fn new(
session_store: S,
identity_key_store: I,
signed_pre_key_store: SP,
pre_key_store: P,
sender_key_store: SK,
protocol_store: S,
csprng: R,
trust_root: PublicKey,
local_uuid: Uuid,
local_device_id: u32,
) -> Self {
Self {
session_store,
identity_key_store,
signed_pre_key_store,
pre_key_store,
sender_key_store,
protocol_store,
csprng,
trust_root,
local_uuid,
Expand All @@ -88,7 +71,7 @@ where
process_sender_key_distribution_message(
&plaintext.metadata.protocol_address(),
&skdm,
&mut self.sender_key_store,
&mut self.protocol_store,
None,
)
.await?;
Expand Down Expand Up @@ -125,7 +108,7 @@ where
let plaintext = match envelope.r#type() {
Type::PrekeyBundle => {
let sender = get_preferred_protocol_address(
&self.session_store,
&self.protocol_store,
&envelope.source_address(),
envelope.source_device().into(),
)
Expand All @@ -141,10 +124,10 @@ where
let mut data = message_decrypt_prekey(
&PreKeySignalMessage::try_from(&ciphertext[..]).unwrap(),
&sender,
&mut self.session_store,
&mut self.identity_key_store,
&mut self.pre_key_store,
&mut self.signed_pre_key_store,
&mut self.protocol_store.clone(),
&mut self.protocol_store.clone(),
&mut self.protocol_store.clone(),
&mut self.protocol_store.clone(),
&mut self.csprng,
None,
)
Expand All @@ -153,7 +136,7 @@ where
.to_vec();

let session_record = self
.session_store
.protocol_store
.load_session(&sender, None)
.await?
.ok_or(SignalProtocolError::SessionNotFound(sender))?;
Expand All @@ -180,7 +163,7 @@ where
},
Type::Ciphertext => {
let sender = get_preferred_protocol_address(
&self.session_store,
&self.protocol_store,
&envelope.source_address(),
envelope.source_device().into(),
)
Expand All @@ -196,8 +179,8 @@ where
let mut data = message_decrypt_signal(
&SignalMessage::try_from(&ciphertext[..])?,
&sender,
&mut self.session_store,
&mut self.identity_key_store,
&mut self.protocol_store.clone(),
&mut self.protocol_store.clone(),
&mut self.csprng,
None,
)
Expand All @@ -206,7 +189,7 @@ where
.to_vec();

let session_record = self
.session_store
.protocol_store
.load_session(&sender, None)
.await?
.ok_or(SignalProtocolError::SessionNotFound(sender))?;
Expand All @@ -230,11 +213,11 @@ where
None,
self.local_uuid.to_string(),
self.local_device_id.into(),
&mut self.identity_key_store,
&mut self.session_store,
&mut self.pre_key_store,
&mut self.signed_pre_key_store,
&mut self.sender_key_store,
&mut self.protocol_store.clone(),
&mut self.protocol_store.clone(),
&mut self.protocol_store.clone(),
&mut self.protocol_store.clone(),
&mut self.protocol_store,
None,
)
.await?;
Expand Down Expand Up @@ -289,7 +272,7 @@ where
content: &[u8],
) -> Result<OutgoingPushMessage, ServiceError> {
let session_record = self
.session_store
.protocol_store
.load_session(address, None)
.await?
.ok_or_else(|| {
Expand All @@ -307,8 +290,8 @@ where
address,
unindentified_access,
&padded_content,
&mut self.session_store,
&mut self.identity_key_store,
&mut self.protocol_store.clone(),
&mut self.protocol_store,
None,
&mut self.csprng,
)
Expand All @@ -325,8 +308,8 @@ where
let message = message_encrypt(
&padded_content,
address,
&mut self.session_store,
&mut self.identity_key_store,
&mut self.protocol_store.clone(),
&mut self.protocol_store.clone(),
None,
)
.await?;
Expand Down
Loading