Skip to content

Commit

Permalink
part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
rubdos committed Apr 20, 2021
1 parent 76758f0 commit c4110d2
Show file tree
Hide file tree
Showing 10 changed files with 471 additions and 45 deletions.
15 changes: 3 additions & 12 deletions libsignal-service/src/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ use std::collections::HashMap;
use std::convert::TryFrom;
use std::time::SystemTime;

use libsignal_protocol::keys::PublicKey;
use libsignal_protocol::{Context, StoreContext};
use libsignal_protocol::PublicKey;

use zkgroup::profiles::ProfileKey;

pub struct AccountManager<Service> {
context: Context,
service: Service,
profile_key: Option<[u8; 32]>,
}
Expand Down Expand Up @@ -51,13 +49,8 @@ const PRE_KEY_MINIMUM: u32 = 10;
const PRE_KEY_BATCH_SIZE: u32 = 100;

impl<Service: PushService> AccountManager<Service> {
pub fn new(
context: Context,
service: Service,
profile_key: Option<[u8; 32]>,
) -> Self {
pub fn new(service: Service, profile_key: Option<[u8; 32]>) -> Self {
Self {
context,
service,
profile_key,
}
Expand Down Expand Up @@ -96,13 +89,11 @@ impl<Service: PushService> AccountManager<Service> {
}

let pre_keys = libsignal_protocol::generate_pre_keys(
&self.context,
pre_keys_offset_id,
PRE_KEY_BATCH_SIZE,
)?;
let identity_key_pair = store_context.identity_key_pair()?;
let signed_pre_key = libsignal_protocol::generate_signed_pre_key(
&self.context,
&identity_key_pair,
next_signed_pre_key_id,
SystemTime::now(),
Expand Down Expand Up @@ -207,7 +198,7 @@ impl<Service: PushService> AccountManager<Service> {
query.get("pub_key").ok_or(LinkError::InvalidPublicKey)?;
let pub_key = base64::decode(&**pub_key)
.map_err(|_e| LinkError::InvalidPublicKey)?;
let pub_key = PublicKey::decode_point(&self.context, &pub_key)
let pub_key = PublicKey::deserialize(&pub_key)
.map_err(|_e| LinkError::InvalidPublicKey)?;

let identity_key_pair = store_context.identity_key_pair()?;
Expand Down
12 changes: 6 additions & 6 deletions libsignal-service/src/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ impl ServiceCipher {
let sender = get_preferred_protocol_address(
&self.store_context,
envelope.source_address(),
envelope.source_device() as i32,
envelope.source_device(),
)?;
let metadata = Metadata {
sender: envelope.source_address(),
sender_device: envelope.source_device() as i32,
sender_device: envelope.source_device(),
timestamp: envelope.timestamp(),
needs_receipt: false,
};
Expand All @@ -130,11 +130,11 @@ impl ServiceCipher {
let sender = get_preferred_protocol_address(
&self.store_context,
envelope.source_address(),
envelope.source_device() as i32,
envelope.source_device(),
)?;
let metadata = Metadata {
sender: envelope.source_address(),
sender_device: envelope.source_device() as i32,
sender_device: envelope.source_device(),
timestamp: envelope.timestamp(),
needs_receipt: false,
};
Expand Down Expand Up @@ -293,8 +293,8 @@ fn strip_padding(
pub fn get_preferred_protocol_address(
store_context: &StoreContext,
address: ServiceAddress,
device_id: i32,
) -> Result<ProtocolAddress, libsignal_protocol::Error> {
device_id: u32,
) -> Result<ProtocolAddress, libsignal_protocol::error::SignalProtocolError> {
if let Some(ref uuid) = address.uuid {
let address = ProtocolAddress::new(uuid.to_string(), device_id as i32);
if store_context.contains_session(&address)? {
Expand Down
2 changes: 1 addition & 1 deletion libsignal-service/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct ServiceCredentials {
pub phonenumber: phonenumber::PhoneNumber,
pub password: Option<String>,
pub signaling_key: Option<SignalingKey>,
pub device_id: Option<i32>,
pub device_id: Option<u32>,
}

impl ServiceCredentials {
Expand Down
2 changes: 1 addition & 1 deletion libsignal-service/src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use crate::{
#[derive(Clone, Debug)]
pub struct Metadata {
pub sender: crate::ServiceAddress,
pub sender_device: i32,
pub sender_device: u32,
pub timestamp: u64,
pub needs_receipt: bool,
}
Expand Down
14 changes: 5 additions & 9 deletions libsignal-service/src/groups_v2/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use libsignal_protocol::{Context, Error};
use libsignal_protocol::{error::SignalProtocolError, Context};
use zkgroup::groups::GroupMasterKey;
use zkgroup::GROUP_MASTER_KEY_LEN;

Expand All @@ -8,15 +8,11 @@ use zkgroup::GROUP_MASTER_KEY_LEN;
pub fn derive_v2_migration_master_key(
ctx: &Context,
group_id: &[u8],
) -> Result<GroupMasterKey, Error> {
) -> Result<GroupMasterKey, SignalProtocolError> {
assert_eq!(group_id.len(), 16, "Group ID must be exactly 16 bytes");
let hkdf = libsignal_protocol::create_hkdf(ctx, 3)?;
let bytes = hkdf.derive_secrets(
GROUP_MASTER_KEY_LEN,
group_id,
&[],
b"GV2 Migration",
)?;
let hkdf = libsignal_protocol::HKDF::new(3)?;
let bytes =
hkdf.derive_secrets(group_id, b"GV2 Migration", GROUP_MASTER_KEY_LEN)?;
let mut bytes_stack = [0u8; GROUP_MASTER_KEY_LEN];
bytes_stack.copy_from_slice(&bytes);
Ok(GroupMasterKey::new(bytes_stack))
Expand Down
Loading

0 comments on commit c4110d2

Please sign in to comment.