Skip to content
Snippets Groups Projects
Commit 4ca8086a authored by ylecollen's avatar ylecollen
Browse files

OlmSession triggers exception when there is a failure.

parent 846ea49a
No related branches found
No related tags found
No related merge requests found
......@@ -131,9 +131,18 @@ public class OlmSessionTest {
assertTrue(0!=aliceSession.getOlmSessionId());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob
assertNotNull(aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey));
try {
aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
String clearMsg = "Heloo bob , this is alice!";
OlmMessage encryptedMsgToBob = aliceSession.encryptMessage(clearMsg);
OlmMessage encryptedMsgToBob = null;
try {
encryptedMsgToBob = aliceSession.encryptMessage(clearMsg);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(encryptedMsgToBob);
assertNotNull(encryptedMsgToBob.mCipherText);
Log.d(LOG_TAG,"## test01AliceToBob(): encryptedMsg="+encryptedMsgToBob.mCipherText);
......@@ -153,7 +162,12 @@ public class OlmSessionTest {
assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false);
}
String decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob);
String decryptedMsg = null;
try {
decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(decryptedMsg);
// MESSAGE COMPARISON: decrypted vs encrypted
......@@ -255,10 +269,22 @@ public class OlmSessionTest {
assertTrue(0!=aliceSession.getOlmSessionId());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob
assertNotNull(aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey));
try {
aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
String helloClearMsg = "Hello I'm Alice!";
OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg);
OlmMessage encryptedAliceToBobMsg1 = null;
try {
encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(encryptedAliceToBobMsg1);
assertNotNull(encryptedAliceToBobMsg1.mCipherText);
......@@ -279,7 +305,12 @@ public class OlmSessionTest {
}
// DECRYPT MESSAGE FROM ALICE
String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1);
String decryptedMsg01 = null;
try {
decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(decryptedMsg01);
// MESSAGE COMPARISON: decrypted vs encrypted
......@@ -291,19 +322,54 @@ public class OlmSessionTest {
String clearMsg3 = "Let's go to the opera.";
// bob encrypts messages
OlmMessage encryptedMsg1 = bobSession.encryptMessage(clearMsg1);
OlmMessage encryptedMsg1 = null;
try {
encryptedMsg1 = bobSession.encryptMessage(clearMsg1);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(encryptedMsg1);
OlmMessage encryptedMsg2 = bobSession.encryptMessage(clearMsg2);
OlmMessage encryptedMsg2 = null;
try {
encryptedMsg2 = bobSession.encryptMessage(clearMsg2);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(encryptedMsg2);
OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
OlmMessage encryptedMsg3 = null;
try {
encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(encryptedMsg3);
// alice decrypts bob's messages
String decryptedMsg1 = aliceSession.decryptMessage(encryptedMsg1);
String decryptedMsg1 = null;
try {
decryptedMsg1 = aliceSession.decryptMessage(encryptedMsg1);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(decryptedMsg1);
String decryptedMsg2 = aliceSession.decryptMessage(encryptedMsg2);
String decryptedMsg2 = null;
try {
decryptedMsg2 = aliceSession.decryptMessage(encryptedMsg2);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(decryptedMsg2);
String decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3);
String decryptedMsg3 = null;
try {
decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(decryptedMsg3);
// comparison tests
......@@ -313,9 +379,22 @@ public class OlmSessionTest {
// and one more from alice to bob
clearMsg1 = "another message from Alice to Bob!!";
encryptedMsg1 = aliceSession.encryptMessage(clearMsg1);
encryptedMsg1 = null;
try {
encryptedMsg1 = aliceSession.encryptMessage(clearMsg1);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(encryptedMsg1);
decryptedMsg1 = bobSession.decryptMessage(encryptedMsg1);
decryptedMsg1 = null;
try {
decryptedMsg1 = bobSession.decryptMessage(encryptedMsg1);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(decryptedMsg1);
assertTrue(clearMsg1.equals(decryptedMsg1));
......@@ -378,10 +457,21 @@ public class OlmSessionTest {
}
assertTrue(0!=bobSession.getOlmSessionId());
String aliceSessionId = aliceSession.sessionIdentifier();
String aliceSessionId = null;
try {
aliceSessionId = aliceSession.sessionIdentifier();
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(aliceSessionId);
String bobSessionId = bobSession.sessionIdentifier();
String bobSessionId = null;
try {
bobSessionId = bobSession.sessionIdentifier();
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(bobSessionId);
// must be the same for both ends of the conversation
......@@ -463,10 +553,21 @@ public class OlmSessionTest {
String bobOneTimeKey1 = TestHelper.getOneTimeKey(bobOneTimeKeys, 1);
// create alice inbound session for bob
assertTrue(0==aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey1));
try {
aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey1);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
String aliceClearMsg = "hello helooo to bob!";
OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(aliceClearMsg);
OlmMessage encryptedAliceToBobMsg1 = null;
try {
encryptedAliceToBobMsg1 = aliceSession.encryptMessage(aliceClearMsg);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertFalse(bobSession.matchesInboundSession(encryptedAliceToBobMsg1.mCipherText));
// init bob session with alice PRE KEY
......@@ -571,10 +672,20 @@ public class OlmSessionTest {
assertTrue(0!=aliceSession.getOlmSessionId());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob
assertNotNull(aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey));
try {
aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
String helloClearMsg = "Hello I'm Alice!";
OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg);
OlmMessage encryptedAliceToBobMsg1 = null;
try {
encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(encryptedAliceToBobMsg1);
assertNotNull(encryptedAliceToBobMsg1.mCipherText);
......@@ -595,7 +706,14 @@ public class OlmSessionTest {
}
// DECRYPT MESSAGE FROM ALICE
String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1);
String decryptedMsg01 = null;
try {
decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(decryptedMsg01);
// MESSAGE COMPARISON: decrypted vs encrypted
......@@ -607,11 +725,28 @@ public class OlmSessionTest {
String clearMsg3 = "Let's go to the opera.";
// bob encrypts messages
OlmMessage encryptedMsg1 = bobSession.encryptMessage(clearMsg1);
OlmMessage encryptedMsg1 = null;
try {
encryptedMsg1 = bobSession.encryptMessage(clearMsg1);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(encryptedMsg1);
OlmMessage encryptedMsg2 = bobSession.encryptMessage(clearMsg2);
OlmMessage encryptedMsg2 = null;
try {
encryptedMsg2 = bobSession.encryptMessage(clearMsg2);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(encryptedMsg2);
OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
OlmMessage encryptedMsg3 = null;
try {
encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(encryptedMsg3);
// serialize alice session
......@@ -742,26 +877,70 @@ public class OlmSessionTest {
}
// SANITY CHECK TESTS FOR: initOutboundSessionWithAccount()
assertTrue(-1==aliceSession.initOutboundSession(null, bobIdentityKey, bobOneTimeKey));
assertTrue(-1==aliceSession.initOutboundSession(aliceAccount, null, bobOneTimeKey));
assertTrue(-1==aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, null));
assertTrue(-1==aliceSession.initOutboundSession(null, null, null));
String errorMessage = null;
try {
aliceSession.initOutboundSession(null, bobIdentityKey, bobOneTimeKey);
} catch (Exception e) {
errorMessage = e.getMessage();
}
assertTrue(null != errorMessage);
errorMessage = null;
try {
aliceSession.initOutboundSession(aliceAccount, null, bobOneTimeKey);
} catch (Exception e) {
errorMessage = e.getMessage();
}
assertTrue(null != errorMessage);
errorMessage = null;
try {
aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, null);
} catch (Exception e) {
errorMessage = e.getMessage();
}
assertTrue(null != errorMessage);
errorMessage = null;
try {
aliceSession.initOutboundSession(null, null, null);
} catch (Exception e) {
errorMessage = e.getMessage();
}
assertTrue(null != errorMessage);
// init properly
assertTrue(0==aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey));
errorMessage = null;
try {
aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey);
} catch (Exception e) {
errorMessage = e.getMessage();
}
assertTrue(null == errorMessage);
// SANITY CHECK TESTS FOR: encryptMessage()
assertTrue(null==aliceSession.encryptMessage(null));
OlmMessage message = null;
try {
message = aliceSession.encryptMessage(null);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertTrue(null==message);
// encrypt properly
OlmMessage encryptedMsgToBob = aliceSession.encryptMessage("A message for bob");
OlmMessage encryptedMsgToBob = null;
try {
encryptedMsgToBob = aliceSession.encryptMessage("A message for bob");
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(encryptedMsgToBob);
// SANITY CHECK TESTS FOR: initInboundSessionWithAccount()
OlmSession bobSession = null;
try {
bobSession = new OlmSession();
String errorMessage = null;
errorMessage = null;
try {
bobSession.initInboundSession(null, encryptedMsgToBob.mCipherText);
} catch (Exception e) {
......@@ -802,7 +981,13 @@ public class OlmSessionTest {
}
// SANITY CHECK TESTS FOR: decryptMessage()
String decryptedMsg = aliceSession.decryptMessage(null);
String decryptedMsg = null;
try {
decryptedMsg = aliceSession.decryptMessage(null);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertTrue(null==decryptedMsg);
// SANITY CHECK TESTS FOR: matchesInboundSession()
......
......@@ -24,41 +24,40 @@ import java.io.IOException;
*/
public class OlmException extends IOException {
// exception codes
public static final int EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION = 0;
public static final int EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION = 1;
public static final int EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION = 2;
public static final int EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION = 3;
public static final int EXCEPTION_CODE_ACCOUNT_SERIALIZATION = 4;
public static final int EXCEPTION_CODE_ACCOUNT_DESERIALIZATION = 5;
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;
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;
public static final int EXCEPTION_CODE_ACCOUNT_IDENTITY_KEYS = 20;
public static final int EXCEPTION_CODE_ACCOUNT_GENERATE_ONE_TIME_KEYS = 21;
public static final int EXCEPTION_CODE_ACCOUNT_ONE_TIME_KEYS = 22;
public static final int EXCEPTION_CODE_ACCOUNT_REMOVE_ONE_TIME_KEYS = 23;
public static final int EXCEPTION_CODE_ACCOUNT_MARK_ONE_KEYS_AS_PUBLISHED = 24;
public static final int EXCEPTION_CODE_ACCOUNT_SIGN_MESSAGE = 25;
public static final int EXCEPTION_CODE_INIT_ACCOUNT_CREATION = 10;
public static final int EXCEPTION_CODE_SESSION_IDENTIFIER = 30;
public static final int EXCEPTION_CODE_SESSION_DECRYPT_SESSION = 31;
public static final int EXCEPTION_CODE_ACCOUNT_SERIALIZATION = 20;
public static final int EXCEPTION_CODE_ACCOUNT_DESERIALIZATION = 21;
public static final int EXCEPTION_CODE_ACCOUNT_IDENTITY_KEYS = 22;
public static final int EXCEPTION_CODE_ACCOUNT_GENERATE_ONE_TIME_KEYS = 23;
public static final int EXCEPTION_CODE_ACCOUNT_ONE_TIME_KEYS = 24;
public static final int EXCEPTION_CODE_ACCOUNT_REMOVE_ONE_TIME_KEYS = 25;
public static final int EXCEPTION_CODE_ACCOUNT_MARK_ONE_KEYS_AS_PUBLISHED = 26;
public static final int EXCEPTION_CODE_ACCOUNT_SIGN_MESSAGE = 27;
public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_IDENTIFIER = 40;
public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_KEY = 41;
public static final int EXCEPTION_CODE_OUTBOUND_GROUP_ENCRYPT_MESSAGE = 42;
public static final int EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION = 30;
public static final int EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION = 31;
public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_IDENTIFIER = 32;
public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_DECRYPT_SESSION = 33;
public static final int EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION = 40;
public static final int EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION = 41;
public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_IDENTIFIER = 42;
public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_KEY = 43;
public static final int EXCEPTION_CODE_OUTBOUND_GROUP_ENCRYPT_MESSAGE = 44;
public static final int EXCEPTION_CODE_INIT_SESSION_CREATION = 50;
public static final int EXCEPTION_CODE_SESSION_INIT_OUTBOUND_SESSION = 51;
public static final int EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION = 52;
public static final int EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION_FROM = 53;
public static final int EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE = 54;
public static final int EXCEPTION_CODE_SESSION_DECRYPT_MESSAGE = 55;
public static final int EXCEPTION_CODE_SESSION_SESSION_IDENTIFIER = 56;
// exception human readable messages
public static final String EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION = "createNewSession() failed";
public static final String EXCEPTION_MSG_NEW_INBOUND_GROUP_SESSION = "createNewSession() failed";
public static final String EXCEPTION_MSG_INIT_OUTBOUND_GROUP_SESSION = "initOutboundGroupSession() failed";
public static final String EXCEPTION_MSG_INIT_INBOUND_GROUP_SESSION = " initInboundGroupSessionWithSessionKey() failed";
public static final String EXCEPTION_MSG_INIT_NEW_ACCOUNT_DESERIALIZATION = "initNewAccount() failure";
public static final String EXCEPTION_MSG_INVALID_PARAMS_DESERIALIZATION = "invalid de-serialized parameters";
public static final String EXCEPTION_MSG_INIT_ACCOUNT_CREATION = "initNewAccount() failed";
public static final String EXCEPTION_MSG_INIT_SESSION_CREATION = "initNewSession() failed";
......
......@@ -141,7 +141,7 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
return new String(sessionIdentifierJni(), "UTF-8");
} catch (Exception e) {
Log.e(LOG_TAG, "## sessionIdentifier() failed " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_IDENTIFIER, e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_INBOUND_GROUP_SESSION_IDENTIFIER, e.getMessage());
}
}
......@@ -165,7 +165,7 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
}
} catch (Exception e) {
Log.e(LOG_TAG, "## decryptMessage() failed " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_DECRYPT_SESSION, e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_INBOUND_GROUP_SESSION_DECRYPT_SESSION, e.getMessage());
}
return result;
......
......@@ -124,25 +124,23 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
* @param aAccount the account to associate with this session
* @param aTheirIdentityKey the identity key of the recipient
* @param aTheirOneTimeKey the one time key of the recipient
* @return 0 if operation succeed, -1 otherwise
* @exception OlmException the failure reason
*/
public int initOutboundSession(OlmAccount aAccount, String aTheirIdentityKey, String aTheirOneTimeKey) {
int retCode=-1;
public void initOutboundSession(OlmAccount aAccount, String aTheirIdentityKey, String aTheirOneTimeKey) throws OlmException {
if ((null == aAccount) || TextUtils.isEmpty(aTheirIdentityKey) || TextUtils.isEmpty(aTheirOneTimeKey)) {
Log.e(LOG_TAG, "## initOutboundSession(): invalid input parameters");
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_INIT_OUTBOUND_SESSION, "invalid input parameters");
} else {
try {
retCode = initOutboundSessionJni(aAccount.getOlmAccountId(), aTheirIdentityKey.getBytes("UTF-8"), aTheirOneTimeKey.getBytes("UTF-8"));
initOutboundSessionJni(aAccount.getOlmAccountId(), aTheirIdentityKey.getBytes("UTF-8"), aTheirOneTimeKey.getBytes("UTF-8"));
} catch (Exception e) {
Log.e(LOG_TAG, "## initOutboundSession(): " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_INIT_OUTBOUND_SESSION, e.getMessage());
}
}
return retCode;
}
private native int initOutboundSessionJni(long aOlmAccountId, byte[] aTheirIdentityKey, byte[] aTheirOneTimeKey);
private native void initOutboundSessionJni(long aOlmAccountId, byte[] aTheirIdentityKey, byte[] aTheirOneTimeKey);
/**
* Create a new in-bound session for sending/receiving messages from an
......@@ -150,29 +148,23 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
* This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY).
* @param aAccount the account to associate with this session
* @param aPreKeyMsg PRE KEY message
* @exception Exception the failure reason
* @exception OlmException the failure reason
*/
public void initInboundSession(OlmAccount aAccount, String aPreKeyMsg) throws Exception {
public void initInboundSession(OlmAccount aAccount, String aPreKeyMsg) throws OlmException {
if ((null == aAccount) || TextUtils.isEmpty(aPreKeyMsg)){
Log.e(LOG_TAG, "## initInboundSession(): invalid input parameters");
throw new Exception("invalid input parameters");
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION, "invalid input parameters");
} else {
StringBuffer errorMsg = new StringBuffer();
try {
initInboundSessionJni(aAccount.getOlmAccountId(), aPreKeyMsg.getBytes("UTF-8"), errorMsg);
initInboundSessionJni(aAccount.getOlmAccountId(), aPreKeyMsg.getBytes("UTF-8"));
} catch (Exception e) {
Log.e(LOG_TAG, "## initInboundSession(): " + e.getMessage());
errorMsg.append(errorMsg);
}
if (errorMsg.length() != 0) {
throw new Exception(errorMsg.toString());
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION, e.getMessage());
}
}
}
private native int initInboundSessionJni(long aOlmAccountId, byte[] aOneTimeKeyMsg, StringBuffer aErrorMsg);
private native void initInboundSessionJni(long aOlmAccountId, byte[] aOneTimeKeyMsg);
/**
* Create a new in-bound session for sending/receiving messages from an
......@@ -183,38 +175,42 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
* @param aAccount the account to associate with this session
* @param aTheirIdentityKey the sender identity key
* @param aPreKeyMsg PRE KEY message
* @return 0 if operation succeed, -1 otherwise
* @exception OlmException the failure reason
*/
public int initInboundSessionFrom(OlmAccount aAccount, String aTheirIdentityKey, String aPreKeyMsg) {
int retCode=-1;
if((null==aAccount) || TextUtils.isEmpty(aPreKeyMsg)){
public void initInboundSessionFrom(OlmAccount aAccount, String aTheirIdentityKey, String aPreKeyMsg) throws OlmException {
if ( (null==aAccount) || TextUtils.isEmpty(aPreKeyMsg)){
Log.e(LOG_TAG, "## initInboundSessionFrom(): invalid input parameters");
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION_FROM, "invalid input parameters");
} else {
try {
retCode = initInboundSessionFromIdKeyJni(aAccount.getOlmAccountId(), aTheirIdentityKey.getBytes("UTF-8"), aPreKeyMsg.getBytes("UTF-8"));
initInboundSessionFromIdKeyJni(aAccount.getOlmAccountId(), aTheirIdentityKey.getBytes("UTF-8"), aPreKeyMsg.getBytes("UTF-8"));
} catch (Exception e) {
Log.e(LOG_TAG, "## initInboundSessionFrom(): " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION_FROM, e.getMessage());
}
}
return retCode;
}
private native int initInboundSessionFromIdKeyJni(long aOlmAccountId, byte[] aTheirIdentityKey, byte[] aOneTimeKeyMsg);
private native void initInboundSessionFromIdKeyJni(long aOlmAccountId, byte[] aTheirIdentityKey, byte[] aOneTimeKeyMsg);
/**
* Get the session identifier.<br> Will be the same for both ends of the
* conversation. The session identifier is returned as a String object.
* Session Id sample: "session_id":"M4fOVwD6AABrkTKl"
* Public API for {@link #getSessionIdentifierJni()}.
* @return the session ID as a String if operation succeed, null otherwise
* @return the session ID
* @exception OlmException the failure reason
*/
public String sessionIdentifier() {
public String sessionIdentifier() throws OlmException {
try {
return new String(getSessionIdentifierJni(), "UTF-8");
byte[] buffer = getSessionIdentifierJni();
if (null != buffer) {
return new String(buffer, "UTF-8");
}
} catch (Exception e) {
Log.e(LOG_TAG, "## sessionIdentifier(): " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_SESSION_IDENTIFIER, e.getMessage());
}
return null;
......@@ -272,39 +268,46 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
* The encrypted message is returned in a OlmMessage object.
* Public API for {@link #encryptMessageJni(byte[], OlmMessage)}.
* @param aClearMsg message to encrypted
* @return the encrypted message if operation succeed, null otherwise
* @return the encrypted message
* @exception OlmException the failure reason
*/
public OlmMessage encryptMessage(String aClearMsg) {
public OlmMessage encryptMessage(String aClearMsg) throws OlmException {
if (null == aClearMsg) {
return null;
}
OlmMessage encryptedMsgRetValue = new OlmMessage();
try {
if (0 != encryptMessageJni(aClearMsg.getBytes("UTF-8"), encryptedMsgRetValue)) {
encryptedMsgRetValue = null;
}
encryptMessageJni(aClearMsg.getBytes("UTF-8"), encryptedMsgRetValue);
} catch (Exception e) {
Log.e(LOG_TAG, "## encryptMessage(): failed " + e.getMessage());
encryptedMsgRetValue = null;
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE, e.getMessage());
}
return encryptedMsgRetValue;
}
private native int encryptMessageJni(byte[] aClearMsg, OlmMessage aEncryptedMsg);
private native void encryptMessageJni(byte[] aClearMsg, OlmMessage aEncryptedMsg);
/**
* Decrypt a message using the session.<br>
* The encrypted message is given as a OlmMessage object.
* @param aEncryptedMsg message to decrypt
* @return the decrypted message if operation succeed, null otherwise
* @return the decrypted message
* @exception OlmException the failure reason
*/
public String decryptMessage(OlmMessage aEncryptedMsg) {
public String decryptMessage(OlmMessage aEncryptedMsg) throws OlmException {
if (null == aEncryptedMsg) {
return null;
}
try {
return new String(decryptMessageJni(aEncryptedMsg), "UTF-8");
} catch (Exception e) {
Log.e(LOG_TAG, "## decryptMessage(): failed " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_DECRYPT_MESSAGE, e.getMessage());
}
return null;
}
private native byte[] decryptMessageJni(OlmMessage aEncryptedMsg);
......@@ -353,7 +356,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
} else {
aErrorMsg.setLength(0);
try {
pickleRetValue = serializeJni(aKey.getBytes("UTF-8"), aErrorMsg);
pickleRetValue = serializeJni(aKey.getBytes("UTF-8"));
} catch (Exception e) {
Log.e(LOG_TAG,"## serializeDataWithKey(): failed " + e.getMessage());
aErrorMsg.append(e.getMessage());
......@@ -362,7 +365,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
return pickleRetValue;
}
private native String serializeJni(byte[] aKey, StringBuffer aErrorMsg);
private native String serializeJni(byte[] aKey);
/**
* Loads an account from a pickled base64 string.<br>
......
......@@ -33,24 +33,24 @@ JNIEXPORT jlong OLM_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thi
JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz);
// outbound session
JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKey, jbyteArray aTheirOneTimeKey);
JNIEXPORT void OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKey, jbyteArray aTheirOneTimeKey);
// inbound sessions: establishment based on PRE KEY message
JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aOneTimeKeyMsg, jobject aErrorMsg);
JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKey, jbyteArray aOneTimeKeyMsg);
JNIEXPORT void OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aOneTimeKeyMsg);
JNIEXPORT void OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKey, jbyteArray aOneTimeKeyMsg);
// match inbound sessions: based on PRE KEY message
JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobject thiz, jbyteArray aOneTimeKeyMsg);
JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aTheirIdentityKey, jbyteArray aOneTimeKeyMsg);
// encrypt/decrypt
JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsg, jobject aEncryptedMsg);
JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsg, jobject aEncryptedMsg);
JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg);
JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz);
// serialization
JNIEXPORT jstring OLM_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKey, jobject aErrorMsg);
JNIEXPORT jstring OLM_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKey);
JNIEXPORT jstring OLM_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedData, jbyteArray aKey);
#ifdef __cplusplus
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment