Skip to content

Commit

Permalink
Merge pull request #12 from signalapp/jack/jni-store-ctor
Browse files Browse the repository at this point in the history
Remove some intermediate helper functions when creating JNI store objects
  • Loading branch information
jack-signal authored Oct 19, 2020
2 parents c3d5f4d + aefc781 commit 5ece875
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 92 deletions.
139 changes: 49 additions & 90 deletions rust/bridge/jni/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,13 @@ pub struct JniIdentityKeyStore<'a> {
}

impl<'a> JniIdentityKeyStore<'a> {
fn new(env: &'a JNIEnv, store: jobject) -> Self {
Self { env, store }
fn new(env: &'a JNIEnv, store: jobject) -> Result<Self, SignalJniError> {
check_jobject_type(
&env,
store,
"org/whispersystems/libsignal/state/IdentityKeyStore",
)?;
Ok(Self { env, store })
}
}

Expand Down Expand Up @@ -986,8 +991,13 @@ pub struct JniPreKeyStore<'a> {
}

impl<'a> JniPreKeyStore<'a> {
fn new(env: &'a JNIEnv, store: jobject) -> Self {
Self { env, store }
fn new(env: &'a JNIEnv, store: jobject) -> Result<Self, SignalJniError> {
check_jobject_type(
&env,
store,
"org/whispersystems/libsignal/state/PreKeyStore",
)?;
Ok(Self { env, store })
}
}

Expand Down Expand Up @@ -1068,8 +1078,13 @@ pub struct JniSignedPreKeyStore<'a> {
}

impl<'a> JniSignedPreKeyStore<'a> {
fn new(env: &'a JNIEnv, store: jobject) -> Self {
Self { env, store }
fn new(env: &'a JNIEnv, store: jobject) -> Result<Self, SignalJniError> {
check_jobject_type(
&env,
store,
"org/whispersystems/libsignal/state/SignedPreKeyStore",
)?;
Ok(Self { env, store })
}
}

Expand Down Expand Up @@ -1143,8 +1158,13 @@ pub struct JniSessionStore<'a> {
}

impl<'a> JniSessionStore<'a> {
fn new(env: &'a JNIEnv, store: jobject) -> Self {
Self { env, store }
fn new(env: &'a JNIEnv, store: jobject) -> Result<Self, SignalJniError> {
check_jobject_type(
&env,
store,
"org/whispersystems/libsignal/state/SessionStore",
)?;
Ok(Self { env, store })
}
}

Expand Down Expand Up @@ -1211,48 +1231,6 @@ impl<'a> SessionStore for JniSessionStore<'a> {
}
}

fn create_identity_store<'a>(
env: &'a JNIEnv,
obj: jobject,
) -> Result<JniIdentityKeyStore<'a>, SignalJniError> {
let identity_key_store = check_jobject_type(
&env,
obj,
"org/whispersystems/libsignal/state/IdentityKeyStore",
)?;
Ok(JniIdentityKeyStore::new(&env, identity_key_store))
}

fn create_session_store<'a>(
env: &'a JNIEnv,
obj: jobject,
) -> Result<JniSessionStore<'a>, SignalJniError> {
let session_store =
check_jobject_type(&env, obj, "org/whispersystems/libsignal/state/SessionStore")?;
Ok(JniSessionStore::new(&env, session_store))
}

fn create_prekey_store<'a>(
env: &'a JNIEnv,
obj: jobject,
) -> Result<JniPreKeyStore<'a>, SignalJniError> {
let prekey_store =
check_jobject_type(&env, obj, "org/whispersystems/libsignal/state/PreKeyStore")?;
Ok(JniPreKeyStore::new(&env, prekey_store))
}

fn create_signed_prekey_store<'a>(
env: &'a JNIEnv,
obj: jobject,
) -> Result<JniSignedPreKeyStore<'a>, SignalJniError> {
let signed_prekey_store = check_jobject_type(
&env,
obj,
"org/whispersystems/libsignal/state/SignedPreKeyStore",
)?;
Ok(JniSignedPreKeyStore::new(&env, signed_prekey_store))
}

