Skip to content

Commit

Permalink
separate chat protobuf code and simplify ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
tiainen committed Oct 19, 2023
1 parent 2126809 commit 76ef3b6
Show file tree
Hide file tree
Showing 35 changed files with 292 additions and 113 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"rust/attest",
"rust/crypto",
"rust/device-transfer",
"rust/chat",
"rust/grpc",
"rust/media",
"rust/pin",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.signal.libsignal;

public class SignalChatCommunicationFailureException extends Exception {
public SignalChatCommunicationFailureException(String msg) { super(msg); }
public SignalChatCommunicationFailureException(Throwable t) { super(t); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.signal.libsignal.profile;

import com.google.protobuf.InvalidProtocolBufferException;
import org.signal.chat.profile.GetVersionedProfileRequest;
import org.signal.chat.profile.GetVersionedProfileResponse;
import org.signal.libsignal.SignalChatCommunicationFailureException;
import org.signal.libsignal.internal.Native;
import org.signal.libsignal.internal.NativeHandleGuard;

public class ProfileClient implements NativeHandleGuard.Owner {

private static final String DEFAULT_TARGET = "https://grpcproxy.gluonhq.net:443";

private final long unsafeHandle;

public ProfileClient() {
this(DEFAULT_TARGET);
}

public ProfileClient(String target) {
this.unsafeHandle = Native.ProfileClient_New(target);
}

@Override @SuppressWarnings("deprecation")
protected void finalize() {
Native.ProfileClient_Destroy(this.unsafeHandle);
}

public long unsafeNativeHandleWithoutGuard() {
return this.unsafeHandle;
}

public GetVersionedProfileResponse getVersionedProfile(GetVersionedProfileRequest request) throws SignalChatCommunicationFailureException {
try (NativeHandleGuard guard = new NativeHandleGuard(this)) {
byte[] serializedResponse = Native.ProfileClient_GetVersionedProfile(guard.nativeHandle(), request.toByteArray());
return GetVersionedProfileResponse.parseFrom(serializedResponse);
} catch (InvalidProtocolBufferException e) {
throw new SignalChatCommunicationFailureException(e);
}
}
}
7 changes: 0 additions & 7 deletions java/shared/java/org/signal/libsignal/grpc/GrpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import java.util.List;
import java.util.Map;

import org.signal.chat.profile.GetVersionedProfileRequest;
import org.signal.chat.profile.GetVersionedProfileResponse;

import org.signal.libsignal.internal.Native;
import org.signal.libsignal.internal.NativeHandleGuard;

Expand All @@ -36,10 +33,6 @@ public long unsafeNativeHandleWithoutGuard() {
return this.unsafeHandle;
}

public GetVersionedProfileResponse getProfileVersion(GetVersionedProfileRequest request) {
return Native.GrpcClient_GetProfileVersion(this.unsafeHandle, request.getAccountIdentifier().getIdentityType().getNumber(), request.getAccountIdentifier().getUuid().toByteArray(), request.getVersion());
}

public byte[] sendDirectMessage(String method, String urlFragment, byte[] body, Map<String, List<String>> headers) {
return Native.GrpcClient_SendDirectMessage(this.unsafeHandle, method, urlFragment, body, headers);
}
Expand Down
31 changes: 16 additions & 15 deletions java/shared/java/org/signal/libsignal/internal/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package org.signal.libsignal.internal;

import org.signal.chat.profile.GetVersionedProfileResponse;

import org.signal.libsignal.grpc.GrpcReplyListener;
import org.signal.libsignal.protocol.message.CiphertextMessage;
import org.signal.libsignal.protocol.state.IdentityKeyStore;
Expand All @@ -27,7 +26,6 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.List;
import java.util.UUID;
import java.util.Map;

Expand Down Expand Up @@ -233,6 +231,12 @@ private Native() {}
public static native long GroupSessionBuilder_CreateSenderKeyDistributionMessage(long sender, UUID distributionId, SenderKeyStore store);
public static native void GroupSessionBuilder_ProcessSenderKeyDistributionMessage(long sender, long senderKeyDistributionMessage, SenderKeyStore store);

public static native void GrpcClient_Destroy(long handle);
public static native long GrpcClient_New(String target);
public static native void GrpcClient_OpenStream(long grpcClient, String uri, Map headers, GrpcReplyListener listener);
public static native byte[] GrpcClient_SendDirectMessage(long grpcClient, String method, String urlFragment, byte[] body, Map headers);
public static native void GrpcClient_SendMessageOnStream(long grpcClient, String method, String urlFragment, byte[] body, Map headers);

public static native byte[] HKDF_DeriveSecrets(int outputLength, byte[] ikm, byte[] label, byte[] salt);

public static native void HsmEnclaveClient_CompleteHandshake(long cli, byte[] handshakeReceived);
Expand Down Expand Up @@ -339,6 +343,10 @@ private Native() {}
public static native int PreKeySignalMessage_GetVersion(long obj);
public static native long PreKeySignalMessage_New(int messageVersion, int registrationId, int preKeyId, int signedPreKeyId, long baseKey, long identityKey, long signalMessage);

public static native void ProfileClient_Destroy(long handle);
public static native byte[] ProfileClient_GetVersionedProfile(long profileClient, byte[] request);
public static native long ProfileClient_New(String target);

public static native void ProfileKeyCiphertext_CheckValidContents(byte[] buffer);

public static native void ProfileKeyCommitment_CheckValidContents(byte[] buffer);
Expand All @@ -363,6 +371,12 @@ private Native() {}
public static native String ProtocolAddress_Name(long obj);
public static native long ProtocolAddress_New(String name, int deviceId);

public static native void QuicClient_Destroy(long handle);
public static native long QuicClient_New(String target);
public static native void QuicClient_OpenControlledStream(long quicClient, String baseUrl, Map headers, QuicCallbackListener listener);
public static native byte[] QuicClient_SendMessage(long quicClient, byte[] data);
public static native void QuicClient_WriteMessageOnStream(long quicClient, byte[] payload);

public static native void ReceiptCredentialPresentation_CheckValidContents(byte[] buffer);
public static native long ReceiptCredentialPresentation_GetReceiptExpirationTime(byte[] presentation);
public static native long ReceiptCredentialPresentation_GetReceiptLevel(byte[] presentation);
Expand Down Expand Up @@ -550,17 +564,4 @@ private Native() {}
public static native boolean ValidatingMac_Finalize(long mac);
public static native long ValidatingMac_Initialize(byte[] key, int chunkSize, byte[] digests);
public static native boolean ValidatingMac_Update(long mac, byte[] bytes, int offset, int length);

public static native long GrpcClient_New(String target);
public static native void GrpcClient_Destroy(long handle);
public static native GetVersionedProfileResponse GrpcClient_GetProfileVersion(long handle, int type, byte[] uuid, String version);
public static native byte[] GrpcClient_SendDirectMessage(long handle, String method, String urlFragment, byte[] body, Map<String, List<String>> headers);
public static native void GrpcClient_OpenStream(long handle, String uri, Map<String, List<String>> headers, GrpcReplyListener listener);
public static native void GrpcClient_SendMessageOnStream(long handle, String method, String urlFragment, byte[] body, Map<String, List<String>> headers);

public static native long QuicClient_New(String target);
public static native void QuicClient_Destroy(long handle);
public static native byte[] QuicClient_SendMessage(long handle, byte[] data);
public static native void QuicClient_OpenControlledStream(long handle, String baseUrl, Map<String, String> headers, QuicCallbackListener listener);
public static native void QuicClient_WriteMessageToStream(long handle, byte[] payload);
}
2 changes: 1 addition & 1 deletion java/shared/java/org/signal/libsignal/quic/QuicClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public void openControlledStream(String baseUrl, Map<String, String> headers, Qu
}

public void writeMessageOnStream(byte[] payload) {
Native.QuicClient_WriteMessageToStream(this.unsafeHandle, payload);
Native.QuicClient_WriteMessageOnStream(this.unsafeHandle, payload);
}
}
3 changes: 3 additions & 0 deletions rust/bridge/jni/bin/Native.java.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package org.signal.libsignal.internal;

