Commit 7bf7a7e4 authored by ylecollen's avatar ylecollen
Browse files

use the same way to name the creation method i.e. createNewXX. Avoid the initWithXX.

parent 13d3f4a1
......@@ -17,6 +17,7 @@
package org.matrix.olm;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONObject;
......@@ -61,7 +62,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
private transient long mNativeId;
public OlmAccount() throws OlmException {
initNewAccount();
createNewAccount();
}
/**
......@@ -85,43 +86,23 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
* Destroy the corresponding OLM account 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 #initNewAccountJni()}.
* See {@link #createNewAccountJni()}.
*/
private native void releaseAccountJni();
/**
* Create and initialize a native account instance.<br>
* Wrapper for {@link #initNewAccountJni()}.
* To be called before any other API call.
* @exception OlmException the failure reason
*/
private void initNewAccount() throws OlmException {
private void createNewAccount() throws OlmException {
try {
mNativeId = initNewAccountJni();
mNativeId = createNewAccountJni();
} catch (Exception e) {
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION, e.getMessage());
}
}
/**
* 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 #mNativeId})
*/
private native long initNewAccountJni();
/**
* Create a native account instance without any initialization.<br>
* Since the account is left uninitialized, this
* method is intended to be used in the serialization mechanism (see {@link #readObject(ObjectInputStream)}).<br>
* Public wrapper for {@link #createNewAccountJni()}.
* @return true if init succeed, false otherwise.
*/
private boolean createNewAccount() {
mNativeId = initNewAccountJni();
return (0 != mNativeId);
}
/**
* Create an OLM account in native side.<br>
* Do not forget to call {@link #releaseAccount()} when JAVA side is done.
......@@ -408,10 +389,9 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
/**
* Kick off the deserialization mechanism.
* @param aInStream input stream
* @throws IOException exception
* @throws ClassNotFoundException exception
* @throws Exception exception
*/
private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException {
private void readObject(ObjectInputStream aInStream) throws Exception {
deserialize(aInStream);
}
......@@ -453,31 +433,28 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
* See {@link #serialize(byte[], StringBuffer)}
* @param aSerializedData bytes buffer
* @param aKey key used to encrypted
* @exception Exception the exception
*/
@Override
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws IOException {
if (!createNewAccount()) {
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION);
}
StringBuffer errorMsg = new StringBuffer();
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception {
createNewAccount();
String errorMsg;
try {
String jniError;
if ((null == aSerializedData) || (null == aKey)) {
Log.e(LOG_TAG, "## deserialize(): invalid input parameters");
errorMsg.append("invalid input parameters");
} else if (null != (jniError = deserializeJni(aSerializedData, aKey))) {
errorMsg.append(jniError);
errorMsg = "invalid input parameters";
} else {
errorMsg = deserializeJni(aSerializedData, aKey);
}
} catch (Exception e) {
Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage());
errorMsg.append(e.getMessage());
errorMsg = e.getMessage();
}
if (errorMsg.length() > 0) {
if (!TextUtils.isEmpty(errorMsg)) {
releaseAccount();
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, String.valueOf(errorMsg));
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg);
}
}
......
......@@ -55,6 +55,7 @@ public class OlmException extends IOException {
public static final int EXCEPTION_CODE_SESSION_DECRYPT_MESSAGE = 405;
public static final int EXCEPTION_CODE_SESSION_SESSION_IDENTIFIER = 406;
public static final int EXCEPTION_CODE_UTILITY_CREATION = 501;
public static final int EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE = 500;
// exception human readable messages
......
......@@ -61,11 +61,8 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
* @throws OlmException constructor failure
*/
public OlmInboundGroupSession(String aSessionKey) throws OlmException {
if(createNewSession()) {
initInboundGroupSession(aSessionKey);
} else {
throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION, OlmException.EXCEPTION_MSG_NEW_INBOUND_GROUP_SESSION);
}
createNewSession();
initInboundGroupSession(aSessionKey);
}
/**
......@@ -88,11 +85,14 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
/**
* Create and save the session native instance ID.<br>
* To be called before any other API call.
* @return true if init succeed, false otherwise.
* @exception OlmException the failure reason
*/
private boolean createNewSession() {
mNativeId = createNewSessionJni();
return (0 != mNativeId);
private void createNewSession() throws OlmException {
try {
mNativeId = createNewSessionJni();
} catch (Exception e) {
throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION, e.getMessage());
}
}
/**
......@@ -190,10 +190,9 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
/**
* Kick off the deserialization mechanism.
* @param aInStream input stream
* @throws IOException exception
* @throws ClassNotFoundException exception
* @throws Exception exception
*/
private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException {
private void readObject(ObjectInputStream aInStream) throws Exception {
deserialize(aInStream);
}
......@@ -242,29 +241,26 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
* @param aKey key used to encrypted
*/
@Override
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws IOException {
if (!createNewSession()) {
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION);
}
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception {
createNewSession();
StringBuffer errorMsg = new StringBuffer();
String errorMsg;
try {
String jniError;
if ((null == aSerializedData) || (null == aKey)) {
Log.e(LOG_TAG, "## deserialize(): invalid input parameters");
errorMsg.append("invalid input parameters");
} else if (null != (jniError = deserializeJni(aSerializedData, aKey))) {
errorMsg.append(jniError);
errorMsg = "invalid input parameters";
} else {
errorMsg = deserializeJni(aSerializedData, aKey);
}
} catch (Exception e) {
Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage());
errorMsg.append(e.getMessage());
errorMsg = e.getMessage();
}
if (errorMsg.length() > 0) {
if (!TextUtils.isEmpty(errorMsg)) {
releaseSession();
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, String.valueOf(errorMsg));
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg);
}
}
......
......@@ -42,8 +42,6 @@ public class OlmManager {
}
public String getSdkOlmVersion() {
//Date currentDate = Calendar.getInstance().getTime();
//String retVal = new SimpleDateFormat("yyyyMMdd_HH:mm:ss").format(currentDate);
return SDK_OLM_VERSION;
}
......
......@@ -50,11 +50,8 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
* @throws OlmException constructor failure
*/
public OlmOutboundGroupSession() throws OlmException {
if(createNewSession()) {
initOutboundGroupSession();
} else {
throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION, OlmException.EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION);
}
createNewSession();
initOutboundGroupSession();
}
/**
......@@ -78,11 +75,14 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
* Create and save the session native instance ID.
* Wrapper for {@link #createNewSessionJni()}.<br>
* To be called before any other API call.
* @return true if init succeed, false otherwise.
* @exception OlmException the exception.
*/
private boolean createNewSession() {
mNativeId = createNewSessionJni();
return (0 != mNativeId);
private void createNewSession() throws OlmException {
try {
mNativeId = createNewSessionJni();
} catch (Exception e) {
throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION, e.getMessage());
}
}
/**
......@@ -203,10 +203,9 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
/**
* Kick off the deserialization mechanism.
* @param aInStream input stream
* @throws IOException exception
* @throws ClassNotFoundException exception
* @throws Exception exception
*/
private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException {
private void readObject(ObjectInputStream aInStream) throws Exception {
deserialize(aInStream);
}
......@@ -247,31 +246,29 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
* See {@link #serialize(byte[], StringBuffer)}
* @param aSerializedData pickled account in a base64 bytes buffer
* @param aKey key used to encrypted
* @exception Exception the exception
*/
@Override
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws IOException {
if (!createNewSession()) {
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION);
}
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception {
createNewSession();
StringBuffer errorMsg = new StringBuffer();
String errorMsg = null;
try {
String jniError;
if ((null == aSerializedData) || (null == aKey)) {
Log.e(LOG_TAG, "## deserialize(): invalid input parameters");
errorMsg.append("invalid input parameters");
} else if (null != (jniError = deserializeJni(aSerializedData, aKey))) {
errorMsg.append(jniError);
errorMsg = "invalid input parameters";
} else {
errorMsg = deserializeJni(aSerializedData, aKey);
}
} catch (Exception e) {
Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage());
errorMsg.append(e.getMessage());
errorMsg = e.getMessage();
}
if (errorMsg.length() > 0) {
if (!TextUtils.isEmpty(errorMsg)) {
releaseSession();
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, String.valueOf(errorMsg));
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg);
}
}
......
......@@ -43,9 +43,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
private transient long mNativeId;
public OlmSession() throws OlmException {
if(!initNewSession()) {
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_SESSION_CREATION, OlmException.EXCEPTION_MSG_INIT_SESSION_CREATION);
}
createNewSession();
}
/**
......@@ -60,7 +58,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
* Destroy the corresponding OLM 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();
......@@ -73,34 +71,19 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
mNativeId = 0;
}
/**
* Create and save the session native instance ID.
* Wrapper for {@link #initNewSessionJni()}.<br>
* To be called before any other API call.
* @return true if init succeed, false otherwise.
*/
private boolean initNewSession() {
mNativeId = initNewSessionJni();
return (0 != mNativeId);
}
/**
* 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 #mNativeId})
*/
private native long initNewSessionJni();
/**
* Create a native account instance without any initialization.<br>
* Since the account is left uninitialized, this
* method is intended to be used in the serialization mechanism (see {@link #readObject(ObjectInputStream)}).<br>
* Public wrapper for {@link #createNewSessionJni()}.
* @return true if init succeed, false otherwise.
* @exception OlmException the exception
*/
private boolean createNewSession() {
mNativeId = initNewSessionJni();
return (0 != mNativeId);
private void createNewSession() throws OlmException {
try {
mNativeId = createNewSessionJni();
} catch (Exception e) {
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_SESSION_CREATION, e.getMessage());
}
}
/**
......@@ -331,7 +314,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
* @throws IOException exception
* @throws ClassNotFoundException exception
*/
private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException {
private void readObject(ObjectInputStream aInStream) throws Exception {
deserialize(aInStream);
}
......@@ -374,10 +357,8 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
* @param aKey key used to encrypted
*/
@Override
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws IOException {
if (!createNewSession()) {
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION);
}
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception {
createNewSession();
StringBuffer errorMsg = new StringBuffer();
......
......@@ -29,7 +29,6 @@ public class OlmUtility {
private static final String LOG_TAG = "OlmUtility";
public static final int RANDOM_KEY_SIZE = 32;
public static final int RANDOM_RANGE = 256;
/** Instance Id returned by JNI.
* This value uniquely identifies this utility instance.
......@@ -46,11 +45,11 @@ public class OlmUtility {
* @return true if init succeed, false otherwise.
*/
private boolean initUtility() {
mNativeId = initUtilityJni();
mNativeId = createUtilityJni();
return (0 != mNativeId);
}
private native long initUtilityJni();
private native long createUtilityJni();
/**
* Release native instance.<br>
......@@ -136,6 +135,12 @@ public class OlmUtility {
SecureRandom secureRandom = new SecureRandom();
byte[] buffer = new byte[RANDOM_KEY_SIZE];
secureRandom.nextBytes(buffer);
// the key is saved as string
// so avoid the UTF8 marker bytes
for(int i = 0; i < RANDOM_KEY_SIZE; i++) {
buffer[i] = (byte)(buffer[i] & 0x7F);
}
return buffer;
}
......
......@@ -42,49 +42,13 @@ OlmAccount* initializeAccountMemory()
return accountPtr;
}
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz)
{
LOGD("## createNewAccountJni(): IN");
OlmAccount* accountPtr = initializeAccountMemory();
LOGD(" ## createNewAccountJni(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr);
return (jlong)(intptr_t)accountPtr;
}
/**
* Release the account allocation made by initializeAccountMemory().<br>
* This method MUST be called when java counter part account instance is done.
*
*/
JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz)
{
LOGD("## releaseAccountJni(): IN");
OlmAccount* accountPtr = getAccountInstanceId(env, thiz);
if (!accountPtr)
{
LOGE(" ## releaseAccountJni(): failure - invalid Account ptr=NULL");
}
else
{
LOGD(" ## releaseAccountJni(): accountPtr=%p",accountPtr);
olm_clear_account(accountPtr);
LOGD(" ## releaseAccountJni(): IN");
// even if free(NULL) does not crash, logs are performed for debug purpose
free(accountPtr);
LOGD(" ## releaseAccountJni(): OUT");
}
}
/**
* Initialize a new account and return it to JAVA side.<br>
* Create a new account and return it to JAVA side.<br>
* Since a C prt is returned as a jlong, special care will be taken
* to make the cast (OlmAccount* => jlong) platform independent.
* @return the initialized OlmAccount* instance
**/
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thiz)
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz)
{
const char* errorMessage = NULL;
OlmAccount *accountPtr = initializeAccountMemory();
......@@ -139,6 +103,32 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi
return (jlong)(intptr_t)accountPtr;
}
/**
* Release the account allocation made by initializeAccountMemory().<br>
* This method MUST be called when java counter part account instance is done.
*
*/
JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz)
{
LOGD("## releaseAccountJni(): IN");
OlmAccount* accountPtr = getAccountInstanceId(env, thiz);
if (!accountPtr)
{
LOGE(" ## releaseAccountJni(): failure - invalid Account ptr=NULL");
}
else
{
LOGD(" ## releaseAccountJni(): accountPtr=%p",accountPtr);
olm_clear_account(accountPtr);
LOGD(" ## releaseAccountJni(): IN");
// even if free(NULL) does not crash, logs are performed for debug purpose
free(accountPtr);
LOGD(" ## releaseAccountJni(): OUT");
}
}
// *********************************************************************
// ************************* IDENTITY KEYS API *************************
......
......@@ -30,7 +30,6 @@ extern "C" {
// account creation/destruction
JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz);
// identity keys
......
......@@ -58,6 +58,7 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env
**/
JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz)
{
const char* errorMessage = NULL;
OlmInboundGroupSession* sessionPtr = NULL;
size_t sessionSize = 0;
......@@ -67,6 +68,7 @@ JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *
if (!sessionSize)
{
LOGE(" ## createNewSessionJni(): failure - inbound group session size = 0");
errorMessage = "inbound group session size = 0";
}
else if ((sessionPtr = (OlmInboundGroupSession*)malloc(sessionSize)))
{
......@@ -76,6 +78,12 @@ JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *
else
{
LOGE(" ## createNewSessionJni(): failure - inbound group session OOM");
errorMessage = "inbound group session OOM";
}
if (errorMessage)
{
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
}
return (jlong)(intptr_t)sessionPtr;
......
......@@ -48,7 +48,16 @@ JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject t
LOGD("## createNewSessionJni(): IN");
OlmSession* accountPtr = initializeSessionMemory();
LOGD(" ## createNewSessionJni(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr);
if (!accountPtr)
{
LOGE("## initNewAccount(): failure - init session OOM");
env->ThrowNew(env->FindClass("java/lang/Exception"), "init session OOM");
}
else
{
LOGD(" ## createNewSessionJni(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr);
}
return (jlong)(intptr_t)accountPtr;
}
......@@ -70,31 +79,6 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz
}
}
/**
* Initialize a new session and return it to JAVA side.<br>
* Since a C prt is returned as a jlong, special care will be taken
* to make the cast (OlmSession* => jlong) platform independent.
* @return the initialized OlmSession* instance if init succeed, NULL otherwise
**/
JNIEXPORT jlong OLM_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz)
{
LOGD("## initNewSessionJni(): OlmSession IN");