#[no_mangle]
pub unsafe extern "system" fn Java_org_whispersystems_libsignal_SessionBuilder_nativeProcessPreKeyBundle(
env: JNIEnv,
Expand All @@ -1266,8 +1244,8 @@ pub unsafe extern "system" fn Java_org_whispersystems_libsignal_SessionBuilder_n
let bundle = native_handle_cast::<PreKeyBundle>(bundle)?;
let protocol_address = native_handle_cast::<ProtocolAddress>(protocol_address)?;

let mut identity_key_store = create_identity_store(&env, identity_key_store)?;
let mut session_store = create_session_store(&env, session_store)?;
let mut identity_key_store = JniIdentityKeyStore::new(&env, identity_key_store)?;
let mut session_store = JniSessionStore::new(&env, session_store)?;

let mut csprng = rand::rngs::OsRng;
process_prekey_bundle(
Expand Down Expand Up @@ -1296,8 +1274,8 @@ pub unsafe extern "system" fn Java_org_whispersystems_libsignal_SessionCipher_na
let message = env.convert_byte_array(message)?;
let protocol_address = native_handle_cast::<ProtocolAddress>(protocol_address)?;

let mut identity_key_store = create_identity_store(&env, identity_key_store)?;
let mut session_store = create_session_store(&env, session_store)?;
let mut identity_key_store = JniIdentityKeyStore::new(&env, identity_key_store)?;
let mut session_store = JniSessionStore::new(&env, session_store)?;

let ctext = message_encrypt(
&message,
Expand Down Expand Up @@ -1340,8 +1318,8 @@ pub unsafe extern "system" fn Java_org_whispersystems_libsignal_SessionCipher_na
let message = native_handle_cast::<SignalMessage>(message)?;
let protocol_address = native_handle_cast::<ProtocolAddress>(protocol_address)?;

let mut identity_key_store = create_identity_store(&env, identity_key_store)?;
let mut session_store = create_session_store(&env, session_store)?;
let mut identity_key_store = JniIdentityKeyStore::new(&env, identity_key_store)?;
let mut session_store = JniSessionStore::new(&env, session_store)?;

let mut csprng = rand::rngs::OsRng;
let ptext = message_decrypt_signal(
Expand Down Expand Up @@ -1371,10 +1349,10 @@ pub unsafe extern "system" fn Java_org_whispersystems_libsignal_SessionCipher_na
run_ffi_safe(&env, || {
let message = native_handle_cast::<PreKeySignalMessage>(message)?;
let protocol_address = native_handle_cast::<ProtocolAddress>(protocol_address)?;
let mut identity_key_store = create_identity_store(&env, identity_key_store)?;
let mut session_store = create_session_store(&env, session_store)?;
let mut prekey_store = create_prekey_store(&env, prekey_store)?;
let mut signed_prekey_store = create_signed_prekey_store(&env, signed_prekey_store)?;
let mut identity_key_store = JniIdentityKeyStore::new(&env, identity_key_store)?;
let mut session_store = JniSessionStore::new(&env, session_store)?;
let mut prekey_store = JniPreKeyStore::new(&env, prekey_store)?;
let mut signed_prekey_store = JniSignedPreKeyStore::new(&env, signed_prekey_store)?;

let mut csprng = rand::rngs::OsRng;
let ptext = message_decrypt_prekey(
Expand All @@ -1398,8 +1376,13 @@ pub struct JniSenderKeyStore<'a> {
}

impl<'a> JniSenderKeyStore<'a> {
fn new(env: &'a JNIEnv, store: jobject) -> Self {
Self { env, store }
fn new(env: &'a JNIEnv, store: jobject) -> Result<Self, SignalJniError> {
check_jobject_type(
&env,
store,
"org/whispersystems/libsignal/groups/state/SenderKeyStore",
)?;
Ok(Self { env, store })
}
}

Expand Down Expand Up @@ -1483,13 +1466,7 @@ pub unsafe extern "system" fn Java_org_whispersystems_libsignal_groups_GroupSess
) -> ObjectHandle {
run_ffi_safe(&env, || {
let sender_key_name = native_handle_cast::<SenderKeyName>(sender_key_name)?;
let store = check_jobject_type(
&env,
store,
"org/whispersystems/libsignal/groups/state/SenderKeyStore",
)?;

let mut sender_key_store = JniSenderKeyStore::new(&env, store);
let mut sender_key_store = JniSenderKeyStore::new(&env, store)?;
let mut csprng = rand::rngs::OsRng;

let skdm = create_sender_key_distribution_message(
Expand All @@ -1514,13 +1491,7 @@ pub unsafe extern "system" fn Java_org_whispersystems_libsignal_groups_GroupSess
let sender_key_name = native_handle_cast::<SenderKeyName>(sender_key_name)?;
let sender_key_distribution_message =
native_handle_cast::<SenderKeyDistributionMessage>(sender_key_distribution_message)?;
let store = check_jobject_type(
&env,
store,
"org/whispersystems/libsignal/groups/state/SenderKeyStore",
)?;

let mut sender_key_store = JniSenderKeyStore::new(&env, store);
let mut sender_key_store = JniSenderKeyStore::new(&env, store)?;

process_sender_key_distribution_message(
sender_key_name,
Expand All @@ -1543,13 +1514,7 @@ pub unsafe extern "system" fn Java_org_whispersystems_libsignal_groups_GroupCiph
run_ffi_safe(&env, || {
let sender_key_name = native_handle_cast::<SenderKeyName>(sender_key_name)?;
let message = env.convert_byte_array(message)?;
let store = check_jobject_type(
&env,
store,
"org/whispersystems/libsignal/groups/state/SenderKeyStore",
)?;

let mut sender_key_store = JniSenderKeyStore::new(&env, store);
let mut sender_key_store = JniSenderKeyStore::new(&env, store)?;

let mut rng = rand::rngs::OsRng;

Expand All @@ -1576,13 +1541,7 @@ pub unsafe extern "system" fn Java_org_whispersystems_libsignal_groups_GroupCiph
run_ffi_safe(&env, || {
let sender_key_name = native_handle_cast::<SenderKeyName>(sender_key_name)?;
let message = env.convert_byte_array(message)?;
let store = check_jobject_type(
&env,
store,
"org/whispersystems/libsignal/groups/state/SenderKeyStore",
)?;

let mut sender_key_store = JniSenderKeyStore::new(&env, store);
let mut sender_key_store = JniSenderKeyStore::new(&env, store)?;

let ptext = group_decrypt(&message, &mut sender_key_store, &sender_key_name, None)?;

Expand Down
4 changes: 2 additions & 2 deletions rust/bridge/jni/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ pub fn check_jobject_type(
env: &JNIEnv,
obj: jobject,
class_name: &'static str,
) -> Result<jobject, SignalJniError> {
) -> Result<(), SignalJniError> {
if obj.is_null() {
return Err(SignalJniError::NullHandle);
}
Expand All @@ -373,7 +373,7 @@ pub fn check_jobject_type(
return Err(SignalJniError::BadJniParameter(class_name));
}

Ok(obj)
Ok(())
}

pub fn get_object_with_native_handle<T: 'static + Clone>(
Expand Down

0 comments on commit 5ece875

Please sign in to comment.