Skip to content

Commit

Permalink
Upgrade to libsignal-protocol 0.32 (whisperfish#188)
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel Feron <[email protected]>
  • Loading branch information
boxdot and gferon authored Sep 20, 2023
1 parent db46e0d commit 8abd8ca
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 61 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[workspace]
members = ["presage", "presage-cli", "presage-store-sled"]
resolver = "2"

[patch.crates-io]
"curve25519-dalek" = { git = 'https://github.com/signalapp/curve25519-dalek', branch = 'lizard2' }
curve25519-dalek = { git = 'https://github.com/signalapp/curve25519-dalek', tag = 'signal-curve25519-4.0.0' }

[patch."https://github.com/whisperfish/libsignal-service-rs.git"]
# [patch."https://github.com/whisperfish/libsignal-service-rs.git"]
# libsignal-service = { path = "../libsignal-service-rs/libsignal-service" }
# libsignal-service-hyper = { path = "../libsignal-service-rs/libsignal-service-hyper" }
2 changes: 1 addition & 1 deletion presage-store-sled/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ anyhow = "1.0"
futures = "0.3"
quickcheck = "1.0.3"
quickcheck_async = "0.1"
rand = "0.7"
rand = "0.8"
tokio = { version = "1.0", default-features = false, features = ["time"] }

[features]
Expand Down
65 changes: 18 additions & 47 deletions presage-store-sled/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use presage::libsignal_service::{
models::Contact,
prelude::{Content, ProfileKey, Uuid},
protocol::{
Context, Direction, GenericSignedPreKey, IdentityKey, IdentityKeyPair, IdentityKeyStore,
Direction, GenericSignedPreKey, IdentityKey, IdentityKeyPair, IdentityKeyStore,
KyberPreKeyId, KyberPreKeyRecord, KyberPreKeyStore, PreKeyId, PreKeyRecord, PreKeyStore,
ProtocolAddress, ProtocolStore, SenderKeyRecord, SenderKeyStore, SessionRecord,
SessionStore, SignalProtocolError, SignedPreKeyId, SignedPreKeyRecord, SignedPreKeyStore,
Expand Down Expand Up @@ -246,10 +246,7 @@ impl SledStore {
}

fn profile_key_for_uuid(&self, uuid: Uuid, key: ProfileKey) -> String {
let key = uuid
.into_bytes()
.into_iter()
.chain(key.get_bytes().into_iter());
let key = uuid.into_bytes().into_iter().chain(key.get_bytes());

let mut hasher = Sha256::new();
hasher.update(key.collect::<Vec<_>>());
Expand Down Expand Up @@ -662,11 +659,7 @@ impl Iterator for SledGroupsIter {

#[async_trait(?Send)]
impl PreKeyStore for SledStore {
async fn get_pre_key(
&self,
prekey_id: PreKeyId,
_ctx: Context,
) -> Result<PreKeyRecord, SignalProtocolError> {
async fn get_pre_key(&self, prekey_id: PreKeyId) -> Result<PreKeyRecord, SignalProtocolError> {
let buf: Vec<u8> = self
.get(SLED_TREE_PRE_KEYS, prekey_id.to_string())
.ok()
Expand All @@ -680,7 +673,6 @@ impl PreKeyStore for SledStore {
&mut self,
prekey_id: PreKeyId,
record: &PreKeyRecord,
_ctx: Context,
) -> Result<(), SignalProtocolError> {
self.insert(
SLED_TREE_PRE_KEYS,
Expand All @@ -691,11 +683,7 @@ impl PreKeyStore for SledStore {
Ok(())
}

async fn remove_pre_key(
&mut self,
prekey_id: PreKeyId,
_ctx: Context,
) -> Result<(), SignalProtocolError> {
async fn remove_pre_key(&mut self, prekey_id: PreKeyId) -> Result<(), SignalProtocolError> {
self.remove(SLED_TREE_PRE_KEYS, prekey_id.to_string())
.expect("failed to remove pre-key");
Ok(())
Expand All @@ -707,7 +695,6 @@ impl SignedPreKeyStore for SledStore {
async fn get_signed_pre_key(
&self,
signed_prekey_id: SignedPreKeyId,
_ctx: Context,
) -> Result<SignedPreKeyRecord, SignalProtocolError> {
let buf: Vec<u8> = self
.get(SLED_TREE_SIGNED_PRE_KEYS, signed_prekey_id.to_string())
Expand All @@ -721,7 +708,6 @@ impl SignedPreKeyStore for SledStore {
&mut self,
signed_prekey_id: SignedPreKeyId,
record: &SignedPreKeyRecord,
_ctx: Context,
) -> Result<(), SignalProtocolError> {
self.insert(
SLED_TREE_SIGNED_PRE_KEYS,
Expand All @@ -741,7 +727,6 @@ impl KyberPreKeyStore for SledStore {
async fn get_kyber_pre_key(
&self,
kyber_prekey_id: KyberPreKeyId,
_ctx: Context,
) -> Result<KyberPreKeyRecord, SignalProtocolError> {
let buf: Vec<u8> = self
.get(SLED_TREE_KYBER_PRE_KEYS, kyber_prekey_id.to_string())
Expand All @@ -755,7 +740,6 @@ impl KyberPreKeyStore for SledStore {
&mut self,
kyber_prekey_id: KyberPreKeyId,
record: &KyberPreKeyRecord,
_ctx: Context,
) -> Result<(), SignalProtocolError> {
self.insert(
SLED_TREE_KYBER_PRE_KEYS,
Expand All @@ -772,7 +756,6 @@ impl KyberPreKeyStore for SledStore {
async fn mark_kyber_pre_key_used(
&mut self,
kyber_prekey_id: KyberPreKeyId,
_ctx: Context,
) -> Result<(), SignalProtocolError> {
let removed = self
.remove(SLED_TREE_KYBER_PRE_KEYS, kyber_prekey_id.to_string())
Expand All @@ -792,7 +775,6 @@ impl SessionStore for SledStore {
async fn load_session(
&self,
address: &ProtocolAddress,
_ctx: Context,
) -> Result<Option<SessionRecord>, SignalProtocolError> {
let session = self
.get(SLED_TREE_SESSIONS, address.to_string())
Expand All @@ -807,7 +789,6 @@ impl SessionStore for SledStore {
&mut self,
address: &ProtocolAddress,
record: &SessionRecord,
_ctx: Context,
) -> Result<(), SignalProtocolError> {
trace!("storing session {}", address);
self.insert(SLED_TREE_SESSIONS, address.to_string(), record.serialize()?)
Expand Down Expand Up @@ -885,10 +866,7 @@ impl SessionStoreExt for SledStore {

#[async_trait(?Send)]
impl IdentityKeyStore for SledStore {
async fn get_identity_key_pair(
&self,
_ctx: Context,
) -> Result<IdentityKeyPair, SignalProtocolError> {
async fn get_identity_key_pair(&self) -> Result<IdentityKeyPair, SignalProtocolError> {
trace!("getting identity_key_pair");
let state = self
.load_state()
Expand All @@ -903,7 +881,7 @@ impl IdentityKeyStore for SledStore {
))
}

async fn get_local_registration_id(&self, _ctx: Context) -> Result<u32, SignalProtocolError> {
async fn get_local_registration_id(&self) -> Result<u32, SignalProtocolError> {
let state = self
.load_state()
.map_err(SledStoreError::into_signal_error)?
Expand All @@ -918,7 +896,6 @@ impl IdentityKeyStore for SledStore {
&mut self,
address: &ProtocolAddress,
identity_key: &IdentityKey,
_ctx: Context,
) -> Result<bool, SignalProtocolError> {
trace!("saving identity");
self.insert(
Expand All @@ -939,7 +916,6 @@ impl IdentityKeyStore for SledStore {
address: &ProtocolAddress,
right_identity_key: &IdentityKey,
_direction: Direction,
_ctx: Context,
) -> Result<bool, SignalProtocolError> {
match self
.get(SLED_TREE_IDENTITIES, address.to_string())
Expand All @@ -959,7 +935,6 @@ impl IdentityKeyStore for SledStore {
async fn get_identity(
&self,
address: &ProtocolAddress,
_ctx: Context,
) -> Result<Option<IdentityKey>, SignalProtocolError> {
self.get(SLED_TREE_IDENTITIES, address.to_string())
.map_err(SledStoreError::into_signal_error)?
Expand All @@ -975,7 +950,6 @@ impl SenderKeyStore for SledStore {
sender: &ProtocolAddress,
distribution_id: Uuid,
record: &SenderKeyRecord,
_ctx: Context,
) -> Result<(), SignalProtocolError> {
let key = format!(
"{}.{}/{}",
Expand All @@ -992,7 +966,6 @@ impl SenderKeyStore for SledStore {
&mut self,
sender: &ProtocolAddress,
distribution_id: Uuid,
_ctx: Context,
) -> Result<Option<SenderKeyRecord>, SignalProtocolError> {
let key = format!(
"{}.{}/{}",
Expand Down Expand Up @@ -1137,14 +1110,12 @@ mod tests {
async fn test_save_get_trust_identity(addr: ProtocolAddress, key_pair: KeyPair) -> bool {
let mut db = SledStore::temporary().unwrap();
let identity_key = protocol::IdentityKey::new(key_pair.0.public_key);
db.save_identity(&addr.0, &identity_key, None)
.await
.unwrap();
let id = db.get_identity(&addr.0, None).await.unwrap().unwrap();
db.save_identity(&addr.0, &identity_key).await.unwrap();
let id = db.get_identity(&addr.0).await.unwrap().unwrap();
if id != identity_key {
return false;
}
db.is_trusted_identity(&addr.0, &id, Direction::Receiving, None)
db.is_trusted_identity(&addr.0, &id, Direction::Receiving)
.await
.unwrap()
}
Expand All @@ -1154,11 +1125,11 @@ mod tests {
let session = SessionRecord::new_fresh();

let mut db = SledStore::temporary().unwrap();
db.store_session(&addr.0, &session, None).await.unwrap();
if db.load_session(&addr.0, None).await.unwrap().is_none() {
db.store_session(&addr.0, &session).await.unwrap();
if db.load_session(&addr.0).await.unwrap().is_none() {
return false;
}
let loaded_session = db.load_session(&addr.0, None).await.unwrap().unwrap();
let loaded_session = db.load_session(&addr.0).await.unwrap().unwrap();
session.serialize().unwrap() == loaded_session.serialize().unwrap()
}

Expand All @@ -1167,15 +1138,15 @@ mod tests {
let id = id.into();
let mut db = SledStore::temporary().unwrap();
let pre_key_record = PreKeyRecord::new(id, &key_pair.0);
db.save_pre_key(id, &pre_key_record, None).await.unwrap();
if db.get_pre_key(id, None).await.unwrap().serialize().unwrap()
db.save_pre_key(id, &pre_key_record).await.unwrap();
if db.get_pre_key(id).await.unwrap().serialize().unwrap()
!= pre_key_record.serialize().unwrap()
{
return false;
}

db.remove_pre_key(id, None).await.unwrap();
db.get_pre_key(id, None).await.is_err()
db.remove_pre_key(id).await.unwrap();
db.get_pre_key(id).await.is_err()
}

#[quickcheck_async::tokio]
Expand All @@ -1188,11 +1159,11 @@ mod tests {
let mut db = SledStore::temporary().unwrap();
let id = id.into();
let signed_pre_key_record = SignedPreKeyRecord::new(id, timestamp, &key_pair.0, &signature);
db.save_signed_pre_key(id, &signed_pre_key_record, None)
db.save_signed_pre_key(id, &signed_pre_key_record)
.await
.unwrap();

db.get_signed_pre_key(id, None)
db.get_signed_pre_key(id)
.await
.unwrap()
.serialize()
Expand Down
6 changes: 3 additions & 3 deletions presage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ authors = ["Gabriel Féron <[email protected]>"]
edition = "2021"

[dependencies]
libsignal-service = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "8789920" }
libsignal-service-hyper = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "8789920" }
libsignal-service = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "8305357" }
libsignal-service-hyper = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "8305357" }

base64 = "0.12"
futures = "0.3"
log = "0.4.8"
rand = "0.7"
rand = "0.8"
serde = "1.0"
serde_json = "1.0"
thiserror = "1.0"
Expand Down
23 changes: 15 additions & 8 deletions presage/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use std::{
use futures::{channel::mpsc, channel::oneshot, future, pin_mut, AsyncReadExt, Stream, StreamExt};
use log::{debug, error, info, trace, warn};
use parking_lot::Mutex;
use rand::{distributions::Alphanumeric, rngs::StdRng, Rng, RngCore, SeedableRng};
use rand::{
distributions::{Alphanumeric, DistString},
rngs::StdRng,
RngCore, SeedableRng,
};
use serde::{Deserialize, Serialize};
use url::Url;

Expand Down Expand Up @@ -190,9 +194,9 @@ impl<C: Store> Manager<C, Registration> {

config_store.clear_registration()?;

// generate a random 24 bytes password
// generate a random alphanumeric 24 chars password
let mut rng = StdRng::from_entropy();
let password: String = (&mut rng).sample_iter(&Alphanumeric).take(24).collect();
let password = Alphanumeric.sample_string(&mut rng, 24);

let service_configuration: ServiceConfiguration = signal_servers.into();
let mut push_service =
Expand Down Expand Up @@ -296,9 +300,9 @@ impl<C: Store> Manager<C, Linking> {
// and you won't be able to use this client anyways
config_store.clear_registration()?;

// generate a random 24 bytes password
// generate a random alphanumeric 24 chars password
let mut rng = StdRng::from_entropy();
let password: String = (&mut rng).sample_iter(&Alphanumeric).take(24).collect();
let password = Alphanumeric.sample_string(&mut rng, 24);

// generate a 52 bytes signaling key
let mut signaling_key = [0u8; 52];
Expand Down Expand Up @@ -823,15 +827,16 @@ impl<C: Store> Manager<C, Registered> {
&mut self,
) -> Result<impl Stream<Item = Result<Envelope, ServiceError>>, Error<C::Error>> {
let credentials = self.credentials()?.ok_or(Error::NotYetRegisteredError)?;
let allow_stories = false;
let pipe = MessageReceiver::new(self.push_service()?)
.create_message_pipe(credentials)
.create_message_pipe(credentials, allow_stories)
.await?;

let service_configuration: ServiceConfiguration = self.state.signal_servers.into();
let mut unidentified_push_service =
HyperPushService::new(service_configuration, None, crate::USER_AGENT.to_string());
let unidentified_ws = unidentified_push_service
.ws("/v1/websocket/", None, false)
.ws("/v1/websocket/", &[], None, false)
.await?;
self.state.identified_websocket.lock().replace(pipe.ws());
self.state
Expand Down Expand Up @@ -1230,7 +1235,7 @@ impl<C: Store> Manager<C, Registered> {
let mut unidentified_push_service =
HyperPushService::new(service_configuration, None, crate::USER_AGENT.to_string());
let unidentified_websocket = unidentified_push_service
.ws("/v1/websocket/", None, false)
.ws("/v1/websocket/", &[], None, false)
.await?;

Ok(MessageSender::new(
Expand Down Expand Up @@ -1397,6 +1402,8 @@ fn save_message_with_thread<C: Store>(
}
ContentBody::ReceiptMessage(_) => debug!("skipping saving receipt message"),
ContentBody::TypingMessage(_) => debug!("skipping saving typing message"),
ContentBody::StoryMessage(_) => debug!("skipping story message"),
ContentBody::PniSignatureMessage(_) => todo!(),
}

Ok(())
Expand Down

0 comments on commit 8abd8ca

Please sign in to comment.