mirror of
https://github.com/signalapp/libsignal.git
synced 2026-05-10 09:02:05 +02:00
Remove more of SessionState from the Java binding
Leaving only a few bits still directly used by Android
This commit is contained in:
@@ -206,24 +206,26 @@ public final class Native {
|
||||
public static native void SessionRecord_ArchiveCurrentState(long handle);
|
||||
public static native long SessionRecord_Deserialize(byte[] data);
|
||||
public static native void SessionRecord_Destroy(long handle);
|
||||
public static native long SessionRecord_FromSessionState(long sessionState);
|
||||
public static native long SessionRecord_FromSingleSessionState(byte[] sessionState);
|
||||
public static native byte[] SessionRecord_GetAliceBaseKey(long handle);
|
||||
public static native byte[] SessionRecord_GetLocalIdentityKeyPublic(long handle);
|
||||
public static native int SessionRecord_GetLocalRegistrationId(long handle);
|
||||
public static native byte[] SessionRecord_GetReceiverChainKeyValue(long sessionState, long key);
|
||||
public static native byte[] SessionRecord_GetRemoteIdentityKeyPublic(long handle);
|
||||
public static native int SessionRecord_GetRemoteRegistrationId(long handle);
|
||||
public static native byte[] SessionRecord_GetSenderChainKeyValue(long handle);
|
||||
public static native long SessionRecord_GetSessionState(long sessionRecord);
|
||||
public static native int SessionRecord_GetSessionVersion(long handle);
|
||||
public static native boolean SessionRecord_HasSenderChain(long handle);
|
||||
public static native long SessionRecord_InitializeAliceSession(long identityKeyPrivate, long identityKeyPublic, long basePrivate, long basePublic, long theirIdentityKey, long theirSignedPrekey, long theirRatchetKey);
|
||||
public static native long SessionRecord_InitializeBobSession(long identityKeyPrivate, long identityKeyPublic, long signedPrekeyPrivate, long signedPrekeyPublic, long ephPrivate, long ephPublic, long theirIdentityKey, long theirBaseKey);
|
||||
public static native long SessionRecord_NewFresh();
|
||||
public static native byte[] SessionRecord_Serialize(long handle);
|
||||
|
||||
public static native long SessionState_Deserialize(byte[] data);
|
||||
public static native void SessionState_Destroy(long handle);
|
||||
public static native byte[] SessionState_GetAliceBaseKey(long handle);
|
||||
public static native byte[] SessionState_GetLocalIdentityKeyPublic(long handle);
|
||||
public static native int SessionState_GetLocalRegistrationId(long handle);
|
||||
public static native byte[] SessionState_GetReceiverChainKeyValue(long sessionRecord, long key);
|
||||
public static native byte[] SessionState_GetRemoteIdentityKeyPublic(long handle);
|
||||
public static native int SessionState_GetRemoteRegistrationId(long handle);
|
||||
public static native byte[] SessionState_GetSenderChainKeyValue(long handle);
|
||||
public static native int SessionState_GetSessionVersion(long handle);
|
||||
public static native boolean SessionState_HasSenderChain(long handle);
|
||||
public static native byte[] SessionState_InitializeAliceSession(long identityKeyPrivate, long identityKeyPublic, long basePrivate, long basePublic, long theirIdentityKey, long theirSignedPrekey, long theirRatchetKey);
|
||||
public static native byte[] SessionState_InitializeBobSession(long identityKeyPrivate, long identityKeyPublic, long signedPrekeyPrivate, long signedPrekeyPublic, long ephPrivate, long ephPublic, long theirIdentityKey, long theirBaseKey);
|
||||
public static native byte[] SessionState_Serialized(long handle);
|
||||
|
||||
public static native long SignalMessage_Deserialize(byte[] data);
|
||||
|
||||
@@ -5,14 +5,13 @@
|
||||
*/
|
||||
package org.whispersystems.libsignal.state;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.signal.client.internal.Native;
|
||||
import org.whispersystems.libsignal.state.SessionState;
|
||||
import org.whispersystems.libsignal.IdentityKeyPair;
|
||||
import org.whispersystems.libsignal.IdentityKey;
|
||||
import org.whispersystems.libsignal.IdentityKeyPair;
|
||||
import org.whispersystems.libsignal.InvalidKeyException;
|
||||
import org.whispersystems.libsignal.ecc.ECKeyPair;
|
||||
import org.whispersystems.libsignal.ecc.ECPublicKey;
|
||||
import org.whispersystems.libsignal.ecc.ECPrivateKey;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A SessionRecord encapsulates the state of an ongoing session.
|
||||
@@ -25,15 +24,19 @@ public class SessionRecord {
|
||||
|
||||
@Override
|
||||
protected void finalize() {
|
||||
Native.SessionRecord_Destroy(this.handle);
|
||||
Native.SessionRecord_Destroy(this.handle);
|
||||
}
|
||||
|
||||
public SessionRecord() {
|
||||
this.handle = Native.SessionRecord_NewFresh();
|
||||
}
|
||||
|
||||
private SessionRecord(long handle) {
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
public static SessionRecord fromSingleSessionState(byte[] sessionStateBytes) throws IOException {
|
||||
return new SessionRecord(new SessionState(sessionStateBytes));
|
||||
return new SessionRecord(Native.SessionRecord_FromSingleSessionState(sessionStateBytes));
|
||||
}
|
||||
|
||||
public SessionRecord(byte[] serialized) throws IOException {
|
||||
@@ -41,94 +44,110 @@ public class SessionRecord {
|
||||
}
|
||||
|
||||
/**
|
||||
* Move the current {@link SessionState} into the list of "previous" session states,
|
||||
* and replace the current {@link org.whispersystems.libsignal.state.SessionState}
|
||||
* with a fresh reset instance.
|
||||
* Move the current {@link SessionState} into the list of "previous" session states, and replace
|
||||
* the current {@link org.whispersystems.libsignal.state.SessionState} with a fresh reset
|
||||
* instance.
|
||||
*/
|
||||
public void archiveCurrentState() {
|
||||
Native.SessionRecord_ArchiveCurrentState(this.handle);
|
||||
}
|
||||
|
||||
public int getSessionVersion() {
|
||||
// return Native.SessionRecord_GetSessionVersion(this.handle);
|
||||
return getSessionState().getSessionVersion();
|
||||
return Native.SessionRecord_GetSessionVersion(this.handle);
|
||||
}
|
||||
|
||||
public int getRemoteRegistrationId() {
|
||||
// return Native.SessionRecord_GetRemoteRegistrationId(this.handle);
|
||||
return getSessionState().getRemoteRegistrationId();
|
||||
return Native.SessionRecord_GetRemoteRegistrationId(this.handle);
|
||||
}
|
||||
|
||||
public int getLocalRegistrationId() {
|
||||
// return Native.SessionRecord_GetLocalRegistrationId(this.handle);
|
||||
return getSessionState().getLocalRegistrationId();
|
||||
return Native.SessionRecord_GetLocalRegistrationId(this.handle);
|
||||
}
|
||||
|
||||
public IdentityKey getRemoteIdentityKey() {
|
||||
// return Native.SessionRecord_GetRemoteIdentityKey(this.handle);
|
||||
return getSessionState().getRemoteIdentityKey();
|
||||
byte[] keyBytes = Native.SessionRecord_GetRemoteIdentityKeyPublic(this.handle);
|
||||
|
||||
if (keyBytes == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return new IdentityKey(keyBytes);
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
public IdentityKey getLocalIdentityKey() {
|
||||
// return Native.SessionRecord_GetLocalIdentityKey(this.handle);
|
||||
return getSessionState().getLocalIdentityKey();
|
||||
byte[] keyBytes = Native.SessionRecord_GetLocalIdentityKeyPublic(this.handle);
|
||||
try {
|
||||
return new IdentityKey(keyBytes);
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasSenderChain() {
|
||||
return getSessionState().hasSenderChain();
|
||||
return Native.SessionRecord_HasSenderChain(this.handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a serialized version of the current SessionRecord.
|
||||
*/
|
||||
/** @return a serialized version of the current SessionRecord. */
|
||||
public byte[] serialize() {
|
||||
return Native.SessionRecord_Serialize(this.handle);
|
||||
}
|
||||
|
||||
// Following functions are for internal or testing use and may be removed in the future:
|
||||
|
||||
public byte[] getReceiverChainKeyValue(ECPublicKey senderEphemeral) {
|
||||
return getSessionState().getReceiverChainKeyValue(senderEphemeral);
|
||||
}
|
||||
|
||||
public byte[] getSenderChainKeyValue() {
|
||||
return getSessionState().getSenderChainKeyValue();
|
||||
}
|
||||
|
||||
public SessionRecord(SessionState sessionState) {
|
||||
this.handle = Native.SessionRecord_FromSessionState(sessionState.nativeHandle());
|
||||
}
|
||||
|
||||
public SessionState getSessionState() {
|
||||
return new SessionState(Native.SessionRecord_GetSessionState(this.handle));
|
||||
}
|
||||
|
||||
public byte[] getReceiverChainKeyValue(ECPublicKey senderEphemeral) {
|
||||
return Native.SessionRecord_GetReceiverChainKeyValue(
|
||||
this.handle, senderEphemeral.nativeHandle());
|
||||
}
|
||||
|
||||
public byte[] getSenderChainKeyValue() {
|
||||
return Native.SessionRecord_GetSenderChainKeyValue(this.handle);
|
||||
}
|
||||
|
||||
public byte[] getAliceBaseKey() {
|
||||
return getSessionState().getAliceBaseKey();
|
||||
return Native.SessionRecord_GetAliceBaseKey(this.handle);
|
||||
}
|
||||
|
||||
static public SessionRecord initializeAliceSession(IdentityKeyPair identityKey,
|
||||
ECKeyPair baseKey,
|
||||
IdentityKey theirIdentityKey,
|
||||
ECPublicKey theirSignedPreKey,
|
||||
ECPublicKey theirRatchetKey) {
|
||||
return new SessionRecord(SessionState.initializeAliceSession(identityKey, baseKey,
|
||||
theirIdentityKey,
|
||||
theirSignedPreKey,
|
||||
theirRatchetKey));
|
||||
public static SessionRecord initializeAliceSession(
|
||||
IdentityKeyPair identityKey,
|
||||
ECKeyPair baseKey,
|
||||
IdentityKey theirIdentityKey,
|
||||
ECPublicKey theirSignedPreKey,
|
||||
ECPublicKey theirRatchetKey) {
|
||||
return new SessionRecord(
|
||||
Native.SessionRecord_InitializeAliceSession(
|
||||
identityKey.getPrivateKey().nativeHandle(),
|
||||
identityKey.getPublicKey().getPublicKey().nativeHandle(),
|
||||
baseKey.getPrivateKey().nativeHandle(),
|
||||
baseKey.getPublicKey().nativeHandle(),
|
||||
theirIdentityKey.getPublicKey().nativeHandle(),
|
||||
theirSignedPreKey.nativeHandle(),
|
||||
theirRatchetKey.nativeHandle()));
|
||||
}
|
||||
|
||||
static public SessionRecord initializeBobSession(IdentityKeyPair identityKey,
|
||||
ECKeyPair signedPreKey,
|
||||
ECKeyPair ephemeralKey,
|
||||
IdentityKey theirIdentityKey,
|
||||
ECPublicKey theirBaseKey) {
|
||||
|
||||
return new SessionRecord(SessionState.initializeBobSession(identityKey,
|
||||
signedPreKey,
|
||||
ephemeralKey,
|
||||
theirIdentityKey,
|
||||
theirBaseKey));
|
||||
public static SessionRecord initializeBobSession(
|
||||
IdentityKeyPair identityKey,
|
||||
ECKeyPair signedPreKey,
|
||||
ECKeyPair ephemeralKey,
|
||||
IdentityKey theirIdentityKey,
|
||||
ECPublicKey theirBaseKey) {
|
||||
return new SessionRecord(
|
||||
Native.SessionRecord_InitializeBobSession(
|
||||
identityKey.getPrivateKey().nativeHandle(),
|
||||
identityKey.getPublicKey().getPublicKey().nativeHandle(),
|
||||
signedPreKey.getPrivateKey().nativeHandle(),
|
||||
signedPreKey.getPublicKey().nativeHandle(),
|
||||
ephemeralKey.getPrivateKey().nativeHandle(),
|
||||
ephemeralKey.getPublicKey().nativeHandle(),
|
||||
theirIdentityKey.getPublicKey().nativeHandle(),
|
||||
theirBaseKey.nativeHandle()));
|
||||
}
|
||||
|
||||
long nativeHandle() {
|
||||
|
||||
@@ -7,13 +7,7 @@
|
||||
package org.whispersystems.libsignal.state;
|
||||
|
||||
import org.signal.client.internal.Native;
|
||||
import org.whispersystems.libsignal.IdentityKey;
|
||||
import org.whispersystems.libsignal.IdentityKeyPair;
|
||||
import org.whispersystems.libsignal.ecc.ECKeyPair;
|
||||
import org.whispersystems.libsignal.ecc.ECPublicKey;
|
||||
import org.whispersystems.libsignal.InvalidKeyException;
|
||||
import java.io.IOException;
|
||||
import static org.whispersystems.libsignal.state.StorageProtos.SessionStructure;
|
||||
|
||||
public class SessionState {
|
||||
private long handle;
|
||||
@@ -23,108 +17,24 @@ public class SessionState {
|
||||
Native.SessionState_Destroy(this.handle);
|
||||
}
|
||||
|
||||
static public SessionState initializeAliceSession(IdentityKeyPair identityKey,
|
||||
ECKeyPair baseKey,
|
||||
IdentityKey theirIdentityKey,
|
||||
ECPublicKey theirSignedPreKey,
|
||||
ECPublicKey theirRatchetKey) {
|
||||
try {
|
||||
return new SessionState(Native.SessionState_InitializeAliceSession(identityKey.getPrivateKey().nativeHandle(),
|
||||
identityKey.getPublicKey().getPublicKey().nativeHandle(),
|
||||
baseKey.getPrivateKey().nativeHandle(),
|
||||
baseKey.getPublicKey().nativeHandle(),
|
||||
theirIdentityKey.getPublicKey().nativeHandle(),
|
||||
theirSignedPreKey.nativeHandle(),
|
||||
theirRatchetKey.nativeHandle()));
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
static public SessionState initializeBobSession(IdentityKeyPair identityKey,
|
||||
ECKeyPair signedPreKey,
|
||||
ECKeyPair ephemeralKey,
|
||||
IdentityKey theirIdentityKey,
|
||||
ECPublicKey theirBaseKey) {
|
||||
try {
|
||||
return new SessionState(Native.SessionState_InitializeBobSession(identityKey.getPrivateKey().nativeHandle(),
|
||||
identityKey.getPublicKey().getPublicKey().nativeHandle(),
|
||||
signedPreKey.getPrivateKey().nativeHandle(),
|
||||
signedPreKey.getPublicKey().nativeHandle(),
|
||||
ephemeralKey.getPrivateKey().nativeHandle(),
|
||||
ephemeralKey.getPublicKey().nativeHandle(),
|
||||
theirIdentityKey.getPublicKey().nativeHandle(),
|
||||
theirBaseKey.nativeHandle()));
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
public SessionState(byte[] serialized) throws IOException {
|
||||
this.handle = Native.SessionState_Deserialize(serialized);
|
||||
}
|
||||
|
||||
public SessionState(SessionStructure sessionStructure) {
|
||||
this.handle = Native.SessionState_Deserialize(sessionStructure.toByteArray());
|
||||
}
|
||||
|
||||
SessionState(long handle) {
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
byte[] getAliceBaseKey() {
|
||||
return Native.SessionState_GetAliceBaseKey(this.handle);
|
||||
}
|
||||
|
||||
// Used by Android
|
||||
public int getSessionVersion() {
|
||||
return Native.SessionState_GetSessionVersion(this.handle);
|
||||
}
|
||||
|
||||
IdentityKey getRemoteIdentityKey() {
|
||||
byte[] keyBytes = Native.SessionState_GetRemoteIdentityKeyPublic(this.handle);
|
||||
|
||||
if (keyBytes == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return new IdentityKey(keyBytes);
|
||||
}
|
||||
catch(InvalidKeyException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
IdentityKey getLocalIdentityKey() {
|
||||
byte[] keyBytes = Native.SessionState_GetLocalIdentityKeyPublic(this.handle);
|
||||
try {
|
||||
return new IdentityKey(keyBytes);
|
||||
}
|
||||
catch(InvalidKeyException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Used by Android
|
||||
public boolean hasSenderChain() {
|
||||
return Native.SessionState_HasSenderChain(this.handle);
|
||||
}
|
||||
|
||||
byte[] getReceiverChainKeyValue(ECPublicKey senderEphemeral) {
|
||||
return Native.SessionState_GetReceiverChainKeyValue(this.handle, senderEphemeral.nativeHandle());
|
||||
}
|
||||
|
||||
byte[] getSenderChainKeyValue() {
|
||||
return Native.SessionState_GetSenderChainKeyValue(this.handle);
|
||||
}
|
||||
|
||||
int getRemoteRegistrationId() {
|
||||
return Native.SessionState_GetRemoteRegistrationId(this.handle);
|
||||
}
|
||||
|
||||
int getLocalRegistrationId() {
|
||||
return Native.SessionState_GetLocalRegistrationId(this.handle);
|
||||
}
|
||||
|
||||
public byte[] serialize() {
|
||||
return Native.SessionState_Serialized(this.handle);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user