Add serialization for outbound group session

This commit is contained in:
pedroGitt 2016-10-21 19:05:20 +02:00
parent 71f57b79e5
commit fae857582c
11 changed files with 319 additions and 14 deletions

View file

@ -183,16 +183,27 @@ public class OlmGroupSessionTest {
outboundGroupSessionSerial = (OlmOutboundGroupSession) objectInput.readObject(); outboundGroupSessionSerial = (OlmOutboundGroupSession) objectInput.readObject();
objectInput.close(); objectInput.close();
// get sessions IDs // get sessions keys
String sessionKeyRef = outboundGroupSessionRef.sessionKey(); String sessionKeyRef = outboundGroupSessionRef.sessionKey();
String sessionKeySerial = outboundGroupSessionSerial.sessionKey(); String sessionKeySerial = outboundGroupSessionSerial.sessionKey();
// session ID sanity check // session keys sanity check
assertFalse(TextUtils.isEmpty(sessionKeyRef)); assertFalse(TextUtils.isEmpty(sessionKeyRef));
assertFalse(TextUtils.isEmpty(sessionKeySerial)); assertFalse(TextUtils.isEmpty(sessionKeySerial));
// session IDs comparison // session keys comparison
assertTrue(sessionKeyRef.equals(sessionKeySerial)); assertTrue(sessionKeyRef.equals(sessionKeySerial));
// get sessions IDs
String sessionIdRef = outboundGroupSessionRef.sessionIdentifier();
String sessionIdSerial = outboundGroupSessionSerial.sessionIdentifier();
// session ID sanity check
assertFalse(TextUtils.isEmpty(sessionIdRef));
assertFalse(TextUtils.isEmpty(sessionIdSerial));
// session IDs comparison
assertTrue(sessionIdRef.equals(sessionIdSerial));
} }
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());

View file