import org.signal.chat.profile.GetVersionedProfileResponse;
import org.signal.libsignal.grpc.GrpcReplyListener;
import org.signal.libsignal.protocol.message.CiphertextMessage;
import org.signal.libsignal.protocol.state.IdentityKeyStore;
import org.signal.libsignal.protocol.state.SessionStore;
Expand All @@ -16,6 +18,7 @@ import org.signal.libsignal.protocol.state.KyberPreKeyStore;
import org.signal.libsignal.protocol.groups.state.SenderKeyStore;
import org.signal.libsignal.protocol.logging.Log;
import org.signal.libsignal.protocol.logging.SignalProtocolLogger;
import org.signal.libsignal.quic.QuicCallbackListener;

import java.io.File;
import java.io.FileOutputStream;
Expand Down
1 change: 1 addition & 0 deletions rust/bridge/shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ usernames = { path = "../../usernames" }
signal-pin = { path = "../../pin" }
signal-media = { path = "../../media", optional = true }
libsignal-bridge-macros = { path = "macros" }
signal-chat = { path = "../../chat" }
signal-grpc = { path = "../../grpc" }
signal-quic = { path = "../../quic" }
aes-gcm-siv = "0.10.1"
Expand Down
6 changes: 6 additions & 0 deletions rust/bridge/shared/src/chat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//
// Copyright 2023 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//

