initInboundSessionWithAccount triggers an exception when it fails.

This commit is contained in:
ylecollen 2017-01-03 17:20:18 +01:00
parent 45a98c20a8
commit 2070de4fc5
4 changed files with 102 additions and 26 deletions

View file

@ -19,6 +19,7 @@ package org.matrix.olm;
import android.content.Context; import android.content.Context;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import org.json.JSONObject; import org.json.JSONObject;
@ -126,7 +127,13 @@ public class OlmSessionTest {
assertTrue("Exception Msg="+e.getMessage(), false); assertTrue("Exception Msg="+e.getMessage(), false);
} }
assertTrue(0!=bobSession.getOlmSessionId()); assertTrue(0!=bobSession.getOlmSessionId());
assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText));
try {
bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText);
} catch (Exception e) {
assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false);
}
String decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob); String decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob);
assertNotNull(decryptedMsg); assertNotNull(decryptedMsg);
@ -218,8 +225,14 @@ public class OlmSessionTest {
} catch (OlmException e) { } catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false); assertTrue("Exception Msg="+e.getMessage(), false);
} }
assertTrue(0!=bobSession.getOlmSessionId()); assertTrue(0!=bobSession.getOlmSessionId());
assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));
try {
bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText);
} catch (Exception e) {
assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false);
}
// DECRYPT MESSAGE FROM ALICE // DECRYPT MESSAGE FROM ALICE
String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1); String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1);
@ -375,7 +388,11 @@ public class OlmSessionTest {
assertFalse(bobSession.matchesInboundSession(encryptedAliceToBobMsg1.mCipherText)); assertFalse(bobSession.matchesInboundSession(encryptedAliceToBobMsg1.mCipherText));
// init bob session with alice PRE KEY // init bob session with alice PRE KEY
assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText)); try {
bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText);
} catch (Exception e) {
assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false);
}
// test matchesInboundSession() and matchesInboundSessionFrom() // test matchesInboundSession() and matchesInboundSessionFrom()
assertTrue(bobSession.matchesInboundSession(encryptedAliceToBobMsg1.mCipherText)); assertTrue(bobSession.matchesInboundSession(encryptedAliceToBobMsg1.mCipherText));
@ -461,7 +478,13 @@ public class OlmSessionTest {
assertTrue("Exception Msg="+e.getMessage(), false); assertTrue("Exception Msg="+e.getMessage(), false);
} }
assertTrue(0!=bobSession.getOlmSessionId()); assertTrue(0!=bobSession.getOlmSessionId());
assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));
// init bob session with alice PRE KEY
try {
bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText);
} catch (Exception e) {
assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false);
}
// DECRYPT MESSAGE FROM ALICE // DECRYPT MESSAGE FROM ALICE
String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1); String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1);
@ -604,11 +627,42 @@ public class OlmSessionTest {
OlmSession bobSession = null; OlmSession bobSession = null;
try { try {
bobSession = new OlmSession(); bobSession = new OlmSession();
assertTrue(-1==bobSession.initInboundSessionWithAccount(null, encryptedMsgToBob.mCipherText)); String errorMessage = null;
assertTrue(-1==bobSession.initInboundSessionWithAccount(bobAccount, null)); try {
assertTrue(-1==bobSession.initInboundSessionWithAccount(bobAccount, INVALID_PRE_KEY)); bobSession.initInboundSessionWithAccount(null, encryptedMsgToBob.mCipherText);
} catch (Exception e) {
errorMessage = e.getMessage();
}
assertTrue(!TextUtils.isEmpty(errorMessage));
errorMessage = null;
try {
bobSession.initInboundSessionWithAccount(bobAccount, null);
} catch (Exception e) {
errorMessage = e.getMessage();
}
assertTrue(!TextUtils.isEmpty(errorMessage));
errorMessage = null;
try {
bobSession.initInboundSessionWithAccount(bobAccount, INVALID_PRE_KEY);
} catch (Exception e) {
errorMessage = e.getMessage();
}
assertTrue(!TextUtils.isEmpty(errorMessage));
// init properly // init properly
assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText)); errorMessage = null;
try {
bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText);
} catch (Exception e) {
errorMessage = e.getMessage();
}
assertTrue(TextUtils.isEmpty(errorMessage));
} catch (OlmException e) { } catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false); assertTrue("Exception Msg="+e.getMessage(), false);
} }

