the OlmInboundGroupSession methods trigger an exception when they fails

This commit is contained in:
ylecollen 2017-01-04 12:35:15 +01:00
parent 88548f687e
commit 261c08086c
6 changed files with 91 additions and 118 deletions

View file

@ -138,7 +138,13 @@ public class OlmGroupSessionTest {
@Test @Test
public void test08GetInboundGroupSessionIdentifier() { public void test08GetInboundGroupSessionIdentifier() {
// check both session identifiers are equals // check both session identifiers are equals
mBobSessionIdentifier = mBobInboundGroupSession.sessionIdentifier(); mBobSessionIdentifier = null;
try {
mBobSessionIdentifier = mBobInboundGroupSession.sessionIdentifier();
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertFalse(TextUtils.isEmpty(mBobSessionIdentifier)); assertFalse(TextUtils.isEmpty(mBobSessionIdentifier));
} }
@ -479,7 +485,7 @@ public class OlmGroupSessionTest {
String exceptionMessage = null; String exceptionMessage = null;
try { try {
bobInboundGroupSession.decryptMessage(corruptedEncryptedMsg); bobInboundGroupSession.decryptMessage(corruptedEncryptedMsg);
} catch (OlmInboundGroupSession.DecryptMessageException e) { } catch (OlmException e) {
exceptionMessage = e.getMessage(); exceptionMessage = e.getMessage();
} }

View file

@ -307,6 +307,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
* The signed message is returned by the method. * The signed message is returned by the method.
* @param aMessage message to sign * @param aMessage message to sign
* @return the signed message * @return the signed message
* @exception OlmException the failure reason
*/ */
public String signMessage(String aMessage) throws OlmException { public String signMessage(String aMessage) throws OlmException {
String result = null; String result = null;

View file

@ -46,6 +46,10 @@ public class OlmException extends IOException {
public static final int EXCEPTION_CODE_ACCOUNT_MARK_ONE_KEYS_AS_PUBLISHED = 24; 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_ACCOUNT_SIGN_MESSAGE = 25;
public static final int EXCEPTION_CODE_SESSION_IDENTIFIER = 30;
public static final int EXCEPTION_CODE_SESSION_DECRYPT_SESSION = 31;
// exception human readable messages // exception human readable messages
public static final String EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION = "createNewSession() failed"; 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_NEW_INBOUND_GROUP_SESSION = "createNewSession() failed";

View file

@ -52,15 +52,6 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
public long mIndex; public long mIndex;
} }
/**
* Exception triggered in {@link #decryptMessage(String)}
*/
static public class DecryptMessageException extends Exception {
public DecryptMessageException(String message) {
super(message);
}
}
/** /**
* Constructor.<br> * Constructor.<br>
* Create and save a new native session instance ID and start a new inbound group session. * Create and save a new native session instance ID and start a new inbound group session.
@ -71,10 +62,7 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
*/ */
public OlmInboundGroupSession(String aSessionKey) throws OlmException { public OlmInboundGroupSession(String aSessionKey) throws OlmException {
if(createNewSession()) { if(createNewSession()) {
if (0 != initInboundGroupSession(aSessionKey)) { initInboundGroupSession(aSessionKey);
releaseSession();// prevent memory leak before throwing
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION,OlmException.EXCEPTION_MSG_INIT_INBOUND_GROUP_SESSION);
}
} else { } else {
throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION, OlmException.EXCEPTION_MSG_NEW_INBOUND_GROUP_SESSION); throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION, OlmException.EXCEPTION_MSG_NEW_INBOUND_GROUP_SESSION);
} }
@ -127,38 +115,34 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
* The session key parameter is retrieved from an outbound group session * The session key parameter is retrieved from an outbound group session
* see {@link OlmOutboundGroupSession#sessionKey()} * see {@link OlmOutboundGroupSession#sessionKey()}
* @param aSessionKey session key * @param aSessionKey session key
* @return 0 if operation succeed, -1 otherwise * @exception OlmException the failure reason
*/ */
private int initInboundGroupSession(String aSessionKey) { private void initInboundGroupSession(String aSessionKey) throws OlmException {
int retCode = -1; if (TextUtils.isEmpty(aSessionKey)) {
if(TextUtils.isEmpty(aSessionKey)){
Log.e(LOG_TAG, "## initInboundGroupSession(): invalid session key"); Log.e(LOG_TAG, "## initInboundGroupSession(): invalid session key");
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION, "invalid session key");
} else { } else {
try { try {
retCode = initInboundGroupSessionJni(aSessionKey.getBytes("UTF-8")); initInboundGroupSessionJni(aSessionKey.getBytes("UTF-8"));
} catch (Exception e) { } catch (Exception e) {
Log.e(LOG_TAG, "## initInboundGroupSession() failed " + e.getMessage()); throw new OlmException(OlmException.EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION, e.getMessage());
} }
} }
return retCode;
} }
private native int initInboundGroupSessionJni(byte[] aSessionKeyBuffer); private native void initInboundGroupSessionJni(byte[] aSessionKeyBuffer);
/** /**
* Retrieve the base64-encoded identifier for this inbound group session. * Retrieve the base64-encoded identifier for this inbound group session.
* @return the session ID if operation succeed, null otherwise * @return the session ID
*/ */
public String sessionIdentifier() { public String sessionIdentifier() throws OlmException {
try { try {
return new String(sessionIdentifierJni(), "UTF-8"); return new String(sessionIdentifierJni(), "UTF-8");
} catch (Exception e) { } catch (Exception e) {
Log.e(LOG_TAG, "## sessionIdentifier() failed " + e.getMessage()); Log.e(LOG_TAG, "## sessionIdentifier() failed " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_IDENTIFIER, e.getMessage());
} }
return null;
} }
private native byte[] sessionIdentifierJni(); private native byte[] sessionIdentifierJni();
@ -168,32 +152,26 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
* In case of error, null is returned and an error message description is provided in aErrorMsg. * In case of error, null is returned and an error message description is provided in aErrorMsg.
* @param aEncryptedMsg the message to be decrypted * @param aEncryptedMsg the message to be decrypted
* @return the decrypted message information * @return the decrypted message information
* @exception DecryptMessageException if there is an error while * @exception OlmException teh failure reason
*/ */
public DecryptMessageResult decryptMessage(String aEncryptedMsg) throws DecryptMessageException { public DecryptMessageResult decryptMessage(String aEncryptedMsg) throws OlmException {
DecryptMessageResult result = new DecryptMessageResult(); DecryptMessageResult result = new DecryptMessageResult();
StringBuffer errorMsg = new StringBuffer();
try { try {
byte[] decryptedMessageBuffer = decryptMessageJni(aEncryptedMsg.getBytes("UTF-8"), result, errorMsg); byte[] decryptedMessageBuffer = decryptMessageJni(aEncryptedMsg.getBytes("UTF-8"), result);
if (null != decryptedMessageBuffer) { if (null != decryptedMessageBuffer) {
result.mDecryptedMessage = new String(decryptedMessageBuffer, "UTF-8"); result.mDecryptedMessage = new String(decryptedMessageBuffer, "UTF-8");
} }
} catch (Exception e) { } catch (Exception e) {
Log.e(LOG_TAG, "## decryptMessage() failed " + e.getMessage()); Log.e(LOG_TAG, "## decryptMessage() failed " + e.getMessage());
errorMsg.append(e.getMessage()); throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_DECRYPT_SESSION, e.getMessage());
}
// check if there is an error while decrypting
if (0 != errorMsg.length()) {
throw new DecryptMessageException(errorMsg.toString());
} }
return result; return result;
} }
private native byte[] decryptMessageJni(byte[] aEncryptedMsg, DecryptMessageResult aDecryptMessageResult, StringBuffer aErrorMsg); private native byte[] decryptMessageJni(byte[] aEncryptedMsg, DecryptMessageResult aDecryptMessageResult);
//============================================================================================================== //==============================================================================================================
// Serialization management // Serialization management
@ -231,15 +209,17 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
protected String serialize(String aKey, StringBuffer aErrorMsg) { protected String serialize(String aKey, StringBuffer aErrorMsg) {
String pickleRetValue = null; String pickleRetValue = null;
aErrorMsg.setLength(0);
// sanity check // sanity check
if(null == aErrorMsg) { if(null == aErrorMsg) {
Log.e(LOG_TAG,"## serialize(): invalid parameter - aErrorMsg=null"); Log.e(LOG_TAG,"## serialize(): invalid parameter - aErrorMsg=null");
aErrorMsg.append("aErrorMsg=null");
} else if(TextUtils.isEmpty(aKey)) { } else if(TextUtils.isEmpty(aKey)) {
aErrorMsg.append("Invalid input parameters in serialize()"); aErrorMsg.append("Invalid input parameters in serialize()");
} else { } else {
aErrorMsg.setLength(0);
try { try {
pickleRetValue = new String(serializeJni(aKey.getBytes("UTF-8"), aErrorMsg), "UTF-8"); pickleRetValue = new String(serializeJni(aKey.getBytes("UTF-8")), "UTF-8");
} catch (Exception e) { } catch (Exception e) {
Log.e(LOG_TAG, "## serialize() failed " + e.getMessage()); Log.e(LOG_TAG, "## serialize() failed " + e.getMessage());
aErrorMsg.append(e.getMessage()); aErrorMsg.append(e.getMessage());
@ -251,10 +231,9 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
/** /**
* JNI counter part of {@link #serialize(String, StringBuffer)}. * JNI counter part of {@link #serialize(String, StringBuffer)}.
* @param aKey encryption key * @param aKey encryption key
* @param aErrorMsg error message description
* @return pickled base64 string if operation succeed, null otherwise * @return pickled base64 string if operation succeed, null otherwise
*/ */
private native byte[] serializeJni(byte[] aKey, StringBuffer aErrorMsg); private native byte[] serializeJni(byte[] aKey);
/** /**
* Loads an account from a pickled base64 string.<br> * Loads an account from a pickled base64 string.<br>

View file

@ -36,7 +36,7 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env
} }
else else
{ {
LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr); LOGD(" ## releaseSessionJni(): sessionPtr=%p", sessionPtr);
#ifdef ENABLE_JNI_LOG #ifdef ENABLE_JNI_LOG
size_t retCode = olm_clear_inbound_group_session(sessionPtr); size_t retCode = olm_clear_inbound_group_session(sessionPtr);
LOGD(" ## releaseSessionJni(): clear_inbound_group_session=%lu",static_cast<long unsigned int>(retCode)); LOGD(" ## releaseSessionJni(): clear_inbound_group_session=%lu",static_cast<long unsigned int>(retCode));
@ -84,11 +84,10 @@ JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *
/** /**
* Create a new in-bound session.<br> * Create a new in-bound session.<br>
* @param aSessionKey session key from an outbound session * @param aSessionKey session key from an outbound session
* @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise
*/ */
JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionJni)(JNIEnv *env, jobject thiz, jbyteArray aSessionKeyBuffer) JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionJni)(JNIEnv *env, jobject thiz, jbyteArray aSessionKeyBuffer)
{ {
jint retCode = ERROR_CODE_KO; const char* errorMessage = NULL;
OlmInboundGroupSession *sessionPtr = NULL; OlmInboundGroupSession *sessionPtr = NULL;
jbyte* sessionKeyPtr = NULL; jbyte* sessionKeyPtr = NULL;
size_t sessionResult; size_t sessionResult;
@ -98,14 +97,17 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionJni)(JN
if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE(" ## initInboundGroupSessionJni(): failure - invalid inbound group session instance"); LOGE(" ## initInboundGroupSessionJni(): failure - invalid inbound group session instance");
errorMessage = "invalid inbound group session instance";
} }
else if (!aSessionKeyBuffer) else if (!aSessionKeyBuffer)
{ {
LOGE(" ## initInboundGroupSessionJni(): failure - invalid aSessionKey"); LOGE(" ## initInboundGroupSessionJni(): failure - invalid aSessionKey");
errorMessage = "invalid aSessionKey";
} }
else if (!(sessionKeyPtr = env->GetByteArrayElements(aSessionKeyBuffer, 0))) else if (!(sessionKeyPtr = env->GetByteArrayElements(aSessionKeyBuffer, 0)))
{ {
LOGE(" ## initInboundGroupSessionJni(): failure - session key JNI allocation OOM"); LOGE(" ## initInboundGroupSessionJni(): failure - session key JNI allocation OOM");
errorMessage = "Session key JNI allocation OOM";
} }
else else
{ {
@ -114,12 +116,11 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionJni)(JN
sessionResult = olm_init_inbound_group_session(sessionPtr, (const uint8_t*)sessionKeyPtr, sessionKeyLength); sessionResult = olm_init_inbound_group_session(sessionPtr, (const uint8_t*)sessionKeyPtr, sessionKeyLength);
if (sessionResult == olm_error()) { if (sessionResult == olm_error()) {
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); errorMessage = olm_inbound_group_session_last_error(sessionPtr);
LOGE(" ## initInboundGroupSessionJni(): failure - init inbound session creation Msg=%s",errorMsgPtr); LOGE(" ## initInboundGroupSessionJni(): failure - init inbound session creation Msg=%s", errorMessage);
} }
else else
{ {
retCode = ERROR_CODE_OK;
LOGD(" ## initInboundGroupSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult)); LOGD(" ## initInboundGroupSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult));
} }
} }
@ -130,15 +131,18 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionJni)(JN
env->ReleaseByteArrayElements(aSessionKeyBuffer, sessionKeyPtr, JNI_ABORT); env->ReleaseByteArrayElements(aSessionKeyBuffer, sessionKeyPtr, JNI_ABORT);
} }
return retCode; if (errorMessage)
{
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
}
} }
/** /**
* Get a base64-encoded identifier for this inbound group session. * Get a base64-encoded identifier for this inbound group session.
*/ */
JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz)
{ {
const char* errorMessage = NULL;
OlmInboundGroupSession *sessionPtr = NULL; OlmInboundGroupSession *sessionPtr = NULL;
jbyteArray returnValue = 0; jbyteArray returnValue = 0;
@ -147,6 +151,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JN
if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE(" ## sessionIdentifierJni(): failure - invalid inbound group session instance"); LOGE(" ## sessionIdentifierJni(): failure - invalid inbound group session instance");
errorMessage = "invalid inbound group session instance";
} }
else else
{ {
@ -158,7 +163,8 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JN
if (!sessionIdPtr) if (!sessionIdPtr)
{ {
LOGE(" ## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM"); LOGE(" ## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM");
errorMessage = "inbound group session identifier allocation OOM";
} }
else else
{ {
@ -166,11 +172,11 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JN
if (result == olm_error()) if (result == olm_error())
{ {
errorMessage = (const char *)olm_inbound_group_session_last_error(sessionPtr);
LOGE(" ## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr)); LOGE(" ## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr));
} }
else else
{ {
sessionIdPtr[result] = static_cast<char>('\0'); sessionIdPtr[result] = static_cast<char>('\0');
LOGD(" ## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%s",static_cast<long unsigned int>(result), (char*)sessionIdPtr); LOGD(" ## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%s",static_cast<long unsigned int>(result), (char*)sessionIdPtr);
@ -182,59 +188,56 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JN
} }
} }
if (errorMessage)
{
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
}
return returnValue; return returnValue;
} }
JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aEncryptedMsgBuffer, jobject aDecryptionResult, jobject aErrorMsg) JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aEncryptedMsgBuffer, jobject aDecryptionResult)
{ {
jbyteArray decryptedMsgBuffer = 0; jbyteArray decryptedMsgBuffer = 0;
const char* errorMessage = NULL;
OlmInboundGroupSession *sessionPtr = NULL; OlmInboundGroupSession *sessionPtr = NULL;
jbyte *encryptedMsgPtr = NULL; jbyte *encryptedMsgPtr = NULL;
jclass indexObjJClass = 0; jclass indexObjJClass = 0;
jfieldID indexMsgFieldId; jfieldID indexMsgFieldId;
jclass errorMsgJClass = 0;
jmethodID errorMsgMethodId = 0;
const char *errorMsgPtr = NULL;
LOGD("## decryptMessageJni(): inbound group session IN"); LOGD("## decryptMessageJni(): inbound group session IN");
if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE(" ## decryptMessageJni(): failure - invalid inbound group session ptr=NULL"); LOGE(" ## decryptMessageJni(): failure - invalid inbound group session ptr=NULL");
errorMessage = "invalid inbound group session ptr=NULL";
} }
else if (!aEncryptedMsgBuffer) else if (!aEncryptedMsgBuffer)
{ {
LOGE(" ## decryptMessageJni(): failure - invalid encrypted message"); LOGE(" ## decryptMessageJni(): failure - invalid encrypted message");
errorMessage = "invalid encrypted message";
} }
else if (!aDecryptionResult) else if (!aDecryptionResult)
{ {
LOGE(" ## decryptMessageJni(): failure - invalid index object"); LOGE(" ## decryptMessageJni(): failure - invalid index object");
} errorMessage = "invalid index object";
else if (!aErrorMsg)
{
LOGE(" ## decryptMessageJni(): failure - invalid error object");
}
else if (!(errorMsgJClass = env->GetObjectClass(aErrorMsg)))
{
LOGE(" ## decryptMessageJni(): failure - unable to get error class");
}
else if (!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;")))
{
LOGE(" ## decryptMessageJni(): failure - unable to get error method ID");
} }
else if (!(encryptedMsgPtr = env->GetByteArrayElements(aEncryptedMsgBuffer, 0))) else if (!(encryptedMsgPtr = env->GetByteArrayElements(aEncryptedMsgBuffer, 0)))
{ {
LOGE(" ## decryptMessageJni(): failure - encrypted message JNI allocation OOM"); LOGE(" ## decryptMessageJni(): failure - encrypted message JNI allocation OOM");
errorMessage = "encrypted message JNI allocation OOM";
} }
else if (!(indexObjJClass = env->GetObjectClass(aDecryptionResult))) else if (!(indexObjJClass = env->GetObjectClass(aDecryptionResult)))
{ {
LOGE("## decryptMessageJni(): failure - unable to get index class"); LOGE("## decryptMessageJni(): failure - unable to get index class");
errorMessage = "unable to get index class";
} }
else if (!(indexMsgFieldId = env->GetFieldID(indexObjJClass,"mIndex","J"))) else if (!(indexMsgFieldId = env->GetFieldID(indexObjJClass,"mIndex","J")))
{ {
LOGE("## decryptMessageJni(): failure - unable to get index type field"); LOGE("## decryptMessageJni(): failure - unable to get index type field");
errorMessage = "unable to get index type field";
} }
else else
{ {
@ -246,6 +249,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEn
if (!tempEncryptedPtr) if (!tempEncryptedPtr)
{ {
LOGE(" ## decryptMessageJni(): failure - tempEncryptedPtr allocation OOM"); LOGE(" ## decryptMessageJni(): failure - tempEncryptedPtr allocation OOM");
errorMessage = "tempEncryptedPtr allocation OOM";
} }
else else
{ {
@ -258,15 +262,8 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEn
encryptedMsgLength); encryptedMsgLength);
if (maxPlainTextLength == olm_error()) if (maxPlainTextLength == olm_error())
{ {
errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); errorMessage = olm_inbound_group_session_last_error(sessionPtr);
LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s",errorMsgPtr); LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s", errorMessage);
jstring errorJstring = env->NewStringUTF(errorMsgPtr);
if (errorJstring)
{
env->CallObjectMethod(aErrorMsg, errorMsgMethodId, errorJstring);
}
} }
else else
{ {
@ -287,15 +284,8 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEn
&messageIndex); &messageIndex);
if (plaintextLength == olm_error()) if (plaintextLength == olm_error())
{ {
errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); errorMessage = olm_inbound_group_session_last_error(sessionPtr);
LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt Msg=%s",errorMsgPtr); LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt Msg=%s", errorMessage);
jstring errorJstring = env->NewStringUTF(errorMsgPtr);
if (errorJstring)
{
env->CallObjectMethod(aErrorMsg, errorMsgMethodId, errorJstring);
}
} }
else else
{ {
@ -327,6 +317,11 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEn
env->ReleaseByteArrayElements(aEncryptedMsgBuffer, encryptedMsgPtr, JNI_ABORT); env->ReleaseByteArrayElements(aEncryptedMsgBuffer, encryptedMsgPtr, JNI_ABORT);
} }
if (errorMessage)
{
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
}
return decryptedMsgBuffer; return decryptedMsgBuffer;
} }
@ -334,15 +329,13 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEn
/** /**
* Serialize and encrypt session instance into a base64 string.<br> * Serialize and encrypt session instance into a base64 string.<br>
* @param aKeyBuffer key used to encrypt the serialized session data * @param aKeyBuffer 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 * @return a base64 string if operation succeed, null otherwise
**/ **/
JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer, jobject aErrorMsg) JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer)
{ {
jbyteArray pickledDataRet = 0; const char* errorMessage = NULL;
jclass errorMsgJClass = 0; jbyteArray pickledDataRet = 0;
jmethodID errorMsgMethodId = 0;
jbyte* keyPtr = NULL; jbyte* keyPtr = NULL;
OlmInboundGroupSession* sessionPtr = NULL; OlmInboundGroupSession* sessionPtr = NULL;
@ -351,26 +344,17 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *en
if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE(" ## serializeJni(): failure - invalid session ptr"); LOGE(" ## serializeJni(): failure - invalid session ptr");
errorMessage = "invalid session ptr";
} }
else if (!aKeyBuffer) else if (!aKeyBuffer)
{ {
LOGE(" ## serializeJni(): failure - invalid key"); LOGE(" ## serializeJni(): failure - invalid key");
} errorMessage = "invalid key";
else if (!aErrorMsg)
{
LOGE(" ## serializeJni(): failure - invalid error object");
}
else if (!(errorMsgJClass = env->GetObjectClass(aErrorMsg)))
{
LOGE(" ## serializeJni(): failure - unable to get error class");
}
else if (!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;")))
{
LOGE(" ## serializeJni(): failure - unable to get error method ID");
} }
else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, 0))) else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, 0)))
{ {
LOGE(" ## serializeJni(): failure - keyPtr JNI allocation OOM"); LOGE(" ## serializeJni(): failure - keyPtr JNI allocation OOM");
errorMessage = "keyPtr JNI allocation OOM";
} }
else else
{ {
@ -384,6 +368,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *en
if (!pickledPtr) if (!pickledPtr)
{ {
LOGE(" ## serializeJni(): failure - pickledPtr buffer OOM"); LOGE(" ## serializeJni(): failure - pickledPtr buffer OOM");
errorMessage = "pickledPtr buffer OOM";
} }
else else
{ {
@ -394,15 +379,8 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *en
pickledLength); pickledLength);
if (result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); errorMessage = olm_inbound_group_session_last_error(sessionPtr);
LOGE(" ## serializeJni(): failure - olm_pickle_outbound_group_session() Msg=%s",errorMsgPtr); LOGE(" ## serializeJni(): failure - olm_pickle_outbound_group_session() Msg=%s", errorMessage);
jstring errorJstring = env->NewStringUTF(errorMsgPtr);
if (errorJstring)
{
env->CallObjectMethod(aErrorMsg, errorMsgMethodId, errorJstring);
}
} }
else else
{ {
@ -423,6 +401,11 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *en
env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT);
} }
if (errorMessage)
{
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
}
return pickledDataRet; return pickledDataRet;
} }

View file

@ -32,12 +32,12 @@ extern "C" {
JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz); JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz); JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionJni)(JNIEnv *env, jobject thiz, jbyteArray aSessionKeyBuffer); JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionJni)(JNIEnv *env, jobject thiz, jbyteArray aSessionKeyBuffer);
JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz); JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aEncryptedMsg, jobject aDecryptIndex, jobject aErrorMsg); JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aEncryptedMsg, jobject aDecryptIndex);
// serialization // serialization
JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKey, jobject aErrorMsg); JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKey);
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedData, jbyteArray aKey); JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedData, jbyteArray aKey);