pub mod profile;
26 changes: 26 additions & 0 deletions rust/bridge/shared/src/chat/profile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright 2023 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//

use libsignal_bridge_macros::*;
use signal_chat::Result;
use signal_chat::profile::ProfileClient;

use crate::support::*;
use crate::*;

bridge_handle!(ProfileClient, clone = false, mut = true);

#[bridge_fn(ffi = false, node = false)]
pub fn ProfileClient_New(target: String) -> Result<ProfileClient> {
ProfileClient::new(target)
}

#[bridge_fn(ffi = false, node = false)]
pub fn ProfileClient_GetVersionedProfile(
profile_client: &mut ProfileClient,
request: &[u8],
) -> Result<Vec<u8>> {
profile_client.get_versioned_profile(request)
}
19 changes: 1 addition & 18 deletions rust/bridge/shared/src/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//

use libsignal_bridge_macros::*;
use signal_grpc::{GetVersionedProfileRequest, GetVersionedProfileResponse, GrpcClient, GrpcReply, GrpcReplyListener, Result, ServiceIdentifier};
use signal_grpc::{GrpcClient, GrpcReply, GrpcReplyListener, Result};

use crate::support::*;
use crate::*;
Expand All @@ -21,23 +21,6 @@ pub fn GrpcClient_New(target: String) -> Result<GrpcClient> {
GrpcClient::new(target)
}

#[bridge_fn(ffi = false, node = false)]
pub fn GrpcClient_GetVersionedProfile(
grpc_client: &mut GrpcClient,
service_identity_type: u32,
service_identity_uuid: &[u8],
version: String,
) -> Result<GetVersionedProfileResponse> {
let request = GetVersionedProfileRequest {
account_identifier: Some(ServiceIdentifier {
identity_type: service_identity_type as i32,
uuid: service_identity_uuid.to_vec(),
}),
version,
};
grpc_client.get_versioned_profile(request)
}

#[bridge_fn(ffi = false, node = false)]
pub fn GrpcClient_SendDirectMessage(
grpc_client: &mut GrpcClient,
Expand Down
54 changes: 15 additions & 39 deletions rust/bridge/shared/src/jni/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ impl<'a> SimpleArgTypeInfo<'a> for CiphertextMessageRef<'a> {
}

