diff options
author | pedroGitt <pedro.contreiras@amdocs.com> | 2016-10-23 23:55:45 +0200 |
---|---|---|
committer | pedroGitt <pedro.contreiras@amdocs.com> | 2016-10-23 23:55:45 +0200 |
commit | 332d9d0c09c1f1f60bf970ea2b9a30cc95b9f5f5 (patch) | |
tree | fb9b26723a7c4b48b0c8ae37e7b9e6bce1094af8 /java/android/OlmLibSdk/olm-sdk/src/main | |
parent | fae857582c445ce89f527f6331aeac4eb66bc692 (diff) |
Add serialization for inbound group session
- remove compiler warnings when logs are not enabled
- new getInstanceId() function to refactor code
Diffstat (limited to 'java/android/OlmLibSdk/olm-sdk/src/main')
15 files changed, 409 insertions, 287 deletions
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java index 8d19a0c..2381585 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java @@ -26,7 +26,6 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; -import java.util.Random; public class OlmAccount implements Serializable { private static final long serialVersionUID = 3497486121598434824L; @@ -52,7 +51,7 @@ public class OlmAccount implements Serializable { /** account raw pointer value (OlmAccount*) returned by JNI. * this value identifies uniquely the native account instance. */ - private transient long mNativeOlmAccountId; + private transient long mNativeId; public OlmAccount() throws OlmException { @@ -179,7 +178,7 @@ public class OlmAccount implements Serializable { * @return native account ID */ public long getOlmAccountId(){ - return mNativeOlmAccountId; + return mNativeId; } /** @@ -189,7 +188,7 @@ public class OlmAccount implements Serializable { public void releaseAccount(){ releaseAccountJni(); - mNativeOlmAccountId = 0; + mNativeId = 0; } /** @@ -208,7 +207,7 @@ public class OlmAccount implements Serializable { */ private boolean initNewAccount() { boolean retCode = false; - if(0 != (mNativeOlmAccountId = initNewAccountJni())){ + if(0 != (mNativeId = initNewAccountJni())){ retCode = true; } return retCode; @@ -217,7 +216,7 @@ public class OlmAccount implements Serializable { /** * Create and initialize an OLM account in native side.<br> * 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(); @@ -230,7 +229,7 @@ public class OlmAccount implements Serializable { */ private boolean createNewAccount() { boolean retCode = false; - if(0 != (mNativeOlmAccountId = createNewAccountJni())){ + if(0 != (mNativeId = createNewAccountJni())){ retCode = true; } return retCode; @@ -239,7 +238,7 @@ public class OlmAccount implements Serializable { /** * Create an OLM account in native side.<br> * 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(); diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java index d74f6e3..fe43f94 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java @@ -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_OUTBOUND_GROUP_SESSION_SERIALIZATION = 10; 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 public static final String EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION = "failed to create a new outbound group Session"; diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java index e78e259..8d33b2c 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java @@ -23,6 +23,9 @@ package org.matrix.olm; import android.text.TextUtils; import android.util.Log; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.io.Serializable; public class OlmInboundGroupSession implements Serializable { @@ -32,26 +35,18 @@ public class OlmInboundGroupSession implements Serializable { /** session raw pointer value returned by JNI.<br> * this value uniquely identifies the native inbound group session instance. */ - private long mNativeOlmInboundGroupSessionId; - - /** - * Getter on the native inbound group session ID. - * @return native inbound group session ID - */ - public long getOlmInboundGroupSessionId() { - return mNativeOlmInboundGroupSessionId; - } + private transient long mNativeId; /** * Constructor.<br> * 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 - * See {@link #initNewSession()} and {@link #initInboundGroupSessionWithSessionKey(String)} + * See {@link #createNewSession()} and {@link #initInboundGroupSessionWithSessionKey(String)} * @param aSessionKey session key * @throws OlmException constructor failure */ public OlmInboundGroupSession(String aSessionKey) throws OlmException { - if(initNewSession()) { + if(createNewSession()) { if( 0 != initInboundGroupSessionWithSessionKey(aSessionKey)) { releaseSession();// prevent memory leak before throwing 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(){ releaseSessionJni(); - mNativeOlmInboundGroupSessionId = 0; + mNativeId = 0; } /** * Destroy the corresponding OLM inbound group session native object.<br> * This method must ALWAYS be called when this JAVA instance * is destroyed (ie. garbage collected) to prevent memory leak in native side. - * See {@link #initNewSessionJni()}. + * See {@link #createNewSessionJni()}. */ private native void releaseSessionJni(); @@ -84,9 +79,9 @@ public class OlmInboundGroupSession implements Serializable { * To be called before any other API call. * @return true if init succeed, false otherwise. */ - private boolean initNewSession() { + private boolean createNewSession() { boolean retCode = false; - if(0 != (mNativeOlmInboundGroupSessionId = initNewSessionJni())){ + if(0 != (mNativeId = createNewSessionJni())){ retCode = true; } return retCode; @@ -95,13 +90,13 @@ public class OlmInboundGroupSession implements Serializable { /** * Create the corresponding OLM inbound group session in native side.<br> * 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> - * 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()} * @param aSessionKey session key * @return 0 if operation succeed, -1 otherwise @@ -132,8 +127,128 @@ public class OlmInboundGroupSession implements Serializable { private native String decryptMessageJni(String aEncryptedMsg); - // TODO missing API: initWithSerializedData - // TODO missing API: serializeDataWithKey - // TODO missing API: initWithCoder - // TODO missing API: encodeWithCoder + /** + * Kick off the serialization mechanism. + * @param aOutStream output stream for serializing + * @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); } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java index 7fdf405..94acbd0 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java @@ -32,25 +32,17 @@ public class OlmOutboundGroupSession implements Serializable { /** session raw pointer value returned by JNI.<br> * this value uniquely identifies the native inbound group session instance. */ - private long mNativeOlmOutboundGroupSessionId; - - /** - * Getter on the native outbound group session ID. - * @return native outbound group session ID - */ - public long getOlmInboundGroupSessionId(){ - return mNativeOlmOutboundGroupSessionId; - } + private transient long mNativeId; /** * Constructor.<br> * Create and save a new session native instance ID and * initialise a new outbound group session.<br> - * See {@link #initNewSession()} and {@link #initOutboundGroupSession()} + * See {@link #createNewSession()} and {@link #initOutboundGroupSession()} * @throws OlmException constructor failure */ public OlmOutboundGroupSession() throws OlmException { - if(initNewSession()) { + if(createNewSession()) { if( 0 != initOutboundGroupSession()) { releaseSession();// prevent memory leak before throwing 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)) { 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); } else if(!initWithSerializedData(pickledData, key, errorMsg)) { @@ -180,26 +172,26 @@ public class OlmOutboundGroupSession implements Serializable { */ public void releaseSession() { releaseSessionJni(); - mNativeOlmOutboundGroupSessionId = 0; + mNativeId = 0; } /** * Destroy the corresponding OLM outbound group session native object.<br> * This method must ALWAYS be called when this JAVA instance * is destroyed (ie. garbage collected) to prevent memory leak in native side. - * See {@link #initNewSessionJni()}. + * See {@link #createNewSessionJni()}. */ private native void releaseSessionJni(); /** * 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. * @return true if init succeed, false otherwise. */ - private boolean initNewSession() { + private boolean createNewSession() { boolean retCode = false; - if(0 != (mNativeOlmOutboundGroupSessionId = initNewSessionJni())){ + if(0 != (mNativeId = createNewSessionJni())){ retCode = true; } return retCode; @@ -208,9 +200,9 @@ public class OlmOutboundGroupSession implements Serializable { /** * Create the corresponding OLM outbound group session in native side.<br> * 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> diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java index eaf6110..5877d24 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java @@ -31,7 +31,7 @@ public class OlmSession implements Serializable { /** session raw pointer value (OlmSession*) returned by JNI. * this value uniquely identifies the native session instance. **/ - private transient long mNativeOlmSessionId; + private transient long mNativeId; public OlmSession() throws OlmException { @@ -158,7 +158,7 @@ public class OlmSession implements Serializable { * @return native session ID */ public long getOlmSessionId(){ - return mNativeOlmSessionId; + return mNativeId; } /** @@ -176,7 +176,7 @@ public class OlmSession implements Serializable { public void releaseSession(){ releaseSessionJni(); - mNativeOlmSessionId = 0; + mNativeId = 0; } /** @@ -187,7 +187,7 @@ public class OlmSession implements Serializable { */ private boolean initNewSession() { boolean retCode = false; - if(0 != (mNativeOlmSessionId = initNewSessionJni())){ + if(0 != (mNativeId = initNewSessionJni())){ retCode = true; } return retCode; @@ -196,7 +196,7 @@ public class OlmSession implements Serializable { /** * Create the corresponding OLM session in native side.<br> * 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(); @@ -210,7 +210,7 @@ public class OlmSession implements Serializable { */ private boolean createNewSession() { boolean retCode = false; - if(0 != (mNativeOlmSessionId = createNewSessionJni())){ + if(0 != (mNativeId = createNewSessionJni())){ retCode = true; } return retCode; @@ -219,7 +219,7 @@ public class OlmSession implements Serializable { /** * Create an OLM account in native side.<br> * 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(); diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java index 342d8eb..3bf65a8 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java @@ -19,7 +19,6 @@ package org.matrix.olm; import android.text.TextUtils; import android.util.Log; -import java.io.Serializable; import java.util.Random; public class OlmUtility { @@ -31,28 +30,20 @@ public class OlmUtility { /** raw pointer value returned by JNI. * this value uniquely identifies this utility instance. **/ - private long mNativeOlmUtilityId; + private long mNativeId; public OlmUtility() { initUtility(); } /** - * Getter on the session ID. - * @return native session ID - */ - public long getOlmUtilityId(){ - return mNativeOlmUtilityId; - } - - /** * Create a native utility instance. * To be called before any other API call. * @return true if init succeed, false otherwise. */ private boolean initUtility() { boolean retCode = false; - if(0 != (mNativeOlmUtilityId = initUtilityJni())){ + if(0 != (mNativeId = initUtilityJni())){ retCode = true; } return retCode; @@ -65,7 +56,7 @@ public class OlmUtility { */ public void releaseUtility(){ releaseUtilityJni(); - mNativeOlmUtilityId = 0; + mNativeId = 0; } private native void releaseUtilityJni(); diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp index 1a1b360..baa7939 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp @@ -119,8 +119,7 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi // create account accountRetCode = olm_create_account(accountPtr, (void*)randomBuffPtr, randomSize); if(accountRetCode == olm_error()) { - const char *errorMsgPtr = olm_account_last_error(accountPtr); - LOGE("## initNewAccount(): failure - account creation failed Msg=%s", errorMsgPtr); + LOGE("## initNewAccount(): failure - account creation failed Msg=%s", (const char *)olm_account_last_error(accountPtr)); } 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 keysResult = olm_account_identity_keys(accountPtr, identityKeysBytesPtr, identityKeysLength); if(keysResult == olm_error()) { - const char *errorMsgPtr = olm_account_last_error(accountPtr); - LOGE("## identityKeys(): failure - error getting identity keys Msg=%s",errorMsgPtr); + LOGE("## identityKeys(): failure - error getting identity keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); } else { // 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 result = olm_account_generate_one_time_keys(accountPtr, (size_t)aNumberOfKeys, (void*)randomBufferPtr, randomLength); if(result == olm_error()) { - const char *errorMsgPtr = olm_account_last_error(accountPtr); - LOGE("## generateOneTimeKeys(): failure - error generating one time keys Msg=%s",errorMsgPtr); + LOGE("## generateOneTimeKeys(): failure - error generating one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); } else { @@ -302,8 +299,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject t { // retrieve key pairs in keysBytesPtr keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength); if(keysResult == olm_error()) { - const char *errorMsgPtr = olm_account_last_error(accountPtr); - LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",errorMsgPtr); + LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); } else { // 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); if(result == olm_error()) { // 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",errorMsgPtr); + LOGW("## removeOneTimeKeysForSessionJni(): failure - removing one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); 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); if(result == olm_error()) { - const char *errorMsgPtr = olm_account_last_error(accountPtr); - LOGW("## markOneTimeKeysAsPublishedJni(): failure - Msg=%s",errorMsgPtr); + LOGW("## markOneTimeKeysAsPublishedJni(): failure - Msg=%s",(const char *)olm_account_last_error(accountPtr)); retCode = ERROR_CODE_KO; } else @@ -448,8 +442,7 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz signatureLength); if(resultSign == olm_error()) { - const char *errorMsgPtr = olm_account_last_error(accountPtr); - LOGE("## signMessageJni(): failure - error signing message Msg=%s",errorMsgPtr); + LOGE("## signMessageJni(): failure - error signing message Msg=%s",(const char *)olm_account_last_error(accountPtr)); } else { diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp index d10a05b..810d62c 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp @@ -38,7 +38,7 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env LOGD(" ## releaseSessionJni(): sessionPtr=%p",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"); 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. * @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; size_t sessionSize = 0; - LOGD("## initNewSessionJni(): inbound group session IN"); + LOGD("## createNewSessionJni(): inbound group session IN"); sessionSize = olm_inbound_group_session_size(); 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))) { 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 { - LOGE(" ## initNewSessionJni(): failure - inbound group session OOM"); + LOGE(" ## createNewSessionJni(): failure - inbound group session OOM"); } 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); 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",errorMsgPtr); + LOGE(" ## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr)); } else { @@ -221,8 +220,7 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv * encryptedMsgLength); 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",errorMsgPtr); + LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr)); } else { @@ -240,8 +238,7 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv * maxPlainTextLength); if(plaintextLength == olm_error()) { - const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); - LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt Msg=%s",errorMsgPtr); + LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr)); } 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; +} diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.h index 1eb8238..897ef46 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.h @@ -29,12 +29,16 @@ extern "C" { // session creation/destruction 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 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); +// 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 } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h index 2e31bff..bb718b6 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h @@ -30,16 +30,15 @@ /* logging macros */ #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 - #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, 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 LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) #else - #define LOGV(...) #define LOGD(...) #define LOGW(...) - #define LOGE(...) #endif #define FUNC_DEF(class_name,func_name) JNICALL Java_org_matrix_olm_##class_name##_##func_name diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp index 174739c..1f0c0df 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp @@ -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 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. **/ -jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) +jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingClass) { jlong instanceId = 0; jfieldID instanceIdField; @@ -77,7 +78,7 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) if(NULL!=aJniEnv) { - requiredClass = aJniEnv->FindClass(CLASS_OLM_ACCOUNT); + requiredClass = aJniEnv->FindClass(aCallingClass); if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) { @@ -85,78 +86,56 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject 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); aJniEnv->DeleteLocalRef(loaderClass); - LOGD("## getAccountInstanceId(): read from java instanceId=%lld",instanceId); + LOGD("## getInstanceId(): read from java instanceId=%lld",instanceId); } else { - LOGE("## getAccountInstanceId() ERROR! GetFieldID=null"); + LOGE("## getInstanceId() ERROR! GetFieldID=null"); } } else { - LOGE("## getAccountInstanceId() ERROR! GetObjectClass=null"); + LOGE("## getInstanceId() ERROR! GetObjectClass=null"); } } 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; } /** -* Read the session instance ID of the calling object (aJavaObject).<br> +* 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 read succeed, -1 otherwise. +* @return the instance ID if operation succeed, -1 if instance ID was not found. **/ -jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) +jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) { - jlong instanceId = 0; - jfieldID instanceIdField; - jclass loaderClass; - jclass requiredClass = 0; + jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_ACCOUNT); + return instanceId; +} - 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); +/** +* Read the session 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 getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) +{ + jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_SESSION); return instanceId; } - /** * Read the inbound group session instance ID of the calling object (aJavaObject).<br> * @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 instanceId = 0; - 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"); - } - + jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_INBOUND_GROUP_SESSION); return instanceId; } @@ -212,84 +157,23 @@ jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) **/ jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) { - jlong instanceId = 0; - 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"); - } - + jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_OUTBOUND_GROUP_SESSION); 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 instanceId = 0; - 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"); - } - + jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_UTILITY); return instanceId; } + template <typename T> jstring serializeDataWithKey(JNIEnv *env, jobject thiz, jstring aKey, diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp index 67bf8bd..8ccd78e 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp @@ -38,7 +38,7 @@ JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *en LOGD(" ## releaseSessionJni(): sessionPtr=%p",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"); 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. * @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; size_t sessionSize = 0; - LOGD("## initNewSessionJni(): outbound group session IN"); + LOGD("## createNewSessionJni(): outbound group session IN"); sessionSize = olm_outbound_group_session_size(); 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))) { 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 { - LOGE(" ## initNewSessionJni(): failure - outbound group session OOM"); + LOGE(" ## createNewSessionJni(): failure - outbound group session OOM"); } 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); if(sessionResult == olm_error()) { - const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); - LOGE(" ## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s",errorMsgPtr); + LOGE(" ## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr)); } 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); if (result == olm_error()) { - const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); - LOGE(" ## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s",errorMsgPtr); + LOGE(" ## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr)); } 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); if (result == olm_error()) { - const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); - LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s",errorMsgPtr); + LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr)); } else { @@ -301,8 +298,7 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv encryptedMsgLength); if(encryptedLength == olm_error()) { - const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); - LOGE(" ## encryptMessageJni(): failure - olm_group_encrypt Msg=%s",errorMsgPtr); + LOGE(" ## encryptMessageJni(): failure - olm_group_encrypt Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr)); } else { diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h index 22c32a7..4b59b93 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h @@ -29,7 +29,7 @@ extern "C" { // session creation/destruction 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 jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz); diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp index 9f64d2e..cfa132c 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp @@ -161,8 +161,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject (void*)randomBuffPtr, randomSize); if(sessionResult == olm_error()) { - const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## initOutboundSessionJni(): failure - session creation Msg=%s",errorMsgPtr); + LOGE("## initOutboundSessionJni(): failure - session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } 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); if(sessionResult == olm_error()) { - const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## initInboundSessionJni(): failure - init inbound session creation Msg=%s",errorMsgPtr); + LOGE("## initInboundSessionJni(): failure - init inbound session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } 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); if(sessionResult == olm_error()) { - const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",errorMsgPtr); + LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } 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); if(matchResult == olm_error()) { - const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## matchesInboundSessionJni(): failure - no match Msg=%s",errorMsgPtr); + LOGE("## matchesInboundSessionJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } 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); if(matchResult == olm_error()) { - const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## matchesInboundSessionFromIdKeyJni(): failure - no match Msg=%s",errorMsgPtr); + LOGE("## matchesInboundSessionFromIdKeyJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } else { @@ -529,8 +524,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz encryptedMsgLength); if(result == olm_error()) { - const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## encryptMessageJni(): failure - Msg=%s",errorMsgPtr); + LOGE("## encryptMessageJni(): failure - Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } else { @@ -542,11 +536,10 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz // update message: encryptedMsgPtr => encryptedJstring jstring encryptedJstring = env->NewStringUTF((const char*)encryptedMsgPtr); - size_t encryptedUtfMsgLength = (size_t)env->GetStringUTFLength(encryptedJstring); env->SetObjectField(aEncryptedMsg, encryptedMsgFieldId, (jobject)encryptedJstring); 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()) { - const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s",errorMsgPtr); + LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } else { @@ -662,8 +654,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t maxPlainTextLength); if(plaintextLength == olm_error()) { - const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## decryptMessageJni(): failure - olm_decrypt Msg=%s",errorMsgPtr); + LOGE("## decryptMessageJni(): failure - olm_decrypt Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } else { @@ -728,8 +719,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, job if (result == olm_error()) { - const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## getSessionIdentifierJni(): failure - get session identifier failure Msg=%s",errorMsgPtr); + LOGE("## getSessionIdentifierJni(): failure - get session identifier failure Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } else { diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp index 6248d34..e11f7eb 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp @@ -203,8 +203,7 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jst hashLength); if(result == olm_error()) { - const char *errorMsgPtr = olm_utility_last_error(utilityPtr); - LOGE("## sha256Jni(): failure - hash creation Msg=%s",errorMsgPtr); + LOGE("## sha256Jni(): failure - hash creation Msg=%s",(const char *)olm_utility_last_error(utilityPtr)); } else { |