Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin-upstream/use-fix-linking-from-ls…
Browse files Browse the repository at this point in the history
…r' into next
  • Loading branch information
Schmiddiii committed Dec 17, 2023
2 parents 86ff0c3 + 9bb1904 commit 44a866a
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 95 deletions.
2 changes: 1 addition & 1 deletion presage-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = ["Gabriel Féron <[email protected]>"]
presage = { path = "../presage" }
presage-store-sled = { path = "../presage-store-sled" }

anyhow = "1.0"
anyhow = { version = "1.0", features = ["backtrace"] }
base64 = "0.12"
chrono = { version = "0.4", default-features = false, features = ["serde", "clock"] }
clap = { version = ">=4.2.4", features = ["derive"] }
Expand Down
37 changes: 21 additions & 16 deletions presage-store-sled/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use presage::libsignal_service::{
content::Content,
groups_v2::Group,
models::Contact,
pre_keys::PreKeysStore,
prelude::{ProfileKey, Uuid},
protocol::{
Direction, GenericSignedPreKey, IdentityKey, IdentityKeyPair, IdentityKeyStore,
Expand All @@ -24,7 +25,7 @@ use presage::libsignal_service::{
session_store::SessionStoreExt,
Profile, ServiceAddress,
};
use presage::store::{ContentExt, ContentsStore, PreKeyStoreExt, StateStore, Store, Thread};
use presage::store::{ContentExt, ContentsStore, StateStore, Store, Thread};
use presage::{manager::RegistrationData, proto::verified};
use prost::Message;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
Expand Down Expand Up @@ -603,39 +604,43 @@ impl ContentsStore for SledStore {
}
}

impl PreKeyStoreExt for SledStore {
type PreKeyStoreExtError = SledStoreError;

fn pre_keys_offset_id(&self) -> Result<u32, SledStoreError> {
impl PreKeysStore for SledStore {
fn pre_keys_offset_id(&self) -> Result<u32, SignalProtocolError> {
Ok(self
.get(SLED_TREE_STATE, SLED_KEY_PRE_KEYS_OFFSET_ID)?
.get(SLED_TREE_STATE, SLED_KEY_PRE_KEYS_OFFSET_ID)
.map_err(|_| SignalProtocolError::InvalidPreKeyId)?
.unwrap_or(0))
}

fn set_pre_keys_offset_id(&mut self, id: u32) -> Result<(), SledStoreError> {
self.insert(SLED_TREE_STATE, SLED_KEY_PRE_KEYS_OFFSET_ID, id)?;
fn set_pre_keys_offset_id(&mut self, id: u32) -> Result<(), SignalProtocolError> {
self.insert(SLED_TREE_STATE, SLED_KEY_PRE_KEYS_OFFSET_ID, id)
.map_err(|_| SignalProtocolError::InvalidPreKeyId)?;
Ok(())
}

fn next_signed_pre_key_id(&self) -> Result<u32, SledStoreError> {
fn next_signed_pre_key_id(&self) -> Result<u32, SignalProtocolError> {
Ok(self
.get(SLED_TREE_STATE, SLED_KEY_NEXT_SIGNED_PRE_KEY_ID)?
.get(SLED_TREE_STATE, SLED_KEY_NEXT_SIGNED_PRE_KEY_ID)
.map_err(|_| SignalProtocolError::InvalidSignedPreKeyId)?
.unwrap_or(0))
}

fn set_next_signed_pre_key_id(&mut self, id: u32) -> Result<(), SledStoreError> {
self.insert(SLED_TREE_STATE, SLED_KEY_NEXT_SIGNED_PRE_KEY_ID, id)?;
fn set_next_signed_pre_key_id(&mut self, id: u32) -> Result<(), SignalProtocolError> {
self.insert(SLED_TREE_STATE, SLED_KEY_NEXT_SIGNED_PRE_KEY_ID, id)
.map_err(|_| SignalProtocolError::InvalidSignedPreKeyId)?;
Ok(())
}

fn next_pq_pre_key_id(&self) -> Result<u32, SledStoreError> {
fn next_pq_pre_key_id(&self) -> Result<u32, SignalProtocolError> {
Ok(self
.get(SLED_TREE_STATE, SLED_KEY_NEXT_PQ_PRE_KEY_ID)?
.get(SLED_TREE_STATE, SLED_KEY_NEXT_PQ_PRE_KEY_ID)
.map_err(|_| SignalProtocolError::InvalidKyberPreKeyId)?
.unwrap_or(0))
}

fn set_next_pq_pre_key_id(&mut self, id: u32) -> Result<(), SledStoreError> {
self.insert(SLED_TREE_STATE, SLED_KEY_NEXT_PQ_PRE_KEY_ID, id)?;
fn set_next_pq_pre_key_id(&mut self, id: u32) -> Result<(), SignalProtocolError> {
self.insert(SLED_TREE_STATE, SLED_KEY_NEXT_PQ_PRE_KEY_ID, id)
.map_err(|_| SignalProtocolError::InvalidKyberPreKeyId)?;
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions presage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ authors = ["Gabriel Féron <[email protected]>"]
edition = "2021"

[dependencies]
libsignal-service = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "2e3bd5813aa54abe354a46e78ac0ee164027b985" }
libsignal-service-hyper = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "2e3bd5813aa54abe354a46e78ac0ee164027b985" }
libsignal-service = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "8f88d8" }
libsignal-service-hyper = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "8f88d8" }

base64 = "0.21"
futures = "0.3"
Expand Down
99 changes: 47 additions & 52 deletions presage/src/manager/linking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use futures::channel::{mpsc, oneshot};
use futures::{future, StreamExt};
use libsignal_service::configuration::{ServiceConfiguration, SignalServers};
use libsignal_service::provisioning::{LinkingManager, SecondaryDeviceProvisioning};
use libsignal_service::push_service::DeviceId;
use libsignal_service::zkgroup::profiles::ProfileKey;
use libsignal_service_hyper::push_service::HyperPushService;
use log::info;
use rand::distributions::{Alphanumeric, DistString};
Expand Down Expand Up @@ -78,12 +76,21 @@ impl<S: Store> Manager<S, Linking> {
HyperPushService::new(service_configuration, None, crate::USER_AGENT.to_string());

let mut linking_manager: LinkingManager<HyperPushService> =
LinkingManager::new(push_service, password.clone());
LinkingManager::new(push_service.clone(), password.clone());

let (tx, mut rx) = mpsc::channel(1);

let (wait_for_qrcode_scan, registration) = future::join(
linking_manager.provision_secondary_device(&mut rng, signaling_key, tx),
// XXX: this is obviously wrong
let mut pni_store = store.clone();

let (wait_for_qrcode_scan, registration_data) = future::join(
linking_manager.provision_secondary_device(
&mut store,
&mut pni_store,
&mut rng,
&device_name,
tx,
),
async move {
if let Some(SecondaryDeviceProvisioning::Url(url)) = rx.next().await {
info!("generating qrcode from provisioning link: {}", &url);
Expand All @@ -93,66 +100,54 @@ impl<S: Store> Manager<S, Linking> {
} else {
return Err(Error::LinkError);
}

if let Some(SecondaryDeviceProvisioning::NewDeviceRegistration {
phone_number,
device_id: DeviceId { device_id },
registration_id,
pni_registration_id,
profile_key,
service_ids,
aci_private_key,
aci_public_key,
pni_private_key,
pni_public_key,
}) = rx.next().await
if let Some(SecondaryDeviceProvisioning::NewDeviceRegistration(data)) =
rx.next().await
{
info!("successfully registered device {}", &service_ids);
Ok(Registered::with_data(RegistrationData {
signal_servers,
device_name: Some(device_name),
phone_number,
service_ids,
signaling_key,
password,
device_id: Some(device_id),
registration_id,
pni_registration_id: Some(pni_registration_id),
aci_public_key,
aci_private_key,
pni_public_key: Some(pni_public_key),
pni_private_key: Some(pni_private_key),
profile_key: ProfileKey::create(
profile_key.try_into().expect("32 bytes for profile key"),
),
}))
Ok(data)
} else {
Err(Error::NoProvisioningMessageReceived)
return Err(Error::NoProvisioningMessageReceived);
}
},
)
.await;

wait_for_qrcode_scan?;

let mut manager = Manager {
rng,
store,
state: registration?,
};
match registration_data {
Ok(d) => {
let registration_data = RegistrationData {
signal_servers,
device_name: Some(device_name),
phone_number: d.phone_number,
service_ids: d.service_ids,
password,
signaling_key,
device_id: Some(d.device_id.into()),
registration_id: d.registration_id,
pni_registration_id: Some(d.pni_registration_id),
aci_public_key: d.aci_public_key,
aci_private_key: d.aci_private_key,
pni_public_key: Some(d.pni_public_key),
pni_private_key: Some(d.pni_private_key),
profile_key: d.profile_key,
};

manager.store.save_registration_data(&manager.state.data)?;
store.save_registration_data(&registration_data)?;
info!(
"successfully registered device {}",
&registration_data.service_ids
);

match (
manager.register_pre_keys().await,
manager.set_account_attributes().await,
) {
(Err(e), _) | (_, Err(e)) => {
// clear the entire store on any error, there's no possible recovery here
manager.store.clear_registration()?;
Ok(Manager {
rng,
store,
state: Registered::with_data(registration_data),
})
}
Err(e) => {
store.clear_registration()?;
Err(e)
}
_ => Ok(manager),
}
}
}
11 changes: 7 additions & 4 deletions presage/src/manager/registered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,15 @@ impl<S: Store> Manager<S, Registered> {
match identified_ws.clone() {
Some(ws) => Ok(ws),
None => {
let keep_alive = true;
let headers = &[("X-Signal-Receive-Stories", "false")];
let ws = self
.identified_push_service()
.ws("/v1/websocket/", headers, self.credentials(), keep_alive)
.ws(
"/v1/websocket/",
"/v1/keepalive",
headers,
self.credentials(),
)
.await?;
identified_ws.replace(ws.clone());
debug!("initialized identified websocket");
Expand All @@ -239,10 +243,9 @@ impl<S: Store> Manager<S, Registered> {
match unidentified_ws.clone() {
Some(ws) => Ok(ws),
None => {
let keep_alive = true;
let ws = self
.unidentified_push_service()
.ws("/v1/websocket/", &[], None, keep_alive)
.ws("/v1/websocket/", "/v1/keepalive", &[], None)
.await?;
unidentified_ws.replace(ws.clone());
debug!("initialized unidentified websocket");
Expand Down
22 changes: 2 additions & 20 deletions presage/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use libsignal_service::{
content::{ContentBody, Metadata},
groups_v2::{Group, Timer},
models::Contact,
pre_keys::PreKeysStore,
prelude::{Content, ProfileKey, Uuid, UuidError},
proto::{
sync_message::{self, Sent},
Expand Down Expand Up @@ -44,25 +45,6 @@ pub trait StateStore {
fn clear_registration(&mut self) -> Result<(), Self::StateStoreError>;
}

/// Stores the keys published ahead of time, pre-keys
///
/// <https://signal.org/docs/specifications/x3dh/>
pub trait PreKeyStoreExt {
type PreKeyStoreExtError: StoreError;

fn pre_keys_offset_id(&self) -> Result<u32, Self::PreKeyStoreExtError>;

fn set_pre_keys_offset_id(&mut self, id: u32) -> Result<(), Self::PreKeyStoreExtError>;

fn next_signed_pre_key_id(&self) -> Result<u32, Self::PreKeyStoreExtError>;

fn next_pq_pre_key_id(&self) -> Result<u32, Self::PreKeyStoreExtError>;

fn set_next_signed_pre_key_id(&mut self, id: u32) -> Result<(), Self::PreKeyStoreExtError>;

fn set_next_pq_pre_key_id(&mut self, id: u32) -> Result<(), Self::PreKeyStoreExtError>;
}

/// Stores messages, contacts, groups and profiles
pub trait ContentsStore {
type ContentsStoreError: StoreError;
Expand Down Expand Up @@ -262,7 +244,7 @@ pub trait ContentsStore {
/// The manager store trait combining all other stores into a single one
pub trait Store:
StateStore<StateStoreError = Self::Error>
+ PreKeyStoreExt<PreKeyStoreExtError = Self::Error>
+ PreKeysStore
+ ContentsStore<ContentsStoreError = Self::Error>
+ ProtocolStore
+ SenderKeyStore
Expand Down

0 comments on commit 44a866a

Please sign in to comment.