-
Notifications
You must be signed in to change notification settings - Fork 34
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
Swap libsignal-protocol dependency with libsignal-client #83
Merged
Merged
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
76758f0
Swap libsignal-protocol dependency with libsignal-client
rubdos c4110d2
part 1
rubdos f4615cc
rand 0.7 because of compatibility
rubdos 9e61ecf
part 2
rubdos adc795d
Merge remote-tracking branch 'upstream/master' into libsignal-client
gferon 04788d8
Progress towards using the new libsignal-protocol in Rust
gferon 5a3531e
Fix HKDF usage
gferon 839baaa
Use dynamic dispatch and fix tests
gferon 3419aa9
Adjust usage of verify_signature
gferon 66b8969
Remove FIXME
gferon 926d062
Last changes before shipping\!
gferon 53dddee
Last changes before shipping!
gferon c867547
Merge remote-tracking branch 'upstream/master' into libsignal-client
gferon f1ae701
Use upstream libsignal-client
gferon 1829246
Address review comments
gferon 6e00342
Merge remote-tracking branch 'upstream/master' into libsignal-client
boxdot 0ab27c5
Merge pull request #1 from boxdot/libsignal-client
gferon 4436018
Merge remote-tracking branch 'fork/libsignal-client' into libsignal-c…
gferon 1365782
Use static dispatch
gferon f6c4df7
Derive clone on ciphers and sender/receiver
gferon 797dead
Fix clippy warnings
gferon a3be3d5
Add get_sub_device_sessions method to ServiceAddress
gferon 4a5cfa2
Change license to AGPLv3
rubdos ead122e
Reflect license and copyright changes in README
rubdos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,8 @@ version = "0.1.0" | |
authors = ["Ruben De Smet <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
libsignal-service = { path = "../libsignal-service" } | ||
libsignal-protocol = { git = "https://github.com/Michael-F-Bryan/libsignal-protocol-rs" } | ||
|
||
awc = { version = "3.0.0-beta.5", features=["rustls"] } | ||
actix = "0.11.1" | ||
|
@@ -22,7 +19,7 @@ rustls = "0.19" | |
url = "2.1" | ||
serde = "1.0" | ||
log = "0.4" | ||
rand = "0.8" | ||
rand = "0.7" | ||
|
||
failure = "0.1.5" | ||
thiserror = "1.0" | ||
|
@@ -36,6 +33,6 @@ env_logger = "0.8" | |
image = { version = "0.23", default-features = false, features = ["png"] } | ||
opener = "0.4" | ||
qrcode = "0.12" | ||
rand = "0.8" | ||
rand = "0.7" | ||
structopt = "0.3" | ||
tokio = { version = "1", features=["macros"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,8 @@ version = "0.1.0" | |
authors = ["Gabriel Féron <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
libsignal-service = { path = "../libsignal-service" } | ||
libsignal-protocol = { git = "https://github.com/Michael-F-Bryan/libsignal-protocol-rs" } | ||
|
||
async-trait = "0.1" | ||
base64 = "0.13" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,3 +1,13 @@ | ||||||||
use std::collections::HashMap; | ||||||||
use std::convert::{TryFrom, TryInto}; | ||||||||
use std::time::SystemTime; | ||||||||
|
||||||||
use libsignal_protocol::{ | ||||||||
IdentityKeyStore, KeyPair, PreKeyRecord, PreKeyStore, PublicKey, | ||||||||
SignalProtocolError, SignedPreKeyRecord, SignedPreKeyStore, | ||||||||
}; | ||||||||
use zkgroup::profiles::ProfileKey; | ||||||||
|
||||||||
use crate::{ | ||||||||
configuration::{Endpoint, ServiceCredentials}, | ||||||||
pre_keys::{PreKeyEntity, PreKeyState}, | ||||||||
|
@@ -11,16 +21,7 @@ use crate::{ | |||||||
}, | ||||||||
}; | ||||||||
|
||||||||
use std::collections::HashMap; | ||||||||
use std::convert::TryFrom; | ||||||||
use std::time::SystemTime; | ||||||||
|
||||||||
use libsignal_protocol::keys::PublicKey; | ||||||||
use libsignal_protocol::{Context, StoreContext}; | ||||||||
use zkgroup::profiles::ProfileKey; | ||||||||
|
||||||||
pub struct AccountManager<Service> { | ||||||||
context: Context, | ||||||||
service: Service, | ||||||||
profile_key: Option<[u8; 32]>, | ||||||||
} | ||||||||
|
@@ -42,7 +43,7 @@ pub enum LinkError { | |||||||
#[error("TsUrl has an invalid pub_key field")] | ||||||||
InvalidPublicKey, | ||||||||
#[error("Protocol error {0}")] | ||||||||
ProtocolError(#[from] libsignal_protocol::Error), | ||||||||
ProtocolError(#[from] SignalProtocolError), | ||||||||
#[error(transparent)] | ||||||||
ProvisioningError(#[from] ProvisioningError), | ||||||||
} | ||||||||
|
@@ -56,15 +57,11 @@ pub struct Profile { | |||||||
|
||||||||
const PRE_KEY_MINIMUM: u32 = 10; | ||||||||
const PRE_KEY_BATCH_SIZE: u32 = 100; | ||||||||
const PRE_KEY_MEDIUM_MAX_VALUE: u32 = 0xFFFFFF; | ||||||||
|
||||||||
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, | ||||||||
} | ||||||||
|
@@ -78,11 +75,14 @@ impl<Service: PushService> AccountManager<Service> { | |||||||
/// Equivalent to Java's RefreshPreKeysJob | ||||||||
/// | ||||||||
/// Returns the next pre-key offset and next signed pre-key offset as a tuple. | ||||||||
pub async fn update_pre_key_bundle( | ||||||||
pub async fn update_pre_key_bundle<R: rand::Rng + rand::CryptoRng>( | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
&mut self, | ||||||||
store_context: StoreContext, | ||||||||
identity_store: &dyn IdentityKeyStore, | ||||||||
pre_key_store: &mut dyn PreKeyStore, | ||||||||
signed_pre_key_store: &mut dyn SignedPreKeyStore, | ||||||||
rubdos marked this conversation as resolved.
Show resolved
Hide resolved
rubdos marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
csprng: &mut R, | ||||||||
pre_keys_offset_id: u32, | ||||||||
next_signed_pre_key_id: u32, | ||||||||
signed_pre_key_id: u32, | ||||||||
gferon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
use_last_resort_key: bool, | ||||||||
) -> Result<(u32, u32), ServiceError> { | ||||||||
let prekey_count = match self.service.get_pre_key_status().await { | ||||||||
|
@@ -99,34 +99,54 @@ impl<Service: PushService> AccountManager<Service> { | |||||||
|
||||||||
if prekey_count >= PRE_KEY_MINIMUM { | ||||||||
log::info!("Available keys sufficient"); | ||||||||
return Ok((pre_keys_offset_id, next_signed_pre_key_id)); | ||||||||
return Ok((pre_keys_offset_id, signed_pre_key_id)); | ||||||||
} | ||||||||
|
||||||||
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(), | ||||||||
)?; | ||||||||
|
||||||||
store_context.store_signed_pre_key(&signed_pre_key)?; | ||||||||
|
||||||||
let mut pre_key_entities = vec![]; | ||||||||
for pre_key in pre_keys { | ||||||||
store_context.store_pre_key(&pre_key)?; | ||||||||
pre_key_entities.push(PreKeyEntity::try_from(pre_key)?); | ||||||||
for i in 0..PRE_KEY_BATCH_SIZE { | ||||||||
let key_pair = KeyPair::generate(csprng); | ||||||||
let pre_key_id = | ||||||||
((pre_keys_offset_id + i) % (PRE_KEY_MEDIUM_MAX_VALUE - 1)) + 1; | ||||||||
let pre_key_record = PreKeyRecord::new(pre_key_id, &key_pair); | ||||||||
pre_key_store | ||||||||
.save_pre_key(pre_key_id, &pre_key_record, None) | ||||||||
.await?; | ||||||||
|
||||||||
pre_key_entities.push(PreKeyEntity::try_from(pre_key_record)?); | ||||||||
} | ||||||||
|
||||||||
// Generate and store the next signed prekey | ||||||||
let identity_key_pair = | ||||||||
identity_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 | ||||||||
.private_key() | ||||||||
.calculate_signature(&signed_pre_key_public.serialize(), csprng)?; | ||||||||
|
||||||||
let unix_time = SystemTime::now() | ||||||||
.duration_since(SystemTime::UNIX_EPOCH) | ||||||||
.unwrap(); | ||||||||
|
||||||||
let signed_prekey_record = SignedPreKeyRecord::new( | ||||||||
signed_pre_key_id + 1, | ||||||||
unix_time.as_secs(), | ||||||||
gferon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
&signed_pre_key_pair, | ||||||||
&signed_pre_key_signature, | ||||||||
); | ||||||||
|
||||||||
signed_pre_key_store | ||||||||
.save_signed_pre_key( | ||||||||
signed_pre_key_id + 1, | ||||||||
&signed_prekey_record, | ||||||||
None, | ||||||||
) | ||||||||
.await?; | ||||||||
|
||||||||
let pre_key_state = PreKeyState { | ||||||||
pre_keys: pre_key_entities, | ||||||||
signed_pre_key: signed_pre_key.into(), | ||||||||
identity_key: identity_key_pair.public(), | ||||||||
signed_pre_key: signed_prekey_record.try_into()?, | ||||||||
identity_key: identity_key_pair.public_key().clone(), | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
last_resort_key: if use_last_resort_key { | ||||||||
Some(PreKeyEntity { | ||||||||
key_id: 0x7fffffff, | ||||||||
|
@@ -142,7 +162,7 @@ impl<Service: PushService> AccountManager<Service> { | |||||||
log::trace!("Successfully refreshed prekeys"); | ||||||||
Ok(( | ||||||||
pre_keys_offset_id + PRE_KEY_BATCH_SIZE, | ||||||||
next_signed_pre_key_id + 1, | ||||||||
signed_pre_key_id + 1, | ||||||||
)) | ||||||||
} | ||||||||
|
||||||||
|
@@ -209,7 +229,7 @@ impl<Service: PushService> AccountManager<Service> { | |||||||
pub async fn link_device( | ||||||||
&mut self, | ||||||||
url: url::Url, | ||||||||
store_context: StoreContext, | ||||||||
identity_store: &dyn IdentityKeyStore, | ||||||||
credentials: ServiceCredentials, | ||||||||
) -> Result<(), LinkError> { | ||||||||
let query: HashMap<_, _> = url.query_pairs().collect(); | ||||||||
|
@@ -218,10 +238,11 @@ 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()?; | ||||||||
let identity_key_pair = | ||||||||
identity_store.get_identity_key_pair(None).await?; | ||||||||
|
||||||||
if credentials.uuid.is_none() { | ||||||||
log::warn!("No local UUID set"); | ||||||||
|
@@ -231,10 +252,10 @@ impl<Service: PushService> AccountManager<Service> { | |||||||
|
||||||||
let msg = ProvisionMessage { | ||||||||
identity_key_public: Some( | ||||||||
identity_key_pair.public().to_bytes()?.as_slice().to_vec(), | ||||||||
identity_key_pair.public_key().serialize().into_vec(), | ||||||||
), | ||||||||
identity_key_private: Some( | ||||||||
identity_key_pair.private().to_bytes()?.as_slice().to_vec(), | ||||||||
identity_key_pair.private_key().serialize(), | ||||||||
), | ||||||||
number: Some(credentials.e164()), | ||||||||
uuid: credentials.uuid.as_ref().map(|u| u.to_string()), | ||||||||
|
@@ -248,8 +269,7 @@ impl<Service: PushService> AccountManager<Service> { | |||||||
user_agent: None, | ||||||||
}; | ||||||||
|
||||||||
let cipher = | ||||||||
ProvisioningCipher::from_public(self.context.clone(), pub_key); | ||||||||
let cipher = ProvisioningCipher::from_public(pub_key); | ||||||||
|
||||||||
let encrypted = cipher.encrypt(msg)?; | ||||||||
self.send_provisioning_message(ephemeral_id, encrypted) | ||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we wait on signalapp/libsignal#279?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we will wait.