Skip to content

Commit

Permalink
make DeviceId into a struct wrapping u32 with From impls
Browse files Browse the repository at this point in the history
remove `PreKeyId` and `SignedPreKeyId` in favor of `DeviceId`

reintroduce `PreKeyId` and `SignedPreKeyId`!

revert the cumbersome `let` destructuring

add MessageVersion

add SessionSeed
  • Loading branch information
cosmicexplorer committed May 5, 2021
1 parent 87f205e commit 7bd9093
Show file tree
Hide file tree
Showing 24 changed files with 487 additions and 314 deletions.
27 changes: 15 additions & 12 deletions rust/bridge/shared/src/ffi/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ impl IdentityKeyStore for &FfiIdentityKeyStoreStruct {
Ok(IdentityKeyPair::new(IdentityKey::new(pub_key), *priv_key))
}

async fn get_local_registration_id(&self, ctx: Context) -> Result<u32, SignalProtocolError> {
async fn get_local_registration_id(
&self,
ctx: Context,
) -> Result<SessionSeed, SignalProtocolError> {
let ctx = ctx.unwrap_or(std::ptr::null_mut());
let mut id = 0;
let result = (self.get_local_registration_id)(self.ctx, &mut id, ctx);
Expand All @@ -89,7 +92,7 @@ impl IdentityKeyStore for &FfiIdentityKeyStoreStruct {
));
}

Ok(id)
Ok(id.into())
}

