Commit fb87d8fe authored by pedroGitt's avatar pedroGitt
Browse files

Serialization for OlmAccount and OlmSession OK

parent 1511962d
......@@ -42,7 +42,7 @@ public class OlmAccountTest {
private static OlmAccount mOlmAccount;
private static OlmManager mOlmManager;
private boolean mIsAccountCreated;
final String FILE_NAME = "SerialTestFile";
private final String FILE_NAME = "SerialTestFile";
@BeforeClass
public static void setUpClass(){
......@@ -170,7 +170,12 @@ public class OlmAccountTest {
@Test
public void test10RemoveOneTimeKeysForSession() {
OlmSession olmSession = new OlmSession();
OlmSession olmSession = null;
try {
olmSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
long sessionId = olmSession.getOlmSessionId();
assertTrue(0 != sessionId);
......@@ -230,16 +235,16 @@ public class OlmAccountTest {
try {
Context context = getInstrumentation().getContext();
context.getFilesDir();
//context.getFilesDir();
fileOutput = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
// serialize
// serialize account
objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(accountRef);
objectOutput.flush();
objectOutput.close();
// deserialize
// deserialize account
FileInputStream fileInput = context.openFileInput(FILE_NAME);
ObjectInputStream objectInput = new ObjectInputStream(fileInput);
accountDeserial = (OlmAccount) objectInput.readObject();
......@@ -262,7 +267,6 @@ public class OlmAccountTest {
accountRef.releaseAccount();
accountDeserial.releaseAccount();
}
catch (FileNotFoundException e) {
Log.e(LOG_TAG, "## test13Serialization(): Exception FileNotFoundException Msg=="+e.getMessage());
}
......@@ -278,7 +282,6 @@ public class OlmAccountTest {
catch (Exception e) {
Log.e(LOG_TAG, "## test13Serialization(): Exception Msg==" + e.getMessage());
}
}
......
package org.matrix.olm;
import android.content.Context;
import android.support.test.runner.AndroidJUnit4;
import android.text.TextUtils;
import android.util.Log;
......@@ -11,6 +12,14 @@ import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
......@@ -19,6 +28,7 @@ import static org.junit.Assert.assertTrue;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OlmGroupSessionTest {
private static final String LOG_TAG = "OlmSessionTest";
private final String FILE_NAME_SERIAL_SESSION = "SerialGroupSession";
private static OlmManager mOlmManager;
private static OlmOutboundGroupSession mAliceOutboundGroupSession;
......@@ -141,4 +151,66 @@ public class OlmGroupSessionTest {
// release group sessions
mBobInboundGroupSession.releaseSession();
}
@Test
public void test14SerializeOutboundSession() {
OlmOutboundGroupSession outboundGroupSessionRef=null;
OlmOutboundGroupSession outboundGroupSessionSerial=null;
// alice creates OUTBOUND GROUP SESSION
try {
outboundGroupSessionRef = new OlmOutboundGroupSession();
} catch (OlmException e) {
assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false);
}
assertNotNull(outboundGroupSessionRef);
// serialize alice session
Context context = getInstrumentation().getContext();
try {
FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_SESSION, Context.MODE_PRIVATE);
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(outboundGroupSessionRef);
objectOutput.flush();
objectOutput.close();
// deserialize session
FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_SESSION);
ObjectInputStream objectInput = new ObjectInputStream(fileInput);
outboundGroupSessionSerial = (OlmOutboundGroupSession) objectInput.readObject();
objectInput.close();
// get sessions IDs
String sessionKeyRef = outboundGroupSessionRef.sessionKey();
String sessionKeySerial = outboundGroupSessionSerial.sessionKey();
// session ID sanity check
assertFalse(TextUtils.isEmpty(sessionKeyRef));
assertFalse(TextUtils.isEmpty(sessionKeySerial));
// session IDs comparison
assertTrue(sessionKeyRef.equals(sessionKeySerial));
}
catch (FileNotFoundException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception FileNotFoundException Msg=="+e.getMessage());
}
catch (ClassNotFoundException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception ClassNotFoundException Msg==" + e.getMessage());
}
catch (IOException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception IOException Msg==" + e.getMessage());
}
/*catch (OlmException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception OlmException Msg==" + e.getMessage());
}*/
catch (Exception e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception Msg==" + e.getMessage());
}
}
}
package org.matrix.olm;
import android.content.Context;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;
......@@ -11,8 +12,15 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
......@@ -20,6 +28,7 @@ import static org.junit.Assert.assertTrue;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OlmSessionTest {
private static final String LOG_TAG = "OlmSessionTest";
private final String FILE_NAME_SERIAL_SESSION = "SerialSession";
private static OlmManager mOlmManager;
......@@ -86,11 +95,16 @@ public class OlmSessionTest {
}
assertNotNull(bobOneTimeKey);
} catch (JSONException e) {
assertTrue("Exception MSg="+e.getMessage(), false);
assertTrue("Exception Msg="+e.getMessage(), false);
}
// CREATE ALICE SESSION
OlmSession aliceSession = new OlmSession();
OlmSession aliceSession = null;
try {
aliceSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=aliceSession.getOlmSessionId());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob
......@@ -101,7 +115,12 @@ public class OlmSessionTest {
Log.d(LOG_TAG,"## test01AliceToBob(): encryptedMsg="+encryptedMsgToBob.mCipherText);
// CREATE BOB INBOUND SESSION and decrypt message from alice
OlmSession bobSession = new OlmSession();
OlmSession bobSession = null;
try {
bobSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=bobSession.getOlmSessionId());
assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText));
String decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob);
......@@ -173,6 +192,7 @@ public class OlmSessionTest {
Iterator<String> generatedKeysIt = generatedKeys.keys();
if(generatedKeysIt.hasNext()) {
// return first otk
bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
}
assertNotNull(bobOneTimeKey);
......@@ -181,7 +201,12 @@ public class OlmSessionTest {
}
// CREATE ALICE SESSION
OlmSession aliceSession = new OlmSession();
OlmSession aliceSession = null;
try {
aliceSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=aliceSession.getOlmSessionId());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob
......@@ -192,7 +217,12 @@ public class OlmSessionTest {
assertNotNull(encryptedAliceToBobMsg1);
// CREATE BOB INBOUND SESSION and decrypt message from alice
OlmSession bobSession = new OlmSession();
OlmSession bobSession = null;
try {
bobSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=bobSession.getOlmSessionId());
assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));
......@@ -210,6 +240,7 @@ public class OlmSessionTest {
String clearMsg2 = "Isn't life grand?";
String clearMsg3 = "Let's go to the opera.";
// bob encrypts messages
OlmMessage encryptedMsg1 = bobSession.encryptMessage(clearMsg1);
assertNotNull(encryptedMsg1);
OlmMessage encryptedMsg2 = bobSession.encryptMessage(clearMsg2);
......@@ -217,6 +248,7 @@ public class OlmSessionTest {
OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
assertNotNull(encryptedMsg3);
// alice decrypts bob's messages
String decryptedMsg1 = aliceSession.decryptMessage(encryptedMsg1);
assertNotNull(decryptedMsg1);
String decryptedMsg2 = aliceSession.decryptMessage(encryptedMsg2);
......@@ -224,6 +256,7 @@ public class OlmSessionTest {
String decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3);
assertNotNull(decryptedMsg3);
// comparison tests
assertTrue(clearMsg1.equals(decryptedMsg1));
assertTrue(clearMsg2.equals(decryptedMsg2));
assertTrue(clearMsg3.equals(decryptedMsg3));
......@@ -235,6 +268,7 @@ public class OlmSessionTest {
aliceSession.releaseSession();
}
@Test
public void test03AliceBobSessionId() {
// creates alice & bob accounts
......@@ -252,11 +286,22 @@ public class OlmSessionTest {
assertTrue(0!=aliceAccount.getOlmAccountId());
// CREATE ALICE SESSION
OlmSession aliceSession = new OlmSession();
OlmSession aliceSession = null;
try {
aliceSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=aliceSession.getOlmSessionId());
// CREATE BOB INBOUND SESSION and decrypt message from alice
OlmSession bobSession = new OlmSession();
// CREATE ALICE SESSION
OlmSession bobSession = null;
try {
bobSession = new OlmSession();
} catch (OlmException e) {
e.printStackTrace();
}
assertTrue(0!=bobSession.getOlmSessionId());
String aliceSessionId = aliceSession.sessionIdentifier();
......@@ -268,4 +313,164 @@ public class OlmSessionTest {
// must be the same for both ends of the conversation
assertTrue(aliceSessionId.equals(bobSessionId));
}
// ********************************************************
// ************* SERIALIZATION TEST ***********************
// ********************************************************
/**
* Same as test02AliceToBobBackAndForth() but alice's session
* is serialized and de-serialized before performing the final
* comparison (encrypt vs )
*/
@Test
public void test03SessionSerialization() {
final int ONE_TIME_KEYS_NUMBER = 1;
String bobIdentityKey = null;
String bobOneTimeKey=null;
OlmAccount aliceAccount = null;
OlmAccount bobAccount = null;
OlmSession aliceSessionDeserial;
// creates alice & bob accounts
try {
aliceAccount = new OlmAccount();
bobAccount = new OlmAccount();
} catch (OlmException e) {
assertTrue(e.getMessage(),false);
}
// test accounts creation
assertTrue(0!=bobAccount.getOlmAccountId());
assertTrue(0!=aliceAccount.getOlmAccountId());
// get bob identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
assertNotNull(bobIdentityKeysJson);
try {
bobIdentityKey = bobIdentityKeysJson.getString(OlmAccount.JSON_KEY_IDENTITY_KEY);
assertTrue(null!=bobIdentityKey);
} catch (JSONException e) {
assertTrue("Exception MSg="+e.getMessage(), false);
}
// get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
assertNotNull(bobOneTimeKeysJsonObj);
try {
JSONObject generatedKeys = bobOneTimeKeysJsonObj.getJSONObject(OlmAccount.JSON_KEY_ONE_TIME_KEY);
assertNotNull(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", generatedKeys);
Iterator<String> generatedKeysIt = generatedKeys.keys();
if(generatedKeysIt.hasNext()) {
// return first otk
bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
}
assertNotNull(bobOneTimeKey);
} catch (JSONException e) {
assertTrue("Exception MSg="+e.getMessage(), false);
}
// CREATE ALICE SESSION
OlmSession aliceSession = null;
try {
aliceSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=aliceSession.getOlmSessionId());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob
assertNotNull(aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey));
String helloClearMsg = "Hello I'm Alice!";
OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg);
assertNotNull(encryptedAliceToBobMsg1);
// CREATE BOB INBOUND SESSION and decrypt message from alice
OlmSession bobSession = null;
try {
bobSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=bobSession.getOlmSessionId());
assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));
// DECRYPT MESSAGE FROM ALICE
String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1);
assertNotNull(decryptedMsg01);
// MESSAGE COMPARISON: decrypted vs encrypted
assertTrue(helloClearMsg.equals(decryptedMsg01));
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
// BACK/FORTH MESSAGE COMPARISON
String clearMsg1 = "Hello I'm Bob!";
String clearMsg2 = "Isn't life grand?";
String clearMsg3 = "Let's go to the opera.";
// bob encrypts messages
OlmMessage encryptedMsg1 = bobSession.encryptMessage(clearMsg1);
assertNotNull(encryptedMsg1);
OlmMessage encryptedMsg2 = bobSession.encryptMessage(clearMsg2);
assertNotNull(encryptedMsg2);
OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
assertNotNull(encryptedMsg3);
// serialize alice session
Context context = getInstrumentation().getContext();
try {
FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_SESSION, Context.MODE_PRIVATE);
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(aliceSession);
objectOutput.flush();
objectOutput.close();
// deserialize session
FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_SESSION);
ObjectInputStream objectInput = new ObjectInputStream(fileInput);
aliceSessionDeserial = (OlmSession) objectInput.readObject();
objectInput.close();
// test deserialize return value
assertNotNull(aliceSessionDeserial);
// de-serialized alice session decrypts bob's messages
String decryptedMsg1 = aliceSessionDeserial.decryptMessage(encryptedMsg1);
assertNotNull(decryptedMsg1);
String decryptedMsg2 = aliceSessionDeserial.decryptMessage(encryptedMsg2);
assertNotNull(decryptedMsg2);
String decryptedMsg3 = aliceSessionDeserial.decryptMessage(encryptedMsg3);
assertNotNull(decryptedMsg3);
// comparison tests
assertTrue(clearMsg1.equals(decryptedMsg1));
assertTrue(clearMsg2.equals(decryptedMsg2));
assertTrue(clearMsg3.equals(decryptedMsg3));
// clean objects..
bobAccount.releaseAccount();
aliceAccount.releaseAccount();
bobSession.releaseSession();
aliceSession.releaseSession();
aliceSessionDeserial.releaseSession();
}
catch (FileNotFoundException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception FileNotFoundException Msg=="+e.getMessage());
}
catch (ClassNotFoundException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception ClassNotFoundException Msg==" + e.getMessage());
}
catch (IOException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception IOException Msg==" + e.getMessage());
}
/*catch (OlmException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception OlmException Msg==" + e.getMessage());
}*/
catch (Exception e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception Msg==" + e.getMessage());
}
}
}
......@@ -31,8 +31,6 @@ import java.util.Random;
public class OlmAccount implements Serializable {
private static final long serialVersionUID = 3497486121598434824L;
private static final String LOG_TAG = "OlmAccount";
private static final int RANDOM_KEY_SIZE = 32;
private static final int RANDOM_RANGE = 256;
// JSON keys used in the JSON objects returned by JNI
/** As well as the identity key, each device creates a number of Curve25519 key pairs which are
......@@ -63,24 +61,17 @@ public class OlmAccount implements Serializable {
}
}
private String getRandomKey() {
String keyRetValue;
Random rand = new Random();
StringBuilder strBuilder = new StringBuilder();
for(int i = 0; i< OlmAccount.RANDOM_KEY_SIZE; i++) {
strBuilder.append(rand.nextInt(RANDOM_RANGE));
}
keyRetValue = strBuilder.toString();
return keyRetValue;
}
/**
* 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 = getRandomKey();
String key = OlmUtility.getRandomKey();
// compute pickle string
StringBuffer errorMsg = new StringBuffer();
......@@ -94,6 +85,13 @@ public class OlmAccount implements Serializable {
}
}
/**
* 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();
......@@ -379,9 +377,4 @@ public class OlmAccount implements Serializable {
return signMessageJni(aMessage);
}
private native String signMessageJni(String aMessage);
// TODO missing API: initWithSerializedData
// TODO missing API: serializeDataWithKey
// TODO missing API: initWithCoder
// TODO missing API: encodeWithCoder
}
......@@ -30,6 +30,7 @@ public class OlmException extends Exception {
public static final int EXCEPTION_CODE_SESSION_SERIALIZATION = 6;
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_SESSION_CREATION = 9;
// exception human readable messages
public static final String EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION = "failed to create a new outbound group Session";
......@@ -40,6 +41,7 @@ public class OlmException extends Exception {
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_INIT_ACCOUNT_CREATION = "Account constructor failure";
public static final String EXCEPTION_MSG_INIT_SESSION_CREATION = "Session constructor failure";
/** exception code to be taken from: {@link #EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION} {@link #EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION}
* {@link #EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION} {@link #EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION}**/
......
......@@ -19,37 +19,146 @@ 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 OlmSession implements Serializable {
private static final long serialVersionUID = -8975488639186976419L;
private static final String LOG_TAG = "OlmSession";
/** session raw pointer value (OlmSession*) returned by JNI.
* this value uniquely identifies the native session instance.
**/
private long mNativeOlmSessionId;
private transient long mNativeOlmSessionId;
/** account instance associated with this session. **/
private OlmAccount mOlmAccount;
public OlmSession() {
initNewSession();
public OlmSession() throws OlmException {
if(!initNewSession()) {
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_SESSION_CREATION, OlmException.EXCEPTION_MSG_INIT_SESSION_CREATION);