View file

@ -150,7 +150,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
* Getter on the session ID. * Getter on the session ID.
* @return native session ID * @return native session ID
*/ */
public long getOlmSessionId(){ long getOlmSessionId(){
return mNativeId; return mNativeId;
} }
@ -209,11 +209,9 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
*/ */
private native long createNewSessionJni(); private native long createNewSessionJni();
/** /**
* Creates a new out-bound session for sending messages to a recipient * Creates a new out-bound session for sending messages to a recipient
* identified by an identity key and a one time key.<br> * identified by an identity key and a one time key.<br>
* Public API for {@link #initOutboundSessionWithAccount(OlmAccount, String, String)}.
* @param aAccount the account to associate with this session * @param aAccount the account to associate with this session
* @param aTheirIdentityKey the identity key of the recipient * @param aTheirIdentityKey the identity key of the recipient
* @param aTheirOneTimeKey the one time key of the recipient * @param aTheirOneTimeKey the one time key of the recipient
@ -222,7 +220,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
public int initOutboundSessionWithAccount(OlmAccount aAccount, String aTheirIdentityKey, String aTheirOneTimeKey) { public int initOutboundSessionWithAccount(OlmAccount aAccount, String aTheirIdentityKey, String aTheirOneTimeKey) {
int retCode=-1; int retCode=-1;
if((null==aAccount) || TextUtils.isEmpty(aTheirIdentityKey) || TextUtils.isEmpty(aTheirOneTimeKey)){ if ((null == aAccount) || TextUtils.isEmpty(aTheirIdentityKey) || TextUtils.isEmpty(aTheirOneTimeKey)) {
Log.e(LOG_TAG, "## initOutboundSession(): invalid input parameters"); Log.e(LOG_TAG, "## initOutboundSession(): invalid input parameters");
} else { } else {
try { try {
@ -240,30 +238,32 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
/** /**
* Create a new in-bound session for sending/receiving messages from an * Create a new in-bound session for sending/receiving messages from an
* incoming PRE_KEY message ({@link OlmMessage#MESSAGE_TYPE_PRE_KEY}).<br> * incoming PRE_KEY message ({@link OlmMessage#MESSAGE_TYPE_PRE_KEY}).<br>
* Public API for {@link #initInboundSessionJni(long, byte[])}.
* This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY). * 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 aAccount the account to associate with this session
* @param aPreKeyMsg PRE KEY message * @param aPreKeyMsg PRE KEY message
* @return 0 if operation succeed, -1 otherwise * @exception Exception the failure reason
*/ */
public int initInboundSessionWithAccount(OlmAccount aAccount, String aPreKeyMsg) { public void initInboundSessionWithAccount(OlmAccount aAccount, String aPreKeyMsg) throws Exception {
int retCode = -1;
if ((null == aAccount) || TextUtils.isEmpty(aPreKeyMsg)){ if ((null == aAccount) || TextUtils.isEmpty(aPreKeyMsg)){
Log.e(LOG_TAG, "## initInboundSessionWithAccount(): invalid input parameters"); Log.e(LOG_TAG, "## initInboundSessionWithAccount(): invalid input parameters");
throw new Exception("invalid input parameters");
} else { } else {
StringBuffer errorMsg = new StringBuffer();
try { try {
retCode = initInboundSessionJni(aAccount.getOlmAccountId(), aPreKeyMsg.getBytes("UTF-8")); initInboundSessionJni(aAccount.getOlmAccountId(), aPreKeyMsg.getBytes("UTF-8"), errorMsg);
} catch (Exception e) { } catch (Exception e) {
Log.e(LOG_TAG, "## initInboundSessionWithAccount(): " + e.getMessage()); Log.e(LOG_TAG, "## initInboundSessionWithAccount(): " + e.getMessage());
errorMsg.append(errorMsg);
}
if (errorMsg.length() != 0) {
throw new Exception(errorMsg.toString());
}
} }
} }
return retCode; private native int initInboundSessionJni(long aOlmAccountId, byte[] aOneTimeKeyMsg, StringBuffer aErrorMsg);
}
private native int initInboundSessionJni(long aOlmAccountId, byte[] aOneTimeKeyMsg);
/** /**
* Create a new in-bound session for sending/receiving messages from an * Create a new in-bound session for sending/receiving messages from an

View file

@ -205,12 +205,14 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject
* @param aOneTimeKeyMsg PRE_KEY message * @param aOneTimeKeyMsg PRE_KEY message
* @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise * @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise
*/ */
JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aOneTimeKeyMsgBuffer) JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aOneTimeKeyMsgBuffer, jobject aErrorMsg)
{ {
jint retCode = ERROR_CODE_KO; jint retCode = ERROR_CODE_KO;
OlmSession *sessionPtr = NULL; OlmSession *sessionPtr = NULL;
OlmAccount *accountPtr = NULL; OlmAccount *accountPtr = NULL;
size_t sessionResult; size_t sessionResult;
jclass errorMsgJClass = 0;
jmethodID errorMsgMethodId = 0;
if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
@ -224,6 +226,18 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject
{ {
LOGE("## initInboundSessionJni(): failure - invalid message"); LOGE("## initInboundSessionJni(): failure - invalid message");
} }
else if (!aErrorMsg)
{
LOGE(" ## initInboundSessionJni(): failure - invalid error output");
}
else if (!(errorMsgJClass = env->GetObjectClass(aErrorMsg)))
{
LOGE(" ## initInboundSessionJni(): failure - unable to get error class");
}
else if (!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;")))
{
LOGE(" ## initInboundSessionJni(): failure - unable to get error method ID");
}
else else
{ {
jbyte* messagePtr = env->GetByteArrayElements(aOneTimeKeyMsgBuffer, 0); jbyte* messagePtr = env->GetByteArrayElements(aOneTimeKeyMsgBuffer, 0);
@ -241,7 +255,15 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject
if (sessionResult == olm_error()) if (sessionResult == olm_error())
{ {
LOGE("## initInboundSessionJni(): failure - init inbound session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr)); const char *errorMsgPtr = olm_session_last_error(sessionPtr);
LOGE("## initInboundSessionJni(): failure - init inbound session creation Msg=%s", errorMsgPtr);
jstring errorJstring = env->NewStringUTF(errorMsgPtr);
if (errorJstring)
{
env->CallObjectMethod(aErrorMsg, errorMsgMethodId, errorJstring);
}
} }
else else
{ {
@ -818,7 +840,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, job
const char *errorMsgPtr = olm_session_last_error(sessionPtr); const char *errorMsgPtr = olm_session_last_error(sessionPtr);
LOGE(" ## serializeDataWithKeyJni(): failure - olm_pickle_session() Msg=%s",errorMsgPtr); LOGE(" ## serializeDataWithKeyJni(): failure - olm_pickle_session() Msg=%s",errorMsgPtr);
if(0 != (errorJstring = env->NewStringUTF(errorMsgPtr))) if ((errorJstring = env->NewStringUTF(errorMsgPtr)))
{ {
env->CallObjectMethod(aErrorMsg, errorMsgMethodId, errorJstring); env->CallObjectMethod(aErrorMsg, errorMsgMethodId, errorJstring);
} }

View file

@ -36,7 +36,7 @@ JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject t
JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKey, jbyteArray aTheirOneTimeKey); JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKey, jbyteArray aTheirOneTimeKey);
// inbound sessions: establishment based on PRE KEY message // inbound sessions: establishment based on PRE KEY message
JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aOneTimeKeyMsg); 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 jint OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKey, jbyteArray aOneTimeKeyMsg);
// match inbound sessions: based on PRE KEY message // match inbound sessions: based on PRE KEY message