async fn save_identity(
Expand Down Expand Up @@ -194,12 +197,12 @@ pub struct FfiPreKeyStoreStruct {
impl PreKeyStore for &FfiPreKeyStoreStruct {
async fn get_pre_key(
&self,
prekey_id: u32,
prekey_id: PreKeyId,
ctx: Context,
) -> Result<PreKeyRecord, SignalProtocolError> {
let ctx = ctx.unwrap_or(std::ptr::null_mut());
let mut record = std::ptr::null_mut();
let result = (self.load_pre_key)(self.ctx, &mut record, prekey_id, ctx);
let result = (self.load_pre_key)(self.ctx, &mut record, prekey_id.into(), ctx);

if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
Expand All @@ -218,12 +221,12 @@ impl PreKeyStore for &FfiPreKeyStoreStruct {

async fn save_pre_key(
&mut self,
prekey_id: u32,
prekey_id: PreKeyId,
record: &PreKeyRecord,
ctx: Context,
) -> Result<(), SignalProtocolError> {
let ctx = ctx.unwrap_or(std::ptr::null_mut());
let result = (self.store_pre_key)(self.ctx, prekey_id, &*record, ctx);
let result = (self.store_pre_key)(self.ctx, prekey_id.into(), &*record, ctx);

if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
Expand All @@ -237,11 +240,11 @@ impl PreKeyStore for &FfiPreKeyStoreStruct {

async fn remove_pre_key(
&mut self,
prekey_id: u32,
prekey_id: PreKeyId,
ctx: Context,
) -> Result<(), SignalProtocolError> {
let ctx = ctx.unwrap_or(std::ptr::null_mut());
let result = (self.remove_pre_key)(self.ctx, prekey_id, ctx);
let result = (self.remove_pre_key)(self.ctx, prekey_id.into(), ctx);

if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
Expand Down Expand Up @@ -279,12 +282,12 @@ pub struct FfiSignedPreKeyStoreStruct {
impl SignedPreKeyStore for &FfiSignedPreKeyStoreStruct {
async fn get_signed_pre_key(
&self,
prekey_id: u32,
prekey_id: SignedPreKeyId,
ctx: Context,
) -> Result<SignedPreKeyRecord, SignalProtocolError> {
let ctx = ctx.unwrap_or(std::ptr::null_mut());
let mut record = std::ptr::null_mut();
let result = (self.load_signed_pre_key)(self.ctx, &mut record, prekey_id, ctx);
let result = (self.load_signed_pre_key)(self.ctx, &mut record, prekey_id.into(), ctx);

if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
Expand All @@ -304,12 +307,12 @@ impl SignedPreKeyStore for &FfiSignedPreKeyStoreStruct {

async fn save_signed_pre_key(
&mut self,
prekey_id: u32,
prekey_id: SignedPreKeyId,
record: &SignedPreKeyRecord,
ctx: Context,
) -> Result<(), SignalProtocolError> {
let ctx = ctx.unwrap_or(std::ptr::null_mut());
let result = (self.store_signed_pre_key)(self.ctx, prekey_id, &*record, ctx);
let result = (self.store_signed_pre_key)(self.ctx, prekey_id.into(), &*record, ctx);

if let Some(error) = CallbackError::check(result) {
return Err(SignalProtocolError::ApplicationCallbackError(
Expand Down
30 changes: 17 additions & 13 deletions rust/bridge/shared/src/jni/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ fn protocol_address_to_jobject<'a>(
address: &ProtocolAddress,
) -> Result<JObject<'a>, SignalJniError> {
let address_class = env.find_class("org/whispersystems/libsignal/SignalProtocolAddress")?;
let device_id: u32 = address.device_id().into();
let address_ctor_args = [
JObject::from(env.new_string(address.name())?).into(),
JValue::from(address.device_id().convert_into(env)?),
JValue::from(device_id.convert_into(env)?),
];

let address_ctor_sig = jni_signature!((java.lang.String, int) -> void);
Expand Down Expand Up @@ -179,8 +180,11 @@ impl<'a> IdentityKeyStore for JniIdentityKeyStore<'a> {
Ok(self.do_get_identity_key_pair()?)
}

async fn get_local_registration_id(&self, _ctx: Context) -> Result<u32, SignalProtocolError> {
Ok(self.do_get_local_registration_id()?)
async fn get_local_registration_id(
&self,
_ctx: Context,
) -> Result<SessionSeed, SignalProtocolError> {
Ok(self.do_get_local_registration_id()?.into())
}

async fn save_identity(
Expand Down Expand Up @@ -290,27 +294,27 @@ impl<'a> JniPreKeyStore<'a> {
impl<'a> PreKeyStore for JniPreKeyStore<'a> {
async fn get_pre_key(
&self,
prekey_id: u32,
prekey_id: PreKeyId,
_ctx: Context,
) -> Result<PreKeyRecord, SignalProtocolError> {
Ok(self.do_get_pre_key(prekey_id)?)
Ok(self.do_get_pre_key(prekey_id.into())?)
}

async fn save_pre_key(
&mut self,
prekey_id: u32,
prekey_id: PreKeyId,
record: &PreKeyRecord,
_ctx: Context,
) -> Result<(), SignalProtocolError> {
Ok(self.do_save_pre_key(prekey_id, record)?)
Ok(self.do_save_pre_key(prekey_id.into(), record)?)
}

async fn remove_pre_key(
&mut self,
prekey_id: u32,
prekey_id: PreKeyId,
_ctx: Context,
) -> Result<(), SignalProtocolError> {
Ok(self.do_remove_pre_key(prekey_id)?)
Ok(self.do_remove_pre_key(prekey_id.into())?)
}
}

Expand Down Expand Up @@ -384,19 +388,19 @@ impl<'a> JniSignedPreKeyStore<'a> {
impl<'a> SignedPreKeyStore for JniSignedPreKeyStore<'a> {
async fn get_signed_pre_key(
&self,
prekey_id: u32,
prekey_id: SignedPreKeyId,
_ctx: Context,
) -> Result<SignedPreKeyRecord, SignalProtocolError> {
Ok(self.do_get_signed_pre_key(prekey_id)?)
Ok(self.do_get_signed_pre_key(prekey_id.into())?)
}

async fn save_signed_pre_key(
&mut self,
prekey_id: u32,
prekey_id: SignedPreKeyId,
record: &SignedPreKeyRecord,
_ctx: Context,
) -> Result<(), SignalProtocolError> {
Ok(self.do_save_signed_pre_key(prekey_id, record)?)
Ok(self.do_save_signed_pre_key(prekey_id.into(), record)?)
}
}

Expand Down
28 changes: 15 additions & 13 deletions rust/bridge/shared/src/node/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,31 @@ impl Finalize for NodePreKeyStore {
impl PreKeyStore for NodePreKeyStore {
async fn get_pre_key(
&self,
pre_key_id: u32,
pre_key_id: PreKeyId,
_ctx: libsignal_protocol::Context,
) -> Result<PreKeyRecord, SignalProtocolError> {
self.do_get_pre_key(pre_key_id)
self.do_get_pre_key(pre_key_id.into())
.await
.map_err(|s| js_error_to_rust("getPreKey", s))
}

async fn save_pre_key(
&mut self,
pre_key_id: u32,
pre_key_id: PreKeyId,
record: &PreKeyRecord,
_ctx: libsignal_protocol::Context,
) -> Result<(), SignalProtocolError> {
self.do_save_pre_key(pre_key_id, record.clone())
self.do_save_pre_key(pre_key_id.into(), record.clone())
.await
.map_err(|s| js_error_to_rust("savePreKey", s))
}

async fn remove_pre_key(
&mut self,
pre_key_id: u32,
pre_key_id: PreKeyId,
_ctx: libsignal_protocol::Context,
) -> Result<(), SignalProtocolError> {
self.do_remove_pre_key(pre_key_id)
self.do_remove_pre_key(pre_key_id.into())
.await
.map_err(|s| js_error_to_rust("removePreKey", s))
}
Expand Down Expand Up @@ -215,21 +215,21 @@ impl Finalize for NodeSignedPreKeyStore {
impl SignedPreKeyStore for NodeSignedPreKeyStore {
async fn get_signed_pre_key(
&self,
signed_pre_key_id: u32,
signed_pre_key_id: SignedPreKeyId,
_ctx: libsignal_protocol::Context,
) -> Result<SignedPreKeyRecord, SignalProtocolError> {
self.do_get_signed_pre_key(signed_pre_key_id)
self.do_get_signed_pre_key(signed_pre_key_id.into())
.await
.map_err(|s| js_error_to_rust("getSignedPreKey", s))
}

async fn save_signed_pre_key(
&mut self,
signed_pre_key_id: u32,
signed_pre_key_id: SignedPreKeyId,
record: &SignedPreKeyRecord,
_ctx: libsignal_protocol::Context,
) -> Result<(), SignalProtocolError> {
self.do_save_signed_pre_key(signed_pre_key_id, record.clone())
self.do_save_signed_pre_key(signed_pre_key_id.into(), record.clone())
.await
.map_err(|s| js_error_to_rust("saveSignedPreKey", s))
}
Expand Down Expand Up @@ -516,10 +516,12 @@ impl IdentityKeyStore for NodeIdentityKeyStore {
async fn get_local_registration_id(
&self,
_ctx: libsignal_protocol::Context,
) -> Result<u32, SignalProtocolError> {
self.do_get_local_registration_id()
) -> Result<SessionSeed, SignalProtocolError> {
Ok(self
.do_get_local_registration_id()
.await
.map_err(|s| js_error_to_rust("getLocalRegistrationId", s))
.map_err(|s| js_error_to_rust("getLocalRegistrationId", s))?
.into())
}

async fn get_identity(
Expand Down
32 changes: 16 additions & 16 deletions rust/bridge/shared/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use libsignal_bridge_macros::*;
use libsignal_protocol::error::Result;
use libsignal_protocol::*;
use static_assertions::const_assert_eq;
use std::convert::TryFrom;
use std::convert::{TryFrom, TryInto};
use uuid::Uuid;

use crate::support::*;
Expand Down Expand Up @@ -41,7 +41,7 @@ fn HKDF_DeriveSecrets<E: Env>(
label: &[u8],
salt: Option<&[u8]>,
) -> Result<E::Buffer> {
let kdf = HKDF::new(version)?;
let kdf = HKDF::new_for_version(version.try_into()?)?;
let buffer = match salt {
Some(salt) => kdf.derive_salted_secrets(ikm, salt, label, output_length as usize)?,
None => kdf.derive_secrets(ikm, label, output_length as usize)?,
Expand All @@ -58,15 +58,15 @@ fn HKDF_Derive(
label: &[u8],
salt: &[u8],
) -> Result<()> {
let kdf = HKDF::new(version)?;
let kdf = HKDF::new_for_version(version.try_into()?)?;
let kdf_output = kdf.derive_salted_secrets(ikm, salt, label, output.len())?;
output.copy_from_slice(&kdf_output);
Ok(())
}

#[bridge_fn(ffi = "address_new")]
fn ProtocolAddress_New(name: String, device_id: u32) -> ProtocolAddress {
ProtocolAddress::new(name, device_id)
ProtocolAddress::new(name, device_id.into())
}

bridge_deserialize!(PublicKey::deserialize, ffi = publickey, jni = false);
Expand Down Expand Up @@ -236,7 +236,7 @@ fn SignalMessage_New(
receiver_identity_key: &PublicKey,
) -> Result<SignalMessage> {
SignalMessage::new(
message_version,
message_version.try_into()?,
mac_key,
*sender_ratchet_key,
counter,
Expand Down Expand Up @@ -277,10 +277,10 @@ fn PreKeySignalMessage_New(
signal_message: &SignalMessage,
) -> Result<PreKeySignalMessage> {
PreKeySignalMessage::new(
message_version,
registration_id,
pre_key_id,
signed_pre_key_id,
message_version.try_into()?,
registration_id.into(),
pre_key_id.map(|id| id.into()),
signed_pre_key_id.into(),
*base_key,
IdentityKey::new(*identity_key),
signal_message.clone(),
Expand Down Expand Up @@ -443,9 +443,9 @@ fn PreKeyBundle_New(
) -> Result<PreKeyBundle> {
let identity_key = IdentityKey::new(*identity_key);

let prekey = match (prekey, prekey_id) {
let prekey: Option<(PreKeyId, PublicKey)> = match (prekey, prekey_id) {
(None, None) => None,
(Some(k), Some(id)) => Some((id, *k)),
(Some(k), Some(id)) => Some((id.into(), *k)),
_ => {
return Err(SignalProtocolError::InvalidArgument(
"Must supply both or neither of prekey and prekey_id".to_owned(),
Expand All @@ -457,7 +457,7 @@ fn PreKeyBundle_New(
registration_id,
device_id,
prekey,
signed_prekey_id,
signed_prekey_id.into(),
*signed_prekey,
signed_prekey_signature.to_vec(),
identity_key,
Expand Down Expand Up @@ -497,7 +497,7 @@ fn SignedPreKeyRecord_New(
signature: &[u8],
) -> SignedPreKeyRecord {
let keypair = KeyPair::new(*pub_key, *priv_key);
SignedPreKeyRecord::new(id, timestamp, &keypair, &signature)
SignedPreKeyRecord::new(id.into(), timestamp, &keypair, &signature)
}

bridge_deserialize!(PreKeyRecord::deserialize);
Expand All @@ -512,7 +512,7 @@ bridge_get!(PreKeyRecord::private_key -> PrivateKey);
#[bridge_fn]
fn PreKeyRecord_New(id: u32, pub_key: &PublicKey, priv_key: &PrivateKey) -> PreKeyRecord {
let keypair = KeyPair::new(*pub_key, *priv_key);
PreKeyRecord::new(id, &keypair)
PreKeyRecord::new(id.into(), &keypair)
}

bridge_deserialize!(SenderKeyRecord::deserialize);
Expand Down Expand Up @@ -583,7 +583,7 @@ fn SenderCertificate_New(
sender_uuid,
sender_e164,
*sender_key,
sender_device_id,
sender_device_id.into(),
expiration,
signer_cert.clone(),
signer_key,
Expand Down Expand Up @@ -1029,7 +1029,7 @@ async fn SealedSender_DecryptMessage(
timestamp,
local_e164,
local_uuid,
local_device_id,
local_device_id.into(),
identity_store,
session_store,
prekey_store,
Expand Down
Loading

0 comments on commit 7bd9093

Please sign in to comment.