Add serialization for inbound group session

- remove compiler warnings when logs are not enabled
- new getInstanceId() function to refactor code
This commit is contained in:
pedroGitt 2016-10-23 23:55:45 +02:00
parent fae857582c
commit 332d9d0c09
16 changed files with 490 additions and 292 deletions

View file

@ -28,7 +28,8 @@ import static org.junit.Assert.assertTrue;
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OlmGroupSessionTest { public class OlmGroupSessionTest {
private static final String LOG_TAG = "OlmSessionTest"; private static final String LOG_TAG = "OlmSessionTest";
private final String FILE_NAME_SERIAL_SESSION = "SerialGroupSession"; private final String FILE_NAME_SERIAL_OUT_SESSION = "SerialOutGroupSession";
private final String FILE_NAME_SERIAL_IN_SESSION = "SerialInGroupSession";
private static OlmManager mOlmManager; private static OlmManager mOlmManager;
private static OlmOutboundGroupSession mAliceOutboundGroupSession; private static OlmOutboundGroupSession mAliceOutboundGroupSession;
@ -158,27 +159,26 @@ public class OlmGroupSessionTest {
OlmOutboundGroupSession outboundGroupSessionRef=null; OlmOutboundGroupSession outboundGroupSessionRef=null;
OlmOutboundGroupSession outboundGroupSessionSerial=null; OlmOutboundGroupSession outboundGroupSessionSerial=null;
// alice creates OUTBOUND GROUP SESSION // create one OUTBOUND GROUP SESSION
try { try {
outboundGroupSessionRef = new OlmOutboundGroupSession(); outboundGroupSessionRef = new OlmOutboundGroupSession();
} catch (OlmException e) { } catch (OlmException e) {
assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false); assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false);
} }
assertNotNull(outboundGroupSessionRef); assertNotNull(outboundGroupSessionRef);
// serialize alice session // serialize alice session
Context context = getInstrumentation().getContext(); Context context = getInstrumentation().getContext();
try { try {
FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_SESSION, Context.MODE_PRIVATE); FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_OUT_SESSION, Context.MODE_PRIVATE);
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput); ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(outboundGroupSessionRef); objectOutput.writeObject(outboundGroupSessionRef);
objectOutput.flush(); objectOutput.flush();
objectOutput.close(); objectOutput.close();
// deserialize session // deserialize session
FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_SESSION); FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_OUT_SESSION);
ObjectInputStream objectInput = new ObjectInputStream(fileInput); ObjectInputStream objectInput = new ObjectInputStream(fileInput);
outboundGroupSessionSerial = (OlmOutboundGroupSession) objectInput.readObject(); outboundGroupSessionSerial = (OlmOutboundGroupSession) objectInput.readObject();
objectInput.close(); objectInput.close();
@ -204,6 +204,9 @@ public class OlmGroupSessionTest {
// session IDs comparison // session IDs comparison
assertTrue(sessionIdRef.equals(sessionIdSerial)); assertTrue(sessionIdRef.equals(sessionIdSerial));
outboundGroupSessionRef.releaseSession();
outboundGroupSessionSerial.releaseSession();
} }
catch (FileNotFoundException e) { catch (FileNotFoundException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception FileNotFoundException Msg=="+e.getMessage()); Log.e(LOG_TAG, "## test03SessionSerialization(): Exception FileNotFoundException Msg=="+e.getMessage());
@ -223,5 +226,78 @@ public class OlmGroupSessionTest {
} }
@Test
public void test15SerializeInboundSession() {
OlmOutboundGroupSession aliceOutboundGroupSession=null;
OlmInboundGroupSession bobInboundGroupSessionRef=null;
OlmInboundGroupSession bobInboundGroupSessionSerial=null;
// alice creates OUTBOUND GROUP SESSION
try {
aliceOutboundGroupSession = new OlmOutboundGroupSession();
} catch (OlmException e) {
assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false);
}
assertNotNull(aliceOutboundGroupSession);
// get the session key from the outbound group session
String sessionKeyRef = aliceOutboundGroupSession.sessionKey();
assertNotNull(sessionKeyRef);
// bob creates INBOUND GROUP SESSION
try {
bobInboundGroupSessionRef = new OlmInboundGroupSession(sessionKeyRef);
} catch (OlmException e) {
assertTrue("Exception in OlmInboundGroupSession, Exception code=" + e.getExceptionCode(), false);
}
assertNotNull(bobInboundGroupSessionRef);
// serialize alice session
Context context = getInstrumentation().getContext();
try {
FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_IN_SESSION, Context.MODE_PRIVATE);
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(bobInboundGroupSessionRef);
objectOutput.flush();
objectOutput.close();
// deserialize session
FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_OUT_SESSION);
ObjectInputStream objectInput = new ObjectInputStream(fileInput);
bobInboundGroupSessionSerial = (OlmInboundGroupSession)objectInput.readObject();
objectInput.close();
// get sessions IDs
String aliceSessionId = aliceOutboundGroupSession.sessionIdentifier();
String sessionIdRef = bobInboundGroupSessionRef.sessionIdentifier();
String sessionIdSerial = bobInboundGroupSessionSerial.sessionIdentifier();
// session ID sanity check
assertFalse(TextUtils.isEmpty(aliceSessionId));
assertFalse(TextUtils.isEmpty(sessionIdRef));
assertFalse(TextUtils.isEmpty(sessionIdSerial));
// session IDs comparison
assertTrue(aliceSessionId.equals(sessionIdSerial));
assertTrue(sessionIdRef.equals(sessionIdSerial));
}
catch (FileNotFoundException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception FileNotFoundException Msg=="+e.getMessage());
}
catch (ClassNotFoundException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception ClassNotFoundException Msg==" + e.getMessage());
}
catch (IOException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception IOException Msg==" + e.getMessage());
}
/*catch (OlmException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception OlmException Msg==" + e.getMessage());
}*/
catch (Exception e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception Msg==" + e.getMessage());
}
}
} }

View file