impl<'a> SimpleArgTypeInfo<'a> for crate::grpc::GrpcHeaders {
type ArgType = JavaArgMap<'a>;
type ArgType = JavaMap<'a>;
fn convert_from(env: &JNIEnv, foreign: Self::ArgType) -> SignalJniResult<Self> {
if foreign.is_null() {
return Err(SignalJniError::NullHandle);
Expand All @@ -465,37 +465,6 @@ impl<'a> SimpleArgTypeInfo<'a> for crate::grpc::GrpcHeaders {
}
}

impl ResultTypeInfo for signal_grpc::GetVersionedProfileResponse {
type ResultType = jobject;

fn convert_into(self, env: &JNIEnv) -> SignalJniResult<Self::ResultType> {
let name = env.byte_array_from_slice(&self.name)?;
let about = env.byte_array_from_slice(&self.about)?;
let about_emoji = env.byte_array_from_slice(&self.about_emoji)?;
let avatar = env.new_string(&self.avatar)?;
let payment_address = env.byte_array_from_slice(&self.payment_address)?;
let args = jni_args!((
name => [byte],
about => [byte],
about_emoji => [byte],
avatar => java.lang.String,
payment_address => [byte],
) -> void);
let jobj = env.new_object(
env.find_class(jni_class_name!(org.signal.chat.profile.GetVersionedProfileResponse))?,
args.sig,
&args.args,
)?;
Ok(jobj.into_inner())
}

fn convert_into_jobject(signal_jni_result: &SignalJniResult<Self::ResultType>) -> JObject {
signal_jni_result
.as_ref()
.map_or(JObject::null(), |&jobj| JObject::from(jobj))
}
}

impl ResultTypeInfo for signal_grpc::GrpcReply {
type ResultType = jobject;

Expand All @@ -521,7 +490,7 @@ impl ResultTypeInfo for signal_grpc::GrpcReply {
}

impl<'a> SimpleArgTypeInfo<'a> for crate::quic::QuicHeaders {
type ArgType = JavaArgMap<'a>;
type ArgType = JavaMap<'a>;
fn convert_from(env: &JNIEnv, foreign: Self::ArgType) -> SignalJniResult<Self> {
if foreign.is_null() {
return Err(SignalJniError::NullHandle);
Expand Down Expand Up @@ -875,6 +844,16 @@ impl<T: ResultTypeInfo> ResultTypeInfo for Result<T, device_transfer::Error> {
}
}

impl<T: ResultTypeInfo> ResultTypeInfo for Result<T, signal_chat::Error> {
type ResultType = T::ResultType;
fn convert_into(self, env: &JNIEnv) -> SignalJniResult<Self::ResultType> {
T::convert_into(self?, env)
}
fn convert_into_jobject(signal_jni_result: &SignalJniResult<Self::ResultType>) -> JObject {
<T as ResultTypeInfo>::convert_into_jobject(signal_jni_result)
}
}

impl<T: ResultTypeInfo> ResultTypeInfo for Result<T, signal_grpc::Error> {
type ResultType = T::ResultType;
fn convert_into(self, env: &JNIEnv) -> SignalJniResult<Self::ResultType> {
Expand Down Expand Up @@ -1325,10 +1304,10 @@ macro_rules! jni_arg_type {
jni::JavaUUID
};
(GrpcHeaders) => {
jni::JavaArgMap
jni::JavaMap
};
(QuicHeaders) => {
jni::JavaArgMap
jni::JavaMap
};
(jni::CiphertextMessageRef) => {
jni::JavaCiphertextMessage
Expand Down Expand Up @@ -1416,11 +1395,8 @@ macro_rules! jni_result_type {
(Vec<u8>) => {
jni::jbyteArray
};
(GetVersionedProfileResponse) => {
jni::JavaReturnGetVersionedProfileResponse
};
(GrpcReply) => {
jni::JavaReturnGrpcReply
jni::jbyteArray
};
(Cds2Metrics) => {
jni::JavaReturnMap
Expand Down
Loading

0 comments on commit 76ef3b6

Please sign in to comment.