@ -31,6 +31,8 @@ public class OlmException extends Exception {
public static final int EXCEPTION_CODE_SESSION_DESERIALIZATION = 7; public static final int EXCEPTION_CODE_SESSION_DESERIALIZATION = 7;
public static final int EXCEPTION_CODE_INIT_ACCOUNT_CREATION = 8; public static final int EXCEPTION_CODE_INIT_ACCOUNT_CREATION = 8;
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_DESERIALIZATION = 11;
// 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";
@ -39,7 +41,7 @@ public class OlmException extends Exception {
public static final String EXCEPTION_MSG_INIT_INBOUND_GROUP_SESSION = "failed to initialize a new inbound group Session"; public static final String EXCEPTION_MSG_INIT_INBOUND_GROUP_SESSION = "failed to initialize a new inbound group Session";
public static final String EXCEPTION_MSG_INIT_NEW_ACCOUNT_DESERIALIZATION = "initNewAccount() failure"; public static final String EXCEPTION_MSG_INIT_NEW_ACCOUNT_DESERIALIZATION = "initNewAccount() failure";
public static final String EXCEPTION_MSG_INIT_ACCOUNT_DESERIALIZATION = "initWithSerializedData() failure"; public static final String EXCEPTION_MSG_INIT_ACCOUNT_DESERIALIZATION = "initWithSerializedData() failure";
public static final String EXCEPTION_MSG_INVALID_PARAMS_DESERIALIZATION = "invalid deserialized parameters"; public static final String EXCEPTION_MSG_INVALID_PARAMS_DESERIALIZATION = "invalid de-serialized parameters";
public static final String EXCEPTION_MSG_INIT_ACCOUNT_CREATION = "Account constructor failure"; public static final String EXCEPTION_MSG_INIT_ACCOUNT_CREATION = "Account constructor failure";
public static final String EXCEPTION_MSG_INIT_SESSION_CREATION = "Session constructor failure"; public static final String EXCEPTION_MSG_INIT_SESSION_CREATION = "Session constructor failure";

View file

@ -26,7 +26,7 @@ import android.util.Log;
import java.io.Serializable; import java.io.Serializable;
public class OlmInboundGroupSession implements Serializable { public class OlmInboundGroupSession implements Serializable {
private static final long serialVersionUID = -772028491251653253L;
private static final String LOG_TAG = "OlmInboundGroupSession"; private static final String LOG_TAG = "OlmInboundGroupSession";
/** session raw pointer value returned by JNI.<br> /** session raw pointer value returned by JNI.<br>

View file

@ -17,10 +17,17 @@
package org.matrix.olm; package org.matrix.olm;
public class OlmManager { import android.util.Log;
static { public class OlmManager {
java.lang.System.loadLibrary("olm"); private static final String LOG_TAG = "OlmManager";
public OlmManager() {
try {
java.lang.System.loadLibrary("olm");
} catch(UnsatisfiedLinkError e) {
Log.e(LOG_TAG,"Exception loadLibrary() - Msg="+e.getMessage());
}
} }
/** /**

View file

@ -18,8 +18,15 @@ package org.matrix.olm;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
public class OlmOutboundGroupSession { import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class OlmOutboundGroupSession implements Serializable {
private static final long serialVersionUID = -3133097431283604416L;
private static final String LOG_TAG = "OlmOutboundGroupSession"; private static final String LOG_TAG = "OlmOutboundGroupSession";
/** session raw pointer value returned by JNI.<br> /** session raw pointer value returned by JNI.<br>
@ -53,6 +60,120 @@ public class OlmOutboundGroupSession {
} }
} }
/**
* 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_OUTBOUND_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_OUTBOUND_GROUP_SESSION_DESERIALIZATION, OlmException.EXCEPTION_MSG_INVALID_PARAMS_DESERIALIZATION+" key");
} 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()) {
throw new OlmException(OlmException.EXCEPTION_CODE_OUTBOUND_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_OUTBOUND_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;
}
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;
}
private native String initWithSerializedDataJni(String aSerializedData, String aKey);
/** /**
* Release native session and invalid its JAVA reference counter part.<br> * Release native session and invalid its JAVA reference counter part.<br>
* Public API for {@link #releaseSessionJni()}. * Public API for {@link #releaseSessionJni()}.

View file

@ -97,7 +97,7 @@ public class OlmSession implements Serializable {
} }
/** /**
* Return an account as a base64 string.<br> * Return a session as a base64 string.<br>
* The account is serialized and encrypted with aKey. * The account is serialized and encrypted with aKey.
* In case of failure, an error human readable * In case of failure, an error human readable
* description is provide in aErrorMsg. * description is provide in aErrorMsg.
@ -124,7 +124,7 @@ public class OlmSession implements Serializable {
/** /**
* Loads an account from a pickled base64 string.<br> * Loads a session from a pickled base64 string.<br>
* See {@link #serializeDataWithKey(String, StringBuffer)} * See {@link #serializeDataWithKey(String, StringBuffer)}
* @param aSerializedData pickled account in a base64 string format * @param aSerializedData pickled account in a base64 string format
* @param aKey key used to encrypted * @param aKey key used to encrypted

View file

@ -47,7 +47,8 @@ olm_account.cpp \
olm_session.cpp \ olm_session.cpp \
olm_jni_helper.cpp \ olm_jni_helper.cpp \
olm_inbound_group_session.cpp \ olm_inbound_group_session.cpp \
olm_outbound_group_session.cpp olm_outbound_group_session.cpp \
olm_utility.cpp
LOCAL_LDLIBS := -llog LOCAL_LDLIBS := -llog

View file

@ -642,7 +642,6 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, j
{ {
LOGD(" ## initWithSerializedDataJni(): success - result=%lu ", result); LOGD(" ## initWithSerializedDataJni(): success - result=%lu ", result);
} }
} }
// free alloc // free alloc

View file

@ -330,4 +330,166 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(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_OUTBOUND_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;
OlmOutboundGroupSession* sessionPtr = NULL;
LOGD("## outbound group session serializeDataWithKeyJni(): IN");
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(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_outbound_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_outbound_group_session(sessionPtr,
(void const *)keyPtr,
keyLength,
(void*)pickledPtr,
pickledLength);
if(result == olm_error())
{
const char *errorMsgPtr = olm_outbound_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_OUTBOUND_GROUP_SESSION_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, jobject thiz, jstring aSerializedData, jstring aKey)
{
OlmOutboundGroupSession* sessionPtr = NULL;
jstring errorMessageRetValue = 0;
const char *keyPtr = NULL;
const char *pickledPtr = NULL;
LOGD("## initWithSerializedDataJni(): IN");
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(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_outbound_group_session(sessionPtr,
(void const *)keyPtr,
keyLength,
(void*)pickledPtr,
pickledLength);
if(result == olm_error())
{
const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr);
LOGE(" ## initWithSerializedDataJni(): failure - olm_unpickle_outbound_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

@ -38,6 +38,9 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jstring aClearMsgPtr); JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jstring aClearMsgPtr);
// serialization
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jstring aKey, jobject aErrorMsg);
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, jobject thiz, jstring aSerializedData, jstring aKey);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -41,7 +41,6 @@ OlmSession* initializeSessionMemory()
return sessionPtr; return sessionPtr;
} }
JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz) JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz)
{ {
LOGD("## createNewSessionJni(): IN"); LOGD("## createNewSessionJni(): IN");