@ -26,7 +26,6 @@ import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.Random;
public class OlmAccount implements Serializable { public class OlmAccount implements Serializable {
private static final long serialVersionUID = 3497486121598434824L; private static final long serialVersionUID = 3497486121598434824L;
@ -52,7 +51,7 @@ public class OlmAccount implements Serializable {
/** account raw pointer value (OlmAccount*) returned by JNI. /** account raw pointer value (OlmAccount*) returned by JNI.
* this value identifies uniquely the native account instance. * this value identifies uniquely the native account instance.
*/ */
private transient long mNativeOlmAccountId; private transient long mNativeId;
public OlmAccount() throws OlmException { public OlmAccount() throws OlmException {
@ -179,7 +178,7 @@ public class OlmAccount implements Serializable {
* @return native account ID * @return native account ID
*/ */
public long getOlmAccountId(){ public long getOlmAccountId(){
return mNativeOlmAccountId; return mNativeId;
} }
/** /**
@ -189,7 +188,7 @@ public class OlmAccount implements Serializable {
public void releaseAccount(){ public void releaseAccount(){
releaseAccountJni(); releaseAccountJni();
mNativeOlmAccountId = 0; mNativeId = 0;
} }
/** /**
@ -208,7 +207,7 @@ public class OlmAccount implements Serializable {
*/ */
private boolean initNewAccount() { private boolean initNewAccount() {
boolean retCode = false; boolean retCode = false;
if(0 != (mNativeOlmAccountId = initNewAccountJni())){ if(0 != (mNativeId = initNewAccountJni())){
retCode = true; retCode = true;
} }
return retCode; return retCode;
@ -217,7 +216,7 @@ public class OlmAccount implements Serializable {
/** /**
* Create and initialize an OLM account in native side.<br> * Create and initialize an OLM account in native side.<br>
* Do not forget to call {@link #releaseAccount()} when JAVA side is done. * Do not forget to call {@link #releaseAccount()} when JAVA side is done.
* @return native account instance identifier (see {@link #mNativeOlmAccountId}) * @return native account instance identifier (see {@link #mNativeId})
*/ */
private native long initNewAccountJni(); private native long initNewAccountJni();
@ -230,7 +229,7 @@ public class OlmAccount implements Serializable {
*/ */
private boolean createNewAccount() { private boolean createNewAccount() {
boolean retCode = false; boolean retCode = false;
if(0 != (mNativeOlmAccountId = createNewAccountJni())){ if(0 != (mNativeId = createNewAccountJni())){
retCode = true; retCode = true;
} }
return retCode; return retCode;
@ -239,7 +238,7 @@ public class OlmAccount implements Serializable {
/** /**
* Create an OLM account in native side.<br> * Create an OLM account in native side.<br>
* Do not forget to call {@link #releaseAccount()} when JAVA side is done. * Do not forget to call {@link #releaseAccount()} when JAVA side is done.
* @return native account instance identifier (see {@link #mNativeOlmAccountId}) * @return native account instance identifier (see {@link #mNativeId})
*/ */
private native long createNewAccountJni(); private native long createNewAccountJni();

View file

@ -33,6 +33,8 @@ public class OlmException extends Exception {
public static final int EXCEPTION_CODE_INIT_SESSION_CREATION = 9; public static final int EXCEPTION_CODE_INIT_SESSION_CREATION = 9;
public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_SERIALIZATION = 10; public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_SERIALIZATION = 10;
public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_DESERIALIZATION = 11; public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_DESERIALIZATION = 11;
public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_SERIALIZATION = 12;
public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_DESERIALIZATION = 13;
// exception human readable messages // exception human readable messages
public static final String EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION = "failed to create a new outbound group Session"; public static final String EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION = "failed to create a new outbound group Session";

View file

@ -23,6 +23,9 @@ package org.matrix.olm;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
public class OlmInboundGroupSession implements Serializable { public class OlmInboundGroupSession implements Serializable {
@ -32,26 +35,18 @@ public class OlmInboundGroupSession implements Serializable {
/** session raw pointer value returned by JNI.<br> /** session raw pointer value returned by JNI.<br>
* this value uniquely identifies the native inbound group session instance. * this value uniquely identifies the native inbound group session instance.
*/ */
private long mNativeOlmInboundGroupSessionId; private transient long mNativeId;
/**
* Getter on the native inbound group session ID.
* @return native inbound group session ID
*/
public long getOlmInboundGroupSessionId() {
return mNativeOlmInboundGroupSessionId;
}
/** /**
* Constructor.<br> * Constructor.<br>
* Create and save a new native session instance ID and start a new inbound group session. * Create and save a new native session instance ID and start a new inbound group session.
* The session key parameter is retrieved from an outbound group session * The session key parameter is retrieved from an outbound group session
* See {@link #initNewSession()} and {@link #initInboundGroupSessionWithSessionKey(String)} * See {@link #createNewSession()} and {@link #initInboundGroupSessionWithSessionKey(String)}
* @param aSessionKey session key * @param aSessionKey session key
* @throws OlmException constructor failure * @throws OlmException constructor failure
*/ */
public OlmInboundGroupSession(String aSessionKey) throws OlmException { public OlmInboundGroupSession(String aSessionKey) throws OlmException {
if(initNewSession()) { if(createNewSession()) {
if( 0 != initInboundGroupSessionWithSessionKey(aSessionKey)) { if( 0 != initInboundGroupSessionWithSessionKey(aSessionKey)) {
releaseSession();// prevent memory leak before throwing releaseSession();// prevent memory leak before throwing
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION,OlmException.EXCEPTION_MSG_INIT_INBOUND_GROUP_SESSION); throw new OlmException(OlmException.EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION,OlmException.EXCEPTION_MSG_INIT_INBOUND_GROUP_SESSION);
@ -68,14 +63,14 @@ public class OlmInboundGroupSession implements Serializable {
public void releaseSession(){ public void releaseSession(){
releaseSessionJni(); releaseSessionJni();
mNativeOlmInboundGroupSessionId = 0; mNativeId = 0;
} }
/** /**
* Destroy the corresponding OLM inbound group session native object.<br> * Destroy the corresponding OLM inbound group session native object.<br>
* This method must ALWAYS be called when this JAVA instance * This method must ALWAYS be called when this JAVA instance
* is destroyed (ie. garbage collected) to prevent memory leak in native side. * is destroyed (ie. garbage collected) to prevent memory leak in native side.
* See {@link #initNewSessionJni()}. * See {@link #createNewSessionJni()}.
*/ */
private native void releaseSessionJni(); private native void releaseSessionJni();
@ -84,9 +79,9 @@ public class OlmInboundGroupSession implements Serializable {
* To be called before any other API call. * To be called before any other API call.
* @return true if init succeed, false otherwise. * @return true if init succeed, false otherwise.
*/ */
private boolean initNewSession() { private boolean createNewSession() {
boolean retCode = false; boolean retCode = false;
if(0 != (mNativeOlmInboundGroupSessionId = initNewSessionJni())){ if(0 != (mNativeId = createNewSessionJni())){
retCode = true; retCode = true;
} }
return retCode; return retCode;
@ -95,13 +90,13 @@ public class OlmInboundGroupSession implements Serializable {
/** /**
* Create the corresponding OLM inbound group session in native side.<br> * Create the corresponding OLM inbound group session in native side.<br>
* Do not forget to call {@link #releaseSession()} when JAVA side is done. * Do not forget to call {@link #releaseSession()} when JAVA side is done.
* @return native session instance identifier (see {@link #mNativeOlmInboundGroupSessionId}) * @return native session instance identifier (see {@link #mNativeId})
*/ */
private native long initNewSessionJni(); private native long createNewSessionJni();
/** /**
* Start a new inbound group session.<br> * Start a new inbound group session.<br>
* The session key parameter is retrieved from a outbound group session * The session key parameter is retrieved from an outbound group session
* see {@link OlmOutboundGroupSession#sessionKey()} * see {@link OlmOutboundGroupSession#sessionKey()}
* @param aSessionKey session key * @param aSessionKey session key
* @return 0 if operation succeed, -1 otherwise * @return 0 if operation succeed, -1 otherwise
@ -132,8 +127,128 @@ public class OlmInboundGroupSession implements Serializable {
private native String decryptMessageJni(String aEncryptedMsg); private native String decryptMessageJni(String aEncryptedMsg);
// TODO missing API: initWithSerializedData /**
// TODO missing API: serializeDataWithKey * Kick off the serialization mechanism.
// TODO missing API: initWithCoder * @param aOutStream output stream for serializing
// TODO missing API: encodeWithCoder * @throws IOException
* @throws OlmException
*/
private void writeObject(ObjectOutputStream aOutStream) throws IOException, OlmException {
aOutStream.defaultWriteObject();
// generate serialization key
String key = OlmUtility.getRandomKey();
// compute pickle string
StringBuffer errorMsg = new StringBuffer();
String pickledData = serializeDataWithKey(key, errorMsg);
if(null == pickledData) {
throw new OlmException(OlmException.EXCEPTION_CODE_INBOUND_GROUP_SESSION_SERIALIZATION, String.valueOf(errorMsg));
} else {
aOutStream.writeObject(key);
aOutStream.writeObject(pickledData);
}
}
/**
* Kick off the deserialization mechanism.
* @param aInStream
* @throws IOException
* @throws ClassNotFoundException
* @throws OlmException
*/
private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException, OlmException {
aInStream.defaultReadObject();
StringBuffer errorMsg = new StringBuffer();
String key = (String) aInStream.readObject();
String pickledData = (String) aInStream.readObject();
if(TextUtils.isEmpty(key)) {
throw new OlmException(OlmException.EXCEPTION_CODE_INBOUND_GROUP_SESSION_DESERIALIZATION, OlmException.EXCEPTION_MSG_INVALID_PARAMS_DESERIALIZATION+" key");
} else if(TextUtils.isEmpty(pickledData)) {
throw new OlmException(OlmException.EXCEPTION_CODE_INBOUND_GROUP_SESSION_DESERIALIZATION, OlmException.EXCEPTION_MSG_INVALID_PARAMS_DESERIALIZATION+" pickle");
} else if(!createNewSession()) {
throw new OlmException(OlmException.EXCEPTION_CODE_INBOUND_GROUP_SESSION_DESERIALIZATION, OlmException.EXCEPTION_MSG_INIT_NEW_ACCOUNT_DESERIALIZATION);
} else if(!initWithSerializedData(pickledData, key, errorMsg)) {
releaseSession(); // prevent memory leak
throw new OlmException(OlmException.EXCEPTION_CODE_INBOUND_GROUP_SESSION_DESERIALIZATION, String.valueOf(errorMsg));
} else {
Log.d(LOG_TAG,"## readObject(): success");
}
}
/**
* Return a session as a base64 string.<br>
* The account is serialized and encrypted with aKey.
* In case of failure, an error human readable
* description is provide in aErrorMsg.
* @param aKey encryption key
* @param aErrorMsg error message description
* @return pickled base64 string if operation succeed, null otherwise
*/
private String serializeDataWithKey(String aKey, StringBuffer aErrorMsg) {
String pickleRetValue = null;
// sanity check
if(null == aErrorMsg) {
Log.e(LOG_TAG,"## serializeDataWithKey(): invalid parameter - aErrorMsg=null");
} else if(TextUtils.isEmpty(aKey)) {
aErrorMsg.append("Invalid input parameters in serializeDataWithKey()");
} else {
aErrorMsg.setLength(0);
pickleRetValue = serializeDataWithKeyJni(aKey, aErrorMsg);
}
return pickleRetValue;
}
/**
* JNI counter part of {@link #serializeDataWithKey(String, StringBuffer)}.
* @param aKey encryption key
* @param aErrorMsg error message description
* @return pickled base64 string if operation succeed, null otherwise
*/
private native String serializeDataWithKeyJni(String aKey, StringBuffer aErrorMsg);
/**
* Loads an account from a pickled base64 string.<br>
* See {@link #serializeDataWithKey(String, StringBuffer)}
* @param aSerializedData pickled account in a base64 string format
* @param aKey key used to encrypted
* @param aErrorMsg error message description
* @return true if operation succeed, false otherwise
*/
private boolean initWithSerializedData(String aSerializedData, String aKey, StringBuffer aErrorMsg) {
boolean retCode = false;
String jniError;
if(null == aErrorMsg) {
Log.e(LOG_TAG, "## initWithSerializedData(): invalid input error parameter");
} else {
aErrorMsg.setLength(0);
if (TextUtils.isEmpty(aSerializedData) || TextUtils.isEmpty(aKey)) {
Log.e(LOG_TAG, "## initWithSerializedData(): invalid input parameters");
} else if (null == (jniError = initWithSerializedDataJni(aSerializedData, aKey))) {
retCode = true;
} else {
aErrorMsg.append(jniError);
}
}
return retCode;
}
/**
* JNI counter part of {@link #initWithSerializedData(String, String, StringBuffer)}.
* @param aSerializedData pickled account in a base64 string format
* @param aKey key used to encrypted
* @return null if operation succeed, an error message if operation failed
*/
private native String initWithSerializedDataJni(String aSerializedData, String aKey);
} }

View file

@ -32,25 +32,17 @@ public class OlmOutboundGroupSession implements Serializable {
/** session raw pointer value returned by JNI.<br> /** session raw pointer value returned by JNI.<br>
* this value uniquely identifies the native inbound group session instance. * this value uniquely identifies the native inbound group session instance.
*/ */
private long mNativeOlmOutboundGroupSessionId; private transient long mNativeId;
/**
* Getter on the native outbound group session ID.
* @return native outbound group session ID
*/
public long getOlmInboundGroupSessionId(){
return mNativeOlmOutboundGroupSessionId;
}
/** /**
* Constructor.<br> * Constructor.<br>
* Create and save a new session native instance ID and * Create and save a new session native instance ID and
* initialise a new outbound group session.<br> * initialise a new outbound group session.<br>
* See {@link #initNewSession()} and {@link #initOutboundGroupSession()} * See {@link #createNewSession()} and {@link #initOutboundGroupSession()}
* @throws OlmException constructor failure * @throws OlmException constructor failure
*/ */
public OlmOutboundGroupSession() throws OlmException { public OlmOutboundGroupSession() throws OlmException {
if(initNewSession()) { if(createNewSession()) {
if( 0 != initOutboundGroupSession()) { if( 0 != initOutboundGroupSession()) {
releaseSession();// prevent memory leak before throwing releaseSession();// prevent memory leak before throwing
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION, OlmException.EXCEPTION_MSG_INIT_OUTBOUND_GROUP_SESSION); throw new OlmException(OlmException.EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION, OlmException.EXCEPTION_MSG_INIT_OUTBOUND_GROUP_SESSION);
@ -104,7 +96,7 @@ public class OlmOutboundGroupSession implements Serializable {
} else if(TextUtils.isEmpty(pickledData)) { } else if(TextUtils.isEmpty(pickledData)) {
throw new OlmException(OlmException.EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_DESERIALIZATION, OlmException.EXCEPTION_MSG_INVALID_PARAMS_DESERIALIZATION+" pickle"); throw new OlmException(OlmException.EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_DESERIALIZATION, OlmException.EXCEPTION_MSG_INVALID_PARAMS_DESERIALIZATION+" pickle");
} else if(!initNewSession()) { } else if(!createNewSession()) {
throw new OlmException(OlmException.EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_DESERIALIZATION, OlmException.EXCEPTION_MSG_INIT_NEW_ACCOUNT_DESERIALIZATION); throw new OlmException(OlmException.EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_DESERIALIZATION, OlmException.EXCEPTION_MSG_INIT_NEW_ACCOUNT_DESERIALIZATION);
} else if(!initWithSerializedData(pickledData, key, errorMsg)) { } else if(!initWithSerializedData(pickledData, key, errorMsg)) {
@ -180,26 +172,26 @@ public class OlmOutboundGroupSession implements Serializable {
*/ */
public void releaseSession() { public void releaseSession() {
releaseSessionJni(); releaseSessionJni();
mNativeOlmOutboundGroupSessionId = 0; mNativeId = 0;
} }
/** /**
* Destroy the corresponding OLM outbound group session native object.<br> * Destroy the corresponding OLM outbound group session native object.<br>
* This method must ALWAYS be called when this JAVA instance * This method must ALWAYS be called when this JAVA instance
* is destroyed (ie. garbage collected) to prevent memory leak in native side. * is destroyed (ie. garbage collected) to prevent memory leak in native side.
* See {@link #initNewSessionJni()}. * See {@link #createNewSessionJni()}.
*/ */
private native void releaseSessionJni(); private native void releaseSessionJni();
/** /**
* Create and save the session native instance ID. * Create and save the session native instance ID.
* Wrapper for {@link #initNewSessionJni()}.<br> * Wrapper for {@link #createNewSessionJni()}.<br>
* To be called before any other API call. * To be called before any other API call.
* @return true if init succeed, false otherwise. * @return true if init succeed, false otherwise.
*/ */
private boolean initNewSession() { private boolean createNewSession() {
boolean retCode = false; boolean retCode = false;
if(0 != (mNativeOlmOutboundGroupSessionId = initNewSessionJni())){ if(0 != (mNativeId = createNewSessionJni())){
retCode = true; retCode = true;
} }
return retCode; return retCode;
@ -208,9 +200,9 @@ public class OlmOutboundGroupSession implements Serializable {
/** /**
* Create the corresponding OLM outbound group session in native side.<br> * Create the corresponding OLM outbound group session in native side.<br>
* Do not forget to call {@link #releaseSession()} when JAVA side is done. * Do not forget to call {@link #releaseSession()} when JAVA side is done.
* @return native session instance identifier (see {@link #mNativeOlmOutboundGroupSessionId}) * @return native session instance identifier (see {@link #mNativeId})
*/ */
private native long initNewSessionJni(); private native long createNewSessionJni();
/** /**
* Start a new outbound group session.<br> * Start a new outbound group session.<br>

View file

@ -31,7 +31,7 @@ public class OlmSession implements Serializable {
/** session raw pointer value (OlmSession*) returned by JNI. /** session raw pointer value (OlmSession*) returned by JNI.
* this value uniquely identifies the native session instance. * this value uniquely identifies the native session instance.
**/ **/
private transient long mNativeOlmSessionId; private transient long mNativeId;
public OlmSession() throws OlmException { public OlmSession() throws OlmException {
@ -158,7 +158,7 @@ public class OlmSession implements Serializable {
* @return native session ID * @return native session ID
*/ */
public long getOlmSessionId(){ public long getOlmSessionId(){
return mNativeOlmSessionId; return mNativeId;
} }
/** /**
@ -176,7 +176,7 @@ public class OlmSession implements Serializable {
public void releaseSession(){ public void releaseSession(){
releaseSessionJni(); releaseSessionJni();
mNativeOlmSessionId = 0; mNativeId = 0;
} }
/** /**
@ -187,7 +187,7 @@ public class OlmSession implements Serializable {
*/ */
private boolean initNewSession() { private boolean initNewSession() {
boolean retCode = false; boolean retCode = false;
if(0 != (mNativeOlmSessionId = initNewSessionJni())){ if(0 != (mNativeId = initNewSessionJni())){
retCode = true; retCode = true;
} }
return retCode; return retCode;
@ -196,7 +196,7 @@ public class OlmSession implements Serializable {
/** /**
* Create the corresponding OLM session in native side.<br> * Create the corresponding OLM session in native side.<br>
* Do not forget to call {@link #releaseSession()} when JAVA side is done. * Do not forget to call {@link #releaseSession()} when JAVA side is done.
* @return native session instance identifier (see {@link #mNativeOlmSessionId}) * @return native session instance identifier (see {@link #mNativeId})
*/ */
private native long initNewSessionJni(); private native long initNewSessionJni();
@ -210,7 +210,7 @@ public class OlmSession implements Serializable {
*/ */
private boolean createNewSession() { private boolean createNewSession() {
boolean retCode = false; boolean retCode = false;
if(0 != (mNativeOlmSessionId = createNewSessionJni())){ if(0 != (mNativeId = createNewSessionJni())){
retCode = true; retCode = true;
} }
return retCode; return retCode;
@ -219,7 +219,7 @@ public class OlmSession implements Serializable {
/** /**
* Create an OLM account in native side.<br> * Create an OLM account in native side.<br>
* Do not forget to call {@link #releaseSession()} when JAVA side is done. * Do not forget to call {@link #releaseSession()} when JAVA side is done.
* @return native account instance identifier (see {@link #mNativeOlmSessionId}) * @return native account instance identifier (see {@link #mNativeId})
*/ */
private native long createNewSessionJni(); private native long createNewSessionJni();

View file

@ -19,7 +19,6 @@ package org.matrix.olm;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import java.io.Serializable;
import java.util.Random; import java.util.Random;
public class OlmUtility { public class OlmUtility {
@ -31,20 +30,12 @@ public class OlmUtility {
/** raw pointer value returned by JNI. /** raw pointer value returned by JNI.
* this value uniquely identifies this utility instance. * this value uniquely identifies this utility instance.
**/ **/
private long mNativeOlmUtilityId; private long mNativeId;
public OlmUtility() { public OlmUtility() {
initUtility(); initUtility();
} }
/**
* Getter on the session ID.
* @return native session ID
*/
public long getOlmUtilityId(){
return mNativeOlmUtilityId;
}
/** /**
* Create a native utility instance. * Create a native utility instance.
* To be called before any other API call. * To be called before any other API call.
@ -52,7 +43,7 @@ public class OlmUtility {
*/ */
private boolean initUtility() { private boolean initUtility() {
boolean retCode = false; boolean retCode = false;
if(0 != (mNativeOlmUtilityId = initUtilityJni())){ if(0 != (mNativeId = initUtilityJni())){
retCode = true; retCode = true;
} }
return retCode; return retCode;
@ -65,7 +56,7 @@ public class OlmUtility {
*/ */
public void releaseUtility(){ public void releaseUtility(){
releaseUtilityJni(); releaseUtilityJni();
mNativeOlmUtilityId = 0; mNativeId = 0;
} }
private native void releaseUtilityJni(); private native void releaseUtilityJni();

View file

@ -119,8 +119,7 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi
// create account // create account
accountRetCode = olm_create_account(accountPtr, (void*)randomBuffPtr, randomSize); accountRetCode = olm_create_account(accountPtr, (void*)randomBuffPtr, randomSize);
if(accountRetCode == olm_error()) { if(accountRetCode == olm_error()) {
const char *errorMsgPtr = olm_account_last_error(accountPtr); LOGE("## initNewAccount(): failure - account creation failed Msg=%s", (const char *)olm_account_last_error(accountPtr));
LOGE("## initNewAccount(): failure - account creation failed Msg=%s", errorMsgPtr);
} }
LOGD("## initNewAccount(): success - OLM account created"); LOGD("## initNewAccount(): success - OLM account created");
@ -171,8 +170,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject
{ // retrieve key pairs in identityKeysBytesPtr { // retrieve key pairs in identityKeysBytesPtr
keysResult = olm_account_identity_keys(accountPtr, identityKeysBytesPtr, identityKeysLength); keysResult = olm_account_identity_keys(accountPtr, identityKeysBytesPtr, identityKeysLength);
if(keysResult == olm_error()) { if(keysResult == olm_error()) {
const char *errorMsgPtr = olm_account_last_error(accountPtr); LOGE("## identityKeys(): failure - error getting identity keys Msg=%s",(const char *)olm_account_last_error(accountPtr));
LOGE("## identityKeys(): failure - error getting identity keys Msg=%s",errorMsgPtr);
} }
else else
{ // allocate the byte array to be returned to java { // allocate the byte array to be returned to java
@ -253,8 +251,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeys)(JNIEnv *env, jobject th
// retrieve key pairs in keysBytesPtr // retrieve key pairs in keysBytesPtr
result = olm_account_generate_one_time_keys(accountPtr, (size_t)aNumberOfKeys, (void*)randomBufferPtr, randomLength); result = olm_account_generate_one_time_keys(accountPtr, (size_t)aNumberOfKeys, (void*)randomBufferPtr, randomLength);
if(result == olm_error()) { if(result == olm_error()) {
const char *errorMsgPtr = olm_account_last_error(accountPtr); LOGE("## generateOneTimeKeys(): failure - error generating one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr));
LOGE("## generateOneTimeKeys(): failure - error generating one time keys Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -302,8 +299,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject t
{ // retrieve key pairs in keysBytesPtr { // retrieve key pairs in keysBytesPtr
keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength); keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength);
if(keysResult == olm_error()) { if(keysResult == olm_error()) {
const char *errorMsgPtr = olm_account_last_error(accountPtr); LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr));
LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",errorMsgPtr);
} }
else else
{ // allocate the byte array to be returned to java { // allocate the byte array to be returned to java
@ -351,8 +347,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysForSessionJni)(JNIEnv *env,
result = olm_remove_one_time_keys(accountPtr, sessionPtr); result = olm_remove_one_time_keys(accountPtr, sessionPtr);
if(result == olm_error()) if(result == olm_error())
{ // the account doesn't have any matching "one time keys".. { // the account doesn't have any matching "one time keys"..
const char *errorMsgPtr = olm_account_last_error(accountPtr); LOGW("## removeOneTimeKeysForSessionJni(): failure - removing one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr));
LOGW("## removeOneTimeKeysForSessionJni(): failure - removing one time keys Msg=%s",errorMsgPtr);
retCode = ERROR_CODE_NO_MATCHING_ONE_TIME_KEYS; retCode = ERROR_CODE_NO_MATCHING_ONE_TIME_KEYS;
} }
@ -386,8 +381,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env,
result = olm_account_mark_keys_as_published(accountPtr); result = olm_account_mark_keys_as_published(accountPtr);
if(result == olm_error()) if(result == olm_error())
{ {
const char *errorMsgPtr = olm_account_last_error(accountPtr); LOGW("## markOneTimeKeysAsPublishedJni(): failure - Msg=%s",(const char *)olm_account_last_error(accountPtr));
LOGW("## markOneTimeKeysAsPublishedJni(): failure - Msg=%s",errorMsgPtr);
retCode = ERROR_CODE_KO; retCode = ERROR_CODE_KO;
} }
else else
@ -448,8 +442,7 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz
signatureLength); signatureLength);
if(resultSign == olm_error()) if(resultSign == olm_error())
{ {
const char *errorMsgPtr = olm_account_last_error(accountPtr); LOGE("## signMessageJni(): failure - error signing message Msg=%s",(const char *)olm_account_last_error(accountPtr));
LOGE("## signMessageJni(): failure - error signing message Msg=%s",errorMsgPtr);
} }
else else
{ {

View file

@ -38,7 +38,7 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env
LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr); LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr);
size_t retCode = olm_clear_inbound_group_session(sessionPtr); size_t retCode = olm_clear_inbound_group_session(sessionPtr);
LOGD(" ## releaseSessionJni(): clear_inbound_group_session=%lu",retCode); LOGI(" ## releaseSessionJni(): clear_inbound_group_session=%lu",retCode);
LOGD(" ## releaseSessionJni(): free IN"); LOGD(" ## releaseSessionJni(): free IN");
free(sessionPtr); free(sessionPtr);
@ -52,26 +52,26 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env
* to make the cast (OlmInboundGroupSession* => jlong) platform independent. * to make the cast (OlmInboundGroupSession* => jlong) platform independent.
* @return the initialized OlmInboundGroupSession* instance if init succeed, NULL otherwise * @return the initialized OlmInboundGroupSession* instance if init succeed, NULL otherwise
**/ **/
JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz) JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz)
{ {
OlmInboundGroupSession* sessionPtr = NULL; OlmInboundGroupSession* sessionPtr = NULL;
size_t sessionSize = 0; size_t sessionSize = 0;
LOGD("## initNewSessionJni(): inbound group session IN"); LOGD("## createNewSessionJni(): inbound group session IN");
sessionSize = olm_inbound_group_session_size(); sessionSize = olm_inbound_group_session_size();
if(0 == sessionSize) if(0 == sessionSize)
{ {
LOGE(" ## initNewSessionJni(): failure - inbound group session size = 0"); LOGE(" ## createNewSessionJni(): failure - inbound group session size = 0");
} }
else if(NULL != (sessionPtr=(OlmInboundGroupSession*)malloc(sessionSize))) else if(NULL != (sessionPtr=(OlmInboundGroupSession*)malloc(sessionSize)))
{ {
sessionPtr = olm_inbound_group_session(sessionPtr); sessionPtr = olm_inbound_group_session(sessionPtr);
LOGD(" ## initNewSessionJni(): success - inbound group session size=%lu",sessionSize); LOGD(" ## createNewSessionJni(): success - inbound group session size=%lu",sessionSize);
} }
else else
{ {
LOGE(" ## initNewSessionJni(): failure - inbound group session OOM"); LOGE(" ## createNewSessionJni(): failure - inbound group session OOM");
} }
return (jlong)(intptr_t)sessionPtr; return (jlong)(intptr_t)sessionPtr;
@ -160,8 +160,7 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEn
size_t result = olm_inbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId); size_t result = olm_inbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
if (result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); LOGE(" ## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr));
LOGE(" ## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -221,8 +220,7 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
encryptedMsgLength); encryptedMsgLength);
if(maxPlainTextLength == olm_error()) if(maxPlainTextLength == olm_error())
{ {
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr));
LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -240,8 +238,7 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
maxPlainTextLength); maxPlainTextLength);
if(plaintextLength == olm_error()) if(plaintextLength == olm_error())
{ {
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr));
LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -275,4 +272,165 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
} }
/**
* Serialize and encrypt session instance into a base64 string.<br>
* @param aKey key used to encrypt the serialized session data
* @param[out] aErrorMsg error message set if operation failed
* @return a base64 string if operation succeed, null otherwise
**/
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jstring aKey, jobject aErrorMsg)
{
jstring pickledDataRetValue = 0;
jclass errorMsgJClass = 0;
jmethodID errorMsgMethodId = 0;
jstring errorJstring = 0;
const char *keyPtr = NULL;
void *pickledPtr = NULL;
OlmInboundGroupSession* sessionPtr = NULL;
LOGD("## inbound group session serializeDataWithKeyJni(): IN");
if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz)))
{
LOGE(" ## serializeDataWithKeyJni(): failure - invalid session ptr");
}
else if(0 == aKey)
{
LOGE(" ## serializeDataWithKeyJni(): failure - invalid key");
}
else if(0 == aErrorMsg)
{
LOGE(" ## serializeDataWithKeyJni(): failure - invalid error object");
}
else if(0 == (errorMsgJClass = env->GetObjectClass(aErrorMsg)))
{
LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error class");
}
else if(0 == (errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;")))
{
LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error method ID");
}
else if(NULL == (keyPtr = env->GetStringUTFChars(aKey, 0)))
{
LOGE(" ## serializeDataWithKeyJni(): failure - keyPtr JNI allocation OOM");
}
else
{
size_t pickledLength = olm_pickle_inbound_group_session_length(sessionPtr);
size_t keyLength = (size_t)env->GetStringUTFLength(aKey);
LOGD(" ## serializeDataWithKeyJni(): pickledLength=%lu keyLength=%lu",pickledLength, keyLength);
LOGD(" ## serializeDataWithKeyJni(): key=%s",(char const *)keyPtr);
if(NULL == (pickledPtr = (void*)malloc((pickledLength+1)*sizeof(uint8_t))))
{
LOGE(" ## serializeDataWithKeyJni(): failure - pickledPtr buffer OOM");
}
else
{
size_t result = olm_pickle_inbound_group_session(sessionPtr,
(void const *)keyPtr,
keyLength,
(void*)pickledPtr,
pickledLength);
if(result == olm_error())
{
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr);
LOGE(" ## serializeDataWithKeyJni(): failure - olm_pickle_outbound_group_session() Msg=%s",errorMsgPtr);
if(0 != (errorJstring = env->NewStringUTF(errorMsgPtr)))
{
env->CallObjectMethod(aErrorMsg, errorMsgMethodId, errorJstring);
}
}
else
{
// build success output
(static_cast<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0');
pickledDataRetValue = env->NewStringUTF((const char*)pickledPtr);
LOGD(" ## serializeDataWithKeyJni(): success - result=%lu pickled=%s", result, static_cast<char*>(pickledPtr));
}
}
}
// free alloc
if(NULL != keyPtr)
{
env->ReleaseStringUTFChars(aKey, keyPtr);
}
if(NULL != pickledPtr)
{
free(pickledPtr);
}
return pickledDataRetValue;
}
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, jobject thiz, jstring aSerializedData, jstring aKey)
{
OlmInboundGroupSession* sessionPtr = NULL;
jstring errorMessageRetValue = 0;
const char *keyPtr = NULL;
const char *pickledPtr = NULL;
LOGD("## initWithSerializedDataJni(): IN");
if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz)))
{
LOGE(" ## initWithSerializedDataJni(): failure - session failure OOM");
}
else if(0 == aKey)
{
LOGE(" ## initWithSerializedDataJni(): failure - invalid key");
}
else if(0 == aSerializedData)
{
LOGE(" ## initWithSerializedDataJni(): failure - serialized data");
}
else if(NULL == (keyPtr = env->GetStringUTFChars(aKey, 0)))
{
LOGE(" ## initWithSerializedDataJni(): failure - keyPtr JNI allocation OOM");
}
else if(NULL == (pickledPtr = env->GetStringUTFChars(aSerializedData, 0)))
{
LOGE(" ## initWithSerializedDataJni(): failure - pickledPtr JNI allocation OOM");
}
else
{
size_t pickledLength = (size_t)env->GetStringUTFLength(aSerializedData);
size_t keyLength = (size_t)env->GetStringUTFLength(aKey);
LOGD(" ## initWithSerializedDataJni(): pickledLength=%lu keyLength=%lu",pickledLength, keyLength);
LOGD(" ## initWithSerializedDataJni(): key=%s",(char const *)keyPtr);
LOGD(" ## initWithSerializedDataJni(): pickled=%s",(char const *)pickledPtr);
size_t result = olm_unpickle_inbound_group_session(sessionPtr,
(void const *)keyPtr,
keyLength,
(void*)pickledPtr,
pickledLength);
if(result == olm_error())
{
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr);
LOGE(" ## initWithSerializedDataJni(): failure - olm_unpickle_inbound_group_session() Msg=%s",errorMsgPtr);
errorMessageRetValue = env->NewStringUTF(errorMsgPtr);
}
else
{
LOGD(" ## initWithSerializedDataJni(): success - result=%lu ", result);
}
}
// free alloc
if(NULL != keyPtr)
{
env->ReleaseStringUTFChars(aKey, keyPtr);
}
if(NULL != pickledPtr)
{
env->ReleaseStringUTFChars(aSerializedData, pickledPtr);
}
return errorMessageRetValue;
}

View file

@ -29,12 +29,16 @@ extern "C" {
// session creation/destruction // session creation/destruction
JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz); JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz); JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSessionKeyJni)(JNIEnv *env, jobject thiz, jstring aSessionKey); JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSessionKeyJni)(JNIEnv *env, jobject thiz, jstring aSessionKey);
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz); JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg); JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg);
// serialization
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jstring aKey, jobject aErrorMsg);
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, jobject thiz, jstring aSerializedData, jstring aKey);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -30,16 +30,15 @@
/* logging macros */ /* logging macros */
#define ENABLE_LOGS #define ENABLE_LOGS
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
#ifdef ENABLE_LOGS #ifdef ENABLE_LOGS
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__) #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__) #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
#else #else
#define LOGV(...)
#define LOGD(...) #define LOGD(...)
#define LOGW(...) #define LOGW(...)
#define LOGE(...)
#endif #endif
#define FUNC_DEF(class_name,func_name) JNICALL Java_org_matrix_olm_##class_name##_##func_name #define FUNC_DEF(class_name,func_name) JNICALL Java_org_matrix_olm_##class_name##_##func_name

View file

@ -63,12 +63,13 @@ bool setRandomInBuffer(uint8_t **aBuffer2Ptr, size_t aRandomSize)
/** /**
* Read the account instance ID of the calling object. * Read the instance ID of the calling object.
* @param aJniEnv pointer pointing on the JNI function table * @param aJniEnv pointer pointing on the JNI function table
* @param aJavaObject reference to the object on which the method is invoked * @param aJavaObject reference to the object on which the method is invoked
* @param aCallingClass java calling clas name
* @return the instance ID if operation succeed, -1 if instance ID was not found. * @return the instance ID if operation succeed, -1 if instance ID was not found.
**/ **/
jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingClass)
{ {
jlong instanceId = 0; jlong instanceId = 0;
jfieldID instanceIdField; jfieldID instanceIdField;
@ -77,7 +78,7 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
if(NULL!=aJniEnv) if(NULL!=aJniEnv)
{ {
requiredClass = aJniEnv->FindClass(CLASS_OLM_ACCOUNT); requiredClass = aJniEnv->FindClass(aCallingClass);
if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass)))
{ {
@ -85,30 +86,44 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
} }
else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject)))
{ {
if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmAccountId", "J"))) if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeId", "J")))
{ {
instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField); instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField);
aJniEnv->DeleteLocalRef(loaderClass); aJniEnv->DeleteLocalRef(loaderClass);
LOGD("## getAccountInstanceId(): read from java instanceId=%lld",instanceId); LOGD("## getInstanceId(): read from java instanceId=%lld",instanceId);
} }
else else
{ {
LOGE("## getAccountInstanceId() ERROR! GetFieldID=null"); LOGE("## getInstanceId() ERROR! GetFieldID=null");
} }
} }
else else
{ {
LOGE("## getAccountInstanceId() ERROR! GetObjectClass=null"); LOGE("## getInstanceId() ERROR! GetObjectClass=null");
} }
} }
else else
{ {
LOGE("## getAccountInstanceId() ERROR! aJniEnv=NULL"); LOGE("## getInstanceId() ERROR! aJniEnv=NULL");
} }
LOGD("## getAccountInstanceId() success - instanceId=%p (jlong)(intptr_t)instanceId=%lld",(void*)instanceId, (jlong)(intptr_t)instanceId); LOGD("## getInstanceId() success - instanceId=%p (jlong)(intptr_t)instanceId=%lld",(void*)instanceId, (jlong)(intptr_t)instanceId);
return instanceId; return instanceId;
} }
/**
* Read the account instance ID of the calling object.
* @param aJniEnv pointer pointing on the JNI function table
* @param aJavaObject reference to the object on which the method is invoked
* @return the instance ID if operation succeed, -1 if instance ID was not found.
**/
jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{
jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_ACCOUNT);
return instanceId;
}
/** /**
* Read the session instance ID of the calling object (aJavaObject).<br> * Read the session instance ID of the calling object (aJavaObject).<br>
* @param aJniEnv pointer pointing on the JNI function table * @param aJniEnv pointer pointing on the JNI function table
@ -117,46 +132,10 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
**/ **/
jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId = 0; jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_SESSION);
jfieldID instanceIdField;
jclass loaderClass;
jclass requiredClass = 0;
if(NULL!=aJniEnv)
{
requiredClass = aJniEnv->FindClass(CLASS_OLM_SESSION);
if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass)))
{
LOGE("## getSessionInstanceId() failure - invalid instance of");
}
else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject)))
{
if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmSessionId", "J")))
{
instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField);
aJniEnv->DeleteLocalRef(loaderClass);
}
else
{
LOGE("## getSessionInstanceId() ERROR! GetFieldID=null");
}
}
else
{
LOGE("## getSessionInstanceId() ERROR! GetObjectClass=null");
}
}
else
{
LOGE("## getSessionInstanceId() ERROR! aJniEnv=NULL");
}
//LOGD("## getSessionInstanceId() success - instanceId=%lld",instanceId);
return instanceId; return instanceId;
} }
/** /**
* Read the inbound group session instance ID of the calling object (aJavaObject).<br> * Read the inbound group session instance ID of the calling object (aJavaObject).<br>
* @param aJniEnv pointer pointing on the JNI function table * @param aJniEnv pointer pointing on the JNI function table
@ -165,41 +144,7 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
**/ **/
jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId = 0; jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_INBOUND_GROUP_SESSION);
jfieldID instanceIdField;
jclass loaderClass;
jclass requiredClass = 0;
if(NULL!=aJniEnv)
{
requiredClass = aJniEnv->FindClass(CLASS_OLM_INBOUND_GROUP_SESSION);
if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass)))
{
LOGE("## getInboundGroupSessionInstanceId() failure - invalid instance of");
}
else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject)))
{
if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmInboundGroupSessionId", "J")))
{
instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField);
aJniEnv->DeleteLocalRef(loaderClass);
}
else
{
LOGE("## getInboundGroupSessionInstanceId() ERROR! GetFieldID=null");
}
}
else
{
LOGE("## getInboundGroupSessionInstanceId() ERROR! GetObjectClass=null");
}
}
else
{
LOGE("## getInboundGroupSessionInstanceId() ERROR! aJniEnv=NULL");
}
return instanceId; return instanceId;
} }
@ -212,84 +157,23 @@ jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
**/ **/
jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId = 0; jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_OUTBOUND_GROUP_SESSION);
jfieldID instanceIdField;
jclass loaderClass;
jclass requiredClass = 0;
if(NULL!=aJniEnv)
{
requiredClass = aJniEnv->FindClass(CLASS_OLM_OUTBOUND_GROUP_SESSION);
if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass)))
{
LOGE("## getOutboundGroupSessionInstanceId() failure - invalid instance of");
}
else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject)))
{
if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmOutboundGroupSessionId", "J")))
{
instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField);
aJniEnv->DeleteLocalRef(loaderClass);
}
else
{
LOGE("## getOutboundGroupSessionInstanceId() ERROR! GetFieldID=null");
}
}
else
{
LOGE("## getOutboundGroupSessionInstanceId() ERROR! GetObjectClass=null");
}
}
else
{
LOGE("## getOutboundGroupSessionInstanceId() ERROR! aJniEnv=NULL");
}
return instanceId; return instanceId;
} }
/**
* Read the utility instance ID of the calling object (aJavaObject).<br>
* @param aJniEnv pointer pointing on the JNI function table
* @param aJavaObject reference to the object on which the method is invoked
* @return the instance ID if read succeed, -1 otherwise.
**/
jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId = 0; jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_UTILITY);
jfieldID instanceIdField;
jclass loaderClass;
jclass requiredClass = 0;
if(NULL!=aJniEnv)
{
requiredClass = aJniEnv->FindClass(CLASS_OLM_UTILITY);
if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass)))
{
LOGE("## getUtilityInstanceId() failure - invalid instance of");
}
else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject)))
{
if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmUtilityId", "J")))
{
instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField);
aJniEnv->DeleteLocalRef(loaderClass);
}
else
{
LOGE("## getUtilityInstanceId() ERROR! GetFieldID=null");
}
}
else
{
LOGE("## getUtilityInstanceId() ERROR! GetObjectClass=null");
}
}
else
{
LOGE("## getUtilityInstanceId() ERROR! aJniEnv=NULL");
}
return instanceId; return instanceId;
} }
template <typename T> template <typename T>
jstring serializeDataWithKey(JNIEnv *env, jobject thiz, jstring serializeDataWithKey(JNIEnv *env, jobject thiz,
jstring aKey, jstring aKey,

View file

@ -38,7 +38,7 @@ JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *en
LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr); LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr);
size_t retCode = olm_clear_outbound_group_session(sessionPtr); size_t retCode = olm_clear_outbound_group_session(sessionPtr);
LOGD(" ## releaseSessionJni(): clear_outbound_group_session=%lu",retCode); LOGI(" ## releaseSessionJni(): clear_outbound_group_session=%lu",retCode);
LOGD(" ## releaseSessionJni(): free IN"); LOGD(" ## releaseSessionJni(): free IN");
free(sessionPtr); free(sessionPtr);
@ -52,26 +52,26 @@ JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *en
* to make the cast (OlmOutboundGroupSession* => jlong) platform independent. * to make the cast (OlmOutboundGroupSession* => jlong) platform independent.
* @return the initialized OlmOutboundGroupSession* instance if init succeed, NULL otherwise * @return the initialized OlmOutboundGroupSession* instance if init succeed, NULL otherwise
**/ **/
JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz) JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz)
{ {
OlmOutboundGroupSession* sessionPtr = NULL; OlmOutboundGroupSession* sessionPtr = NULL;
size_t sessionSize = 0; size_t sessionSize = 0;
LOGD("## initNewSessionJni(): outbound group session IN"); LOGD("## createNewSessionJni(): outbound group session IN");
sessionSize = olm_outbound_group_session_size(); sessionSize = olm_outbound_group_session_size();
if(0 == sessionSize) if(0 == sessionSize)
{ {
LOGE(" ## initNewSessionJni(): failure - outbound group session size = 0"); LOGE(" ## createNewSessionJni(): failure - outbound group session size = 0");
} }
else if(NULL != (sessionPtr=(OlmOutboundGroupSession*)malloc(sessionSize))) else if(NULL != (sessionPtr=(OlmOutboundGroupSession*)malloc(sessionSize)))
{ {
sessionPtr = olm_outbound_group_session(sessionPtr); sessionPtr = olm_outbound_group_session(sessionPtr);
LOGD(" ## initNewSessionJni(): success - outbound group session size=%lu",sessionSize); LOGD(" ## createNewSessionJni(): success - outbound group session size=%lu",sessionSize);
} }
else else
{ {
LOGE(" ## initNewSessionJni(): failure - outbound group session OOM"); LOGE(" ## createNewSessionJni(): failure - outbound group session OOM");
} }
return (jlong)(intptr_t)sessionPtr; return (jlong)(intptr_t)sessionPtr;
@ -111,8 +111,7 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(
size_t sessionResult = olm_init_outbound_group_session(sessionPtr, randomBuffPtr, randomLength); size_t sessionResult = olm_init_outbound_group_session(sessionPtr, randomBuffPtr, randomLength);
if(sessionResult == olm_error()) { if(sessionResult == olm_error()) {
const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); LOGE(" ## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr));
LOGE(" ## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -160,8 +159,7 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIE
size_t result = olm_outbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId); size_t result = olm_outbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
if (result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); LOGE(" ## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr));
LOGE(" ## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -237,8 +235,7 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env
size_t result = olm_outbound_group_session_key(sessionPtr, sessionKeyPtr, sessionKeyLength); size_t result = olm_outbound_group_session_key(sessionPtr, sessionKeyPtr, sessionKeyLength);
if (result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr));
LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -301,8 +298,7 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv
encryptedMsgLength); encryptedMsgLength);
if(encryptedLength == olm_error()) if(encryptedLength == olm_error())
{ {
const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); LOGE(" ## encryptMessageJni(): failure - olm_group_encrypt Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr));
LOGE(" ## encryptMessageJni(): failure - olm_group_encrypt Msg=%s",errorMsgPtr);
} }
else else
{ {

View file

@ -29,7 +29,7 @@ extern "C" {
// session creation/destruction // session creation/destruction
JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz); JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz); JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(JNIEnv *env, jobject thiz); JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz); JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz);

View file

@ -161,8 +161,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject
(void*)randomBuffPtr, (void*)randomBuffPtr,
randomSize); randomSize);
if(sessionResult == olm_error()) { if(sessionResult == olm_error()) {
const char *errorMsgPtr = olm_session_last_error(sessionPtr); LOGE("## initOutboundSessionJni(): failure - session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr));
LOGE("## initOutboundSessionJni(): failure - session creation Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -236,8 +235,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject
sessionResult = olm_create_inbound_session(sessionPtr, accountPtr, (void*)messagePtr , messageLength); sessionResult = olm_create_inbound_session(sessionPtr, accountPtr, (void*)messagePtr , messageLength);
if(sessionResult == olm_error()) { if(sessionResult == olm_error()) {
const char *errorMsgPtr = olm_session_last_error(sessionPtr); LOGE("## initInboundSessionJni(): failure - init inbound session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr));
LOGE("## initInboundSessionJni(): failure - init inbound session creation Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -302,8 +300,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env,
sessionResult = olm_create_inbound_session_from(sessionPtr, accountPtr, theirIdentityKeyPtr, theirIdentityKeyLength, (void*)messagePtr , messageLength); sessionResult = olm_create_inbound_session_from(sessionPtr, accountPtr, theirIdentityKeyPtr, theirIdentityKeyLength, (void*)messagePtr , messageLength);
if(sessionResult == olm_error()) { if(sessionResult == olm_error()) {
const char *errorMsgPtr = olm_session_last_error(sessionPtr); LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr));
LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -355,8 +352,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobje
size_t matchResult = olm_matches_inbound_session(sessionPtr, (void*)messagePtr , messageLength); size_t matchResult = olm_matches_inbound_session(sessionPtr, (void*)messagePtr , messageLength);
if(matchResult == olm_error()) { if(matchResult == olm_error()) {
const char *errorMsgPtr = olm_session_last_error(sessionPtr); LOGE("## matchesInboundSessionJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr));
LOGE("## matchesInboundSessionJni(): failure - no match Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -416,8 +412,7 @@ JNIEXPORT jint JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(J
size_t matchResult = olm_matches_inbound_session_from(sessionPtr, (void const *)theirIdentityKeyPtr, identityKeyLength, (void*)messagePtr , messageLength); size_t matchResult = olm_matches_inbound_session_from(sessionPtr, (void const *)theirIdentityKeyPtr, identityKeyLength, (void*)messagePtr , messageLength);
if(matchResult == olm_error()) { if(matchResult == olm_error()) {
const char *errorMsgPtr = olm_session_last_error(sessionPtr); LOGE("## matchesInboundSessionFromIdKeyJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr));
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - no match Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -529,8 +524,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
encryptedMsgLength); encryptedMsgLength);
if(result == olm_error()) if(result == olm_error())
{ {
const char *errorMsgPtr = olm_session_last_error(sessionPtr); LOGE("## encryptMessageJni(): failure - Msg=%s",(const char *)olm_session_last_error(sessionPtr));
LOGE("## encryptMessageJni(): failure - Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -542,11 +536,10 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
// update message: encryptedMsgPtr => encryptedJstring // update message: encryptedMsgPtr => encryptedJstring
jstring encryptedJstring = env->NewStringUTF((const char*)encryptedMsgPtr); jstring encryptedJstring = env->NewStringUTF((const char*)encryptedMsgPtr);
size_t encryptedUtfMsgLength = (size_t)env->GetStringUTFLength(encryptedJstring);
env->SetObjectField(aEncryptedMsg, encryptedMsgFieldId, (jobject)encryptedJstring); env->SetObjectField(aEncryptedMsg, encryptedMsgFieldId, (jobject)encryptedJstring);
retCode = ERROR_CODE_OK; retCode = ERROR_CODE_OK;
LOGD("## encryptMessageJni(): success - result=%lu Type=%lu utfLength=%lu encryptedMsg=%s", result, messageType, encryptedUtfMsgLength, (const char*)encryptedMsgPtr); LOGD("## encryptMessageJni(): success - result=%lu Type=%lu utfLength=%lu encryptedMsg=%s", result, messageType, (size_t)env->GetStringUTFLength(encryptedJstring), (const char*)encryptedMsgPtr);
} }
} }
} }
@ -642,8 +635,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t
if(maxPlainTextLength == olm_error()) if(maxPlainTextLength == olm_error())
{ {
const char *errorMsgPtr = olm_session_last_error(sessionPtr); LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s",(const char *)olm_session_last_error(sessionPtr));
LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -662,8 +654,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t
maxPlainTextLength); maxPlainTextLength);
if(plaintextLength == olm_error()) if(plaintextLength == olm_error())
{ {
const char *errorMsgPtr = olm_session_last_error(sessionPtr); LOGE("## decryptMessageJni(): failure - olm_decrypt Msg=%s",(const char *)olm_session_last_error(sessionPtr));
LOGE("## decryptMessageJni(): failure - olm_decrypt Msg=%s",errorMsgPtr);
} }
else else
{ {
@ -728,8 +719,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, job
if (result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_session_last_error(sessionPtr); LOGE("## getSessionIdentifierJni(): failure - get session identifier failure Msg=%s",(const char *)olm_session_last_error(sessionPtr));
LOGE("## getSessionIdentifierJni(): failure - get session identifier failure Msg=%s",errorMsgPtr);
} }
else else
{ {

View file

@ -203,8 +203,7 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jst
hashLength); hashLength);
if(result == olm_error()) if(result == olm_error())
{ {
const char *errorMsgPtr = olm_utility_last_error(utilityPtr); LOGE("## sha256Jni(): failure - hash creation Msg=%s",(const char *)olm_utility_last_error(utilityPtr));
LOGE("## sha256Jni(): failure - hash creation Msg=%s",errorMsgPtr);
} }
else else
{ {